MongoRegister.cs 2.2 KB

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