2024-04-02 15:36:52 +08:00
|
|
|
|
2024-11-04 23:10:41 +08:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2024-04-02 15:36:52 +08:00
|
|
|
HANDLE runexe(const std::wstring &exe, const std::optional<std::wstring> &startup_argument)
|
2024-03-23 18:33:58 +08:00
|
|
|
{
|
|
|
|
STARTUPINFOW si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
2024-04-02 15:36:52 +08:00
|
|
|
ZeroMemory(&si, sizeof(si));
|
2024-03-23 18:33:58 +08:00
|
|
|
si.cb = sizeof(si);
|
2024-04-02 15:36:52 +08:00
|
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
|
|
|
|
std::vector<wchar_t> argu;
|
|
|
|
if (startup_argument.has_value())
|
|
|
|
{
|
|
|
|
argu.resize(startup_argument.value().size() + 1);
|
|
|
|
wcscpy(argu.data(), startup_argument.value().c_str());
|
2024-03-23 18:33:58 +08:00
|
|
|
}
|
2024-04-02 15:36:52 +08:00
|
|
|
CreateProcessW(exe.c_str(), // No module name (use command line)
|
|
|
|
startup_argument.has_value() ? argu.data() : NULL,
|
|
|
|
NULL, // Process handle not inheritable
|
|
|
|
NULL, // Thread handle not inheritable
|
|
|
|
FALSE, // Set handle inheritance to FALSE
|
|
|
|
0, // No creation flags
|
|
|
|
NULL, // Use parent's environment block
|
|
|
|
NULL, // Use parent's starting directory
|
|
|
|
&si, // Pointer to STARTUPINFO structure
|
|
|
|
&pi); // Pointer to PROCESS_INFORMATION structure
|
|
|
|
|
2024-03-23 18:33:58 +08:00
|
|
|
return pi.hProcess;
|
|
|
|
}
|
|
|
|
|
2024-04-02 15:36:52 +08:00
|
|
|
std::wstring stolower(const std::wstring &s1)
|
|
|
|
{
|
|
|
|
auto s = s1;
|
|
|
|
std::transform(s.begin(), s.end(), s.begin(), tolower);
|
|
|
|
return s;
|
2024-03-23 18:33:58 +08:00
|
|
|
}
|
2024-04-02 15:36:52 +08:00
|
|
|
std::vector<DWORD> EnumerateProcesses(const std::wstring &exe)
|
2024-03-23 18:33:58 +08:00
|
|
|
{
|
2024-04-02 15:36:52 +08:00
|
|
|
|
2024-03-23 18:33:58 +08:00
|
|
|
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
if (hSnapshot == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2024-04-02 15:36:52 +08:00
|
|
|
|
2024-03-23 18:33:58 +08:00
|
|
|
PROCESSENTRY32 pe32;
|
|
|
|
pe32.dwSize = sizeof(PROCESSENTRY32);
|
|
|
|
|
|
|
|
if (!Process32First(hSnapshot, &pe32))
|
|
|
|
{
|
|
|
|
CloseHandle(hSnapshot);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::vector<DWORD> pids;
|
|
|
|
do
|
|
|
|
{
|
2024-04-02 15:36:52 +08:00
|
|
|
if (stolower(exe) == stolower(pe32.szExeFile))
|
2024-03-23 18:33:58 +08:00
|
|
|
pids.push_back(pe32.th32ProcessID);
|
|
|
|
} while (Process32Next(hSnapshot, &pe32));
|
2024-04-02 15:36:52 +08:00
|
|
|
|
2024-03-23 18:33:58 +08:00
|
|
|
CloseHandle(hSnapshot);
|
|
|
|
return pids;
|
|
|
|
}
|
2024-04-02 15:36:52 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
STRING = 12,
|
|
|
|
MESSAGE_SIZE = 500,
|
|
|
|
PIPE_BUFFER_SIZE = 50000,
|
|
|
|
SHIFT_JIS = 932,
|
|
|
|
MAX_MODULE_SIZE = 120,
|
|
|
|
PATTERN_SIZE = 30,
|
|
|
|
HOOK_NAME_SIZE = 60,
|
|
|
|
FIXED_SPLIT_VALUE = 0x10001,
|
|
|
|
HOOKCODE_LEN = 500
|
|
|
|
};
|
2024-03-23 18:33:58 +08:00
|
|
|
struct ThreadParam
|
|
|
|
{
|
2024-04-02 15:36:52 +08:00
|
|
|
bool operator==(ThreadParam other) const { return processId == other.processId && addr == other.addr && ctx == other.ctx && ctx2 == other.ctx2; }
|
|
|
|
DWORD processId;
|
|
|
|
uint64_t addr;
|
|
|
|
uint64_t ctx; // The context of the hook: by default the first value on stack, usually the return address
|
|
|
|
uint64_t ctx2; // The subcontext of the hook: 0 by default, generated in a method specific to the hook
|
2024-03-23 18:33:58 +08:00
|
|
|
};
|
2024-11-28 16:59:08 +08:00
|
|
|
|
|
|
|
typedef void (*ProcessEvent)(DWORD pid);
|
|
|
|
typedef void (*HookInsertHandler)(DWORD pid, uint64_t address, const wchar_t *hookcode);
|
|
|
|
typedef void (*EmbedCallback)(const wchar_t *text, ThreadParam);
|
2024-04-30 12:32:49 +08:00
|
|
|
nlohmann::json config;
|
|
|
|
std::map<std::string, std::string> translation;
|
|
|
|
std::unordered_set<DWORD> connectedpids;
|
2024-11-28 16:59:08 +08:00
|
|
|
void (*Luna_Start)(ProcessEvent Connect, ProcessEvent Disconnect, void *, void *, void *, void *, HookInsertHandler hookinsert, EmbedCallback embed);
|
2024-04-30 12:32:49 +08:00
|
|
|
void (*Luna_Inject)(DWORD pid, LPCWSTR basepath);
|
2024-12-21 22:46:35 +08:00
|
|
|
void (*Luna_EmbedSettings)(DWORD pid, UINT32 waittime, UINT8 fontCharSet, bool fontCharSetEnabled, wchar_t *fontFamily, int displaymode, bool fastskipignore);
|
2024-11-03 20:31:31 +08:00
|
|
|
void (*Luna_useembed)(ThreadParam, bool use);
|
2024-11-03 23:59:43 +08:00
|
|
|
void (*Luna_embedcallback)(ThreadParam, LPCWSTR text, LPCWSTR trans);
|
2024-04-30 12:32:49 +08:00
|
|
|
std::set<std::string> notranslation;
|
|
|
|
HANDLE hsema;
|
2024-04-02 15:36:52 +08:00
|
|
|
class lunapatch
|
|
|
|
{
|
2024-03-23 18:33:58 +08:00
|
|
|
public:
|
|
|
|
HANDLE hMessage;
|
|
|
|
HANDLE hwait;
|
2024-04-30 12:32:49 +08:00
|
|
|
|
|
|
|
lunapatch(std::wstring dll, nlohmann::json &&_translation, nlohmann::json &&_config)
|
2024-04-02 15:36:52 +08:00
|
|
|
{
|
2024-04-30 12:32:49 +08:00
|
|
|
translation = _translation;
|
|
|
|
config = _config;
|
2024-04-02 15:36:52 +08:00
|
|
|
auto LunaHost = LoadLibraryW(dll.c_str());
|
|
|
|
|
|
|
|
Luna_Start = (decltype(Luna_Start))GetProcAddress(LunaHost, "Luna_Start");
|
|
|
|
Luna_EmbedSettings = (decltype(Luna_EmbedSettings))GetProcAddress(LunaHost, "Luna_EmbedSettings");
|
|
|
|
Luna_Inject = (decltype(Luna_Inject))GetProcAddress(LunaHost, "Luna_Inject");
|
|
|
|
Luna_useembed = (decltype(Luna_useembed))GetProcAddress(LunaHost, "Luna_useembed");
|
|
|
|
Luna_embedcallback = (decltype(Luna_embedcallback))GetProcAddress(LunaHost, "Luna_embedcallback");
|
|
|
|
hsema = CreateSemaphore(NULL, 0, 100, NULL);
|
2024-04-30 12:32:49 +08:00
|
|
|
Luna_Start(
|
|
|
|
[](DWORD pid)
|
|
|
|
{
|
2024-12-21 22:46:35 +08:00
|
|
|
Luna_EmbedSettings(pid, 1000 * config["embedsettings"]["timeout_translate"], 2, false, config["embedsettings"]["changefont"] ? (StringToWideString(config["embedsettings"]["changefont_font"]).data()) : L"", config["embedsettings"]["displaymode"], false);
|
2024-04-30 12:32:49 +08:00
|
|
|
connectedpids.insert(pid);
|
|
|
|
},
|
|
|
|
[](DWORD pid)
|
|
|
|
{
|
|
|
|
connectedpids.erase(pid);
|
|
|
|
ReleaseSemaphore(hsema, 1, NULL);
|
|
|
|
},
|
2024-10-06 00:51:50 +08:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2024-11-03 20:31:31 +08:00
|
|
|
[](DWORD pid, uint64_t addr, const wchar_t *output)
|
2024-04-30 12:32:49 +08:00
|
|
|
{
|
|
|
|
std::wstring newhookcode = output;
|
|
|
|
for (auto hook : config["embedhook"])
|
|
|
|
{
|
|
|
|
auto hookcode = StringToWideString(hook[0]);
|
|
|
|
uint64_t _addr = hook[1];
|
|
|
|
uint64_t _ctx1 = hook[2];
|
|
|
|
uint64_t _ctx2 = hook[3];
|
|
|
|
if (hookcode == newhookcode)
|
|
|
|
{
|
2024-11-03 20:31:31 +08:00
|
|
|
ThreadParam tp{pid, addr, _ctx1, _ctx2};
|
|
|
|
Luna_useembed(tp, true);
|
2024-04-30 12:32:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[](const wchar_t *output, ThreadParam tp)
|
|
|
|
{
|
|
|
|
std::wstring text = output;
|
2024-11-03 23:59:43 +08:00
|
|
|
auto trans = findtranslation(text);
|
|
|
|
Luna_embedcallback(tp, output, trans.c_str());
|
2024-11-28 16:59:08 +08:00
|
|
|
});
|
2024-03-23 18:33:58 +08:00
|
|
|
}
|
2024-04-02 15:36:52 +08:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
auto target_exe = StringToWideString(config["target_exe"]);
|
|
|
|
|
|
|
|
auto _startup_argument = config["startup_argument"];
|
2024-03-23 18:33:58 +08:00
|
|
|
|
|
|
|
std::optional<std::wstring> startup_argument;
|
2024-04-02 15:36:52 +08:00
|
|
|
if (_startup_argument.is_null())
|
|
|
|
startup_argument = {};
|
2024-03-23 18:33:58 +08:00
|
|
|
else
|
2024-04-02 15:36:52 +08:00
|
|
|
startup_argument = StringToWideString(config["startup_argument"]);
|
|
|
|
hwait = runexe(target_exe, startup_argument);
|
2024-03-23 18:33:58 +08:00
|
|
|
}
|
2024-04-02 15:36:52 +08:00
|
|
|
~lunapatch()
|
|
|
|
{
|
|
|
|
if (notranslation.size())
|
|
|
|
{
|
|
|
|
for (auto &text : notranslation)
|
|
|
|
{
|
|
|
|
translation[text] = "";
|
2024-03-23 18:33:58 +08:00
|
|
|
}
|
2024-04-02 15:36:52 +08:00
|
|
|
auto notrs = nlohmann::json(translation).dump(4);
|
2024-03-23 18:33:58 +08:00
|
|
|
std::ofstream of;
|
2024-03-23 20:24:03 +08:00
|
|
|
of.open(std::string(config["translation_file"]));
|
2024-04-02 15:36:52 +08:00
|
|
|
of << notrs;
|
2024-03-23 18:33:58 +08:00
|
|
|
of.close();
|
|
|
|
}
|
|
|
|
}
|
2024-04-02 15:36:52 +08:00
|
|
|
void wait()
|
|
|
|
{
|
|
|
|
WaitForSingleObject(hwait, INFINITE);
|
|
|
|
while (connectedpids.size())
|
|
|
|
WaitForSingleObject(hsema, INFINITE);
|
2024-03-23 18:33:58 +08:00
|
|
|
}
|
2024-04-02 15:36:52 +08:00
|
|
|
void inject()
|
|
|
|
{
|
|
|
|
// chrome has multi process
|
2024-03-23 18:33:58 +08:00
|
|
|
Sleep(config["inject_timeout"]);
|
2024-04-02 15:36:52 +08:00
|
|
|
for (auto exe : std::set<std::string>{config["target_exe"], config["target_exe2"]})
|
2024-03-23 20:24:03 +08:00
|
|
|
{
|
2024-04-02 15:36:52 +08:00
|
|
|
auto pids = EnumerateProcesses(StringToWideString(exe));
|
|
|
|
for (auto pid : pids)
|
|
|
|
{
|
|
|
|
wprintf(L"%d\n", pid);
|
|
|
|
Luna_Inject(pid, L"");
|
2024-03-23 20:24:03 +08:00
|
|
|
}
|
2024-03-23 18:33:58 +08:00
|
|
|
}
|
|
|
|
}
|
2024-04-30 12:32:49 +08:00
|
|
|
static std::wstring findtranslation(const std::wstring &text)
|
2024-04-02 15:36:52 +08:00
|
|
|
{
|
|
|
|
auto utf8text = WideStringToString(text);
|
|
|
|
if (translation.find(utf8text) == translation.end())
|
|
|
|
{
|
|
|
|
// wprintf(L"%s\n",text.c_str());
|
2024-03-23 18:33:58 +08:00
|
|
|
notranslation.insert(utf8text);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return StringToWideString(translation.at(utf8text));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::wstring GetExecutablePath()
|
|
|
|
{
|
|
|
|
WCHAR buffer[MAX_PATH];
|
|
|
|
GetModuleFileNameW(NULL, buffer, MAX_PATH);
|
|
|
|
|
|
|
|
std::wstring fullPath(buffer);
|
|
|
|
size_t pos = fullPath.find_last_of(L"\\/");
|
|
|
|
if (pos != std::wstring::npos)
|
|
|
|
{
|
|
|
|
return fullPath.substr(0, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return L"";
|
|
|
|
}
|
2024-04-02 15:36:52 +08:00
|
|
|
bool checkisapatch()
|
|
|
|
{
|
|
|
|
auto curr = std::filesystem::path(GetExecutablePath());
|
|
|
|
auto config = curr / "LunaPatch.json";
|
|
|
|
if (std::filesystem::exists(config) == false)
|
2024-03-23 18:33:58 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::ifstream jsonfile;
|
|
|
|
jsonfile.open(config);
|
2024-04-02 15:36:52 +08:00
|
|
|
auto configjson = nlohmann::json::parse(jsonfile);
|
2024-03-23 18:33:58 +08:00
|
|
|
jsonfile.close();
|
2024-04-02 15:36:52 +08:00
|
|
|
std::string translation_file = configjson["translation_file"];
|
|
|
|
|
2024-03-23 18:33:58 +08:00
|
|
|
jsonfile.open(translation_file);
|
2024-04-02 15:36:52 +08:00
|
|
|
std::map<std::string, std::string> translation = nlohmann::json::parse(jsonfile);
|
2024-03-23 18:33:58 +08:00
|
|
|
jsonfile.close();
|
|
|
|
|
2024-04-02 15:36:52 +08:00
|
|
|
auto LunaHost = (curr / (std::wstring(L"LunaHost") + std::to_wstring(8 * sizeof(void *)))).wstring();
|
2024-03-23 18:33:58 +08:00
|
|
|
|
2024-04-02 15:36:52 +08:00
|
|
|
lunapatch _lunapatch(LunaHost, std::move(translation), std::move(configjson));
|
2024-03-23 18:33:58 +08:00
|
|
|
_lunapatch.run();
|
|
|
|
_lunapatch.inject();
|
|
|
|
_lunapatch.wait();
|
|
|
|
return true;
|
|
|
|
}
|