Files
BoneSync/BoneSync/Networking/Messages/RegisterSyncableMessage.cs
2025-03-03 16:23:40 +02:00

86 lines
2.7 KiB
C#

using BoneSync.Data;
using BoneSync.Patching;
using BoneSync.Sync;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BoneSync.Networking.Messages
{
public enum RegisterSyncType
{
RegisterFromPath = 0,
RegisterAndSpawn = 1,
}
public struct SpawnPoolableInfo
{
public string spawnableTitle;
public SimpleSyncTransform spawnLocation;
}
public struct RegisterSyncableInfo
{
public string transformPath;
public ushort id;
public ushort callbackId;
public ulong ownerId;
public RegisterSyncType type;
public SpawnPoolableInfo? spawnInfo;
}
[PacketType(PacketType.RegisterSyncable), PacketReliability(PacketReliability.ReliableFast), PacketCatchup(0)]
internal class RegisterSyncableMessage : NetworkMessage
{
private RegisterSyncableInfo _info;
public RegisterSyncableInfo info => _info;
public RegisterSyncableMessage(RegisterSyncableInfo info)
{
_info = info;
byteEncoder.WriteByte((byte)_info.type);
byteEncoder.WriteULong(_info.ownerId);
byteEncoder.WriteUShort(_info.id);
byteEncoder.WriteUShort(_info.callbackId);
switch (_info.type)
{
case RegisterSyncType.RegisterAndSpawn:
byteEncoder.WriteString(_info.spawnInfo.Value.spawnableTitle);
byteEncoder.WriteSimpleTransform(_info.spawnInfo.Value.spawnLocation);
break;
case RegisterSyncType.RegisterFromPath:
byteEncoder.WriteString(_info.transformPath);
break;
}
}
public RegisterSyncableMessage(Packet packet)
{
byteEncoder.WriteBytes(packet.Data);
_info.type = (RegisterSyncType)byteEncoder.ReadByte();
_info.ownerId = byteEncoder.ReadULong();
_info.id = byteEncoder.ReadUShort();
_info.callbackId = byteEncoder.ReadUShort();
switch (_info.type)
{
case RegisterSyncType.RegisterAndSpawn:
_info.spawnInfo = new SpawnPoolableInfo()
{
spawnableTitle = byteEncoder.ReadString(),
spawnLocation = byteEncoder.ReadSimpleTransform(),
};
break;
case RegisterSyncType.RegisterFromPath:
_info.transformPath = byteEncoder.ReadString();
break;
}
}
public override void Execute()
{
ObjectSync.OnRegisterSyncMessage(this);
}
}
}