♻️ plugin api

This commit is contained in:
2023-04-14 14:32:04 +04:00
parent 6c4a51534a
commit fc28f512ba
21 changed files with 155 additions and 97 deletions
@@ -32,7 +32,8 @@ internal class AuthorizeSuccessHandler: Handler
{
var playerId = buffer.ReadString();
var playerName = buffer.ReadString();
var playerPayload = buffer.ReadString();
_listenerList.OnAuthorizationSuccess(playerId, playerName);
_listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload);
}
}
@@ -19,11 +19,11 @@ using Ragon.Protocol;
namespace Ragon.Client;
internal class EntityDestroyHandler: Handler
internal class EntityRemoveHandler: Handler
{
private readonly RagonEntityCache _entityCache;
public EntityDestroyHandler(RagonEntityCache entityCache)
public EntityRemoveHandler(RagonEntityCache entityCache)
{
_entityCache = entityCache;
}
+1 -1
View File
@@ -95,7 +95,7 @@ namespace Ragon.Client
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, _listenerList);
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, _listenerList);
_handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateHandler(this, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityDestroyHandler(_entityCache);
_handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityRemoveHandler(_entityCache);
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache);
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventHandler(this, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.SNAPSHOT] = new SnapshotHandler(this, _listenerList, _entityCache, _playerCache);
+1 -1
View File
@@ -150,7 +150,7 @@ namespace Ragon.Client
_playerLeftListeners.Remove(listener);
}
public void OnAuthorizationSuccess(string playerId, string playerName)
public void OnAuthorizationSuccess(string playerId, string playerName, string payload)
{
foreach (var listener in _authorizationListeners)
listener.OnAuthorizationSuccess(_client, playerId, playerName);
@@ -0,0 +1,16 @@
using Ragon.Protocol;
using Ragon.Server.Room;
namespace Ragon.Server.Entity;
public interface IRagonEntity
{
public ushort Id { get; }
public ushort Type { get; }
public ushort StaticId { get; }
public ushort AttachId { get; }
public RagonRoomPlayer Owner { get; }
public RagonAuthority Authority { get; }
public RagonPayload Payload { get; }
public IRagonEntityState State { get; }
}
@@ -0,0 +1,8 @@
using Ragon.Protocol;
namespace Ragon.Server.Entity;
public interface IRagonEntityState
{
}
+19 -10
View File
@@ -20,7 +20,7 @@ using Ragon.Server.Room;
namespace Ragon.Server.Entity;
public class RagonEntity
public class RagonEntity: IRagonEntity
{
private static ushort _idGenerator = 100;
public ushort Id { get; private set; }
@@ -30,8 +30,10 @@ public class RagonEntity
public RagonRoomPlayer Owner { get; private set; }
public RagonAuthority Authority { get; private set; }
public RagonPayload Payload { get; private set; }
public RagonEntityState State { get; private set; }
public IRagonEntityState State => _state;
private readonly List<RagonEvent> _bufferedEvents;
private readonly RagonEntityState _state;
public RagonEntity(RagonEntityParameters parameters)
{
@@ -41,10 +43,9 @@ public class RagonEntity
Type = parameters.Type;
AttachId = parameters.AttachId;
Authority = parameters.Authority;
State = new RagonEntityState(this);
Payload = new RagonPayload();
_state = new RagonEntityState(this);
_bufferedEvents = new List<RagonEvent>();
}
@@ -121,7 +122,8 @@ public class RagonEntity
buffer.WriteUShort(Payload.Size);
Payload.Write(buffer);
State.Snapshot(buffer);
_state.Snapshot(buffer);
}
public void ReplicateEvent(
@@ -215,16 +217,23 @@ public class RagonEntity
}
}
public void Write(RagonBuffer writer)
public void AddProperty(RagonProperty property)
{
State.Write(writer);
_state.AddProperty(property);
}
public void WriteState(RagonBuffer writer)
{
_state.Write(writer);
}
public void Read(RagonRoomPlayer player, RagonBuffer reader)
public bool TryReadState(RagonRoomPlayer player, RagonBuffer reader)
{
if (Owner.Connection.Id != player.Connection.Id)
return;
return false;
State.Read(reader);
_state.Read(reader);
return true;
}
}
@@ -19,14 +19,16 @@ using Ragon.Protocol;
namespace Ragon.Server.Entity;
public class RagonEntityState
public class RagonEntityState: IRagonEntityState
{
private List<RagonProperty> _properties;
private RagonEntity _entity;
private RagonBuffer _buffer;
public RagonEntityState(RagonEntity entity, int capacity = 10)
{
_entity = entity;
_buffer = new RagonBuffer(8);
_properties = new List<RagonProperty>(capacity);
}
@@ -65,8 +67,8 @@ public class RagonEntityState
{
foreach (var property in _properties)
{
var hasPayload = property.IsFixed || !property.IsFixed && property.Size > 0;
if (hasPayload)
var hasPayloadOrFixed = property.IsFixed || property is { IsFixed: false, Size: > 0 };
if (hasPayloadOrFixed)
{
buffer.WriteBool(true);
property.Write(buffer);
@@ -47,7 +47,7 @@ public sealed class EntityCreateOperation : IRagonOperation
var propertyType = reader.ReadBool();
var propertySize = reader.ReadUShort();
entity.State.AddProperty(new RagonProperty(propertySize, propertyType));
entity.AddProperty(new RagonProperty(propertySize, propertyType));
}
if (reader.Capacity > 0)
@@ -32,10 +32,14 @@ public sealed class EntityStateOperation: IRagonOperation
for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++)
{
var entityId = reader.ReadUShort();
if (room.Entities.TryGetValue(entityId, out var entity))
entity.Read(player, reader);
if (room.Entities.TryGetValue(entityId, out var entity) && entity.TryReadState(player, reader))
{
room.Track(entity);
}
else
{
_logger.Error($"Entity with Id {entityId} not found, replication interrupted");
}
}
}
}
@@ -25,13 +25,12 @@ namespace Ragon.Server.Handler;
public sealed class RoomJoinOperation : IRagonOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
private readonly RagonWebHookPlugin _webHook;
public RoomJoinOperation(IServerPlugin serverPlugin, RagonWebHookPlugin plugin)
public RoomJoinOperation(RagonWebHookPlugin plugin)
{
_serverPlugin = serverPlugin;
_ragonWebHookPlugin = plugin;
_webHook = plugin;
}
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
@@ -50,10 +49,10 @@ public sealed class RoomJoinOperation : IRagonOperation
var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(existsRoom, player);
if (!_serverPlugin.OnRoomJoin(player, existsRoom))
if (!existsRoom.Plugin.OnPlayerJoined(player))
return;
_ragonWebHookPlugin.RoomJoined(context, existsRoom, player);
_webHook.RoomJoined(context, existsRoom, player);
JoinSuccess(context, existsRoom, writer);
@@ -24,12 +24,10 @@ namespace Ragon.Server.Handler;
public sealed class RoomLeaveOperation: IRagonOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
public RoomLeaveOperation(IServerPlugin serverPlugin, RagonWebHookPlugin plugin)
private readonly RagonWebHookPlugin _webHook;
public RoomLeaveOperation(RagonWebHookPlugin plugin)
{
_serverPlugin = serverPlugin;
_ragonWebHookPlugin = plugin;
_webHook = plugin;
}
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
@@ -39,9 +37,13 @@ public sealed class RoomLeaveOperation: IRagonOperation
if (room != null)
{
_serverPlugin.OnRoomLeave(roomPlayer, room);
_ragonWebHookPlugin.RoomLeaved(context, room, roomPlayer);
context.Room?.DetachPlayer(roomPlayer);
var plugin = room.Plugin;
plugin.OnPlayerLeaved(roomPlayer);
room.DetachPlayer(roomPlayer);
_webHook.RoomLeaved(context, room, roomPlayer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} leaved from {room.Id}");
}
}
@@ -64,7 +64,7 @@ public sealed class SceneLoadedOperation : IRagonOperation
{
var propertyType = reader.ReadBool();
var propertySize = reader.ReadUShort();
entity.State.AddProperty(new RagonProperty(propertySize, propertyType));
entity.AddProperty(new RagonProperty(propertySize, propertyType));
}
var roomPlugin = room.Plugin;
+3 -2
View File
@@ -15,11 +15,12 @@
*/
using Ragon.Server.IO;
using Ragon.Server.Lobby;
namespace Ragon.Server;
public interface IRagonServer
{
RagonContext? ResolveContext(INetworkConnection connection);
RagonContext? ResolveContext(string id);
RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection);
RagonLobbyPlayer? GetPlayerById(string id);
}
+18 -18
View File
@@ -22,44 +22,44 @@ namespace Ragon.Server.Plugin;
public class BaseRoomPlugin: IRoomPlugin
{
private IRagonRoom _ragonRoom;
public RagonRoomPlayer GetPlayerById(string id)
{
var player = _ragonRoom.GetPlayerById(id);
return player;
}
public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection)
{
var player = _ragonRoom.GetPlayerByConnection(connection);
return player;
}
public IRagonRoom Room { get; private set; }
public virtual void OnAttached(IRagonRoom room)
{
_ragonRoom = room;
Room = room;
}
public virtual void OnDetached()
{
}
#region VIRTUAL
#region VIRTUAL
public virtual bool OnPlayerJoined(RagonRoomPlayer player)
{
return true;
}
public virtual bool OnPlayerLeaved(RagonRoomPlayer player)
{
return true;
}
public virtual void Tick(float dt)
{
}
public virtual bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity)
public virtual bool OnEntityCreate(RagonRoomPlayer creator, IRagonEntity entity)
{
return true;
}
public virtual bool OnEntityRemove(RagonRoomPlayer remover, RagonEntity entity)
public virtual bool OnEntityRemove(RagonRoomPlayer remover, IRagonEntity entity)
{
return true;
}
@@ -22,26 +22,14 @@ namespace Ragon.Server.Plugin;
public class BaseServerPlugin: IServerPlugin
{
private IRagonServer _ragonServer;
public IRagonServer Server { get; protected set; }
public RagonLobbyPlayer? GetPlayerById(string id)
public virtual void OnAttached(IRagonServer server)
{
var context = _ragonServer.ResolveContext(id);
return context?.LobbyPlayer;
Server = server;
}
public RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection)
{
var context = _ragonServer.ResolveContext(connection);
return context?.LobbyPlayer;
}
public void OnAttached(IRagonServer server)
{
_ragonServer = server;
}
public void OnDetached()
public virtual void OnDetached()
{
}
@@ -56,16 +44,6 @@ public class BaseServerPlugin: IServerPlugin
return true;
}
public virtual bool OnRoomLeave(RagonRoomPlayer player, RagonRoom room)
{
return true;
}
public virtual bool OnRoomJoin(RagonRoomPlayer player, RagonRoom room)
{
return true;
}
public virtual bool OnCommand(string command, string payload)
{
return true;
+4 -2
View File
@@ -24,6 +24,8 @@ public interface IRoomPlugin
void Tick(float dt);
void OnAttached(IRagonRoom room);
void OnDetached();
bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity);
bool OnEntityRemove(RagonRoomPlayer remover, RagonEntity entity);
bool OnPlayerJoined(RagonRoomPlayer player);
bool OnPlayerLeaved(RagonRoomPlayer player);
bool OnEntityCreate(RagonRoomPlayer player, IRagonEntity entity);
bool OnEntityRemove(RagonRoomPlayer player, IRagonEntity entity);
}
@@ -26,8 +26,6 @@ public interface IServerPlugin
void OnDetached();
bool OnRoomCreate(RagonLobbyPlayer player, RagonRoom room);
bool OnRoomRemove(RagonLobbyPlayer player, RagonRoom room);
bool OnRoomLeave(RagonRoomPlayer player, RagonRoom room);
bool OnRoomJoin(RagonRoomPlayer player, RagonRoom room);
bool OnCommand(string command, string payload);
IRoomPlugin CreateRoomPlugin(RoomInformation information);
}
+21 -5
View File
@@ -77,8 +77,8 @@ public class RagonServer : IRagonServer, INetworkListener
_handlers[(byte) RagonOperation.AUTHORIZE] = new AuthorizationOperation(_webhooks, contextObserver, _writer, configuration);
_handlers[(byte) RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(plugin, _webhooks);
_handlers[(byte) RagonOperation.CREATE_ROOM] = new RoomCreateOperation(plugin, _webhooks);
_handlers[(byte) RagonOperation.JOIN_ROOM] = new RoomJoinOperation(plugin, _webhooks);
_handlers[(byte) RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(plugin, _webhooks);
_handlers[(byte) RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_webhooks);
_handlers[(byte) RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(_webhooks);
_handlers[(byte) RagonOperation.LOAD_SCENE] = new SceneLoadOperation();
_handlers[(byte) RagonOperation.SCENE_LOADED] = new SceneLoadedOperation();
_handlers[(byte) RagonOperation.CREATE_ENTITY] = new EntityCreateOperation();
@@ -198,7 +198,23 @@ public class RagonServer : IRagonServer, INetworkListener
_logger.Error(ex);
}
}
public IRagonOperation ResolveOperation(RagonOperation operation) => _handlers[(byte)operation];
public RagonContext? ResolveContext(INetworkConnection connection) => _contextsByConnection.TryGetValue(connection.Id, out var context) ? context : null;
public RagonContext? ResolveContext(string playerId) => _contextsByPlayerId.TryGetValue(playerId, out var context) ? context : null;
public IRagonOperation ResolveOperation(RagonOperation operation)
{
return _handlers[(byte)operation];
}
public RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection)
{
return _contextsByConnection.TryGetValue(connection.Id, out var context) ?
context.LobbyPlayer :
null;
}
public RagonLobbyPlayer? GetPlayerById(string playerId)
{
return _contextsByPlayerId.TryGetValue(playerId, out var context) ?
context.LobbyPlayer :
null;
}
}
+3
View File
@@ -14,6 +14,7 @@
* limitations under the License.
*/
using Ragon.Server.Entity;
using Ragon.Server.IO;
namespace Ragon.Server.Room;
@@ -22,4 +23,6 @@ public interface IRagonRoom
{
RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection);
RagonRoomPlayer GetPlayerById(string id);
IRagonEntity GetEntityById(ushort id);
IRagonEntity[] GetEntitiesOfPlayer(RagonRoomPlayer id);
}
+23 -4
View File
@@ -100,7 +100,7 @@ public class RagonRoom : IRagonRoom, IRagonAction
Writer.WriteUShort(entities);
foreach (var entity in _entitiesDirtySet)
entity.Write(Writer);
entity.WriteState(Writer);
_entitiesDirtySet.Clear();
@@ -205,7 +205,26 @@ public class RagonRoom : IRagonRoom, IRagonAction
foreach (var readyPlayer in ReadyPlayersList)
readyPlayer.Connection.Reliable.Send(data);
}
public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection) => Players[connection.Id];
public RagonRoomPlayer GetPlayerById(string id) => PlayerList.First(p => p.Id == id);
public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection)
{
return Players[connection.Id];
}
public RagonRoomPlayer? GetPlayerById(string id)
{
return PlayerList.FirstOrDefault(p => p.Id == id);
}
public IRagonEntity? GetEntityById(ushort id)
{
return Entities.TryGetValue(id, out var entity) ?
entity :
null;
}
public IRagonEntity[] GetEntitiesOfPlayer(RagonRoomPlayer player)
{
return EntityList.Where(e => e.Owner.Connection.Id == player.Connection.Id).ToArray();
}
}