[+] 接入ET8服务端

[+] 接入ET8服务端
This commit is contained in:
ALEXTANG
2023-07-13 12:23:48 +08:00
parent e0be062006
commit 336d4b2eb9
1316 changed files with 130657 additions and 626 deletions

View File

@@ -0,0 +1,62 @@
using System;
using System.Buffers;
using System.IO;
namespace ET
{
public class MemoryBuffer: MemoryStream, IBufferWriter<byte>
{
private int origin;
public MemoryBuffer()
{
}
public MemoryBuffer(int capacity): base(capacity)
{
}
public MemoryBuffer(byte[] buffer): base(buffer)
{
}
public MemoryBuffer(byte[] buffer, int index, int length): base(buffer, index, length)
{
this.origin = index;
}
public ReadOnlyMemory<byte> WrittenMemory => this.GetBuffer().AsMemory(this.origin, (int)this.Position);
public ReadOnlySpan<byte> WrittenSpan => this.GetBuffer().AsSpan(this.origin, (int)this.Position);
public void Advance(int count)
{
long newLength = this.Position + count;
if (newLength > this.Length)
{
this.SetLength(newLength);
}
this.Position = newLength;
}
public Memory<byte> GetMemory(int sizeHint = 0)
{
if (this.Length - this.Position < sizeHint)
{
this.SetLength(this.Position + sizeHint);
}
var memory = this.GetBuffer().AsMemory((int)this.Position + this.origin, (int)(this.Length - this.Position));
return memory;
}
public Span<byte> GetSpan(int sizeHint = 0)
{
if (this.Length - this.Position < sizeHint)
{
this.SetLength(this.Position + sizeHint);
}
var span = this.GetBuffer().AsSpan((int)this.Position + this.origin, (int)(this.Length - this.Position));
return span;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9c85a4c0a92b422e9f5c81471a16e64a
timeCreated: 1682481633

View File

@@ -0,0 +1,59 @@
using System;
using System.ComponentModel;
using MemoryPack;
namespace ET
{
public static class MemoryPackHelper
{
public static byte[] Serialize(object message)
{
return MemoryPackSerializer.Serialize(message.GetType(), message);
}
public static void Serialize(object message, MemoryBuffer stream)
{
MemoryPackSerializer.Serialize(message.GetType(), stream, message);
}
public static object Deserialize(Type type, byte[] bytes, int index, int count)
{
object o = MemoryPackSerializer.Deserialize(type, bytes.AsSpan(index, count));
if (o is ISupportInitialize supportInitialize)
{
supportInitialize.EndInit();
}
return o;
}
public static object Deserialize(Type type, byte[] bytes, int index, int count, ref object o)
{
MemoryPackSerializer.Deserialize(type, bytes.AsSpan(index, count), ref o);
if (o is ISupportInitialize supportInitialize)
{
supportInitialize.EndInit();
}
return o;
}
public static object Deserialize(Type type, MemoryBuffer stream)
{
object o = MemoryPackSerializer.Deserialize(type, stream.GetSpan());
if (o is ISupportInitialize supportInitialize)
{
supportInitialize.EndInit();
}
return o;
}
public static object Deserialize(Type type, MemoryBuffer stream, ref object o)
{
MemoryPackSerializer.Deserialize(type, stream.GetSpan(), ref o);
if (o is ISupportInitialize supportInitialize)
{
supportInitialize.EndInit();
}
return o;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e48026652b4f4d09ae05581b76e2095b
timeCreated: 1682484738

View File

@@ -0,0 +1,250 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.Serializers;
using TrueSync;
using Unity.Mathematics;
namespace ET
{
public static class MongoHelper
{
private class StructBsonSerialize<TValue>: StructSerializerBase<TValue> where TValue : struct
{
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
{
Type nominalType = args.NominalType;
IBsonWriter bsonWriter = context.Writer;
bsonWriter.WriteStartDocument();
FieldInfo[] fields = nominalType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
foreach (FieldInfo field in fields)
{
bsonWriter.WriteName(field.Name);
BsonSerializer.Serialize(bsonWriter, field.FieldType, field.GetValue(value));
}
bsonWriter.WriteEndDocument();
}
public override TValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
//boxing is required for SetValue to work
object obj = new TValue();
Type actualType = args.NominalType;
IBsonReader bsonReader = context.Reader;
bsonReader.ReadStartDocument();
while (bsonReader.State != BsonReaderState.EndOfDocument)
{
switch (bsonReader.State)
{
case BsonReaderState.Name:
{
string name = bsonReader.ReadName(Utf8NameDecoder.Instance);
FieldInfo field = actualType.GetField(name);
if (field != null)
{
object value = BsonSerializer.Deserialize(bsonReader, field.FieldType);
field.SetValue(obj, value);
}
break;
}
case BsonReaderState.Type:
{
bsonReader.ReadBsonType();
break;
}
case BsonReaderState.Value:
{
bsonReader.SkipValue();
break;
}
}
}
bsonReader.ReadEndDocument();
return (TValue)obj;
}
}
[StaticField]
private static readonly JsonWriterSettings defaultSettings = new() { OutputMode = JsonOutputMode.RelaxedExtendedJson };
public static void Register()
{
// 自动注册IgnoreExtraElements
ConventionPack conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) };
ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);
RegisterStruct<float2>();
RegisterStruct<float3>();
RegisterStruct<float4>();
RegisterStruct<quaternion>();
RegisterStruct<FP>();
RegisterStruct<TSVector>();
RegisterStruct<TSVector2>();
RegisterStruct<TSVector4>();
RegisterStruct<TSQuaternion>();
Dictionary<string, Type> types = EventSystem.Instance.GetTypes();
foreach (Type type in types.Values)
{
if (!type.IsSubclassOf(typeof (Object)))
{
continue;
}
if (type.IsGenericType)
{
continue;
}
BsonClassMap.LookupClassMap(type);
}
}
public static void RegisterStruct<T>() where T : struct
{
BsonSerializer.RegisterSerializer(typeof (T), new StructBsonSerialize<T>());
}
public static string ToJson(object obj)
{
if (obj is ISupportInitialize supportInitialize)
{
supportInitialize.BeginInit();
}
return obj.ToJson(defaultSettings);
}
public static string ToJson(object obj, JsonWriterSettings settings)
{
if (obj is ISupportInitialize supportInitialize)
{
supportInitialize.BeginInit();
}
return obj.ToJson(settings);
}
public static T FromJson<T>(string str)
{
try
{
return BsonSerializer.Deserialize<T>(str);
}
catch (Exception e)
{
throw new Exception($"{str}\n{e}");
}
}
public static object FromJson(Type type, string str)
{
return BsonSerializer.Deserialize(str, type);
}
public static byte[] Serialize(object obj)
{
if (obj is ISupportInitialize supportInitialize)
{
supportInitialize.BeginInit();
}
return obj.ToBson();
}
public static void Serialize(object message, MemoryStream stream)
{
if (message is ISupportInitialize supportInitialize)
{
supportInitialize.BeginInit();
}
using (BsonBinaryWriter bsonWriter = new BsonBinaryWriter(stream, BsonBinaryWriterSettings.Defaults))
{
BsonSerializationContext context = BsonSerializationContext.CreateRoot(bsonWriter);
BsonSerializationArgs args = default;
args.NominalType = typeof (object);
IBsonSerializer serializer = BsonSerializer.LookupSerializer(args.NominalType);
serializer.Serialize(context, args, message);
}
}
public static object Deserialize(Type type, byte[] bytes)
{
try
{
return BsonSerializer.Deserialize(bytes, type);
}
catch (Exception e)
{
throw new Exception($"from bson error: {type.FullName} {bytes.Length}", e);
}
}
public static object Deserialize(Type type, byte[] bytes, int index, int count)
{
try
{
using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
{
return BsonSerializer.Deserialize(memoryStream, type);
}
}
catch (Exception e)
{
throw new Exception($"from bson error: {type.FullName} {bytes.Length} {index} {count}", e);
}
}
public static object Deserialize(Type type, Stream stream)
{
try
{
return BsonSerializer.Deserialize(stream, type);
}
catch (Exception e)
{
throw new Exception($"from bson error: {type.FullName} {stream.Position} {stream.Length}", e);
}
}
public static T Deserialize<T>(byte[] bytes)
{
try
{
using (MemoryStream memoryStream = new MemoryStream(bytes))
{
return (T)BsonSerializer.Deserialize(memoryStream, typeof (T));
}
}
catch (Exception e)
{
throw new Exception($"from bson error: {typeof (T).FullName} {bytes.Length}", e);
}
}
public static T Deserialize<T>(byte[] bytes, int index, int count)
{
return (T)Deserialize(typeof (T), bytes, index, count);
}
public static T Clone<T>(T t)
{
return Deserialize<T>(Serialize(t));
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ea73bf40fc38b474cabda2b211c90155
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: