aboutsummaryrefslogtreecommitdiff
path: root/loader_wsock32_proxy/loader.cpp
diff options
context:
space:
mode:
authorHappyDOGE <28511119+HappyDOGE@users.noreply.github.com>2022-01-03 14:33:16 +0300
committerHappyDOGE <28511119+HappyDOGE@users.noreply.github.com>2022-01-03 14:33:16 +0300
commitf3ffed0742d62adf48c37dca0acfad621724c21b (patch)
tree7e78b6f15bb4cd0f54fa982446061b0b39643e0c /loader_wsock32_proxy/loader.cpp
parent2942ea56527100d5e39e0bbba4a1d16b1cde6128 (diff)
parent966f5052b7b59fc7200eb736c8d393056e0389cd (diff)
downloadNorthstarLauncher-f3ffed0742d62adf48c37dca0acfad621724c21b.tar.gz
NorthstarLauncher-f3ffed0742d62adf48c37dca0acfad621724c21b.zip
merge with upstream
Diffstat (limited to 'loader_wsock32_proxy/loader.cpp')
-rw-r--r--loader_wsock32_proxy/loader.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/loader_wsock32_proxy/loader.cpp b/loader_wsock32_proxy/loader.cpp
new file mode 100644
index 00000000..907caa9d
--- /dev/null
+++ b/loader_wsock32_proxy/loader.cpp
@@ -0,0 +1,86 @@
+#include "pch.h"
+#include "loader.h"
+#include "../NorthstarDedicatedTest/hookutils.h"
+#include <string>
+#include <system_error>
+#include <sstream>
+#include <fstream>
+
+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 ShouldLoadNorthstar()
+{
+ bool loadNorthstar = strstr(GetCommandLineA(), "-northstar");
+
+ 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 = true;
+ }
+ return loadNorthstar;
+}
+
+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)
+{
+ if (ShouldLoadNorthstar())
+ LoadNorthstar();
+ return LauncherMainOriginal(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
+}
+
+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);
+ 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<LPVOID*>(&LauncherMainOriginal));
+
+ return true;
+} \ No newline at end of file