MongoHelper.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using MongoDB.Bson;
  5. using MongoDB.Bson.IO;
  6. using MongoDB.Bson.Serialization;
  7. using MongoDB.Bson.Serialization.Serializers;
  8. namespace ETModel
  9. {
  10. public static class MongoHelper
  11. {
  12. static MongoHelper()
  13. {
  14. Type bsonClassMap = typeof(BsonClassMap);
  15. MethodInfo methodInfo = bsonClassMap.GetMethod("RegisterClassMap", new Type[] { });
  16. Type[] types = typeof(Game).Assembly.GetTypes();
  17. foreach (Type type in types)
  18. {
  19. if (!type.IsSubclassOf(typeof(Component)))
  20. {
  21. continue;
  22. }
  23. if(type == typeof(ComponentWithId) || type == typeof(Component) || type == typeof(Entity))
  24. {
  25. continue;
  26. }
  27. methodInfo.MakeGenericMethod(type).Invoke(null, null);
  28. }
  29. BsonSerializer.RegisterSerializer(new EnumSerializer<NumericType>(BsonType.String));
  30. }
  31. public static string ToJson(object obj)
  32. {
  33. return obj.ToJson();
  34. }
  35. public static string ToJson(object obj, JsonWriterSettings settings)
  36. {
  37. return obj.ToJson(settings);
  38. }
  39. public static T FromJson<T>(string str)
  40. {
  41. return BsonSerializer.Deserialize<T>(str);
  42. }
  43. public static object FromJson(Type type, string str)
  44. {
  45. return BsonSerializer.Deserialize(str, type);
  46. }
  47. public static byte[] ToBson(object obj)
  48. {
  49. return obj.ToBson();
  50. }
  51. public static void ToBson(object obj, MemoryStream stream)
  52. {
  53. using (BsonBinaryWriter bsonWriter = new BsonBinaryWriter(stream, BsonBinaryWriterSettings.Defaults))
  54. {
  55. BsonSerializationContext context = BsonSerializationContext.CreateRoot(bsonWriter);
  56. BsonSerializationArgs args = default (BsonSerializationArgs);
  57. args.NominalType = typeof(object);
  58. IBsonSerializer serializer = BsonSerializer.LookupSerializer(args.NominalType);
  59. serializer.Serialize(context, args, obj);
  60. }
  61. }
  62. public static object FromBson(Type type, byte[] bytes)
  63. {
  64. return BsonSerializer.Deserialize(bytes, type);
  65. }
  66. public static object FromBson(Type type, byte[] bytes, int index, int count)
  67. {
  68. using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
  69. {
  70. return BsonSerializer.Deserialize(memoryStream, type);
  71. }
  72. }
  73. public static object FromBson(object instance, byte[] bytes, int index, int count)
  74. {
  75. using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
  76. {
  77. return BsonSerializer.Deserialize(memoryStream, instance.GetType());
  78. }
  79. }
  80. public static object FromBson(object instance, Stream stream)
  81. {
  82. return BsonSerializer.Deserialize(stream, instance.GetType());
  83. }
  84. public static object FromStream(Type type, Stream stream)
  85. {
  86. return BsonSerializer.Deserialize(stream, type);
  87. }
  88. public static T FromBson<T>(byte[] bytes)
  89. {
  90. using (MemoryStream memoryStream = new MemoryStream(bytes))
  91. {
  92. return (T) BsonSerializer.Deserialize(memoryStream, typeof (T));
  93. }
  94. }
  95. public static T FromBson<T>(byte[] bytes, int index, int count)
  96. {
  97. return (T) FromBson(typeof (T), bytes, index, count);
  98. }
  99. public static T Clone<T>(T t)
  100. {
  101. return FromBson<T>(ToBson(t));
  102. }
  103. }
  104. }