diff --git a/Game/Source/GameAuthorizer.cs b/Game/Source/GameAuthorizer.cs index 2e390eb..cbac446 100644 --- a/Game/Source/GameAuthorizer.cs +++ b/Game/Source/GameAuthorizer.cs @@ -1,11 +1,20 @@ +using System; +using System.Text; using Ragon.Core; namespace Game.Source; public class GameAuthorizer: AuthorizationManager { - public override bool OnAuthorize(uint peerId, byte[] payload) + private Configuration _configuration; + public GameAuthorizer(Configuration configuration) { - return true; + _configuration = configuration; + } + + public override bool OnAuthorize(uint peerId, ref ReadOnlySpan payload) + { + var apiKey = Encoding.UTF8.GetString(payload); + return _configuration.ApiKey == apiKey; } } \ No newline at end of file diff --git a/Game/Source/GameFactory.cs b/Game/Source/GameFactory.cs index 085bd8f..e3ab0ca 100644 --- a/Game/Source/GameFactory.cs +++ b/Game/Source/GameFactory.cs @@ -6,6 +6,6 @@ namespace Game.Source { public string PluginName { get; set; } = "ExamplePlugin"; public PluginBase CreatePlugin(string map) => new ExamplePlugin(); - public AuthorizationManager CreateManager() => new GameAuthorizer(); + public AuthorizationManager CreateManager(Configuration configuration) => new GameAuthorizer(configuration); } } \ No newline at end of file diff --git a/Ragon/Ragon.csproj b/Ragon/Ragon.csproj index 9169aa0..dede3de 100755 --- a/Ragon/Ragon.csproj +++ b/Ragon/Ragon.csproj @@ -28,9 +28,4 @@ - - - - - diff --git a/Ragon/Sources/Application.cs b/Ragon/Sources/Application.cs index bdb6d8c..36f1773 100755 --- a/Ragon/Sources/Application.cs +++ b/Ragon/Sources/Application.cs @@ -16,11 +16,13 @@ namespace Ragon.Core private readonly List _roomThreads = new(); private readonly Dictionary _socketByRoomThreads = new(); private readonly Dictionary _roomThreadCounter = new(); - + private readonly Configuration _configuration; private readonly ENetServer _socketServer; + public Application(PluginFactory factory, Configuration configuration, int threadsCount) { _socketServer = new ENetServer(); + _configuration = configuration; for (var i = 0; i < threadsCount; i++) { diff --git a/Ragon/Sources/AuthorizationManager.cs b/Ragon/Sources/AuthorizationManager.cs index ec9797b..4cb5f6d 100644 --- a/Ragon/Sources/AuthorizationManager.cs +++ b/Ragon/Sources/AuthorizationManager.cs @@ -1,8 +1,10 @@ +using System; + namespace Ragon.Core; public class AuthorizationManager { - public virtual bool OnAuthorize(uint peerId, byte[] payload) + public virtual bool OnAuthorize(uint peerId, ref ReadOnlySpan payload) { return true; } diff --git a/Ragon/Sources/Configuration/Configuration.cs b/Ragon/Sources/Configuration/Configuration.cs index 5da93fd..1b6aa40 100755 --- a/Ragon/Sources/Configuration/Configuration.cs +++ b/Ragon/Sources/Configuration/Configuration.cs @@ -1,9 +1,10 @@ -namespace Ragon.Core +using System; + +namespace Ragon.Core { - public class Configuration + [Serializable] + public struct Configuration { - public int UdpPort; - public int WsPort; - public string Transport; + public string ApiKey; } } \ No newline at end of file diff --git a/Ragon/Sources/Plugin/PluginFactory.cs b/Ragon/Sources/Plugin/PluginFactory.cs index c660bbd..a264d15 100644 --- a/Ragon/Sources/Plugin/PluginFactory.cs +++ b/Ragon/Sources/Plugin/PluginFactory.cs @@ -4,7 +4,7 @@ namespace Ragon.Core { public string PluginName { get; set; } public PluginBase CreatePlugin(string map); - public AuthorizationManager CreateManager(); + public AuthorizationManager CreateManager(Configuration configuration); } diff --git a/Ragon/Sources/Rooms/Room.cs b/Ragon/Sources/Rooms/Room.cs index a5afd0d..229b984 100755 --- a/Ragon/Sources/Rooms/Room.cs +++ b/Ragon/Sources/Rooms/Room.cs @@ -106,8 +106,6 @@ namespace Ragon.Core public void ProcessEvent(RagonOperation operation, uint peerId, ReadOnlySpan rawData) { - if (_plugin.InternalHandle(peerId, (ushort) operation, ref rawData)) return; - switch (operation) { case RagonOperation.REPLICATE_ENTITY_STATE: @@ -158,6 +156,11 @@ namespace Ragon.Core case RagonOperation.REPLICATE_ENTITY_EVENT: { Span data = stackalloc byte[rawData.Length]; + + var evntCodeData = rawData.Slice(2, 2); + var evntId = RagonHeader.ReadUShort(ref evntCodeData); + if (_plugin.InternalHandle(peerId, evntId, ref rawData)) return; + rawData.CopyTo(data); Broadcast(_readyPlayers, data, DeliveryType.Reliable); break; diff --git a/Ragon/Sources/Rooms/RoomManager.cs b/Ragon/Sources/Rooms/RoomManager.cs index 8a09c25..c0c5408 100644 --- a/Ragon/Sources/Rooms/RoomManager.cs +++ b/Ragon/Sources/Rooms/RoomManager.cs @@ -23,7 +23,7 @@ namespace Ragon.Core _roomThread = roomThread; _factory = factory; - _manager = _factory.CreateManager(); + _manager = _factory.CreateManager(roomThread); _rooms = new List(); _peersByRoom = new Dictionary(); } diff --git a/Ragon/Sources/Rooms/RoomThread.cs b/Ragon/Sources/Rooms/RoomThread.cs index 87c7ee8..449086c 100755 --- a/Ragon/Sources/Rooms/RoomThread.cs +++ b/Ragon/Sources/Rooms/RoomThread.cs @@ -18,20 +18,23 @@ namespace Ragon.Core private RingBuffer _receiveBuffer = new RingBuffer(8192 + 8192); private RingBuffer _sendBuffer = new RingBuffer(8192 + 8192); - + + public Configuration Configuration { get; private set; } public bool ReadOutEvent(out Event evnt) => _sendBuffer.TryDequeue(out evnt); public void WriteOutEvent(Event evnt) => _sendBuffer.Enqueue(evnt); public bool ReadIntEvent(out Event evnt) => _receiveBuffer.TryDequeue(out evnt); public void WriteInEvent(Event evnt) => _receiveBuffer.Enqueue(evnt); - - public RoomThread(PluginFactory factory) + + public RoomThread(PluginFactory factory, Configuration configuration) { _thread = new Thread(Execute); _thread.IsBackground = true; _timer = new Stopwatch(); _socketByRooms = new Dictionary(); - + + Configuration = configuration; + _roomManager = new RoomManager(this, factory); _roomManager.OnJoined += (tuple) => _socketByRooms.Add(tuple.Item1, tuple.Item2); _roomManager.OnLeaved += (tuple) => _socketByRooms.Remove(tuple.Item1); diff --git a/Ragon/Sources/Storage/EntityInfo.cs b/Ragon/Sources/Storage/EntityInfo.cs new file mode 100644 index 0000000..54f3b74 --- /dev/null +++ b/Ragon/Sources/Storage/EntityInfo.cs @@ -0,0 +1,6 @@ +namespace Ragon.Core.Storage; + +public struct EntityInfo +{ + +} \ No newline at end of file diff --git a/Ragon/Sources/Storage/PlayerInfo.cs b/Ragon/Sources/Storage/PlayerInfo.cs new file mode 100644 index 0000000..a1bf14b --- /dev/null +++ b/Ragon/Sources/Storage/PlayerInfo.cs @@ -0,0 +1,6 @@ +namespace Ragon.Core.Storage; + +public struct PlayerInfo +{ + +} \ No newline at end of file diff --git a/Ragon/Sources/Storage/RoomInfo.cs b/Ragon/Sources/Storage/RoomInfo.cs new file mode 100644 index 0000000..e7721c2 --- /dev/null +++ b/Ragon/Sources/Storage/RoomInfo.cs @@ -0,0 +1,6 @@ +namespace Ragon.Core.Storage; + +public struct RoomInfo +{ + +} \ No newline at end of file diff --git a/Ragon/Sources/Storage/Storage.cs b/Ragon/Sources/Storage/Storage.cs new file mode 100644 index 0000000..5e5c28a --- /dev/null +++ b/Ragon/Sources/Storage/Storage.cs @@ -0,0 +1,30 @@ +using StackExchange.Redis; + +namespace Ragon.Core.Storage; + +public class Storage +{ + private ConnectionMultiplexer _connection; + + public Storage(Configuration _configuration) + { + _connection = ConnectionMultiplexer.Connect(_configuration.ApiKey); + } + + public void UpdateEntity(int entityId) + { + var db = _connection.GetDatabase(); + + db.set("entity_", ) + } + + public void UpdatePlayer() + { + + } + + public void UpdateRoom() + { + + } +}