MongoHelper.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using MongoDB.Bson;
  5. using MongoDB.Bson.IO;
  6. using MongoDB.Bson.Serialization;
  7. using MongoDB.Bson.Serialization.Conventions;
  8. using MongoDB.Bson.Serialization.Serializers;
  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. #if SERVER
  20. BsonSerializer.RegisterSerializer(typeof(Vector3), new StructBsonSerialize<Vector3>());
  21. BsonSerializer.RegisterSerializer(typeof(Vector4), new StructBsonSerialize<Vector4>());
  22. BsonSerializer.RegisterSerializer(typeof(Quaternion), new StructBsonSerialize<Quaternion>());
  23. #elif ROBOT
  24. BsonSerializer.RegisterSerializer(typeof(Quaternion), new StructBsonSerialize<Quaternion>());
  25. BsonSerializer.RegisterSerializer(typeof(Vector3), new StructBsonSerialize<Vector3>());
  26. BsonSerializer.RegisterSerializer(typeof(Vector4), new StructBsonSerialize<Vector4>());
  27. #else
  28. BsonSerializer.RegisterSerializer(typeof (Vector4), new StructBsonSerialize<Vector4>());
  29. BsonSerializer.RegisterSerializer(typeof (Vector3), new StructBsonSerialize<Vector3>());
  30. BsonSerializer.RegisterSerializer(typeof (Vector2Int), new StructBsonSerialize<Vector2Int>());
  31. #endif
  32. var types = Game.EventSystem.GetTypes();
  33. foreach (Type type in types)
  34. {
  35. if (!type.IsSubclassOf(typeof (Object)))
  36. {
  37. continue;
  38. }
  39. if (type.IsGenericType)
  40. {
  41. continue;
  42. }
  43. BsonClassMap.LookupClassMap(type);
  44. }
  45. }
  46. public static void Init()
  47. {
  48. }
  49. public static string ToJson(object obj)
  50. {
  51. return obj.ToJson();
  52. }
  53. public static string ToJson(object obj, JsonWriterSettings settings)
  54. {
  55. return obj.ToJson(settings);
  56. }
  57. public static T FromJson<T>(string str)
  58. {
  59. try
  60. {
  61. return BsonSerializer.Deserialize<T>(str);
  62. }
  63. catch (Exception e)
  64. {
  65. throw new Exception($"{str}\n{e}");
  66. }
  67. }
  68. public static object FromJson(Type type, string str)
  69. {
  70. return BsonSerializer.Deserialize(str, type);
  71. }
  72. public static byte[] ToBson(object obj)
  73. {
  74. return obj.ToBson();
  75. }
  76. public static void ToStream(object message, MemoryStream stream)
  77. {
  78. using (BsonBinaryWriter bsonWriter = new BsonBinaryWriter(stream, BsonBinaryWriterSettings.Defaults))
  79. {
  80. BsonSerializationContext context = BsonSerializationContext.CreateRoot(bsonWriter);
  81. BsonSerializationArgs args = default;
  82. args.NominalType = typeof (object);
  83. IBsonSerializer serializer = BsonSerializer.LookupSerializer(args.NominalType);
  84. serializer.Serialize(context, args, message);
  85. }
  86. }
  87. public static object FromBson(Type type, byte[] bytes)
  88. {
  89. try
  90. {
  91. return BsonSerializer.Deserialize(bytes, type);
  92. }
  93. catch (Exception e)
  94. {
  95. throw new Exception($"from bson error: {type.Name}", e);
  96. }
  97. }
  98. public static object FromBson(Type type, byte[] bytes, int index, int count)
  99. {
  100. try
  101. {
  102. using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
  103. {
  104. return BsonSerializer.Deserialize(memoryStream, type);
  105. }
  106. }
  107. catch (Exception e)
  108. {
  109. throw new Exception($"from bson error: {type.Name}", e);
  110. }
  111. }
  112. public static object FromStream(Type type, Stream stream)
  113. {
  114. try
  115. {
  116. return BsonSerializer.Deserialize(stream, type);
  117. }
  118. catch (Exception e)
  119. {
  120. throw new Exception($"from bson error: {type.Name}", e);
  121. }
  122. }
  123. public static T FromBson<T>(byte[] bytes)
  124. {
  125. try
  126. {
  127. using (MemoryStream memoryStream = new MemoryStream(bytes))
  128. {
  129. return (T) BsonSerializer.Deserialize(memoryStream, typeof (T));
  130. }
  131. }
  132. catch (Exception e)
  133. {
  134. throw new Exception($"from bson error: {typeof (T).Name}", e);
  135. }
  136. }
  137. public static T FromBson<T>(byte[] bytes, int index, int count)
  138. {
  139. return (T) FromBson(typeof (T), bytes, index, count);
  140. }
  141. public static T Clone<T>(T t)
  142. {
  143. return FromBson<T>(ToBson(t));
  144. }
  145. }
  146. }