Skip to content

Commit

Permalink
DLL bootstrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
DisabledMallis committed Dec 6, 2021
1 parent a970559 commit e829c9f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@
*.exe
*.out
*.app
.vscode/
*.vcxproj
*.vcxproj.filters
*.sln
*.cmake
CMakeCache.txt
x64/
wininet.dir/
Release/
CMakeFiles/
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.17)
project(R8PD VERSION 1.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)

add_library(wininet SHARED main.cpp)
78 changes: 78 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Written by DisabledMallis (https://github.com/DisabledMallis)
Originally made for the NKHook5 project, repurposed for Round8
Usage:
This DLL is loaded when the game loads. This allows us to run our own code before the game's
code so we can modify how it loads without patching the binary. This DLL searches for a relative
directory called "loaders" which will contain any DLLs that should be injected before the game starts.
These DLLs should be 'tweaker' DLLs that prepare & patch the game for mod loading.
*/
#include <Windows.h>
#include <string>
#include <iostream>
#include <shlobj_core.h>
#include <filesystem>
#include <cstdint>



//Used to ensure the functions required are properly exported
#define EXPORT comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__)

//HMODULE for the original dll
HMODULE winINet;
size_t(__stdcall* InternetGetConnectedState_orig)(size_t,int);

extern "C" __declspec(dllexport) size_t __stdcall InternetGetConnectedState(size_t lpdwFlags, int dwReserved) {
int result = InternetGetConnectedState_orig(lpdwFlags, dwReserved);
return result;
}

auto initialize() -> int {
//Find the original wininet.dll
char sys32Path[MAX_PATH];

//We need to use diff dirs on diff platforms
#if INTPTR_MAX == INT64_MAX
SHGetFolderPathA(nullptr, CSIDL_SYSTEM, nullptr, SHGFP_TYPE_CURRENT, sys32Path);
#elif INTPTR_MAX == INT32_MAX
SHGetFolderPathA(nullptr, CSIDL_SYSTEMX86, nullptr, SHGFP_TYPE_CURRENT, sys32Path);
#else
#error Unknown pointer size or missing size macros!
#endif

std::string sys32Str(sys32Path);
std::string wininetPath = sys32Str + "\\wininet.dll";


//Load the original wininet dll
winINet = LoadLibraryA(wininetPath.c_str());
//Get the original function to call when requested
InternetGetConnectedState_orig = (size_t(__stdcall*)(size_t,int))GetProcAddress(winINet, "InternetGetConnectedState");

//Load tweaker DLLs
std::string modsDir = "loaders/";
for(const auto& tweaker : std::filesystem::directory_iterator(modsDir)) {
LoadLibraryW(tweaker.path().c_str());
}

return 0;
}


extern "C" __declspec(dllexport) bool __stdcall DllMain(
HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpReserved ) // reserved
{
// Perform actions based on the reason for calling.
switch( fdwReason )
{
case DLL_PROCESS_ATTACH:
initialize();
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
}

0 comments on commit e829c9f

Please sign in to comment.