diff options
Diffstat (limited to 'primedev/mods/compiled')
-rw-r--r-- | primedev/mods/compiled/kb_act.cpp | 44 | ||||
-rw-r--r-- | primedev/mods/compiled/modkeyvalues.cpp | 106 | ||||
-rw-r--r-- | primedev/mods/compiled/modpdef.cpp | 118 | ||||
-rw-r--r-- | primedev/mods/compiled/modscriptsrson.cpp | 65 |
4 files changed, 333 insertions, 0 deletions
diff --git a/primedev/mods/compiled/kb_act.cpp b/primedev/mods/compiled/kb_act.cpp new file mode 100644 index 00000000..6117fd28 --- /dev/null +++ b/primedev/mods/compiled/kb_act.cpp @@ -0,0 +1,44 @@ +#include "mods/modmanager.h" +#include "core/filesystem/filesystem.h" + +#include <fstream> + +const char* KB_ACT_PATH = "scripts\\kb_act.lst"; + +// compiles the file kb_act.lst, that defines entries for keybindings in the options menu +void ModManager::BuildKBActionsList() +{ + spdlog::info("Building kb_act.lst"); + + fs::create_directories(GetCompiledAssetsPath() / "scripts"); + std::ofstream soCompiledKeys(GetCompiledAssetsPath() / KB_ACT_PATH, std::ios::binary); + + // write vanilla file's content to compiled file + soCompiledKeys << ReadVPKOriginalFile(KB_ACT_PATH); + + for (Mod& mod : m_LoadedMods) + { + if (!mod.m_bEnabled) + continue; + + // write content of each modded file to compiled file + std::ifstream siModKeys(mod.m_ModDirectory / "kb_act.lst"); + + if (siModKeys.good()) + soCompiledKeys << siModKeys.rdbuf() << std::endl; + + siModKeys.close(); + } + + soCompiledKeys.close(); + + // push to overrides + ModOverrideFile overrideFile; + overrideFile.m_pOwningMod = nullptr; + overrideFile.m_Path = KB_ACT_PATH; + + if (m_ModFiles.find(KB_ACT_PATH) == m_ModFiles.end()) + m_ModFiles.insert(std::make_pair(KB_ACT_PATH, overrideFile)); + else + m_ModFiles[KB_ACT_PATH] = overrideFile; +} diff --git a/primedev/mods/compiled/modkeyvalues.cpp b/primedev/mods/compiled/modkeyvalues.cpp new file mode 100644 index 00000000..e44a81d3 --- /dev/null +++ b/primedev/mods/compiled/modkeyvalues.cpp @@ -0,0 +1,106 @@ +#include "mods/modmanager.h" +#include "core/filesystem/filesystem.h" + +#include <fstream> + +AUTOHOOK_INIT() + +void ModManager::TryBuildKeyValues(const char* filename) +{ + spdlog::info("Building KeyValues for file {}", filename); + + std::string normalisedPath = g_pModManager->NormaliseModFilePath(fs::path(filename)); + fs::path compiledPath = GetCompiledAssetsPath() / filename; + fs::path compiledDir = compiledPath.parent_path(); + fs::create_directories(compiledDir); + + fs::path kvPath(filename); + std::string ogFilePath = "mod_original_"; + ogFilePath += kvPath.filename().string(); + + std::string newKvs = "// AUTOGENERATED: MOD PATCH KV\n"; + + int patchNum = 0; + + // copy over patch kv files, and add #bases to new file, last mods' patches should be applied first + // note: #include should be identical but it's actually just broken, thanks respawn + for (int64_t i = m_LoadedMods.size() - 1; i > -1; i--) + { + if (!m_LoadedMods[i].m_bEnabled) + continue; + + size_t fileHash = STR_HASH(normalisedPath); + auto modKv = m_LoadedMods[i].KeyValues.find(fileHash); + if (modKv != m_LoadedMods[i].KeyValues.end()) + { + // should result in smth along the lines of #include "mod_patch_5_mp_weapon_car.txt" + + std::string patchFilePath = "mod_patch_"; + patchFilePath += std::to_string(patchNum++); + patchFilePath += "_"; + patchFilePath += kvPath.filename().string(); + + newKvs += "#base \""; + newKvs += patchFilePath; + newKvs += "\"\n"; + + fs::remove(compiledDir / patchFilePath); + + fs::copy_file(m_LoadedMods[i].m_ModDirectory / "keyvalues" / filename, compiledDir / patchFilePath); + } + } + + // add original #base last, #bases don't override preexisting keys, including the ones we've just done + newKvs += "#base \""; + newKvs += ogFilePath; + newKvs += "\"\n"; + + // load original file, so we can parse out the name of the root obj (e.g. WeaponData for weapons) + std::string originalFile = ReadVPKOriginalFile(filename); + + if (!originalFile.length()) + { + spdlog::warn("Tried to patch kv {} but no base kv was found!", ogFilePath); + return; + } + + char rootName[64]; + memset(rootName, 0, sizeof(rootName)); + + // iterate until we hit an ascii char that isn't in a # command or comment to get root obj name + int i = 0; + while (!(originalFile[i] >= 65 && originalFile[i] <= 122)) + { + // if we hit a comment or # thing, iterate until end of line + if (originalFile[i] == '/' || originalFile[i] == '#') + while (originalFile[i] != '\n') + i++; + + i++; + } + + int j = 0; + for (int j = 0; originalFile[i] >= 65 && originalFile[i] <= 122; j++) + rootName[j] = originalFile[i++]; + + // empty kv, all the other stuff gets #base'd + newKvs += rootName; + newKvs += "\n{\n}\n"; + + std::ofstream originalFileWriteStream(compiledDir / ogFilePath, std::ios::binary); + originalFileWriteStream << originalFile; + originalFileWriteStream.close(); + + std::ofstream writeStream(compiledPath, std::ios::binary); + writeStream << newKvs; + writeStream.close(); + + ModOverrideFile overrideFile; + overrideFile.m_pOwningMod = nullptr; + overrideFile.m_Path = normalisedPath; + + if (m_ModFiles.find(normalisedPath) == m_ModFiles.end()) + m_ModFiles.insert(std::make_pair(normalisedPath, overrideFile)); + else + m_ModFiles[normalisedPath] = overrideFile; +} diff --git a/primedev/mods/compiled/modpdef.cpp b/primedev/mods/compiled/modpdef.cpp new file mode 100644 index 00000000..d268a063 --- /dev/null +++ b/primedev/mods/compiled/modpdef.cpp @@ -0,0 +1,118 @@ +#include "mods/modmanager.h" +#include "core/filesystem/filesystem.h" + +#include <map> +#include <sstream> +#include <fstream> + +const fs::path MOD_PDEF_SUFFIX = "cfg/server/persistent_player_data_version_231.pdef"; +const char* VPK_PDEF_PATH = "cfg/server/persistent_player_data_version_231.pdef"; + +void ModManager::BuildPdef() +{ + spdlog::info("Building persistent_player_data_version_231.pdef..."); + + fs::path MOD_PDEF_PATH = fs::path(GetCompiledAssetsPath() / MOD_PDEF_SUFFIX); + + fs::remove(MOD_PDEF_PATH); + std::string pdef = ReadVPKOriginalFile(VPK_PDEF_PATH); + + for (Mod& mod : m_LoadedMods) + { + if (!mod.m_bEnabled || !mod.Pdiff.size()) + continue; + + // this code probably isn't going to be pretty lol + // refer to shared/pjson.js for an actual okish parser of the pdiff format + // but pretty much, $ENUM_ADD blocks define members added to preexisting enums + // $PROP_START ends the custom stuff, and from there it's just normal props we append to the pdef + + std::map<std::string, std::vector<std::string>> enumAdds; + + // read pdiff + bool inEnum = false; + bool inProp = false; + std::string currentEnum; + std::string currentLine; + std::istringstream pdiffStream(mod.Pdiff); + + while (std::getline(pdiffStream, currentLine)) + { + if (inProp) + { + // just append to pdef here + pdef += currentLine; + pdef += '\n'; + continue; + } + + // trim leading whitespace + size_t start = currentLine.find_first_not_of(" \n\r\t\f\v"); + size_t end = currentLine.find("//"); + if (end == std::string::npos) + end = currentLine.size() - 1; // last char + + if (!currentLine.size() || !currentLine.compare(start, 2, "//")) + continue; + + if (inEnum) + { + if (!currentLine.compare(start, 9, "$ENUM_END")) + inEnum = false; + else + enumAdds[currentEnum].push_back(currentLine); // only need to push_back current line, if there's syntax errors then game + // pdef parser will handle them + } + else if (!currentLine.compare(start, 9, "$ENUM_ADD")) + { + inEnum = true; + currentEnum = currentLine.substr(start + 10 /*$ENUM_ADD + 1*/, currentLine.size() - end - (start + 10)); + enumAdds.insert(std::make_pair(currentEnum, std::vector<std::string>())); + } + else if (!currentLine.compare(start, 11, "$PROP_START")) + { + inProp = true; + pdef += "\n// $PROP_START "; + pdef += mod.Name; + pdef += "\n"; + } + } + + // add new members to preexisting enums + // note: this code could 100% be messed up if people put //$ENUM_START comments and the like + // could make it protect against this, but honestly not worth atm + for (auto enumAdd : enumAdds) + { + std::string addStr; + for (std::string enumMember : enumAdd.second) + { + addStr += enumMember; + addStr += '\n'; + } + + // start of enum we're adding to + std::string startStr = "$ENUM_START "; + startStr += enumAdd.first; + + // insert enum values into enum + size_t insertIdx = pdef.find("$ENUM_END", pdef.find(startStr)); + pdef.reserve(addStr.size()); + pdef.insert(insertIdx, addStr); + } + } + + fs::create_directories(MOD_PDEF_PATH.parent_path()); + + std::ofstream writeStream(MOD_PDEF_PATH, std::ios::binary); + writeStream << pdef; + writeStream.close(); + + ModOverrideFile overrideFile; + overrideFile.m_pOwningMod = nullptr; + overrideFile.m_Path = VPK_PDEF_PATH; + + if (m_ModFiles.find(VPK_PDEF_PATH) == m_ModFiles.end()) + m_ModFiles.insert(std::make_pair(VPK_PDEF_PATH, overrideFile)); + else + m_ModFiles[VPK_PDEF_PATH] = overrideFile; +} diff --git a/primedev/mods/compiled/modscriptsrson.cpp b/primedev/mods/compiled/modscriptsrson.cpp new file mode 100644 index 00000000..d130745f --- /dev/null +++ b/primedev/mods/compiled/modscriptsrson.cpp @@ -0,0 +1,65 @@ +#include "mods/modmanager.h" +#include "core/filesystem/filesystem.h" +#include "squirrel/squirrel.h" + +#include <fstream> + +const std::string MOD_SCRIPTS_RSON_SUFFIX = "scripts/vscripts/scripts.rson"; +const char* VPK_SCRIPTS_RSON_PATH = "scripts\\vscripts\\scripts.rson"; + +void ModManager::BuildScriptsRson() +{ + spdlog::info("Building custom scripts.rson"); + fs::path MOD_SCRIPTS_RSON_PATH = fs::path(GetCompiledAssetsPath() / MOD_SCRIPTS_RSON_SUFFIX); + fs::remove(MOD_SCRIPTS_RSON_PATH); + + std::string scriptsRson = ReadVPKOriginalFile(VPK_SCRIPTS_RSON_PATH); + scriptsRson += "\n\n// START MODDED SCRIPT CONTENT\n\n"; // newline before we start custom stuff + + for (Mod& mod : m_LoadedMods) + { + if (!mod.m_bEnabled) + continue; + + // this isn't needed at all, just nice to have imo + scriptsRson += "// MOD: "; + scriptsRson += mod.Name; + scriptsRson += ":\n\n"; + + for (ModScript& script : mod.Scripts) + { + /* should create something with this format for each script + When: "CONTEXT" + Scripts: + [ + _coolscript.gnut + ]*/ + + scriptsRson += "When: \""; + scriptsRson += script.RunOn; + scriptsRson += "\"\n"; + + scriptsRson += "Scripts:\n[\n\t"; + scriptsRson += script.Path; + scriptsRson += "\n]\n\n"; + } + } + + fs::create_directories(MOD_SCRIPTS_RSON_PATH.parent_path()); + + std::ofstream writeStream(MOD_SCRIPTS_RSON_PATH, std::ios::binary); + writeStream << scriptsRson; + writeStream.close(); + + ModOverrideFile overrideFile; + overrideFile.m_pOwningMod = nullptr; + overrideFile.m_Path = VPK_SCRIPTS_RSON_PATH; + + if (m_ModFiles.find(VPK_SCRIPTS_RSON_PATH) == m_ModFiles.end()) + m_ModFiles.insert(std::make_pair(VPK_SCRIPTS_RSON_PATH, overrideFile)); + else + m_ModFiles[VPK_SCRIPTS_RSON_PATH] = overrideFile; + + // todo: for preventing dupe scripts in scripts.rson, we could actually parse when conditions with the squirrel vm, just need a way to + // get a result out of squirrelmanager.ExecuteCode this would probably be the best way to do this, imo +} |