提交 c5723551 编写于 作者: T tanghai

1.把repl功能拆分成两个组件ConsoleComponent跟ReplComponent,

2.console增加reload指令,输入reload就可以热更Hotfix.dll
3.console中执行repl就可以进入repl模式,repl模式中执行reset可以重置repl环境,执行exit则退出repl模式
上级 f2442531
......@@ -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:
......
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);
}
}
}
}
}
\ No newline at end of file
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);
}
}
}
}
}
\ No newline at end of file
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;
}
}
}
\ No newline at end of file
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册