Просмотр исходного кода

配置文件相关类调试OK,配置文件使用mongodb的json格式,既可作为文件存放也可放到mongodb数据库中,格式一致,非常方便使用

tanghai 11 лет назад
Родитель
Сommit
69a8e2a3be
31 измененных файлов с 204 добавлено и 251 удалено
  1. 0 15
      CSharp/CSharp.sln
  2. 6 0
      CSharp/Config/BuffConfig/1.json
  3. 2 1
      CSharp/Config/GlobalConfig/1.json
  4. 4 1
      CSharp/Game/BehaviorTree/BehaviorTree.csproj
  5. 1 0
      CSharp/Game/BehaviorTree/BehaviorTreeFactory.cs
  6. 1 0
      CSharp/Game/BehaviorTree/Node.cs
  7. 3 1
      CSharp/Game/BehaviorTree/Not.cs
  8. 3 1
      CSharp/Game/BehaviorTree/Selector.cs
  9. 3 1
      CSharp/Game/BehaviorTree/Sequence.cs
  10. 14 1
      CSharp/Game/Model/Buff.cs
  11. 19 25
      CSharp/Game/Model/BuffComponent.cs
  12. 24 0
      CSharp/Game/Model/BuffConfig.cs
  13. 8 8
      CSharp/Game/Model/GameObjectManager.cs
  14. 3 3
      CSharp/Game/Model/GlobalConfig.cs
  15. 4 0
      CSharp/Game/Model/Model.csproj
  16. 2 4
      CSharp/Game/Model/NodeConfig.cs
  17. 2 3
      CSharp/Game/Model/World.cs
  18. 2 2
      CSharp/Game/MongoDBTest/MongoDBTest.cs
  19. 0 39
      CSharp/Game/World/Properties/AssemblyInfo.cs
  20. 0 63
      CSharp/Game/World/World.csproj
  21. 4 1
      CSharp/Platform/Common/Base/Component.cs
  22. 17 3
      CSharp/Platform/Common/Base/Entity.cs
  23. 4 4
      CSharp/Platform/Common/Base/Object.cs
  24. 1 1
      CSharp/Platform/Common/Common.csproj
  25. 11 11
      CSharp/Platform/Common/Config/ACategory.cs
  26. 19 0
      CSharp/Platform/Common/Config/AConfig.cs
  27. 41 14
      CSharp/Platform/Common/Config/ConfigManager.cs
  28. 3 2
      CSharp/Platform/Common/Config/ICategory.cs
  29. 0 7
      CSharp/Platform/Common/Config/IConfig.cs
  30. 3 1
      CSharp/Platform/ENetTest/ENetTest.csproj
  31. 0 39
      CSharp/Platform/ENetTest/Properties/AssemblyInfo.cs

+ 0 - 15
CSharp/CSharp.sln

@@ -32,8 +32,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Game", "Game", "{D0CC1FF4-2
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BehaviorTree", "Game\BehaviorTree\BehaviorTree.csproj", "{C4E7A34A-095C-4983-AB63-FC2D20CD6824}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "World", "Game\World\World.csproj", "{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}"
-EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ENetExe", "Platform\ENetExe\ENetExe.csproj", "{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}"
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2814EB55-E1C2-4751-AC14-94116DA9D22B}"
@@ -140,18 +138,6 @@ Global
 		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Release|Mixed Platforms.Build.0 = Release|Any CPU
 		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Release|Win32.ActiveCfg = Release|Any CPU
 		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Release|x86.ActiveCfg = Release|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Debug|Win32.ActiveCfg = Debug|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Debug|x86.ActiveCfg = Debug|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Release|Any CPU.Build.0 = Release|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Release|Mixed Platforms.Build.0 = Release|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Release|Win32.ActiveCfg = Release|Any CPU
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Release|x86.ActiveCfg = Release|Any CPU
 		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -260,7 +246,6 @@ Global
 		{D0B4CFAC-A368-4742-9863-68776CFA9938} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{901A8E5C-C4C6-4C3C-8E18-068D75119F5D} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{C4E7A34A-095C-4983-AB63-FC2D20CD6824} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
-		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{B42D431A-3A54-4649-942A-C5356D7F9FBC} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{F176D1FA-63E5-4B89-9A03-D44CCCCC069A} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}

+ 6 - 0
CSharp/Config/BuffConfig/1.json

@@ -0,0 +1,6 @@
+{ 
+	"_id" : 1,
+	"Type" : 0,
+	"Name" : "yun",
+	"Duration" : 1500,
+}

+ 2 - 1
CSharp/Config/GlobalConfig/1.json

@@ -1,3 +1,4 @@
-{ 
+{
+	"_id": 1
 	"Type" : 1 
 }

+ 4 - 1
CSharp/Game/BehaviorTree/BehaviorTree.csproj

@@ -46,7 +46,6 @@
   <ItemGroup>
     <Compile Include="BehaviorTree.cs" />
     <Compile Include="BlackBoard.cs" />
-    <Compile Include="NodeConfig.cs" />
     <Compile Include="Node.cs" />
     <Compile Include="BehaviorTreeFactory.cs" />
     <Compile Include="NodeAttribute.cs" />
@@ -60,6 +59,10 @@
       <Project>{19f8f043-1f99-4550-99df-dea5c7d77e55}</Project>
       <Name>Common</Name>
     </ProjectReference>
+    <ProjectReference Include="..\Model\Model.csproj">
+      <Project>{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE}</Project>
+      <Name>Model</Name>
+    </ProjectReference>
   </ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />

+ 1 - 0
CSharp/Game/BehaviorTree/BehaviorTreeFactory.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Reflection;
+using Model;
 
 namespace BehaviorTree
 {

+ 1 - 0
CSharp/Game/BehaviorTree/Node.cs

@@ -1,4 +1,5 @@
 using System.Collections.Generic;
+using Model;
 
 namespace BehaviorTree
 {

+ 3 - 1
CSharp/Game/BehaviorTree/Not.cs

@@ -1,4 +1,6 @@
-namespace BehaviorTree
+using Model;
+
+namespace BehaviorTree
 {
     [Node(NodeType.Not)]
     public class Not: Node

+ 3 - 1
CSharp/Game/BehaviorTree/Selector.cs

@@ -1,4 +1,6 @@
-namespace BehaviorTree
+using Model;
+
+namespace BehaviorTree
 {
     [Node(NodeType.Selector)]
     public class Selector: Node

+ 3 - 1
CSharp/Game/BehaviorTree/Sequence.cs

@@ -1,4 +1,6 @@
-namespace BehaviorTree
+using Model;
+
+namespace BehaviorTree
 {
     [Node(NodeType.Sequence)]
     internal class Sequence: Node

+ 14 - 1
CSharp/Game/Model/Buff.cs

@@ -4,6 +4,19 @@ namespace Model
 {
     public class Buff: Object
     {
-        public BuffType Type { get; set; }
+        private int ConfigId { get; set; }
+
+        public Buff(int configId)
+        {
+            this.ConfigId = configId;
+        }
+
+        public BuffConfig Config
+        {
+            get
+            {
+                return World.Instance.ConfigManager.Get<BuffConfig>(this.ConfigId);
+            }
+        }
     }
 }

+ 19 - 25
CSharp/Game/Model/BuffComponent.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using Common.Base;
 using MongoDB.Bson;
 using MongoDB.Bson.Serialization.Attributes;
@@ -11,14 +12,14 @@ namespace Model
         [BsonElement]
         private HashSet<Buff> Buffs { get; set; }
 
-        private Dictionary<ObjectId, Buff> buffGuidDict { get; set; }
+        private Dictionary<ObjectId, Buff> buffIdDict { get; set; }
 
         private MultiMap<BuffType, Buff> buffTypeMMap { get; set; }
 
         public BuffComponent()
         {
             this.Buffs = new HashSet<Buff>();
-            this.buffGuidDict = new Dictionary<ObjectId, Buff>();
+            this.buffIdDict = new Dictionary<ObjectId, Buff>();
             this.buffTypeMMap = new MultiMap<BuffType, Buff>();
         }
 
@@ -26,43 +27,36 @@ namespace Model
         {
             foreach (var buff in this.Buffs)
             {
-                this.buffGuidDict.Add(buff.Guid, buff);
-                this.buffTypeMMap.Add(buff.Type, buff);
+                this.buffIdDict.Add(buff.Id, buff);
+                this.buffTypeMMap.Add(buff.Config.Type, buff);
             }
         }
 
-        public bool Add(Buff buff)
+        public void Add(Buff buff)
         {
             if (this.Buffs.Contains(buff))
             {
-                return false;
-            }
-
-            if (this.buffGuidDict.ContainsKey(buff.Guid))
-            {
-                return false;
+                throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
             }
 
-            if (this.buffTypeMMap.GetOne(buff.Type) != null)
+            if (this.buffIdDict.ContainsKey(buff.Id))
             {
-                return false;
+                throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
             }
 
             this.Buffs.Add(buff);
-            this.buffGuidDict.Add(buff.Guid, buff);
-            this.buffTypeMMap.Add(buff.Type, buff);
-
-            return true;
+            this.buffIdDict.Add(buff.Id, buff);
+            this.buffTypeMMap.Add(buff.Config.Type, buff);
         }
 
-        public Buff GetByGuid(ObjectId guid)
+        public Buff GetById(ObjectId id)
         {
-            if (!this.buffGuidDict.ContainsKey(guid))
+            if (!this.buffIdDict.ContainsKey(id))
             {
                 return null;
             }
 
-            return this.buffGuidDict[guid];
+            return this.buffIdDict[id];
         }
 
         public Buff GetOneByType(BuffType type)
@@ -83,15 +77,15 @@ namespace Model
             }
 
             this.Buffs.Remove(buff);
-            this.buffGuidDict.Remove(buff.Guid);
-            this.buffTypeMMap.Remove(buff.Type, buff);
+            this.buffIdDict.Remove(buff.Id);
+            this.buffTypeMMap.Remove(buff.Config.Type, buff);
 
             return true;
         }
 
-        public bool RemoveByGuid(ObjectId guid)
+        public bool RemoveById(ObjectId id)
         {
-            Buff buff = this.GetByGuid(guid);
+            Buff buff = this.GetById(id);
             return this.Remove(buff);
         }
 

+ 24 - 0
CSharp/Game/Model/BuffConfig.cs

@@ -0,0 +1,24 @@
+using System.Collections.Generic;
+using Common.Config;
+
+namespace Model
+{
+    public class BuffConfig: AConfig
+    {
+        public BuffType Type { get; set; }
+        public string Name { get; set; }
+        public long Duration { get; set; }
+        public int MaxStack { get; set; }
+        public List<int> Effects { get; set; }
+
+        public BuffConfig()
+        {
+            this.Effects = new List<int>();
+        }
+    }
+
+    [Config]
+    public class BuffCategory : ACategory<BuffConfig>
+    {
+    }
+}

+ 8 - 8
CSharp/Game/Model/GameObjectManager.cs

@@ -15,18 +15,18 @@ namespace Model
 
         public void Add(GameObject gameObject)
         {
-            this.gameObjects.Add(gameObject.Guid, gameObject);
+            this.gameObjects.Add(gameObject.Id, gameObject);
             if (!this.typeGameObjects.ContainsKey(gameObject.Type))
             {
                 this.typeGameObjects.Add(gameObject.Type, new Dictionary<ObjectId, GameObject>());
             }
-            this.typeGameObjects[gameObject.Type].Add(gameObject.Guid, gameObject);
+            this.typeGameObjects[gameObject.Type].Add(gameObject.Id, gameObject);
         }
 
-        public GameObject Get(ObjectId guid)
+        public GameObject Get(ObjectId id)
         {
             GameObject gameObject = null;
-            this.gameObjects.TryGetValue(guid, out gameObject);
+            this.gameObjects.TryGetValue(id, out gameObject);
             return gameObject;
         }
 
@@ -46,20 +46,20 @@ namespace Model
             {
                 throw new ArgumentNullException("gameObject");
             }
-            if (!this.gameObjects.Remove(gameObject.Guid))
+            if (!this.gameObjects.Remove(gameObject.Id))
             {
                 return false;
             }
-            if (!this.typeGameObjects[gameObject.Type].Remove(gameObject.Guid))
+            if (!this.typeGameObjects[gameObject.Type].Remove(gameObject.Id))
             {
                 return false;
             }
             return true;
         }
 
-        public bool Remove(ObjectId guid)
+        public bool Remove(ObjectId id)
         {
-            GameObject gameObject = this.Get(guid);
+            GameObject gameObject = this.Get(id);
             if (gameObject == null)
             {
                 return false;

+ 3 - 3
CSharp/Game/World/Config/GlobalConfig.cs → CSharp/Game/Model/GlobalConfig.cs

@@ -1,10 +1,10 @@
 using Common.Config;
 
-namespace World.Config
+namespace Model
 {
-    public class GlobalConfig: IConfig
+    public class GlobalConfig: AConfig
     {
-        public int Id { get; set; }
+        public int Type { get; set; }
     }
 
     [Config]

+ 4 - 0
CSharp/Game/Model/Model.csproj

@@ -47,9 +47,13 @@
   <ItemGroup>
     <Compile Include="Buff.cs" />
     <Compile Include="BuffComponent.cs" />
+    <Compile Include="BuffConfig.cs" />
     <Compile Include="BuffType.cs" />
     <Compile Include="GameObject.cs" />
     <Compile Include="GameObjectManager.cs" />
+    <Compile Include="GlobalConfig.cs" />
+    <Compile Include="NodeConfig.cs" />
+    <Compile Include="World.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Properties\" />

+ 2 - 4
CSharp/Game/BehaviorTree/NodeConfig.cs → CSharp/Game/Model/NodeConfig.cs

@@ -2,12 +2,10 @@
 using Common.Config;
 using MongoDB.Bson.Serialization.Attributes;
 
-namespace BehaviorTree
+namespace Model
 {
-    public class NodeConfig: IConfig
+    public class NodeConfig: AConfig
     {
-        public int Id { get; set; }
-
         [BsonIgnoreIfNull]
         public List<string> Args { get; set; }
 

+ 2 - 3
CSharp/Game/World/World.cs → CSharp/Game/Model/World.cs

@@ -1,13 +1,12 @@
 using Common.Config;
-using Model;
 
-namespace World
+namespace Model
 {
     public class World
     {
         private static readonly World instance = new World();
 
-        private readonly ConfigManager configManager = new ConfigManager();
+        private readonly ConfigManager configManager = ConfigManager.Instance;
 
         private readonly GameObjectManager gameObjectManager = new GameObjectManager();
 

+ 2 - 2
CSharp/Game/MongoDBTest/MongoDBTest.cs

@@ -21,12 +21,12 @@ namespace MongoDBTest
             var collection = database.GetCollection<GameObject>("GameObjects");
 
             GameObject player1 = GameObjectFactory.CreatePlayer();
-            player1.GetComponent<BuffComponent>().Add(new Buff());
+            player1.GetComponent<BuffComponent>().Add(new Buff(1));
             player1["hp"] = 10;
 
             collection.Insert(player1);
 
-            var query = Query<GameObject>.EQ(e => e.Guid, player1.Guid);
+            var query = Query<GameObject>.EQ(e => e.Id, player1.Id);
             GameObject player2 = collection.FindOne(query);
 
             Console.WriteLine(MongoHelper.ToJson(player2));

+ 0 - 39
CSharp/Game/World/Properties/AssemblyInfo.cs

@@ -1,39 +0,0 @@
-using System.Reflection;
-using System.Runtime.InteropServices;
-
-// 有关程序集的常规信息通过以下
-// 特性集控制。更改这些特性值可修改
-// 与程序集关联的信息。
-
-[assembly: AssemblyTitle("Component")]
-[assembly: AssemblyDescription("")]
-[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("")]
-[assembly: AssemblyProduct("Component")]
-[assembly: AssemblyCopyright("Copyright ©  2013")]
-[assembly: AssemblyTrademark("")]
-[assembly: AssemblyCulture("")]
-
-// 将 ComVisible 设置为 false 使此程序集中的类型
-// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
-// 则将该类型上的 ComVisible 特性设置为 true。
-
-[assembly: ComVisible(false)]
-
-// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
-
-[assembly: Guid("55cc5b17-d8c8-4c34-922c-47afac13d174")]
-
-// 程序集的版本信息由下面四个值组成:
-//
-//      主版本
-//      次版本 
-//      生成号
-//      修订号
-//
-// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
-// 方法是按如下所示使用“*”:
-// [assembly: AssemblyVersion("1.0.*")]
-
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]

+ 0 - 63
CSharp/Game/World/World.csproj

@@ -1,63 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
-  <PropertyGroup>
-    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
-    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProjectGuid>{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}</ProjectGuid>
-    <OutputType>Library</OutputType>
-    <AppDesignerFolder>Properties</AppDesignerFolder>
-    <RootNamespace>World</RootNamespace>
-    <AssemblyName>World</AssemblyName>
-    <TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
-    <FileAlignment>512</FileAlignment>
-    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
-    <RestorePackages>true</RestorePackages>
-    <TargetFrameworkProfile />
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
-    <DebugSymbols>true</DebugSymbols>
-    <DebugType>full</DebugType>
-    <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\Debug\</OutputPath>
-    <DefineConstants>DEBUG;TRACE</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
-    <DebugType>pdbonly</DebugType>
-    <Optimize>true</Optimize>
-    <OutputPath>..\..\Bin\Release\</OutputPath>
-    <DefineConstants>TRACE</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-  </PropertyGroup>
-  <ItemGroup>
-    <Reference Include="System" />
-    <Reference Include="System.Core" />
-  </ItemGroup>
-  <ItemGroup>
-    <Compile Include="Config\GlobalConfig.cs" />
-    <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="World.cs" />
-  </ItemGroup>
-  <ItemGroup>
-    <ProjectReference Include="..\..\Platform\Common\Common.csproj">
-      <Project>{19f8f043-1f99-4550-99df-dea5c7d77e55}</Project>
-      <Name>Common</Name>
-    </ProjectReference>
-    <ProjectReference Include="..\Model\Model.csproj">
-      <Project>{0fa529d1-d0a9-4a8e-90f5-117ce80f2ede}</Project>
-      <Name>Model</Name>
-    </ProjectReference>
-  </ItemGroup>
-  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
-  <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
-  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
-       Other similar extension points exist, see Microsoft.Common.targets.
-  <Target Name="BeforeBuild">
-  </Target>
-  <Target Name="AfterBuild">
-  </Target>
-  -->
-</Project>

+ 4 - 1
CSharp/Platform/Common/Base/Component.cs

@@ -3,6 +3,9 @@ using MongoDB.Bson.Serialization.Attributes;
 
 namespace Common.Base
 {
+    /// <summary>
+    /// Component的Id与Owner Entity Id一样
+    /// </summary>
     public abstract class Component : Object, ISupportInitialize
     {
         private Entity owner;
@@ -17,7 +20,7 @@ namespace Common.Base
             set
             {
                 this.owner = value;
-                this.Guid = this.owner.Guid;
+                this.Id = this.owner.Id;
             }
         }
 

+ 17 - 3
CSharp/Platform/Common/Base/Entity.cs

@@ -21,6 +21,12 @@ namespace Common.Base
 
         public void AddComponent<T>() where T : Component, new()
         {
+            if (this.ComponentDict.ContainsKey(typeof (T)))
+            {
+                throw new Exception(
+                    string.Format("AddComponent, component already exist, id: {0}, component: {1}", 
+                    this.Id, typeof(T).Name));
+            }
             T t = new T { Owner = this };
             this.Components.Add(t);
             this.ComponentDict.Add(typeof (T), t);
@@ -29,7 +35,13 @@ namespace Common.Base
         public void RemoveComponent<T>() where T : Component
         {
             Component t;
-            this.ComponentDict.TryGetValue(typeof(T), out t);
+            if (!this.ComponentDict.TryGetValue(typeof (T), out t))
+            {
+                throw new Exception(
+                    string.Format("RemoveComponent, component not exist, id: {0}, component: {1}",
+                    this.Id, typeof(T).Name));
+            }
+            
             this.Components.Remove(t);
             this.ComponentDict.Remove(typeof(T));
         }
@@ -39,7 +51,9 @@ namespace Common.Base
             Component t;
             if (!this.ComponentDict.TryGetValue(typeof (T), out t))
             {
-                return null;
+                throw new Exception(
+                    string.Format("GetComponent, component not exist, id: {0}, component: {1}",
+                    this.Id, typeof(T).Name));
             }
             return (T) t;
         }
@@ -57,8 +71,8 @@ namespace Common.Base
         {
             foreach (Component component in this.Components)
             {
-                this.ComponentDict.Add(component.GetType(), component);
                 component.Owner = this;
+                this.ComponentDict.Add(component.GetType(), component);
             }
         }
     }

+ 4 - 4
CSharp/Platform/Common/Base/Object.cs

@@ -7,7 +7,7 @@ namespace Common.Base
     public abstract class Object
     {
         [BsonId]
-        public ObjectId Guid { get; protected set; }
+        public ObjectId Id { get; protected set; }
 
         [BsonElement]
         [BsonIgnoreIfNull]
@@ -15,12 +15,12 @@ namespace Common.Base
 
         protected Object()
         {
-            this.Guid = ObjectId.GenerateNewId();
+            this.Id = ObjectId.GenerateNewId();
         }
 
-        protected Object(ObjectId guid)
+        protected Object(ObjectId id)
         {
-            this.Guid = guid;
+            this.Id = id;
         }
 
         public object this[string key]

+ 1 - 1
CSharp/Platform/Common/Common.csproj

@@ -70,7 +70,7 @@
     <Compile Include="Config\ConfigAttribute.cs" />
     <Compile Include="Config\ACategory.cs" />
     <Compile Include="Config\ConfigManager.cs" />
-    <Compile Include="Config\IConfig.cs" />
+    <Compile Include="Config\AConfig.cs" />
     <Compile Include="Helper\BigIntegerHelper.cs" />
     <Compile Include="Helper\ByteHelper.cs" />
     <Compile Include="Helper\EnumHelper.cs" />

+ 11 - 11
CSharp/Platform/Common/Config/ACategory.cs

@@ -6,13 +6,13 @@ using Common.Helper;
 
 namespace Common.Config
 {
-    public abstract class ACategory<T>: ICategory where T : IConfig
+    public abstract class ACategory<T>: ICategory where T : AConfig
     {
         protected readonly Dictionary<int, T> dict = new Dictionary<int, T>();
 
-        public void BeginInit()
+        public virtual void BeginInit()
         {
-            string path = Path.Combine(@"./Config/", this.Name);
+            string path = Path.Combine(@"../../Config/", typeof(T).Name);
 
             if (!Directory.Exists(path))
             {
@@ -26,23 +26,23 @@ namespace Common.Config
             }
         }
 
-        public void EndInit()
-        {
-        }
-
-        public T this[int type]
+        public Type ConfigType
         {
             get
             {
-                return this.dict[type];
+                return typeof (T);
             }
         }
 
-        public string Name
+        public virtual void EndInit()
+        {
+        }
+
+        public T this[int type]
         {
             get
             {
-                return typeof (T).Name;
+                return this.dict[type];
             }
         }
 

+ 19 - 0
CSharp/Platform/Common/Config/AConfig.cs

@@ -0,0 +1,19 @@
+using System.ComponentModel;
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Common.Config
+{
+    public abstract class AConfig: ISupportInitialize
+    {
+        [BsonId]
+        public int Id { get; set; }
+
+        public virtual void BeginInit()
+        {
+        }
+
+        public virtual void EndInit()
+        {
+        }
+    }
+}

+ 41 - 14
CSharp/Platform/Common/Config/ConfigManager.cs

@@ -1,20 +1,36 @@
 using System;
 using System.Collections.Generic;
 using System.Reflection;
+using Common.Logger;
 
 namespace Common.Config
 {
     public class ConfigManager
     {
-        public Dictionary<string, object> allConfig;
+        private static ConfigManager instance = new ConfigManager();
+
+        public static ConfigManager Instance
+        {
+            get
+            {
+                return instance;
+            }
+        }
+
+        public Dictionary<Type, ICategory> allConfig;
+
+        private ConfigManager()
+        {
+        }
 
         public void Load(Assembly assembly)
         {
-            var localAllConfig = new Dictionary<string, object>();
+            var localAllConfig = new Dictionary<Type, ICategory>();
             Type[] types = assembly.GetTypes();
-            foreach (var type in types)
+            foreach (Type type in types)
             {
                 object[] attrs = type.GetCustomAttributes(typeof (ConfigAttribute), false);
+                Log.Debug(type.Name);
                 if (attrs.Length == 0)
                 {
                     continue;
@@ -25,34 +41,45 @@ namespace Common.Config
                 ICategory iCategory = obj as ICategory;
                 if (iCategory == null)
                 {
-                    throw new Exception(string.Format("class: {0} not inherit from ACategory",
-                            type.Name));
+                    throw new Exception(
+                        string.Format("class: {0} not inherit from ACategory", type.Name));
                 }
                 iCategory.BeginInit();
                 iCategory.EndInit();
 
-                localAllConfig[type.Name] = obj;
+                localAllConfig[iCategory.ConfigType] = iCategory;
             }
             this.allConfig = localAllConfig;
         }
 
-        public T Get<T>(int type) where T : IConfig
+        public T Get<T>(int id) where T : AConfig
         {
-            var configCategory = (ACategory<T>) this.allConfig[typeof (T).Name];
-            return configCategory[type];
+            Type type = typeof (T);
+            ICategory configCategory;
+            if (!this.allConfig.TryGetValue(type, out configCategory))
+            {
+                throw new KeyNotFoundException(string.Format("ConfigManager not found key: {0}", type.FullName));
+            }
+            return ((ACategory<T>) configCategory)[id];
         }
 
-        public T[] GetAll<T>() where T : IConfig
+        public T[] GetAll<T>() where T : AConfig
         {
-            var configCategory = (ACategory<T>) this.allConfig[typeof (T).Name];
-            return configCategory.GetAll();
+            Type type = typeof(T);
+            ICategory configCategory;
+            if (!this.allConfig.TryGetValue(type, out configCategory))
+            {
+                throw new KeyNotFoundException(string.Format("ConfigManager not found key: {0}", type.FullName));
+            }
+            return ((ACategory<T>)configCategory).GetAll();
         }
 
         public T GetCategory<T>() where T : class, ICategory, new()
         {
             T t = new T();
-            object category;
-            bool ret = this.allConfig.TryGetValue(t.Name, out category);
+            Type type = t.ConfigType;
+            ICategory category;
+            bool ret = this.allConfig.TryGetValue(type, out category);
             return ret? (T) category : null;
         }
     }

+ 3 - 2
CSharp/Platform/Common/Config/ICategory.cs

@@ -1,9 +1,10 @@
-using System.ComponentModel;
+using System;
+using System.ComponentModel;
 
 namespace Common.Config
 {
     public interface ICategory: ISupportInitialize
     {
-        string Name { get; }
+        Type ConfigType { get; }
     }
 }

+ 0 - 7
CSharp/Platform/Common/Config/IConfig.cs

@@ -1,7 +0,0 @@
-namespace Common.Config
-{
-    public interface IConfig
-    {
-        int Id { get; }
-    }
-}

+ 3 - 1
CSharp/Platform/ENetTest/ENetTest.csproj

@@ -55,7 +55,6 @@
   </Choose>
   <ItemGroup>
     <Compile Include="ENetClientServerTest.cs" />
-    <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\Common\Common.csproj">
@@ -67,6 +66,9 @@
       <Name>ENet</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <Folder Include="Properties\" />
+  </ItemGroup>
   <Choose>
     <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
       <ItemGroup>

+ 0 - 39
CSharp/Platform/ENetTest/Properties/AssemblyInfo.cs

@@ -1,39 +0,0 @@
-using System.Reflection;
-using System.Runtime.InteropServices;
-
-// 有关程序集的常规信息通过以下特性集 
-// 控制。更改这些特性值可修改
-// 与程序集关联的信息。
-
-[assembly: AssemblyTitle("ENetCSTest")]
-[assembly: AssemblyDescription("")]
-[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("Microsoft")]
-[assembly: AssemblyProduct("ENetCSTest")]
-[assembly: AssemblyCopyright("Copyright © Microsoft 2012")]
-[assembly: AssemblyTrademark("")]
-[assembly: AssemblyCulture("")]
-
-// 将 ComVisible 设置为 false 会使此程序集中的类型 
-// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
-// 请将该类型上的 ComVisible 特性设置为 true。
-
-[assembly: ComVisible(false)]
-
-// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
-
-[assembly: Guid("b52f4804-3f6e-4268-810a-08fe8ca12bd0")]
-
-// 程序集的版本信息由以下四个值组成:
-//
-//      主版本
-//      次版本
-//      生成号
-//      修订号
-//
-// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
-// 方法是按如下所示使用“*”:
-// [assembly: AssemblyVersion("1.0.*")]
-
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]