8
Assets/Scripts/NBC/Runtime/Core/Serialize/BsonPack.meta
Normal file
8
Assets/Scripts/NBC/Runtime/Core/Serialize/BsonPack.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a19189e3038447bb88d6d991d3f1228
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
namespace NBC.Serialize
|
||||
{
|
||||
public class BsonPackHelper : ISerialize
|
||||
{
|
||||
public string SerializeName { get; } = "Bson";
|
||||
public T Deserialize<T>(byte[] bytes)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T Deserialize<T>(MemoryStreamBuffer buffer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, byte[] bytes)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, MemoryStreamBuffer buffer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T Deserialize<T>(byte[] bytes, int index, int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, byte[] bytes, int index, int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Serialize<T>(T @object, IBufferWriter<byte> buffer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Serialize(object @object, IBufferWriter<byte> buffer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Serialize(Type type, object @object, IBufferWriter<byte> buffer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T Clone<T>(T t)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 564581ee1c8ee4e348fe55a0d02977ed
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/NBC/Runtime/Core/Serialize/Interface.meta
Normal file
8
Assets/Scripts/NBC/Runtime/Core/Serialize/Interface.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 67cee2b81543348e8b18e5425e0e4863
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using NBC.Pool;
|
||||
using Newtonsoft.Json;
|
||||
using ProtoBuf;
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
|
||||
namespace NBC.Serialize
|
||||
{
|
||||
public abstract class ASerialize : ISupportInitialize, IDisposable
|
||||
{
|
||||
public virtual void Dispose() { }
|
||||
public virtual void BeginInit() { }
|
||||
public virtual void EndInit() { }
|
||||
public virtual void AfterDeserialization() => EndInit();
|
||||
}
|
||||
|
||||
public abstract class AMessage : ASerialize, IPool
|
||||
{
|
||||
|
||||
[BsonIgnore]
|
||||
[JsonIgnore]
|
||||
[IgnoreDataMember]
|
||||
[ProtoIgnore]
|
||||
private Scene _scene;
|
||||
protected Scene GetScene()
|
||||
{
|
||||
return _scene;
|
||||
}
|
||||
|
||||
public void SetScene(Scene scene)
|
||||
{
|
||||
_scene = scene;
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
[IgnoreDataMember]
|
||||
[ProtoIgnore]
|
||||
private bool _isPool;
|
||||
|
||||
public bool IsPool()
|
||||
{
|
||||
return _isPool;
|
||||
}
|
||||
|
||||
public void SetIsPool(bool isPool)
|
||||
{
|
||||
_isPool = isPool;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c0d1f037ce81346e8a04a78f128b0e65
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
namespace NBC.Serialize
|
||||
{
|
||||
public interface ISerialize
|
||||
{
|
||||
/// <summary>
|
||||
/// 序列化器的名字,用于在协议里指定用什么协议序列化使用
|
||||
/// </summary>
|
||||
string SerializeName { get; }
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// </summary>
|
||||
/// <param name="bytes"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
T Deserialize<T>(byte[] bytes);
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// </summary>
|
||||
/// <param name="buffer"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
T Deserialize<T>(MemoryStreamBuffer buffer);
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="bytes"></param>
|
||||
/// <returns></returns>
|
||||
object Deserialize(Type type, byte[] bytes);
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="buffer"></param>
|
||||
/// <returns></returns>
|
||||
object Deserialize(Type type, MemoryStreamBuffer buffer);
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// </summary>
|
||||
/// <param name="bytes"></param>
|
||||
/// <param name="index"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
T Deserialize<T>(byte[] bytes, int index, int count);
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="bytes"></param>
|
||||
/// <param name="index"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
object Deserialize(Type type, byte[] bytes, int index, int count);
|
||||
/// <summary>
|
||||
/// 序列化
|
||||
/// </summary>
|
||||
/// <param name="object"></param>
|
||||
/// <param name="buffer"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
void Serialize<T>(T @object, IBufferWriter<byte> buffer);
|
||||
/// <summary>
|
||||
/// 序列化
|
||||
/// </summary>
|
||||
/// <param name="object"></param>
|
||||
/// <param name="buffer"></param>
|
||||
void Serialize(object @object, IBufferWriter<byte> buffer);
|
||||
/// <summary>
|
||||
/// 序列化
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="object"></param>
|
||||
/// <param name="buffer"></param>
|
||||
void Serialize(Type type, object @object, IBufferWriter<byte> buffer);
|
||||
/// <summary>
|
||||
/// 克隆
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
T Clone<T>(T t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a7d62fda7ac04f3fb7d1fd6955da409
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.IO;
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
namespace NBC.Serialize
|
||||
{
|
||||
public enum MemoryStreamBufferSource
|
||||
{
|
||||
None = 0,
|
||||
Pack = 1,
|
||||
UnPack = 2,
|
||||
}
|
||||
|
||||
public sealed class MemoryStreamBuffer : MemoryStream, IBufferWriter<byte>
|
||||
{
|
||||
public MemoryStreamBufferSource MemoryStreamBufferSource;
|
||||
public MemoryStreamBuffer() { }
|
||||
|
||||
public MemoryStreamBuffer(MemoryStreamBufferSource memoryStreamBufferSource, int capacity) : base(capacity)
|
||||
{
|
||||
MemoryStreamBufferSource = memoryStreamBufferSource;
|
||||
}
|
||||
public MemoryStreamBuffer(byte[] buffer): base(buffer) { }
|
||||
|
||||
public void Advance(int count)
|
||||
{
|
||||
if (count < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(count), count, "The value of 'count' cannot be negative.");
|
||||
}
|
||||
|
||||
var newLength = Position + count;
|
||||
|
||||
if (newLength != Length)
|
||||
{
|
||||
SetLength(newLength);
|
||||
}
|
||||
|
||||
Position = newLength;
|
||||
}
|
||||
|
||||
public Memory<byte> GetMemory(int sizeHint = 0)
|
||||
{
|
||||
if (sizeHint < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(sizeHint), sizeHint, "The value of 'count' cannot be negative.");
|
||||
}
|
||||
|
||||
if (Length - Position <= sizeHint)
|
||||
{
|
||||
SetLength(Position + sizeHint);
|
||||
}
|
||||
|
||||
return new Memory<byte>(GetBuffer(), (int)Position, (int)(Length - Position));
|
||||
}
|
||||
|
||||
public Span<byte> GetSpan(int sizeHint = 0)
|
||||
{
|
||||
if (sizeHint < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(sizeHint), sizeHint, "The value of 'count' cannot be negative.");
|
||||
}
|
||||
|
||||
if (Length - Position <= sizeHint)
|
||||
{
|
||||
SetLength(Position + sizeHint);
|
||||
}
|
||||
|
||||
return new Span<byte>(GetBuffer(), (int)Position, (int)(Length - Position));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c6e3b5d66fc8451482eaf828663e6a1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0f56f731b0e9a4bdb931dce6773bc1e1
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace NBC.Serialize
|
||||
{
|
||||
/// <summary>
|
||||
/// 代表是一个ProtoBuf协议
|
||||
/// </summary>
|
||||
public interface IProto
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d8890f4dc3e2c433fbd22b91ead3bfe4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,200 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.IO;
|
||||
using NBC.Assembly;
|
||||
using ProtoBuf;
|
||||
using ProtoBuf.Meta;
|
||||
|
||||
namespace NBC.Serialize
|
||||
{
|
||||
/// <summary>
|
||||
/// ProtoBufP帮助类,Unity平台使用
|
||||
/// </summary>
|
||||
public sealed class ProtoBufPackHelper : ISerialize
|
||||
{
|
||||
/// <summary>
|
||||
/// 序列化器的名字
|
||||
/// </summary>
|
||||
public string SerializeName { get; } = "ProtoBuf";
|
||||
|
||||
/// <summary>
|
||||
/// 使用ProtoBuf反序列化数据到实例
|
||||
/// </summary>
|
||||
/// <param name="bytes"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public unsafe T Deserialize<T>(byte[] bytes)
|
||||
{
|
||||
fixed (byte* ptr = bytes)
|
||||
{
|
||||
using var stream = new UnmanagedMemoryStream(ptr, bytes.Length);
|
||||
var @object = ProtoBuf.Serializer.Deserialize<T>(stream);
|
||||
if (@object is ASerialize aSerialize)
|
||||
{
|
||||
aSerialize.AfterDeserialization();
|
||||
}
|
||||
return @object;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用ProtoBuf反序列化数据到实例
|
||||
/// </summary>
|
||||
/// <param name="buffer"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public T Deserialize<T>(MemoryStreamBuffer buffer)
|
||||
{
|
||||
var @object = ProtoBuf.Serializer.Deserialize<T>(buffer);
|
||||
if (@object is ASerialize aSerialize)
|
||||
{
|
||||
aSerialize.AfterDeserialization();
|
||||
}
|
||||
return @object;
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用ProtoBuf反序列化数据到实例
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="bytes"></param>
|
||||
/// <returns></returns>
|
||||
public unsafe object Deserialize(Type type, byte[] bytes)
|
||||
{
|
||||
fixed (byte* ptr = bytes)
|
||||
{
|
||||
using var stream = new UnmanagedMemoryStream(ptr, bytes.Length);
|
||||
var @object = ProtoBuf.Serializer.Deserialize(type, stream);
|
||||
if (@object is ASerialize aSerialize)
|
||||
{
|
||||
aSerialize.AfterDeserialization();
|
||||
}
|
||||
|
||||
return @object;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用ProtoBuf反序列化数据到实例
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="buffer"></param>
|
||||
/// <returns></returns>
|
||||
public object Deserialize(Type type, MemoryStreamBuffer buffer)
|
||||
{
|
||||
var @object = ProtoBuf.Serializer.Deserialize(type, buffer);
|
||||
if (@object is ASerialize aSerialize)
|
||||
{
|
||||
aSerialize.AfterDeserialization();
|
||||
}
|
||||
|
||||
return @object;
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用ProtoBuf反序列化数据到实例
|
||||
/// </summary>
|
||||
/// <param name="bytes"></param>
|
||||
/// <param name="index"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public unsafe T Deserialize<T>(byte[] bytes, int index, int count)
|
||||
{
|
||||
fixed (byte* ptr = &bytes[index])
|
||||
{
|
||||
using var stream = new UnmanagedMemoryStream(ptr, count);
|
||||
var @object = ProtoBuf.Serializer.Deserialize<T>(stream);
|
||||
if (@object is ASerialize aSerialize)
|
||||
{
|
||||
aSerialize.AfterDeserialization();
|
||||
}
|
||||
return @object;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用ProtoBuf反序列化数据到实例
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="bytes"></param>
|
||||
/// <param name="index"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
public unsafe object Deserialize(Type type, byte[] bytes, int index, int count)
|
||||
{
|
||||
fixed (byte* ptr = &bytes[index])
|
||||
{
|
||||
using var stream = new UnmanagedMemoryStream(ptr, count);
|
||||
var @object = ProtoBuf.Serializer.Deserialize(type, stream);
|
||||
if (@object is ASerialize aSerialize)
|
||||
{
|
||||
aSerialize.AfterDeserialization();
|
||||
}
|
||||
return @object;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用ProtoBuf序列化某一个实例到IBufferWriter中
|
||||
/// </summary>
|
||||
/// <param name="object"></param>
|
||||
/// <param name="buffer"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public void Serialize<T>(T @object, IBufferWriter<byte> buffer)
|
||||
{
|
||||
if (@object is ASerialize aSerialize)
|
||||
{
|
||||
aSerialize.BeginInit();
|
||||
}
|
||||
|
||||
RuntimeTypeModel.Default.Serialize((MemoryStream)buffer, @object);
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用ProtoBuf序列化某一个实例到IBufferWriter中
|
||||
/// </summary>
|
||||
/// <param name="object"></param>
|
||||
/// <param name="buffer"></param>
|
||||
public void Serialize(object @object, IBufferWriter<byte> buffer)
|
||||
{
|
||||
if (@object is ASerialize aSerialize)
|
||||
{
|
||||
aSerialize.BeginInit();
|
||||
}
|
||||
|
||||
RuntimeTypeModel.Default.Serialize((MemoryStream)buffer, @object);
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用ProtoBuf序列化某一个实例到IBufferWriter中
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="object"></param>
|
||||
/// <param name="buffer"></param>
|
||||
public void Serialize(Type type, object @object, IBufferWriter<byte> buffer)
|
||||
{
|
||||
if (@object is ASerialize aSerialize)
|
||||
{
|
||||
aSerialize.BeginInit();
|
||||
}
|
||||
|
||||
RuntimeTypeModel.Default.Serialize((MemoryStream)buffer, @object);
|
||||
}
|
||||
private byte[] Serialize<T>(T @object)
|
||||
{
|
||||
if (@object is ASerialize aSerialize)
|
||||
{
|
||||
aSerialize.BeginInit();
|
||||
}
|
||||
|
||||
using (var buffer = new MemoryStream())
|
||||
{
|
||||
RuntimeTypeModel.Default.Serialize(buffer, @object);
|
||||
return buffer.ToArray();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 克隆
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public T Clone<T>(T t)
|
||||
{
|
||||
return Deserialize<T>(Serialize(t));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b5e2bbc6d50014a4693df1cf25208d0d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
136
Assets/Scripts/NBC/Runtime/Core/Serialize/SerializerManager.cs
Normal file
136
Assets/Scripts/NBC/Runtime/Core/Serialize/SerializerManager.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NBC.Assembly;
|
||||
using NBC.Helper;
|
||||
#if !FANTASY_EXPORTER
|
||||
using NBC.Network;
|
||||
#endif
|
||||
#pragma warning disable CS8604 // Possible null reference argument.
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
|
||||
#pragma warning disable CS8602 // Dereference of a possibly null reference.
|
||||
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
|
||||
|
||||
namespace NBC.Serialize
|
||||
{
|
||||
/// <summary>
|
||||
/// 框架内置的序列化器类型
|
||||
/// </summary>
|
||||
public static class FantasySerializerType
|
||||
{
|
||||
/// <summary>
|
||||
/// ProtoBuf在SerializerManager的数组下标
|
||||
/// </summary>
|
||||
public const int ProtoBuf = 0;
|
||||
/// <summary>
|
||||
/// Bson在SerializerManager的数组下标
|
||||
/// </summary>
|
||||
public const int Bson = 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 管理序列化静态方法,主要是优化网络协议时使用。
|
||||
/// </summary>
|
||||
public static class SerializerManager
|
||||
{
|
||||
private static ISerialize[] _serializers;
|
||||
private static bool _isInitialized = false;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化方法
|
||||
/// </summary>
|
||||
public static void Initialize()
|
||||
{
|
||||
if (_isInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var sort = new SortedList<long, ISerialize>();
|
||||
|
||||
foreach (var serializerType in AssemblySystem.ForEach(typeof(ISerialize)))
|
||||
{
|
||||
var serializer = (ISerialize)Activator.CreateInstance(serializerType);
|
||||
var computeHash64 = HashCodeHelper.ComputeHash64(serializer.SerializeName);
|
||||
sort.Add(computeHash64, serializer);
|
||||
}
|
||||
|
||||
var index = 1;
|
||||
_serializers = new ISerialize[sort.Count];
|
||||
|
||||
foreach (var (_, serialize) in sort)
|
||||
{
|
||||
var serializerIndex = 0;
|
||||
|
||||
switch (serialize)
|
||||
{
|
||||
case ProtoBufPackHelper:
|
||||
{
|
||||
serializerIndex = FantasySerializerType.ProtoBuf;
|
||||
break;
|
||||
}
|
||||
case BsonPackHelper:
|
||||
{
|
||||
serializerIndex = FantasySerializerType.Bson;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
serializerIndex = ++index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_serializers[serializerIndex] = serialize;
|
||||
}
|
||||
|
||||
_isInitialized = true;
|
||||
Log.Info($"初始化序列化器成功,数量为:{_serializers.Length}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁方法
|
||||
/// </summary>
|
||||
public static void Dispose()
|
||||
{
|
||||
_isInitialized = false;
|
||||
Array.Clear(_serializers, 0, _serializers.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据协议类型获取序列化器
|
||||
/// </summary>
|
||||
/// <param name="opCodeProtocolType"></param>
|
||||
/// <returns></returns>
|
||||
public static ISerialize GetSerializer(uint opCodeProtocolType)
|
||||
{
|
||||
return _serializers[opCodeProtocolType];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获得一个序列化器
|
||||
/// </summary>
|
||||
/// <param name="opCodeProtocolType"></param>
|
||||
/// <param name="serializer"></param>
|
||||
/// <returns></returns>
|
||||
public static bool TryGetSerializer(uint opCodeProtocolType, out ISerialize serializer)
|
||||
{
|
||||
if (opCodeProtocolType < _serializers.Length)
|
||||
{
|
||||
serializer = _serializers[opCodeProtocolType];
|
||||
return true;
|
||||
}
|
||||
|
||||
serializer = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 697ffcbc908194a5c9175595fd4e78e9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user