zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Model/Helper/MongoHelper.cs

173 lines
5.2 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
2021-04-08 20:09:59 +08:00
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.Serializers;
#if UNITY
2021-04-08 20:09:59 +08:00
using UnityEngine;
using Vector3 = UnityEngine.Vector3;
using Vector4 = UnityEngine.Vector4;
2021-04-08 20:09:59 +08:00
#endif
2021-04-08 20:09:59 +08:00
namespace ET
{
public static class MongoHelper
{
static MongoHelper()
{
// 自动注册IgnoreExtraElements
ConventionPack conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) };
ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);
#if SERVER
BsonSerializer.RegisterSerializer(typeof(Vector3), new StructBsonSerialize<Vector3>());
BsonSerializer.RegisterSerializer(typeof(Vector4), new StructBsonSerialize<Vector4>());
BsonSerializer.RegisterSerializer(typeof(Quaternion), new StructBsonSerialize<Quaternion>());
#elif ROBOT
BsonSerializer.RegisterSerializer(typeof(Quaternion), new StructBsonSerialize<Quaternion>());
BsonSerializer.RegisterSerializer(typeof(Vector3), new StructBsonSerialize<Vector3>());
BsonSerializer.RegisterSerializer(typeof(Vector4), new StructBsonSerialize<Vector4>());
#else
BsonSerializer.RegisterSerializer(typeof (Vector4), new StructBsonSerialize<Vector4>());
BsonSerializer.RegisterSerializer(typeof (Vector3), new StructBsonSerialize<Vector3>());
BsonSerializer.RegisterSerializer(typeof (Vector2Int), new StructBsonSerialize<Vector2Int>());
#endif
var types = Game.EventSystem.GetTypes();
foreach (Type type in types)
{
if (!type.IsSubclassOf(typeof (Object)))
{
continue;
}
if (type.IsGenericType)
{
continue;
}
BsonClassMap.LookupClassMap(type);
}
}
public static void Init()
{
}
public static string ToJson(object obj)
{
return obj.ToJson();
}
public static string ToJson(object obj, JsonWriterSettings settings)
{
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[] ToBson(object obj)
{
return obj.ToBson();
}
public static void ToStream(object message, MemoryStream stream)
{
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 FromBson(Type type, byte[] bytes)
{
try
{
return BsonSerializer.Deserialize(bytes, type);
}
catch (Exception e)
{
throw new Exception($"from bson error: {type.Name}", e);
}
}
public static object FromBson(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.Name}", e);
}
}
public static object FromStream(Type type, Stream stream)
{
try
{
return BsonSerializer.Deserialize(stream, type);
}
catch (Exception e)
{
throw new Exception($"from bson error: {type.Name}", e);
}
}
public static T FromBson<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).Name}", e);
}
}
public static T FromBson<T>(byte[] bytes, int index, int count)
{
return (T) FromBson(typeof (T), bytes, index, count);
}
public static T Clone<T>(T t)
{
return FromBson<T>(ToBson(t));
}
}
}