2021-07-18 18:02:39 +02:00
|
|
|
#pragma once
|
2021-08-02 04:20:10 +02:00
|
|
|
#include "convar.h"
|
2022-10-18 00:26:07 +02:00
|
|
|
#include "memalloc.h"
|
|
|
|
#include "squirrel.h"
|
|
|
|
|
|
|
|
#include "rapidjson/document.h"
|
2021-07-18 18:02:39 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-07-20 03:14:17 +02:00
|
|
|
#include <filesystem>
|
|
|
|
|
2022-01-30 20:32:33 +01:00
|
|
|
const std::string MOD_FOLDER_SUFFIX = "/mods";
|
2022-10-18 00:26:07 +02:00
|
|
|
const std::string REMOTE_MOD_FOLDER_SUFFIX = "/runtime/remote/mods";
|
2021-07-23 16:36:18 +02:00
|
|
|
const fs::path MOD_OVERRIDE_DIR = "mod";
|
2022-01-30 20:32:33 +01:00
|
|
|
const std::string COMPILED_ASSETS_SUFFIX = "/runtime/compiled";
|
2021-07-18 18:02:39 +02:00
|
|
|
|
2021-07-23 03:32:18 +02:00
|
|
|
struct ModConVar
|
2021-07-18 18:02:39 +02:00
|
|
|
{
|
2021-07-20 03:14:17 +02:00
|
|
|
public:
|
2021-07-18 18:02:39 +02:00
|
|
|
std::string Name;
|
|
|
|
std::string DefaultValue;
|
|
|
|
std::string HelpString;
|
|
|
|
int Flags;
|
|
|
|
};
|
|
|
|
|
2021-07-23 03:32:18 +02:00
|
|
|
struct ModScriptCallback
|
2021-07-18 18:02:39 +02:00
|
|
|
{
|
2021-07-20 03:14:17 +02:00
|
|
|
public:
|
2021-12-20 22:18:55 +01:00
|
|
|
ScriptContext Context;
|
2021-07-18 18:02:39 +02:00
|
|
|
|
|
|
|
// called before the codecallback is executed
|
|
|
|
std::string BeforeCallback;
|
|
|
|
// called after the codecallback has finished executing
|
|
|
|
std::string AfterCallback;
|
|
|
|
};
|
|
|
|
|
2021-07-23 03:32:18 +02:00
|
|
|
struct ModScript
|
2021-07-18 18:02:39 +02:00
|
|
|
{
|
2021-07-20 03:14:17 +02:00
|
|
|
public:
|
2021-07-18 18:02:39 +02:00
|
|
|
std::string Path;
|
2022-10-18 00:26:07 +02:00
|
|
|
std::string RunOn;
|
2021-07-18 18:02:39 +02:00
|
|
|
|
2021-11-05 23:43:56 +01:00
|
|
|
std::vector<ModScriptCallback> Callbacks;
|
2021-07-18 18:02:39 +02:00
|
|
|
};
|
|
|
|
|
2022-03-15 06:57:15 +01:00
|
|
|
// these are pretty much identical, could refactor to use the same stuff?
|
|
|
|
struct ModVPKEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool m_bAutoLoad;
|
|
|
|
std::string m_sVpkPath;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ModRpakEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool m_bAutoLoad;
|
2022-04-09 03:53:05 +02:00
|
|
|
std::string m_sPakName;
|
2022-07-09 00:21:13 +02:00
|
|
|
std::string m_sLoadAfterPak;
|
2022-03-15 06:57:15 +01:00
|
|
|
};
|
|
|
|
|
2021-07-18 18:02:39 +02:00
|
|
|
class Mod
|
|
|
|
{
|
|
|
|
public:
|
2021-08-02 04:20:10 +02:00
|
|
|
// runtime stuff
|
2022-10-18 00:26:07 +02:00
|
|
|
bool m_bEnabled = true;
|
|
|
|
bool m_bWasReadSuccessfully = false;
|
|
|
|
fs::path m_ModDirectory;
|
|
|
|
// bool m_bIsRemote;
|
2021-07-23 03:32:18 +02:00
|
|
|
|
2021-07-18 18:02:39 +02:00
|
|
|
// mod.json stuff:
|
|
|
|
|
|
|
|
// the mod's name
|
|
|
|
std::string Name;
|
|
|
|
// the mod's description
|
|
|
|
std::string Description;
|
|
|
|
// the mod's version, should be in semver
|
|
|
|
std::string Version;
|
|
|
|
// a download link to the mod, for clients that try to join without the mod
|
|
|
|
std::string DownloadLink;
|
|
|
|
|
|
|
|
// whether clients need the mod to join servers running this mod
|
2021-07-25 05:43:11 +02:00
|
|
|
bool RequiredOnClient;
|
|
|
|
// the priority for this mod's files, mods with prio 0 are loaded first, then 1, then 2, etc
|
|
|
|
int LoadPriority;
|
2021-07-18 18:02:39 +02:00
|
|
|
|
|
|
|
// custom scripts used by the mod
|
2021-11-05 23:43:56 +01:00
|
|
|
std::vector<ModScript> Scripts;
|
2021-07-18 18:02:39 +02:00
|
|
|
// convars created by the mod
|
2021-10-01 18:16:42 +02:00
|
|
|
std::vector<ModConVar*> ConVars;
|
2021-08-09 23:21:46 +02:00
|
|
|
// custom localisation files created by the mod
|
|
|
|
std::vector<std::string> LocalisationFiles;
|
2021-07-18 18:02:39 +02:00
|
|
|
|
|
|
|
// other files:
|
|
|
|
|
2022-03-15 06:57:15 +01:00
|
|
|
std::vector<ModVPKEntry> Vpks;
|
2021-11-07 00:03:16 +01:00
|
|
|
std::unordered_map<size_t, std::string> KeyValues;
|
2022-03-15 06:57:15 +01:00
|
|
|
std::vector<std::string> BinkVideos;
|
2021-11-07 00:03:16 +01:00
|
|
|
std::string Pdiff; // only need one per mod
|
2021-07-20 03:14:17 +02:00
|
|
|
|
2022-03-15 06:57:15 +01:00
|
|
|
std::vector<ModRpakEntry> Rpaks;
|
2022-03-28 23:48:05 +02:00
|
|
|
std::unordered_map<std::string, std::string>
|
|
|
|
RpakAliases; // paks we alias to other rpaks, e.g. to load sp_crashsite paks on the map mp_crashsite
|
2021-07-20 03:14:17 +02:00
|
|
|
|
2022-10-18 00:26:07 +02:00
|
|
|
std::unordered_map<std::string, std::string> DependencyConstants;
|
2021-07-20 03:14:17 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
Mod(fs::path modPath, char* jsonBuf);
|
2021-07-18 18:02:39 +02:00
|
|
|
};
|
|
|
|
|
2021-07-23 03:32:18 +02:00
|
|
|
struct ModOverrideFile
|
|
|
|
{
|
|
|
|
public:
|
2022-10-18 00:26:07 +02:00
|
|
|
Mod* m_pOwningMod;
|
|
|
|
fs::path m_Path;
|
2021-07-23 03:32:18 +02:00
|
|
|
};
|
|
|
|
|
2021-07-18 18:02:39 +02:00
|
|
|
class ModManager
|
|
|
|
{
|
2021-10-06 22:46:32 +02:00
|
|
|
private:
|
2022-10-18 00:26:07 +02:00
|
|
|
bool m_bHasLoadedMods = false;
|
|
|
|
bool m_bHasEnabledModsCfg;
|
|
|
|
rapidjson_document m_EnabledModsCfg;
|
2021-10-06 22:46:32 +02:00
|
|
|
|
2021-11-07 02:35:06 +01:00
|
|
|
// precalculated hashes
|
|
|
|
size_t m_hScriptsRsonHash;
|
|
|
|
size_t m_hPdefHash;
|
|
|
|
|
2021-07-23 03:32:18 +02:00
|
|
|
public:
|
2022-10-18 00:26:07 +02:00
|
|
|
std::vector<Mod> m_LoadedMods;
|
|
|
|
std::unordered_map<std::string, ModOverrideFile> m_ModFiles;
|
|
|
|
std::unordered_map<std::string, std::string> m_DependencyConstants;
|
2021-07-18 18:02:39 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
ModManager();
|
|
|
|
void LoadMods();
|
2021-08-02 04:20:10 +02:00
|
|
|
void UnloadMods();
|
2022-10-18 00:26:07 +02:00
|
|
|
std::string NormaliseModFilePath(const fs::path path);
|
2021-07-23 16:36:18 +02:00
|
|
|
void CompileAssetsForFile(const char* filename);
|
|
|
|
|
2022-10-18 00:26:07 +02:00
|
|
|
// compile asset type stuff, these are done in files under runtime/compiled/
|
2021-07-23 16:36:18 +02:00
|
|
|
void BuildScriptsRson();
|
2021-08-02 04:20:10 +02:00
|
|
|
void TryBuildKeyValues(const char* filename);
|
2021-11-07 02:35:06 +01:00
|
|
|
void BuildPdef();
|
2021-07-18 18:02:39 +02:00
|
|
|
};
|
|
|
|
|
2022-01-30 20:32:33 +01:00
|
|
|
fs::path GetModFolderPath();
|
|
|
|
fs::path GetCompiledAssetsPath();
|
2021-07-18 18:02:39 +02:00
|
|
|
|
2022-10-18 00:26:07 +02:00
|
|
|
extern ModManager* g_pModManager;
|