From ceb1a838f89ecb2bb173e109c83dd77a6bb482f6 Mon Sep 17 00:00:00 2001 From: SokyranTheDragon Date: Mon, 6 Nov 2023 19:59:17 +0100 Subject: [PATCH] Added ISessionManagerAPI The intention is to move it to MP API as a way to provide a way for other mods to interact with the session manager. Currently needs a little bit more documentation (currently all documentation was moved from SessionManager). --- .../Client/Experimental/ISessionManagerAPI.cs | 50 +++++++++++++++++++ Source/Client/Persistent/SessionManager.cs | 23 +-------- 2 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 Source/Client/Experimental/ISessionManagerAPI.cs diff --git a/Source/Client/Experimental/ISessionManagerAPI.cs b/Source/Client/Experimental/ISessionManagerAPI.cs new file mode 100644 index 00000000..22c3ff98 --- /dev/null +++ b/Source/Client/Experimental/ISessionManagerAPI.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using Multiplayer.Client.Persistent; +using Verse; + +namespace Multiplayer.Client.Experimental; + +public interface ISessionManagerAPI +{ + IReadOnlyList AllSessions { get; } + IReadOnlyList ExposableSessions { get; } + IReadOnlyList SemiPersistentSessions { get; } + IReadOnlyList TickingSessions { get; } + bool AnySessionActive { get; } + + /// + /// Adds a new session to the list of active sessions. + /// + /// The session to try to add to active sessions. + /// if the session was added to active ones, if there was a conflict between sessions. + bool AddSession(ISession session); + + /// + /// Tries to get a conflicting session (through the use of ) or, if there was none, returns the input . + /// + /// The session to try to add to active sessions. + /// A session that was conflicting with the input one, or the input itself if there were no conflicts. It may be of a different type than the input. + ISession GetOrAddSessionAnyConflict(ISession session); + + /// + /// Tries to get a conflicting session (through the use of ) or, if there was none, returns the input . + /// + /// The session to try to add to active sessions. + /// A session that was conflicting with the input one if it's the same type (other is T), null if it's a different type, or the input itself if there were no conflicts. + T GetOrAddSession(T session) where T : ISession; + + /// + /// Tries to remove a session from active ones. + /// + /// The session to try to remove from the active sessions. + /// if successfully removed from . Doesn't correspond to if it was successfully removed from other lists of sessions. + bool RemoveSession(ISession session); + + T GetFirstOfType() where T : ISession; + + T GetFirstWithId(int id) where T : ISession; + + ISession GetFirstWithId(int id); + + bool IsAnySessionCurrentlyPausing(Map map); // Is it necessary for the API? +} diff --git a/Source/Client/Persistent/SessionManager.cs b/Source/Client/Persistent/SessionManager.cs index 1c442d46..f23748a6 100644 --- a/Source/Client/Persistent/SessionManager.cs +++ b/Source/Client/Persistent/SessionManager.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using HarmonyLib; +using Multiplayer.Client.Experimental; using Multiplayer.Client.Saving; using Multiplayer.Common; using RimWorld; @@ -9,7 +10,7 @@ namespace Multiplayer.Client.Persistent; -public class SessionManager : IHasSemiPersistentData +public class SessionManager : IHasSemiPersistentData, ISessionManagerAPI { public IReadOnlyList AllSessions => allSessions.AsReadOnly(); public IReadOnlyList ExposableSessions => exposableSessions.AsReadOnly(); @@ -24,11 +25,6 @@ public class SessionManager : IHasSemiPersistentData public bool AnySessionActive => allSessions.Count > 0; - /// - /// Adds a new session to the list of active sessions. - /// - /// The session to try to add to active sessions. - /// if the session was added to active ones, if there was a conflict between sessions. public bool AddSession(ISession session) { if (GetFirstConflictingSession(session) != null) @@ -38,11 +34,6 @@ public bool AddSession(ISession session) return true; } - /// - /// Tries to get a conflicting session (through the use of ) or, if there was none, returns the input . - /// - /// The session to try to add to active sessions. - /// A session that was conflicting with the input one, or the input itself if there were no conflicts. It may be of a different type than the input. public ISession GetOrAddSessionAnyConflict(ISession session) { if (GetFirstConflictingSession(session) is { } other) @@ -52,11 +43,6 @@ public ISession GetOrAddSessionAnyConflict(ISession session) return session; } - /// - /// Tries to get a conflicting session (through the use of ) or, if there was none, returns the input . - /// - /// The session to try to add to active sessions. - /// A session that was conflicting with the input one if it's the same type (other is T), null if it's a different type, or the input itself if there were no conflicts. public T GetOrAddSession(T session) where T : ISession { if (session is ISessionWithCreationRestrictions s) @@ -98,11 +84,6 @@ private void AddSessionNoCheck(ISession session) session.PostAddSession(); } - /// - /// Tries to remove a session from active ones. - /// - /// The session to try to remove from the active sessions. - /// if successfully removed from . Doesn't correspond to if it was successfully removed from other lists of sessions. public bool RemoveSession(ISession session) { if (session is IExposableSession exposable)