提交 c9e5a9b2 编写于 作者: T tanghai

以后再也不需要手动添加BsonKnowType标签啦!

上级 d904f249
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
using System.Net; using System.Net;
using System.Threading; using System.Threading;
using ETModel; using ETModel;
using MongoDB.Bson.Serialization;
using NLog; using NLog;
namespace App namespace App
...@@ -13,8 +14,6 @@ namespace App ...@@ -13,8 +14,6 @@ namespace App
// 异步方法全部会回掉到主线程 // 异步方法全部会回掉到主线程
OneThreadSynchronizationContext contex = new OneThreadSynchronizationContext(); OneThreadSynchronizationContext contex = new OneThreadSynchronizationContext();
SynchronizationContext.SetSynchronizationContext(contex); SynchronizationContext.SetSynchronizationContext(contex);
MongoHelper.Init();
try try
{ {
......
using System; using System;
using System.IO; using System.IO;
using System.Reflection;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.IO; using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization;
...@@ -9,8 +10,21 @@ namespace ETModel ...@@ -9,8 +10,21 @@ namespace ETModel
{ {
public static class MongoHelper public static class MongoHelper
{ {
public static void Init() static MongoHelper()
{ {
Type bsonClassMap = typeof(BsonClassMap);
MethodInfo methodInfo = bsonClassMap.GetMethod("RegisterClassMap", new Type[] { });
Type[] types = typeof(Game).Assembly.GetTypes();
foreach (Type type in types)
{
if (!type.IsSubclassOf(typeof(Component)))
{
continue;
}
methodInfo.MakeGenericMethod(type).Invoke(null, null);
}
BsonSerializer.RegisterSerializer(new EnumSerializer<NumericType>(BsonType.String)); BsonSerializer.RegisterSerializer(new EnumSerializer<NumericType>(BsonType.String));
} }
......
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
{
[BsonKnownTypes(typeof(AConfigComponent))]
[BsonKnownTypes(typeof(UnitGateComponent))]
[BsonKnownTypes(typeof(NumericComponent))]
[BsonKnownTypes(typeof(ComponentWithId))]
public partial class Component
{
}
}
\ No newline at end of file
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
{
[BsonKnownTypes(typeof(Entity))]
public partial class ComponentWithId
{
}
}
\ No newline at end of file
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
{
[BsonKnownTypes(typeof(Location))]
[BsonKnownTypes(typeof(Recharge))]
[BsonKnownTypes(typeof(RechargeRecord))]
public partial class Entity
{
}
}
...@@ -5,13 +5,27 @@ using MongoDB.Bson.IO; ...@@ -5,13 +5,27 @@ using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers; using MongoDB.Bson.Serialization.Serializers;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection;
namespace ETModel namespace ETModel
{ {
public static class MongoHelper public static class MongoHelper
{ {
public static void Init() static MongoHelper()
{ {
Type bsonClassMap = typeof(BsonClassMap);
MethodInfo methodInfo = bsonClassMap.GetMethod("RegisterClassMap", new Type[] { });
Type[] types = typeof(Game).Assembly.GetTypes();
foreach (Type type in types)
{
if (!type.IsSubclassOf(typeof(Component)))
{
continue;
}
methodInfo.MakeGenericMethod(type).Invoke(null, null);
}
BsonSerializer.RegisterSerializer(new EnumSerializer<NumericType>(BsonType.String)); BsonSerializer.RegisterSerializer(new EnumSerializer<NumericType>(BsonType.String));
} }
......
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
{
[BsonKnownTypes(typeof(AConfigComponent))]
[BsonKnownTypes(typeof(ComponentWithId))]
public partial class Component
{
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 7f862da8d48947748af29b67dcda2dd2
timeCreated: 1512358818
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
{
[BsonKnownTypes(typeof(Entity))]
public partial class ComponentWithId
{
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 580a3c3b48fdd5b438d5e185e407db41
timeCreated: 1521513816
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
{
public partial class Entity
{
}
}
fileFormatVersion: 2
guid: cc5bfaad42962324d8fa891fea5efba7
timeCreated: 1512358818
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using MongoDB.Bson.Serialization.Attributes; namespace ETModel
namespace ETModel
{ {
/// <summary> /// <summary>
/// 每个Config的基类 /// 每个Config的基类
/// </summary> /// </summary>
[BsonKnownTypes(typeof(ClientConfig))]
[BsonKnownTypes(typeof(InnerConfig))]
[BsonKnownTypes(typeof(OuterConfig))]
[BsonKnownTypes(typeof(HttpConfig))]
[BsonKnownTypes(typeof(DBConfig))]
[BsonKnownTypes(typeof(RunServerConfig))]
public abstract class AConfigComponent: Component, ISerializeToEntity public abstract class AConfigComponent: Component, ISerializeToEntity
{ {
} }
......
...@@ -12,12 +12,15 @@ ...@@ -12,12 +12,15 @@
<ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> <ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier> <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<TargetFrameworkProfile></TargetFrameworkProfile> <TargetFrameworkProfile>
<CompilerResponseFile></CompilerResponseFile> </TargetFrameworkProfile>
<CompilerResponseFile>
</CompilerResponseFile>
<UnityProjectType>Game:1</UnityProjectType> <UnityProjectType>Game:1</UnityProjectType>
<UnityBuildTarget>Android:13</UnityBuildTarget> <UnityBuildTarget>Android:13</UnityBuildTarget>
<UnityVersion>2017.1.3p2</UnityVersion> <UnityVersion>2017.1.3p2</UnityVersion>
<RootNamespace></RootNamespace> <RootNamespace>
</RootNamespace>
<LangVersion>6</LangVersion> <LangVersion>6</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
...@@ -146,12 +149,9 @@ ...@@ -146,12 +149,9 @@
<Compile Include="Assets\Scripts\Base\Math\VRect.cs" /> <Compile Include="Assets\Scripts\Base\Math\VRect.cs" />
<Compile Include="Assets\Scripts\Base\MultiMap.cs" /> <Compile Include="Assets\Scripts\Base\MultiMap.cs" />
<Compile Include="Assets\Scripts\Base\Object\Component.cs" /> <Compile Include="Assets\Scripts\Base\Object\Component.cs" />
<Compile Include="Assets\Scripts\Base\Object\ComponentAttribute.cs" />
<Compile Include="Assets\Scripts\Base\Object\ComponentFactory.cs" /> <Compile Include="Assets\Scripts\Base\Object\ComponentFactory.cs" />
<Compile Include="Assets\Scripts\Base\Object\ComponentWithId.cs" /> <Compile Include="Assets\Scripts\Base\Object\ComponentWithId.cs" />
<Compile Include="Assets\Scripts\Base\Object\ComponentWithIdAttribute.cs" />
<Compile Include="Assets\Scripts\Base\Object\Entity.cs" /> <Compile Include="Assets\Scripts\Base\Object\Entity.cs" />
<Compile Include="Assets\Scripts\Base\Object\EntityAttribute.cs" />
<Compile Include="Assets\Scripts\Base\Object\EntityEventAttribute.cs" /> <Compile Include="Assets\Scripts\Base\Object\EntityEventAttribute.cs" />
<Compile Include="Assets\Scripts\Base\Object\EntityType.cs" /> <Compile Include="Assets\Scripts\Base\Object\EntityType.cs" />
<Compile Include="Assets\Scripts\Base\Object\EventProxy.cs" /> <Compile Include="Assets\Scripts\Base\Object\EventProxy.cs" />
...@@ -774,4 +774,4 @@ ...@@ -774,4 +774,4 @@
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="GenerateTargetFrameworkMonikerAttribute" /> <Target Name="GenerateTargetFrameworkMonikerAttribute" />
</Project> </Project>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册