Skip to content

Commit

Permalink
Reset SaveManager.PlayerSaves each time the server starts
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Mar 23, 2024
1 parent c1946b7 commit e30fdda
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
78 changes: 43 additions & 35 deletions NebulaNetwork/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions NebulaNetwork/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down

0 comments on commit e30fdda

Please sign in to comment.