mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] TEngineServer
[+] TEngineServer
This commit is contained in:
118
Assets/GameScripts/DotNet/Core/Helper/Mongo/MongoHelper.cs
Normal file
118
Assets/GameScripts/DotNet/Core/Helper/Mongo/MongoHelper.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
#if TENGINE_NET
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization;
|
||||
using MongoDB.Bson.Serialization.Conventions;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace TEngine.Core;
|
||||
|
||||
public sealed class MongoHelper : Singleton<MongoHelper>
|
||||
{
|
||||
private readonly HashSet<int> _registerCount = new HashSet<int>(3);
|
||||
|
||||
static MongoHelper()
|
||||
{
|
||||
// 自动注册IgnoreExtraElements
|
||||
var conventionPack = new ConventionPack {new IgnoreExtraElementsConvention(true)};
|
||||
ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);
|
||||
BsonSerializer.TryRegisterSerializer(typeof(float2), new StructBsonSerialize<float2>());
|
||||
BsonSerializer.TryRegisterSerializer(typeof(float3), new StructBsonSerialize<float3>());
|
||||
BsonSerializer.TryRegisterSerializer(typeof(float4), new StructBsonSerialize<float4>());
|
||||
BsonSerializer.TryRegisterSerializer(typeof(quaternion), new StructBsonSerialize<quaternion>());
|
||||
}
|
||||
|
||||
protected override void OnLoad(int assemblyName)
|
||||
{
|
||||
if (_registerCount.Count == 3)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_registerCount.Add(assemblyName);
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
foreach (var type in AssemblyManager.ForEach(assemblyName))
|
||||
{
|
||||
if (type.IsInterface || type.IsAbstract || !typeof(Entity).IsAssignableFrom(type))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
BsonClassMap.LookupClassMap(type);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public T Deserialize<T>(byte[] bytes)
|
||||
{
|
||||
return BsonSerializer.Deserialize<T>(bytes);
|
||||
}
|
||||
|
||||
public object Deserialize(byte[] bytes, Type type)
|
||||
{
|
||||
return BsonSerializer.Deserialize(bytes, type);
|
||||
}
|
||||
|
||||
public object Deserialize(byte[] bytes, string type)
|
||||
{
|
||||
return BsonSerializer.Deserialize(bytes, Type.GetType(type));
|
||||
}
|
||||
|
||||
public T Deserialize<T>(Stream stream)
|
||||
{
|
||||
return BsonSerializer.Deserialize<T>(stream);
|
||||
}
|
||||
|
||||
public object Deserialize(Stream stream, Type type)
|
||||
{
|
||||
return BsonSerializer.Deserialize(stream, type);
|
||||
}
|
||||
|
||||
public object DeserializeFrom(Type type, MemoryStream stream)
|
||||
{
|
||||
return Deserialize(stream, type);
|
||||
}
|
||||
|
||||
public T DeserializeFrom<T>(MemoryStream stream)
|
||||
{
|
||||
return Deserialize<T>(stream);
|
||||
}
|
||||
|
||||
public T DeserializeFrom<T>(byte[] bytes, int index, int count)
|
||||
{
|
||||
return BsonSerializer.Deserialize<T>(bytes.AsMemory(index, count).ToArray());
|
||||
}
|
||||
|
||||
public byte[] SerializeTo<T>(T t)
|
||||
{
|
||||
return t.ToBson();
|
||||
}
|
||||
|
||||
public void SerializeTo<T>(T t, MemoryStream stream)
|
||||
{
|
||||
var bytes = t.ToBson();
|
||||
|
||||
stream.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
public T Clone<T>(T t)
|
||||
{
|
||||
return Deserialize<T>(t.ToBson());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if TENGINE_UNITY
|
||||
using System;
|
||||
using System.IO;
|
||||
namespace TEngine.Core
|
||||
{
|
||||
public sealed class MongoHelper : Singleton<MongoHelper>
|
||||
{
|
||||
public object DeserializeFrom(Type type, MemoryStream stream)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8305336cdf88b72498d9c3365424ed49
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,57 @@
|
||||
#if TENGINE_NET
|
||||
using System.Reflection;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.IO;
|
||||
using MongoDB.Bson.Serialization;
|
||||
using MongoDB.Bson.Serialization.Serializers;
|
||||
|
||||
namespace TEngine.Core;
|
||||
|
||||
public class StructBsonSerialize<TValue> : StructSerializerBase<TValue> where TValue : struct
|
||||
{
|
||||
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
|
||||
{
|
||||
var nominalType = args.NominalType;
|
||||
|
||||
var bsonWriter = context.Writer;
|
||||
|
||||
bsonWriter.WriteStartDocument();
|
||||
|
||||
var fields = nominalType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
foreach (var 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();
|
||||
var actualType = args.NominalType;
|
||||
var bsonReader = context.Reader;
|
||||
|
||||
bsonReader.ReadStartDocument();
|
||||
|
||||
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
|
||||
{
|
||||
var name = bsonReader.ReadName(Utf8NameDecoder.Instance);
|
||||
|
||||
var field = actualType.GetField(name,
|
||||
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
if (field != null)
|
||||
{
|
||||
var value = BsonSerializer.Deserialize(bsonReader, field.FieldType);
|
||||
field.SetValue(obj, value);
|
||||
}
|
||||
}
|
||||
|
||||
bsonReader.ReadEndDocument();
|
||||
|
||||
return (TValue) obj;
|
||||
}
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8ac9e1e3f21a01f40a9bda7f7cc4e667
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user