aboutsummaryrefslogtreecommitdiff
path: root/primedev/mods/mod.cpp
blob: a149b61135e67f61ceb48da480f3cc3fa90f382b (plain)
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
#include "rapidjson/error/en.h"

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);

	spdlog::info("Loading mod file at path '{}'", modDir.string());

	// 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();
	spdlog::info("Loading mod '{}'", Name);

	// Don't load blacklisted mods
	if (!strstr(GetCommandLineA(), "-nomodblacklist") && MODS_BLACKLIST.find(Name) != std::end(MODS_BLACKLIST))
	{
		spdlog::warn("Skipping blacklisted mod \"{}\"!", Name);
		return;
	}

	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;
	}

	// Parse all array fields
	ParseConVars(modJson);
	ParseConCommands(modJson);
	ParseScripts(modJson);
	ParseLocalization(modJson);
	ParseDependencies(modJson);
	ParsePluginDependencies(modJson);
	ParseInitScript(modJson);

	// A mod is remote if it's located in the remote mods folder
	m_bIsRemote = m_ModDirectory.generic_string().find(GetRemoteModFolderPath().generic_string()) != std::string::npos;

	m_bWasReadSuccessfully = true;
}

void Mod::ParseConVars(rapidjson_document& json)
{
	if (!json.HasMember("ConVars"))
		return;

	if (!json["ConVars"].IsArray())
	{
		spdlog::warn("'ConVars' field is not an array, skipping...");
		return;
	}

	for (auto& convarObj : json["ConVars"].GetArray())
	{
		if (!convarObj.IsObject())
		{
			spdlog::warn("ConVar is not an object, skipping...");
			continue;
		}
		if (!convarObj.HasMember("Name"))
		{
			spdlog::warn("ConVar does not have a Name, skipping...");
			continue;
		}
		// from here on, the ConVar can be referenced by name in logs
		if (!convarObj.HasMember("DefaultValue"))
		{
			spdlog::warn("ConVar '{}' does not have a DefaultValue, skipping...", convarObj["Name"].GetString());
			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 = new ModConVar;
		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);

		spdlog::info("'{}' contains ConVar '{}'", Name, convar->Name);
	}
}

void Mod::ParseConCommands(rapidjson_document& json)
{
	if (!json.HasMember("ConCommands"))
		return;

	if (!json["ConCommands"].IsArray())
	{
		spdlog::warn("'ConCommands' field is not an array, skipping...");
		return;
	}

	for (auto& concommandObj : json["ConCommands"].GetArray())
	{
		if (!concommandObj.IsObject())
		{
			spdlog::warn("ConCommand is not an object, skipping...");
			continue;
		}
		if (!concommandObj.HasMember("Name"))
		{
			spdlog::warn("ConCommand does not have a Name, skipping...");
			continue;
		}
		// from here on, the ConCommand can be referenced by name in logs
		if (!concommandObj.HasMember("Function"))
		{
			spdlog::warn("ConCommand '{}' does not have a Function, skipping...", concommandObj["Name"].GetString());
			continue;
		}
		if (!concommandObj.HasMember("Context"))
		{
			spdlog::warn("ConCommand '{}' does not have a Context, skipping...", concommandObj["Name"].GetString());
			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 = new ModConCommand;
		concommand->Name = concommandObj["Name"].GetString();
		concommand->Function = concommandObj["Function"].GetString();
		concommand->Context = ScriptContextFromString(concommandObj["Context"].GetString());
		if (concommand->Context == ScriptContext::INVALID)
		{
			spdlog::warn("ConCommand '{}' has invalid context '{}', skipping...", 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);

		spdlog::info("'{}' contains ConCommand '{}'", Name, concommand->Name);
	}
}

void Mod::ParseScripts(rapidjson_document& json)
{
	if (!json.HasMember("Scripts"))
		return;

	if (!json["Scripts"].IsArray())
	{
		spdlog::warn("'Scripts' field is not an array, skipping...");
		return;
	}

	for (auto& scriptObj : json["Scripts"].GetArray())
	{
		if (!scriptObj.IsObject())
		{
			spdlog::warn("Script is not an object, skipping...");
			continue;
		}
		if (!scriptObj.HasMember("Path"))
		{
			spdlog::warn("Script does not have a Path, skipping...");
			continue;
		}
		// from here on, the Path for a script is used as it's name in logs
		if (!scriptObj.HasMember("RunOn"))
		{
			// "a RunOn" sounds dumb but anything else doesn't match the format of the warnings...
			// this is the best i could think of within 20 seconds
			spdlog::warn("Script '{}' does not have a RunOn field, skipping...", scriptObj["Path"].GetString());
			continue;
		}

		ModScript script;

		script.Path = scriptObj["Path"].GetString();
		script.RunOn = scriptObj["RunOn"].GetString();

		if (scriptObj.HasMember("ServerCallback"))
		{
			if (scriptObj["ServerCallback"].IsObject())
			{
				ModScriptCallback callback;
				callback.Context = ScriptContext::SERVER;

				if (scriptObj["ServerCallback"].HasMember("Before"))
				{
					if (scriptObj["ServerCallback"]["Before"].IsString())
						callback.BeforeCallback = scriptObj["ServerCallback"]["Before"].GetString();
					else
						spdlog::warn("'Before' ServerCallback for script '{}' is not a string, skipping...", scriptObj["Path"].GetString());
				}

				if (scriptObj["ServerCallback"].HasMember("After"))
				{
					if (scriptObj["ServerCallback"]["After"].IsString())
						callback.AfterCallback = scriptObj["ServerCallback"]["After"].GetString();
					else
						spdlog::warn("'After' ServerCallback for script '{}' is not a string, skipping...", scriptObj["Path"].GetString());
				}

				if (scriptObj["ServerCallback"].HasMember("Destroy"))
				{
					if (scriptObj["ServerCallback"]["Destroy"].IsString())
						callback.DestroyCallback = scriptObj["ServerCallback"]["Destroy"].GetString();
					else
						spdlog::warn(
							"'Destroy' ServerCallback for script '{}' is not a string, skipping...", scriptObj["Path"].GetString());
				}

				script.Callbacks.push_back(callback);
			}
			else
			{
				spdlog::warn("ServerCallback for script '{}' is not an object, skipping...", scriptObj["Path"].GetString());
			}
		}

		if (scriptObj.HasMember("ClientCallback"))
		{
			if (scriptObj["ClientCallback"].IsObject())
			{
				ModScriptCallback callback;
				callback.Context = ScriptContext::CLIENT;

				if (scriptObj["ClientCallback"].HasMember("Before"))
				{
					if (scriptObj["ClientCallback"]["Before"].IsString())
						callback.BeforeCallback = scriptObj["ClientCallback"]["Before"].GetString();
					else
						spdlog::warn("'Before' ClientCallback for script '{}' is not a string, skipping...", scriptObj["Path"].GetString());
				}

				if (scriptObj["ClientCallback"].HasMember("After"))
				{
					if (scriptObj["ClientCallback"]["After"].IsString())
						callback.AfterCallback = scriptObj["ClientCallback"]["After"].GetString();
					else
						spdlog::warn("'After' ClientCallback for script '{}' is not a string, skipping...", scriptObj["Path"].GetString());
				}

				if (scriptObj["ClientCallback"].HasMember("Destroy"))
				{
					if (scriptObj["ClientCallback"]["Destroy"].IsString())
						callback.DestroyCallback = scriptObj["ClientCallback"]["Destroy"].GetString();
					else
						spdlog::warn(
							"'Destroy' ClientCallback for script '{}' is not a string, skipping...", scriptObj["Path"].GetString());
				}

				script.Callbacks.push_back(callback);
			}
			else
			{
				spdlog::warn("ClientCallback for script '{}' is not an object, skipping...", scriptObj["Path"].GetString());
			}
		}

		if (scriptObj.HasMember("UICallback"))
		{
			if (scriptObj["UICallback"].IsObject())
			{
				ModScriptCallback callback;
				callback.Context = ScriptContext::UI;

				if (scriptObj["UICallback"].HasMember("Before"))
				{
					if (scriptObj["UICallback"]["Before"].IsString())
						callback.BeforeCallback = scriptObj["UICallback"]["Before"].GetString();
					else
						spdlog::warn("'Before' UICallback for script '{}' is not a string, skipping...", scriptObj["Path"].GetString());
				}

				if (scriptObj["UICallback"].HasMember("After"))
				{
					if (scriptObj["UICallback"]["After"].IsString())
						callback.AfterCallback = scriptObj["UICallback"]["After"].GetString();
					else
						spdlog::warn("'After' UICallback for script '{}' is not a string, skipping...", scriptObj["Path"].GetString());
				}

				if (scriptObj["UICallback"].HasMember("Destroy"))
				{
					if (scriptObj["UICallback"]["Destroy"].IsString())
						callback.DestroyCallback = scriptObj["UICallback"]["Destroy"].GetString();
					else
						spdlog::warn("'Destroy' UICallback for script '{}' is not a string, skipping...", scriptObj["Path"].GetString());
				}

				script.Callbacks.push_back(callback);
			}
			else
			{
				spdlog::warn("UICallback for script '{}' is not an object, skipping...", scriptObj["Path"].GetString());
			}
		}

		Scripts.push_back(script);

		spdlog::info("'{}' contains Script '{}'", Name, script.Path);
	}
}

void Mod::ParseLocalization(rapidjson_document& json)
{
	if (!json.HasMember("Localisation"))
		return;

	if (!json["Localisation"].IsArray())
	{
		spdlog::warn("'Localisation' field is not an array, skipping...");
		return;
	}

	for (auto& localisationStr : json["Localisation"].GetArray())
	{
		if (!localisationStr.IsString())
		{
			// not a string but we still GetString() to log it :trol:
			spdlog::warn("Localisation '{}' is not a string, skipping...", localisationStr.GetString());
			continue;
		}

		LocalisationFiles.push_back(localisationStr.GetString());

		spdlog::info("'{}' registered Localisation '{}'", Name, localisationStr.GetString());
	}
}

void Mod::ParseDependencies(rapidjson_document& json)
{
	if (!json.HasMember("Dependencies"))
		return;

	if (!json["Dependencies"].IsObject())
	{
		spdlog::warn("'Dependencies' field is not an object, skipping...");
		return;
	}

	for (auto v = json["Dependencies"].MemberBegin(); v != json["Dependencies"].MemberEnd(); v++)
	{
		if (!v->name.IsString())
		{
			spdlog::warn("Dependency constant '{}' is not a string, skipping...", v->name.GetString());
			continue;
		}
		if (!v->value.IsString())
		{
			spdlog::warn("Dependency constant '{}' is not a string, skipping...", v->value.GetString());
			continue;
		}

		if (DependencyConstants.find(v->name.GetString()) != DependencyConstants.end() &&
			v->value.GetString() != DependencyConstants[v->name.GetString()])
		{
			// this is fatal because otherwise the mod will probably try to use functions that dont exist,
			// which will cause errors further down the line that are harder to debug
			spdlog::error(
				"'{}' attempted to register a dependency constant '{}' for '{}' that already exists for '{}'. "
				"Change the constant name.",
				Name,
				v->name.GetString(),
				v->value.GetString(),
				DependencyConstants[v->name.GetString()]);
			return;
		}

		if (DependencyConstants.find(v->name.GetString()) == DependencyConstants.end())
			DependencyConstants.emplace(v->name.GetString(), v->value.GetString());

		spdlog::info("'{}' registered dependency constant '{}' for mod '{}'", Name, v->name.GetString(), v->value.GetString());
	}
}

void Mod::ParsePluginDependencies(rapidjson_document& json)
{
	if (!json.HasMember("PluginDependencies"))
		return;

	if (!json["PluginDependencies"].IsArray())
	{
		spdlog::warn("'PluginDependencies' field is not an object, skipping...");
		return;
	}

	for (auto& name : json["PluginDependencies"].GetArray())
	{
		if (!name.IsString())
			continue;

		spdlog::info("Plugin Constant {} defined by {}", name.GetString(), Name);

		PluginDependencyConstants.push_back(name.GetString());
	}
}

void Mod::ParseInitScript(rapidjson_document& json)
{
	if (!json.HasMember("InitScript"))
		return;

	if (!json["InitScript"].IsString())
	{
		spdlog::warn("'InitScript' field is not a string, skipping...");
		return;
	}

	initScript = json["InitScript"].GetString();
}