]> git.deb.at Git - rhonda/impostor.git/commitdiff
Fix SyncSettingsAsync
authorAeonLucid <aeonlucid@gmail.com>
Mon, 19 Dec 2022 01:24:51 +0000 (02:24 +0100)
committerAeonLucid <aeonlucid@gmail.com>
Mon, 19 Dec 2022 01:24:51 +0000 (02:24 +0100)
src/Impostor.Plugins.Example/Handlers/GameEventListener.cs
src/Impostor.Server/Net/Inner/Objects/GameManager/InnerGameManager.cs
src/Impostor.Server/Net/State/Game.Api.cs
src/Impostor.Server/Net/State/Game.Data.cs

index 1384e8b528e75b6474573a823929d252c52e1e09..39bcb0148b219dcaa4424199981e78b39477f843 100644 (file)
@@ -19,21 +19,16 @@ namespace Impostor.Plugins.Example.Handlers
         {
             _logger.LogInformation("Game creation requested by {client}", e.Client == null ? "a plugin" : e.Client.Name);
 
-            if (e.Client != null)
-            {
-                var gameCode = GameCode.From(e.Client.Name);
-
-                if (!gameCode.IsInvalid)
-                {
-                    e.GameCode = gameCode;
-                }
-
-                if (e.Client.Name == "dima")
-                {
-                    e.IsCancelled = true;
-                    e.Client.DisconnectAsync(DisconnectReason.Custom, "No you dont >:(");
-                }
-            }
+            // TODO: Code below causes the lobby to stop loading and close after 5 secs.
+            // if (e.Client != null)
+            // {
+            //     var gameCode = GameCode.From(e.Client.Name);
+            //
+            //     if (!gameCode.IsInvalid)
+            //     {
+            //         e.GameCode = gameCode;
+            //     }
+            // }
         }
 
         [EventListener]
index 27ce592c1a017fe967abaa16454a803480fb67da..08891ae362a067cb186ef5bf47ad99a6f5155b5a 100644 (file)
@@ -39,6 +39,21 @@ internal abstract class InnerGameManager : InnerNetObject, IInnerGameManager
         return logic;
     }
 
+    internal int? GetGameLogicTag<T>(T logic)
+        where T : GameLogicComponent
+    {
+        for (var i = 0; i < LogicComponents.Count; i++)
+        {
+            var component = LogicComponents[i];
+            if (component == logic)
+            {
+                return i;
+            }
+        }
+
+        return null;
+    }
+
     public override ValueTask<bool> SerializeAsync(IMessageWriter writer, bool initialState)
     {
         throw new System.NotImplementedException();
index f685ca1a3d6cb2922fa80595455af4a0c58596d9..ad6c793219ba44b6517a0f811fc0d8677773647a 100644 (file)
@@ -2,10 +2,13 @@
 using System.Threading.Tasks;
 using Impostor.Api;
 using Impostor.Api.Games;
+using Impostor.Api.Innersloth.GameOptions;
 using Impostor.Api.Net;
 using Impostor.Api.Net.Inner;
-using Impostor.Api.Net.Messages.Rpcs;
+using Impostor.Api.Net.Messages;
 using Impostor.Hazel;
+using Impostor.Server.Net.Inner;
+using Impostor.Server.Net.Inner.Objects.GameManager;
 
 namespace Impostor.Server.Net.State
 {
@@ -20,24 +23,45 @@ namespace Impostor.Server.Net.State
             _bannedIps.Add(ipAddress);
         }
 
-        // TODO This no longer does anything, it was replaced by LogicOptions
         public async ValueTask SyncSettingsAsync()
         {
             if (Host?.Character == null)
             {
-                throw new ImpostorException("Attempted to set infected when the host was not spawned.");
+                throw new ImpostorException("Attempted to change settings when the host was not spawned.");
             }
 
-            using (var writer = StartRpc(Host.Character.NetId, RpcCalls.SyncSettings))
+            var gameManager = FindObjectByType<InnerGameManager>();
+            if (gameManager == null)
             {
-                // Someone will probably forget to do this, so we include it here.
-                // If this is not done, the host will overwrite changes later with the defaults.
-                Options.IsDefaults = false;
-
-                Rpc02SyncSettings.Serialize(writer, Options);
+                throw new ImpostorException("Attempted to change options when the game manager was not spawned.");
+            }
 
-                await FinishRpcAsync(writer);
+            var gameOptionsTag = gameManager.GetGameLogicTag(gameManager.LogicOptions);
+            if (gameOptionsTag == null)
+            {
+                throw new ImpostorException("Attempted to change options when the LogicOptions was not spawned.");
             }
+
+            // Someone will probably forget to do this, so we include it here.
+            // If this is not done, the host will overwrite changes later with the defaults.
+            Options.IsDefaults = false;
+
+            using var writer = MessageWriter.Get(MessageType.Reliable);
+
+            writer.StartMessage(MessageFlags.GameData);
+            Code.Serialize(writer);
+
+            writer.StartMessage(GameDataTag.DataFlag);
+            writer.WritePacked(gameManager.NetId);
+
+            writer.StartMessage((byte)gameOptionsTag);
+            GameOptionsFactory.Serialize(writer, Options);
+
+            writer.EndMessage();
+            writer.EndMessage();
+            writer.EndMessage();
+
+            await SendToAllAsync(writer);
         }
 
         public async ValueTask SetPrivacyAsync(bool isPublic)
index d08b636fc24218dd7bc0d7cb3d9fcf1badf53dd9..e0a37129c1c4c46c7e90a10aa144428d176fb865 100644 (file)
@@ -60,6 +60,20 @@ namespace Impostor.Server.Net.State
             return default;
         }
 
+        public T? FindObjectByType<T>()
+            where T : IInnerNetObject
+        {
+            foreach (var netObject in _allObjects)
+            {
+                if (netObject is T result)
+                {
+                    return result;
+                }
+            }
+
+            return default;
+        }
+
         public async ValueTask<bool> HandleGameDataAsync(IMessageReader parent, ClientPlayer sender, bool toPlayer)
         {
             // Find target player.