Skip to content

Commit

Permalink
Show server last save time in client esc menu
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Jun 19, 2024
1 parent 7ef1c5c commit 702cd84
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
20 changes: 20 additions & 0 deletions NebulaModel/Packets/GameStates/GameStateSaveInfoPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#region

using NebulaAPI.Packets;

#endregion

namespace NebulaModel.Packets.GameStates;

[HidePacketInDebugLogs]
public class GameStateSaveInfoPacket
{
public GameStateSaveInfoPacket() { }

public GameStateSaveInfoPacket(long lastSaveTime)
{
LastSaveTime = lastSaveTime;
}

public long LastSaveTime { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#region

using NebulaAPI.Packets;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.GameStates;
using NebulaWorld.GameStates;

#endregion

namespace NebulaNetwork.PacketProcessors.GameStates;

[RegisterPacketProcessor]
public class GameStateSaveInfoProcessor : PacketProcessor<GameStateSaveInfoPacket>
{
protected override void ProcessPacket(GameStateSaveInfoPacket packet, NebulaConnection conn)
{
GameStatesManager.LastSaveTime = packet.LastSaveTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using NebulaAPI.Packets;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.GameStates;
using NebulaModel.Packets.Session;
using NebulaWorld.GameStates;

#endregion

Expand Down Expand Up @@ -74,5 +76,7 @@ protected override void ProcessPacket(GlobalGameDataRequest packet, NebulaConnec
conn.SendPacket(new GlobalGameDataResponse(
GlobalGameDataResponse.EDataType.Ready, writer.CloseAndGetBytes()));
}

conn.SendPacket(new GameStateSaveInfoPacket(GameStatesManager.LastSaveTime));
}
}
12 changes: 11 additions & 1 deletion NebulaPatcher/Patches/Dynamic/GameSave_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#region

using System.Diagnostics.CodeAnalysis;
using HarmonyLib;
using NebulaModel;
using NebulaNetwork;
using NebulaModel.Packets.GameStates;
using NebulaWorld;
using NebulaWorld.GameStates;

#endregion

Expand Down Expand Up @@ -46,6 +48,9 @@ public static void SaveCurrentGame_Postfix()
(GameMain.mainPlayer.sandCount, Multiplayer.Session.LocalPlayer.Data.Mecha.SandCount) = (
Multiplayer.Session.LocalPlayer.Data.Mecha.SandCount, GameMain.mainPlayer.sandCount);
}
// Update last save time in clients
GameStatesManager.LastSaveTime = GameMain.gameTick;
Multiplayer.Session.Server.SendPacket(new GameStateSaveInfoPacket(GameMain.gameTick));
}

[HarmonyPrefix]
Expand All @@ -66,6 +71,7 @@ public static bool SaveAsLastExit_Prefix()

[HarmonyPostfix]
[HarmonyPatch(nameof(GameSave.LoadCurrentGame))]
[SuppressMessage("Style", "IDE1006:Naming Styles")]
public static void LoadCurrentGame_Postfix(bool __result)
{
// If loading success, check and correct offset for all inserters
Expand All @@ -88,5 +94,9 @@ public static void LoadCurrentGame_Postfix(bool __result)
}
}
}
if (Multiplayer.IsActive && Multiplayer.Session.LocalPlayer.IsHost)
{
GameStatesManager.LastSaveTime = GameMain.gameTick;
}
}
}
16 changes: 16 additions & 0 deletions NebulaPatcher/Patches/Dynamic/UIEscMenu_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NebulaModel.Packets.Players;
using NebulaPatcher.Patches.Transpilers;
using NebulaWorld;
using NebulaWorld.GameStates;
using UnityEngine;
using UnityEngine.UI;

Expand All @@ -30,6 +31,21 @@ public static void _OnOpen_Prefix(UIEscMenu __instance)
SetButtonEnableState(loadGameWindowButton, !Multiplayer.IsActive);
}

[HarmonyPostfix]
[HarmonyPatch(nameof(UIEscMenu._OnOpen))]
[SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Original Function Name")]
public static void _OnOpen_Postfix(UIEscMenu __instance)
{
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost) return;

var timeSinceSave = GameMain.gameTick - GameStatesManager.LastSaveTime;
var second = (int)(timeSinceSave / 60L);
var minute = second / 60;
var hour = minute / 60;
var saveBtnText = "存档时间".Translate() + $" {hour}h{minute % 60}m{second % 60}s ago";
__instance.button2Text.text = saveBtnText;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(UIEscMenu.OnButton5Click))]
public static void OnButton5Click_Prefix()
Expand Down
3 changes: 2 additions & 1 deletion NebulaWorld/GameStates/GameStatesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class GameStatesManager : IDisposable
public static long RealGameTick => GameMain.gameTick;
public static float RealUPS => (float)FPSController.currentUPS;
public static string ImportedSaveName { get; set; }
public static long LastSaveTime { get; set; }
public static GameDesc NewGameDesc { get; set; }
public static int FragmentSize { get; set; }

Expand All @@ -33,7 +34,7 @@ public class GameStatesManager : IDisposable

public void Dispose()
{
FragmentSize = 0;
LastSaveTime = FragmentSize = 0;
SandboxToolsEnabled = false;
HistoryBinaryData = null;
GalacticTransportBinaryData = null;
Expand Down

0 comments on commit 702cd84

Please sign in to comment.