Explorar o código

1.行为树工厂,根据NodeAttribute注册节点
2.增加EventTrigger类,用于事件触发,消息分发

tanghai %!s(int64=11) %!d(string=hai) anos
pai
achega
4876dab15a
Modificáronse 37 ficheiros con 172 adicións e 468 borrados
  1. 1 16
      CSharp/CSharp.sln
  2. 7 1
      CSharp/Game/BehaviorTree/BehaviorTree.csproj
  3. 29 14
      CSharp/Game/BehaviorTree/BehaviorTreeFactory.cs
  4. 1 1
      CSharp/Game/BehaviorTree/BlackBoard.cs
  5. 6 1
      CSharp/Game/BehaviorTree/Node.cs
  6. 17 0
      CSharp/Game/BehaviorTree/NodeAttribute.cs
  7. 5 5
      CSharp/Game/BehaviorTree/NodeConfig.cs
  8. 9 0
      CSharp/Game/BehaviorTree/NodeType.cs
  9. 3 11
      CSharp/Game/BehaviorTree/Not.cs
  10. 2 2
      CSharp/Game/BehaviorTree/Selector.cs
  11. 2 2
      CSharp/Game/BehaviorTree/Sequence.cs
  12. 3 1
      CSharp/Game/Component/Buff.cs
  13. 0 11
      CSharp/Game/Component/Component.csproj
  14. 0 11
      CSharp/Game/Component/EventAttribute.cs
  15. 0 15
      CSharp/Game/Component/EventType.cs
  16. 3 1
      CSharp/Game/Component/GameObject.cs
  17. 0 11
      CSharp/Game/Component/HandlerAttribute.cs
  18. 0 7
      CSharp/Game/Component/IEvent.cs
  19. 0 7
      CSharp/Game/Component/IHandler.cs
  20. 0 7
      CSharp/Game/Component/ILogic.cs
  21. 0 139
      CSharp/Game/Component/LogicManager.cs
  22. 0 19
      CSharp/Game/Component/Message.cs
  23. 0 10
      CSharp/Game/Component/MessageAttribute.cs
  24. 0 6
      CSharp/Game/Component/MessageEnv.cs
  25. 0 14
      CSharp/Game/Logic/Event/BeforeLoginWorldEvent.cs
  26. 0 14
      CSharp/Game/Logic/Event/BeforeUseItemEvent.cs
  27. 0 21
      CSharp/Game/Logic/Handler/ChatHandler.cs
  28. 0 15
      CSharp/Game/Logic/Handler/LoginWorldHandler.cs
  29. 0 24
      CSharp/Game/Logic/Handler/ReloadHandler.cs
  30. 0 69
      CSharp/Game/Logic/Logic.csproj
  31. 1 11
      CSharp/Game/World/World.cs
  32. 6 0
      CSharp/Platform/Common/Common.csproj
  33. 6 0
      CSharp/Platform/Common/Component/Env.cs
  34. 54 0
      CSharp/Platform/Common/Component/EventTrigger.cs
  35. 7 0
      CSharp/Platform/Common/Component/IEvent.cs
  36. 8 0
      CSharp/Platform/Common/Component/IEventAttribute.cs
  37. 2 2
      CSharp/Platform/Common/Component/Object.cs

+ 1 - 16
CSharp/CSharp.sln

@@ -1,7 +1,7 @@
 
 
 Microsoft Visual Studio Solution File, Format Version 12.00
 Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio 2013
 # Visual Studio 2013
-VisualStudioVersion = 12.0.30501.0
+VisualStudioVersion = 12.0.30723.0
 MinimumVisualStudioVersion = 10.0.40219.1
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}"
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}"
 EndProject
 EndProject
@@ -34,8 +34,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BehaviorTree", "Game\Behavi
 EndProject
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "World", "Game\World\World.csproj", "{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}"
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "World", "Game\World\World.csproj", "{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}"
 EndProject
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logic", "Game\Logic\Logic.csproj", "{CB5BCF0A-4741-477A-94C6-49ECA782555F}"
-EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Component", "Game\Component\Component.csproj", "{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE}"
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Component", "Game\Component\Component.csproj", "{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE}"
 EndProject
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tree", "App\Modules\Tree\Tree.csproj", "{6CD185D1-08E0-4729-A999-2D5B57BA8193}"
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tree", "App\Modules\Tree\Tree.csproj", "{6CD185D1-08E0-4729-A999-2D5B57BA8193}"
@@ -150,18 +148,6 @@ Global
 		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Release|Mixed Platforms.Build.0 = 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|Win32.ActiveCfg = Release|Any CPU
 		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Release|x86.ActiveCfg = Release|Any CPU
 		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0}.Release|x86.ActiveCfg = Release|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Debug|Win32.ActiveCfg = Debug|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Debug|x86.ActiveCfg = Debug|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Release|Any CPU.Build.0 = Release|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Release|Mixed Platforms.Build.0 = Release|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Release|Win32.ActiveCfg = Release|Any CPU
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F}.Release|x86.ActiveCfg = Release|Any CPU
 		{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
 		{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -247,7 +233,6 @@ Global
 		{901A8E5C-C4C6-4C3C-8E18-068D75119F5D} = {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}
 		{C4E7A34A-095C-4983-AB63-FC2D20CD6824} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 		{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
-		{CB5BCF0A-4741-477A-94C6-49ECA782555F} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 		{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 		{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 		{6CD185D1-08E0-4729-A999-2D5B57BA8193} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
 		{6CD185D1-08E0-4729-A999-2D5B57BA8193} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
 		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}

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

@@ -46,15 +46,21 @@
   <ItemGroup>
   <ItemGroup>
     <Compile Include="BehaviorTree.cs" />
     <Compile Include="BehaviorTree.cs" />
     <Compile Include="BlackBoard.cs" />
     <Compile Include="BlackBoard.cs" />
-    <Compile Include="Config.cs" />
+    <Compile Include="NodeConfig.cs" />
     <Compile Include="Node.cs" />
     <Compile Include="Node.cs" />
     <Compile Include="BehaviorTreeFactory.cs" />
     <Compile Include="BehaviorTreeFactory.cs" />
+    <Compile Include="NodeAttribute.cs" />
+    <Compile Include="NodeType.cs" />
     <Compile Include="Not.cs" />
     <Compile Include="Not.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Selector.cs" />
     <Compile Include="Selector.cs" />
     <Compile Include="Sequence.cs" />
     <Compile Include="Sequence.cs" />
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="..\..\Platform\Common\Common.csproj">
+      <Project>{19f8f043-1f99-4550-99df-dea5c7d77e55}</Project>
+      <Name>Common</Name>
+    </ProjectReference>
     <ProjectReference Include="..\Component\Component.csproj">
     <ProjectReference Include="..\Component\Component.csproj">
       <Project>{0fa529d1-d0a9-4a8e-90f5-117ce80f2ede}</Project>
       <Project>{0fa529d1-d0a9-4a8e-90f5-117ce80f2ede}</Project>
       <Name>Component</Name>
       <Name>Component</Name>

+ 29 - 14
CSharp/Game/BehaviorTree/BehaviorTreeFactory.cs

@@ -5,32 +5,47 @@ namespace BehaviorTree
 {
 {
     public class BehaviorTreeFactory
     public class BehaviorTreeFactory
     {
     {
-        private readonly Dictionary<string, Func<Config, Node>> dictionary =
-                new Dictionary<string, Func<Config, Node>>();
+        private static readonly BehaviorTreeFactory instance = new BehaviorTreeFactory();
 
 
-        public BehaviorTreeFactory()
+        public static BehaviorTreeFactory Instance
         {
         {
-            this.dictionary.Add("selector", config => new Selector(config));
-            this.dictionary.Add("sequence", config => new Sequence(config));
-            this.dictionary.Add("not", config => new Not(config));
+            get
+            {
+                return instance;
+            }
         }
         }
 
 
-        public void Register(string name, Func<Config, Node> action)
+        private readonly Dictionary<NodeType, Func<NodeConfig, Node>> dictionary =
+                new Dictionary<NodeType, Func<NodeConfig, Node>>();
+
+        private BehaviorTreeFactory()
         {
         {
-            this.dictionary.Add(name, action);
+            var assembly = typeof(BehaviorTreeFactory).Assembly;
+            Type[] types = assembly.GetTypes();
+            foreach (var type in types)
+            {
+                object[] attrs = type.GetCustomAttributes(typeof(NodeAttribute), false);
+                if (attrs.Length == 0)
+                {
+                    continue;
+                }
+                NodeAttribute attribute = (NodeAttribute)attrs[0];
+
+                dictionary.Add(attribute.NodeType, config => (Node)Activator.CreateInstance(attribute.ClassType, new object[] { config }));
+            }
         }
         }
 
 
-        private Node CreateNode(Config config)
+        private Node CreateNode(NodeConfig config)
         {
         {
-            if (!this.dictionary.ContainsKey(config.Name))
+            if (!this.dictionary.ContainsKey((NodeType) config.Type))
             {
             {
                 throw new KeyNotFoundException(string.Format("CreateNode cannot found: {0}",
                 throw new KeyNotFoundException(string.Format("CreateNode cannot found: {0}",
-                        config.Name));
+                        config.Type));
             }
             }
-            return this.dictionary[config.Name](config);
+            return this.dictionary[(NodeType) config.Type](config);
         }
         }
 
 
-        private Node CreateTreeNode(Config config)
+        private Node CreateTreeNode(NodeConfig config)
         {
         {
             var node = this.CreateNode(config);
             var node = this.CreateNode(config);
             if (config.SubConfigs == null)
             if (config.SubConfigs == null)
@@ -46,7 +61,7 @@ namespace BehaviorTree
             return node;
             return node;
         }
         }
 
 
-        public BehaviorTree CreateTree(Config config)
+        public BehaviorTree CreateTree(NodeConfig config)
         {
         {
             var node = this.CreateTreeNode(config);
             var node = this.CreateTreeNode(config);
             return new BehaviorTree(node);
             return new BehaviorTree(node);

+ 1 - 1
CSharp/Game/BehaviorTree/BlackBoard.cs

@@ -1,4 +1,4 @@
-using Component;
+using Common.Component;
 
 
 namespace BehaviorTree
 namespace BehaviorTree
 {
 {

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

@@ -4,10 +4,15 @@ namespace BehaviorTree
 {
 {
     public abstract class Node
     public abstract class Node
     {
     {
-        public string Name { get; protected set; }
+        public NodeConfig Config { get; private set; }
 
 
         protected readonly List<Node> children = new List<Node>();
         protected readonly List<Node> children = new List<Node>();
 
 
+        protected Node(NodeConfig config)
+        {
+            this.Config = config;
+        }
+
         public void AddChild(Node child)
         public void AddChild(Node child)
         {
         {
             this.children.Add(child);
             this.children.Add(child);

+ 17 - 0
CSharp/Game/BehaviorTree/NodeAttribute.cs

@@ -0,0 +1,17 @@
+using System;
+
+namespace BehaviorTree
+{
+    [AttributeUsage(AttributeTargets.Class)]
+    public class NodeAttribute : Attribute
+    {
+        public NodeType NodeType { get; set; }
+        public Type ClassType { get; set; }
+
+        public NodeAttribute(NodeType nodeType, Type classType)
+        {
+            this.NodeType = nodeType;
+            this.ClassType = classType;
+        }
+    }
+}

+ 5 - 5
CSharp/Game/BehaviorTree/Config.cs → CSharp/Game/BehaviorTree/NodeConfig.cs

@@ -3,17 +3,17 @@ using MongoDB.Bson.Serialization.Attributes;
 
 
 namespace BehaviorTree
 namespace BehaviorTree
 {
 {
-    public class Config
+    public class NodeConfig
     {
     {
         public uint Id { get; set; }
         public uint Id { get; set; }
 
 
-        public string Name { get; set; }
+        public int Type { get; set; }
 
 
         [BsonIgnoreIfNull]
         [BsonIgnoreIfNull]
         public List<string> Args { get; set; }
         public List<string> Args { get; set; }
 
 
         [BsonIgnoreIfNull]
         [BsonIgnoreIfNull]
-        public List<Config> SubConfigs { get; set; }
+        public List<NodeConfig> SubConfigs { get; set; }
 
 
         public void AddArgs(string arg)
         public void AddArgs(string arg)
         {
         {
@@ -25,11 +25,11 @@ namespace BehaviorTree
             this.Args.Add(arg);
             this.Args.Add(arg);
         }
         }
 
 
-        public void AddSubConfig(Config subConfig)
+        public void AddSubConfig(NodeConfig subConfig)
         {
         {
             if (this.SubConfigs == null)
             if (this.SubConfigs == null)
             {
             {
-                this.SubConfigs = new List<Config>();
+                this.SubConfigs = new List<NodeConfig>();
             }
             }
 
 
             this.SubConfigs.Add(subConfig);
             this.SubConfigs.Add(subConfig);

+ 9 - 0
CSharp/Game/BehaviorTree/NodeType.cs

@@ -0,0 +1,9 @@
+namespace BehaviorTree
+{
+    public enum NodeType
+    {
+        Select = 1,
+        Sequence = 2,
+        Not = 10,
+    }
+}

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

@@ -1,22 +1,14 @@
-using System;
-
-namespace BehaviorTree
+namespace BehaviorTree
 {
 {
+    [NodeAttribute(NodeType.Not, typeof(Not))]
     public class Not: Node
     public class Not: Node
     {
     {
-        public Not(Config config)
+        public Not(NodeConfig config): base(config)
         {
         {
-            this.Name = config.Name;
         }
         }
 
 
         public override bool Run(BlackBoard blackBoard)
         public override bool Run(BlackBoard blackBoard)
         {
         {
-            if (this.children.Count != 1)
-            {
-                throw new Exception(string.Format("not node children count not eq 1: {0}",
-                        this.children.Count));
-            }
-
             return !this.children[0].Run(blackBoard);
             return !this.children[0].Run(blackBoard);
         }
         }
     }
     }

+ 2 - 2
CSharp/Game/BehaviorTree/Selector.cs

@@ -1,10 +1,10 @@
 namespace BehaviorTree
 namespace BehaviorTree
 {
 {
+    [NodeAttribute(NodeType.Select, typeof(Selector))]
     public class Selector: Node
     public class Selector: Node
     {
     {
-        public Selector(Config config)
+        public Selector(NodeConfig config): base(config)
         {
         {
-            this.Name = config.Name;
         }
         }
 
 
         public override bool Run(BlackBoard blackBoard)
         public override bool Run(BlackBoard blackBoard)

+ 2 - 2
CSharp/Game/BehaviorTree/Sequence.cs

@@ -1,10 +1,10 @@
 namespace BehaviorTree
 namespace BehaviorTree
 {
 {
+    [NodeAttribute(NodeType.Sequence, typeof(Sequence))]
     internal class Sequence: Node
     internal class Sequence: Node
     {
     {
-        public Sequence(Config config)
+        public Sequence(NodeConfig config): base(config)
         {
         {
-            this.Name = config.Name;
         }
         }
 
 
         public override bool Run(BlackBoard blackBoard)
         public override bool Run(BlackBoard blackBoard)

+ 3 - 1
CSharp/Game/Component/Buff.cs

@@ -1,4 +1,6 @@
-namespace Component
+using Common.Component;
+
+namespace Component
 {
 {
     public class Buff: Object
     public class Buff: Object
     {
     {

+ 0 - 11
CSharp/Game/Component/Component.csproj

@@ -52,21 +52,10 @@
     <Compile Include="ConfigAttribute.cs" />
     <Compile Include="ConfigAttribute.cs" />
     <Compile Include="ConfigCategory.cs" />
     <Compile Include="ConfigCategory.cs" />
     <Compile Include="ConfigManager.cs" />
     <Compile Include="ConfigManager.cs" />
-    <Compile Include="EventAttribute.cs" />
-    <Compile Include="EventType.cs" />
     <Compile Include="GameObjectManager.cs" />
     <Compile Include="GameObjectManager.cs" />
-    <Compile Include="HandlerAttribute.cs" />
     <Compile Include="IConfigInitialize.cs" />
     <Compile Include="IConfigInitialize.cs" />
-    <Compile Include="IEvent.cs" />
-    <Compile Include="IHandler.cs" />
-    <Compile Include="ILogic.cs" />
     <Compile Include="IType.cs" />
     <Compile Include="IType.cs" />
     <Compile Include="KeyDefine.cs" />
     <Compile Include="KeyDefine.cs" />
-    <Compile Include="LogicManager.cs" />
-    <Compile Include="Message.cs" />
-    <Compile Include="MessageAttribute.cs" />
-    <Compile Include="MessageEnv.cs" />
-    <Compile Include="Object.cs" />
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <Folder Include="Properties\" />
     <Folder Include="Properties\" />

+ 0 - 11
CSharp/Game/Component/EventAttribute.cs

@@ -1,11 +0,0 @@
-using System;
-
-namespace Component
-{
-    [AttributeUsage(AttributeTargets.Class)]
-    public class EventAttribute: Attribute
-    {
-        public EventType Type { get; set; }
-        public int Order { get; set; }
-    }
-}

+ 0 - 15
CSharp/Game/Component/EventType.cs

@@ -1,15 +0,0 @@
-namespace Component
-{
-    // 定义事件类型
-    public enum EventType: short
-    {
-        // 默认的event
-        DefaultEvent,
-
-        // 登录world前触发
-        BeforeLoginWorldEvent,
-
-        // 使用物品前触发
-        BeforeUseItemEvent,
-    }
-}

+ 3 - 1
CSharp/Game/Component/GameObject.cs

@@ -1,4 +1,6 @@
-namespace Component
+using Common.Component;
+
+namespace Component
 {
 {
     public class GameObject: Object
     public class GameObject: Object
     {
     {

+ 0 - 11
CSharp/Game/Component/HandlerAttribute.cs

@@ -1,11 +0,0 @@
-using System;
-
-namespace Component
-{
-    [AttributeUsage(AttributeTargets.Class)]
-    public class HandlerAttribute: Attribute
-    {
-        public Type Type { get; set; }
-        public int Opcode { get; set; }
-    }
-}

+ 0 - 7
CSharp/Game/Component/IEvent.cs

@@ -1,7 +0,0 @@
-namespace Component
-{
-    public interface IEvent
-    {
-        void Trigger(MessageEnv messageEnv);
-    }
-}

+ 0 - 7
CSharp/Game/Component/IHandler.cs

@@ -1,7 +0,0 @@
-namespace Component
-{
-    public interface IHandler
-    {
-        void Handle(MessageEnv messageEnv);
-    }
-}

+ 0 - 7
CSharp/Game/Component/ILogic.cs

@@ -1,7 +0,0 @@
-namespace Component
-{
-    public interface ILogic
-    {
-        void Handle(short opcode, byte[] content);
-    }
-}

+ 0 - 139
CSharp/Game/Component/LogicManager.cs

@@ -1,139 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using Common.Helper;
-using Common.Logger;
-
-namespace Component
-{
-    public class LogicManager: ILogic
-    {
-        private static readonly LogicManager instance = new LogicManager();
-
-        private Dictionary<int, Tuple<IHandler, Type>> handlers;
-
-        private Dictionary<EventType, SortedDictionary<int, IEvent>> events;
-
-        public static LogicManager Instance
-        {
-            get
-            {
-                return instance;
-            }
-        }
-
-        private LogicManager()
-        {
-            this.Load();
-        }
-
-        private void Load()
-        {
-            string dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logic.dll");
-            var assembly = LoaderHelper.Load(dllPath);
-            Type[] types = assembly.GetTypes();
-
-            // 加载封包处理器
-            var localHandlers = new Dictionary<int, Tuple<IHandler, Type>>();
-            foreach (var type in types)
-            {
-                object[] attrs = type.GetCustomAttributes(typeof (HandlerAttribute), false);
-                if (attrs.Length == 0)
-                {
-                    continue;
-                }
-                var handler = (IHandler) Activator.CreateInstance(type);
-                int opcode = ((HandlerAttribute) attrs[0]).Opcode;
-                Type messageType = ((HandlerAttribute) attrs[0]).Type;
-                if (opcode == 0 || messageType == null)
-                {
-                    throw new Exception(string.Format("not set opcode or type, handler name: {0}",
-                            type.Name));
-                }
-                if (localHandlers.ContainsKey(opcode))
-                {
-                    throw new Exception(string.Format(
-                                                      "same handler opcode, opcode: {0}, name: {1}",
-                            opcode, type.Name));
-                }
-                localHandlers[opcode] = new Tuple<IHandler, Type>(handler, messageType);
-            }
-
-            // 加载事件处理器
-            var localEvents = new Dictionary<EventType, SortedDictionary<int, IEvent>>();
-            foreach (var type in types)
-            {
-                object[] attrs = type.GetCustomAttributes(typeof (EventAttribute), false);
-                if (attrs.Length == 0)
-                {
-                    continue;
-                }
-                var evt = (IEvent) Activator.CreateInstance(type);
-                EventType eventType = ((EventAttribute) attrs[0]).Type;
-                int eventOrder = ((EventAttribute) attrs[0]).Order;
-
-                if (eventOrder == 0 || eventType == EventType.DefaultEvent)
-                {
-                    throw new Exception(string.Format("not set order or type, event name: {0}",
-                            type.Name));
-                }
-
-                if (!localEvents.ContainsKey(eventType))
-                {
-                    localEvents[eventType] = new SortedDictionary<int, IEvent>();
-                }
-                if (localEvents[eventType].ContainsKey(eventOrder))
-                {
-                    throw new Exception(
-                            string.Format("same event number, type: {0}, number: {1}, name: {2}",
-                                    eventType, eventOrder, type.Name));
-                }
-                localEvents[eventType][eventOrder] = evt;
-            }
-
-            // 
-            this.handlers = localHandlers;
-            this.events = localEvents;
-        }
-
-        public void Reload()
-        {
-            this.Load();
-        }
-
-        public void Handle(short opcode, byte[] content)
-        {
-            Tuple<IHandler, Type> tuple = null;
-            if (!this.handlers.TryGetValue(opcode, out tuple))
-            {
-                throw new Exception(string.Format("not found handler opcode {0}", opcode));
-            }
-
-            try
-            {
-                object message = MongoHelper.FromBson(content, tuple.Item2);
-                var messageEnv = new MessageEnv();
-                messageEnv[KeyDefine.KMessage] = message;
-                tuple.Item1.Handle(messageEnv);
-            }
-            catch (Exception e)
-            {
-                Log.Trace("message handle error: {0}", e.Message);
-            }
-        }
-
-        public void Trigger(MessageEnv messageEnv, EventType type)
-        {
-            SortedDictionary<int, IEvent> iEventDict = null;
-            if (!this.events.TryGetValue(type, out iEventDict))
-            {
-                return;
-            }
-
-            foreach (var iEvent in iEventDict)
-            {
-                iEvent.Value.Trigger(messageEnv);
-            }
-        }
-    }
-}

+ 0 - 19
CSharp/Game/Component/Message.cs

@@ -1,19 +0,0 @@
-namespace Component
-{
-    public class CChat
-    {
-        public string Content { get; set; }
-    }
-
-    public class CLoginWorld
-    {
-    }
-
-    public class CReloadHandler
-    {
-    }
-
-    public class CReloadConfig
-    {
-    }
-}

+ 0 - 10
CSharp/Game/Component/MessageAttribute.cs

@@ -1,10 +0,0 @@
-using System;
-
-namespace Component
-{
-    [AttributeUsage(AttributeTargets.Class)]
-    public class MessageAttribute: Attribute
-    {
-        public short Opcode { get; set; }
-    }
-}

+ 0 - 6
CSharp/Game/Component/MessageEnv.cs

@@ -1,6 +0,0 @@
-namespace Component
-{
-    public class MessageEnv: Object
-    {
-    }
-}

+ 0 - 14
CSharp/Game/Logic/Event/BeforeLoginWorldEvent.cs

@@ -1,14 +0,0 @@
-using Common.Logger;
-using Component;
-
-namespace Logic.Event
-{
-    [Event(Type = EventType.BeforeLoginWorldEvent, Order = 1)]
-    public class CheckPlayerEvent: IEvent
-    {
-        public void Trigger(MessageEnv messageEnv)
-        {
-            Log.Trace("check player");
-        }
-    }
-}

+ 0 - 14
CSharp/Game/Logic/Event/BeforeUseItemEvent.cs

@@ -1,14 +0,0 @@
-using Common.Logger;
-using Component;
-
-namespace Logic.Event
-{
-    [Event(Type = EventType.BeforeUseItemEvent, Order = 1)]
-    public class UseCountStatisticsEvent: IEvent
-    {
-        public void Trigger(MessageEnv messageEnv)
-        {
-            Log.Trace("check player");
-        }
-    }
-}

+ 0 - 21
CSharp/Game/Logic/Handler/ChatHandler.cs

@@ -1,21 +0,0 @@
-using Common.Helper;
-using Common.Logger;
-using Component;
-using Component.Config;
-
-namespace Logic.Handler
-{
-    [Handler(Type = typeof (CChat), Opcode = 1)]
-    internal class ChatHandler: IHandler
-    {
-        public void Handle(MessageEnv messageEnv)
-        {
-            var chat = (CChat) messageEnv[KeyDefine.KMessage];
-
-            var world = World.World.Instance;
-            var globalConfig = world.ConfigManager.Get<GlobalConfig>(1);
-            Log.Debug(MongoHelper.ToJson(globalConfig));
-            Log.Debug("chat content: {0}", chat.Content);
-        }
-    }
-}

+ 0 - 15
CSharp/Game/Logic/Handler/LoginWorldHandler.cs

@@ -1,15 +0,0 @@
-using Component;
-
-namespace Logic.Event.Handler
-{
-    [Handler(Type = typeof (CLoginWorld), Opcode = 2)]
-    public class LoginWorldHandler: IHandler
-    {
-        public void Handle(MessageEnv messageEnv)
-        {
-            var world = World.World.Instance;
-            // 登录world前触发事件
-            world.LogicManager.Trigger(messageEnv, EventType.BeforeLoginWorldEvent);
-        }
-    }
-}

+ 0 - 24
CSharp/Game/Logic/Handler/ReloadHandler.cs

@@ -1,24 +0,0 @@
-using Component;
-
-namespace Logic.Handler
-{
-    [Handler(Type = typeof (CReloadHandler), Opcode = 3)]
-    internal class ReloadHandlerHandler: IHandler
-    {
-        public void Handle(MessageEnv messageEnv)
-        {
-            var world = World.World.Instance;
-            world.LogicManager.Reload();
-        }
-    }
-
-    [Handler(Type = typeof (CReloadConfig), Opcode = 4)]
-    internal class ReloadConfigHandler: IHandler
-    {
-        public void Handle(MessageEnv messageEnv)
-        {
-            var world = World.World.Instance;
-            world.ConfigManager.Reload();
-        }
-    }
-}

+ 0 - 69
CSharp/Game/Logic/Logic.csproj

@@ -1,69 +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>{CB5BCF0A-4741-477A-94C6-49ECA782555F}</ProjectGuid>
-    <OutputType>Library</OutputType>
-    <AppDesignerFolder>Properties</AppDesignerFolder>
-    <RootNamespace>Logic</RootNamespace>
-    <AssemblyName>Logic</AssemblyName>
-    <TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
-    <FileAlignment>512</FileAlignment>
-    <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="Event\BeforeUseItemEvent.cs" />
-    <Compile Include="Event\BeforeLoginWorldEvent.cs" />
-    <Compile Include="Handler\ChatHandler.cs" />
-    <Compile Include="Handler\LoginWorldHandler.cs" />
-    <Compile Include="Handler\ReloadHandler.cs" />
-  </ItemGroup>
-  <ItemGroup>
-    <ProjectReference Include="..\..\Platform\Common\Common.csproj">
-      <Project>{19f8f043-1f99-4550-99df-dea5c7d77e55}</Project>
-      <Name>Common</Name>
-    </ProjectReference>
-    <ProjectReference Include="..\Component\Component.csproj">
-      <Project>{0fa529d1-d0a9-4a8e-90f5-117ce80f2ede}</Project>
-      <Name>Component</Name>
-    </ProjectReference>
-    <ProjectReference Include="..\World\World.csproj">
-      <Project>{cbedbe33-a883-4bfa-ae0a-8b3573f09bd0}</Project>
-      <Name>World</Name>
-    </ProjectReference>
-  </ItemGroup>
-  <ItemGroup>
-    <Folder Include="Properties\" />
-  </ItemGroup>
-  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.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>

+ 1 - 11
CSharp/Game/World/World.cs

@@ -5,9 +5,7 @@ namespace World
     public class World
     public class World
     {
     {
         private static readonly World instance = new World();
         private static readonly World instance = new World();
-
-        private readonly LogicManager logicManager = LogicManager.Instance;
-
+        
         private readonly ConfigManager configManager = ConfigManager.Instance;
         private readonly ConfigManager configManager = ConfigManager.Instance;
 
 
         private readonly GameObjectManager gameObjectManager = new GameObjectManager();
         private readonly GameObjectManager gameObjectManager = new GameObjectManager();
@@ -20,14 +18,6 @@ namespace World
             }
             }
         }
         }
 
 
-        public LogicManager LogicManager
-        {
-            get
-            {
-                return this.logicManager;
-            }
-        }
-
         public ConfigManager ConfigManager
         public ConfigManager ConfigManager
         {
         {
             get
             get

+ 6 - 0
CSharp/Platform/Common/Common.csproj

@@ -58,6 +58,11 @@
     <Reference Include="System.Xml" />
     <Reference Include="System.Xml" />
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
+    <Compile Include="Component\EventTrigger.cs" />
+    <Compile Include="Component\Env.cs" />
+    <Compile Include="Component\IEventAttribute.cs" />
+    <Compile Include="Component\IEvent.cs" />
+    <Compile Include="Component\Object.cs" />
     <Compile Include="Helper\BigIntegerHelper.cs" />
     <Compile Include="Helper\BigIntegerHelper.cs" />
     <Compile Include="Helper\ByteHelper.cs" />
     <Compile Include="Helper\ByteHelper.cs" />
     <Compile Include="Helper\EnumHelper.cs" />
     <Compile Include="Helper\EnumHelper.cs" />
@@ -80,6 +85,7 @@
     <None Include="Helper\Packages.config" />
     <None Include="Helper\Packages.config" />
     <None Include="packages.config" />
     <None Include="packages.config" />
   </ItemGroup>
   </ItemGroup>
+  <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
   <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
   <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
   <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

+ 6 - 0
CSharp/Platform/Common/Component/Env.cs

@@ -0,0 +1,6 @@
+namespace Common.Component
+{
+    public class Env: Object
+    {
+    }
+}

+ 54 - 0
CSharp/Platform/Common/Component/EventTrigger.cs

@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+
+namespace Common.Component
+{
+    public class EventTrigger<T> where T : IEventAttribute
+    {
+        private readonly Dictionary<int, SortedDictionary<int, IEvent>> events;
+
+        public EventTrigger()
+        {
+            this.events = new Dictionary<int, SortedDictionary<int, IEvent>>();
+
+            Type type = typeof (T);
+            var types = type.Assembly.GetTypes();
+            foreach (var t in types)
+            {
+                object[] attrs = t.GetCustomAttributes(type, false);
+                if (attrs.Length == 0)
+                {
+                    continue;
+                }
+                object obj = Activator.CreateInstance(t);
+                IEvent iEvent = obj as IEvent;
+                if (iEvent == null)
+                {
+                    throw new Exception(string.Format("event not inherit IEvent interface: {0}", obj.GetType().FullName));
+                }
+
+                IEventAttribute iEventAttribute = (T) attrs[0];
+
+                if (!this.events.ContainsKey(iEventAttribute.Type))
+                {
+                    this.events.Add(iEventAttribute.Type, new SortedDictionary<int, IEvent>());
+                }
+                this.events[iEventAttribute.Type].Add(iEventAttribute.Order, iEvent);
+            }
+        }
+
+        public void Trigger(int type, Env env)
+        {
+            SortedDictionary<int, IEvent> iEventDict = null;
+            if (!this.events.TryGetValue(type, out iEventDict))
+            {
+                return;
+            }
+
+            foreach (var iEvent in iEventDict)
+            {
+                iEvent.Value.Trigger(env);
+            }
+        }
+    }
+}

+ 7 - 0
CSharp/Platform/Common/Component/IEvent.cs

@@ -0,0 +1,7 @@
+namespace Common.Component
+{
+    public interface IEvent
+    {
+        void Trigger(Env env);
+    }
+}

+ 8 - 0
CSharp/Platform/Common/Component/IEventAttribute.cs

@@ -0,0 +1,8 @@
+namespace Common.Component
+{
+    public class IEventAttribute
+    {
+        public int Type { get; set; }
+        public int Order { get; set; }
+    }
+}

+ 2 - 2
CSharp/Game/Component/Object.cs → CSharp/Platform/Common/Component/Object.cs

@@ -1,7 +1,7 @@
 using System.Collections.Generic;
 using System.Collections.Generic;
 using MongoDB.Bson;
 using MongoDB.Bson;
 
 
-namespace Component
+namespace Common.Component
 {
 {
     public class Object
     public class Object
     {
     {
@@ -31,7 +31,7 @@ namespace Component
         {
         {
             if (!this.Dict.ContainsKey(key))
             if (!this.Dict.ContainsKey(key))
             {
             {
-                throw new KeyNotFoundException(string.Format("not found key: {0}", key));
+                return default(T);
             }
             }
             return (T) this.Dict[key];
             return (T) this.Dict[key];
         }
         }