refactoring: heap -> span
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
using System;
|
||||
using NetStack.Serialization;
|
||||
|
||||
namespace Ragon.Common
|
||||
{
|
||||
public static class BitBufferExtension
|
||||
{
|
||||
public static BitBuffer AddBytes(this BitBuffer buffer, byte[] data)
|
||||
{
|
||||
buffer.AddInt(data.Length);
|
||||
foreach (var b in data)
|
||||
buffer.AddByte(b);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public static byte[] ReadBytes(this BitBuffer buffer)
|
||||
{
|
||||
var size = buffer.ReadInt();
|
||||
var data = new byte[size];
|
||||
var i = 0;
|
||||
|
||||
while (i < size)
|
||||
data[i] = buffer.ReadByte();
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
using NetStack.Serialization;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
|
||||
public class AuthorationData : IData
|
||||
{
|
||||
public string Login { get; set; }
|
||||
public string Password { get; set; }
|
||||
|
||||
public void Serialize(BitBuffer buffer)
|
||||
{
|
||||
buffer.AddString(Login);
|
||||
buffer.AddString(Password);
|
||||
}
|
||||
|
||||
public void Deserialize(BitBuffer buffer)
|
||||
{
|
||||
Login = buffer.ReadString();
|
||||
Password = buffer.ReadString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
using NetStack.Serialization;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public class FindAndJoinData: IData
|
||||
{
|
||||
public string Map;
|
||||
|
||||
public void Serialize(BitBuffer buffer)
|
||||
{
|
||||
buffer.AddString(Map);
|
||||
}
|
||||
|
||||
public void Deserialize(BitBuffer buffer)
|
||||
{
|
||||
Map = buffer.ReadString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
using NetStack.Serialization;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public class SceneLoadData: IData
|
||||
{
|
||||
public string Scene;
|
||||
|
||||
public void Serialize(BitBuffer buffer)
|
||||
{
|
||||
buffer.AddString(Scene);
|
||||
}
|
||||
|
||||
public void Deserialize(BitBuffer buffer)
|
||||
{
|
||||
Scene = buffer.ReadString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
using NetStack.Serialization;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
|
||||
public class EntityData: IData
|
||||
{
|
||||
public int EntityId;
|
||||
public byte[] State;
|
||||
public void Serialize(BitBuffer buffer)
|
||||
{
|
||||
buffer.AddInt(EntityId);
|
||||
buffer.AddBytes(State);
|
||||
}
|
||||
|
||||
public void Deserialize(BitBuffer buffer)
|
||||
{
|
||||
EntityId = buffer.ReadInt();
|
||||
State = buffer.ReadBytes();
|
||||
}
|
||||
}
|
||||
public class SnapshotData: IData
|
||||
{
|
||||
public EntityData[] Entities;
|
||||
public void Serialize(BitBuffer buffer)
|
||||
{
|
||||
buffer.AddInt(Entities.Length);
|
||||
foreach (var entityData in Entities)
|
||||
entityData.Serialize(buffer);
|
||||
}
|
||||
public void Deserialize(BitBuffer buffer)
|
||||
{
|
||||
var entitiesSize = buffer.ReadInt();
|
||||
var i = 0;
|
||||
|
||||
Entities = new EntityData[entitiesSize];
|
||||
while (i < entitiesSize)
|
||||
{
|
||||
Entities[i] = new EntityData();
|
||||
Entities[i].Deserialize(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ using NetStack.Serialization;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public interface IData
|
||||
public interface IPacket
|
||||
{
|
||||
public void Serialize(BitBuffer buffer);
|
||||
public void Deserialize(BitBuffer buffer);
|
||||
@@ -11,9 +11,9 @@ namespace Ragon.Common.Protocol
|
||||
|
||||
LOAD_SCENE,
|
||||
SCENE_IS_LOADED,
|
||||
|
||||
PLAYER_CONNECTED,
|
||||
PLAYER_DISCONNECTED,
|
||||
|
||||
PLAYER_JOINED,
|
||||
PLAYER_LEAVED,
|
||||
|
||||
CREATE_ENTITY,
|
||||
DESTROY_ENTITY,
|
||||
@@ -24,6 +24,7 @@ namespace Ragon.Common.Protocol
|
||||
|
||||
REPLICATE_ENTITY_STATE,
|
||||
REPLICATE_ENTITY_PROPERTY,
|
||||
REPLICATE_ENTITY_EVENT,
|
||||
REPLICATE_EVENT,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using NetStack.Buffers;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public static class ProtocolHeader
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void WriteUShort(ushort id, ref Span<byte> data) {
|
||||
data[0] = (byte)(id & 0x00FF);
|
||||
data[1] = (byte)((id & 0xFF00) >> 8);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ushort ReadUShort(ref ReadOnlySpan<byte> data)
|
||||
{
|
||||
return (ushort)(data[0] + (data[1] << 8));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void WriteInt(int id, ref Span<byte> data) {
|
||||
data[0] = (byte)(id & 0x00FF);
|
||||
data[1] = (byte)((id & 0xFF00) >> 8);
|
||||
data[2] = (byte)((id & 0xFF00) >> 16);
|
||||
data[3] = (byte)((id & 0xFF00) >> 24);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int ReadInt(ref ReadOnlySpan<byte> data)
|
||||
{
|
||||
return (ushort)(data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using NetStack.Buffers;
|
||||
|
||||
namespace Ragon.Common.Protocol
|
||||
{
|
||||
public static class ProtocolHeader
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void WriteOperation(ushort id, byte[] data) {
|
||||
data[0] = (byte)(id & 0x00FF);
|
||||
data[1] = (byte)((id & 0xFF00) >> 8);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ushort ReadOperation(byte[] data, int offset = 0)
|
||||
{
|
||||
return (ushort)(data[offset] + (data[offset + 1] << 8));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void WriteEntity(int id, byte[] data, int offset = 2) {
|
||||
data[offset] = (byte)(id & 0x00FF);
|
||||
data[offset + 1] = (byte)((id & 0xFF00) >> 8);
|
||||
data[offset + 2] = (byte)((id & 0xFF00) >> 16);
|
||||
data[offset + 3] = (byte)((id & 0xFF00) >> 24);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int ReadEntity(byte[] data, int offset = 2)
|
||||
{
|
||||
return (ushort)(data[offset] + (data[offset + 1] << 8) + (data[offset + 2] << 16) + (data[offset + 3] << 24));
|
||||
}
|
||||
|
||||
public static int ReadProperty(byte[] data, int offset = 2)
|
||||
{
|
||||
return ReadEntity(data, offset);
|
||||
}
|
||||
|
||||
public static void WriteProperty(int id, byte[] data)
|
||||
{
|
||||
WriteEntity(id, data);
|
||||
}
|
||||
|
||||
public static int ReadEvent(byte[] data, int offset = 2)
|
||||
{
|
||||
return ReadEntity(data, offset);
|
||||
}
|
||||
|
||||
public static void WriteEvent(int id, byte[] data)
|
||||
{
|
||||
WriteEntity(id, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,10 +11,12 @@
|
||||
<OutputPath>/Users/edmand46/UnityProjects/ragon-client/Assets/RagonSDK/</OutputPath>
|
||||
<DebugSymbols>false</DebugSymbols>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<DefineConstants>TRACE;NETSTACK_SPAN</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<OutputPath>/Users/edmand46/UnityProjects/ragon-client/Assets/RagonSDK/</OutputPath>
|
||||
<DefineConstants>TRACE;NETSTACK_SPAN</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user