MongoHelper.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. byte[] bytes = obj.ToBson();
  54. stream.Write(bytes);
  55. stream.Seek(0, SeekOrigin.Begin);
  56. }
  57. public static object FromBson(Type type, byte[] bytes)
  58. {
  59. return BsonSerializer.Deserialize(bytes, type);
  60. }
  61. public static object FromBson(Type type, byte[] bytes, int index, int count)
  62. {
  63. using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
  64. {
  65. return BsonSerializer.Deserialize(memoryStream, type);
  66. }
  67. }
  68. public static object FromBson(object instance, byte[] bytes, int index, int count)
  69. {
  70. using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
  71. {
  72. return BsonSerializer.Deserialize(memoryStream, instance.GetType());
  73. }
  74. }
  75. public static object FromBson(object instance, Stream stream)
  76. {
  77. return BsonSerializer.Deserialize(stream, instance.GetType());
  78. }
  79. public static object FromStream(Type type, Stream stream)
  80. {
  81. return BsonSerializer.Deserialize(stream, type);
  82. }
  83. public static T FromBson<T>(byte[] bytes)
  84. {
  85. using (MemoryStream memoryStream = new MemoryStream(bytes))
  86. {
  87. return (T) BsonSerializer.Deserialize(memoryStream, typeof (T));
  88. }
  89. }
  90. public static T FromBson<T>(byte[] bytes, int index, int count)
  91. {
  92. return (T) FromBson(typeof (T), bytes, index, count);
  93. }
  94. public static T Clone<T>(T t)
  95. {
  96. return FromBson<T>(ToBson(t));
  97. }
  98. }
  99. }