-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdllmain.cpp
More file actions
146 lines (113 loc) · 4.42 KB
/
dllmain.cpp
File metadata and controls
146 lines (113 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <windows.h>
#include <string>
#include <algorithm>
#include <sys/stat.h>
#include <stdio.h>
constexpr uintptr_t HOOK_FILELOADER = 0x006E6B20;
constexpr int PROLOGUE_SIZE = 6;
constexpr uintptr_t MEMORYLIMIT_ADDRESS = 0x006FF4E4;
constexpr DWORD ORIGINAL_GAME_LIMIT = 100663296;
constexpr DWORD DEFAULT_LIMIT = ORIGINAL_GAME_LIMIT * 4;
constexpr INT MAX_LIMIT_MB = 1024;
typedef DWORD(__cdecl* ProcessAndHashFileName_t)(DWORD, char*, BYTE);
ProcessAndHashFileName_t Gateway_FileLoader = nullptr;
static bool FileExists(const std::string& name) {
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}
// Bat name decoder for game, game stores bat id in a single byte in a roster
static int GetFixedBatID(const std::string& originalFilename) {
size_t pos = originalFilename.find("bt");
if (pos == std::string::npos) return 0;
if (pos + 3 >= originalFilename.length()) return 0;
char tensChar = originalFilename[pos + 2];
char onesChar = originalFilename[pos + 3];
bool tensBugged = (tensChar < '0' || tensChar > '9');
bool onesBugged = (onesChar < '0' || onesChar > '9');
if (!tensBugged && !onesBugged) return 0;
int tens = tensChar - 48;
int ones = onesChar - 48;
int realID = (tens * 10) + ones;
if (realID > 99 && realID < 300) {
return realID;
}
return 0;
}
// Direct filename loader logic
static DWORD __cdecl Detour_ProcessAndHashFileName(DWORD param_1, char* fileName, BYTE useHash)
{
if (fileName)
{
std::string rawPath = fileName;
// Lowercase conversion
std::string cleanPath = fileName;
std::transform(cleanPath.begin(), cleanPath.end(), cleanPath.begin(), ::tolower);
std::replace(cleanPath.begin(), cleanPath.end(), '\\', '/');
// Check if direct file exists
if (FileExists(cleanPath))
{
return Gateway_FileLoader(param_1, (char*)cleanPath.c_str(), 0);
}
// Bat name fix
int fixedID = GetFixedBatID(rawPath);
if (fixedID > 0)
{
char newPath[256];
sprintf_s(newPath, "misc/textures/bt%d.fsh", fixedID);
if (FileExists(newPath))
{
return Gateway_FileLoader(param_1, newPath, 0);
}
}
}
return Gateway_FileLoader(param_1, fileName, useHash);
}
// Injecting fileloader logic
static void InjectFileLoaderHook() {
BYTE* trampoline = (BYTE*)VirtualAlloc(NULL, 15, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!trampoline) {
MessageBoxA(NULL, "Failed to allocate memory!", "Loader Error", MB_ICONERROR);
return;
}
memcpy(trampoline, (void*)HOOK_FILELOADER, PROLOGUE_SIZE);
trampoline[PROLOGUE_SIZE] = 0xE9;
DWORD returnAddr = (HOOK_FILELOADER + PROLOGUE_SIZE) - ((DWORD)trampoline + PROLOGUE_SIZE) - 5;
*(DWORD*)(trampoline + PROLOGUE_SIZE + 1) = returnAddr;
Gateway_FileLoader = (ProcessAndHashFileName_t)trampoline;
DWORD oldProtect;
if (VirtualProtect((void*)HOOK_FILELOADER, PROLOGUE_SIZE, PAGE_EXECUTE_READWRITE, &oldProtect)) {
BYTE* patch = (BYTE*)HOOK_FILELOADER;
patch[0] = 0xE9;
*(DWORD*)(patch + 1) = (DWORD)((DWORD)Detour_ProcessAndHashFileName - HOOK_FILELOADER - 5);
patch[5] = 0x90;
VirtualProtect((void*)HOOK_FILELOADER, PROLOGUE_SIZE, oldProtect, &oldProtect);
}
}
void ApplyMemoryLimitPatch() {
// Read from ini
int userMB = GetPrivateProfileIntA("MAIN", "MemoryLimit", 0, ".\\Cricket07FileLoader.ini");
DWORD newLimitBytes = 0;
// If missing (0) or too huge (>1024MB), use safe default (384MB)
if (userMB <= 0 || userMB > MAX_LIMIT_MB) {
newLimitBytes = DEFAULT_LIMIT;
}
else {
// Convert MB to Bytes (MB * 1024 * 1024)
newLimitBytes = (DWORD)(userMB * 1024 * 1024);
}
// Patch the Memory
DWORD oldProtect;
if (VirtualProtect((void*)MEMORYLIMIT_ADDRESS, sizeof(DWORD), PAGE_EXECUTE_READWRITE, &oldProtect)) {
// Overwrite the hardcoded 96MB limit
*(DWORD*)MEMORYLIMIT_ADDRESS = newLimitBytes;
VirtualProtect((void*)MEMORYLIMIT_ADDRESS, sizeof(DWORD), oldProtect, &oldProtect);
}
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
DisableThreadLibraryCalls(hModule);
InjectFileLoaderHook();
ApplyMemoryLimitPatch();
}
return TRUE;
}