diff --git a/Server/App/Program.cs b/Server/App/Program.cs index 042081598fe0a805b6d72a6a356f0844a7092e0a..6ce1f9421975dc21645931d373c5530c0b9b4366 100644 --- a/Server/App/Program.cs +++ b/Server/App/Program.cs @@ -123,7 +123,7 @@ namespace App Game.Scene.AddComponent(); Game.Scene.AddComponent(); - Game.Scene.AddComponent(); + Game.Scene.AddComponent(); // Game.Scene.AddComponent(); break; case AppType.Benchmark: diff --git a/Server/Hotfix/Module/Repl/ReplComponentSystem.cs b/Server/Hotfix/Module/Repl/ReplComponentSystem.cs deleted file mode 100644 index ad0ddd25a1a1b7cb2dfa152f8c59adc6d279a19c..0000000000000000000000000000000000000000 --- a/Server/Hotfix/Module/Repl/ReplComponentSystem.cs +++ /dev/null @@ -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 - { - 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 - { - 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); - } - } - } - } -} \ No newline at end of file diff --git a/Server/Model/Component/ConsoleComponent.cs b/Server/Model/Component/ConsoleComponent.cs new file mode 100644 index 0000000000000000000000000000000000000000..06f628b1733cc77fb3a5c069fb6b3e99bc85d1d3 --- /dev/null +++ b/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 + { + 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(); + } + catch (Exception e) + { + Console.WriteLine(e); + } + break; + default: + ReplComponent replComponent = Game.Scene.GetComponent(); + if (replComponent == null) + { + Console.WriteLine($"no command: {line}!"); + break; + } + + try + { + if (line == "exit") + { + this.OutputPrefix = ""; + Game.Scene.RemoveComponent(); + break; + } + + switch (line) + { + case "exit": + this.OutputPrefix = ""; + Game.Scene.RemoveComponent(); + break; + case "reset": + Game.Scene.RemoveComponent(); + Game.Scene.AddComponent(); + break; + default: + await replComponent.Run(line, this.CancellationTokenSource.Token); + break; + } + } + catch (Exception e) + { + Console.WriteLine(e); + } + + break; + } + } + catch (Exception e) + { + Console.WriteLine(e); + } + } + } + } +} \ No newline at end of file diff --git a/Server/Model/Component/ReplComponent.cs b/Server/Model/Component/ReplComponent.cs new file mode 100644 index 0000000000000000000000000000000000000000..55f2076bf8b70e738a218e6160244813d0d74628 --- /dev/null +++ b/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 + { + 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; + } + } +} \ No newline at end of file diff --git a/Server/Model/Module/Repl/ReplComponent.cs b/Server/Model/Module/Repl/ReplComponent.cs deleted file mode 100644 index 8190cebc1dfe43e77090e96f5ef73067165495d2..0000000000000000000000000000000000000000 --- a/Server/Model/Module/Repl/ReplComponent.cs +++ /dev/null @@ -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; - } -} \ No newline at end of file