Files
Ragon/Ragon.Client/Sources/Handler/SnapshotHandler.cs
T

118 lines
3.5 KiB
C#
Raw Normal View History

2023-03-06 10:06:43 +04:00
/*
2024-05-19 12:26:42 +03:00
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
2023-03-06 10:06:43 +04:00
*
* 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.
*/
2023-06-27 23:41:30 +03:00
using System.Diagnostics;
2023-03-06 10:06:43 +04:00
using Ragon.Protocol;
namespace Ragon.Client;
2023-10-04 14:42:59 +03:00
internal class SnapshotHandler : IHandler
2023-03-06 10:06:43 +04:00
{
2023-07-29 10:58:06 +03:00
private readonly IRagonEntityListener _entityListener;
private readonly RagonClient _client;
private readonly RagonListenerList _listenerList;
private readonly RagonEntityCache _entityCache;
private readonly RagonPlayerCache _playerCache;
2023-03-06 10:06:43 +04:00
public SnapshotHandler(
RagonClient ragonClient,
RagonListenerList listenerList,
RagonEntityCache entityCache,
2023-07-29 10:58:06 +03:00
RagonPlayerCache playerCache,
2023-07-30 16:56:11 +03:00
IRagonEntityListener entityListener
2023-03-06 10:06:43 +04:00
)
{
_client = ragonClient;
2023-07-29 10:58:06 +03:00
_entityListener = entityListener;
2023-03-06 10:06:43 +04:00
_listenerList = listenerList;
_entityCache = entityCache;
_playerCache = playerCache;
}
public void Handle(RagonBuffer buffer)
{
2024-01-13 15:15:29 +03:00
var entities = new List<RagonEntity>();
2023-03-06 10:06:43 +04:00
var dynamicEntities = buffer.ReadUShort();
RagonLog.Trace("Dynamic Entities: " + dynamicEntities);
for (var i = 0; i < dynamicEntities; i++)
{
var entityType = buffer.ReadUShort();
var entityId = buffer.ReadUShort();
var ownerPeerId = buffer.ReadUShort();
var payloadSize = buffer.ReadUShort();
2023-07-29 10:58:06 +03:00
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
if (player == null)
{
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
2023-11-05 22:19:55 +03:00
_playerCache.Dump();
2023-07-29 10:58:06 +03:00
return;
}
2023-03-06 10:06:43 +04:00
2023-06-27 23:41:30 +03:00
var hasAuthority = _playerCache.Local.Id == player.Id;
2023-07-29 10:58:06 +03:00
var entity = _entityCache.TryGetEntity(0, entityType, 0, entityId, hasAuthority, out _);
var payload = RagonPayload.Empty;
2023-07-30 16:56:11 +03:00
if (payloadSize > 0)
{
payload = new RagonPayload(payloadSize);
2023-07-30 16:56:11 +03:00
payload.Read(buffer);
}
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
2023-07-29 10:58:06 +03:00
_entityListener.OnEntityCreated(entity);
2023-07-30 16:56:11 +03:00
entity.Read(buffer);
2024-01-13 15:15:29 +03:00
entities.Add(entity);
2023-03-06 10:06:43 +04:00
}
var staticEntities = buffer.ReadUShort();
RagonLog.Trace("Scene Entities: " + staticEntities);
for (var i = 0; i < staticEntities; i++)
{
var entityType = buffer.ReadUShort();
var entityId = buffer.ReadUShort();
var staticId = buffer.ReadUShort();
var ownerPeerId = buffer.ReadUShort();
var payloadSize = buffer.ReadUShort();
2023-07-30 16:56:11 +03:00
2023-07-29 10:58:06 +03:00
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
if (player == null)
{
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
2023-11-05 22:19:55 +03:00
_playerCache.Dump();
2023-07-29 10:58:06 +03:00
return;
}
2023-07-30 16:56:11 +03:00
2023-06-27 23:41:30 +03:00
var hasAuthority = _playerCache.Local.Id == player.Id;
2023-07-29 10:58:06 +03:00
var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _);
entity.Prepare(_client, entityId, entityType, hasAuthority, player, RagonPayload.Empty);
2023-07-30 16:56:11 +03:00
entity.Read(buffer);
2023-07-29 10:58:06 +03:00
entities.Add(entity);
2023-07-30 21:46:42 +03:00
}
2024-01-13 15:15:29 +03:00
foreach (var entity in entities)
entity.Attach();
2023-07-30 21:14:14 +03:00
_listenerList.OnSceneLoaded();
2023-03-06 10:06:43 +04:00
}
}