This repository was archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdllmain.cpp
More file actions
378 lines (307 loc) · 9.74 KB
/
dllmain.cpp
File metadata and controls
378 lines (307 loc) · 9.74 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <codecvt>
#include <atlstr.h>
#include "incl/fmt/core.h"
#include "incl/MinHook.h"
#include "lib/process.h"
#include <iostream>
#include <filesystem>
#include <iomanip>
#include <sstream>
#include <ctime>
#include <string>
#include "modules/fs.h"
#include "incl/python/Python.h"
#include <windows.h>
#include <stdlib.h>
#include <winnt.h>
#include <stdio.h>
#include "addresses.h"
#include "global.hpp"
#include "lib/tinydir.h"
//#include "lib/cinnamon.h"
using namespace std;
namespace stdfs = filesystem;
namespace fs = cinnamon::fs;
/// WARNING: If you don't know what you're doing, please avoid everything below here. ///
wstring s2ws(const string& str)
{
using convert_typeX = codecvt_utf8<wchar_t>;
wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.from_bytes(str);
}
string ws2s(const wstring& wstr)
{
using convert_typeX = codecvt_utf8<wchar_t>;
wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.to_bytes(wstr);
}
string int_to_string(int num)
{
stringstream stream;
stream << hex << num;
return stream.str();
}
int str_ends_with(const char* str, const char* suffix) {
if (str == NULL || suffix == NULL)
return 0;
size_t str_len = strlen(str);
size_t suffix_len = strlen(suffix);
if (suffix_len > str_len)
return 0;
return 0 == strncmp(str + str_len - suffix_len, suffix, suffix_len);
}
vector<string> split(const string& str, const string& delim)
{
vector<string> tokens;
size_t prev = 0, pos = 0;
do
{
pos = str.find(delim, prev);
if (pos == string::npos) pos = str.length();
string token = str.substr(prev, pos - prev);
if (!token.empty()) tokens.push_back(token);
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());
return tokens;
}
int setenv(const char* name, const char* value, int overwrite)
{
int errcode = 0;
if (!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, name);
if (errcode || envsize) return errcode;
}
return _putenv_s(name, value);
}
void println(string title, string msg)
{
auto t = time(nullptr);
auto tm = *localtime(&t);
ostringstream sstr;
sstr << put_time(&tm, "%d/%m/%Y %H:%M:%S");
cout << fmt::format("[{}] [{}]: {}", sstr.str().c_str(), title.c_str(), msg.c_str()) << endl;
}
int find_path()
{
char* path = getenv("PATH");
auto paths = split(string(path), string(";"));
bool found = false;
println("DEBUG", path);
for (const auto &strpath : paths)
{
if (strpath.find("Python39") != string::npos)
{
println("DEBUG", "A");
// found path
if (strpath.find("32") != string::npos)
{
println("DEBUG", "B");
// it's 32 bit
if (strpath.find("Scripts") == string::npos)
{
println("DEBUG", "C");
// ignore Scripts path
println("DEBUG", strpath);
setenv("PYPATH", strpath.c_str(), 0);
found = true;
break;
}
}
}
}
if (found)
{
const char* pypath = getenv("PYPATH");
println("DEBUG", "Found Python Executable in PATH");
Py_SetPythonHome(Py_DecodeLocale(pypath, NULL));
return 1;
} else
{
println("DEBUG", "Couldn't find Python Executable in PATH.");
return 0;
}
}
void init_hook()
{
println("DEBUG", "Loading hooks...");
MH_Initialize();
println("DEBUG", "Hooking: MenuLayer");
//MenuLayer::hook();
MH_EnableHook(MH_ALL_HOOKS);
println("DEBUG", "Hooks loaded!");
}
void init_mods()
{
// this is all to get the directory path properly
// can someone please find a way to make this look not ugly
auto e = stdfs::current_path().c_str();
wstring path = wstring(stdfs::current_path().c_str()) + s2ws(string("/cinnamon/mods"));
println("DEBUG", "Loading Mods");
tinydir_dir dir;
tinydir_open(&dir,path.c_str());
// iterate.
while (dir.has_next)
{
// read file
tinydir_file file;
if (tinydir_readfile(&dir, &file) == -1)
{
println("ERROR", "Failed to load a specific file.");
goto bail;
}
if (file.is_dir)
{
// pain
wstring pain(file.path);
string fpath(pain.begin(), pain.end());
// check if the file path ends with . (ignore recursive and previous dir)
if (str_ends_with(fpath.c_str(), ".") == 0) // ignore the .'s
{
tinydir_dir moddir;
tinydir_file mainmodfile;
PyObject *modpy, *modpyname;
// file name
wstring wname(file.name);
string name(wname.begin(), wname.end());
// file path
string mpypath = fpath + string("/mod.py");
wstring wmpypath = s2ws(mpypath);
tinydir_open(&moddir, s2ws(fpath).c_str());
println(fmt::format("{}/INFO", name), fmt::format("Loading {}...", name));
if (tinydir_file_open(&mainmodfile, CA2T(mpypath.c_str())) == -1)
{
println(fmt::format("{}/ERROR", name), fmt::format("{} does not contain a valid mod.py file.", name));
goto bail;
} else
{
// set cwd to mod folder
PyRun_SimpleString((string("import os\nos.chdir('") + fpath + string("')")).c_str());
PyRun_SimpleString("import os\nprint(os.getcwd())");
Py_Finalize();
Py_Initialize();
modpyname = PyUnicode_DecodeFSDefault(mpypath.c_str());
modpy = PyImport_Import(modpyname);
Py_DECREF(modpyname);
if (modpy != NULL) {
} else
{
println(fmt::format("{}/ERROR", name), fmt::format("{} does not contain a valid mod.py file.", name));
goto bail;
}
}
}
}
if (tinydir_next(&dir) == -1)
{
println("DEBUG", "Finished loading mods.");
goto bail;
}
}
bail:
tinydir_close(&dir);
// done
}
void init_python()
{
println("DEBUG", "Loading Python API...");
// find path
int res = find_path();
if (res == 1)
{
// initialize python
Py_Initialize();
// load mods (or try to)
init_mods();
}
else
{
// display warning
/*system("cls");
// change title
wstring title = s2ws(string("Cinnamon ")
+ string(cinnamon_version())
+ string(" - NOT running Geometry Dash ")
+ string(gd_version()));
SetConsoleTitle(title.c_str());
// display warning
cout << " " << endl;
cout << " WARNING: " << endl;
cout << " If you are seeing this message and screen, then that means Cinnamon failed to" << endl;
cout << " load entirely. While this means your installation itself is fine, as of right" << endl;
cout << " now you do not have Python 3.9.1 (32-bit) installed." << endl;
cout << " " << endl;
cout << " Before continuing to run Cinnamon, please install it before doing such." << endl;
cout << " NOTE: Make sure to add it to the environment variables, as well!" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " Thank you!" << endl;
cout << " - The Cinnamon Dev Team" << endl;
cout << " " << endl;
cout << " PS: We appreciate your usage of our modloader!" << endl;*/
}
}
void init()
{
// create the main directory if it doesn't exist
fs::Directory::create(stdfs::current_path().string() + "/cinnamon");
// create the mods directory if it doesn't exist
fs::Directory::create(stdfs::current_path().string() + "/cinnamon/mods");
// init hooks
init_hook();
// init pythonic api
init_python();
}
DWORD WINAPI thread(void* hModule)
{
// create console
AllocConsole();
// painfully set the title
wstring title = s2ws(string("Cinnamon ")
+ string(cinnamon_version())
+ string(" - running Geometry Dash ")
+ string(gd_version()));
SetConsoleTitle(title.c_str());
// console stuff
HWND hwnd = GetConsoleWindow();
HMENU hmen = GetSystemMenu(hwnd, FALSE);
// disable closing the console
//EnableMenuItem(hmen, SC_CLOSE, MF_GRAYED);
// open stdout
freopen("CONOUT$", "w", stdout);
// print startup
println("INFO", fmt::format("Starting Cinnamon v{} for Geometry Dash {}", cinnamon_version(), gd_version()));
println("INFO", fmt::format("Running on: x{}", architecture()));
println("DEBUG", fmt::format("Module Address: 0x{}", int_to_string((int)_MODULE_HANDLE)));
println("DEBUG", fmt::format("Executable Path: {}", cinnamon::process::current_directory()));
// init everything
init();
return 0;
}
_declspec(dllexport) BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
// attached to gd
case DLL_PROCESS_ATTACH: {
// safecheck
try
{
CreateThread(0, 0x1000, thread, hModule, 0, 0);
} catch (exception e)
{
MessageBox(0, (LPCWSTR)"Cinnamon: Hi, I'm an Error!", (LPCWSTR)e.what(), MB_OK);
}
}
// detatched from GD
case DLL_PROCESS_DETACH: {
if (Py_FinalizeEx() < 0) {
FreeConsole(); // free console
}
}
}
return TRUE;
}