diff options
-rw-r--r-- | NorthstarDLL/core/filesystem/rpakfilesystem.cpp | 6 | ||||
-rw-r--r-- | NorthstarDLL/dedicated/dedicated.cpp | 7 | ||||
-rw-r--r-- | NorthstarDLL/logging/crashhandler.cpp | 2 | ||||
-rw-r--r-- | NorthstarDLL/logging/logging.cpp | 8 | ||||
-rw-r--r-- | NorthstarDLL/logging/logging.h | 2 | ||||
-rw-r--r-- | NorthstarDLL/mods/modmanager.cpp | 28 | ||||
-rw-r--r-- | NorthstarDLL/mods/modmanager.h | 3 | ||||
-rw-r--r-- | NorthstarDLL/squirrel/squirrel.cpp | 28 |
8 files changed, 77 insertions, 7 deletions
diff --git a/NorthstarDLL/core/filesystem/rpakfilesystem.cpp b/NorthstarDLL/core/filesystem/rpakfilesystem.cpp index 4284d5b7..2bddd8fe 100644 --- a/NorthstarDLL/core/filesystem/rpakfilesystem.cpp +++ b/NorthstarDLL/core/filesystem/rpakfilesystem.cpp @@ -266,6 +266,9 @@ void*, __fastcall, (const char* pPath, void* pCallback)) if (path.extension() == ".stbsp") { + if (IsDedicatedServer()) + return nullptr; + NS::log::rpak->info("LoadStreamBsp: {}", filename.string()); // resolve modded stbsp path so we can load mod stbsps @@ -278,6 +281,9 @@ void*, __fastcall, (const char* pPath, void* pCallback)) } else if (path.extension() == ".starpak") { + if (IsDedicatedServer()) + return nullptr; + // code for this is mostly stolen from above // unfortunately I can't find a way to get the rpak that is causing this function call, so I have to diff --git a/NorthstarDLL/dedicated/dedicated.cpp b/NorthstarDLL/dedicated/dedicated.cpp index 33a3f034..3d7d756c 100644 --- a/NorthstarDLL/dedicated/dedicated.cpp +++ b/NorthstarDLL/dedicated/dedicated.cpp @@ -48,9 +48,9 @@ void RunServer(CDedicatedExports* dedicated) // initialise engine g_pEngine->Frame(); - // add +map if not present + // add +map if no map loading command is present // don't manually execute this from cbuf as users may have it in their startup args anyway, easier just to run from stuffcmds if present - if (!Tier0::CommandLine()->CheckParm("+map")) + if (!Tier0::CommandLine()->CheckParm("+map") && !Tier0::CommandLine()->CheckParm("+launchplaylist")) Tier0::CommandLine()->AppendParm("+map", g_pCVar->FindVar("match_defaultMap")->GetString()); // re-run commandline @@ -278,7 +278,10 @@ void, __fastcall, (void* sqvm)) // atm, this will crash if not aborted, so this just closes more gracefully static ConVar* Cvar_fatal_script_errors = g_pCVar->FindVar("fatal_script_errors"); if (Cvar_fatal_script_errors->GetBool()) + { + NS::log::FlushLoggers(); abort(); + } } ON_DLL_LOAD_DEDI("server.dll", DedicatedServerGameDLL, (CModule module)) diff --git a/NorthstarDLL/logging/crashhandler.cpp b/NorthstarDLL/logging/crashhandler.cpp index 84f8ac99..3427e10a 100644 --- a/NorthstarDLL/logging/crashhandler.cpp +++ b/NorthstarDLL/logging/crashhandler.cpp @@ -70,6 +70,8 @@ void PrintExceptionLog(ExceptionLog& exc) "Northstar has crashed! Crash info can be found in R2Northstar/logs", "Northstar has crashed!", MB_ICONERROR | MB_OK | MB_SYSTEMMODAL); + + NS::log::FlushLoggers(); } std::string GetExceptionName(ExceptionLog& exc) diff --git a/NorthstarDLL/logging/logging.cpp b/NorthstarDLL/logging/logging.cpp index 6bb57170..8b906911 100644 --- a/NorthstarDLL/logging/logging.cpp +++ b/NorthstarDLL/logging/logging.cpp @@ -203,3 +203,11 @@ void InitialiseLogging() loggers.push_back(NS::log::rpak); loggers.push_back(NS::log::echo); } + +void NS::log::FlushLoggers() +{ + for (auto& logger : loggers) + logger->flush(); + + spdlog::default_logger()->flush(); +} diff --git a/NorthstarDLL/logging/logging.h b/NorthstarDLL/logging/logging.h index b710915e..2cccdde6 100644 --- a/NorthstarDLL/logging/logging.h +++ b/NorthstarDLL/logging/logging.h @@ -100,6 +100,8 @@ namespace NS::log extern std::shared_ptr<ColoredLogger> echo; extern std::shared_ptr<ColoredLogger> NORTHSTAR; + + void FlushLoggers(); }; // namespace NS::log void RegisterCustomSink(std::shared_ptr<CustomSink> sink); diff --git a/NorthstarDLL/mods/modmanager.cpp b/NorthstarDLL/mods/modmanager.cpp index 754f648b..5011f0fb 100644 --- a/NorthstarDLL/mods/modmanager.cpp +++ b/NorthstarDLL/mods/modmanager.cpp @@ -11,7 +11,7 @@ #include "rapidjson/error/en.h" #include "rapidjson/document.h" #include "rapidjson/ostreamwrapper.h" -#include "rapidjson/writer.h" +#include "rapidjson/prettywriter.h" #include <filesystem> #include <fstream> #include <string> @@ -199,6 +199,9 @@ Mod::Mod(fs::path modDir, char* jsonBuf) if (scriptObj["ServerCallback"].HasMember("After") && scriptObj["ServerCallback"]["After"].IsString()) callback.AfterCallback = scriptObj["ServerCallback"]["After"].GetString(); + if (scriptObj["ServerCallback"].HasMember("Destroy") && scriptObj["ServerCallback"]["Destroy"].IsString()) + callback.DestroyCallback = scriptObj["ServerCallback"]["Destroy"].GetString(); + script.Callbacks.push_back(callback); } @@ -213,6 +216,9 @@ Mod::Mod(fs::path modDir, char* jsonBuf) if (scriptObj["ClientCallback"].HasMember("After") && scriptObj["ClientCallback"]["After"].IsString()) callback.AfterCallback = scriptObj["ClientCallback"]["After"].GetString(); + if (scriptObj["ClientCallback"].HasMember("Destroy") && scriptObj["ClientCallback"]["Destroy"].IsString()) + callback.DestroyCallback = scriptObj["ClientCallback"]["Destroy"].GetString(); + script.Callbacks.push_back(callback); } @@ -227,6 +233,9 @@ Mod::Mod(fs::path modDir, char* jsonBuf) if (scriptObj["UICallback"].HasMember("After") && scriptObj["UICallback"]["After"].IsString()) callback.AfterCallback = scriptObj["UICallback"]["After"].GetString(); + if (scriptObj["UICallback"].HasMember("Destroy") && scriptObj["UICallback"]["Destroy"].IsString()) + callback.DestroyCallback = scriptObj["UICallback"]["Destroy"].GetString(); + script.Callbacks.push_back(callback); } @@ -343,6 +352,7 @@ void ModManager::LoadMods() // ensure dirs exist fs::remove_all(GetCompiledAssetsPath()); fs::create_directories(GetModFolderPath()); + fs::create_directories(GetRemoteModFolderPath()); m_DependencyConstants.clear(); @@ -363,9 +373,13 @@ void ModManager::LoadMods() } // get mod directories - for (fs::directory_entry dir : fs::directory_iterator(GetModFolderPath())) - if (fs::exists(dir.path() / "mod.json")) - modDirs.push_back(dir.path()); + std::filesystem::directory_iterator classicModsDir = fs::directory_iterator(GetModFolderPath()); + std::filesystem::directory_iterator remoteModsDir = fs::directory_iterator(GetRemoteModFolderPath()); + + for (std::filesystem::directory_iterator modIterator : {classicModsDir, remoteModsDir}) + for (fs::directory_entry dir : modIterator) + if (fs::exists(dir.path() / "mod.json")) + modDirs.push_back(dir.path()); for (fs::path modDir : modDirs) { @@ -762,7 +776,7 @@ void ModManager::UnloadMods() std::ofstream writeStream(GetNorthstarPrefix() + "/enabledmods.json"); rapidjson::OStreamWrapper writeStreamWrapper(writeStream); - rapidjson::Writer<rapidjson::OStreamWrapper> writer(writeStreamWrapper); + rapidjson::PrettyWriter<rapidjson::OStreamWrapper> writer(writeStreamWrapper); m_EnabledModsCfg.Accept(writer); // do we need to dealloc individual entries in m_loadedMods? idk, rework @@ -819,6 +833,10 @@ fs::path GetModFolderPath() { return fs::path(GetNorthstarPrefix() + MOD_FOLDER_SUFFIX); } +fs::path GetRemoteModFolderPath() +{ + return fs::path(GetNorthstarPrefix() + REMOTE_MOD_FOLDER_SUFFIX); +} fs::path GetCompiledAssetsPath() { return fs::path(GetNorthstarPrefix() + COMPILED_ASSETS_SUFFIX); diff --git a/NorthstarDLL/mods/modmanager.h b/NorthstarDLL/mods/modmanager.h index 57ae8581..d44a202f 100644 --- a/NorthstarDLL/mods/modmanager.h +++ b/NorthstarDLL/mods/modmanager.h @@ -41,6 +41,8 @@ struct ModScriptCallback std::string BeforeCallback; // called after the codecallback has finished executing std::string AfterCallback; + // called right before the vm is destroyed. + std::string DestroyCallback; }; struct ModScript @@ -184,6 +186,7 @@ class ModManager }; fs::path GetModFolderPath(); +fs::path GetRemoteModFolderPath(); fs::path GetCompiledAssetsPath(); extern ModManager* g_pModManager; diff --git a/NorthstarDLL/squirrel/squirrel.cpp b/NorthstarDLL/squirrel/squirrel.cpp index 60d0eab3..0bbc496f 100644 --- a/NorthstarDLL/squirrel/squirrel.cpp +++ b/NorthstarDLL/squirrel/squirrel.cpp @@ -1,5 +1,6 @@ #include "pch.h" #include "squirrel.h" +#include "logging/logging.h" #include "core/convar/concommand.h" #include "mods/modmanager.h" #include "dedicated/dedicated.h" @@ -212,7 +213,34 @@ template <ScriptContext context> void SquirrelManager<context>::VMCreated(CSquir template <ScriptContext context> void SquirrelManager<context>::VMDestroyed() { + // Call all registered mod Destroy callbacks. + if (g_pModManager) + { + NS::log::squirrel_logger<context>()->info("Calling Destroy callbacks for all loaded mods."); + + for (const Mod& loadedMod : g_pModManager->m_LoadedMods) + { + for (const ModScript& script : loadedMod.Scripts) + { + for (const ModScriptCallback& callback : script.Callbacks) + { + if (callback.Context != context || callback.DestroyCallback.length() == 0) + { + continue; + } + + Call(callback.DestroyCallback.c_str()); + NS::log::squirrel_logger<context>()->info("Executed Destroy callback {}.", callback.DestroyCallback); + } + } + } + } + + // Discard the previous vm and delete the message buffer. m_pSQVM = nullptr; + + delete g_pSquirrel<context>->messageBuffer; + g_pSquirrel<context>->messageBuffer = nullptr; } template <ScriptContext context> void SquirrelManager<context>::ExecuteCode(const char* pCode) |