From c5cf48bba33159fc268904688f29ed20c14b11b3 Mon Sep 17 00:00:00 2001
From: BobTheBob <32057864+BobTheBob9@users.noreply.github.com>
Date: Mon, 11 Oct 2021 23:35:40 +0100
Subject: add server script early persistence writes
---
.../NorthstarDedicatedTest.vcxproj | 2 ++
.../NorthstarDedicatedTest.vcxproj.filters | 6 +++++
NorthstarDedicatedTest/dllmain.cpp | 2 ++
NorthstarDedicatedTest/masterserver.cpp | 3 ++-
NorthstarDedicatedTest/masterserver.h | 10 +++++++-
NorthstarDedicatedTest/modlocalisation.cpp | 5 ++--
NorthstarDedicatedTest/modmanager.cpp | 5 ++++
NorthstarDedicatedTest/serverauthentication.cpp | 3 ++-
NorthstarDedicatedTest/serverauthentication.h | 1 +
NorthstarDedicatedTest/serverscriptpersistence.cpp | 30 ++++++++++++++++++++++
NorthstarDedicatedTest/serverscriptpersistence.h | 1 +
NorthstarDedicatedTest/squirrel.cpp | 5 ++++
NorthstarDedicatedTest/squirrel.h | 4 +++
13 files changed, 72 insertions(+), 5 deletions(-)
create mode 100644 NorthstarDedicatedTest/serverscriptpersistence.cpp
create mode 100644 NorthstarDedicatedTest/serverscriptpersistence.h
(limited to 'NorthstarDedicatedTest')
diff --git a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj
index 43eda97a..f0d10de3 100644
--- a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj
+++ b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj
@@ -316,6 +316,7 @@
+
@@ -357,6 +358,7 @@
+
diff --git a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters
index 8908df5f..8d39b3bd 100644
--- a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters
+++ b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters
@@ -576,6 +576,9 @@
Header Files\Shared\Convar
+
+ Header Files\Server
+
@@ -665,6 +668,9 @@
Source Files\Shared\Convar
+
+ Source Files\Server
+
diff --git a/NorthstarDedicatedTest/dllmain.cpp b/NorthstarDedicatedTest/dllmain.cpp
index 7bf1c091..cb3477e0 100644
--- a/NorthstarDedicatedTest/dllmain.cpp
+++ b/NorthstarDedicatedTest/dllmain.cpp
@@ -20,6 +20,7 @@
#include "modlocalisation.h"
#include "playlist.h"
#include "securitypatches.h"
+#include "serverscriptpersistence.h"
bool initialised = false;
@@ -91,6 +92,7 @@ void InitialiseNorthstar()
AddDllLoadCallback("server.dll", InitialiseServerSquirrel);
AddDllLoadCallback("engine.dll", InitialiseServerAuthentication);
AddDllLoadCallback("engine.dll", InitialiseSharedMasterServer);
+ AddDllLoadCallback("server.dll", InitialiseServerScriptPersistence);
AddDllLoadCallback("engine.dll", InitialisePlaylistHooks);
diff --git a/NorthstarDedicatedTest/masterserver.cpp b/NorthstarDedicatedTest/masterserver.cpp
index d7f966db..36b360a1 100644
--- a/NorthstarDedicatedTest/masterserver.cpp
+++ b/NorthstarDedicatedTest/masterserver.cpp
@@ -5,6 +5,7 @@
#include "hookutils.h"
#include "httplib.h"
#include "serverauthentication.h"
+#include "gameutils.h"
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
@@ -560,7 +561,7 @@ void CHostState__State_NewGameHook(CHostState* hostState)
Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec autoexec_ns_server", cmd_source_t::kCommandSrcCode);
Cbuf_Execute();
- g_MasterServerManager->AddSelfToServerList(Cvar_hostport->m_nValue, Cvar_ns_player_auth_port->m_nValue, Cvar_ns_server_name->m_pszString, Cvar_ns_server_desc->m_pszString, hostState->m_levelName, (char*)GetCurrentPlaylistName(), 16, Cvar_ns_server_password->m_pszString);
+ g_MasterServerManager->AddSelfToServerList(Cvar_hostport->m_nValue, Cvar_ns_player_auth_port->m_nValue, Cvar_ns_server_name->m_pszString, Cvar_ns_server_desc->m_pszString, hostState->m_levelName, (char*)GetCurrentPlaylistName(), std::stoi(GetCurrentPlaylistVar("max_players", true)), Cvar_ns_server_password->m_pszString);
g_ServerAuthenticationManager->StartPlayerAuthServer();
CHostState__State_NewGame(hostState);
diff --git a/NorthstarDedicatedTest/masterserver.h b/NorthstarDedicatedTest/masterserver.h
index d7dcf485..67f9a96f 100644
--- a/NorthstarDedicatedTest/masterserver.h
+++ b/NorthstarDedicatedTest/masterserver.h
@@ -2,6 +2,13 @@
#include "convar.h"
#include
+struct RemoteServerModInfo
+{
+public:
+ std::string modName;
+ std::string modServer;
+};
+
class RemoteServerInfo
{
public:
@@ -12,6 +19,7 @@ public:
std::string description;
char map[32];
char playlist[16];
+ std::vector requiredMods;
int playerCount;
int maxPlayers;
@@ -37,12 +45,12 @@ class MasterServerManager
private:
bool m_requestingServerList = false;
bool m_authenticatingWithGameServer = false;
- bool m_savingPersistentData = false;
public:
char m_ownServerId[33];
bool m_bRequireClientAuth = false;
+ bool m_savingPersistentData = false;
bool m_scriptRequestingServerList = false;
bool m_successfullyConnected = true;
diff --git a/NorthstarDedicatedTest/modlocalisation.cpp b/NorthstarDedicatedTest/modlocalisation.cpp
index d8168d0c..91f0416a 100644
--- a/NorthstarDedicatedTest/modlocalisation.cpp
+++ b/NorthstarDedicatedTest/modlocalisation.cpp
@@ -22,8 +22,9 @@ bool AddLocalisationFileHook(void* g_pVguiLocalize, const char* path, const char
loadModLocalisationFiles = false;
for (Mod* mod : g_ModManager->m_loadedMods)
- for (std::string& localisationFile : mod->LocalisationFiles)
- AddLocalisationFile(g_pVguiLocalize, localisationFile.c_str(), pathId, unknown);
+ if (mod->Enabled)
+ for (std::string& localisationFile : mod->LocalisationFiles)
+ AddLocalisationFile(g_pVguiLocalize, localisationFile.c_str(), pathId, unknown);
loadModLocalisationFiles = true;
diff --git a/NorthstarDedicatedTest/modmanager.cpp b/NorthstarDedicatedTest/modmanager.cpp
index b561c03b..42ad0545 100644
--- a/NorthstarDedicatedTest/modmanager.cpp
+++ b/NorthstarDedicatedTest/modmanager.cpp
@@ -245,6 +245,11 @@ void ModManager::LoadMods()
if (mod->wasReadSuccessfully)
{
spdlog::info("Loaded mod {} successfully", mod->Name);
+ if (mod->Enabled)
+ spdlog::info("Mod {} is enabled", mod->Name);
+ else
+ spdlog::info("Mod {} is disabled", mod->Name);
+
m_loadedMods.push_back(mod);
}
else
diff --git a/NorthstarDedicatedTest/serverauthentication.cpp b/NorthstarDedicatedTest/serverauthentication.cpp
index fed4eb82..fe3c6183 100644
--- a/NorthstarDedicatedTest/serverauthentication.cpp
+++ b/NorthstarDedicatedTest/serverauthentication.cpp
@@ -257,7 +257,8 @@ void CBaseClient__DisconnectHook(void* self, uint32_t unknownButAlways1, const c
if (strcmp(reason, "Connection closing"))
{
// dcing, write persistent data
- g_ServerAuthenticationManager->WritePersistentData(self);
+ if (!g_ServerAuthenticationManager->m_additionalPlayerData[self].needPersistenceWriteOnLeave)
+ g_ServerAuthenticationManager->WritePersistentData(self);
g_ServerAuthenticationManager->RemovePlayerAuthData(self); // won't do anything 99% of the time, but just in case
}
diff --git a/NorthstarDedicatedTest/serverauthentication.h b/NorthstarDedicatedTest/serverauthentication.h
index 38008e9b..c0f4bea2 100644
--- a/NorthstarDedicatedTest/serverauthentication.h
+++ b/NorthstarDedicatedTest/serverauthentication.h
@@ -17,6 +17,7 @@ struct AdditionalPlayerData
{
bool usingLocalPdata;
size_t pdataSize;
+ bool needPersistenceWriteOnLeave = true;
double lastClientCommandQuotaStart = 0;
int numClientCommandsInQuota = 0;
diff --git a/NorthstarDedicatedTest/serverscriptpersistence.cpp b/NorthstarDedicatedTest/serverscriptpersistence.cpp
new file mode 100644
index 00000000..4f7bba88
--- /dev/null
+++ b/NorthstarDedicatedTest/serverscriptpersistence.cpp
@@ -0,0 +1,30 @@
+#include "pch.h"
+#include "serverscriptpersistence.h"
+#include "squirrel.h"
+#include "masterserver.h"
+#include "serverauthentication.h"
+
+// void function NSEarlyWritePlayerPersistenceForLeave( entity player )
+SQInteger SQ_EarlyWritePlayerPersistenceForLeave(void* sqvm)
+{
+ void* player;
+ if (!ServerSq_getentity(sqvm, &player) || !g_ServerAuthenticationManager->m_additionalPlayerData.count(player))
+ return 0;
+
+ g_ServerAuthenticationManager->m_additionalPlayerData[player].needPersistenceWriteOnLeave = false;
+ g_ServerAuthenticationManager->WritePersistentData(player);
+ return 0;
+}
+
+// bool function NSIsWritingPlayerPersistence()
+SQInteger SQ_IsWritingPlayerPersistence(void* sqvm)
+{
+ ServerSq_pushbool(sqvm, g_MasterServerManager->m_savingPersistentData);
+ return 1;
+}
+
+void InitialiseServerScriptPersistence(HMODULE baseAddress)
+{
+ g_ServerSquirrelManager->AddFuncRegistration("void", "NSEarlyWritePlayerPersistenceForLeave", "entity player", "", SQ_EarlyWritePlayerPersistenceForLeave);
+ g_ServerSquirrelManager->AddFuncRegistration("bool", "NSIsWritingPlayerPersistence", "", "", SQ_IsWritingPlayerPersistence);
+}
\ No newline at end of file
diff --git a/NorthstarDedicatedTest/serverscriptpersistence.h b/NorthstarDedicatedTest/serverscriptpersistence.h
new file mode 100644
index 00000000..c4eb863f
--- /dev/null
+++ b/NorthstarDedicatedTest/serverscriptpersistence.h
@@ -0,0 +1 @@
+void InitialiseServerScriptPersistence(HMODULE baseAddress);
\ No newline at end of file
diff --git a/NorthstarDedicatedTest/squirrel.cpp b/NorthstarDedicatedTest/squirrel.cpp
index dfeb1805..f8d8cd33 100644
--- a/NorthstarDedicatedTest/squirrel.cpp
+++ b/NorthstarDedicatedTest/squirrel.cpp
@@ -84,6 +84,9 @@ sq_getfloatType ServerSq_getfloat;
sq_getboolType ClientSq_getbool;
sq_getboolType ServerSq_getbool;
+sq_getentityType ClientSq_getentity;
+sq_getentityType ServerSq_getentity;
+
template void ExecuteCodeCommand(const CCommand& args);
@@ -134,6 +137,7 @@ void InitialiseClientSquirrel(HMODULE baseAddress)
ClientSq_getinteger = (sq_getintegerType)((char*)baseAddress + 0x60E0);
ClientSq_getfloat = (sq_getfloatType)((char*)baseAddress + 0x6100);
ClientSq_getbool = (sq_getboolType)((char*)baseAddress + 0x6130);
+ ClientSq_getentity = (sq_getentityType)((char*)baseAddress + 0x12F80);
ENABLER_CREATEHOOK(hook, (char*)baseAddress + 0x26130, &CreateNewVMHook, reinterpret_cast(&ClientCreateNewVM)); // client createnewvm function
ENABLER_CREATEHOOK(hook, (char*)baseAddress + 0x26E70, &DestroyVMHook, reinterpret_cast(&ClientDestroyVM)); // client destroyvm function
@@ -164,6 +168,7 @@ void InitialiseServerSquirrel(HMODULE baseAddress)
ServerSq_getinteger = (sq_getintegerType)((char*)baseAddress + 0x60C0);
ServerSq_getfloat = (sq_getfloatType)((char*)baseAddress + 0x60E0);
ServerSq_getbool = (sq_getboolType)((char*)baseAddress + 0x6110);
+ ServerSq_getentity = (sq_getentityType)((char*)baseAddress + 0x203B0);
ENABLER_CREATEHOOK(hook, (char*)baseAddress + 0x1FE90, &SQPrintHook, reinterpret_cast(&ServerSQPrint)); // server print function
ENABLER_CREATEHOOK(hook, (char*)baseAddress + 0x260E0, &CreateNewVMHook, reinterpret_cast(&ServerCreateNewVM)); // server createnewvm function
diff --git a/NorthstarDedicatedTest/squirrel.h b/NorthstarDedicatedTest/squirrel.h
index 5ccab090..48eba539 100644
--- a/NorthstarDedicatedTest/squirrel.h
+++ b/NorthstarDedicatedTest/squirrel.h
@@ -118,6 +118,10 @@ typedef SQBool(*sq_getboolType)(void*, SQInteger stackpos);
extern sq_getboolType ClientSq_getbool;
extern sq_getboolType ServerSq_getbool;
+typedef SQBool(*sq_getentityType)(void*, void** entity);
+extern sq_getentityType ClientSq_getentity;
+extern sq_getentityType ServerSq_getentity;
+
template class SquirrelManager
{
--
cgit v1.2.3