MongoHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.IO;
  3. using MongoDB.Bson;
  4. using MongoDB.Bson.IO;
  5. using MongoDB.Bson.Serialization;
  6. using MongoDB.Bson.Serialization.Serializers;
  7. using System.Collections.Generic;
  8. using MongoDB.Bson.Serialization.Conventions;
  9. using UnityEngine;
  10. namespace ET
  11. {
  12. public static class MongoHelper
  13. {
  14. static MongoHelper()
  15. {
  16. // 自动注册IgnoreExtraElements
  17. ConventionPack conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) };
  18. ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);
  19. Type[] types = typeof(Game).Assembly.GetTypes();
  20. foreach (Type type in types)
  21. {
  22. if (!type.IsSubclassOf(typeof(Entity)))
  23. {
  24. continue;
  25. }
  26. if (type.IsGenericType)
  27. {
  28. continue;
  29. }
  30. try
  31. {
  32. BsonClassMap.LookupClassMap(type);
  33. }
  34. catch (Exception e)
  35. {
  36. Log.Error($"11111111111111111: {type.Name} {e}");
  37. }
  38. }
  39. #if SERVER
  40. BsonSerializer.RegisterSerializer(typeof(Vector3), new StructBsonSerialize<Vector3>());
  41. #else
  42. BsonSerializer.RegisterSerializer(typeof(Vector4), new StructBsonSerialize<Vector4>());
  43. BsonSerializer.RegisterSerializer(typeof(Vector2Int), new StructBsonSerialize<Vector2Int>());
  44. #endif
  45. }
  46. public static void Init()
  47. {
  48. // 调用这个是为了调用MongoHelper的静态方法
  49. }
  50. public static string ToJson(object obj)
  51. {
  52. return obj.ToJson();
  53. }
  54. public static string ToJson(object obj, JsonWriterSettings settings)
  55. {
  56. return obj.ToJson(settings);
  57. }
  58. public static T FromJson<T>(string str)
  59. {
  60. return BsonSerializer.Deserialize<T>(str);
  61. }
  62. public static object FromJson(Type type, string str)
  63. {
  64. return BsonSerializer.Deserialize(str, type);
  65. }
  66. public static byte[] ToBson(object obj)
  67. {
  68. return obj.ToBson();
  69. }
  70. public static void ToBson(object obj, MemoryStream stream)
  71. {
  72. using (BsonBinaryWriter bsonWriter = new BsonBinaryWriter(stream, BsonBinaryWriterSettings.Defaults))
  73. {
  74. BsonSerializationContext context = BsonSerializationContext.CreateRoot(bsonWriter);
  75. BsonSerializationArgs args = default (BsonSerializationArgs);
  76. args.NominalType = typeof(object);
  77. IBsonSerializer serializer = BsonSerializer.LookupSerializer(args.NominalType);
  78. serializer.Serialize(context, args, obj);
  79. }
  80. }
  81. public static object FromBson(Type type, byte[] bytes)
  82. {
  83. return BsonSerializer.Deserialize(bytes, type);
  84. }
  85. public static object FromBson(Type type, byte[] bytes, int index, int count)
  86. {
  87. using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
  88. {
  89. return BsonSerializer.Deserialize(memoryStream, type);
  90. }
  91. }
  92. public static object FromBson(object instance, byte[] bytes, int index, int count)
  93. {
  94. using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
  95. {
  96. return BsonSerializer.Deserialize(memoryStream, instance.GetType());
  97. }
  98. }
  99. public static object FromBson(object instance, Stream stream)
  100. {
  101. return BsonSerializer.Deserialize(stream, instance.GetType());
  102. }
  103. public static object FromStream(Type type, Stream stream)
  104. {
  105. return BsonSerializer.Deserialize(stream, type);
  106. }
  107. public static T FromBson<T>(byte[] bytes)
  108. {
  109. using (MemoryStream memoryStream = new MemoryStream(bytes))
  110. {
  111. return (T) BsonSerializer.Deserialize(memoryStream, typeof (T));
  112. }
  113. }
  114. public static T FromBson<T>(byte[] bytes, int index, int count)
  115. {
  116. return (T) FromBson(typeof (T), bytes, index, count);
  117. }
  118. public static T Clone<T>(T t)
  119. {
  120. return FromBson<T>(ToBson(t));
  121. }
  122. public static void AvoidAOT()
  123. {
  124. ArraySerializer<int> aint = new ArraySerializer<int>();
  125. ArraySerializer<string> astring = new ArraySerializer<string>();
  126. ArraySerializer<long> along = new ArraySerializer<long>();
  127. EnumerableInterfaceImplementerSerializer<List<int>> e = new EnumerableInterfaceImplementerSerializer<List<int>>();
  128. EnumerableInterfaceImplementerSerializer<List<int>, int> elistint = new EnumerableInterfaceImplementerSerializer<List<int>, int>();
  129. }
  130. }
  131. }