injector refactoring

This commit is contained in:
NukedBart 2025-12-10 23:07:02 +08:00
parent b8971674ed
commit 444ccad844
14 changed files with 845 additions and 514 deletions

55
operator/core/FilePath.h Normal file
View file

@ -0,0 +1,55 @@
// core/FilePath.h
#pragma once
#include <windows.h>
#include <string>
namespace FilePath
{
/// @brief Gets the current executable's directory in UTF-8.
inline std::string GetExecutableDirectory()
{
wchar_t wpath[MAX_PATH] = { 0 };
if (!GetModuleFileNameW(nullptr, wpath, MAX_PATH))
return {};
std::wstring ws(wpath);
size_t pos = ws.rfind(L'\\');
if (pos == std::wstring::npos)
return {};
ws.resize(pos + 1); // keep the \
int size = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (size <= 1) return {};
std::string result(size - 1, 0);
WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), -1, result.data(), size, nullptr, nullptr);
return result;
}
/// @brief Converts UTF-8 to std::wstring
inline std::wstring Utf8ToWide(const std::string_view utf8)
{
if (utf8.empty()) return {};
int n = MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.length()), nullptr, 0);
if (n == 0) return {};
std::wstring wide(n, L'\0');
MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.length()), wide.data(), n);
return wide;
}
/// @brief Converts std::wstring to UTF-8
inline std::string WideToUtf8(std::wstring_view wide)
{
if (wide.empty()) return {};
int n = WideCharToMultiByte(CP_UTF8, 0, wide.data(), static_cast<int>(wide.length()), nullptr, 0, nullptr, nullptr);
if (n == 0) return {};
std::string utf8(n, 0);
WideCharToMultiByte(CP_UTF8, 0, wide.data(), static_cast<int>(wide.length()), utf8.data(), n, nullptr, nullptr);
return utf8;
}
}

3
operator/core/Logger.cpp Normal file
View file

@ -0,0 +1,3 @@
#include "Logger.h"
std::mutex Logger::mtx;

63
operator/core/Logger.h Normal file
View file

@ -0,0 +1,63 @@
// core/Logger.h
#pragma once
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <chrono>
#include <mutex>
#include <windows.h>
enum class LogLevel { Debug, Info, Warning, Error, Fatal };
#define LOG_D(msg) Logger::Log(LogLevel::Debug, CURRENT_MODULE, __func__, msg)
#define LOG_I(msg) Logger::Log(LogLevel::Info, CURRENT_MODULE, __func__, msg)
#define LOG_W(msg) Logger::Log(LogLevel::Warning, CURRENT_MODULE, __func__, msg)
#define LOG_E(msg) Logger::Log(LogLevel::Error, CURRENT_MODULE, __func__, msg)
#define LOG_F(msg) Logger::Log(LogLevel::Fatal, CURRENT_MODULE, __func__, msg)
class Logger
{
static std::mutex mtx;
static std::string GetTime()
{
auto now = std::chrono::system_clock::now();
auto tt = std::chrono::system_clock::to_time_t(now);
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
tm local{};
localtime_s(&local, &tt);
std::ostringstream oss;
oss << std::put_time(&local, "%Y-%m-%d %H:%M:%S")
<< '.' << std::setfill('0') << std::setw(3) << ms.count();
return oss.str();
}
static char LevelChar(LogLevel lv)
{
switch (lv)
{
case LogLevel::Debug: return 'D';
case LogLevel::Info: return 'I';
case LogLevel::Warning: return 'W';
case LogLevel::Error: return 'E';
case LogLevel::Fatal: return 'F';
default: return '?';
}
}
public:
template<typename... Args>
static void Log(LogLevel level, const char* module, const char* func, const std::string& message)
{
std::lock_guard<std::mutex> lock(mtx);
std::ostringstream oss;
oss << "[" << GetTime() << "] "
<< std::left << std::setw(40) << (std::string(module) + "::" + func + "()")
<< LevelChar(level) << " : " << message;
std::cout << oss.str() << std::endl;
OutputDebugStringA((oss.str() + "\n").c_str());
}
};

45
operator/core/Types.h Normal file
View file

@ -0,0 +1,45 @@
// core/Types.h
#pragma once
#include <windows.h>
typedef int PROCESSINFOCLASS;
typedef struct _UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING, *PUNICODE_STRING;
typedef struct _RTL_USER_PROCESS_PARAMETERS {
BYTE Reserved1[16];
PVOID Reserved2[10];
UNICODE_STRING ImagePathName;
UNICODE_STRING CommandLine;
} RTL_USER_PROCESS_PARAMETERS, *PRTL_USER_PROCESS_PARAMETERS;
typedef struct _PEB {
BYTE Reserved1[2];
BYTE BeingDebugged;
BYTE Reserved2[1];
PVOID Reserved3[2];
PVOID Ldr;
PRTL_USER_PROCESS_PARAMETERS ProcessParameters;
} PEB, *PPEB;
typedef struct _PROCESS_BASIC_INFORMATION {
PVOID Reserved1;
PPEB PebBaseAddress;
PVOID Reserved2[2];
ULONG_PTR UniqueProcessId;
PVOID Reserved3;
} PROCESS_BASIC_INFORMATION;
#define ProcessBasicInformation 0
typedef LONG(NTAPI* NtQueryInformationProcess_t)(
HANDLE ProcessHandle,
PROCESSINFOCLASS ProcessInformationClass,
PVOID ProcessInformation,
ULONG ProcessInformationLength,
PULONG ReturnLength
);