MongoRegister.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // 自动注册IgnoreExtraElements
  24. ConventionPack conventionPack = new() { new IgnoreExtraElementsConvention(true) };
  25. ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);
  26. RegisterStruct<float2>();
  27. RegisterStruct<float3>();
  28. RegisterStruct<float4>();
  29. RegisterStruct<quaternion>();
  30. RegisterStruct<FP>();
  31. RegisterStruct<TSVector>();
  32. RegisterStruct<TSVector2>();
  33. RegisterStruct<TSVector4>();
  34. RegisterStruct<TSQuaternion>();
  35. RegisterStruct<LSInput>();
  36. Dictionary<string, Type> types = CodeTypes.Instance.GetTypes();
  37. foreach (Type type in types.Values)
  38. {
  39. if (!type.IsSubclassOf(typeof (Object)))
  40. {
  41. continue;
  42. }
  43. if (type.IsGenericType)
  44. {
  45. continue;
  46. }
  47. BsonClassMap.LookupClassMap(type);
  48. }
  49. }
  50. }
  51. }