From 365191707407566841c1e37935619d260a4f67a7 Mon Sep 17 00:00:00 2001
From: BobTheBob <32057864+BobTheBob9@users.noreply.github.com>
Date: Sun, 7 Nov 2021 01:35:06 +0000
Subject: more modmanager improvements
---
NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj | 2 ++
.../NorthstarDedicatedTest.vcxproj.filters | 6 ++++++
NorthstarDedicatedTest/modmanager.cpp | 18 ++++++++++++------
NorthstarDedicatedTest/modmanager.h | 5 +++++
NorthstarDedicatedTest/pdef.cpp | 13 +++++++++++++
NorthstarDedicatedTest/pdef.h | 4 ++++
6 files changed, 42 insertions(+), 6 deletions(-)
create mode 100644 NorthstarDedicatedTest/pdef.cpp
create mode 100644 NorthstarDedicatedTest/pdef.h
diff --git a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj
index 4997b322..fc3d0fbe 100644
--- a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj
+++ b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj
@@ -315,6 +315,7 @@
+
@@ -352,6 +353,7 @@
Create
Create
+
diff --git a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters
index 99f45f39..59a5cb19 100644
--- a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters
+++ b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters
@@ -579,6 +579,9 @@
Header Files\Server
+
+ Header Files\Shared\Mods\Compiled
+
@@ -671,6 +674,9 @@
Source Files\Server
+
+ Source Files\Shared\Mods\Compiled
+
diff --git a/NorthstarDedicatedTest/modmanager.cpp b/NorthstarDedicatedTest/modmanager.cpp
index de06fb73..66e6d048 100644
--- a/NorthstarDedicatedTest/modmanager.cpp
+++ b/NorthstarDedicatedTest/modmanager.cpp
@@ -184,6 +184,11 @@ Mod::Mod(fs::path modDir, char* jsonBuf)
ModManager::ModManager()
{
+ // precaculated string hashes
+ // note: use backslashes for these, since we use lexically_normal for file paths which uses them
+ m_hScriptsRsonHash = std::hash{}("scripts\\vscripts\\scripts.rson");
+ m_hPdefHash = std::hash{}("cfg\\server\\persistent_player_data_version_231.pdef"); // this can have multiple versions, but we use 231 so that's what we hash
+
LoadMods();
}
@@ -356,8 +361,6 @@ void ModManager::LoadMods()
void ModManager::UnloadMods()
{
// clean up stuff from mods before we unload
-
- // do we need to dealloc individual entries in m_modFiles? idk, rework
m_modFiles.clear();
fs::remove_all(COMPILED_ASSETS_PATH);
@@ -373,6 +376,8 @@ void ModManager::UnloadMods()
mod.KeyValues.clear();
// 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::StringRef(mod.Name.c_str()), rapidjson::Value(false), m_enabledModsCfg.GetAllocator());
@@ -390,11 +395,13 @@ void ModManager::UnloadMods()
void ModManager::CompileAssetsForFile(const char* filename)
{
- fs::path path(filename);
+ size_t fileHash = std::hash{}(fs::path(filename).lexically_normal().string());
- if (!path.filename().compare("scripts.rson"))
+ if (fileHash == m_hScriptsRsonHash)
BuildScriptsRson();
- else //if (!strcmp((filename + strlen(filename)) - 3, "txt")) // check if it's a .txt
+ else if (fileHash == m_hPdefHash)
+ BuildPdef();
+ else
{
// check if we should build keyvalues, depending on whether any of our mods have patch kvs for this file
for (Mod& mod : m_loadedMods)
@@ -402,7 +409,6 @@ void ModManager::CompileAssetsForFile(const char* filename)
if (!mod.Enabled)
continue;
- size_t fileHash = std::hash{}(fs::path(filename).lexically_normal().string());
if (mod.KeyValues.find(fileHash) != mod.KeyValues.end())
{
TryBuildKeyValues(filename);
diff --git a/NorthstarDedicatedTest/modmanager.h b/NorthstarDedicatedTest/modmanager.h
index e1c246b1..cc3442ec 100644
--- a/NorthstarDedicatedTest/modmanager.h
+++ b/NorthstarDedicatedTest/modmanager.h
@@ -102,6 +102,10 @@ private:
bool m_hasEnabledModsCfg;
rapidjson::Document m_enabledModsCfg;
+ // precalculated hashes
+ size_t m_hScriptsRsonHash;
+ size_t m_hPdefHash;
+
public:
std::vector m_loadedMods;
std::unordered_map m_modFiles;
@@ -115,6 +119,7 @@ public:
// compile asset type stuff, these are done in files under Mods/Compiled/
void BuildScriptsRson();
void TryBuildKeyValues(const char* filename);
+ void BuildPdef();
};
void InitialiseModManager(HMODULE baseAddress);
diff --git a/NorthstarDedicatedTest/pdef.cpp b/NorthstarDedicatedTest/pdef.cpp
new file mode 100644
index 00000000..75ce3656
--- /dev/null
+++ b/NorthstarDedicatedTest/pdef.cpp
@@ -0,0 +1,13 @@
+#include "pch.h"
+#include "modmanager.h"
+#include "filesystem.h"
+#include "hookutils.h"
+#include "pdef.h"
+
+void ModManager::BuildPdef()
+{
+ spdlog::info("Building persistent_player_data_version_231.pdef...");
+
+ std::string originalPdef = ReadVPKOriginalFile(VPK_PDEF_PATH);
+ spdlog::info(originalPdef);
+}
\ No newline at end of file
diff --git a/NorthstarDedicatedTest/pdef.h b/NorthstarDedicatedTest/pdef.h
new file mode 100644
index 00000000..eb5f668b
--- /dev/null
+++ b/NorthstarDedicatedTest/pdef.h
@@ -0,0 +1,4 @@
+#pragma once
+
+const fs::path MOD_PDEF_PATH = COMPILED_ASSETS_PATH / "cfg/server/persistent_player_data_version_231.pdef";
+const char* VPK_PDEF_PATH = "cfg/server/persistent_player_data_version_231.pdef";
\ No newline at end of file
--
cgit v1.2.3