208 lines
5.1 KiB
C#
208 lines
5.1 KiB
C#
using StressLevelZero;
|
|
using StressLevelZero.Combat;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BoneSync.Networking
|
|
{
|
|
internal class ByteEncoder
|
|
{
|
|
public List<byte> Data;
|
|
public ByteEncoder()
|
|
{
|
|
Data = new List<byte>();
|
|
}
|
|
|
|
public ByteEncoder(byte[] data)
|
|
{
|
|
Data = data.ToList();
|
|
}
|
|
|
|
public byte[] ToArray()
|
|
{
|
|
return Data.ToArray();
|
|
}
|
|
|
|
public void WriteByte(byte value)
|
|
{
|
|
Data.Add(value);
|
|
}
|
|
|
|
public void WriteBytes(byte[] value)
|
|
{
|
|
Data.AddRange(value);
|
|
}
|
|
public byte[] ReadBytes(int count)
|
|
{
|
|
if (Data.Count < count)
|
|
{
|
|
throw new Exception("Not enough data to read, expected " + count + " but only have " + Data.Count);
|
|
}
|
|
byte[] value = Data.GetRange(0, count).ToArray();
|
|
Data.RemoveRange(0, count);
|
|
return value;
|
|
}
|
|
|
|
public byte ReadByte()
|
|
{
|
|
byte value = Data[0];
|
|
Data.RemoveAt(0);
|
|
return value;
|
|
}
|
|
|
|
public void WriteBool(bool value)
|
|
{
|
|
WriteByte((byte)(value ? 1 : 0));
|
|
}
|
|
|
|
public bool ReadBool()
|
|
{
|
|
return ReadByte() == 1;
|
|
}
|
|
|
|
public void WriteInt(int value)
|
|
{
|
|
WriteBytes(BitConverter.GetBytes(value));
|
|
}
|
|
|
|
public int ReadInt()
|
|
{
|
|
return BitConverter.ToInt32(ReadBytes(sizeof(int)), 0);
|
|
}
|
|
|
|
public void WriteLong(long value)
|
|
{
|
|
WriteBytes(BitConverter.GetBytes(value));
|
|
}
|
|
|
|
public long ReadLong()
|
|
{
|
|
return BitConverter.ToInt64(ReadBytes(sizeof(long)), 0);
|
|
}
|
|
|
|
public void WriteFloat(float value)
|
|
{
|
|
WriteBytes(BitConverter.GetBytes(value));
|
|
}
|
|
|
|
public float ReadFloat()
|
|
{
|
|
return BitConverter.ToSingle(ReadBytes(sizeof(float)), 0);
|
|
}
|
|
|
|
public void WriteUShort(ushort value)
|
|
{
|
|
WriteBytes(BitConverter.GetBytes(value));
|
|
}
|
|
public ushort ReadUShort() {
|
|
return BitConverter.ToUInt16(ReadBytes(sizeof(ushort)), 0);
|
|
}
|
|
|
|
public void WriteDouble(double value)
|
|
{
|
|
WriteBytes(BitConverter.GetBytes(value));
|
|
}
|
|
|
|
public double ReadDouble()
|
|
{
|
|
return BitConverter.ToDouble(ReadBytes(sizeof(double)), 0);
|
|
}
|
|
|
|
public void WriteString(string value)
|
|
{
|
|
byte[] bytes = Encoding.UTF8.GetBytes(value);
|
|
WriteInt(bytes.Length);
|
|
WriteBytes(bytes);
|
|
}
|
|
|
|
public string ReadString()
|
|
{
|
|
int length = ReadInt();
|
|
return Encoding.UTF8.GetString(ReadBytes(length));
|
|
}
|
|
|
|
public void WriteUlong(ulong value)
|
|
{
|
|
WriteBytes(BitConverter.GetBytes(value));
|
|
}
|
|
|
|
public ulong ReadUlong()
|
|
{
|
|
return BitConverter.ToUInt64(ReadBytes(sizeof(ulong)), 0);
|
|
}
|
|
|
|
public void WriteVector3(UnityEngine.Vector3 value)
|
|
{
|
|
WriteFloat(value.x);
|
|
WriteFloat(value.y);
|
|
WriteFloat(value.z);
|
|
}
|
|
|
|
public UnityEngine.Vector3 ReadVector3()
|
|
{
|
|
float x = ReadFloat();
|
|
float y = ReadFloat();
|
|
float z = ReadFloat();
|
|
return new UnityEngine.Vector3(x, y, z);
|
|
}
|
|
|
|
public void WriteQuaternion(UnityEngine.Quaternion value)
|
|
{
|
|
WriteVector3(value.eulerAngles);
|
|
}
|
|
|
|
public UnityEngine.Quaternion ReadQuaternion()
|
|
{
|
|
UnityEngine.Vector3 eulerAngles = ReadVector3();
|
|
return UnityEngine.Quaternion.Euler(eulerAngles);
|
|
}
|
|
|
|
public void WriteSimpleTransform(SimpleTransform value)
|
|
{
|
|
WriteVector3(value.position);
|
|
WriteQuaternion(value.rotation);
|
|
WriteVector3(value.scale);
|
|
}
|
|
|
|
public SimpleTransform ReadSimpleTransform()
|
|
{
|
|
UnityEngine.Vector3 position = ReadVector3();
|
|
UnityEngine.Quaternion rotation = ReadQuaternion();
|
|
UnityEngine.Vector3 scale = ReadVector3();
|
|
return new SimpleTransform()
|
|
{
|
|
position = position,
|
|
rotation = rotation,
|
|
scale = scale
|
|
};
|
|
}
|
|
|
|
public void WriteSLZAttack(Attack attack)
|
|
{
|
|
WriteByte((byte)attack.attackType);
|
|
WriteFloat(attack.damage);
|
|
}
|
|
|
|
public void WriteMatrix4x4(UnityEngine.Matrix4x4 matrix)
|
|
{
|
|
for (int i = 0; i < 16; i++)
|
|
{
|
|
WriteFloat(matrix[i]);
|
|
}
|
|
}
|
|
|
|
public UnityEngine.Matrix4x4 ReadMatrix4x4()
|
|
{
|
|
UnityEngine.Matrix4x4 matrix = new UnityEngine.Matrix4x4();
|
|
for (int i = 0; i < 16; i++)
|
|
{
|
|
matrix[i] = ReadFloat();
|
|
}
|
|
return matrix;
|
|
}
|
|
}
|
|
}
|