diff --git a/Source/Client/Patches/VTRSyncPatch.cs b/Source/Client/Patches/VTRSyncPatch.cs index b9282f35..d07b99cb 100644 --- a/Source/Client/Patches/VTRSyncPatch.cs +++ b/Source/Client/Patches/VTRSyncPatch.cs @@ -56,8 +56,8 @@ static class VTRSync { // Special identifier for world map (since it doesn't have a uniqueID like regular maps) public const int WorldMapId = -2; - public const int InvalidMapIndex = -1; - public static int lastMovedToMap = InvalidMapIndex; + public const int InvalidMapId = -1; + public static int lastMovedToMapId = InvalidMapId; public static int lastSentAtTick = -1; // Vtr rates @@ -69,18 +69,18 @@ static class VTRSync public static void SendViewedMapUpdate(int previous, int current) { string warn = string.Empty; - if (previous != lastMovedToMap) - warn = $" mismatch between expected previous map {previous} and last moved to map {lastMovedToMap}"; + if (previous != lastMovedToMapId) + warn = $" mismatch between expected previous map {previous} and last moved to map {lastMovedToMapId}"; else if (previous == current) return; int currentTick = Find.TickManager?.TicksGame ?? 0; - MpLog.Debug($"VTR MapSwitchPatch: {lastMovedToMap}->{current} @ tick {currentTick}{warn}"); + MpLog.Debug($"VTR MapSwitchPatch: {lastMovedToMapId}->{current} @ tick {currentTick}{warn}"); Multiplayer.Client.SendCommand(CommandType.PlayerCount, ScheduledCommand.Global, ByteWriter.GetBytes(previous, current)); - lastMovedToMap = current; + lastMovedToMapId = current; } public static void Reset() { - lastMovedToMap = InvalidMapIndex; + lastMovedToMapId = InvalidMapId; } } @@ -94,13 +94,13 @@ static void Prefix(Map value) try { // WorldRenderModePatch will handle it - if (VTRSync.lastMovedToMap == VTRSync.WorldMapId) return; - int previousMap = GetPreviousMapIndex(); - int newMap = value?.uniqueID ?? VTRSync.InvalidMapIndex; + if (VTRSync.lastMovedToMapId == VTRSync.WorldMapId) return; + int previousMap = GetPreviousMapId(); + int newMap = value?.uniqueID ?? VTRSync.InvalidMapId; int currentTick = Find.TickManager?.TicksGame ?? 0; if (previousMap == newMap) return; - if (VTRSync.lastMovedToMap == newMap && currentTick == VTRSync.lastSentAtTick) return; + if (VTRSync.lastMovedToMapId == newMap && currentTick == VTRSync.lastSentAtTick) return; VTRSync.SendViewedMapUpdate(previousMap, newMap); VTRSync.lastSentAtTick = currentTick; @@ -111,16 +111,16 @@ static void Prefix(Map value) } } - private static int GetPreviousMapIndex() + private static int GetPreviousMapId() { bool currentMapIsRemovedAndWasLatestMap = Current.Game.currentMapIndex >= Find.Maps.Count; if (currentMapIsRemovedAndWasLatestMap) { - return VTRSync.InvalidMapIndex; + return VTRSync.InvalidMapId; } - return Find.CurrentMap?.uniqueID ?? VTRSync.InvalidMapIndex; + return Find.CurrentMap?.uniqueID ?? VTRSync.InvalidMapId; } } @@ -138,16 +138,16 @@ static void Postfix(WorldRenderMode __result) // Detect transition to world map (Planet mode) if (__result == WorldRenderMode.Planet && lastRenderMode != WorldRenderMode.Planet) { - if (VTRSync.lastMovedToMap != VTRSync.InvalidMapIndex && VTRSync.lastMovedToMap != VTRSync.WorldMapId) + if (VTRSync.lastMovedToMapId != VTRSync.InvalidMapId && VTRSync.lastMovedToMapId != VTRSync.WorldMapId) { - VTRSync.SendViewedMapUpdate(VTRSync.lastMovedToMap, VTRSync.WorldMapId); + VTRSync.SendViewedMapUpdate(VTRSync.lastMovedToMapId, VTRSync.WorldMapId); } } // Detect transition back to tile map else if (__result != WorldRenderMode.Planet && lastRenderMode == WorldRenderMode.Planet) { - var newMap = Find.CurrentMap?.uniqueID ?? VTRSync.InvalidMapIndex; - if (newMap != VTRSync.InvalidMapIndex && VTRSync.lastMovedToMap == VTRSync.WorldMapId) + var newMap = Find.CurrentMap?.uniqueID ?? VTRSync.InvalidMapId; + if (newMap != VTRSync.InvalidMapId && VTRSync.lastMovedToMapId == VTRSync.WorldMapId) { VTRSync.SendViewedMapUpdate(VTRSync.WorldMapId, newMap); } diff --git a/Source/Common/Networking/State/ServerPlayingState.cs b/Source/Common/Networking/State/ServerPlayingState.cs index 3c9f9f1e..c0a11653 100644 --- a/Source/Common/Networking/State/ServerPlayingState.cs +++ b/Source/Common/Networking/State/ServerPlayingState.cs @@ -51,12 +51,12 @@ public void HandleClientCommand(ByteReader data) if (cmd == CommandType.PlayerCount) { ByteReader reader = new ByteReader(extra); - var prevMap = reader.ReadInt32(); - var newMap = reader.ReadInt32(); - if (Player.currentMap != prevMap) - ServerLog.Error($"Inconsistent player {Player.Username} map. Last known map: {Player.currentMap}, " + - $"however received command with transition: {prevMap} -> {newMap}"); - Player.currentMap = newMap; + var prevMapId = reader.ReadInt32(); + var newMapId = reader.ReadInt32(); + if (Player.currentMapId != prevMapId) + ServerLog.Error($"Inconsistent player {Player.Username} map. Last known map: {Player.currentMapId}, " + + $"however received command with transition: {prevMapId} -> {newMapId}"); + Player.currentMapId = newMapId; } // todo check if map id is valid for the player diff --git a/Source/Common/PlayerManager.cs b/Source/Common/PlayerManager.cs index aacd3ff2..457baf45 100644 --- a/Source/Common/PlayerManager.cs +++ b/Source/Common/PlayerManager.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; @@ -84,9 +84,9 @@ public void SetDisconnected(ConnectionBase conn, MpDisconnectReason reason) if (player.hasJoined) { // Send PlayerCount command to remove the player from their last known map - if (player.currentMap != -1) + if (player.currentMapId != -1) { - byte[] playerCountData = ByteWriter.GetBytes(player.currentMap, -1); // previousMap: player's map, newMap: -1 (disconnected) + byte[] playerCountData = ByteWriter.GetBytes(player.currentMapId, -1); // previousMap: player's map, newMap: -1 (disconnected) server.commands.Send(CommandType.PlayerCount, ScheduledCommand.NoFaction, ScheduledCommand.Global, playerCountData); } // todo check player.IsPlaying? diff --git a/Source/Common/ServerPlayer.cs b/Source/Common/ServerPlayer.cs index 14b35e42..3dd89906 100644 --- a/Source/Common/ServerPlayer.cs +++ b/Source/Common/ServerPlayer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Diagnostics; using System.Linq; @@ -31,7 +31,7 @@ public class ServerPlayer : IChatSource public int unfrozenAt; // Track which map the player is currently on - public int currentMap = -1; + public int currentMapId = -1; public string Username => conn.username; public int Latency => conn.Latency;