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

RobotCase分发改成使用Callback分发,大大简化了代码,只要用int分发的逻辑都可以用Callback分发

tanghai 3 лет назад
Родитель
Сommit
b59e509628
19 измененных файлов с 50 добавлено и 146 удалено
  1. 0 5
      DotNet/Hotfix/DotNet.Hotfix.csproj
  2. 0 1
      Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/EntryEvent2_InitServer.cs
  3. 3 3
      Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Robot/Case/RobotCase_FirstCase.cs
  4. 1 1
      Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Robot/Console/CreateRobotConsoleHandler.cs
  5. 2 2
      Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Robot/RobotConsoleHandler.cs
  6. 24 0
      Unity/Assets/Scripts/Codes/Hotfix/Server/Module/RobotCase/ARobotCase.cs
  7. 1 1
      Unity/Assets/Scripts/Codes/Hotfix/Server/Module/RobotCase/ARobotCase.cs.meta
  8. 0 82
      Unity/Assets/Scripts/Codes/Hotfix/Server/Module/RobotCase/RobotCaseDispatcherComponentSystem.cs
  9. 1 1
      Unity/Assets/Scripts/Codes/Hotfix/Share/Module/AI/AIDispatcherComponentSystem.cs
  10. 1 0
      Unity/Assets/Scripts/Codes/Model/Server/Demo/Robot/RobotCaseType.cs
  11. 0 7
      Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/IRobotCase.cs
  12. 0 11
      Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/IRobotCase.cs.meta
  13. 8 0
      Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCallbackArgs.cs
  14. 1 1
      Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCallbackArgs.cs.meta
  15. 1 3
      Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCase.cs
  16. 0 14
      Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCaseAttribute.cs
  17. 0 13
      Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCaseDispatcherComponent.cs
  18. 6 0
      Unity/Assets/Scripts/Core/Object/EnableClassAttribute.cs
  19. 1 1
      Unity/Assets/Scripts/Core/Object/EnableClassAttribute.cs.meta

+ 0 - 5
DotNet/Hotfix/DotNet.Hotfix.csproj

@@ -38,10 +38,5 @@
         <ProjectReference Include="..\..\Share\Analyzer\Share.Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
         <ProjectReference Include="..\Model\DotNet.Model.csproj" />
     </ItemGroup>
-    <ItemGroup>
-      <Folder Include="..\..\Unity\Assets\Scripts\Codes\Hotfix\Share\Module\Config">
-        <Link>Share\Module\Config</Link>
-      </Folder>
-    </ItemGroup>
 
 </Project>

+ 0 - 1
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/EntryEvent2_InitServer.cs

@@ -15,7 +15,6 @@ namespace ET.Server
             Game.Scene.AddComponent<LocationProxyComponent>();
             Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
             Game.Scene.AddComponent<ServerSceneManagerComponent>();
-            Game.Scene.AddComponent<RobotCaseDispatcherComponent>();
             Game.Scene.AddComponent<RobotCaseComponent>();
 
             Game.Scene.AddComponent<NavmeshComponent>();

+ 3 - 3
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Robot/Case/RobotCase_FirstCase.cs

@@ -2,10 +2,10 @@ using System;
 
 namespace ET.Server
 {
-    [RobotCase(RobotCaseType.FirstCase)]
-    public class RobotCase_FirstCase: IRobotCase
+    [Callback(RobotCaseType.FirstCase)]
+    public class RobotCase_FirstCase: ARobotCase
     {
-        public async ETTask Run(RobotCase robotCase)
+        protected override async ETTask Run(RobotCase robotCase)
         {
             using ListComponent<Scene> robots = ListComponent<Scene>.Create();
             

+ 1 - 1
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Robot/Console/CreateRobotConsoleHandler.cs

@@ -38,7 +38,7 @@ namespace ET.Server
                         {
                             int index = i % thisProcessRobotScenes.Count;
                             StartSceneConfig robotSceneConfig = thisProcessRobotScenes[index];
-                            Scene robotScene = Game.Scene.Get(robotSceneConfig.Id);
+                            Scene robotScene = ServerSceneManagerComponent.Instance.Get(robotSceneConfig.Id);
                             RobotManagerComponent robotManagerComponent = robotScene.GetComponent<RobotManagerComponent>();
                             Scene robot = await robotManagerComponent.NewRobot(Options.Instance.Process * 10000 + i);
                             robot.AddComponent<AIComponent, int>(1);

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Robot/RobotConsoleHandler.cs

@@ -21,7 +21,7 @@ namespace ET.Server
                     try
                     {
                         RobotLog.Debug($"run case start: {caseType}");
-                        await RobotCaseDispatcherComponent.Instance.Run(caseType, content);
+                        await EventSystem.Instance.Callback<RobotCallbackArgs, ETTask>(new RobotCallbackArgs() {Id = caseType, Content = content});
                         RobotLog.Debug($"run case finish: {caseType}");
                     }
                     catch (Exception e)
@@ -44,7 +44,7 @@ namespace ET.Server
                         try
                         {
                             RobotLog.Debug($"run case start: {caseType}");
-                            await RobotCaseDispatcherComponent.Instance.Run(caseType, content);
+                            await EventSystem.Instance.Callback<RobotCallbackArgs, ETTask>(new RobotCallbackArgs() {Id = caseType, Content = content});
                             RobotLog.Debug($"---------run case finish: {caseType}");
                         }
                         catch (Exception e)

+ 24 - 0
Unity/Assets/Scripts/Codes/Hotfix/Server/Module/RobotCase/ARobotCase.cs

@@ -0,0 +1,24 @@
+namespace ET.Server
+{
+    // 这里为什么能定义class呢?因为这里只有逻辑,热重载后新的handler替换旧的,仍然没有问题
+    [EnableClass]
+    public abstract class ARobotCase: ACallbackHandler<RobotCallbackArgs, ETTask>
+    {
+        protected abstract ETTask Run(RobotCase robotCase);
+
+        public override async ETTask Handle(RobotCallbackArgs a)
+        {
+            using RobotCase robotCase = await RobotCaseComponent.Instance.New();
+            
+            try
+            {
+                await this.Run(robotCase);
+            }
+            catch (System.Exception e)
+            {
+                Log.Error($"{robotCase.DomainZone()} {e}");
+                RobotLog.Console($"RobotCase Error {a.Id}:\n\t{e}");
+            }
+        }
+    }
+}

+ 1 - 1
Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCaseAttribute.cs.meta → Unity/Assets/Scripts/Codes/Hotfix/Server/Module/RobotCase/ARobotCase.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 80ef27e19be736e46befd780183377c8
+guid: 650ca738fe9d240e4b3f6bca2b2cf997
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 0 - 82
Unity/Assets/Scripts/Codes/Hotfix/Server/Module/RobotCase/RobotCaseDispatcherComponentSystem.cs

@@ -1,82 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace ET.Server
-{
-    [FriendOf(typeof(RobotCaseDispatcherComponent))]
-    [FriendOf(typeof(RobotCase))]
-    public static class RobotCaseDispatcherComponentSystem
-    {
-        [ObjectSystem]
-        public class RobotCaseDispatcherComponentAwakeSystem: AwakeSystem<RobotCaseDispatcherComponent>
-        {
-            protected override void Awake(RobotCaseDispatcherComponent self)
-            {
-                RobotCaseDispatcherComponent.Instance = self;
-                self.Load();
-            }
-        }
-
-        [ObjectSystem]
-        public class RobotCaseDispatcherComponentLoadSystem: LoadSystem<RobotCaseDispatcherComponent>
-        {
-            protected override void Load(RobotCaseDispatcherComponent self)
-            {
-                self.Load();
-            }
-        }
-        
-        public static void Load(this RobotCaseDispatcherComponent self)
-        {
-            self.Dictionary.Clear();
-
-            HashSet<Type> types = EventSystem.Instance.GetTypes(typeof(RobotCaseAttribute));
-
-            foreach (Type type in types)
-            {
-                object[] attrs = type.GetCustomAttributes(typeof(RobotCaseAttribute), false);
-                if (attrs.Length == 0)
-                {
-                    continue;
-                }
-                
-                RobotCaseAttribute attr = attrs[0] as RobotCaseAttribute;
-                if (attr == null)
-                {
-                    continue;
-                }
-                
-                IRobotCase robotCase = Activator.CreateInstance(type) as IRobotCase;
-                if (robotCase == null)
-                {
-                    Log.Error($"RobotCase handle {type.Name} 需要继承 IRobotCase");
-                    continue;
-                }
-                
-                self.Dictionary.Add(attr.CaseType, robotCase);
-            }
-        }
-        
-        public static async ETTask Run(this RobotCaseDispatcherComponent self, int caseType, string line)
-        {
-            if (!self.Dictionary.TryGetValue(caseType, out IRobotCase iRobotCase))
-            {
-                return;
-            }
-
-            try
-            {
-                using (RobotCase robotCase = await RobotCaseComponent.Instance.New())
-                {
-                    robotCase.CommandLine = line;
-                    await iRobotCase.Run(robotCase);
-                }
-            }
-            catch (Exception e)
-            {
-                Log.Error($"{self.DomainZone()} {e}");
-                RobotLog.Console($"RobotCase Error {caseType}:\n\t{e}");
-            }
-        }
-    }
-}

+ 1 - 1
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/AI/AIDispatcherComponentSystem.cs

@@ -34,7 +34,7 @@ namespace ET
             }
         }
         
-        public static void Load(this AIDispatcherComponent self)
+        private static void Load(this AIDispatcherComponent self)
         {
             self.AIHandlers.Clear();
             

+ 1 - 0
Unity/Assets/Scripts/Codes/Model/Server/Demo/Robot/RobotCaseType.cs

@@ -1,5 +1,6 @@
 namespace ET.Server
 {
+    [UniqueId(1, 10000)]
     public static class RobotCaseType
     {
         public const int FirstCase = 1;

+ 0 - 7
Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/IRobotCase.cs

@@ -1,7 +0,0 @@
-namespace ET.Server
-{
-    public interface IRobotCase
-    {
-        ETTask Run(RobotCase robotCase);
-    }
-}

+ 0 - 11
Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/IRobotCase.cs.meta

@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: d395fe03ef85a934191f7acd13de22a9
-MonoImporter:
-  externalObjects: {}
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 8 - 0
Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCallbackArgs.cs

@@ -0,0 +1,8 @@
+namespace ET.Server
+{
+    public struct RobotCallbackArgs: ICallback
+    {
+        public int Id { get; set; }
+        public string Content { get; set; }
+    }
+}

+ 1 - 1
Unity/Assets/Scripts/Codes/Hotfix/Server/Module/RobotCase/RobotCaseDispatcherComponentSystem.cs.meta → Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCallbackArgs.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: bb0c8d4cbd812364db443d01a13425f7
+guid: 9df65fd90f15c45f5aacd342c4eb12d0
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 1 - 3
Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCase.cs

@@ -1,6 +1,4 @@
-using System.Collections.Generic;
-
-namespace ET.Server
+namespace ET.Server
 {
     [ChildOf(typeof(RobotCaseComponent))]
     public class RobotCase: Entity, IAwake

+ 0 - 14
Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCaseAttribute.cs

@@ -1,14 +0,0 @@
-using System;
-
-namespace ET.Server
-{
-    public class RobotCaseAttribute: BaseAttribute
-    {
-        public int CaseType { get; }
-
-        public RobotCaseAttribute(int caseType)
-        {
-            this.CaseType = caseType;
-        }
-    }
-}

+ 0 - 13
Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCaseDispatcherComponent.cs

@@ -1,13 +0,0 @@
-using System.Collections.Generic;
-
-namespace ET.Server
-{
-    [ComponentOf(typeof(Scene))]
-    public class RobotCaseDispatcherComponent: Entity, IAwake, ILoad
-    {
-        [StaticField]
-        public static RobotCaseDispatcherComponent Instance;
-        
-        public Dictionary<int, IRobotCase> Dictionary = new Dictionary<int, IRobotCase>();
-    }
-}

+ 6 - 0
Unity/Assets/Scripts/Core/Object/EnableClassAttribute.cs

@@ -0,0 +1,6 @@
+namespace ET
+{
+    public class EnableClassAttribute: BaseAttribute
+    {
+    }
+}

+ 1 - 1
Unity/Assets/Scripts/Codes/Model/Server/Module/RobotCase/RobotCaseDispatcherComponent.cs.meta → Unity/Assets/Scripts/Core/Object/EnableClassAttribute.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: bb8e4d6b5f5ad6046b30bd7dde3ccb38
+guid: 41028c857d1434bd78bb45a748e476e8
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2