MongoRegister.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using MongoDB.Bson.Serialization;
  5. using MongoDB.Bson.Serialization.Conventions;
  6. using TrueSync;
  7. using Unity.Mathematics;
  8. namespace ET
  9. {
  10. public static class MongoRegister
  11. {
  12. private static void RegisterStruct<T>() where T : struct
  13. {
  14. BsonSerializer.RegisterSerializer(typeof (T), new StructBsonSerialize<T>());
  15. }
  16. public static void Init()
  17. {
  18. // 清理老的数据
  19. MethodInfo createSerializerRegistry = typeof (BsonSerializer).GetMethod("CreateSerializerRegistry", BindingFlags.Static | BindingFlags.NonPublic);
  20. createSerializerRegistry.Invoke(null, Array.Empty<object>());
  21. MethodInfo registerIdGenerators = typeof (BsonSerializer).GetMethod("RegisterIdGenerators", BindingFlags.Static | BindingFlags.NonPublic);
  22. registerIdGenerators.Invoke(null, Array.Empty<object>());
  23. BsonSerializer.RegisterSerializer(typeof(ComponentsCollection), new BsonComponentsCollectionSerializer());
  24. BsonSerializer.RegisterSerializer(typeof(ChildrenCollection), new BsonChildrenCollectionSerializer());
  25. // 自动注册IgnoreExtraElements
  26. ConventionPack conventionPack = new() { new IgnoreExtraElementsConvention(true) };
  27. ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);
  28. RegisterStruct<float2>();
  29. RegisterStruct<float3>();
  30. RegisterStruct<float4>();
  31. RegisterStruct<quaternion>();
  32. RegisterStruct<FP>();
  33. RegisterStruct<TSVector>();
  34. RegisterStruct<TSVector2>();
  35. RegisterStruct<TSVector4>();
  36. RegisterStruct<TSQuaternion>();
  37. RegisterStruct<LSInput>();
  38. Dictionary<string, Type> types = CodeTypes.Instance.GetTypes();
  39. foreach (Type type in types.Values)
  40. {
  41. if (!type.IsSubclassOf(typeof (Object)))
  42. {
  43. continue;
  44. }
  45. if (type.IsGenericType)
  46. {
  47. continue;
  48. }
  49. BsonClassMap.LookupClassMap(type);
  50. }
  51. }
  52. }
  53. }