diff --git a/LunaHost/LunaHostDll.cpp b/LunaHost/LunaHostDll.cpp index 14dbf26..cdf0c68 100644 --- a/LunaHost/LunaHostDll.cpp +++ b/LunaHost/LunaHostDll.cpp @@ -16,51 +16,37 @@ BOOL APIENTRY DllMain(HMODULE hModule, } typedef void (*ProcessEvent)(DWORD); -typedef void (*ThreadEvent)(wchar_t *, char *, ThreadParam); -typedef bool (*OutputCallback)(wchar_t *, char *, ThreadParam, const wchar_t *); +typedef void (*ThreadEvent)(const wchar_t *, const char *, ThreadParam); +typedef bool (*OutputCallback)(const wchar_t *, const char *, ThreadParam, const wchar_t *); typedef void (*ConsoleHandler)(const wchar_t *); typedef void (*HookInsertHandler)(uint64_t, const wchar_t *); typedef void (*EmbedCallback)(const wchar_t *, ThreadParam); -#define XXXX \ - char name[HOOK_NAME_SIZE]; \ - wchar_t hookcode[HOOKCODE_LEN]; \ - wcscpy_s(hookcode, HOOKCODE_LEN, thread.hp.hookcode); \ - strcpy_s(name, HOOK_NAME_SIZE, thread.hp.name); +template +std::optional checkoption(bool check, T &&t) +{ + if (check) + return std::move(t); + return {}; +} C_LUNA_API void Luna_Start(ProcessEvent Connect, ProcessEvent Disconnect, ThreadEvent Create, ThreadEvent Destroy, OutputCallback Output, ConsoleHandler console, HookInsertHandler hookinsert, EmbedCallback embed, ConsoleHandler Warning) { Host::StartEx( - Connect, - Disconnect, - [=](const TextThread &thread) - { - XXXX - Create(hookcode, name, thread.tp); - }, - [=](const TextThread &thread) - { - XXXX - Destroy(hookcode, name, thread.tp); - }, - [=](const TextThread &thread, std::wstring &output) - { - XXXX return Output(hookcode, name, thread.tp, output.c_str()); - }, - [=](const std::wstring &output) - { - console(output.c_str()); - }, - [=](uint64_t addr, const std::wstring &output) - { - hookinsert(addr, output.c_str()); - }, - [=](const std::wstring &output, const ThreadParam &tp) - { - embed(output.c_str(), tp); - }, - [=](const std::wstring &output) - { - Warning(output.c_str()); - }); + checkoption(Connect, std::function(Connect)), + checkoption(Disconnect, std::function(Disconnect)), + checkoption(Create, [=](const TextThread &thread) + { Create(thread.hp.hookcode, thread.hp.name, thread.tp); }), + checkoption(Destroy, [=](const TextThread &thread) + { Destroy(thread.hp.hookcode, thread.hp.name, thread.tp); }), + checkoption(Output, [=](const TextThread &thread, std::wstring &output) + { return Output(thread.hp.hookcode, thread.hp.name, thread.tp, output.c_str()); }), + checkoption(console, [=](const std::wstring &output) + { console(output.c_str()); }), + checkoption(hookinsert, [=](uint64_t addr, const std::wstring &output) + { hookinsert(addr, output.c_str()); }), + checkoption(embed, [=](const std::wstring &output, const ThreadParam &tp) + { embed(output.c_str(), tp); }), + checkoption(Warning, [=](const std::wstring &output) + { Warning(output.c_str()); })); } C_LUNA_API void Luna_Inject(DWORD pid, LPCWSTR basepath) { diff --git a/LunaHost/host.cpp b/LunaHost/host.cpp index 88977c9..75776f7 100644 --- a/LunaHost/host.cpp +++ b/LunaHost/host.cpp @@ -217,18 +217,20 @@ namespace Host // CreatePipe(); } - void StartEx(ProcessEventHandler Connect, ProcessEventHandler Disconnect, ThreadEventHandler Create, ThreadEventHandler Destroy, TextThread::OutputCallback Output, std::optional console, std::optional hookinsert, std::optional embed, std::optional warning) + void StartEx(std::optional Connect, std::optional Disconnect, std::optional Create, std::optional Destroy, std::optional Output, std::optional console, std::optional hookinsert, std::optional embed, std::optional warning) { - Start(Connect, Disconnect, Create, Destroy, Output, !console.has_value()); - if (warning.has_value()) + Start(Connect.value_or([](auto) {}), Disconnect.value_or([](auto) {}), Create.value_or([](auto &) {}), Destroy.value_or([](auto &) {}), Output.value_or([](auto &, auto &) + { return false; }), + !console); + if (warning) OnWarning = warning.value(); - if (console.has_value()) + if (console) OnConsole = [=](auto &&...args) {std::lock_guard _(outputmutex);console.value()(std::forward(args)...); }; - if (hookinsert.has_value()) + if (hookinsert) HookInsert = [=](auto &&...args) {std::lock_guard _(threadmutex);hookinsert.value()(std::forward(args)...); }; - if (embed.has_value()) + if (embed) embedcallback = [=](auto &&...args) {std::lock_guard _(outputmutex);embed.value()(std::forward(args)...); }; } diff --git a/LunaHost/host.h b/LunaHost/host.h index 3b53e4f..ee7d021 100644 --- a/LunaHost/host.h +++ b/LunaHost/host.h @@ -10,7 +10,7 @@ namespace Host using HookInsertHandler = std::function; using EmbedCallback = std::function; void Start(ProcessEventHandler Connect, ProcessEventHandler Disconnect, ThreadEventHandler Create, ThreadEventHandler Destroy, TextThread::OutputCallback Output, bool createconsole = true); - void StartEx(ProcessEventHandler Connect, ProcessEventHandler Disconnect, ThreadEventHandler Create, ThreadEventHandler Destroy, TextThread::OutputCallback Output, std::optional console, std::optional hookinsert, std::optional embed, std::optional warning); + void StartEx(std::optional Connect, std::optional Disconnect, std::optional Create, std::optional Destroy, std::optional Output, std::optional console, std::optional hookinsert, std::optional embed, std::optional warning); void InjectProcess(DWORD processId, const std::wstring locationX = L""); bool CreatePipeAndCheck(DWORD processId);