Skip to content

Commit

Permalink
Fix session map syncing
Browse files Browse the repository at this point in the history
- Added a single parameter (Map) constructor to all `Session` subclasses with docs to inform devs using the API of a mandatory constructor
- `ExposableSession` will now be provided the `Map` parameter by the `SessionManager`
- `SemiPersistentSessions` won't sync a map on a per-session basis, instead being provided the `SessionManager` map
  • Loading branch information
SokyranTheDragon committed Nov 14, 2023
1 parent f85c6b4 commit ccf0b22
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 26 deletions.
3 changes: 2 additions & 1 deletion Source/Client/Comp/Map/MultiplayerMapComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MultiplayerMapComp : IExposable, IHasSemiPersistentData
public Dictionary<int, FactionMapData> factionData = new();
public Dictionary<int, CustomFactionMapData> customFactionData = new();

public SessionManager sessionManager = new();
public SessionManager sessionManager;
public List<PersistentDialog> mapDialogs = new();
public int autosaveCounter;

Expand All @@ -33,6 +33,7 @@ public class MultiplayerMapComp : IExposable, IHasSemiPersistentData
public MultiplayerMapComp(Map map)
{
this.map = map;
sessionManager = new(map);
}

public CaravanFormingSession CreateCaravanFormingSession(bool reform, Action onClosed, bool mapAboutToBeRemoved, IntVec3? meetingSpot = null)
Expand Down
4 changes: 2 additions & 2 deletions Source/Client/Comp/World/MultiplayerWorldComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MultiplayerWorldComp : IHasSemiPersistentData

public TileTemperaturesComp uiTemperatures;
public List<MpTradeSession> trading = new(); // Should only be modified from MpTradeSession in PostAdd/Remove and ExposeData
public SessionManager sessionManager = new();
public SessionManager sessionManager = new(null);

public Faction spectatorFaction;

Expand All @@ -39,7 +39,7 @@ public void ExposeData()
sessionManager.ExposeSessions();
// Ensure a pause lock session exists if there's any pause locks registered
if (!PauseLockSession.pauseLocks.NullOrEmpty())
sessionManager.AddSession(new PauseLockSession());
sessionManager.AddSession(new PauseLockSession(null));

DoBackCompat();
}
Expand Down
12 changes: 12 additions & 0 deletions Source/Client/Experimental/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public int SessionId
/// </summary>
public virtual bool IsSessionValid => true;

/// <summary>
/// Mandatory constructor for any subclass of <see cref="Session"/>.
/// </summary>
/// <param name="map">The map this session belongs to. It will be provided by session manager when syncing.</param>
protected Session(Map map) { }

/// <summary>
/// Called once the sessions has been added to the list of active sessions. Can be used for initialization.
/// </summary>
Expand Down Expand Up @@ -93,6 +99,9 @@ protected static void SwitchToMapOrWorld(Map map)
/// </summary>
public abstract class ExposableSession : Session, IExposable
{
/// <inheritdoc cref="Session(Map)"/>
protected ExposableSession(Map map) : base(map) { }

// For subclasses implementing IExplosableSession
public virtual void ExposeData()
{
Expand All @@ -110,6 +119,9 @@ public virtual void ExposeData()
/// </summary>
public abstract class SemiPersistentSession : Session
{
/// <inheritdoc cref="Session(Map)"/>
protected SemiPersistentSession(Map map) : base(map) { }

/// <summary>
/// Writes/reads the data used by this session.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/Persistent/CaravanFormingSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CaravanFormingSession : ExposableSession, ISessionWithTransferables

public override Map Map => map;

public CaravanFormingSession(Map map)
public CaravanFormingSession(Map map) : base(map)
{
this.map = map;
}
Expand Down
8 changes: 6 additions & 2 deletions Source/Client/Persistent/CaravanSplittingSession.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using JetBrains.Annotations;
using Verse;
using RimWorld;
using RimWorld.Planet;
Expand Down Expand Up @@ -35,12 +36,15 @@ public class CaravanSplittingSession : ExposableSession, ISessionWithTransferabl
/// </summary>
public CaravanSplittingProxy dialog;

public CaravanSplittingSession(Map map) : base(null)
{
}

/// <summary>
/// Handles creation of new CaravanSplittingSession.
/// Ensures a unique Id is given to this session and creates the dialog.
/// </summary>
/// <param name="caravan"></param>
public CaravanSplittingSession(Caravan caravan)
public CaravanSplittingSession(Caravan caravan) : base(null)
{
Caravan = caravan;

Expand Down
2 changes: 2 additions & 0 deletions Source/Client/Persistent/PauseLockSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class PauseLockSession : Session, ISessionWithCreationRestrictions

public override Map Map => null;

public PauseLockSession(Map _) : base(null) { }

public override bool IsCurrentlyPausing(Map map) => pauseLocks.Any(x => x(map));

// Should we add some message explaining pause locks/having a list of pausing ones?
Expand Down
5 changes: 2 additions & 3 deletions Source/Client/Persistent/Rituals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ public class RitualSession : SemiPersistentSession, IPausingWithDialog

public override Map Map => map;

public RitualSession(Map map)
public RitualSession(Map map) : base(map)
{
this.map = map;
}

public RitualSession(Map map, RitualData data)
public RitualSession(Map map, RitualData data) : this(map)
{
this.map = map;
this.data = data;
this.data.assignments.session = this;
}
Expand Down
22 changes: 8 additions & 14 deletions Source/Client/Persistent/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ public class SessionManager : IHasSemiPersistentData, ISessionManager
private List<ITickingSession> tickingSessions = new();
private static HashSet<Type> tempCleanupLoggingTypes = new();

public Map Map { get; }
public bool AnySessionActive => allSessions.Count > 0;

public SessionManager(Map map)
{
Map = map;
}

public bool AddSession(Session session)
{
if (GetFirstConflictingSession(session) != null)
Expand Down Expand Up @@ -151,7 +157,6 @@ public void WriteSemiPersistent(ByteWriter data)
foreach (var session in semiPersistentSessions)
{
data.WriteUShort((ushort)ImplSerialization.sessions.FindIndex(session.GetType()));
data.WriteInt32(session.Map?.uniqueID ?? -1);
data.WriteInt32(session.SessionId);

try
Expand All @@ -174,7 +179,6 @@ public void ReadSemiPersistent(ByteReader data)
for (int i = 0; i < sessionsCount; i++)
{
ushort typeIndex = data.ReadUShort();
int mapId = data.ReadInt32();
int sessionId = data.ReadInt32();

if (typeIndex >= ImplSerialization.sessions.Length)
Expand All @@ -184,20 +188,10 @@ public void ReadSemiPersistent(ByteReader data)
}

var objType = ImplSerialization.sessions[typeIndex];
Map map = null;
if (mapId != -1)
{
map = Find.Maps.FirstOrDefault(m => m.uniqueID == mapId);
if (map == null)
{
Log.Error($"Trying to read semi persistent session of type {objType} received a null map while expecting a map with ID {mapId}");
// Continue? Let it run?
}
}

try
{
if (Activator.CreateInstance(objType, map) is SemiPersistentSession session)
if (Activator.CreateInstance(objType, Map) is SemiPersistentSession session)
{
session.SessionId = sessionId;
session.Sync(new ReadingSyncWorker(data));
Expand All @@ -214,7 +208,7 @@ public void ReadSemiPersistent(ByteReader data)

public void ExposeSessions()
{
Scribe_Collections.Look(ref exposableSessions, "sessions", LookMode.Deep);
Scribe_Collections.Look(ref exposableSessions, "sessions", LookMode.Deep, Map);

if (Scribe.mode == LoadSaveMode.PostLoadInit)
{
Expand Down
4 changes: 2 additions & 2 deletions Source/Client/Persistent/Trading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public string Label

public override bool IsSessionValid => trader != null && playerNegotiator != null;

public MpTradeSession() { }
public MpTradeSession(Map _) : base(null) { }

private MpTradeSession(ITrader trader, Pawn playerNegotiator, bool giftMode)
private MpTradeSession(ITrader trader, Pawn playerNegotiator, bool giftMode) : base(null)
{
this.trader = trader;
this.playerNegotiator = playerNegotiator;
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/Persistent/TransporterLoadingSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TransporterLoading : ExposableSession, ISessionWithTransferables, I

public bool uiDirty;

public TransporterLoading(Map map)
public TransporterLoading(Map map) : base(map)
{
this.map = map;
}
Expand Down

0 comments on commit ccf0b22

Please sign in to comment.