From 664d5d434e8e31f8f74992f2f2b94ffd8a7609c0 Mon Sep 17 00:00:00 2001 From: p0358 Date: Sun, 2 Jan 2022 07:57:21 +0100 Subject: add wsock32 proxy!!! (it works quite nicely) --- loader_wsock32_proxy/loader.cpp | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 loader_wsock32_proxy/loader.cpp (limited to 'loader_wsock32_proxy/loader.cpp') diff --git a/loader_wsock32_proxy/loader.cpp b/loader_wsock32_proxy/loader.cpp new file mode 100644 index 00000000..19a448b2 --- /dev/null +++ b/loader_wsock32_proxy/loader.cpp @@ -0,0 +1,61 @@ +#include "pch.h" +#include "loader.h" +#include "../NorthstarDedicatedTest/hookutils.h" +#include +#include + +void LibraryLoadError(DWORD dwMessageId, const wchar_t* libName, const wchar_t* location) +{ + char text[2048]; + std::string message = std::system_category().message(dwMessageId); + sprintf_s(text, "Failed to load the %ls at \"%ls\" (%lu):\n\n%hs", libName, location, dwMessageId, message.c_str()); + MessageBoxA(GetForegroundWindow(), text, "Northstar Wsock32 Proxy Error", 0); +} + +bool LoadNorthstar() +{ + FARPROC Hook_Init = nullptr; + { + swprintf_s(dllPath, L"%s\\Northstar.dll", exePath); + auto hHookModule = LoadLibraryExW(dllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH); + if (hHookModule) Hook_Init = GetProcAddress(hHookModule, "InitialiseNorthstar"); + if (!hHookModule || Hook_Init == nullptr) + { + LibraryLoadError(GetLastError(), L"Northstar.dll", dllPath); + return false; + } + } + + ((bool (*)()) Hook_Init)(); + return true; +} + +typedef int(*LauncherMainType)(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow); +LauncherMainType LauncherMainOriginal; + +int LauncherMainHook(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) +{ + LoadNorthstar(); + return LauncherMainOriginal(hInstance, hPrevInstance, lpCmdLine, nCmdShow); +} + +bool ProvisionNorthstar() +{ + if (MH_Initialize() != MH_OK) + { + MessageBoxA(GetForegroundWindow(), "MH_Initialize failed\nThe game cannot continue and has to exit.", "Northstar Wsock32 Proxy Error", 0); + return false; + } + + auto launcherHandle = GetModuleHandleA("launcher.dll"); + if (!launcherHandle) + { + MessageBoxA(GetForegroundWindow(), "Launcher isn't loaded yet.\nThe game cannot continue and has to exit.", "Northstar Wsock32 Proxy Error", 0); + return false; + } + + HookEnabler hook; + ENABLER_CREATEHOOK(hook, GetProcAddress(launcherHandle, "LauncherMain"), &LauncherMainHook, reinterpret_cast(&LauncherMainOriginal)); + + return true; +} \ No newline at end of file -- cgit v1.2.3 From 6b46d5a0617ce2c87aa1428bcd25c304f94a2749 Mon Sep 17 00:00:00 2001 From: p0358 Date: Sun, 2 Jan 2022 08:16:44 +0100 Subject: restore ShouldLoadNorthstar() to wsock32 proxy as well --- loader_wsock32_proxy/loader.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'loader_wsock32_proxy/loader.cpp') diff --git a/loader_wsock32_proxy/loader.cpp b/loader_wsock32_proxy/loader.cpp index 19a448b2..49096bb0 100644 --- a/loader_wsock32_proxy/loader.cpp +++ b/loader_wsock32_proxy/loader.cpp @@ -3,6 +3,8 @@ #include "../NorthstarDedicatedTest/hookutils.h" #include #include +#include +#include void LibraryLoadError(DWORD dwMessageId, const wchar_t* libName, const wchar_t* location) { @@ -12,6 +14,25 @@ void LibraryLoadError(DWORD dwMessageId, const wchar_t* libName, const wchar_t* MessageBoxA(GetForegroundWindow(), text, "Northstar Wsock32 Proxy Error", 0); } +bool ShouldLoadNorthstar() +{ + bool loadNorthstar = !strstr(GetCommandLineA(), "-vanilla"); + + if (!loadNorthstar) + return loadNorthstar; + + auto runNorthstarFile = std::ifstream("run_northstar.txt"); + if (runNorthstarFile) + { + std::stringstream runNorthstarFileBuffer; + runNorthstarFileBuffer << runNorthstarFile.rdbuf(); + runNorthstarFile.close(); + if (runNorthstarFileBuffer.str()._Starts_with("0")) + loadNorthstar = false; + } + return loadNorthstar; +} + bool LoadNorthstar() { FARPROC Hook_Init = nullptr; @@ -35,7 +56,8 @@ LauncherMainType LauncherMainOriginal; int LauncherMainHook(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { - LoadNorthstar(); + if (ShouldLoadNorthstar()) + LoadNorthstar(); return LauncherMainOriginal(hInstance, hPrevInstance, lpCmdLine, nCmdShow); } -- cgit v1.2.3 From 92add9355edbe7a9b639e48077518ee4419e3d4c Mon Sep 17 00:00:00 2001 From: p0358 Date: Sun, 2 Jan 2022 09:51:35 +0100 Subject: make wsock32 proxy opt-in --- loader_wsock32_proxy/dllmain.cpp | 4 ++++ loader_wsock32_proxy/loader.cpp | 11 +++++++---- loader_wsock32_proxy/loader.h | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) (limited to 'loader_wsock32_proxy/loader.cpp') diff --git a/loader_wsock32_proxy/dllmain.cpp b/loader_wsock32_proxy/dllmain.cpp index 8e3dcd72..0b518dbe 100644 --- a/loader_wsock32_proxy/dllmain.cpp +++ b/loader_wsock32_proxy/dllmain.cpp @@ -36,6 +36,7 @@ BOOL WINAPI DllMain(HINSTANCE hInst, DWORD reason, LPVOID) if (!ProvisionNorthstar()) // does not call InitialiseNorthstar yet, will do it on LauncherMain hook return 1; + // copy the original library for system to our local directory, with changed name so that we can load it swprintf_s(dllPath, L"%s\\bin\\x64_retail\\wsock32.org.dll", exePath); GetSystemDirectoryW(dllPath2, 4096); swprintf_s(dllPath2, L"%s\\wsock32.dll", dllPath2); @@ -54,6 +55,9 @@ BOOL WINAPI DllMain(HINSTANCE hInst, DWORD reason, LPVOID) hL = LoadLibraryExW(dllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH); if (!hL) return false; + // load the functions to proxy + // it's only some of them, because in case of wsock32 most of the functions can actually be natively redirected + // (see wsock32.def and https://source.winehq.org/WineAPI/wsock32.html) p[1] = GetProcAddress(hL, "EnumProtocolsA"); p[2] = GetProcAddress(hL, "EnumProtocolsW"); p[4] = GetProcAddress(hL, "GetAddressByNameA"); diff --git a/loader_wsock32_proxy/loader.cpp b/loader_wsock32_proxy/loader.cpp index 49096bb0..907caa9d 100644 --- a/loader_wsock32_proxy/loader.cpp +++ b/loader_wsock32_proxy/loader.cpp @@ -16,9 +16,9 @@ void LibraryLoadError(DWORD dwMessageId, const wchar_t* libName, const wchar_t* bool ShouldLoadNorthstar() { - bool loadNorthstar = !strstr(GetCommandLineA(), "-vanilla"); + bool loadNorthstar = strstr(GetCommandLineA(), "-northstar"); - if (!loadNorthstar) + if (loadNorthstar) return loadNorthstar; auto runNorthstarFile = std::ifstream("run_northstar.txt"); @@ -27,8 +27,8 @@ bool ShouldLoadNorthstar() std::stringstream runNorthstarFileBuffer; runNorthstarFileBuffer << runNorthstarFile.rdbuf(); runNorthstarFile.close(); - if (runNorthstarFileBuffer.str()._Starts_with("0")) - loadNorthstar = false; + if (!runNorthstarFileBuffer.str()._Starts_with("0")) + loadNorthstar = true; } return loadNorthstar; } @@ -63,6 +63,9 @@ int LauncherMainHook(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi bool ProvisionNorthstar() { + if (!ShouldLoadNorthstar()) + return true; + if (MH_Initialize() != MH_OK) { MessageBoxA(GetForegroundWindow(), "MH_Initialize failed\nThe game cannot continue and has to exit.", "Northstar Wsock32 Proxy Error", 0); diff --git a/loader_wsock32_proxy/loader.h b/loader_wsock32_proxy/loader.h index 02ccb97d..9a14b2e7 100644 --- a/loader_wsock32_proxy/loader.h +++ b/loader_wsock32_proxy/loader.h @@ -4,4 +4,5 @@ extern wchar_t exePath[4096]; extern wchar_t dllPath[8192]; extern wchar_t dllPath2[4096]; +bool ShouldLoadNorthstar(); bool ProvisionNorthstar(); -- cgit v1.2.3