geode/loader/launcher/windows/proxyLoader.cpp

70 lines
2.4 KiB
C++
Raw Normal View History

2024-06-02 16:46:01 -04:00
#include <Windows.h>
#include <string>
#define XINPUT_PATH "\\\\.\\GLOBALROOT\\SystemRoot\\System32\\XInput1_4.dll"
// https://github.com/mrexodia/perfect-dll-proxy
#define PROXY_EXPORT(sym) \
"/export:" #sym "=" XINPUT_PATH "." #sym
#pragma comment(linker, PROXY_EXPORT(XInputSetState))
#pragma comment(linker, PROXY_EXPORT(XInputGetCapabilities))
#pragma comment(linker, PROXY_EXPORT(XInputEnable))
#pragma comment(linker, PROXY_EXPORT(XInputGetBatteryInformation))
#pragma comment(linker, PROXY_EXPORT(XInputGetKeystroke))
#pragma comment(linker, PROXY_EXPORT(XInputGetAudioDeviceIds))
struct XINPUT_STATE;
2024-06-03 10:03:37 -04:00
struct XINPUT_CAPABILITIES;
2024-06-02 16:46:01 -04:00
// libcocos2d.dll requires for this function to have ordinal 2.
#pragma comment(linker, "/export:XInputGetState,@2")
extern "C" DWORD XInputGetState(DWORD dwUserIndex, XINPUT_STATE *pState) {
auto xinput = GetModuleHandleA(XINPUT_PATH);
2024-06-02 17:10:05 -04:00
if (!xinput)
xinput = LoadLibraryA(XINPUT_PATH);
2024-06-02 16:46:01 -04:00
if (xinput) {
auto fp = GetProcAddress(xinput, "XInputGetState");
if (fp) {
using FPType = decltype(&XInputGetState);
return reinterpret_cast<FPType>(fp)(dwUserIndex, pState);
}
}
return ERROR_DEVICE_NOT_CONNECTED;
}
2024-06-03 10:03:37 -04:00
// Hi nikki please leave this as is, it fixes wine :)
#pragma comment(linker, "/export:XInputGetCapabilities,@4")
extern "C" DWORD XInputGetCapabilities(DWORD dwUserIndex, DWORD dwFlags, XINPUT_CAPABILITIES *pCapabilities) {
auto xinput = GetModuleHandleA(XINPUT_PATH);
if (!xinput)
xinput = LoadLibraryA(XINPUT_PATH);
if (xinput) {
auto fp = GetProcAddress(xinput, "XInputGetCapabilities");
if (fp) {
using FPType = decltype(&XInputGetCapabilities);
return reinterpret_cast<FPType>(fp)(dwUserIndex, dwFlags, pCapabilities);
}
}
return ERROR_DEVICE_NOT_CONNECTED;
}
2024-06-02 16:46:01 -04:00
static std::wstring getErrorString() {
return L"Could not load Geode. Error code: " + std::to_wstring(GetLastError());
}
__declspec(dllexport) BOOL WINAPI DllMain(HINSTANCE module, DWORD reason, LPVOID _) {
if (reason == DLL_PROCESS_ATTACH) {
DisableThreadLibraryCalls(module);
// This is UB.
if (LoadLibraryW(L"Geode.dll") == NULL)
MessageBoxW(NULL, getErrorString().c_str(), L"Load failed" , MB_OK | MB_ICONWARNING);
}
return TRUE;
}