Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d3ae5a4465 | |||
| 5bf1881f81 | |||
| 6886808132 | |||
| b4cba20d82 | |||
| accd442388 | |||
| d115c98b79 | |||
| 5c20fbafc1 | |||
| bd0c6df5ea | |||
| c4811ef052 | |||
| d82964526c | |||
| acedaef270 | |||
| 0c4bdb83c9 | |||
| 5a79502848 |
@@ -12,11 +12,11 @@
|
|||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<DebugType>none</DebugType>
|
<DebugType>none</DebugType>
|
||||||
<OutputPath>/Users/edmand46/UnityProjects/itd-client/Assets/Ragon/Runtime/Plugins/</OutputPath>
|
<OutputPath></OutputPath>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||||
<OutputPath>/Users/edmand46/RagonProjects/ragon-oss-examples/Assets/Ragon/Plugins/</OutputPath>
|
<OutputPath></OutputPath>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ namespace Ragon.Client
|
|||||||
public ushort Id { get; private set; }
|
public ushort Id { get; private set; }
|
||||||
public ushort Type { get; private set; }
|
public ushort Type { get; private set; }
|
||||||
|
|
||||||
|
|
||||||
public RagonAuthority Authority { get; private set; }
|
public RagonAuthority Authority { get; private set; }
|
||||||
public RagonPlayer Owner { get; private set; }
|
public RagonPlayer Owner { get; private set; }
|
||||||
public RagonEntityState State { get; private set; }
|
public RagonEntityState State { get; private set; }
|
||||||
|
|||||||
@@ -19,9 +19,9 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
public struct RagonRoomInformation
|
public struct RoomParameters
|
||||||
{
|
{
|
||||||
public RagonRoomInformation(string roomId, string playerId, string ownerId, ushort min, ushort max)
|
public RoomParameters(string roomId, string playerId, string ownerId, ushort min, ushort max)
|
||||||
{
|
{
|
||||||
RoomId = roomId;
|
RoomId = roomId;
|
||||||
PlayerId = playerId;
|
PlayerId = playerId;
|
||||||
@@ -39,6 +39,8 @@ public struct RagonRoomInformation
|
|||||||
|
|
||||||
internal class JoinSuccessHandler : IHandler
|
internal class JoinSuccessHandler : IHandler
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private readonly RagonListenerList _listenerList;
|
private readonly RagonListenerList _listenerList;
|
||||||
private readonly RagonPlayerCache _playerCache;
|
private readonly RagonPlayerCache _playerCache;
|
||||||
private readonly RagonEntityCache _entityCache;
|
private readonly RagonEntityCache _entityCache;
|
||||||
@@ -67,7 +69,7 @@ internal class JoinSuccessHandler : IHandler
|
|||||||
var sceneName = reader.ReadString();
|
var sceneName = reader.ReadString();
|
||||||
|
|
||||||
var scene = new RagonScene(_client, _playerCache, _entityCache, sceneName);
|
var scene = new RagonScene(_client, _playerCache, _entityCache, sceneName);
|
||||||
var roomInfo = new RagonRoomInformation(roomId, localId, ownerId, min, max);
|
var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max);
|
||||||
var room = new RagonRoom(_client, _entityCache, _playerCache, roomInfo, scene);
|
var room = new RagonRoom(_client, _entityCache, _playerCache, roomInfo, scene);
|
||||||
|
|
||||||
_playerCache.SetOwnerAndLocal(ownerId, localId);
|
_playerCache.SetOwnerAndLocal(ownerId, localId);
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client
|
||||||
|
{
|
||||||
|
|
||||||
|
internal class PlayerUserDataHandler: IHandler
|
||||||
|
{
|
||||||
|
private RagonPlayerCache _playerCache;
|
||||||
|
private RagonListenerList _listenerList;
|
||||||
|
|
||||||
|
public PlayerUserDataHandler(
|
||||||
|
RagonPlayerCache playerCache,
|
||||||
|
RagonListenerList listenerList
|
||||||
|
)
|
||||||
|
{
|
||||||
|
_playerCache = playerCache;
|
||||||
|
_listenerList = listenerList;
|
||||||
|
}
|
||||||
|
public void Handle(RagonBuffer reader)
|
||||||
|
{
|
||||||
|
var playerPeerId = reader.ReadUShort();
|
||||||
|
var player = _playerCache.GetPlayerByPeer(playerPeerId);
|
||||||
|
|
||||||
|
if (player != null)
|
||||||
|
{
|
||||||
|
player.UserData.Read(reader);
|
||||||
|
|
||||||
|
_listenerList.OnPlayerUserData(player);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
RagonLog.Warn("Received user data for unknown player.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -50,6 +50,6 @@ public class RoomEventHandler : IHandler
|
|||||||
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
|
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_client.Room.Event(eventCode, player, buffer);
|
_client.Room.HandleEvent(eventCode, player, buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
internal class RoomListHandler: IHandler
|
||||||
|
{
|
||||||
|
private RagonListenerList _listenerList;
|
||||||
|
private RagonSession _session;
|
||||||
|
|
||||||
|
public RoomListHandler(RagonSession session, RagonListenerList list)
|
||||||
|
{
|
||||||
|
_session = session;
|
||||||
|
_listenerList = list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Handle(RagonBuffer reader)
|
||||||
|
{
|
||||||
|
var roomCount = reader.ReadUShort();
|
||||||
|
var roomList = new RagonRoomInformation[roomCount];
|
||||||
|
for (int i = 0; i < roomCount; i++)
|
||||||
|
{
|
||||||
|
var id = reader.ReadString();
|
||||||
|
var scene = reader.ReadString();
|
||||||
|
var maxPlayers = reader.ReadUShort();
|
||||||
|
var minPlayers = reader.ReadUShort();
|
||||||
|
var players = reader.ReadUShort();
|
||||||
|
|
||||||
|
var roomInfo = new RagonRoomInformation()
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
Scene = scene,
|
||||||
|
PlayerCount = players,
|
||||||
|
PlayerMax = maxPlayers,
|
||||||
|
PlayerMin = minPlayers,
|
||||||
|
Properties = new Dictionary<string, byte[]>()
|
||||||
|
};
|
||||||
|
|
||||||
|
roomList[i] = roomInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
_listenerList.OnRoomList(roomList);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client
|
||||||
|
{
|
||||||
|
internal class RoomUserDataHandler : IHandler
|
||||||
|
{
|
||||||
|
private readonly RagonClient _client;
|
||||||
|
private readonly RagonListenerList _listenerList;
|
||||||
|
|
||||||
|
public RoomUserDataHandler(RagonClient client, RagonListenerList listenerList)
|
||||||
|
{
|
||||||
|
_client = client;
|
||||||
|
_listenerList = listenerList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Handle(RagonBuffer reader)
|
||||||
|
{
|
||||||
|
_client.Room?.HandleUserData(reader);
|
||||||
|
_listenerList.OnRoomUserData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
public interface IUserData
|
||||||
|
{
|
||||||
|
public byte[] this[string key] { get; set; }
|
||||||
|
bool Dirty { get; }
|
||||||
|
void Read(RagonBuffer buffer);
|
||||||
|
void Write(RagonBuffer buffer);
|
||||||
|
}
|
||||||
@@ -25,7 +25,10 @@ namespace Ragon.Client
|
|||||||
IRagonSceneListener,
|
IRagonSceneListener,
|
||||||
IRagonOwnershipChangedListener,
|
IRagonOwnershipChangedListener,
|
||||||
IRagonPlayerJoinListener,
|
IRagonPlayerJoinListener,
|
||||||
IRagonPlayerLeftListener
|
IRagonPlayerLeftListener,
|
||||||
|
IRagonRoomListListener,
|
||||||
|
IRagonRoomUserDataListener,
|
||||||
|
IRagonPlayerUserDataListener
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
public interface IRagonPlayerUserDataListener
|
||||||
|
{
|
||||||
|
void OnPlayerUserDataUpdated(RagonClient client, RagonPlayer player);
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
public interface IRagonRoomListListener
|
||||||
|
{
|
||||||
|
public void OnRoomListUpdate(IReadOnlyList<RagonRoomInformation> roomsInfos);
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
public interface IRagonRoomUserDataListener
|
||||||
|
{
|
||||||
|
public void OnUserDataUpdated(RagonClient client);
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -29,7 +29,7 @@ namespace Ragon.Client
|
|||||||
private RagonBuffer _writeBuffer;
|
private RagonBuffer _writeBuffer;
|
||||||
private RagonRoom _room;
|
private RagonRoom _room;
|
||||||
private RagonSession _session;
|
private RagonSession _session;
|
||||||
private RagonListenerList listeners;
|
private RagonListenerList _listeners;
|
||||||
private RagonPlayerCache _playerCache;
|
private RagonPlayerCache _playerCache;
|
||||||
private RagonEntityCache _entityCache;
|
private RagonEntityCache _entityCache;
|
||||||
private RagonEventCache _eventCache;
|
private RagonEventCache _eventCache;
|
||||||
@@ -47,7 +47,7 @@ namespace Ragon.Client
|
|||||||
public RagonEntityCache Entity => _entityCache;
|
public RagonEntityCache Entity => _entityCache;
|
||||||
public NetworkStatistics Statistics => _stats;
|
public NetworkStatistics Statistics => _stats;
|
||||||
public RagonRoom Room => _room;
|
public RagonRoom Room => _room;
|
||||||
|
|
||||||
internal RagonBuffer Buffer => _writeBuffer;
|
internal RagonBuffer Buffer => _writeBuffer;
|
||||||
internal INetworkChannel Reliable => _connection.Reliable;
|
internal INetworkChannel Reliable => _connection.Reliable;
|
||||||
internal INetworkChannel Unreliable => _connection.Unreliable;
|
internal INetworkChannel Unreliable => _connection.Unreliable;
|
||||||
@@ -56,13 +56,13 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public RagonClient(INetworkConnection connection, int rate)
|
public RagonClient(INetworkConnection connection, int rate)
|
||||||
{
|
{
|
||||||
listeners = new RagonListenerList(this);
|
_listeners = new RagonListenerList(this);
|
||||||
|
|
||||||
_connection = connection;
|
_connection = connection;
|
||||||
_connection.OnData += OnData;
|
_connection.OnData += OnData;
|
||||||
_connection.OnConnected += OnConnected;
|
_connection.OnConnected += OnConnected;
|
||||||
_connection.OnDisconnected += OnDisconnected;
|
_connection.OnDisconnected += OnDisconnected;
|
||||||
|
|
||||||
_replicationRate = (1000.0f / rate) / 1000.0f;
|
_replicationRate = (1000.0f / rate) / 1000.0f;
|
||||||
_replicationTime = 0;
|
_replicationTime = 0;
|
||||||
|
|
||||||
@@ -98,30 +98,37 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
_writeBuffer = new RagonBuffer();
|
_writeBuffer = new RagonBuffer();
|
||||||
_readBuffer = new RagonBuffer();
|
_readBuffer = new RagonBuffer();
|
||||||
_session = new RagonSession(this, _writeBuffer);
|
|
||||||
|
|
||||||
_playerCache = new RagonPlayerCache();
|
_playerCache = new RagonPlayerCache();
|
||||||
|
_session = new RagonSession(this, _writeBuffer);
|
||||||
_entityCache = new RagonEntityCache(this, _playerCache, _sceneCollector);
|
_entityCache = new RagonEntityCache(this, _playerCache, _sceneCollector);
|
||||||
|
|
||||||
_handlers = new IHandler[byte.MaxValue];
|
_handlers = new IHandler[byte.MaxValue];
|
||||||
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, listeners);
|
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, _listeners);
|
||||||
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(listeners);
|
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listeners);
|
||||||
_handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, listeners, _playerCache, _entityCache);
|
_handlers[(byte)RagonOperation.JOIN_SUCCESS] =
|
||||||
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(listeners);
|
new JoinSuccessHandler(this, _listeners, _playerCache, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, listeners, _entityCache);
|
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listeners);
|
||||||
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = new OwnershipRoomHandler(listeners, _playerCache, _entityCache);
|
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.OWNERSHIP_ENTITY_CHANGED] = new EntityOwnershipHandler(listeners, _playerCache, _entityCache);
|
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] =
|
||||||
_handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, listeners);
|
new OwnershipRoomHandler(_listeners, _playerCache, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, listeners);
|
_handlers[(byte)RagonOperation.OWNERSHIP_ENTITY_CHANGED] =
|
||||||
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, listeners);
|
new EntityOwnershipHandler(_listeners, _playerCache, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateHandler(this, _playerCache, _entityCache, _entityListener);
|
_handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.CREATE_ENTITY] =
|
||||||
|
new EntityCreateHandler(this, _playerCache, _entityCache, _entityListener);
|
||||||
_handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityRemoveHandler(_entityCache);
|
_handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityRemoveHandler(_entityCache);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache);
|
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventHandler(_playerCache, _entityCache);
|
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventHandler(_playerCache, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache);
|
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache);
|
||||||
_handlers[(byte)RagonOperation.SNAPSHOT] = new SnapshotHandler(this, listeners, _entityCache, _playerCache, _entityListener);
|
_handlers[(byte)RagonOperation.SNAPSHOT] =
|
||||||
|
new SnapshotHandler(this, _listeners, _entityCache, _playerCache, _entityListener);
|
||||||
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampHandler(this);
|
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampHandler(this);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataHandler(_playerCache, listeners);
|
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataHandler(_playerCache, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.ROOM_LIST_UPDATED] = new RoomListHandler(_session, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataHandler(this, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataHandler(_playerCache, _listeners);
|
||||||
|
|
||||||
var protocolRaw = RagonVersion.Parse(protocol);
|
var protocolRaw = RagonVersion.Parse(protocol);
|
||||||
_connection.Connect(address, port, protocolRaw);
|
_connection.Connect(address, port, protocolRaw);
|
||||||
@@ -147,12 +154,14 @@ namespace Ragon.Client
|
|||||||
_entityCache.WriteState(_writeBuffer);
|
_entityCache.WriteState(_writeBuffer);
|
||||||
|
|
||||||
SendTimestamp();
|
SendTimestamp();
|
||||||
|
SendRoomUserData();
|
||||||
|
SendPlayerUserData();
|
||||||
}
|
}
|
||||||
|
|
||||||
_stats.Update(_connection.BytesSent, _connection.BytesReceived, _connection.Ping, dt);
|
_stats.Update(_connection.BytesSent, _connection.BytesReceived, _connection.Ping, dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
listeners.Update();
|
_listeners.Update();
|
||||||
_connection.Update();
|
_connection.Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,34 +172,40 @@ namespace Ragon.Client
|
|||||||
_status = RagonStatus.DISCONNECTED;
|
_status = RagonStatus.DISCONNECTED;
|
||||||
_connection.Disconnect();
|
_connection.Disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
_connection.Dispose();
|
_connection.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddListener(IRagonListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonAuthorizationListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonAuthorizationListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonConnectionListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonConnectionListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonFailedListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonFailedListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonJoinListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonJoinListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonLeftListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonLeftListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonOwnershipChangedListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonOwnershipChangedListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonPlayerJoinListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonPlayerJoinListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonPlayerLeftListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonPlayerLeftListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonSceneListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonSceneListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonSceneRequestListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonSceneRequestListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonDataListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonDataListener listener) => _listeners.Add(listener);
|
||||||
|
public void AddListener(IRagonRoomListListener listener) => _listeners.Add(listener);
|
||||||
public void RemoveListener(IRagonListener listener) => listeners.Remove(listener);
|
public void AddListener(IRagonPlayerUserDataListener listener) => _listeners.Add(listener);
|
||||||
public void RemoveListener(IRagonAuthorizationListener listener) => listeners.Remove(listener);
|
public void AddListener(IRagonRoomUserDataListener listener) => _listeners.Add(listener);
|
||||||
public void RemoveListener(IRagonConnectionListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonFailedListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonAuthorizationListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonJoinListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonConnectionListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonLeftListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonFailedListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonOwnershipChangedListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonJoinListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonPlayerJoinListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonLeftListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonPlayerLeftListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonOwnershipChangedListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonSceneListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonPlayerJoinListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonSceneRequestListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonPlayerLeftListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonDataListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonSceneListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonSceneRequestListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonDataListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonRoomListListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonRoomUserDataListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonPlayerUserDataListener listener) => _listeners.Remove(listener);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -230,11 +245,43 @@ namespace Ragon.Client
|
|||||||
_writeBuffer.Write(value.Int1, 32);
|
_writeBuffer.Write(value.Int1, 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void SendRoomUserData()
|
||||||
|
{
|
||||||
|
if (_room == null) return;
|
||||||
|
|
||||||
|
var props = _room.UserData;
|
||||||
|
if (!props.Dirty) return;
|
||||||
|
|
||||||
|
_writeBuffer.Clear();
|
||||||
|
_writeBuffer.WriteOperation(RagonOperation.ROOM_DATA_UPDATED);
|
||||||
|
|
||||||
|
props.Write(_writeBuffer);
|
||||||
|
|
||||||
|
var sendData = _writeBuffer.ToArray();
|
||||||
|
_connection.Reliable.Send(sendData);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SendPlayerUserData()
|
||||||
|
{
|
||||||
|
if (_playerCache.Local == null) return;
|
||||||
|
|
||||||
|
var props = _playerCache.Local.UserData;
|
||||||
|
if (!props.Dirty) return;
|
||||||
|
|
||||||
|
_writeBuffer.Clear();
|
||||||
|
_writeBuffer.WriteOperation(RagonOperation.PLAYER_DATA_UPDATED);
|
||||||
|
|
||||||
|
props.Write(_writeBuffer);
|
||||||
|
|
||||||
|
var sendData = _writeBuffer.ToArray();
|
||||||
|
_connection.Reliable.Send(sendData);
|
||||||
|
}
|
||||||
|
|
||||||
private void OnConnected()
|
private void OnConnected()
|
||||||
{
|
{
|
||||||
RagonLog.Trace("Connected");
|
RagonLog.Trace("Connected");
|
||||||
|
|
||||||
listeners.OnConnected();
|
_listeners.OnConnected();
|
||||||
_status = RagonStatus.CONNECTED;
|
_status = RagonStatus.CONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,7 +289,7 @@ namespace Ragon.Client
|
|||||||
{
|
{
|
||||||
RagonLog.Trace($"Disconnected: {reason}");
|
RagonLog.Trace($"Disconnected: {reason}");
|
||||||
|
|
||||||
listeners.OnDisconnected(reason);
|
_listeners.OnDisconnected(reason);
|
||||||
_status = RagonStatus.DISCONNECTED;
|
_status = RagonStatus.DISCONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -32,6 +32,9 @@ namespace Ragon.Client
|
|||||||
private readonly List<IRagonPlayerJoinListener> _playerJoinListeners = new();
|
private readonly List<IRagonPlayerJoinListener> _playerJoinListeners = new();
|
||||||
private readonly List<IRagonPlayerLeftListener> _playerLeftListeners = new();
|
private readonly List<IRagonPlayerLeftListener> _playerLeftListeners = new();
|
||||||
private readonly List<IRagonDataListener> _dataListeners = new();
|
private readonly List<IRagonDataListener> _dataListeners = new();
|
||||||
|
private readonly List<IRagonRoomListListener> _roomListListeners = new();
|
||||||
|
private readonly List<IRagonRoomUserDataListener> _roomUserDataListeners = new();
|
||||||
|
private readonly List<IRagonPlayerUserDataListener> _playerUserDataListeners = new();
|
||||||
private readonly List<Action> _delayedActions = new();
|
private readonly List<Action> _delayedActions = new();
|
||||||
|
|
||||||
public RagonListenerList(RagonClient client)
|
public RagonListenerList(RagonClient client)
|
||||||
@@ -50,6 +53,8 @@ namespace Ragon.Client
|
|||||||
_ownershipChangedListeners.Add(listener);
|
_ownershipChangedListeners.Add(listener);
|
||||||
_playerJoinListeners.Add(listener);
|
_playerJoinListeners.Add(listener);
|
||||||
_playerLeftListeners.Add(listener);
|
_playerLeftListeners.Add(listener);
|
||||||
|
_roomUserDataListeners.Add(listener);
|
||||||
|
_playerUserDataListeners.Add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(IRagonListener listener)
|
public void Remove(IRagonListener listener)
|
||||||
@@ -65,6 +70,8 @@ namespace Ragon.Client
|
|||||||
_ownershipChangedListeners.Remove(listener);
|
_ownershipChangedListeners.Remove(listener);
|
||||||
_playerJoinListeners.Remove(listener);
|
_playerJoinListeners.Remove(listener);
|
||||||
_playerLeftListeners.Remove(listener);
|
_playerLeftListeners.Remove(listener);
|
||||||
|
_roomUserDataListeners.Remove(listener);
|
||||||
|
_playerUserDataListeners.Remove(listener);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +87,7 @@ namespace Ragon.Client
|
|||||||
{
|
{
|
||||||
_dataListeners.Add(dataListener);
|
_dataListeners.Add(dataListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add(IRagonAuthorizationListener listener)
|
public void Add(IRagonAuthorizationListener listener)
|
||||||
{
|
{
|
||||||
_authorizationListeners.Add(listener);
|
_authorizationListeners.Add(listener);
|
||||||
@@ -130,12 +137,27 @@ namespace Ragon.Client
|
|||||||
{
|
{
|
||||||
_playerLeftListeners.Add(listener);
|
_playerLeftListeners.Add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Add(IRagonRoomListListener listener)
|
||||||
|
{
|
||||||
|
_roomListListeners.Add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(IRagonRoomUserDataListener listener)
|
||||||
|
{
|
||||||
|
_roomUserDataListeners.Add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(IRagonPlayerUserDataListener listener)
|
||||||
|
{
|
||||||
|
_playerUserDataListeners.Add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
public void Remove(IRagonDataListener listener)
|
public void Remove(IRagonDataListener listener)
|
||||||
{
|
{
|
||||||
_delayedActions.Add(() => _dataListeners.Remove(listener));
|
_delayedActions.Add(() => _dataListeners.Remove(listener));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(IRagonSceneRequestListener listener)
|
public void Remove(IRagonSceneRequestListener listener)
|
||||||
{
|
{
|
||||||
_delayedActions.Add(() => _sceneRequestListeners.Remove(listener));
|
_delayedActions.Add(() => _sceneRequestListeners.Remove(listener));
|
||||||
@@ -186,6 +208,21 @@ namespace Ragon.Client
|
|||||||
_delayedActions.Add(() => _playerLeftListeners.Remove(listener));
|
_delayedActions.Add(() => _playerLeftListeners.Remove(listener));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Remove(IRagonRoomListListener listener)
|
||||||
|
{
|
||||||
|
_delayedActions.Add(() => _roomListListeners.Remove(listener));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(IRagonRoomUserDataListener listener)
|
||||||
|
{
|
||||||
|
_delayedActions.Add(() => _roomUserDataListeners.Remove(listener));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(IRagonPlayerUserDataListener listener)
|
||||||
|
{
|
||||||
|
_delayedActions.Add(() => _playerUserDataListeners.Remove(listener));
|
||||||
|
}
|
||||||
|
|
||||||
public void OnAuthorizationSuccess(string playerId, string playerName, string payload)
|
public void OnAuthorizationSuccess(string playerId, string playerName, string payload)
|
||||||
{
|
{
|
||||||
foreach (var listener in _authorizationListeners)
|
foreach (var listener in _authorizationListeners)
|
||||||
@@ -263,5 +300,23 @@ namespace Ragon.Client
|
|||||||
foreach (var listener in _dataListeners)
|
foreach (var listener in _dataListeners)
|
||||||
listener.OnData(player, data);
|
listener.OnData(player, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnRoomList(RagonRoomInformation[] roomInfos)
|
||||||
|
{
|
||||||
|
foreach (var listListener in _roomListListeners)
|
||||||
|
listListener.OnRoomListUpdate(roomInfos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnRoomUserData()
|
||||||
|
{
|
||||||
|
foreach (var userDataListener in _roomUserDataListeners)
|
||||||
|
userDataListener.OnUserDataUpdated(_client);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnPlayerUserData(RagonPlayer player)
|
||||||
|
{
|
||||||
|
foreach(var playerUserDataListener in _playerUserDataListeners)
|
||||||
|
playerUserDataListener.OnPlayerUserDataUpdated(_client, player);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -24,6 +24,7 @@ namespace Ragon.Client
|
|||||||
public ushort PeerId { get; set; }
|
public ushort PeerId { get; set; }
|
||||||
public bool IsRoomOwner { get; set; }
|
public bool IsRoomOwner { get; set; }
|
||||||
public bool IsLocal { get; set; }
|
public bool IsLocal { get; set; }
|
||||||
|
public IUserData UserData { get; private set; }
|
||||||
|
|
||||||
public RagonPlayer(ushort peerId, string playerId, string name, bool isRoomOwner, bool isLocal)
|
public RagonPlayer(ushort peerId, string playerId, string name, bool isRoomOwner, bool isLocal)
|
||||||
{
|
{
|
||||||
@@ -32,6 +33,7 @@ namespace Ragon.Client
|
|||||||
IsLocal = isLocal;
|
IsLocal = isLocal;
|
||||||
Name = name;
|
Name = name;
|
||||||
Id = playerId;
|
Id = playerId;
|
||||||
}
|
UserData = isLocal ? new RagonUserData() : new RagonUserDataReadOnly();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,40 +18,74 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client
|
namespace Ragon.Client
|
||||||
{
|
{
|
||||||
public class RagonRoom: IDisposable
|
public class RagonRoom : IDisposable
|
||||||
{
|
{
|
||||||
|
private class EventSubscription : IDisposable
|
||||||
|
{
|
||||||
|
private List<Action<RagonPlayer, IRagonEvent>> _callbacks;
|
||||||
|
private List<Action<RagonPlayer, IRagonEvent>> _localCallbacks;
|
||||||
|
private Action<RagonPlayer, IRagonEvent> _callback;
|
||||||
|
|
||||||
|
public EventSubscription(
|
||||||
|
List<Action<RagonPlayer, IRagonEvent>> callbacks,
|
||||||
|
List<Action<RagonPlayer, IRagonEvent>> localCallbacks,
|
||||||
|
Action<RagonPlayer, IRagonEvent> callback)
|
||||||
|
{
|
||||||
|
_callbacks = callbacks;
|
||||||
|
_localCallbacks = localCallbacks;
|
||||||
|
_callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_callbacks?.Remove(_callback);
|
||||||
|
_localCallbacks?.Remove(_callback);
|
||||||
|
|
||||||
|
_callbacks = null!;
|
||||||
|
_localCallbacks = null!;
|
||||||
|
_callback = null!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
|
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
|
||||||
|
|
||||||
private RagonClient _client;
|
private readonly RagonClient _client;
|
||||||
private RagonScene _scene;
|
private readonly RagonScene _scene;
|
||||||
private RagonEntityCache _entityCache;
|
private readonly RagonEntityCache _entityCache;
|
||||||
private RagonPlayerCache _playerCache;
|
private readonly RagonPlayerCache _playerCache;
|
||||||
private RagonRoomInformation _information;
|
private readonly RoomParameters _parameters;
|
||||||
|
private readonly RagonUserData _userData;
|
||||||
|
|
||||||
public string Id => _information.RoomId;
|
public string Id => _parameters.RoomId;
|
||||||
public int MinPlayers => _information.Min;
|
public int MinPlayers => _parameters.Min;
|
||||||
public int MaxPlayers => _information.Max;
|
public int MaxPlayers => _parameters.Max;
|
||||||
public string Scene => _scene.Name;
|
public string Scene => _scene.Name;
|
||||||
|
|
||||||
public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
|
public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
|
||||||
public RagonPlayer Local => _playerCache.Local;
|
public RagonPlayer Local => _playerCache.Local;
|
||||||
public RagonPlayer Owner => _playerCache.Owner;
|
public RagonPlayer Owner => _playerCache.Owner;
|
||||||
|
public RagonUserData UserData => _userData;
|
||||||
|
|
||||||
private readonly Dictionary<int, OnEventDelegate> _events = new Dictionary<int, OnEventDelegate>();
|
private readonly Dictionary<int, OnEventDelegate> _events = new Dictionary<int, OnEventDelegate>();
|
||||||
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
|
|
||||||
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
|
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners =
|
||||||
|
new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
|
||||||
|
|
||||||
|
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners =
|
||||||
|
new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
|
||||||
|
|
||||||
public RagonRoom(RagonClient client,
|
public RagonRoom(RagonClient client,
|
||||||
RagonEntityCache entityCache,
|
RagonEntityCache entityCache,
|
||||||
RagonPlayerCache playerCache,
|
RagonPlayerCache playerCache,
|
||||||
RagonRoomInformation information,
|
RoomParameters parameters,
|
||||||
RagonScene scene)
|
RagonScene scene)
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_information = information;
|
_parameters = parameters;
|
||||||
_entityCache = entityCache;
|
_entityCache = entityCache;
|
||||||
_playerCache = playerCache;
|
_playerCache = playerCache;
|
||||||
_scene = scene;
|
_scene = scene;
|
||||||
|
_userData = new RagonUserData();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Cleanup()
|
internal void Cleanup()
|
||||||
@@ -65,7 +99,7 @@ namespace Ragon.Client
|
|||||||
_scene.Update(sceneName);
|
_scene.Update(sceneName);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
|
internal void HandleEvent(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
|
||||||
{
|
{
|
||||||
if (_events.TryGetValue(eventCode, out var evnt))
|
if (_events.TryGetValue(eventCode, out var evnt))
|
||||||
evnt?.Invoke(caller, buffer);
|
evnt?.Invoke(caller, buffer);
|
||||||
@@ -73,19 +107,23 @@ namespace Ragon.Client
|
|||||||
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
|
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Action<RagonPlayer, IRagonEvent> OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
|
internal void HandleUserData(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
_userData.Read(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IDisposable OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
|
||||||
{
|
{
|
||||||
var t = new TEvent();
|
var t = new TEvent();
|
||||||
var eventCode = _client.Event.GetEventCode(t);
|
var eventCode = _client.Event.GetEventCode(t);
|
||||||
|
|
||||||
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
|
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
|
||||||
|
|
||||||
if (!_listeners.TryGetValue(eventCode, out var callbacks))
|
if (!_listeners.TryGetValue(eventCode, out var callbacks))
|
||||||
{
|
{
|
||||||
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
||||||
_listeners.Add(eventCode, callbacks);
|
_listeners.Add(eventCode, callbacks);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_localListeners.TryGetValue(eventCode, out var localCallbacks))
|
if (!_localListeners.TryGetValue(eventCode, out var localCallbacks))
|
||||||
{
|
{
|
||||||
localCallbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
localCallbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
||||||
@@ -106,26 +144,18 @@ namespace Ragon.Client
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return action;
|
return new EventSubscription(callbacks, localCallbacks, action);
|
||||||
}
|
|
||||||
|
|
||||||
public void OffEvent<TEvent>(Action<RagonPlayer, IRagonEvent> callback) where TEvent : IRagonEvent, new()
|
|
||||||
{
|
|
||||||
var t = new TEvent();
|
|
||||||
var eventCode = _client.Event.GetEventCode(t);
|
|
||||||
|
|
||||||
if (_listeners.TryGetValue(eventCode, out var callbacks))
|
|
||||||
callbacks.Remove(callback);
|
|
||||||
|
|
||||||
if (_localListeners.TryGetValue(eventCode, out var localCallbacks))
|
|
||||||
localCallbacks.Remove(callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LoadScene(string sceneName) => _scene.Load(sceneName);
|
public void LoadScene(string sceneName) => _scene.Load(sceneName);
|
||||||
public void SceneLoaded() => _scene.SceneLoaded();
|
public void SceneLoaded() => _scene.SceneLoaded();
|
||||||
|
|
||||||
public void ReplicateEvent<TEvent>(TEvent evnt, RagonTarget target, RagonReplicationMode mode) where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
|
public void ReplicateEvent<TEvent>(TEvent evnt, RagonTarget target, RagonReplicationMode mode)
|
||||||
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode mode) where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
|
where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
|
||||||
|
|
||||||
|
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode mode)
|
||||||
|
where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
|
||||||
|
|
||||||
public void ReplicateData(byte[] data, bool reliable = false) => _scene.ReplicateData(data, reliable);
|
public void ReplicateData(byte[] data, bool reliable = false) => _scene.ReplicateData(data, reliable);
|
||||||
|
|
||||||
public void CreateEntity(RagonEntity entity) => CreateEntity(entity, null);
|
public void CreateEntity(RagonEntity entity) => CreateEntity(entity, null);
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace Ragon.Client
|
||||||
|
{
|
||||||
|
public struct RagonRoomInformation
|
||||||
|
{
|
||||||
|
public string Id;
|
||||||
|
public string Scene;
|
||||||
|
public int PlayerMax;
|
||||||
|
public int PlayerMin;
|
||||||
|
public int PlayerCount;
|
||||||
|
public Dictionary<string, byte[]> Properties;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,11 +31,11 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public void CreateOrJoin(string sceneName, int minPlayers, int maxPlayers)
|
public void CreateOrJoin(string sceneName, int minPlayers, int maxPlayers)
|
||||||
{
|
{
|
||||||
var parameters = new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers};
|
var parameters = new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers};
|
||||||
CreateOrJoin(parameters);
|
CreateOrJoin(parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CreateOrJoin(RagonRoomParameters parameters)
|
public void CreateOrJoin(RagonRoomPayload parameters)
|
||||||
{
|
{
|
||||||
_buffer.Clear();
|
_buffer.Clear();
|
||||||
_buffer.WriteOperation(RagonOperation.JOIN_OR_CREATE_ROOM);
|
_buffer.WriteOperation(RagonOperation.JOIN_OR_CREATE_ROOM);
|
||||||
@@ -48,15 +48,15 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public void Create(string sceneName, int minPlayers, int maxPlayers)
|
public void Create(string sceneName, int minPlayers, int maxPlayers)
|
||||||
{
|
{
|
||||||
Create(null, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
|
Create(null, new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Create(string roomId, string sceneName, int minPlayers, int maxPlayers)
|
public void Create(string roomId, string sceneName, int minPlayers, int maxPlayers)
|
||||||
{
|
{
|
||||||
Create(roomId, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
|
Create(roomId, new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Create(string roomId, RagonRoomParameters parameters)
|
public void Create(string roomId, RagonRoomPayload parameters)
|
||||||
{
|
{
|
||||||
_buffer.Clear();
|
_buffer.Clear();
|
||||||
_buffer.WriteOperation(RagonOperation.CREATE_ROOM);
|
_buffer.WriteOperation(RagonOperation.CREATE_ROOM);
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client
|
||||||
|
{
|
||||||
|
public class RagonUserData: IUserData
|
||||||
|
{
|
||||||
|
public byte[] this[string key]
|
||||||
|
{
|
||||||
|
get => _properties[key];
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_properties[key] = value;
|
||||||
|
|
||||||
|
_dirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool Dirty => _dirty;
|
||||||
|
|
||||||
|
private bool _dirty = false;
|
||||||
|
private readonly Dictionary<string, byte[]> _properties = new();
|
||||||
|
|
||||||
|
public RagonUserData()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Read(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
_properties.Clear();
|
||||||
|
|
||||||
|
var len = buffer.ReadUShort();
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
var key = buffer.ReadString();
|
||||||
|
var valueSize = buffer.ReadUShort();
|
||||||
|
var value = buffer.ReadBytes(valueSize);
|
||||||
|
|
||||||
|
_properties[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
buffer.WriteUShort((ushort)_properties.Count);
|
||||||
|
foreach (var property in _properties)
|
||||||
|
{
|
||||||
|
buffer.WriteString(property.Key);
|
||||||
|
buffer.WriteUShort((ushort) property.Value.Length);
|
||||||
|
buffer.WriteBytes(property.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
_dirty = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
public class RagonUserDataReadOnly : IUserData
|
||||||
|
{
|
||||||
|
public byte[] this[string key]
|
||||||
|
{
|
||||||
|
get => _properties[key];
|
||||||
|
set { }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Dirty => _dirty;
|
||||||
|
|
||||||
|
private bool _dirty = false;
|
||||||
|
private readonly Dictionary<string, byte[]> _properties = new();
|
||||||
|
|
||||||
|
public RagonUserDataReadOnly()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
buffer.WriteUShort((ushort)_properties.Count);
|
||||||
|
foreach (var property in _properties)
|
||||||
|
{
|
||||||
|
buffer.WriteString(property.Key);
|
||||||
|
buffer.WriteUShort((ushort)property.Value.Length);
|
||||||
|
buffer.WriteBytes(property.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
_dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Read(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
_properties.Clear();
|
||||||
|
|
||||||
|
var len = buffer.ReadUShort();
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
var key = buffer.ReadString();
|
||||||
|
var valueSize = buffer.ReadUShort();
|
||||||
|
var value = buffer.ReadBytes(valueSize);
|
||||||
|
|
||||||
|
_properties[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -121,6 +121,21 @@ namespace Ragon.Protocol
|
|||||||
return (RagonOperation)Read(8);
|
return (RagonOperation)Read(8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteFloat(float value)
|
||||||
|
{
|
||||||
|
var bytes = BitConverter.GetBytes(value);
|
||||||
|
WriteBytes(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public float ReadFloat()
|
||||||
|
{
|
||||||
|
var bytes = ReadBytes(4);
|
||||||
|
var value = BitConverter.ToSingle(bytes, 0);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteFloat(float value, float min, float max, float precision)
|
public void WriteFloat(float value, float min, float max, float precision)
|
||||||
{
|
{
|
||||||
@@ -147,6 +162,23 @@ namespace Ragon.Protocol
|
|||||||
return adjusted;
|
return adjusted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteInt(int value)
|
||||||
|
{
|
||||||
|
var bytes = BitConverter.GetBytes(value);
|
||||||
|
|
||||||
|
WriteBytes(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int ReadInt()
|
||||||
|
{
|
||||||
|
var bytes = ReadBytes(4);
|
||||||
|
var value = BitConverter.ToInt32(bytes, 0);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteInt(int value, int min, int max)
|
public void WriteInt(int value, int min, int max)
|
||||||
{
|
{
|
||||||
@@ -250,6 +282,7 @@ namespace Ragon.Protocol
|
|||||||
|
|
||||||
_write += numBits;
|
_write += numBits;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public uint Read(int numBits = 16)
|
public uint Read(int numBits = 16)
|
||||||
@@ -281,10 +314,10 @@ namespace Ragon.Protocol
|
|||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
[Obsolete("Do not use this method, will be removed")]
|
[Obsolete("Do not use this method, will be removed")]
|
||||||
public byte[] ReadBytes(int lenght)
|
public byte[] ReadBytes(int len)
|
||||||
{
|
{
|
||||||
var data = new byte[lenght];
|
var data = new byte[len];
|
||||||
for (int i = 0; i < lenght; i++)
|
for (int i = 0; i < len; i++)
|
||||||
data[i] = (byte)Read(8);
|
data[i] = (byte)Read(8);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
|||||||
@@ -44,5 +44,8 @@ namespace Ragon.Protocol
|
|||||||
TRANSFER_ROOM_OWNERSHIP = 23,
|
TRANSFER_ROOM_OWNERSHIP = 23,
|
||||||
TRANSFER_ENTITY_OWNERSHIP = 24,
|
TRANSFER_ENTITY_OWNERSHIP = 24,
|
||||||
TIMESTAMP_SYNCHRONIZATION = 25,
|
TIMESTAMP_SYNCHRONIZATION = 25,
|
||||||
|
ROOM_LIST_UPDATED = 26,
|
||||||
|
PLAYER_DATA_UPDATED = 27,
|
||||||
|
ROOM_DATA_UPDATED = 28,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
namespace Ragon.Protocol
|
namespace Ragon.Protocol
|
||||||
{
|
{
|
||||||
public class RagonRoomParameters: IRagonSerializable
|
public class RagonRoomPayload: IRagonSerializable
|
||||||
{
|
{
|
||||||
public string Scene { get; set; }
|
public string Scene { get; set; }
|
||||||
public int Min { get; set; }
|
public int Min { get; set; }
|
||||||
|
|||||||
@@ -0,0 +1,309 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Ragon.Protocol
|
||||||
|
{
|
||||||
|
[StructLayout(LayoutKind.Explicit)]
|
||||||
|
internal struct ValueConverter
|
||||||
|
{
|
||||||
|
[FieldOffset(0)] public int Int;
|
||||||
|
[FieldOffset(0)] public float Float;
|
||||||
|
[FieldOffset(0)] public long Long;
|
||||||
|
[FieldOffset(0)] public byte Byte0;
|
||||||
|
[FieldOffset(1)] public byte Byte1;
|
||||||
|
[FieldOffset(2)] public byte Byte2;
|
||||||
|
[FieldOffset(3)] public byte Byte3;
|
||||||
|
[FieldOffset(4)] public byte Byte4;
|
||||||
|
[FieldOffset(5)] public byte Byte5;
|
||||||
|
[FieldOffset(6)] public byte Byte6;
|
||||||
|
[FieldOffset(7)] public byte Byte7;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RagonStream
|
||||||
|
{
|
||||||
|
private byte[] _data;
|
||||||
|
private int _offset;
|
||||||
|
private int _size;
|
||||||
|
|
||||||
|
public int Lenght => _offset;
|
||||||
|
public int Size => _size - _offset;
|
||||||
|
|
||||||
|
public RagonStream(int capacity = 256)
|
||||||
|
{
|
||||||
|
_data = new byte[capacity];
|
||||||
|
_offset = 0;
|
||||||
|
_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Reset()
|
||||||
|
{
|
||||||
|
_size = _offset;
|
||||||
|
_offset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void AddOffset(int offset)
|
||||||
|
{
|
||||||
|
_offset += offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteByte(byte value)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(1);
|
||||||
|
_data[_offset] = value;
|
||||||
|
_offset += 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public byte ReadByte()
|
||||||
|
{
|
||||||
|
var value = _data[_offset];
|
||||||
|
_offset += 1;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteBool(bool value)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(1);
|
||||||
|
_data[_offset] = value ? (byte)1 : (byte)0;
|
||||||
|
_offset += 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool ReadBool()
|
||||||
|
{
|
||||||
|
var value = _data[_offset];
|
||||||
|
_offset += 1;
|
||||||
|
return value == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteInt(int value)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(4);
|
||||||
|
var converter = new ValueConverter() { Int = value };
|
||||||
|
_data[_offset] = converter.Byte0;
|
||||||
|
_data[_offset + 1] = converter.Byte1;
|
||||||
|
_data[_offset + 2] = converter.Byte2;
|
||||||
|
_data[_offset + 3] = converter.Byte3;
|
||||||
|
_offset += 4;
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteInt(int value, int offset)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(4);
|
||||||
|
var converter = new ValueConverter() { Int = value };
|
||||||
|
_data[offset] = converter.Byte0;
|
||||||
|
_data[offset + 1] = converter.Byte1;
|
||||||
|
_data[offset + 2] = converter.Byte2;
|
||||||
|
_data[offset + 3] = converter.Byte3;
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int ReadInt()
|
||||||
|
{
|
||||||
|
var converter = new ValueConverter
|
||||||
|
{ Byte0 = _data[_offset], Byte1 = _data[_offset + 1], Byte2 = _data[_offset + 2], Byte3 = _data[_offset + 3] };
|
||||||
|
_offset += 4;
|
||||||
|
return converter.Int;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteLong(long value)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(8);
|
||||||
|
WriteLong(value, _offset);
|
||||||
|
_offset += 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteLong(long value, int offset)
|
||||||
|
{
|
||||||
|
var converter = new ValueConverter() { Long = value };
|
||||||
|
_data[offset] = converter.Byte0;
|
||||||
|
_data[offset + 1] = converter.Byte1;
|
||||||
|
_data[offset + 2] = converter.Byte2;
|
||||||
|
_data[offset + 3] = converter.Byte3;
|
||||||
|
_data[offset + 4] = converter.Byte4;
|
||||||
|
_data[offset + 5] = converter.Byte5;
|
||||||
|
_data[offset + 6] = converter.Byte6;
|
||||||
|
_data[offset + 7] = converter.Byte7;
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public long ReadLong()
|
||||||
|
{
|
||||||
|
var converter = new ValueConverter
|
||||||
|
{
|
||||||
|
Byte0 = _data[_offset],
|
||||||
|
Byte1 = _data[_offset + 1],
|
||||||
|
Byte2 = _data[_offset + 2],
|
||||||
|
Byte3 = _data[_offset + 3],
|
||||||
|
Byte4 = _data[_offset + 4],
|
||||||
|
Byte5 = _data[_offset + 5],
|
||||||
|
Byte6 = _data[_offset + 6],
|
||||||
|
Byte7 = _data[_offset + 7],
|
||||||
|
};
|
||||||
|
_offset += 8;
|
||||||
|
return converter.Long;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteFloat(float value)
|
||||||
|
{
|
||||||
|
var converter = new ValueConverter() { Float = value };
|
||||||
|
WriteInt(converter.Int);
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public float ReadFloat()
|
||||||
|
{
|
||||||
|
var rawValue = ReadInt();
|
||||||
|
var converter = new ValueConverter() { Int = rawValue };
|
||||||
|
var value = converter.Float;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteString(string value)
|
||||||
|
{
|
||||||
|
var rawData = Encoding.UTF8.GetBytes(value);
|
||||||
|
var len = rawData.Length;
|
||||||
|
ResizeIfNeed(2 + len);
|
||||||
|
WriteUShort((ushort)len);
|
||||||
|
|
||||||
|
Buffer.BlockCopy(rawData, 0, _data, _offset, len);
|
||||||
|
|
||||||
|
_offset += len;
|
||||||
|
|
||||||
|
return len + 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public string ReadString()
|
||||||
|
{
|
||||||
|
var len = ReadUShort();
|
||||||
|
var rawData = new byte[len];
|
||||||
|
|
||||||
|
Buffer.BlockCopy(_data, _offset, rawData, 0, len);
|
||||||
|
|
||||||
|
var str = Encoding.UTF8.GetString(rawData);
|
||||||
|
_offset += len;
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public byte[] ReadBinary(int len)
|
||||||
|
{
|
||||||
|
if (len >= _data.Length)
|
||||||
|
return Array.Empty<byte>();
|
||||||
|
|
||||||
|
var payload = new byte[len];
|
||||||
|
Buffer.BlockCopy(_data, _offset, payload, 0, len);
|
||||||
|
|
||||||
|
_offset += len;
|
||||||
|
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteBinary(byte[] payload)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(payload.Length);
|
||||||
|
|
||||||
|
Array.Copy(payload, 0, _data, _offset, payload.Length);
|
||||||
|
|
||||||
|
_offset += payload.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteOperation(RagonOperation operation)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(1);
|
||||||
|
|
||||||
|
_data[_offset] = (byte)operation;
|
||||||
|
_offset += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public RagonOperation ReadOperation()
|
||||||
|
{
|
||||||
|
var op = (RagonOperation)_data[_offset];
|
||||||
|
_offset += 1;
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteUShort(ushort value)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(2);
|
||||||
|
|
||||||
|
_data[_offset] = (byte)(value & 0x00FF);
|
||||||
|
_data[_offset + 1] = (byte)((value & 0xFF00) >> 8);
|
||||||
|
_offset += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteUShort(ushort value, int offset)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(2);
|
||||||
|
_data[offset] = (byte)(value & 0x00FF);
|
||||||
|
_data[offset + 1] = (byte)((value & 0xFF00) >> 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public ushort ReadUShort()
|
||||||
|
{
|
||||||
|
var value = (ushort)(_data[_offset] + (_data[_offset + 1] << 8));
|
||||||
|
_offset += 2;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
_offset = 0;
|
||||||
|
_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void FromArray(byte[] data)
|
||||||
|
{
|
||||||
|
Clear();
|
||||||
|
ResizeIfNeed(data.Length);
|
||||||
|
Buffer.BlockCopy(data, 0, _data, 0, data.Length);
|
||||||
|
_size = data.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public byte[] ToArray()
|
||||||
|
{
|
||||||
|
var bytes = new byte[_offset];
|
||||||
|
Buffer.BlockCopy(_data, 0, bytes, 0, _offset);
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ResizeIfNeed(int lenght)
|
||||||
|
{
|
||||||
|
if (_offset + lenght < _data.Length)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var newData = new byte[_data.Length * 4 + lenght];
|
||||||
|
Buffer.BlockCopy(_data, 0, newData, 0, _data.Length);
|
||||||
|
_data = newData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<RootNamespace>Ragon.Relay</RootNamespace>
|
<RootNamespace>Ragon.Relay</RootNamespace>
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
"limitPlayersPerRoom": 20,
|
"limitPlayersPerRoom": 20,
|
||||||
"limitRooms": 200,
|
"limitRooms": 200,
|
||||||
"limitBufferedEvents": 50,
|
"limitBufferedEvents": 50,
|
||||||
|
"limitUserData": 1024,
|
||||||
"webHooks":
|
"webHooks":
|
||||||
{
|
{
|
||||||
"room-created": "http://127.0.0.1:3000/service/create-room",
|
"room-created": "http://127.0.0.1:3000/service/create-room",
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Data;
|
||||||
|
|
||||||
|
public class RagonData
|
||||||
|
{
|
||||||
|
private byte[] _data;
|
||||||
|
public bool IsDirty { get; set; }
|
||||||
|
public byte[] Data
|
||||||
|
{
|
||||||
|
get => _data;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_data = value;
|
||||||
|
IsDirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public RagonData(byte[] data)
|
||||||
|
{
|
||||||
|
_data = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,16 +29,19 @@ public sealed class AuthorizationOperation: BaseOperation
|
|||||||
private readonly RagonWebHookPlugin _webhook;
|
private readonly RagonWebHookPlugin _webhook;
|
||||||
private readonly RagonContextObserver _observer;
|
private readonly RagonContextObserver _observer;
|
||||||
private readonly RagonBuffer _writer;
|
private readonly RagonBuffer _writer;
|
||||||
|
private readonly RagonServerConfiguration _configuration;
|
||||||
|
|
||||||
public AuthorizationOperation(
|
public AuthorizationOperation(
|
||||||
RagonBuffer reader,
|
RagonBuffer reader,
|
||||||
RagonBuffer writer,
|
RagonBuffer writer,
|
||||||
RagonWebHookPlugin webhook,
|
RagonWebHookPlugin webhook,
|
||||||
RagonContextObserver observer): base(reader, writer)
|
RagonContextObserver observer,
|
||||||
|
RagonServerConfiguration configuration): base(reader, writer)
|
||||||
{
|
{
|
||||||
_webhook = webhook;
|
_webhook = webhook;
|
||||||
_observer = observer;
|
_observer = observer;
|
||||||
_writer = writer;
|
_writer = writer;
|
||||||
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Handle(RagonContext context, NetworkChannel channel)
|
public override void Handle(RagonContext context, NetworkChannel channel)
|
||||||
@@ -55,7 +58,7 @@ public sealed class AuthorizationOperation: BaseOperation
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var configuration = context.Configuration;
|
var configuration = _configuration;
|
||||||
var key = Reader.ReadString();
|
var key = Reader.ReadString();
|
||||||
var name = Reader.ReadString();
|
var name = Reader.ReadString();
|
||||||
var payload = Reader.ReadString();
|
var payload = Reader.ReadString();
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ public sealed class EntityCreateOperation : BaseOperation
|
|||||||
Authority = eventAuthority,
|
Authority = eventAuthority,
|
||||||
AttachId = attachId,
|
AttachId = attachId,
|
||||||
StaticId = 0,
|
StaticId = 0,
|
||||||
BufferedEvents = context.Configuration.LimitBufferedEvents,
|
BufferedEvents = context.LimitBufferedEvents,
|
||||||
};
|
};
|
||||||
|
|
||||||
var entity = new RagonEntity(entityParameters);
|
var entity = new RagonEntity(entityParameters);
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
using NLog;
|
||||||
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Handler
|
||||||
|
{
|
||||||
|
public class PlayerUserDataOperation : BaseOperation
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
private readonly int _userDataLimit;
|
||||||
|
|
||||||
|
public PlayerUserDataOperation(
|
||||||
|
RagonBuffer reader,
|
||||||
|
RagonBuffer writer,
|
||||||
|
int userDataLimit
|
||||||
|
) : base(reader, writer)
|
||||||
|
{
|
||||||
|
_userDataLimit = userDataLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Handle(RagonContext context, NetworkChannel channel)
|
||||||
|
{
|
||||||
|
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
|
||||||
|
{
|
||||||
|
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var playerUserData = Reader.ReadBytes(Reader.Capacity);
|
||||||
|
if (playerUserData.Length > _userDataLimit)
|
||||||
|
{
|
||||||
|
_logger.Warn($"Player {context.Connection.Id} exceeded user data limit");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
context.UserData.Data = playerUserData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,7 +26,7 @@ namespace Ragon.Server.Handler;
|
|||||||
|
|
||||||
public sealed class RoomCreateOperation : BaseOperation
|
public sealed class RoomCreateOperation : BaseOperation
|
||||||
{
|
{
|
||||||
private readonly RagonRoomParameters _roomParameters = new();
|
private readonly RagonRoomPayload _roomPayload = new();
|
||||||
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
private readonly IServerPlugin _serverPlugin;
|
private readonly IServerPlugin _serverPlugin;
|
||||||
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
|
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
|
||||||
@@ -65,13 +65,13 @@ public sealed class RoomCreateOperation : BaseOperation
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_roomParameters.Deserialize(Reader);
|
_roomPayload.Deserialize(Reader);
|
||||||
|
|
||||||
var information = new RoomInformation()
|
var information = new RoomInformation()
|
||||||
{
|
{
|
||||||
Scene = _roomParameters.Scene,
|
Scene = _roomPayload.Scene,
|
||||||
Max = _roomParameters.Max,
|
Max = _roomPayload.Max,
|
||||||
Min = _roomParameters.Min,
|
Min = _roomPayload.Min,
|
||||||
};
|
};
|
||||||
|
|
||||||
var lobbyPlayer = context.LobbyPlayer;
|
var lobbyPlayer = context.LobbyPlayer;
|
||||||
@@ -80,6 +80,9 @@ public sealed class RoomCreateOperation : BaseOperation
|
|||||||
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
|
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
|
||||||
var room = new RagonRoom(roomId, information, roomPlugin);
|
var room = new RagonRoom(roomId, information, roomPlugin);
|
||||||
|
|
||||||
|
if (!roomPlugin.OnPlayerJoined(roomPlayer))
|
||||||
|
return;
|
||||||
|
|
||||||
roomPlayer.OnAttached(room);
|
roomPlayer.OnAttached(room);
|
||||||
|
|
||||||
context.Scheduler.Run(room);
|
context.Scheduler.Run(room);
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ namespace Ragon.Server.Handler;
|
|||||||
|
|
||||||
public sealed class RoomJoinOrCreateOperation : BaseOperation
|
public sealed class RoomJoinOrCreateOperation : BaseOperation
|
||||||
{
|
{
|
||||||
private readonly RagonRoomParameters _roomParameters = new();
|
private readonly RagonRoomPayload _roomPayload = new();
|
||||||
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
private readonly IServerPlugin _serverPlugin;
|
private readonly IServerPlugin _serverPlugin;
|
||||||
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
|
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
|
||||||
@@ -48,11 +48,15 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
|
|||||||
var roomId = Guid.NewGuid().ToString();
|
var roomId = Guid.NewGuid().ToString();
|
||||||
var lobbyPlayer = context.LobbyPlayer;
|
var lobbyPlayer = context.LobbyPlayer;
|
||||||
|
|
||||||
_roomParameters.Deserialize(Reader);
|
_roomPayload.Deserialize(Reader);
|
||||||
|
|
||||||
if (context.Lobby.FindRoomByScene(_roomParameters.Scene, out var existsRoom))
|
if (context.Lobby.FindRoomByScene(_roomPayload.Scene, out var existsRoom))
|
||||||
{
|
{
|
||||||
var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||||
|
|
||||||
|
if (!existsRoom.Plugin.OnPlayerJoined(player))
|
||||||
|
return;
|
||||||
|
|
||||||
context.SetRoom(existsRoom, player);
|
context.SetRoom(existsRoom, player);
|
||||||
|
|
||||||
_ragonWebHookPlugin.RoomJoined(context, existsRoom, player);
|
_ragonWebHookPlugin.RoomJoined(context, existsRoom, player);
|
||||||
@@ -63,15 +67,18 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
|
|||||||
{
|
{
|
||||||
var information = new RoomInformation()
|
var information = new RoomInformation()
|
||||||
{
|
{
|
||||||
Scene = _roomParameters.Scene,
|
Scene = _roomPayload.Scene,
|
||||||
Max = _roomParameters.Max,
|
Max = _roomPayload.Max,
|
||||||
Min = _roomParameters.Min,
|
Min = _roomPayload.Min,
|
||||||
};
|
};
|
||||||
|
|
||||||
var roomPlayer = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
var roomPlayer = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||||
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
|
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
|
||||||
var room = new RagonRoom(roomId, information, roomPlugin);
|
var room = new RagonRoom(roomId, information, roomPlugin);
|
||||||
|
|
||||||
|
if (!roomPlugin.OnPlayerJoined(roomPlayer))
|
||||||
|
return;
|
||||||
|
|
||||||
_ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
|
_ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
|
||||||
|
|
||||||
context.Lobby.Persist(room);
|
context.Lobby.Persist(room);
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using NLog;
|
||||||
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
|
public sealed class RoomUserDataOperation : BaseOperation
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
private readonly int _userDataLimit;
|
||||||
|
|
||||||
|
public RoomUserDataOperation(
|
||||||
|
RagonBuffer reader,
|
||||||
|
RagonBuffer writer,
|
||||||
|
int userDataLimit
|
||||||
|
) : base(reader, writer)
|
||||||
|
{
|
||||||
|
_userDataLimit = userDataLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Handle(RagonContext context, NetworkChannel channel)
|
||||||
|
{
|
||||||
|
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
|
||||||
|
{
|
||||||
|
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var roomUserData = Reader.ReadBytes(Reader.Capacity);
|
||||||
|
if (roomUserData.Length > _userDataLimit)
|
||||||
|
{
|
||||||
|
_logger.Warn("Room user data is too big");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var room = context.Room;
|
||||||
|
if (room != null)
|
||||||
|
room.UserData.Data = roomUserData;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -62,7 +62,7 @@ public sealed class SceneLoadedOperation : BaseOperation
|
|||||||
Authority = eventAuthority,
|
Authority = eventAuthority,
|
||||||
AttachId = 0,
|
AttachId = 0,
|
||||||
StaticId = staticId,
|
StaticId = staticId,
|
||||||
BufferedEvents = context.Configuration.LimitBufferedEvents,
|
BufferedEvents = context.LimitBufferedEvents,
|
||||||
};
|
};
|
||||||
|
|
||||||
var entity = new RagonEntity(entityParameters);
|
var entity = new RagonEntity(entityParameters);
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ namespace Ragon.Server.Lobby;
|
|||||||
|
|
||||||
public interface IRagonLobby
|
public interface IRagonLobby
|
||||||
{
|
{
|
||||||
|
public IReadOnlyList<IRagonRoom> Rooms { get; }
|
||||||
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room);
|
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room);
|
||||||
public bool FindRoomByScene(string sceneName, [MaybeNullWhen(false)] out RagonRoom room);
|
public bool FindRoomByScene(string sceneName, [MaybeNullWhen(false)] out RagonRoom room);
|
||||||
public void Persist(RagonRoom room);
|
public void Persist(RagonRoom room);
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Lobby;
|
||||||
|
|
||||||
|
public class RagonLobbyDispatcher
|
||||||
|
{
|
||||||
|
private IRagonLobby _lobby;
|
||||||
|
|
||||||
|
public RagonLobbyDispatcher(IRagonLobby lobby)
|
||||||
|
{
|
||||||
|
_lobby = lobby;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write(RagonBuffer writer)
|
||||||
|
{
|
||||||
|
writer.Clear();
|
||||||
|
writer.WriteOperation(RagonOperation.ROOM_LIST_UPDATED);
|
||||||
|
var rooms = _lobby.Rooms;
|
||||||
|
|
||||||
|
writer.WriteUShort((ushort)rooms.Count);
|
||||||
|
for (int i = 0; i < rooms.Count; i++)
|
||||||
|
{
|
||||||
|
var room = rooms[i];
|
||||||
|
|
||||||
|
writer.WriteString(room.Id);
|
||||||
|
writer.WriteString(room.Scene);
|
||||||
|
writer.WriteUShort((ushort)room.PlayerMax);
|
||||||
|
writer.WriteUShort((ushort)room.PlayerMin);
|
||||||
|
writer.WriteUShort((ushort)room.PlayerCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,6 +25,8 @@ public class LobbyInMemory : IRagonLobby
|
|||||||
private readonly List<RagonRoom> _rooms = new();
|
private readonly List<RagonRoom> _rooms = new();
|
||||||
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
public IReadOnlyList<IRagonRoom> Rooms => _rooms.AsReadOnly();
|
||||||
|
|
||||||
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room)
|
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room)
|
||||||
{
|
{
|
||||||
foreach (var existRagonRoom in _rooms)
|
foreach (var existRagonRoom in _rooms)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using Ragon.Server.Data;
|
||||||
using Ragon.Server.IO;
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
namespace Ragon.Server.Lobby;
|
namespace Ragon.Server.Lobby;
|
||||||
@@ -36,7 +37,7 @@ public class RagonLobbyPlayer
|
|||||||
{
|
{
|
||||||
Id = id;
|
Id = id;
|
||||||
Name = name;
|
Name = name;
|
||||||
Payload = payload;
|
|
||||||
Connection = connection;
|
Connection = connection;
|
||||||
|
Payload = payload;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,8 +2,9 @@ namespace Ragon.Server.Logging;
|
|||||||
|
|
||||||
public interface IRagonLogger
|
public interface IRagonLogger
|
||||||
{
|
{
|
||||||
public void Warning(string tag, string message);
|
public void Warning(string message);
|
||||||
public void Info(string tag, string message);
|
public void Info(string message);
|
||||||
public void Error(string tag, string message);
|
public void Error(string message);
|
||||||
public void Trace(string tag, string message);
|
public void Error(Exception ex);
|
||||||
|
public void Trace(string message);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Ragon.Server.Logging;
|
||||||
|
|
||||||
|
public interface IRagonLoggerFactory
|
||||||
|
{
|
||||||
|
public IRagonLogger GetLogger(string tag);
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
namespace Ragon.Server.Logging
|
||||||
|
{
|
||||||
|
public class LoggerManager
|
||||||
|
{
|
||||||
|
private static IRagonLoggerFactory _factory = null!;
|
||||||
|
|
||||||
|
public static void SetLoggerFactory(IRagonLoggerFactory loggerFactory)
|
||||||
|
{
|
||||||
|
_factory = loggerFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IRagonLogger GetLogger(string tag)
|
||||||
|
{
|
||||||
|
return _factory.GetLogger(tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
|
using System.Text;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
using Ragon.Server.Handler;
|
using Ragon.Server.Handler;
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using Ragon.Server.Data;
|
||||||
using Ragon.Server.IO;
|
using Ragon.Server.IO;
|
||||||
using Ragon.Server.Lobby;
|
using Ragon.Server.Lobby;
|
||||||
using Ragon.Server.Time;
|
using Ragon.Server.Time;
|
||||||
@@ -26,43 +27,42 @@ public class RagonContext
|
|||||||
public ConnectionStatus ConnectionStatus { get; set; }
|
public ConnectionStatus ConnectionStatus { get; set; }
|
||||||
public INetworkConnection Connection { get; }
|
public INetworkConnection Connection { get; }
|
||||||
public IExecutor Executor { get; private set; }
|
public IExecutor Executor { get; private set; }
|
||||||
public RagonServerConfiguration Configuration { get; private set; }
|
public int LimitBufferedEvents { get; private set; }
|
||||||
public IRagonLobby Lobby { get; private set; }
|
public IRagonLobby Lobby { get; private set; }
|
||||||
public RagonLobbyPlayer? LobbyPlayer { get; private set; }
|
public RagonLobbyPlayer? LobbyPlayer { get; private set; }
|
||||||
|
|
||||||
public RagonRoom? Room { get; private set; }
|
public RagonRoom? Room { get; private set; }
|
||||||
public RagonRoomPlayer? RoomPlayer { get; private set; }
|
public RagonRoomPlayer? RoomPlayer { get; private set; }
|
||||||
|
public RagonData UserData { get; private set; }
|
||||||
public RagonScheduler Scheduler { get; private set; }
|
public RagonScheduler Scheduler { get; private set; }
|
||||||
|
|
||||||
public RagonContext(
|
public RagonContext(
|
||||||
INetworkConnection connection,
|
INetworkConnection connection,
|
||||||
RagonServerConfiguration configuration,
|
IExecutor executor,
|
||||||
IExecutor executor,
|
IRagonLobby lobby,
|
||||||
IRagonLobby lobby,
|
RagonScheduler scheduler,
|
||||||
RagonScheduler scheduler)
|
int limitBufferedEvents)
|
||||||
{
|
{
|
||||||
ConnectionStatus = ConnectionStatus.Unauthorized;
|
ConnectionStatus = ConnectionStatus.Unauthorized;
|
||||||
Configuration = configuration;
|
LimitBufferedEvents = limitBufferedEvents;
|
||||||
Connection = connection;
|
Connection = connection;
|
||||||
Executor = executor;
|
Executor = executor;
|
||||||
Lobby = lobby;
|
Lobby = lobby;
|
||||||
Scheduler = scheduler;
|
Scheduler = scheduler;
|
||||||
|
UserData = new RagonData(Array.Empty<byte>());
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SetPlayer(RagonLobbyPlayer player)
|
internal void SetPlayer(RagonLobbyPlayer player)
|
||||||
{
|
{
|
||||||
LobbyPlayer = player;
|
LobbyPlayer = player;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SetRoom(RagonRoom room, RagonRoomPlayer player)
|
internal void SetRoom(RagonRoom room, RagonRoomPlayer player)
|
||||||
{
|
{
|
||||||
Room?.DetachPlayer(RoomPlayer);
|
Room?.DetachPlayer(RoomPlayer);
|
||||||
|
|
||||||
Room = room;
|
Room = room;
|
||||||
RoomPlayer = player;
|
RoomPlayer = player;
|
||||||
|
|
||||||
Room.AttachPlayer(RoomPlayer);
|
Room.AttachPlayer(RoomPlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -45,6 +45,7 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
private readonly Dictionary<ushort, RagonContext> _contextsByConnection;
|
private readonly Dictionary<ushort, RagonContext> _contextsByConnection;
|
||||||
private readonly Dictionary<string, RagonContext> _contextsByPlayerId;
|
private readonly Dictionary<string, RagonContext> _contextsByPlayerId;
|
||||||
private readonly Stopwatch _timer;
|
private readonly Stopwatch _timer;
|
||||||
|
private readonly RagonLobbyDispatcher _lobbySerializer;
|
||||||
private readonly long _tickRate = 0;
|
private readonly long _tickRate = 0;
|
||||||
|
|
||||||
public RagonServer(
|
public RagonServer(
|
||||||
@@ -59,6 +60,7 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
_contextsByConnection = new Dictionary<ushort, RagonContext>();
|
_contextsByConnection = new Dictionary<ushort, RagonContext>();
|
||||||
_contextsByPlayerId = new Dictionary<string, RagonContext>();
|
_contextsByPlayerId = new Dictionary<string, RagonContext>();
|
||||||
_lobby = new LobbyInMemory();
|
_lobby = new LobbyInMemory();
|
||||||
|
_lobbySerializer = new RagonLobbyDispatcher(_lobby);
|
||||||
_scheduler = new RagonScheduler();
|
_scheduler = new RagonScheduler();
|
||||||
_webhooks = new RagonWebHookPlugin(this, configuration);
|
_webhooks = new RagonWebHookPlugin(this, configuration);
|
||||||
_dedicatedThread = new Thread(Execute);
|
_dedicatedThread = new Thread(Execute);
|
||||||
@@ -68,13 +70,17 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
_writer = new RagonBuffer();
|
_writer = new RagonBuffer();
|
||||||
_tickRate = 1000 / _configuration.ServerTickRate;
|
_tickRate = 1000 / _configuration.ServerTickRate;
|
||||||
_timer = new Stopwatch();
|
_timer = new Stopwatch();
|
||||||
|
|
||||||
var contextObserver = new RagonContextObserver(_contextsByPlayerId);
|
var contextObserver = new RagonContextObserver(_contextsByPlayerId);
|
||||||
|
|
||||||
|
_scheduler.Run(new RagonActionTimer(SendRoomList, 1.0f));
|
||||||
|
_scheduler.Run(new RagonActionTimer(SendPlayerUserData, 0.2f));
|
||||||
|
_scheduler.Run(new RagonActionTimer(SendRoomUserData, 0.2f));
|
||||||
|
|
||||||
_serverPlugin.OnAttached(this);
|
_serverPlugin.OnAttached(this);
|
||||||
|
|
||||||
_handlers = new BaseOperation[byte.MaxValue];
|
_handlers = new BaseOperation[byte.MaxValue];
|
||||||
_handlers[(byte)RagonOperation.AUTHORIZE] = new AuthorizationOperation(_reader, _writer, _webhooks, contextObserver);
|
_handlers[(byte)RagonOperation.AUTHORIZE] = new AuthorizationOperation(_reader, _writer, _webhooks, contextObserver, configuration);
|
||||||
_handlers[(byte)RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(_reader, _writer, plugin, _webhooks);
|
_handlers[(byte)RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(_reader, _writer, plugin, _webhooks);
|
||||||
_handlers[(byte)RagonOperation.CREATE_ROOM] = new RoomCreateOperation(_reader, _writer, plugin, _webhooks);
|
_handlers[(byte)RagonOperation.CREATE_ROOM] = new RoomCreateOperation(_reader, _writer, plugin, _webhooks);
|
||||||
_handlers[(byte)RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_reader, _writer, _webhooks);
|
_handlers[(byte)RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_reader, _writer, _webhooks);
|
||||||
@@ -90,6 +96,8 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampSyncOperation(_reader, _writer);
|
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampSyncOperation(_reader, _writer);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventOperation(_reader, _writer);
|
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventOperation(_reader, _writer);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataOperation(_reader, _writer);
|
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataOperation(_reader, _writer);
|
||||||
|
_handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataOperation(_reader, _writer, _configuration.LimitUserData);
|
||||||
|
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataOperation(_reader, _writer, _configuration.LimitUserData);
|
||||||
|
|
||||||
_logger.Trace($"Server Tick Rate: {_configuration.ServerTickRate}");
|
_logger.Trace($"Server Tick Rate: {_configuration.ServerTickRate}");
|
||||||
}
|
}
|
||||||
@@ -107,7 +115,7 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
if (_timer.ElapsedMilliseconds > _tickRate)
|
if (_timer.ElapsedMilliseconds > _tickRate)
|
||||||
{
|
{
|
||||||
_timer.Restart();
|
_timer.Restart();
|
||||||
_scheduler.Update(_timer.ElapsedMilliseconds / 1000.0f);
|
_scheduler.Update(_tickRate);
|
||||||
|
|
||||||
SendTimestamp();
|
SendTimestamp();
|
||||||
}
|
}
|
||||||
@@ -146,7 +154,7 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
|
|
||||||
public void OnConnected(INetworkConnection connection)
|
public void OnConnected(INetworkConnection connection)
|
||||||
{
|
{
|
||||||
var context = new RagonContext(connection, _configuration, _executor, _lobby, _scheduler);
|
var context = new RagonContext(connection, _executor, _lobby, _scheduler, _configuration.LimitBufferedEvents);
|
||||||
|
|
||||||
_logger.Trace($"Connected: {connection.Id}");
|
_logger.Trace($"Connected: {connection.Id}");
|
||||||
_contextsByConnection.Add(connection.Id, context);
|
_contextsByConnection.Add(connection.Id, context);
|
||||||
@@ -228,6 +236,55 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
_server.Broadcast(sendData, NetworkChannel.UNRELIABLE);
|
_server.Broadcast(sendData, NetworkChannel.UNRELIABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SendRoomList()
|
||||||
|
{
|
||||||
|
_lobbySerializer.Write(_writer);
|
||||||
|
|
||||||
|
var sendData = _writer.ToArray();
|
||||||
|
foreach (var (_, value) in _contextsByPlayerId)
|
||||||
|
{
|
||||||
|
if (value.Room == null) // If only in lobby, then send room list data
|
||||||
|
value.Connection.Reliable.Send(sendData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SendPlayerUserData()
|
||||||
|
{
|
||||||
|
foreach (var (_, value) in _contextsByPlayerId)
|
||||||
|
{
|
||||||
|
if (value.UserData.IsDirty)
|
||||||
|
{
|
||||||
|
_writer.Clear();
|
||||||
|
_writer.WriteOperation(RagonOperation.PLAYER_DATA_UPDATED);
|
||||||
|
_writer.WriteUShort(value.Connection.Id);
|
||||||
|
_writer.WriteBytes(value.UserData.Data);
|
||||||
|
|
||||||
|
var sendData = _writer.ToArray();
|
||||||
|
_server.Broadcast(sendData, NetworkChannel.RELIABLE);
|
||||||
|
|
||||||
|
value.UserData.IsDirty = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SendRoomUserData()
|
||||||
|
{
|
||||||
|
foreach (var room in _lobby.Rooms)
|
||||||
|
{
|
||||||
|
if (room.UserData.IsDirty)
|
||||||
|
{
|
||||||
|
_writer.Clear();
|
||||||
|
_writer.WriteOperation(RagonOperation.ROOM_DATA_UPDATED);
|
||||||
|
_writer.WriteBytes(room.UserData.Data);
|
||||||
|
|
||||||
|
var sendData = _writer.ToArray();
|
||||||
|
_server.Broadcast(sendData, NetworkChannel.RELIABLE);
|
||||||
|
|
||||||
|
room.UserData.IsDirty = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public BaseOperation ResolveHandler(RagonOperation operation)
|
public BaseOperation ResolveHandler(RagonOperation operation)
|
||||||
{
|
{
|
||||||
return _handlers[(byte)operation];
|
return _handlers[(byte)operation];
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ public struct RagonServerConfiguration
|
|||||||
public int LimitPlayersPerRoom;
|
public int LimitPlayersPerRoom;
|
||||||
public int LimitRooms;
|
public int LimitRooms;
|
||||||
public int LimitBufferedEvents;
|
public int LimitBufferedEvents;
|
||||||
|
public int LimitUserData;
|
||||||
public Dictionary<string, string> WebHooks;
|
public Dictionary<string, string> WebHooks;
|
||||||
|
|
||||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using Ragon.Server.Data;
|
||||||
using Ragon.Server.Entity;
|
using Ragon.Server.Entity;
|
||||||
using Ragon.Server.IO;
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
@@ -21,6 +22,13 @@ namespace Ragon.Server.Room;
|
|||||||
|
|
||||||
public interface IRagonRoom
|
public interface IRagonRoom
|
||||||
{
|
{
|
||||||
|
public string Id { get; }
|
||||||
|
public string Scene { get; }
|
||||||
|
public int PlayerMin { get; }
|
||||||
|
public int PlayerMax { get; }
|
||||||
|
public int PlayerCount { get; }
|
||||||
|
public RagonData UserData { get; }
|
||||||
|
|
||||||
RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection);
|
RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection);
|
||||||
RagonRoomPlayer GetPlayerById(string id);
|
RagonRoomPlayer GetPlayerById(string id);
|
||||||
IRagonEntity GetEntityById(ushort id);
|
IRagonEntity GetEntityById(ushort id);
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Data;
|
||||||
using Ragon.Server.Entity;
|
using Ragon.Server.Entity;
|
||||||
using Ragon.Server.IO;
|
using Ragon.Server.IO;
|
||||||
using Ragon.Server.Plugin;
|
using Ragon.Server.Plugin;
|
||||||
@@ -30,6 +31,7 @@ public class RagonRoom : IRagonRoom, IRagonAction
|
|||||||
public int PlayerMin { get; private set; }
|
public int PlayerMin { get; private set; }
|
||||||
public int PlayerCount => WaitPlayersList.Count;
|
public int PlayerCount => WaitPlayersList.Count;
|
||||||
|
|
||||||
|
public RagonData UserData { get; set; }
|
||||||
public RagonRoomPlayer Owner { get; private set; }
|
public RagonRoomPlayer Owner { get; private set; }
|
||||||
public RagonBuffer Writer { get; }
|
public RagonBuffer Writer { get; }
|
||||||
public IRoomPlugin Plugin { get; private set; }
|
public IRoomPlugin Plugin { get; private set; }
|
||||||
@@ -66,6 +68,7 @@ public class RagonRoom : IRagonRoom, IRagonAction
|
|||||||
|
|
||||||
_entitiesDirtySet = new HashSet<RagonEntity>();
|
_entitiesDirtySet = new HashSet<RagonEntity>();
|
||||||
|
|
||||||
|
UserData = new RagonData(Array.Empty<byte>());
|
||||||
Writer = new RagonBuffer();
|
Writer = new RagonBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using Ragon.Server.Data;
|
||||||
using Ragon.Server.Entity;
|
using Ragon.Server.Entity;
|
||||||
using Ragon.Server.IO;
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using Ragon.Server.Time;
|
||||||
|
|
||||||
|
public class RagonActionTimer: IRagonAction
|
||||||
|
{
|
||||||
|
private Action _callback;
|
||||||
|
private float _timer;
|
||||||
|
private float _time;
|
||||||
|
|
||||||
|
public RagonActionTimer(Action callback, float timeInSeconds)
|
||||||
|
{
|
||||||
|
_callback = callback;
|
||||||
|
_time = timeInSeconds * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Tick(float dt)
|
||||||
|
{
|
||||||
|
_timer += dt;
|
||||||
|
if (_timer >= _time)
|
||||||
|
{
|
||||||
|
_callback?.Invoke();
|
||||||
|
_timer = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user