Skip to content

Commit

Permalink
Added ISessionManagerAPI
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
SokyranTheDragon committed Nov 6, 2023
1 parent d1d5810 commit ceb1a83
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
50 changes: 50 additions & 0 deletions Source/Client/Experimental/ISessionManagerAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using Multiplayer.Client.Persistent;
using Verse;

namespace Multiplayer.Client.Experimental;

public interface ISessionManagerAPI
{
IReadOnlyList<ISession> AllSessions { get; }
IReadOnlyList<IExposableSession> ExposableSessions { get; }
IReadOnlyList<ISemiPersistentSession> SemiPersistentSessions { get; }
IReadOnlyList<ITickingSession> TickingSessions { get; }
bool AnySessionActive { get; }

/// <summary>
/// Adds a new session to the list of active sessions.
/// </summary>
/// <param name="session">The session to try to add to active sessions.</param>
/// <returns><see langword="true"/> if the session was added to active ones, <see langword="false"/> if there was a conflict between sessions.</returns>
bool AddSession(ISession session);

/// <summary>
/// Tries to get a conflicting session (through the use of <see cref="ISessionWithCreationRestrictions"/>) or, if there was none, returns the input <paramref name="session"/>.
/// </summary>
/// <param name="session">The session to try to add to active sessions.</param>
/// <returns>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.</returns>
ISession GetOrAddSessionAnyConflict(ISession session);

/// <summary>
/// Tries to get a conflicting session (through the use of <see cref="ISessionWithCreationRestrictions"/>) or, if there was none, returns the input <paramref name="session"/>.
/// </summary>
/// <param name="session">The session to try to add to active sessions.</param>
/// <returns>A session that was conflicting with the input one if it's the same type (<c>other is T</c>), null if it's a different type, or the input itself if there were no conflicts.</returns>
T GetOrAddSession<T>(T session) where T : ISession;

/// <summary>
/// Tries to remove a session from active ones.
/// </summary>
/// <param name="session">The session to try to remove from the active sessions.</param>
/// <returns><see langword="true"/> if successfully removed from <see cref="AllSessions"/>. Doesn't correspond to if it was successfully removed from other lists of sessions.</returns>
bool RemoveSession(ISession session);

T GetFirstOfType<T>() where T : ISession;

T GetFirstWithId<T>(int id) where T : ISession;

ISession GetFirstWithId(int id);

bool IsAnySessionCurrentlyPausing(Map map); // Is it necessary for the API?
}
23 changes: 2 additions & 21 deletions Source/Client/Persistent/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using Multiplayer.Client.Experimental;
using Multiplayer.Client.Saving;
using Multiplayer.Common;
using RimWorld;
using Verse;

namespace Multiplayer.Client.Persistent;

public class SessionManager : IHasSemiPersistentData
public class SessionManager : IHasSemiPersistentData, ISessionManagerAPI
{
public IReadOnlyList<ISession> AllSessions => allSessions.AsReadOnly();
public IReadOnlyList<IExposableSession> ExposableSessions => exposableSessions.AsReadOnly();
Expand All @@ -24,11 +25,6 @@ public class SessionManager : IHasSemiPersistentData

public bool AnySessionActive => allSessions.Count > 0;

/// <summary>
/// Adds a new session to the list of active sessions.
/// </summary>
/// <param name="session">The session to try to add to active sessions.</param>
/// <returns><see langword="true"/> if the session was added to active ones, <see langword="false"/> if there was a conflict between sessions.</returns>
public bool AddSession(ISession session)
{
if (GetFirstConflictingSession(session) != null)
Expand All @@ -38,11 +34,6 @@ public bool AddSession(ISession session)
return true;
}

/// <summary>
/// Tries to get a conflicting session (through the use of <see cref="ISessionWithCreationRestrictions"/>) or, if there was none, returns the input <paramref name="session"/>.
/// </summary>
/// <param name="session">The session to try to add to active sessions.</param>
/// <returns>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.</returns>
public ISession GetOrAddSessionAnyConflict(ISession session)
{
if (GetFirstConflictingSession(session) is { } other)
Expand All @@ -52,11 +43,6 @@ public ISession GetOrAddSessionAnyConflict(ISession session)
return session;
}

/// <summary>
/// Tries to get a conflicting session (through the use of <see cref="ISessionWithCreationRestrictions"/>) or, if there was none, returns the input <paramref name="session"/>.
/// </summary>
/// <param name="session">The session to try to add to active sessions.</param>
/// <returns>A session that was conflicting with the input one if it's the same type (<c>other is T</c>), null if it's a different type, or the input itself if there were no conflicts.</returns>
public T GetOrAddSession<T>(T session) where T : ISession
{
if (session is ISessionWithCreationRestrictions s)
Expand Down Expand Up @@ -98,11 +84,6 @@ private void AddSessionNoCheck(ISession session)
session.PostAddSession();
}

/// <summary>
/// Tries to remove a session from active ones.
/// </summary>
/// <param name="session">The session to try to remove from the active sessions.</param>
/// <returns><see langword="true"/> if successfully removed from <see cref="AllSessions"/>. Doesn't correspond to if it was successfully removed from other lists of sessions.</returns>
public bool RemoveSession(ISession session)
{
if (session is IExposableSession exposable)
Expand Down

0 comments on commit ceb1a83

Please sign in to comment.