From e30fdda07d9fc3999136939a6db783cefc36af06 Mon Sep 17 00:00:00 2001 From: starfish <50672801+starfi5h@users.noreply.github.com> Date: Thu, 21 Mar 2024 23:41:02 +0800 Subject: [PATCH] Reset SaveManager.PlayerSaves each time the server starts --- NebulaNetwork/SaveManager.cs | 78 ++++++++++++++++++++---------------- NebulaNetwork/Server.cs | 5 +-- 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/NebulaNetwork/SaveManager.cs b/NebulaNetwork/SaveManager.cs index eb589e94d..61e38941f 100644 --- a/NebulaNetwork/SaveManager.cs +++ b/NebulaNetwork/SaveManager.cs @@ -91,25 +91,31 @@ private static void HandleAutoSave() File.Move(str1, str2); } - public static void LoadServerData() + public static void LoadServerData(bool loadSaveFile) { - var path = GameConfig.gameSaveFolder + DSPGame.LoadFile + FILE_EXTENSION; + playerSaves.Clear(); - // var playerManager = Multiplayer.Session.Network.PlayerManager; + if (!loadSaveFile) + { + return; + } + var path = GameConfig.gameSaveFolder + DSPGame.LoadFile + FILE_EXTENSION; if (!File.Exists(path)) { + Log.Info($"No server file"); return; } - var source = File.ReadAllBytes(path); - var netDataReader = new NetDataReader(source); - ushort revision; try { + var source = File.ReadAllBytes(path); + var netDataReader = new NetDataReader(source); + ushort revision; + var revString = netDataReader.GetString(); if (revString != "REV") { - throw new Exception(); + throw new Exception("Incorrect header"); } revision = netDataReader.GetUShort(); @@ -119,43 +125,45 @@ public static void LoadServerData() // Supported revision: 5~8 if (revision is < 5 or > REVISION) { - throw new Exception(); + throw new Exception($"Unsupport version {revision}"); } } - } - catch (Exception) - { - Log.Warn("Skipping server data from unsupported Nebula version..."); - return; - } - var playerNum = netDataReader.GetInt(); + var playerNum = netDataReader.GetInt(); - for (var i = 0; i < playerNum; i++) - { - var hash = netDataReader.GetString(); - PlayerData playerData = null; - switch (revision) + for (var i = 0; i < playerNum; i++) { - case REVISION: - playerData = netDataReader.Get(() => new PlayerData()); - break; - case >= 5: - playerData = new PlayerData(); - playerData.Import(netDataReader, revision); - break; - } + var hash = netDataReader.GetString(); + PlayerData playerData = null; + switch (revision) + { + case REVISION: + playerData = netDataReader.Get(() => new PlayerData()); + break; + case >= 5: + playerData = new PlayerData(); + playerData.Import(netDataReader, revision); + break; + } - if (!playerSaves.ContainsKey(hash) && playerData != null) - { - playerSaves.Add(hash, playerData); - } - else if (playerData == null) - { - Log.Warn($"could not load player data from unsupported save file revision {revision}"); + if (!playerSaves.ContainsKey(hash) && playerData != null) + { + playerSaves.Add(hash, playerData); + } + else if (playerData == null) + { + Log.Warn($"Could not load player data from unsupported save file revision {revision}"); + } } } + catch (Exception e) + { + playerSaves.Clear(); + Log.WarnInform("Skipping server data due to following excpetion:\n" + e.Message); + Log.Warn(e); + return; + } } public static bool TryAdd(string clientCertHash, IPlayerData playerData) diff --git a/NebulaNetwork/Server.cs b/NebulaNetwork/Server.cs index 029864dc5..4b018736c 100644 --- a/NebulaNetwork/Server.cs +++ b/NebulaNetwork/Server.cs @@ -174,10 +174,7 @@ internal void OnSocketDisconnection(INebulaConnection conn) public void Start() { - if (loadSaveFile) - { - SaveManager.LoadServerData(); - } + SaveManager.LoadServerData(loadSaveFile); foreach (var assembly in AssembliesUtils.GetNebulaAssemblies()) {