Skip to content

Commit

Permalink
Clean up wsock proxy code and move wsock build system logic (#671)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Jan200101 authored Feb 18, 2024
1 parent fc63948 commit 30e58ac
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 152 deletions.
2 changes: 1 addition & 1 deletion primedev/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include(Northstar.cmake)
include(Launcher.cmake)
include(WSockProxy.cmake)
add_subdirectory(wsockproxy)
11 changes: 5 additions & 6 deletions primedev/WSockProxy.cmake → primedev/wsockproxy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ find_package(minhook REQUIRED)

add_library(
loader_wsock32_proxy SHARED
"wsockproxy/dllmain.cpp"
"wsockproxy/loader.cpp"
"wsockproxy/loader.h"
"wsockproxy/wsock32.asm"
"wsockproxy/wsock32.def"
"dllmain.cpp"
"loader.cpp"
"loader.h"
"wsock32.def"
)

target_link_libraries(
Expand Down Expand Up @@ -36,7 +35,7 @@ target_link_libraries(
target_precompile_headers(
loader_wsock32_proxy
PRIVATE
wsockproxy/pch.h
pch.h
)

target_compile_definitions(loader_wsock32_proxy PRIVATE UNICODE _UNICODE)
Expand Down
153 changes: 22 additions & 131 deletions primedev/wsockproxy/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,90 +1,29 @@
#include "loader.h"

#include <shlwapi.h>
#include <filesystem>

HINSTANCE hLThis = 0;
FARPROC p[857];
HINSTANCE hL = 0;
FARPROC p[73];
HMODULE hL = 0;

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);
}

wchar_t exePath[4096];
wchar_t buffer1[8192];
wchar_t buffer2[12288];
static wchar_t wsockPath[4096];

BOOL WINAPI DllMain(HINSTANCE hInst, DWORD reason, LPVOID)
{
if (reason == DLL_PROCESS_ATTACH)
{
hLThis = hInst;

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;
}

SetCurrentDirectoryW(exePath);
// Tell the OS we don't need to know about threads
DisableThreadLibraryCalls(hInst);

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

// copy the original library for system to our local directory, with changed name so that we can load it
swprintf_s(buffer1, L"%s\\bin\\x64_retail\\wsock32.org.dll", exePath);
GetSystemDirectoryW(buffer2, 4096);
swprintf_s(buffer2, L"%s\\wsock32.dll", buffer2);
try
{
std::filesystem::copy_file(buffer2, buffer1);
}
catch (const std::exception& e1)
{
if (!std::filesystem::exists(buffer1))
{
// fallback by copying to temp dir...
// because apparently games installed by EA Desktop app don't have write permissions in their directories
auto temp_dir = std::filesystem::temp_directory_path() / L"wsock32.org.dll";
try
{
std::filesystem::copy_file(buffer2, temp_dir);
}
catch (const std::exception& e2)
{
if (!std::filesystem::exists(temp_dir))
{
swprintf_s(
buffer2,
L"Failed copying wsock32.dll from system32 to \"%s\"\n\n%S\n\nFurthermore, we failed copying wsock32.dll into "
L"temporary directory at \"%s\"\n\n%S",
buffer1,
e1.what(),
temp_dir.c_str(),
e2.what());
MessageBoxW(GetForegroundWindow(), buffer2, L"Northstar Wsock32 Proxy Error", 0);
return false;
}
}
swprintf_s(buffer1, L"%s", temp_dir.c_str());
}
}
hL = LoadLibraryExW(buffer1, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
GetSystemDirectoryW(wsockPath, 4096);
swprintf_s(wsockPath, 4096, L"%s\\wsock32.dll", wsockPath);

hL = LoadLibraryExW(wsockPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
if (!hL)
{
LibraryLoadError(GetLastError(), L"wsock32.org.dll", buffer1);
LibraryLoadError(GetLastError(), L"wsock32.dll", wsockPath);
return false;
}

Expand Down Expand Up @@ -119,64 +58,16 @@ extern "C"
FARPROC PA = NULL;
int RunASM();

void PROXY_EnumProtocolsA()
{
PA = p[1];
RunASM();
}
void PROXY_EnumProtocolsW()
{
PA = p[2];
RunASM();
}
void PROXY_GetAddressByNameA()
{
PA = p[4];
RunASM();
}
void PROXY_GetAddressByNameW()
{
PA = p[5];
RunASM();
}
void PROXY_WEP()
{
PA = p[17];
RunASM();
}
void PROXY_WSARecvEx()
{
PA = p[30];
RunASM();
}
void PROXY___WSAFDIsSet()
{
PA = p[36];
RunASM();
}
void PROXY_getnetbyname()
{
PA = p[45];
RunASM();
}
void PROXY_getsockopt()
{
PA = p[52];
RunASM();
}
void PROXY_inet_network()
{
PA = p[56];
RunASM();
}
void PROXY_s_perror()
{
PA = p[67];
RunASM();
}
void PROXY_setsockopt()
{
PA = p[72];
RunASM();
}
void PROXY_EnumProtocolsA() { p[1](); }
void PROXY_EnumProtocolsW() { p[2](); }
void PROXY_GetAddressByNameA() { p[4](); }
void PROXY_GetAddressByNameW() { p[5](); }
void PROXY_WEP() { p[17](); }
void PROXY_WSARecvEx() { p[30](); }
void PROXY___WSAFDIsSet() { p[36](); }
void PROXY_getnetbyname() { p[45](); }
void PROXY_getsockopt() { p[52](); }
void PROXY_inet_network() { p[56](); }
void PROXY_s_perror() { p[67](); }
void PROXY_setsockopt() { p[72](); }
}
38 changes: 31 additions & 7 deletions primedev/wsockproxy/loader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "loader.h"
#include <shlwapi.h>
#include <string>
#include <system_error>
#include <sstream>
Expand All @@ -8,6 +9,21 @@

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];
Expand Down Expand Up @@ -75,22 +91,30 @@ bool LoadNorthstar()
strProfile = "R2Northstar";
}

wchar_t buffer[8192];
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(buffer, L"%s\\%s\\Northstar.dll", exePath, std::wstring(strProfile.begin(), strProfile.end()).c_str());
swprintf_s(northstarPath, L"%s\\%s\\Northstar.dll", exePath, std::wstring(strProfile.begin(), strProfile.end()).c_str());

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

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

HMODULE hHookModule = LoadLibraryExW(buffer, 0, 8u);
HMODULE hHookModule = LoadLibraryExW(northstarPath, 0, 8u);
if (hHookModule)
Hook_Init = GetProcAddress(hHookModule, "InitialiseNorthstar");
if (!hHookModule || Hook_Init == nullptr)
{
LibraryLoadError(GetLastError(), L"Northstar.dll", buffer);
LibraryLoadError(GetLastError(), L"Northstar.dll", northstarPath);
return false;
}
}
Expand Down
7 changes: 0 additions & 7 deletions primedev/wsockproxy/wsock32.asm

This file was deleted.

0 comments on commit 30e58ac

Please sign in to comment.