1
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,18 +73,12 @@ namespace BITKit
|
||||
|
||||
var typeName = reader.ReadString();
|
||||
if (netReaders.TryGetValue(typeName, out var netReader))
|
||||
{
|
||||
return netReader.ReadBinaryAsObject(reader);
|
||||
}
|
||||
var json = reader.ReadString();
|
||||
json = reader.ReadString();
|
||||
try
|
||||
{
|
||||
|
||||
if (BITSharp.TryGetTypeFromFullName(typeName, out var type))
|
||||
{
|
||||
return JsonConvert.DeserializeObject(json, type);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -119,10 +126,12 @@ namespace BITKit
|
||||
{
|
||||
netReader.WriteBinaryAsObject(writer,value);
|
||||
}
|
||||
#if NET5_0_OR_GREATER
|
||||
else if( value is IBinarySerialize serialize)
|
||||
{
|
||||
serialize.Write(writer);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
//throw new Exception($"没有找到{value.GetType().Name}的Binary写入方法");
|
||||
@@ -139,10 +148,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