MongoHelper.cs 5.0 KB

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