Skip to content

Commit

Permalink
Fix #2
Browse files Browse the repository at this point in the history
this also reformats some garbo in PluginLoadHook, it looked even more garbage
  • Loading branch information
FromDarkHell committed Oct 9, 2020
1 parent c649eae commit 729bcbd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
51 changes: 28 additions & 23 deletions BL3DX11Injection/PluginLoadHook.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
#include <set>
#include <fstream>
#include <tchar.h>
#include <list>
#include <vector>

#include "pch.h"
#include "INIReader.h" // https://github.com/jtilly/inih

#include "HookLib.h"
#include "PluginLoadHook.h"
#pragma comment(lib, "Zydis.lib")
#pragma comment(lib, "HookLib.lib")

std::wstring pluginsPath;
static std::wstring iniPath;
Expand Down Expand Up @@ -39,6 +28,16 @@ VOID WINAPI ExitProcessHook(ULONG ExitCode)
#pragma endregion

#pragma region Plugin Loading

void PluginLoadFunction(std::wstring dll, long delay) {
std::this_thread::sleep_for(std::chrono::seconds(delay));
HMODULE hMod = LoadLibrary(dll.c_str());
if (hMod)
loadedModules.push_back(hMod);
else
std::wcout << "Unable to load plugin: " << dll << ": " << GetLastErrorAsString() << std::endl;
}

void LoadPlugins() {
std::wcout << "Loading Plugins..." << std::endl;

Expand Down Expand Up @@ -67,31 +66,37 @@ void LoadPlugins() {
return; // Just return now, no need to bother to execute the rest of the code
}

std::vector<std::thread> threads;

do {
if ((!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))) {
std::wstring pluginName = (std::wstring)fd.cFileName;
std::string s(pluginName.begin(), pluginName.end());

std::wstring filePath = pluginsPath + (pluginName); // Generate our file path + the name of our plugin to load

if (reader.Sections().count(s)) {
float delayTime = reader.GetFloat(s, "delaySeconds", 0);
std::wcout << "Waiting " << delayTime << " seconds to load " << WidenString(s) << std::endl;
Sleep(delayTime * 1000);
std::wcout << "Waiting " << delayTime << " seconds to load " << pluginName << "\n";
threads.push_back(std::thread(PluginLoadFunction, filePath, (long)delayTime));
}


std::wstring filePath = pluginsPath + (pluginName); // Generate our file path + the name of our plugin to load
HMODULE hMod = LoadLibrary(filePath.c_str());
if (hMod) {
loadedModules.push_back(hMod);
dllCount++;
else {
HMODULE hMod = LoadLibrary(filePath.c_str());
if (hMod) loadedModules.push_back(hMod);
else std::wcout << "Unable to load plugin: " << filePath << ": " << GetLastErrorAsString() << std::endl;
}
else
std::wcout << "Unable to load plugin: " << filePath << ": " << GetLastErrorAsString() << std::endl;
}

} while (FindNextFile(dllFile, &fd));

FindClose(dllFile);

for (auto& t : threads) {
if (t.joinable())
t.join();
}

// Add an extra new line just in case it all gets messed up with the whole multithreading
std::wcout << std::endl;
}

using _LoadLibrary = HMODULE(WINAPI*)(LPCWSTR lpLibFileName);
Expand Down
8 changes: 5 additions & 3 deletions BL3DX11Injection/PluginLoadHook.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@

#include <set>
#include <fstream>
#include <tchar.h>
#include <list>
#include <vector>

#include "pch.h"
#include "INIReader.h" // https://github.com/jtilly/inih

#include "HookLib.h"
#include <thread>
#pragma comment(lib, "Zydis.lib")
#pragma comment(lib, "HookLib.lib")

VOID WINAPI ExitProcessHook(ULONG ExitCode);

void PluginLoadFunction(std::wstring dll, long delay);

void InitializePluginHooks(HMODULE gameModule);

void LoadPlugins();
11 changes: 6 additions & 5 deletions BL3DX11Injection/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
#include <list>
#include "dllmain.h"
#include "PluginLoadHook.h"
#include <thread>

static HINSTANCE hL;
static HMODULE gameModule;

#pragma pack(1)
FARPROC p[51] = { 0 };


BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID) {
if(reason == DLL_PROCESS_ATTACH) {

Expand All @@ -40,13 +37,17 @@ int executionThread() {
std::string cmdArgs = GetCommandLineA(); // Get the command line args for our running process

// If we're running in debug mode, we wanna allocate the console

if (cmdArgs.find("--debug") != std::string::npos) {
AllocConsole(); // Allocate our console
}

SetConsoleTitle(L"Borderlands 3 Plugin Loader");

FILE* f = nullptr;
freopen_s(&f, "CONIN$", "r", stdin);
freopen_s(&f, "CONOUT$", "w", stderr);
freopen_s(&f, "CONOUT$", "w", stdout);

HANDLE hStdout = CreateFile(L"CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hStdin = CreateFile(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
Expand Down

0 comments on commit 729bcbd

Please sign in to comment.