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

1.把repl功能拆分成两个组件ConsoleComponent跟ReplComponent,
2.console增加reload指令,输入reload就可以热更Hotfix.dll
3.console中执行repl就可以进入repl模式,repl模式中执行reset可以重置repl环境,执行exit则退出repl模式

tanghai 7 лет назад
Родитель
Сommit
c57235512d

+ 1 - 1
Server/App/Program.cs

@@ -123,7 +123,7 @@ namespace App
 						Game.Scene.AddComponent<PlayerComponent>();
 						Game.Scene.AddComponent<UnitComponent>();
 
-						Game.Scene.AddComponent<ReplComponent>();
+						Game.Scene.AddComponent<ConsoleComponent>();
 						// Game.Scene.AddComponent<HttpComponent>();
 						break;
 					case AppType.Benchmark:

+ 0 - 75
Server/Hotfix/Module/Repl/ReplComponentSystem.cs

@@ -1,75 +0,0 @@
-using System;
-using System.Threading;
-using System.Threading.Tasks;
-using ETModel;
-using Microsoft.CodeAnalysis.Scripting;
-using Microsoft.CodeAnalysis.CSharp.Scripting;
-
-namespace ETHotfix
-{
-    [ObjectSystem]
-    public class ReplComponentStartSystem : StartSystem<ReplComponent>
-    {
-        public override void Start(ReplComponent self)
-        {
-            self.ScriptOptions = ScriptOptions.Default
-                    .WithMetadataResolver(ScriptMetadataResolver.Default.WithBaseDirectory(Environment.CurrentDirectory))
-                    .AddReferences(typeof (ReplComponent).Assembly)
-                    .AddImports("System");
-
-            self.Run().NoAwait();
-        }
-    }
-    
-    [ObjectSystem]
-    public class ReplComponentLoadSystem : LoadSystem<ReplComponent>
-    {
-        public override void Load(ReplComponent self)
-        {
-            self.CancellationTokenSource?.Cancel();
-            self.ScriptState = null;
-            self.Run().NoAwait();
-        }
-    }
-
-    public static class ReplComponentHelper
-    {
-        public static async ETVoid Run(this ReplComponent self)
-        {
-            self.CancellationTokenSource = new CancellationTokenSource();
-            
-            while (true)
-            {
-                try
-                {
-                    string line = await Task.Factory.StartNew(() =>
-                    {
-                        Console.Out.Write("> ");
-                        return Console.In.ReadLine();
-                    }, self.CancellationTokenSource.Token);
-                    
-                    line = line.Trim();
-                    
-                    if (line == "exit")
-                    {
-                        self.ScriptState = null;
-                        continue;
-                    }
-    
-                    if (self.ScriptState == null)
-                    {
-                        self.ScriptState = await CSharpScript.RunAsync(line, self.ScriptOptions, cancellationToken: self.CancellationTokenSource.Token);
-                    }
-                    else
-                    {
-                        self.ScriptState = await self.ScriptState.ContinueWithAsync(line, cancellationToken: self.CancellationTokenSource.Token);
-                    }
-                }
-                catch (Exception e)
-                {
-                    Console.WriteLine(e);
-                }
-            }
-        }
-    }
-}

+ 107 - 0
Server/Model/Component/ConsoleComponent.cs

@@ -0,0 +1,107 @@
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace ETModel
+{
+    [ObjectSystem]
+    public class ConsoleComponentAwakeSystem : StartSystem<ConsoleComponent>
+    {
+        public override void Start(ConsoleComponent self)
+        {
+            self.Start().NoAwait();
+        }
+    }
+    
+    public class ConsoleComponent: Component
+    {
+        public CancellationTokenSource CancellationTokenSource;
+        public string OutputPrefix = "";
+
+        public async ETVoid Start()
+        {
+            this.CancellationTokenSource = new CancellationTokenSource();
+            
+            while (true)
+            {
+                try
+                {
+                    string line = await Task.Factory.StartNew(() =>
+                    {
+                        Console.Write($"{OutputPrefix}> ");
+                        return Console.In.ReadLine();
+                    }, this.CancellationTokenSource.Token);
+                    
+                    line = line.Trim();
+
+                    switch (line)
+                    {
+                        case "reload":
+                            try
+                            {
+                                Game.EventSystem.Add(DLLType.Hotfix, DllHelper.GetHotfixAssembly());
+                            }
+                            catch (Exception e)
+                            {
+                                Console.WriteLine(e);
+                            }
+                            break;
+                        case "repl":
+                            try
+                            {
+                                this.OutputPrefix = "repl";
+                                Game.Scene.AddComponent<ReplComponent>();
+                            }
+                            catch (Exception e)
+                            {
+                                Console.WriteLine(e);
+                            }
+                            break;
+                        default:
+                            ReplComponent replComponent = Game.Scene.GetComponent<ReplComponent>();
+                            if (replComponent == null)
+                            {
+                                Console.WriteLine($"no command: {line}!");
+                                break;
+                            }
+                            
+                            try
+                            {
+                                if (line == "exit")
+                                {
+                                    this.OutputPrefix = "";
+                                    Game.Scene.RemoveComponent<ReplComponent>();
+                                    break;
+                                }
+
+                                switch (line)
+                                {
+                                    case "exit":
+                                        this.OutputPrefix = "";
+                                        Game.Scene.RemoveComponent<ReplComponent>();
+                                        break;
+                                    case "reset":
+                                        Game.Scene.RemoveComponent<ReplComponent>();
+                                        Game.Scene.AddComponent<ReplComponent>();
+                                        break;
+                                    default:
+                                        await replComponent.Run(line, this.CancellationTokenSource.Token);
+                                        break;
+                                }
+                            }
+                            catch (Exception e)
+                            {
+                                Console.WriteLine(e);
+                            }
+
+                            break;
+                    }
+                }
+                catch (Exception e)
+                {
+                    Console.WriteLine(e);
+                }
+            }
+        }
+    }
+}

+ 48 - 0
Server/Model/Component/ReplComponent.cs

@@ -0,0 +1,48 @@
+using System;
+using System.Threading;
+using Microsoft.CodeAnalysis.CSharp.Scripting;
+using Microsoft.CodeAnalysis.Scripting;
+
+namespace ETModel
+{
+    [ObjectSystem]
+    public class ReplComponentAwakeSystem : AwakeSystem<ReplComponent>
+    {
+        public override void Awake(ReplComponent self)
+        {
+            self.ScriptOptions = ScriptOptions.Default
+                    .WithMetadataResolver(ScriptMetadataResolver.Default.WithBaseDirectory(Environment.CurrentDirectory))
+                    .AddReferences(typeof (ReplComponent).Assembly)
+                    .AddImports("System");
+        }
+    }
+    
+    public class ReplComponent: Component
+    {
+        public ScriptOptions ScriptOptions;
+        public ScriptState ScriptState;
+
+        public async ETTask Run(string line, CancellationToken cancellationToken)
+        {
+            if (this.ScriptState == null)
+            {
+                this.ScriptState = await CSharpScript.RunAsync(line, this.ScriptOptions, cancellationToken: cancellationToken);
+            }
+            else
+            {
+                this.ScriptState = await this.ScriptState.ContinueWithAsync(line, cancellationToken: cancellationToken);
+            }
+        }
+
+        public override void Dispose()
+        {
+            if (this.IsDisposed)
+            {
+                return;
+            }
+            base.Dispose();
+            this.ScriptOptions = null;
+            this.ScriptState = null;
+        }
+    }
+}

+ 0 - 12
Server/Model/Module/Repl/ReplComponent.cs

@@ -1,12 +0,0 @@
-using System.Threading;
-using Microsoft.CodeAnalysis.Scripting;
-
-namespace ETModel
-{
-    public class ReplComponent: Component
-    {
-        public ScriptOptions ScriptOptions;
-        public ScriptState ScriptState;
-        public CancellationTokenSource CancellationTokenSource;
-    }
-}