Search Unity

Best way to create a plugin API

Discussion in 'Multiplayer' started by unity_-YhAGBX97tqBoA, Apr 13, 2022.

  1. unity_-YhAGBX97tqBoA

    unity_-YhAGBX97tqBoA

    Joined:
    May 5, 2021
    Posts:
    1
    I'm working on multiplayer game and I want to create a server plugin system, but I don't know what is the best way to create plugin API.

    For example, I have a GameSession class that manages the current game session. It looks something like this:
    Code (CSharp):
    1. public class GameSession : NetworkBehaviour
    2. {
    3.     public IEnumerable<Member> Members { get => ... }
    4.     public int MembersCount { get=> ... }
    5.  
    6.     public event Action<int> MemberConnected;
    7.     public event Action<int> MemberDisconnected;
    8.  
    9.     ...
    10.  
    11.     public void StartSession()
    12.     { ... }
    13.     public void Finish(Winner winner)
    14.     { ... }
    15.  
    16.     public Member GetMember(int connectionId)
    17.     { ... }
    18.  
    19.     ...
    20. }
    How to let plugins manage this class? I have a few options:
    1. Provide the GameSession class directly
    2. Wrap the GameSession in an interface and provide it to the plugin class
    3. Create wrapper class for GameSession and provide it to the plugin class
    When wrapping a class in an interface, I am adding the interface to GameSession. The interface looks like this:
    Code (CSharp):
    1. public interface ISession
    2. {
    3.     public IEnumerable<Member> Members { get; }
    4.     public int MembersCount { get; }
    5.  
    6.     public event Action<int> MemberConnected;
    7.     public event Action<int> MemberDisconnected;
    8.  
    9.     public void StartSession();
    10.     public void Finish(Winner winner);
    11.  
    12.     public Member GetMember(int connectionId);
    13. }
    And then I can provide ISession to plugins.

    The wrapper class would look like this:
    Code (CSharp):
    1. public class SessionWrapper
    2. {
    3.     public IEnumerable<Member> Members { get => _session.Members; }
    4.     public int MembersCount { get => _session.MembersCount; }
    5.  
    6.     public event Action<int> MemberConnected;
    7.     public event Action<int> MemberDisconnected;
    8.  
    9.     private GameSession _session;
    10.  
    11.     public SessionWrapper (GameSession session)
    12.     {
    13.         _session = session;
    14.     }
    15.  
    16.     public void StartSession()
    17.     {
    18.         _session.StartSession();
    19.     }
    20.  
    21.     public void Finish(Winner winner)
    22.     {
    23.         _session.Finish(winner);
    24.     }
    25.  
    26.     public Member GetMember(int connectionId)
    27.     {
    28.         return _session.GetMember(connectionId);
    29.     }
    30.  
    31.     // Also, there should be subscriptions to _session events and calling SessionWrapper events
    32. }
    And then I can provide SessionWrapper to plugins.

    Which option should I choose? Or maybe someone can suggest something else?
     
    Last edited: Apr 13, 2022