Skip to content

Commit 30e58ac

Browse files
authored
Clean up wsock proxy code and move wsock build system logic (#671)
- moves `WSockProxy` to `wsockproxy/CmakeLists` - remove exepath stuff from dllmain + its still done in loader.cpp because its used when reporting failure - Disabled any Thread Library calls + we don't need to know about threads at all in the proxy - yoink `wsock32.asm` into outer space + turns out, we can just call the function in a void shim since that wont touch the registers - stop copying `wsock32.dll` to the game directory + this should improve the state of things when using the EA App
1 parent fc63948 commit 30e58ac

File tree

5 files changed

+59
-152
lines changed

5 files changed

+59
-152
lines changed

primedev/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
include(Northstar.cmake)
22
include(Launcher.cmake)
3-
include(WSockProxy.cmake)
3+
add_subdirectory(wsockproxy)

primedev/WSockProxy.cmake primedev/wsockproxy/CMakeLists.txt

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ find_package(minhook REQUIRED)
44

55
add_library(
66
loader_wsock32_proxy SHARED
7-
"wsockproxy/dllmain.cpp"
8-
"wsockproxy/loader.cpp"
9-
"wsockproxy/loader.h"
10-
"wsockproxy/wsock32.asm"
11-
"wsockproxy/wsock32.def"
7+
"dllmain.cpp"
8+
"loader.cpp"
9+
"loader.h"
10+
"wsock32.def"
1211
)
1312

1413
target_link_libraries(
@@ -36,7 +35,7 @@ target_link_libraries(
3635
target_precompile_headers(
3736
loader_wsock32_proxy
3837
PRIVATE
39-
wsockproxy/pch.h
38+
pch.h
4039
)
4140

4241
target_compile_definitions(loader_wsock32_proxy PRIVATE UNICODE _UNICODE)

primedev/wsockproxy/dllmain.cpp

+22-131
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,29 @@
11
#include "loader.h"
22

3-
#include <shlwapi.h>
43
#include <filesystem>
54

6-
HINSTANCE hLThis = 0;
7-
FARPROC p[857];
8-
HINSTANCE hL = 0;
5+
FARPROC p[73];
6+
HMODULE hL = 0;
97

10-
bool GetExePathWide(wchar_t* dest, DWORD destSize)
11-
{
12-
if (!dest)
13-
return NULL;
14-
if (destSize < MAX_PATH)
15-
return NULL;
16-
17-
DWORD length = GetModuleFileNameW(NULL, dest, destSize);
18-
return length && PathRemoveFileSpecW(dest);
19-
}
20-
21-
wchar_t exePath[4096];
22-
wchar_t buffer1[8192];
23-
wchar_t buffer2[12288];
8+
static wchar_t wsockPath[4096];
249

2510
BOOL WINAPI DllMain(HINSTANCE hInst, DWORD reason, LPVOID)
2611
{
2712
if (reason == DLL_PROCESS_ATTACH)
2813
{
29-
hLThis = hInst;
30-
31-
if (!GetExePathWide(exePath, 4096))
32-
{
33-
MessageBoxA(
34-
GetForegroundWindow(),
35-
"Failed getting game directory.\nThe game cannot continue and has to exit.",
36-
"Northstar Wsock32 Proxy Error",
37-
0);
38-
return true;
39-
}
40-
41-
SetCurrentDirectoryW(exePath);
14+
// Tell the OS we don't need to know about threads
15+
DisableThreadLibraryCalls(hInst);
4216

4317
if (!ProvisionNorthstar()) // does not call InitialiseNorthstar yet, will do it on LauncherMain hook
4418
return true;
4519

46-
// copy the original library for system to our local directory, with changed name so that we can load it
47-
swprintf_s(buffer1, L"%s\\bin\\x64_retail\\wsock32.org.dll", exePath);
48-
GetSystemDirectoryW(buffer2, 4096);
49-
swprintf_s(buffer2, L"%s\\wsock32.dll", buffer2);
50-
try
51-
{
52-
std::filesystem::copy_file(buffer2, buffer1);
53-
}
54-
catch (const std::exception& e1)
55-
{
56-
if (!std::filesystem::exists(buffer1))
57-
{
58-
// fallback by copying to temp dir...
59-
// because apparently games installed by EA Desktop app don't have write permissions in their directories
60-
auto temp_dir = std::filesystem::temp_directory_path() / L"wsock32.org.dll";
61-
try
62-
{
63-
std::filesystem::copy_file(buffer2, temp_dir);
64-
}
65-
catch (const std::exception& e2)
66-
{
67-
if (!std::filesystem::exists(temp_dir))
68-
{
69-
swprintf_s(
70-
buffer2,
71-
L"Failed copying wsock32.dll from system32 to \"%s\"\n\n%S\n\nFurthermore, we failed copying wsock32.dll into "
72-
L"temporary directory at \"%s\"\n\n%S",
73-
buffer1,
74-
e1.what(),
75-
temp_dir.c_str(),
76-
e2.what());
77-
MessageBoxW(GetForegroundWindow(), buffer2, L"Northstar Wsock32 Proxy Error", 0);
78-
return false;
79-
}
80-
}
81-
swprintf_s(buffer1, L"%s", temp_dir.c_str());
82-
}
83-
}
84-
hL = LoadLibraryExW(buffer1, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
20+
GetSystemDirectoryW(wsockPath, 4096);
21+
swprintf_s(wsockPath, 4096, L"%s\\wsock32.dll", wsockPath);
22+
23+
hL = LoadLibraryExW(wsockPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
8524
if (!hL)
8625
{
87-
LibraryLoadError(GetLastError(), L"wsock32.org.dll", buffer1);
26+
LibraryLoadError(GetLastError(), L"wsock32.dll", wsockPath);
8827
return false;
8928
}
9029

@@ -119,64 +58,16 @@ extern "C"
11958
FARPROC PA = NULL;
12059
int RunASM();
12160

122-
void PROXY_EnumProtocolsA()
123-
{
124-
PA = p[1];
125-
RunASM();
126-
}
127-
void PROXY_EnumProtocolsW()
128-
{
129-
PA = p[2];
130-
RunASM();
131-
}
132-
void PROXY_GetAddressByNameA()
133-
{
134-
PA = p[4];
135-
RunASM();
136-
}
137-
void PROXY_GetAddressByNameW()
138-
{
139-
PA = p[5];
140-
RunASM();
141-
}
142-
void PROXY_WEP()
143-
{
144-
PA = p[17];
145-
RunASM();
146-
}
147-
void PROXY_WSARecvEx()
148-
{
149-
PA = p[30];
150-
RunASM();
151-
}
152-
void PROXY___WSAFDIsSet()
153-
{
154-
PA = p[36];
155-
RunASM();
156-
}
157-
void PROXY_getnetbyname()
158-
{
159-
PA = p[45];
160-
RunASM();
161-
}
162-
void PROXY_getsockopt()
163-
{
164-
PA = p[52];
165-
RunASM();
166-
}
167-
void PROXY_inet_network()
168-
{
169-
PA = p[56];
170-
RunASM();
171-
}
172-
void PROXY_s_perror()
173-
{
174-
PA = p[67];
175-
RunASM();
176-
}
177-
void PROXY_setsockopt()
178-
{
179-
PA = p[72];
180-
RunASM();
181-
}
61+
void PROXY_EnumProtocolsA() { p[1](); }
62+
void PROXY_EnumProtocolsW() { p[2](); }
63+
void PROXY_GetAddressByNameA() { p[4](); }
64+
void PROXY_GetAddressByNameW() { p[5](); }
65+
void PROXY_WEP() { p[17](); }
66+
void PROXY_WSARecvEx() { p[30](); }
67+
void PROXY___WSAFDIsSet() { p[36](); }
68+
void PROXY_getnetbyname() { p[45](); }
69+
void PROXY_getsockopt() { p[52](); }
70+
void PROXY_inet_network() { p[56](); }
71+
void PROXY_s_perror() { p[67](); }
72+
void PROXY_setsockopt() { p[72](); }
18273
}

primedev/wsockproxy/loader.cpp

+31-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "loader.h"
2+
#include <shlwapi.h>
23
#include <string>
34
#include <system_error>
45
#include <sstream>
@@ -8,6 +9,21 @@
89

910
namespace fs = std::filesystem;
1011

12+
static wchar_t northstarPath[8192];
13+
static wchar_t exePath[4096];
14+
15+
bool GetExePathWide(wchar_t* dest, DWORD destSize)
16+
{
17+
if (!dest)
18+
return NULL;
19+
if (destSize < MAX_PATH)
20+
return NULL;
21+
22+
DWORD length = GetModuleFileNameW(NULL, dest, destSize);
23+
return length && PathRemoveFileSpecW(dest);
24+
}
25+
26+
1127
void LibraryLoadError(DWORD dwMessageId, const wchar_t* libName, const wchar_t* location)
1228
{
1329
char text[4096];
@@ -75,22 +91,30 @@ bool LoadNorthstar()
7591
strProfile = "R2Northstar";
7692
}
7793

78-
wchar_t buffer[8192];
94+
if (!GetExePathWide(exePath, 4096))
95+
{
96+
MessageBoxA(
97+
GetForegroundWindow(),
98+
"Failed getting game directory.\nThe game cannot continue and has to exit.",
99+
"Northstar Wsock32 Proxy Error",
100+
0);
101+
return true;
102+
}
79103

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

83-
if (!fs::exists(fs::path(buffer)))
84-
swprintf_s(buffer, L"%s\\Northstar.dll", exePath);
107+
if (!fs::exists(fs::path(northstarPath)))
108+
swprintf_s(northstarPath, L"%s\\Northstar.dll", exePath);
85109

86-
std::wcout << L"[*] Using: " << buffer << std::endl;
110+
std::wcout << L"[*] Using: " << northstarPath << std::endl;
87111

88-
HMODULE hHookModule = LoadLibraryExW(buffer, 0, 8u);
112+
HMODULE hHookModule = LoadLibraryExW(northstarPath, 0, 8u);
89113
if (hHookModule)
90114
Hook_Init = GetProcAddress(hHookModule, "InitialiseNorthstar");
91115
if (!hHookModule || Hook_Init == nullptr)
92116
{
93-
LibraryLoadError(GetLastError(), L"Northstar.dll", buffer);
117+
LibraryLoadError(GetLastError(), L"Northstar.dll", northstarPath);
94118
return false;
95119
}
96120
}

primedev/wsockproxy/wsock32.asm

-7
This file was deleted.

0 commit comments

Comments
 (0)