Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions Source/Client/Patches/VTRSyncPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}

Expand All @@ -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;
Expand All @@ -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;
}
}

Expand All @@ -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);
}
Expand Down
12 changes: 6 additions & 6 deletions Source/Common/Networking/State/ServerPlayingState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
{
var playerId = data.ReadInt32();
var traces = data.ReadPrefixedBytes();
Server.GetPlayer(playerId)?.SendPacket(Packets.Server_Traces, new object[] { TracesPacket.Transfer, traces });

Check warning on line 40 in Source/Common/Networking/State/ServerPlayingState.cs

View workflow job for this annotation

GitHub Actions / Builds

Possible null reference assignment.
}
}

Expand All @@ -51,12 +51,12 @@
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
Expand Down
6 changes: 3 additions & 3 deletions Source/Common/PlayerManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -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?
Expand Down
4 changes: 2 additions & 2 deletions Source/Common/ServerPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.Linq;

Expand Down Expand Up @@ -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;
Expand Down
Loading