-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathModAPI.cpp
More file actions
593 lines (502 loc) · 19.4 KB
/
ModAPI.cpp
File metadata and controls
593 lines (502 loc) · 19.4 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include "RetroEngine.hpp"
#if RETRO_USE_MOD_LOADER || !RETRO_USE_ORIGINAL_CODE
char savePath[0x100];
#endif
char playerNames[PLAYER_COUNT][0x20];
byte playerCount = 0;
#if RETRO_USE_MOD_LOADER
std::vector<ModInfo> modList;
int activeMod = -1;
char modsPath[0x100];
bool redirectSave = false;
char modTypeNames[OBJECT_COUNT][0x40];
char modScriptPaths[OBJECT_COUNT][0x40];
byte modScriptFlags[OBJECT_COUNT];
byte modObjCount = 0;
#include <filesystem>
#include <locale>
void OpenModMenu()
{
Engine.gameMode = ENGINE_INITMODMENU;
Engine.modMenuCalled = true;
}
#if RETRO_PLATFORM == RETRO_ANDROID
namespace fs = std::__fs::filesystem; // this is so we can avoid using c++17, which causes a ton of warnings w asio and looks ugly
#else
namespace fs = std::filesystem;
#endif
fs::path resolvePath(fs::path given)
{
if (given.is_relative())
given = fs::current_path() / given; // thanks for the weird syntax!
for (auto &p : fs::directory_iterator{ given.parent_path() }) {
char pbuf[0x100];
char gbuf[0x100];
auto pf = p.path().filename();
auto pstr = pf.string();
StringLowerCase(pbuf, pstr.c_str());
auto gf = given.filename();
auto gstr = gf.string();
StringLowerCase(gbuf, gstr.c_str());
if (StrComp(pbuf, gbuf)) {
return p.path();
}
}
return given; // might work might not!
}
void InitMods()
{
modList.clear();
forceUseScripts = forceUseScripts_Config;
skipStartMenu = skipStartMenu_Config;
disableFocusPause = disableFocusPause_Config;
redirectSave = false;
Engine.forceSonic1 = false;
sprintf(savePath, "");
char modBuf[0x100];
sprintf(modBuf, "%smods", modsPath);
fs::path modPath = resolvePath(modBuf);
if (fs::exists(modPath) && fs::is_directory(modPath)) {
std::string mod_config = modPath.string() + "/modconfig.ini";
FileIO *configFile = fOpen(mod_config.c_str(), "r");
if (configFile) {
fClose(configFile);
IniParser modConfig(mod_config.c_str(), false);
for (int m = 0; m < modConfig.items.size(); ++m) {
bool active = false;
ModInfo info;
modConfig.GetBool("mods", modConfig.items[m].key, &active);
if (LoadMod(&info, modPath.string(), modConfig.items[m].key, active))
modList.push_back(info);
}
}
try {
auto rdi = fs::directory_iterator(modPath);
for (auto de : rdi) {
if (de.is_directory()) {
fs::path modDirPath = de.path();
ModInfo info;
std::string modDir = modDirPath.string().c_str();
const std::string mod_inifile = modDir + "/mod.ini";
std::string folder = modDirPath.filename().string();
bool flag = true;
for (int m = 0; m < modList.size(); ++m) {
if (modList[m].folder == folder) {
flag = false;
break;
}
}
if (flag) {
if (LoadMod(&info, modPath.string(), modDirPath.filename().string(), false))
modList.push_back(info);
}
}
}
} catch (fs::filesystem_error fe) {
PrintLog("Mods Folder Scanning Error: ");
PrintLog(fe.what());
}
}
forceUseScripts = forceUseScripts_Config;
skipStartMenu = skipStartMenu_Config;
disableFocusPause = disableFocusPause_Config;
redirectSave = false;
Engine.forceSonic1 = false;
sprintf(savePath, "");
for (int m = 0; m < modList.size(); ++m) {
if (!modList[m].active)
continue;
if (modList[m].useScripts)
forceUseScripts = true;
if (modList[m].skipStartMenu)
skipStartMenu = true;
if (modList[m].disableFocusPause)
disableFocusPause |= modList[m].disableFocusPause;
if (modList[m].redirectSave) {
sprintf(savePath, "%s", modList[m].savePath.c_str());
redirectSave = true;
}
if (modList[m].forceSonic1)
Engine.forceSonic1 = true;
}
ReadSaveRAMData();
ReadUserdata();
}
bool LoadMod(ModInfo *info, std::string modsPath, std::string folder, bool active)
{
if (!info)
return false;
info->fileMap.clear();
info->name = "";
info->desc = "";
info->author = "";
info->version = "";
info->folder = "";
info->active = false;
const std::string modDir = modsPath + "/" + folder;
FileIO *f = fOpen((modDir + "/mod.ini").c_str(), "r");
if (f) {
fClose(f);
IniParser modSettings((modDir + "/mod.ini").c_str(), false);
info->name = "Unnamed Mod";
info->desc = "";
info->author = "Unknown Author";
info->version = "1.0.0";
info->folder = folder;
char infoBuf[0x100];
// Name
StrCopy(infoBuf, "");
modSettings.GetString("", "Name", infoBuf);
if (!StrComp(infoBuf, ""))
info->name = infoBuf;
// Desc
StrCopy(infoBuf, "");
modSettings.GetString("", "Description", infoBuf);
if (!StrComp(infoBuf, ""))
info->desc = infoBuf;
// Author
StrCopy(infoBuf, "");
modSettings.GetString("", "Author", infoBuf);
if (!StrComp(infoBuf, ""))
info->author = infoBuf;
// Version
StrCopy(infoBuf, "");
modSettings.GetString("", "Version", infoBuf);
if (!StrComp(infoBuf, ""))
info->version = infoBuf;
info->active = active;
ScanModFolder(info);
info->useScripts = false;
modSettings.GetBool("", "TxtScripts", &info->useScripts);
if (info->useScripts && info->active)
forceUseScripts = true;
info->skipStartMenu = false;
modSettings.GetBool("", "SkipStartMenu", &info->skipStartMenu);
if (info->skipStartMenu && info->active)
skipStartMenu = true;
info->disableFocusPause = false;
modSettings.GetInteger("", "DisableFocusPause", &info->disableFocusPause);
if (info->disableFocusPause && info->active)
disableFocusPause |= info->disableFocusPause;
info->redirectSave = false;
modSettings.GetBool("", "RedirectSaveRAM", &info->redirectSave);
if (info->redirectSave) {
char path[0x100];
sprintf(path, "mods/%s/", folder.c_str());
info->savePath = path;
}
info->forceSonic1 = false;
modSettings.GetBool("", "ForceSonic1", &info->forceSonic1);
if (info->forceSonic1 && info->active)
Engine.forceSonic1 = true;
return true;
}
return false;
}
void ScanModFolder(ModInfo *info)
{
if (!info)
return;
char modBuf[0x100];
sprintf(modBuf, "%smods", modsPath);
fs::path modPath = resolvePath(modBuf);
const std::string modDir = modPath.string() + "/" + info->folder;
info->fileMap.clear();
// Check for Data/ replacements
fs::path dataPath = resolvePath(modDir + "/Data");
if (fs::exists(dataPath) && fs::is_directory(dataPath)) {
try {
auto data_rdi = fs::recursive_directory_iterator(dataPath);
for (auto &data_de : data_rdi) {
if (data_de.is_regular_file()) {
char modBuf[0x100];
StrCopy(modBuf, data_de.path().string().c_str());
char folderTest[4][0x10] = {
"Data/",
"Data\\",
"data/",
"data\\",
};
int tokenPos = -1;
for (int i = 0; i < 4; ++i) {
tokenPos = FindLastStringToken(modBuf, folderTest[i]);
if (tokenPos >= 0)
break;
}
if (tokenPos >= 0) {
char buffer[0x80];
for (int i = StrLength(modBuf); i >= tokenPos; --i) {
buffer[i - tokenPos] = modBuf[i] == '\\' ? '/' : modBuf[i];
}
// PrintLog(modBuf);
std::string path(buffer);
std::string modPath(modBuf);
char pathLower[0x100];
memset(pathLower, 0, sizeof(char) * 0x100);
for (int c = 0; c < path.size(); ++c) {
pathLower[c] = tolower(path.c_str()[c]);
}
info->fileMap.insert(std::pair<std::string, std::string>(pathLower, modBuf));
}
}
}
} catch (fs::filesystem_error fe) {
PrintLog("Data Folder Scanning Error: ");
PrintLog(fe.what());
}
}
// Check for Bytecode/ replacements
fs::path bytecodePath = resolvePath(modDir + "/Bytecode");
if (fs::exists(bytecodePath) && fs::is_directory(bytecodePath)) {
try {
auto data_rdi = fs::recursive_directory_iterator(bytecodePath);
for (auto &data_de : data_rdi) {
if (data_de.is_regular_file()) {
char modBuf[0x100];
StrCopy(modBuf, data_de.path().string().c_str());
char folderTest[4][0x10] = {
"Bytecode/",
"Bytecode\\",
"bytecode/",
"bytecode\\",
};
int tokenPos = -1;
for (int i = 0; i < 4; ++i) {
tokenPos = FindLastStringToken(modBuf, folderTest[i]);
if (tokenPos >= 0)
break;
}
if (tokenPos >= 0) {
char buffer[0x80];
for (int i = StrLength(modBuf); i >= tokenPos; --i) {
buffer[i - tokenPos] = modBuf[i] == '\\' ? '/' : modBuf[i];
}
// PrintLog(modBuf);
std::string path(buffer);
std::string modPath(modBuf);
char pathLower[0x100];
memset(pathLower, 0, sizeof(char) * 0x100);
for (int c = 0; c < path.size(); ++c) {
pathLower[c] = tolower(path.c_str()[c]);
}
info->fileMap.insert(std::pair<std::string, std::string>(pathLower, modBuf));
}
}
}
} catch (fs::filesystem_error fe) {
PrintLog("Bytecode Folder Scanning Error: ");
PrintLog(fe.what());
}
}
}
void SaveMods()
{
char modBuf[0x100];
sprintf(modBuf, "%smods", modsPath);
fs::path modPath = resolvePath(modBuf);
if (fs::exists(modPath) && fs::is_directory(modPath)) {
std::string mod_config = modPath.string() + "/modconfig.ini";
IniParser modConfig;
for (int m = 0; m < modList.size(); ++m) {
ModInfo *info = &modList[m];
modConfig.SetBool("mods", info->folder.c_str(), info->active);
}
modConfig.Write(mod_config.c_str(), false);
}
}
void RefreshEngine()
{
// Reload entire engine
Engine.LoadGameConfig("Data/Game/GameConfig.bin");
#if RETRO_USING_SDL2
if (Engine.window) {
char gameTitle[0x40];
sprintf(gameTitle, "%s%s", Engine.gameWindowText, Engine.usingDataFile_Config ? "" : " (Using Data Folder)");
SDL_SetWindowTitle(Engine.window, gameTitle);
}
#elif RETRO_USING_SDL1
char gameTitle[0x40];
sprintf(gameTitle, "%s%s", Engine.gameWindowText, Engine.usingDataFile_Config ? "" : " (Using Data Folder)");
SDL_WM_SetCaption(gameTitle, NULL);
#endif
ClearMeshData();
ClearTextures(true);
nativeEntityCountBackup = 0;
memset(backupEntityList, 0, sizeof(backupEntityList));
memset(objectEntityBackup, 0, sizeof(objectEntityBackup));
nativeEntityCountBackupS = 0;
memset(backupEntityListS, 0, sizeof(backupEntityListS));
memset(objectEntityBackupS, 0, sizeof(objectEntityBackupS));
for (int i = 0; i < FONTLIST_COUNT; ++i) {
fontList[i].count = 2;
}
ReleaseStageSfx();
ReleaseGlobalSfx();
LoadGlobalSfx();
InitLocalizedStrings();
for (nativeEntityPos = 0; nativeEntityPos < nativeEntityCount; ++nativeEntityPos) {
NativeEntity *entity = &objectEntityBank[activeEntityList[nativeEntityPos]];
entity->eventCreate(entity);
}
forceUseScripts = forceUseScripts_Config;
skipStartMenu = skipStartMenu_Config;
disableFocusPause = disableFocusPause_Config;
redirectSave = false;
Engine.forceSonic1 = false;
sprintf(savePath, "");
for (int m = 0; m < modList.size(); ++m) {
if (!modList[m].active)
continue;
if (modList[m].useScripts)
forceUseScripts = true;
if (modList[m].skipStartMenu)
skipStartMenu = true;
if (modList[m].disableFocusPause)
disableFocusPause |= modList[m].disableFocusPause;
if (modList[m].redirectSave) {
sprintf(savePath, "%s", modList[m].savePath.c_str());
redirectSave = true;
}
if (modList[m].forceSonic1)
Engine.forceSonic1 = true;
}
Engine.gameType = GAME_SONIC2;
if (strstr(Engine.gameWindowText, "Sonic 1") || Engine.forceSonic1) {
Engine.gameType = GAME_SONIC1;
}
achievementCount = 0;
if (Engine.gameType == GAME_SONIC1) {
AddAchievement("Ramp Ring Acrobatics",
"Without touching the ground,\rcollect all the rings in a\rtrapezoid formation in Green\rHill Zone Act 1");
AddAchievement("Blast Processing", "Clear Green Hill Zone Act 1\rin under 30 seconds");
AddAchievement("Secret of Marble Zone", "Travel though a secret\rroom in Marbale Zone Act 3");
AddAchievement("Block Buster", "Break 16 blocks in a row\rwithout stopping");
AddAchievement("Ring King", "Collect 200 Rings");
AddAchievement("Secret of Labyrinth Zone", "Activate and ride the\rhidden platform in\rLabyrinth Zone Act 1");
AddAchievement("Flawless Pursuit", "Clear the boss in Labyrinth\rZone without getting hurt");
AddAchievement("Bombs Away", "Defeat the boss in Starlight Zone\rusing only the see-saw bombs");
AddAchievement("Hidden Transporter", "Collect 50 Rings and take the hidden transporter path\rin Scrap Brain Act 2");
AddAchievement("Chaos Connoisseur", "Collect all the chaos\remeralds");
AddAchievement("One For the Road", "As a parting gift, land a\rfinal hit on Dr. Eggman's\rescaping Egg Mobile");
AddAchievement("Beat The Clock", "Clear the Time Attack\rmode in less than 45\rminutes");
}
else if (Engine.gameType == GAME_SONIC2) {
AddAchievement("Quick Run", "Complete Emerald Hill\rZone Act 1 in under 35\rseconds");
AddAchievement("100% Chemical Free", "Complete Chemical Plant\rwithout going underwater");
AddAchievement("Early Bird Special", "Collect all the Chaos\rEmeralds before Chemical\rPlant");
AddAchievement("Superstar", "Complete any Act as\rSuper Sonic");
AddAchievement("Hit it Big", "Get a jackpot on the Casino Night slot machines");
AddAchievement("Bop Non-stop", "Defeat any boss in 8\rconsecutive hits without\rtouching he ground");
AddAchievement("Perfectionist", "Get a Perfect Bonus by\rcollecting every Ring in an\rAct");
AddAchievement("A Secret Revealed", "Find and complete\rHidden Palace Zone");
AddAchievement("Head 2 Head", "Win a 2P Versus race\ragainst a friend");
AddAchievement("Metropolis Master", "Complete Any Metropolis\rZone Act without getting\rhurt");
AddAchievement("Scrambled Egg", "Defeat Dr. Eggman's Boss\rAttack mode in under 7\rminutes");
AddAchievement("Beat the Clock", "Complete the Time Attack\rmode in less than 45\rminutes");
}
SaveMods();
ReadSaveRAMData();
ReadUserdata();
}
void GetModCount() { scriptEng.checkResult = (int)modList.size(); }
void GetModName(int *textMenu, int *highlight, uint *id, int *unused)
{
if (*id >= modList.size())
return;
TextMenu *menu = &gameMenu[*textMenu];
menu->entryHighlight[menu->rowCount] = *highlight;
AddTextMenuEntry(menu, modList[*id].name.c_str());
}
void GetModDescription(int *textMenu, int *highlight, uint *id, int *unused)
{
if (*id >= modList.size())
return;
TextMenu *menu = &gameMenu[*textMenu];
menu->entryHighlight[menu->rowCount] = *highlight;
AddTextMenuEntry(menu, modList[*id].desc.c_str());
}
void GetModAuthor(int *textMenu, int *highlight, uint *id, int *unused)
{
if (*id >= modList.size())
return;
TextMenu *menu = &gameMenu[*textMenu];
menu->entryHighlight[menu->rowCount] = *highlight;
AddTextMenuEntry(menu, modList[*id].author.c_str());
}
void GetModVersion(int *textMenu, int *highlight, uint *id, int *unused)
{
if (*id >= modList.size())
return;
TextMenu *menu = &gameMenu[*textMenu];
menu->entryHighlight[menu->rowCount] = *highlight;
AddTextMenuEntry(menu, modList[*id].version.c_str());
}
void GetModID(int *unused, const char *modFolder)
{
for (int n = 0; n < (int)modList.size(); ++n) {
if (modList[n].folder == modFolder) {
scriptEng.checkResult = n;
return;
}
}
scriptEng.checkResult = -1;
}
void GetModActive(uint *id, int *unused)
{
scriptEng.checkResult = false;
if (*id >= modList.size())
return;
scriptEng.checkResult = modList[*id].active;
}
void SetModActive(uint *id, int *active)
{
if (*id >= modList.size())
return;
modList[*id].active = *active;
}
void MoveMod(uint *id, int *up)
{
if (!id || !up)
return;
int preOption = *id;
int option = preOption + (*up ? -1 : 1);
if (option < 0 || preOption < 0)
return;
if (option >= (int)modList.size() || preOption >= (int)modList.size())
return;
ModInfo swap = modList[preOption];
modList[preOption] = modList[option];
modList[option] = swap;
}
#endif
#if RETRO_USE_MOD_LOADER || !RETRO_USE_ORIGINAL_CODE
int GetSceneID(byte listID, const char *sceneName)
{
if (listID >= 3)
return -1;
char scnName[0x40];
int scnPos = 0;
int pos = 0;
while (sceneName[scnPos]) {
if (sceneName[scnPos] != ' ')
scnName[pos++] = sceneName[scnPos];
++scnPos;
}
scnName[pos] = 0;
for (int s = 0; s < stageListCount[listID]; ++s) {
char nameBuffer[0x40];
scnPos = 0;
pos = 0;
while (stageList[listID][s].name[scnPos]) {
if (stageList[listID][s].name[scnPos] != ' ')
nameBuffer[pos++] = stageList[listID][s].name[scnPos];
++scnPos;
}
nameBuffer[pos] = 0;
if (StrComp(scnName, nameBuffer)) {
return s;
}
}
return -1;
}
#endif