From 7c16120680858c072d84712b334d54f0a0fafa9a Mon Sep 17 00:00:00 2001 From: Guus Dohmen Date: Fri, 24 Jan 2020 23:49:04 +0100 Subject: [PATCH] #45 sub 11 | Added sync chat command (#165) --- src/CSM.csproj | 2 + .../Internal/RequestWorldTransferCommand.cs | 14 ++++++ .../Internal/RequestWorldTransferHandler.cs | 43 +++++++++++++++++++ src/Panels/ChatLogPanel.cs | 14 +++++- 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/Commands/Data/Internal/RequestWorldTransferCommand.cs create mode 100644 src/Commands/Handler/Internal/RequestWorldTransferHandler.cs diff --git a/src/CSM.csproj b/src/CSM.csproj index e35a1f31..3f081ab2 100644 --- a/src/CSM.csproj +++ b/src/CSM.csproj @@ -110,6 +110,7 @@ + @@ -191,6 +192,7 @@ + diff --git a/src/Commands/Data/Internal/RequestWorldTransferCommand.cs b/src/Commands/Data/Internal/RequestWorldTransferCommand.cs new file mode 100644 index 00000000..1319e9a4 --- /dev/null +++ b/src/Commands/Data/Internal/RequestWorldTransferCommand.cs @@ -0,0 +1,14 @@ +using ProtoBuf; + +namespace CSM.Commands.Data.Internal +{ + /// + /// Requests a world transfer/sync, only sent by clients + /// + + [ProtoContract] + class RequestWorldTransferCommand : CommandBase + { + + } +} diff --git a/src/Commands/Handler/Internal/RequestWorldTransferHandler.cs b/src/Commands/Handler/Internal/RequestWorldTransferHandler.cs new file mode 100644 index 00000000..18fe9c2d --- /dev/null +++ b/src/Commands/Handler/Internal/RequestWorldTransferHandler.cs @@ -0,0 +1,43 @@ +using CSM.Commands.Data.Internal; +using CSM.Helpers; +using CSM.Networking; +using ColossalFramework.Threading; +using ColossalFramework.UI; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using CSM.Networking.Status; + +namespace CSM.Commands.Handler.Internal +{ + + class RequestWorldTransferHandler : CommandHandler + { + protected override void Handle(RequestWorldTransferCommand command) + { + //pause game + MultiplayerManager.Instance.GameBlocked = true; + SimulationManager.instance.SimulationPaused = true; + //saving world + SaveHelpers.SaveServerLevel(); + + new Thread(() => + { + while (SaveHelpers.IsSaving()) + { + Thread.Sleep(100); + } + //send the world to the client + Command.SendToClient(MultiplayerManager.Instance.CurrentServer.ConnectedPlayers[command.SenderId], new WorldTransferCommand + { + World = SaveHelpers.GetWorldFile() + }); + + MultiplayerManager.Instance.CurrentServer.ConnectedPlayers[command.SenderId].Status = ClientStatus.Loading; + }).Start(); + + } + } +} diff --git a/src/Panels/ChatLogPanel.cs b/src/Panels/ChatLogPanel.cs index 21d2fa80..041c51a3 100644 --- a/src/Panels/ChatLogPanel.cs +++ b/src/Panels/ChatLogPanel.cs @@ -91,7 +91,19 @@ public ChatLogPanel() new ChatCommand("open-log", "Opens the multiplayer log.", (command) => { Process.Start(Path.GetFullPath(".") + "/multiplayer-logs/log-current.txt"); - }) + }), + new ChatCommand("sync", "Redownloads the entire save", (command) => { + if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Client) + { + MultiplayerManager.Instance.CurrentClient.Status = Networking.Status.ClientStatus.Downloading; + PrintGameMessage("Requesting the save game from the server"); + Command.SendToServer(new RequestWorldTransferCommand()); + } + else { + PrintGameMessage("You are the server"); + } + } + ) }; }