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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
|
#include "pch.h"
#include "modmanager.h"
#include "core/convar/convar.h"
#include "core/convar/concommand.h"
#include "client/audio.h"
#include "masterserver/masterserver.h"
#include "core/filesystem/filesystem.h"
#include "core/filesystem/rpakfilesystem.h"
#include "config/profile.h"
#include "rapidjson/error/en.h"
#include "rapidjson/document.h"
#include "rapidjson/ostreamwrapper.h"
#include "rapidjson/writer.h"
#include <filesystem>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
ModManager* g_pModManager;
Mod::Mod(fs::path modDir, char* jsonBuf)
{
m_bWasReadSuccessfully = false;
m_ModDirectory = modDir;
rapidjson_document modJson;
modJson.Parse<rapidjson::ParseFlag::kParseCommentsFlag | rapidjson::ParseFlag::kParseTrailingCommasFlag>(jsonBuf);
// fail if parse error
if (modJson.HasParseError())
{
spdlog::error(
"Failed reading mod file {}: encountered parse error \"{}\" at offset {}",
(modDir / "mod.json").string(),
GetParseError_En(modJson.GetParseError()),
modJson.GetErrorOffset());
return;
}
// fail if it's not a json obj (could be an array, string, etc)
if (!modJson.IsObject())
{
spdlog::error("Failed reading mod file {}: file is not a JSON object", (modDir / "mod.json").string());
return;
}
// basic mod info
// name is required
if (!modJson.HasMember("Name"))
{
spdlog::error("Failed reading mod file {}: missing required member \"Name\"", (modDir / "mod.json").string());
return;
}
Name = modJson["Name"].GetString();
if (modJson.HasMember("Description"))
Description = modJson["Description"].GetString();
else
Description = "";
if (modJson.HasMember("Version"))
Version = modJson["Version"].GetString();
else
{
Version = "0.0.0";
spdlog::warn("Mod file {} is missing a version, consider adding a version", (modDir / "mod.json").string());
}
if (modJson.HasMember("DownloadLink"))
DownloadLink = modJson["DownloadLink"].GetString();
else
DownloadLink = "";
if (modJson.HasMember("RequiredOnClient"))
RequiredOnClient = modJson["RequiredOnClient"].GetBool();
else
RequiredOnClient = false;
if (modJson.HasMember("LoadPriority"))
LoadPriority = modJson["LoadPriority"].GetInt();
else
{
spdlog::info("Mod file {} is missing a LoadPriority, consider adding one", (modDir / "mod.json").string());
LoadPriority = 0;
}
// mod convars
if (modJson.HasMember("ConVars") && modJson["ConVars"].IsArray())
{
for (auto& convarObj : modJson["ConVars"].GetArray())
{
if (!convarObj.IsObject() || !convarObj.HasMember("Name") || !convarObj.HasMember("DefaultValue"))
continue;
// have to allocate this manually, otherwise convar registration will break
// unfortunately this causes us to leak memory on reload, unsure of a way around this rn
ModConVar convar;
convar.Name = convarObj["Name"].GetString();
convar.DefaultValue = convarObj["DefaultValue"].GetString();
if (convarObj.HasMember("HelpString"))
convar.HelpString = convarObj["HelpString"].GetString();
else
convar.HelpString = "";
convar.Flags = FCVAR_NONE;
if (convarObj.HasMember("Flags"))
{
// read raw integer flags
if (convarObj["Flags"].IsInt())
convar.Flags = convarObj["Flags"].GetInt();
else if (convarObj["Flags"].IsString())
{
// parse cvar flags from string
// example string: ARCHIVE_PLAYERPROFILE | GAMEDLL
convar.Flags |= ParseConVarFlagsString(convar.Name, convarObj["Flags"].GetString());
}
}
ConVars.push_back(convar);
}
}
// mod commands
if (modJson.HasMember("ConCommands") && modJson["ConCommands"].IsArray())
{
for (auto& concommandObj : modJson["ConCommands"].GetArray())
{
if (!concommandObj.IsObject() || !concommandObj.HasMember("Name") || !concommandObj.HasMember("Function") ||
!concommandObj.HasMember("Context"))
{
continue;
}
// have to allocate this manually, otherwise concommand registration will break
// unfortunately this causes us to leak memory on reload, unsure of a way around this rn
ModConCommand concommand;
concommand.Name = concommandObj["Name"].GetString();
concommand.Function = concommandObj["Function"].GetString();
concommand.Context = ScriptContextFromString(concommandObj["Context"].GetString());
if (concommand.Context == ScriptContext::INVALID)
{
spdlog::warn("Mod ConCommand {} has invalid context {}", concommand.Name, concommandObj["Context"].GetString());
continue;
}
if (concommandObj.HasMember("HelpString"))
concommand.HelpString = concommandObj["HelpString"].GetString();
else
concommand.HelpString = "";
concommand.Flags = FCVAR_NONE;
if (concommandObj.HasMember("Flags"))
{
// read raw integer flags
if (concommandObj["Flags"].IsInt())
{
concommand.Flags = concommandObj["Flags"].GetInt();
}
else if (concommandObj["Flags"].IsString())
{
// parse cvar flags from string
// example string: ARCHIVE_PLAYERPROFILE | GAMEDLL
concommand.Flags |= ParseConVarFlagsString(concommand.Name, concommandObj["Flags"].GetString());
}
}
ConCommands.push_back(concommand);
}
}
// mod scripts
if (modJson.HasMember("Scripts") && modJson["Scripts"].IsArray())
{
for (auto& scriptObj : modJson["Scripts"].GetArray())
{
if (!scriptObj.IsObject() || !scriptObj.HasMember("Path") || !scriptObj.HasMember("RunOn"))
continue;
ModScript script;
script.Path = scriptObj["Path"].GetString();
script.RunOn = scriptObj["RunOn"].GetString();
if (scriptObj.HasMember("ServerCallback") && scriptObj["ServerCallback"].IsObject())
{
ModScriptCallback callback;
callback.Context = ScriptContext::SERVER;
if (scriptObj["ServerCallback"].HasMember("Before") && scriptObj["ServerCallback"]["Before"].IsString())
callback.BeforeCallback = scriptObj["ServerCallback"]["Before"].GetString();
if (scriptObj["ServerCallback"].HasMember("After") && scriptObj["ServerCallback"]["After"].IsString())
callback.AfterCallback = scriptObj["ServerCallback"]["After"].GetString();
script.Callbacks.push_back(callback);
}
if (scriptObj.HasMember("ClientCallback") && scriptObj["ClientCallback"].IsObject())
{
ModScriptCallback callback;
callback.Context = ScriptContext::CLIENT;
if (scriptObj["ClientCallback"].HasMember("Before") && scriptObj["ClientCallback"]["Before"].IsString())
callback.BeforeCallback = scriptObj["ClientCallback"]["Before"].GetString();
if (scriptObj["ClientCallback"].HasMember("After") && scriptObj["ClientCallback"]["After"].IsString())
callback.AfterCallback = scriptObj["ClientCallback"]["After"].GetString();
script.Callbacks.push_back(callback);
}
if (scriptObj.HasMember("UICallback") && scriptObj["UICallback"].IsObject())
{
ModScriptCallback callback;
callback.Context = ScriptContext::UI;
if (scriptObj["UICallback"].HasMember("Before") && scriptObj["UICallback"]["Before"].IsString())
callback.BeforeCallback = scriptObj["UICallback"]["Before"].GetString();
if (scriptObj["UICallback"].HasMember("After") && scriptObj["UICallback"]["After"].IsString())
callback.AfterCallback = scriptObj["UICallback"]["After"].GetString();
script.Callbacks.push_back(callback);
}
Scripts.push_back(script);
}
}
if (modJson.HasMember("Localisation") && modJson["Localisation"].IsArray())
{
for (auto& localisationStr : modJson["Localisation"].GetArray())
{
if (!localisationStr.IsString())
continue;
LocalisationFiles.push_back(localisationStr.GetString());
}
}
if (modJson.HasMember("Dependencies") && modJson["Dependencies"].IsObject())
{
for (auto v = modJson["Dependencies"].MemberBegin(); v != modJson["Dependencies"].MemberEnd(); v++)
{
if (!v->name.IsString() || !v->value.IsString())
continue;
spdlog::info("Constant {} defined by {} for mod {}", v->name.GetString(), Name, v->value.GetString());
if (DependencyConstants.find(v->name.GetString()) != DependencyConstants.end() &&
v->value.GetString() != DependencyConstants[v->name.GetString()])
{
spdlog::error("A dependency constant with the same name already exists for another mod. Change the constant name.");
return;
}
if (DependencyConstants.find(v->name.GetString()) == DependencyConstants.end())
DependencyConstants.emplace(v->name.GetString(), v->value.GetString());
}
}
m_bWasReadSuccessfully = true;
}
ModManager::ModManager()
{
// precaculated string hashes
// note: use backslashes for these, since we use lexically_normal for file paths which uses them
m_hScriptsRsonHash = STR_HASH("scripts\\vscripts\\scripts.rson");
m_hPdefHash = STR_HASH(
"cfg\\server\\persistent_player_data_version_231.pdef" // this can have multiple versions, but we use 231 so that's what we hash
);
m_hKBActHash = STR_HASH("scripts\\kb_act.lst");
LoadMods();
}
template <ScriptContext context> auto ModConCommandCallback_Internal(std::string name, const CCommand& command)
{
if (g_pSquirrel<context>->m_pSQVM && g_pSquirrel<context>->m_pSQVM->sqvm)
{
std::vector<std::string> vArgs;
vArgs.reserve(command.ArgC());
for (int i = 1; i < command.ArgC(); i++)
vArgs.push_back(command.Arg(i));
g_pSquirrel<context>->AsyncCall(name, vArgs);
}
else
spdlog::warn("ConCommand \"{}\" was called while the associated Squirrel VM \"{}\" was unloaded", name, GetContextName(context));
}
auto ModConCommandCallback(const CCommand& command)
{
ModConCommand* pFoundCommand = nullptr;
std::string sCommandName = command.Arg(0);
// Find the mod this command belongs to
for (Mod& mod : g_pModManager->GetMods())
{
auto res = std::find_if(
mod.ConCommands.begin(),
mod.ConCommands.end(),
[&sCommandName](const ModConCommand* other) { return other->Name == sCommandName; });
if (res != mod.ConCommands.end())
{
pFoundCommand = &*res;
break;
}
}
if (!pFoundCommand)
return;
switch (pFoundCommand->Context)
{
case ScriptContext::CLIENT:
ModConCommandCallback_Internal<ScriptContext::CLIENT>(pFoundCommand->Function, command);
break;
case ScriptContext::SERVER:
ModConCommandCallback_Internal<ScriptContext::SERVER>(pFoundCommand->Function, command);
break;
case ScriptContext::UI:
ModConCommandCallback_Internal<ScriptContext::UI>(pFoundCommand->Function, command);
break;
};
}
void ModManager::LoadMods()
{
if (m_bHasLoadedMods)
UnloadMods();
std::vector<fs::path> modDirs;
// ensure dirs exist
fs::remove_all(GetCompiledAssetsPath());
fs::create_directories(GetModFolderPath());
m_DependencyConstants.clear();
// read enabled mods cfg
std::ifstream enabledModsStream(GetNorthstarPrefix() + "/enabledmods.json");
std::stringstream enabledModsStringStream;
if (!enabledModsStream.fail())
{
while (enabledModsStream.peek() != EOF)
enabledModsStringStream << (char)enabledModsStream.get();
enabledModsStream.close();
m_EnabledModsCfg.Parse<rapidjson::ParseFlag::kParseCommentsFlag | rapidjson::ParseFlag::kParseTrailingCommasFlag>(
enabledModsStringStream.str().c_str());
m_bHasEnabledModsCfg = m_EnabledModsCfg.IsObject();
}
// get mod directories
for (fs::directory_entry dir : fs::directory_iterator(GetModFolderPath()))
if (fs::exists(dir.path() / "mod.json"))
modDirs.push_back(dir.path());
for (fs::path modDir : modDirs)
{
// read mod json file
std::ifstream jsonStream(modDir / "mod.json");
std::stringstream jsonStringStream;
// fail if no mod json
if (jsonStream.fail())
{
spdlog::warn("Mod {} has a directory but no mod.json", modDir.string());
continue;
}
while (jsonStream.peek() != EOF)
jsonStringStream << (char)jsonStream.get();
jsonStream.close();
Mod mod(modDir, (char*)jsonStringStream.str().c_str());
for (auto& pair : mod.DependencyConstants)
{
if (m_DependencyConstants.find(pair.first) != m_DependencyConstants.end() && m_DependencyConstants[pair.first] != pair.second)
{
spdlog::error("Constant {} in mod {} already exists in another mod.", pair.first, mod.Name);
mod.m_bWasReadSuccessfully = false;
break;
}
if (m_DependencyConstants.find(pair.first) == m_DependencyConstants.end())
m_DependencyConstants.emplace(pair);
}
if (m_bHasEnabledModsCfg && m_EnabledModsCfg.HasMember(mod.Name.c_str()))
mod.m_bEnabled = m_EnabledModsCfg[mod.Name.c_str()].IsTrue();
else
mod.m_bEnabled = true;
if (mod.m_bWasReadSuccessfully)
{
spdlog::info("Loaded mod {} successfully", mod.Name);
if (mod.m_bEnabled)
spdlog::info("Mod {} is enabled", mod.Name);
else
spdlog::info("Mod {} is disabled", mod.Name);
m_LoadedMods.push_back(mod);
}
else
spdlog::warn("Skipping loading mod file {}", (modDir / "mod.json").string());
}
// sort by load prio, lowest-highest
std::sort(
m_ModLoadState.m_LoadedMods.begin(),
m_ModLoadState.m_LoadedMods.end(),
[](Mod& a, Mod& b) { return a.LoadPriority < b.LoadPriority; });
for (Mod& mod : m_LoadedMods)
{
if (!mod.m_bEnabled)
continue;
// register convars
for (ModConVar convar : mod.ConVars)
{
ConVar* pVar = R2::g_pCVar->FindVar(convar.Name.c_str());
// make sure convar isn't registered yet, if it is then modify its flags, helpstring etc
if (!pVar)
new ConVar(convar.Name.c_str(), convar.DefaultValue.c_str(), convar.Flags, convar.HelpString.c_str());
else
{
// TODO: should probably make sure this is actually a mod convar we're messing with
pVar->m_ConCommandBase.m_nFlags = convar.Flags;
// unfortunately this leaks memory and we can't really not leak memory because we don't know who allocated this
// so we can't delete it without risking a crash
if (convar.HelpString.compare(pVar->GetHelpText()))
{
int nHelpSize = convar.HelpString.size();
char* pNewHelpString = new char[nHelpSize + 1];
strncpy_s(pNewHelpString, nHelpSize + 1, convar.HelpString.c_str(), convar.HelpString.size());
pVar->m_ConCommandBase.m_pszHelpString = pNewHelpString;
}
if (convar.DefaultValue.compare(pVar->m_pszDefaultValue))
{
int nDefaultValueSize = convar.DefaultValue.size();
char* pNewDefaultValueString = new char[nDefaultValueSize + 1];
strncpy_s(pNewDefaultValueString, nDefaultValueSize + 1, convar.DefaultValue.c_str(), convar.DefaultValue.size());
pVar->m_pszDefaultValue = pNewDefaultValueString;
pVar->SetValue(pNewDefaultValueString);
}
}
}
for (ModConCommand command : mod.ConCommands)
{
// make sure command isnt't registered multiple times.
if (!R2::g_pCVar->FindCommand(command.Name.c_str()))
{
std::string funcName = command.Function;
RegisterConCommand(command.Name.c_str(), ModConCommandCallback, command.HelpString.c_str(), command.Flags);
}
}
// read vpk paths
if (fs::exists(mod.m_ModDirectory / "vpk"))
{
// read vpk cfg
std::ifstream vpkJsonStream(mod.m_ModDirectory / "vpk/vpk.json");
std::stringstream vpkJsonStringStream;
bool bUseVPKJson = false;
rapidjson::Document dVpkJson;
if (!vpkJsonStream.fail())
{
while (vpkJsonStream.peek() != EOF)
vpkJsonStringStream << (char)vpkJsonStream.get();
vpkJsonStream.close();
dVpkJson.Parse<rapidjson::ParseFlag::kParseCommentsFlag | rapidjson::ParseFlag::kParseTrailingCommasFlag>(
vpkJsonStringStream.str().c_str());
bUseVPKJson = !dVpkJson.HasParseError() && dVpkJson.IsObject();
}
for (fs::directory_entry file : fs::directory_iterator(mod.m_ModDirectory / "vpk"))
{
// a bunch of checks to make sure we're only adding dir vpks and their paths are good
// note: the game will literally only load vpks with the english prefix
if (fs::is_regular_file(file) && file.path().extension() == ".vpk" &&
file.path().string().find("english") != std::string::npos &&
file.path().string().find(".bsp.pak000_dir") != std::string::npos)
{
std::string formattedPath = file.path().filename().string();
// this really fucking sucks but it'll work
std::string vpkName = formattedPath.substr(strlen("english"), formattedPath.find(".bsp") - 3);
ModVPKEntry& modVpk = mod.Vpks.emplace_back();
modVpk.m_bAutoLoad = !bUseVPKJson || (dVpkJson.HasMember("Preload") && dVpkJson["Preload"].IsObject() &&
dVpkJson["Preload"].HasMember(vpkName) && dVpkJson["Preload"][vpkName].IsTrue());
modVpk.m_sVpkPath = (file.path().parent_path() / vpkName).string();
if (m_bHasLoadedMods && modVpk.m_bAutoLoad)
(*R2::g_pFilesystem)->m_vtable->MountVPK(*R2::g_pFilesystem, vpkName.c_str());
}
}
}
// read rpak paths
if (fs::exists(mod.m_ModDirectory / "paks"))
{
// read rpak cfg
std::ifstream rpakJsonStream(mod.m_ModDirectory / "paks/rpak.json");
std::stringstream rpakJsonStringStream;
bool bUseRpakJson = false;
rapidjson::Document dRpakJson;
if (!rpakJsonStream.fail())
{
while (rpakJsonStream.peek() != EOF)
rpakJsonStringStream << (char)rpakJsonStream.get();
rpakJsonStream.close();
dRpakJson.Parse<rapidjson::ParseFlag::kParseCommentsFlag | rapidjson::ParseFlag::kParseTrailingCommasFlag>(
rpakJsonStringStream.str().c_str());
bUseRpakJson = !dRpakJson.HasParseError() && dRpakJson.IsObject();
}
// read pak aliases
if (bUseRpakJson && dRpakJson.HasMember("Aliases") && dRpakJson["Aliases"].IsObject())
{
for (rapidjson::Value::ConstMemberIterator iterator = dRpakJson["Aliases"].MemberBegin();
iterator != dRpakJson["Aliases"].MemberEnd();
iterator++)
{
if (!iterator->name.IsString() || !iterator->value.IsString())
continue;
mod.RpakAliases.insert(std::make_pair(iterator->name.GetString(), iterator->value.GetString()));
}
}
for (fs::directory_entry file : fs::directory_iterator(mod.m_ModDirectory / "paks"))
{
// ensure we're only loading rpaks
if (fs::is_regular_file(file) && file.path().extension() == ".rpak")
{
std::string pakName(file.path().filename().string());
ModRpakEntry& modPak = mod.Rpaks.emplace_back();
modPak.m_bAutoLoad =
!bUseRpakJson || (dRpakJson.HasMember("Preload") && dRpakJson["Preload"].IsObject() &&
dRpakJson["Preload"].HasMember(pakName) && dRpakJson["Preload"][pakName].IsTrue());
// postload things
if (!bUseRpakJson ||
(dRpakJson.HasMember("Postload") && dRpakJson["Postload"].IsObject() && dRpakJson["Postload"].HasMember(pakName)))
modPak.m_sLoadAfterPak = dRpakJson["Postload"][pakName].GetString();
modPak.m_sPakName = pakName;
// read header of file and get the starpak paths
// this is done here as opposed to on starpak load because multiple rpaks can load a starpak
// and there is seemingly no good way to tell which rpak is causing the load of a starpak :/
std::ifstream rpakStream(file.path(), std::ios::binary);
// seek to the point in the header where the starpak reference size is
rpakStream.seekg(0x38, std::ios::beg);
int starpaksSize = 0;
rpakStream.read((char*)&starpaksSize, 2);
// seek to just after the header
rpakStream.seekg(0x58, std::ios::beg);
// read the starpak reference(s)
std::vector<char> buf(starpaksSize);
rpakStream.read(buf.data(), starpaksSize);
rpakStream.close();
// split the starpak reference(s) into strings to hash
std::string str = "";
for (int i = 0; i < starpaksSize; i++)
{
// if the current char is null, that signals the end of the current starpak path
if (buf[i] != 0x00)
{
str += buf[i];
}
else
{
// only add the string we are making if it isnt empty
if (!str.empty())
{
mod.StarpakPaths.push_back(STR_HASH(str));
spdlog::info("Mod {} registered starpak '{}'", mod.Name, str);
str = "";
}
}
}
// not using atm because we need to resolve path to rpak
// if (m_hasLoadedMods && modPak.m_bAutoLoad)
// g_pPakLoadManager->LoadPakAsync(pakName.c_str());
}
}
}
// read keyvalues paths
if (fs::exists(mod.m_ModDirectory / "keyvalues"))
{
for (fs::directory_entry file : fs::recursive_directory_iterator(mod.m_ModDirectory / "keyvalues"))
{
if (fs::is_regular_file(file))
{
std::string kvStr =
g_pModManager->NormaliseModFilePath(file.path().lexically_relative(mod.m_ModDirectory / "keyvalues"));
mod.KeyValues.emplace(STR_HASH(kvStr), kvStr);
}
}
}
// read pdiff
if (fs::exists(mod.m_ModDirectory / "mod.pdiff"))
{
std::ifstream pdiffStream(mod.m_ModDirectory / "mod.pdiff");
if (!pdiffStream.fail())
{
std::stringstream pdiffStringStream;
while (pdiffStream.peek() != EOF)
pdiffStringStream << (char)pdiffStream.get();
pdiffStream.close();
mod.Pdiff = pdiffStringStream.str();
}
}
// read bink video paths
if (fs::exists(mod.m_ModDirectory / "media"))
{
for (fs::directory_entry file : fs::recursive_directory_iterator(mod.m_ModDirectory / "media"))
if (fs::is_regular_file(file) && file.path().extension() == ".bik")
mod.BinkVideos.push_back(file.path().filename().string());
}
// try to load audio
if (fs::exists(mod.m_ModDirectory / "audio"))
{
for (fs::directory_entry file : fs::directory_iterator(mod.m_ModDirectory / "audio"))
{
if (fs::is_regular_file(file) && file.path().extension().string() == ".json")
{
if (!g_CustomAudioManager.TryLoadAudioOverride(file.path()))
{
spdlog::warn("Mod {} has an invalid audio def {}", mod.Name, file.path().filename().string());
continue;
}
}
}
}
}
// in a seperate loop because we register mod files in reverse order, since mods loaded later should have their files prioritised
for (int64_t i = m_LoadedMods.size() - 1; i > -1; i--)
{
if (!m_LoadedMods[i].m_bEnabled)
continue;
if (fs::exists(m_LoadedMods[i].m_ModDirectory / MOD_OVERRIDE_DIR))
{
for (fs::directory_entry file : fs::recursive_directory_iterator(m_LoadedMods[i].m_ModDirectory / MOD_OVERRIDE_DIR))
{
std::string path =
g_pModManager->NormaliseModFilePath(file.path().lexically_relative(m_LoadedMods[i].m_ModDirectory / MOD_OVERRIDE_DIR));
if (file.is_regular_file() && m_ModFiles.find(path) == m_ModFiles.end())
{
ModOverrideFile modFile;
modFile.m_pOwningMod = &m_LoadedMods[i];
modFile.m_Path = path;
m_ModFiles.insert(std::make_pair(path, modFile));
}
}
}
}
// build modinfo obj for masterserver
rapidjson_document modinfoDoc;
auto& alloc = modinfoDoc.GetAllocator();
modinfoDoc.SetObject();
modinfoDoc.AddMember("Mods", rapidjson::kArrayType, alloc);
int currentModIndex = 0;
for (Mod& mod : m_LoadedMods)
{
if (!mod.m_bEnabled || (!mod.RequiredOnClient && !mod.Pdiff.size()))
continue;
modinfoDoc["Mods"].PushBack(rapidjson::kObjectType, modinfoDoc.GetAllocator());
modinfoDoc["Mods"][currentModIndex].AddMember("Name", rapidjson::StringRef(&mod.Name[0]), modinfoDoc.GetAllocator());
modinfoDoc["Mods"][currentModIndex].AddMember("Version", rapidjson::StringRef(&mod.Version[0]), modinfoDoc.GetAllocator());
modinfoDoc["Mods"][currentModIndex].AddMember("RequiredOnClient", mod.RequiredOnClient, modinfoDoc.GetAllocator());
modinfoDoc["Mods"][currentModIndex].AddMember("Pdiff", rapidjson::StringRef(&mod.Pdiff[0]), modinfoDoc.GetAllocator());
currentModIndex++;
}
rapidjson::StringBuffer buffer;
buffer.Clear();
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
modinfoDoc.Accept(writer);
g_pMasterServerManager->m_sOwnModInfoJson = std::string(buffer.GetString());
m_bHasLoadedMods = true;
}
void ModManager::UnloadMods()
{
m_LastModLoadState = m_ModLoadState;
// clean up stuff from mods before we unload
m_ModFiles.clear();
fs::remove_all(GetCompiledAssetsPath());
g_CustomAudioManager.ClearAudioOverrides();
if (!m_bHasEnabledModsCfg)
m_EnabledModsCfg.SetObject();
for (Mod& mod : m_LoadedMods)
{
// remove all built kvs
for (std::pair<size_t, std::string> kvPaths : mod.KeyValues)
fs::remove(GetCompiledAssetsPath() / fs::path(kvPaths.second).lexically_relative(mod.m_ModDirectory));
// write to m_enabledModsCfg
// should we be doing this here or should scripts be doing this manually?
// main issue with doing this here is when we reload mods for connecting to a server, we write enabled mods, which isn't necessarily
// what we wanna do
if (!m_EnabledModsCfg.HasMember(mod.Name.c_str()))
m_EnabledModsCfg.AddMember(rapidjson_document::StringRefType(mod.Name.c_str()), false, m_EnabledModsCfg.GetAllocator());
m_EnabledModsCfg[mod.Name.c_str()].SetBool(mod.m_bEnabled);
}
std::ofstream writeStream(GetNorthstarPrefix() + "/enabledmods.json");
rapidjson::OStreamWrapper writeStreamWrapper(writeStream);
rapidjson::Writer<rapidjson::OStreamWrapper> writer(writeStreamWrapper);
m_EnabledModsCfg.Accept(writer);
// do we need to dealloc individual entries in m_loadedMods? idk, rework
m_LoadedMods.clear();
}
std::string ModManager::NormaliseModFilePath(const fs::path path)
{
std::string str = path.lexically_normal().string();
// force to lowercase
for (char& c : str)
if (c <= 'Z' && c >= 'A')
c = c - ('Z' - 'z');
return str;
}
void ModManager::CompileAssetsForFile(const char* filename)
{
size_t fileHash = STR_HASH(NormaliseModFilePath(fs::path(filename)));
if (fileHash == m_hScriptsRsonHash)
BuildScriptsRson();
else if (fileHash == m_hPdefHash)
{
// BuildPdef(); todo
}
else if (fileHash == m_hKBActHash)
BuildKBActionsList();
else
{
// check if we should build keyvalues, depending on whether any of our mods have patch kvs for this file
for (Mod& mod : GetMods())
{
if (!mod.m_bEnabled)
continue;
if (mod.KeyValues.find(fileHash) != mod.KeyValues.end())
{
TryBuildKeyValues(filename);
return;
}
}
}
}
void ConCommand_reload_mods(const CCommand& args)
{
g_pModManager->LoadMods();
}
fs::path GetModFolderPath()
{
return fs::path(GetNorthstarPrefix() + MOD_FOLDER_SUFFIX);
}
fs::path GetCompiledAssetsPath()
{
return fs::path(GetNorthstarPrefix() + COMPILED_ASSETS_SUFFIX);
}
ON_DLL_LOAD_RELIESON("engine.dll", ModManager, (ConCommand, MasterServer), (CModule module))
{
g_pModManager = new ModManager;
RegisterConCommand("reload_mods", ConCommand_reload_mods, "reloads mods", FCVAR_NONE);
}
|