MongoHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 UnityEngine;
  9. namespace ET
  10. {
  11. public static class MongoHelper
  12. {
  13. public static void Register(Dictionary<string, Type> types)
  14. {
  15. // 自动注册IgnoreExtraElements
  16. ConventionPack conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) };
  17. ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);
  18. BsonSerializer.RegisterSerializer(typeof(Vector3), new StructBsonSerialize<Vector3>());
  19. BsonSerializer.RegisterSerializer(typeof(Vector4), new StructBsonSerialize<Vector4>());
  20. BsonSerializer.RegisterSerializer(typeof(Quaternion), new StructBsonSerialize<Quaternion>());
  21. foreach (Type type in types.Values)
  22. {
  23. if (!type.IsSubclassOf(typeof (Object)))
  24. {
  25. continue;
  26. }
  27. if (type.IsGenericType)
  28. {
  29. continue;
  30. }
  31. BsonClassMap.LookupClassMap(type);
  32. }
  33. }
  34. public static string ToJson(object obj)
  35. {
  36. return obj.ToJson();
  37. }
  38. public static string ToJson(object obj, JsonWriterSettings settings)
  39. {
  40. return obj.ToJson(settings);
  41. }
  42. public static T FromJson<T>(string str)
  43. {
  44. try
  45. {
  46. return BsonSerializer.Deserialize<T>(str);
  47. }
  48. catch (Exception e)
  49. {
  50. throw new Exception($"{str}\n{e}");
  51. }
  52. }
  53. public static object FromJson(Type type, string str)
  54. {
  55. return BsonSerializer.Deserialize(str, type);
  56. }
  57. public static byte[] ToBson(object obj)
  58. {
  59. return obj.ToBson();
  60. }
  61. public static void ToStream(object message, MemoryStream stream)
  62. {
  63. using (BsonBinaryWriter bsonWriter = new BsonBinaryWriter(stream, BsonBinaryWriterSettings.Defaults))
  64. {
  65. BsonSerializationContext context = BsonSerializationContext.CreateRoot(bsonWriter);
  66. BsonSerializationArgs args = default;
  67. args.NominalType = typeof (object);
  68. IBsonSerializer serializer = BsonSerializer.LookupSerializer(args.NominalType);
  69. serializer.Serialize(context, args, message);
  70. }
  71. }
  72. public static object FromBson(Type type, byte[] bytes)
  73. {
  74. try
  75. {
  76. return BsonSerializer.Deserialize(bytes, type);
  77. }
  78. catch (Exception e)
  79. {
  80. throw new Exception($"from bson error: {type.Name}", e);
  81. }
  82. }
  83. public static object FromBson(Type type, byte[] bytes, int index, int count)
  84. {
  85. try
  86. {
  87. using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
  88. {
  89. return BsonSerializer.Deserialize(memoryStream, type);
  90. }
  91. }
  92. catch (Exception e)
  93. {
  94. throw new Exception($"from bson error: {type.Name}", e);
  95. }
  96. }
  97. public static object FromStream(Type type, Stream stream)
  98. {
  99. try
  100. {
  101. return BsonSerializer.Deserialize(stream, type);
  102. }
  103. catch (Exception e)
  104. {
  105. throw new Exception($"from bson error: {type.Name}", e);
  106. }
  107. }
  108. public static T FromBson<T>(byte[] bytes)
  109. {
  110. try
  111. {
  112. using (MemoryStream memoryStream = new MemoryStream(bytes))
  113. {
  114. return (T) BsonSerializer.Deserialize(memoryStream, typeof (T));
  115. }
  116. }
  117. catch (Exception e)
  118. {
  119. throw new Exception($"from bson error: {typeof (T).Name}", e);
  120. }
  121. }
  122. public static T FromBson<T>(byte[] bytes, int index, int count)
  123. {
  124. return (T) FromBson(typeof (T), bytes, index, count);
  125. }
  126. public static T Clone<T>(T t)
  127. {
  128. return FromBson<T>(ToBson(t));
  129. }
  130. }
  131. }