add kcp
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#if UNITY
|
||||
#if NET5_0_OR_GREATER
|
||||
#else
|
||||
using UnityEngine;
|
||||
#endif
|
||||
using System;
|
||||
@@ -6,7 +7,9 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Cysharp.Threading.Tasks;
|
||||
#if NET5_0_OR_GREATER
|
||||
using Microsoft.SqlServer.Server;
|
||||
#endif
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BITKit
|
||||
@@ -14,27 +17,37 @@ namespace BITKit
|
||||
public class BITBinary
|
||||
{
|
||||
private static readonly Dictionary<string, INetMessageReader> netReaders = new();
|
||||
private static readonly List<Type> serializableTypes = new();
|
||||
#if NET5_0_OR_GREATER
|
||||
public static readonly List<Type> serializableTypes = new();
|
||||
#endif
|
||||
|
||||
public static async UniTask Start()
|
||||
{
|
||||
netReaders.Clear();
|
||||
#if NET5_0_OR_GREATER
|
||||
serializableTypes.Clear();
|
||||
#endif
|
||||
foreach (var x in await ReflectionHelper.GetInstances<INetMessageReader>())
|
||||
{
|
||||
var typeName = x.GetMessageType().FullName;
|
||||
if (typeName == null) continue;
|
||||
netReaders.Add(typeName, x);
|
||||
BIT4Log.Log<BITBinary>($"已注册类型:{typeName}");
|
||||
}
|
||||
#if NET5_0_OR_GREATER
|
||||
var serializes = await ReflectionHelper.GetInstances<IBinarySerialize>();
|
||||
#if UNITY
|
||||
serializes = serializes.Where(x => x is not UnityEngine.Object);
|
||||
#else
|
||||
|
||||
#endif
|
||||
foreach (var x in serializes)
|
||||
{
|
||||
serializableTypes.Add(x.GetType());
|
||||
BIT4Log.Log<BITBinary>($"已注册类型:{x.GetType().FullName}");
|
||||
}
|
||||
// #if NET5_0_OR_GREATER
|
||||
// #else
|
||||
// serializes = serializes.Where(x => x is not UnityEngine.Object);
|
||||
// #endif
|
||||
// foreach (var x in serializes)
|
||||
// {
|
||||
// serializableTypes.Add(x.GetType());
|
||||
// BIT4Log.Log<BITBinary>($"已注册类型:{x.GetType().FullName}");
|
||||
// }
|
||||
}
|
||||
|
||||
public static object Read<T>(byte[] buffer) => (T)ReadAsValue(buffer);
|
||||
@@ -60,26 +73,22 @@ namespace BITKit
|
||||
|
||||
var typeName = reader.ReadString();
|
||||
if (netReaders.TryGetValue(typeName, out var netReader))
|
||||
{
|
||||
return netReader.ReadBinaryAsObject(reader);
|
||||
}
|
||||
var json = reader.ReadString();
|
||||
json = reader.ReadString();
|
||||
if (string.IsNullOrEmpty(json))
|
||||
{
|
||||
throw new Exception($"从二进制中读取的json值为空");
|
||||
}
|
||||
try
|
||||
{
|
||||
|
||||
if (BITSharp.TryGetTypeFromFullName(typeName, out var type))
|
||||
{
|
||||
return JsonConvert.DeserializeObject(json, type);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.Warning<BITBinary>($"反序列化失败,类型:{typeName},值:\n{json}");
|
||||
throw;
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
|
||||
|
||||
throw new Exception("未找到读取该二进制的BinaryReader");
|
||||
}
|
||||
|
||||
@@ -119,14 +128,8 @@ namespace BITKit
|
||||
{
|
||||
netReader.WriteBinaryAsObject(writer,value);
|
||||
}
|
||||
else if( value is IBinarySerialize serialize)
|
||||
{
|
||||
serialize.Write(writer);
|
||||
}
|
||||
else
|
||||
{
|
||||
//throw new Exception($"没有找到{value.GetType().Name}的Binary写入方法");
|
||||
writer.Write(value!.GetType().FullName!);
|
||||
writer.Write(JsonConvert.SerializeObject(value));
|
||||
}
|
||||
}
|
||||
@@ -139,10 +142,12 @@ namespace BITKit
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (serializableTypes.Any(x => x.FullName == typeName))
|
||||
#if NET5_0_OR_GREATER
|
||||
else if (serializableTypes.Any(x => x.FullName == typeName))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user