This repository has been archived by the owner on May 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
221 lines (182 loc) · 5.91 KB
/
main.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
*
* ------------------------------\
* Platinum |
* Bedrock Dedicated Server |
* mod for enhancements and |
* more customization. |
* ------------------------------/
*
* This is Free Software: You can edit and distribute it
* under the MIT license.
* author: PlatinumBDS's contributors
* link: https://github.com/Imrglop/PlatinumBDS
*/
#include "main.h"
#include "Mod/include_all.h"
#include "Sig/SignatureScanner.h"
#include <vector>
#include <Psapi.h>
#include "Settings/settings.h"
#include "Mod/Module.h"
#define PLATINUM_SEPERATE_THREAD false
namespace mods
{
Knockback kb;
HurtTime hurtTime;
BasicAntiCheat basicAC;
SeedHider sh;
}
std::vector<Module*> modules = { &mods::kb, &mods::hurtTime, &mods::basicAC, &mods::sh };
ModuleData data;
ModuleFunctions funcs;
bool _isLegacyVersion = false;
SignatureScanner scanner = NULL;
bool scanSigs() {
using namespace settings;
funcs.KnockbackRules_useLegacyKnockback = scanner.scan(
getFunction("KnockbackRules::useLegacyKnockback", false)
);
funcs.Mob_knockback = scanner.scan(
getFunction("Mob::knockback", false)
);
funcs.Mob_hurtEffects_setHurtTime = scanner.scan(
getFunction("Mob::hurtEffects:set-hurt-time", false)
);
funcs.ServerNetworkHandler_handle_SpawnExperienceOrbPacket = scanner.scan(
getFunction("ServerNetworkHandler::handle(SpawnExperienceOrbPacket)", false)
);
funcs._getSeed_return_address = scanner.scan(
getFunction("_getSeed return address", false)
);
funcs.LevelData_getSeed = scanner.scan(
getFunction("LevelData::getSeed", false)
);
funcs.MovePlayerPacket__read = scanner.scan(
getFunction("MovePlayerPacket::_read", false)
);
uintptr_t sigRes = scanner.scan(getFunction("Player::`vftable'", false));
if (sigRes != 0) {
int offset = *reinterpret_cast<int*>(sigRes + 3); // lea .., [<this value>], offset will wrap around
funcs.Player_vtable = sigRes + offset + 7;
}
// -------------- SUCCESS CHECK --------------
int amountOfZeroFunctions = 0;
size_t sizeOfFunctions = sizeof(funcs);
size_t amountOfFunctions = sizeOfFunctions / sizeof(uintptr_t);
uintptr_t* lpFuncs = reinterpret_cast<uintptr_t*>(&funcs);
for (size_t i = 0; i < amountOfFunctions; i++) {
if (lpFuncs[i] == NULL) {
#if defined(PLATINUM_DBG)
ldbg("Missing function (index: " << i << ")");
#endif
amountOfZeroFunctions++;
}
}
llog("Functions: (" << (amountOfFunctions - amountOfZeroFunctions) << " / " <<
amountOfFunctions << ")");
if (amountOfZeroFunctions != 0) {
lwarn(amountOfZeroFunctions << " missing function(s). Not all modules will work.");
}
return true;
}
HANDLE output = NULL;
HANDLE getConsoleOutput() {
if (output == NULL) {
output = GetStdHandle(STD_OUTPUT_HANDLE);
}
return output;
}
size_t numEnabledMods = 0;
DWORD
#if PLATINUM_SEPERATE_THREAD
__stdcall
#endif
init(void* lpParam) {
// -------------- GENERAL INIT --------------
llog("Initializing.. (Thread ID: " << GetCurrentThreadId() << ")");
auto status = MH_Initialize();
if (status != MH_OK) {
llog("Error loading MinHook! status = " << status);
return EXIT_FAILURE;
}
settings::init(".");
_isLegacyVersion = settings::getSettings()->getBool("legacy-bds-version");
// -------------- INITIALIZE APPLICATION INFORMATION --------------
HMODULE* pHModule = reinterpret_cast<HMODULE*>(lpParam);
HMODULE hModule = *pHModule;
data.base = (uintptr_t)GetModuleHandle(NULL);
MODULEINFO info;
GetModuleInformation(GetCurrentProcess(), (HMODULE)data.base, &info, sizeof(MODULEINFO));
data.functionEnd = data.base + info.SizeOfImage;
scanner = SignatureScanner(data.base, data.functionEnd);
scanSigs();
// -------------- INITIALIZE MODULES --------------
auto settings = settings::getSettings();
bool modsEnabled = settings->getBool("mod-enabled");
if (modsEnabled) {
for (Module* mod : modules) {
mod->enabled = settings::getModuleBool(mod->nid, "enabled");
if (mod->enabled) {
if (!mod->enable()) {
mod->enabled = false;
lerr("Mod " << mod->name << " failed to load.");
}
else {
llog("Loaded " << mod->name << ".");
numEnabledMods++;
}
}
#ifdef PLATINUM_DBG
else {
ldbg(mod->nid << ": not enabled");
}
#endif
}
llog("Enabled " << numEnabledMods << " modules.");
// -------------- INITIALIZE HOOKS --------------
status = MH_EnableHook(MH_ALL_HOOKS);
if (status != MH_OK) {
lerr("Error enabling hooks! status = " << status);
return EXIT_FAILURE;
}
}
else {
llog("Mods are disabled. Edit settings to enable them.");
}
return EXIT_SUCCESS;
// ----------------------------------------------
}
int __stdcall DllMain(HMODULE hModule, DWORD ulCallReason)
{
if (ulCallReason == DLL_PROCESS_ATTACH) {
#if PLATINUM_SEPERATE_THREAD == TRUE
CreateThread(nullptr, 0, init, reinterpret_cast<void*>(&hModule), 0, nullptr);
#else
init(reinterpret_cast<void*>(&hModule));
#endif
}
else if (ulCallReason == DLL_PROCESS_DETACH) {
for (Module* mod : modules) {
if (mod->enabled) {
mod->disable();
llog("Unloaded " << mod->name);
}
}
MH_Uninitialize();
if (numEnabledMods > 0) {
llog("Disabled " << numEnabledMods << " modules.");
}
}
return TRUE;
}
ModuleData& main::getData()
{
return std::ref(data);
}
ModuleFunctions& main::getFunctions() {
return std::ref(funcs);
}
bool main::isLegacyVersion() {
return _isLegacyVersion;
}