Skip to content

Commit 729bcbd

Browse files
committed
Fix #2
this also reformats some garbo in PluginLoadHook, it looked even more garbage
1 parent c649eae commit 729bcbd

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

BL3DX11Injection/PluginLoadHook.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
#include <set>
2-
#include <fstream>
3-
#include <tchar.h>
4-
#include <list>
5-
#include <vector>
6-
71
#include "pch.h"
8-
#include "INIReader.h" // https://github.com/jtilly/inih
9-
10-
#include "HookLib.h"
112
#include "PluginLoadHook.h"
12-
#pragma comment(lib, "Zydis.lib")
13-
#pragma comment(lib, "HookLib.lib")
143

154
std::wstring pluginsPath;
165
static std::wstring iniPath;
@@ -39,6 +28,16 @@ VOID WINAPI ExitProcessHook(ULONG ExitCode)
3928
#pragma endregion
4029

4130
#pragma region Plugin Loading
31+
32+
void PluginLoadFunction(std::wstring dll, long delay) {
33+
std::this_thread::sleep_for(std::chrono::seconds(delay));
34+
HMODULE hMod = LoadLibrary(dll.c_str());
35+
if (hMod)
36+
loadedModules.push_back(hMod);
37+
else
38+
std::wcout << "Unable to load plugin: " << dll << ": " << GetLastErrorAsString() << std::endl;
39+
}
40+
4241
void LoadPlugins() {
4342
std::wcout << "Loading Plugins..." << std::endl;
4443

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

69+
std::vector<std::thread> threads;
70+
7071
do {
7172
if ((!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))) {
7273
std::wstring pluginName = (std::wstring)fd.cFileName;
7374
std::string s(pluginName.begin(), pluginName.end());
7475

76+
std::wstring filePath = pluginsPath + (pluginName); // Generate our file path + the name of our plugin to load
77+
7578
if (reader.Sections().count(s)) {
7679
float delayTime = reader.GetFloat(s, "delaySeconds", 0);
77-
std::wcout << "Waiting " << delayTime << " seconds to load " << WidenString(s) << std::endl;
78-
Sleep(delayTime * 1000);
80+
std::wcout << "Waiting " << delayTime << " seconds to load " << pluginName << "\n";
81+
threads.push_back(std::thread(PluginLoadFunction, filePath, (long)delayTime));
7982
}
80-
81-
82-
std::wstring filePath = pluginsPath + (pluginName); // Generate our file path + the name of our plugin to load
83-
HMODULE hMod = LoadLibrary(filePath.c_str());
84-
if (hMod) {
85-
loadedModules.push_back(hMod);
86-
dllCount++;
83+
else {
84+
HMODULE hMod = LoadLibrary(filePath.c_str());
85+
if (hMod) loadedModules.push_back(hMod);
86+
else std::wcout << "Unable to load plugin: " << filePath << ": " << GetLastErrorAsString() << std::endl;
8787
}
88-
else
89-
std::wcout << "Unable to load plugin: " << filePath << ": " << GetLastErrorAsString() << std::endl;
9088
}
9189

9290
} while (FindNextFile(dllFile, &fd));
93-
9491
FindClose(dllFile);
92+
93+
for (auto& t : threads) {
94+
if (t.joinable())
95+
t.join();
96+
}
97+
98+
// Add an extra new line just in case it all gets messed up with the whole multithreading
99+
std::wcout << std::endl;
95100
}
96101

97102
using _LoadLibrary = HMODULE(WINAPI*)(LPCWSTR lpLibFileName);

BL3DX11Injection/PluginLoadHook.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
21
#include <set>
32
#include <fstream>
43
#include <tchar.h>
54
#include <list>
65
#include <vector>
7-
86
#include "pch.h"
97
#include "INIReader.h" // https://github.com/jtilly/inih
10-
118
#include "HookLib.h"
9+
#include <thread>
10+
#pragma comment(lib, "Zydis.lib")
11+
#pragma comment(lib, "HookLib.lib")
1212

1313
VOID WINAPI ExitProcessHook(ULONG ExitCode);
1414

15+
void PluginLoadFunction(std::wstring dll, long delay);
16+
1517
void InitializePluginHooks(HMODULE gameModule);
1618

1719
void LoadPlugins();

BL3DX11Injection/dllmain.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
#include <list>
88
#include "dllmain.h"
99
#include "PluginLoadHook.h"
10+
#include <thread>
1011

1112
static HINSTANCE hL;
1213
static HMODULE gameModule;
1314

14-
#pragma pack(1)
15-
FARPROC p[51] = { 0 };
16-
17-
1815
BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID) {
1916
if(reason == DLL_PROCESS_ATTACH) {
2017

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

4239
// If we're running in debug mode, we wanna allocate the console
43-
4440
if (cmdArgs.find("--debug") != std::string::npos) {
4541
AllocConsole(); // Allocate our console
4642
}
4743

4844
SetConsoleTitle(L"Borderlands 3 Plugin Loader");
4945

46+
FILE* f = nullptr;
47+
freopen_s(&f, "CONIN$", "r", stdin);
48+
freopen_s(&f, "CONOUT$", "w", stderr);
49+
freopen_s(&f, "CONOUT$", "w", stdout);
50+
5051
HANDLE hStdout = CreateFile(L"CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
5152
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
5253
HANDLE hStdin = CreateFile(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,

0 commit comments

Comments
 (0)