aboutsummaryrefslogtreecommitdiff
path: root/primedev/wsockproxy/loader.cpp
blob: 4664c20c31497df5f5e236077b70573a2dacb879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "loader.h"
#include <shlwapi.h>
#include <string>
#include <system_error>
#include <sstream>
#include <fstream>
#include <filesystem>
#include <iostream>

#include "MinHook.h"

namespace fs = std::filesystem;

static wchar_t northstarPath[8192];
static wchar_t exePath[4096];

bool GetExePathWide(wchar_t* dest, DWORD destSize)
{
	if (!dest)
		return NULL;
	if (destSize < MAX_PATH)
		return NULL;

	DWORD length = GetModuleFileNameW(NULL, dest, destSize);
	return length && PathRemoveFileSpecW(dest);
}


void LibraryLoadError(DWORD dwMessageId, const wchar_t* libName, const wchar_t* location)
{
	char text[4096];
	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());
	if (dwMessageId == 126 && std::filesystem::exists(location))
	{
		sprintf_s(
			text,
			"%s\n\nThe file at the specified location DOES exist, so this error indicates that one of its *dependencies* failed to be "
			"found.",
			text);
	}
	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;
	{
		std::string strProfile = "R2Northstar";
		char* clachar = strstr(GetCommandLineA(), "-profile=");
		if (clachar)
		{
			std::string cla = std::string(clachar);
			if (strncmp(cla.substr(9, 1).c_str(), "\"", 1))
			{
				size_t space = cla.find(" ");
				std::string dirname = cla.substr(9, space - 9);
				std::cout << "[*] Found profile in command line arguments: " << dirname << std::endl;
				strProfile = dirname.c_str();
			}
			else
			{
				std::string quote = "\"";
				size_t quote1 = cla.find(quote);
				size_t quote2 = (cla.substr(quote1 + 1)).find(quote);
				std::string dirname = cla.substr(quote1 + 1, quote2);
				std::cout << "[*] Found profile in command line arguments: " << dirname << std::endl;
				strProfile = dirname;
			}
		}
		else
		{
			std::cout << "[*] Profile was not found in command line arguments. Using default: R2Northstar" << std::endl;
			strProfile = "R2Northstar";
		}

		if (!GetExePathWide(exePath, 4096))
		{
			MessageBoxA(
				GetForegroundWindow(),
				"Failed getting game directory.\nThe game cannot continue and has to exit.",
				"Northstar Wsock32 Proxy Error",
				0);
			return true;
		}

		// Check if "Northstar.dll" exists in profile directory, if it doesnt fall back to root
		swprintf_s(northstarPath, L"%s\\%s\\Northstar.dll", exePath, std::wstring(strProfile.begin(), strProfile.end()).c_str());

		if (!fs::exists(fs::path(northstarPath)))
			swprintf_s(northstarPath, L"%s\\Northstar.dll", exePath);

		std::wcout << L"[*] Using: " << northstarPath << std::endl;

		HMODULE hHookModule = LoadLibraryExW(northstarPath, 0, 8u);
		if (hHookModule)
			Hook_Init = GetProcAddress(hHookModule, "InitialiseNorthstar");
		if (!hHookModule || Hook_Init == nullptr)
		{
			LibraryLoadError(GetLastError(), L"Northstar.dll", northstarPath);
			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;
	}

	LPVOID pTarget = (LPVOID)GetProcAddress(launcherHandle, "LauncherMain");
	if (MH_CreateHook(pTarget, (LPVOID)&LauncherMainHook, reinterpret_cast<LPVOID*>(&LauncherMainOriginal)) != MH_OK ||
		MH_EnableHook(pTarget) != MH_OK)
		MessageBoxA(GetForegroundWindow(), "Hook creation failed for function LauncherMain.", "Northstar Wsock32 Proxy Error", 0);

	return true;
}