提交 c8c66bc3 编写于 作者: T tanghai

重构了ConsoleComponent

上级 c282113d
......@@ -40,6 +40,41 @@ namespace ETModel
line = line.Trim();
if (this.Mode != "")
{
bool isExited = true;
switch (this.Mode)
{
case ConsoleMode.Repl:
{
ReplComponent replComponent = this.GetComponent<ReplComponent>();
if (replComponent == null)
{
Console.WriteLine($"no command: {line}!");
break;
}
try
{
isExited = await replComponent.Run(line, this.CancellationTokenSource.Token);
}
catch (Exception e)
{
Console.WriteLine(e);
}
break;
}
}
if (isExited)
{
this.Mode = "";
}
continue;
}
switch (line)
{
case "reload":
......@@ -63,50 +98,8 @@ namespace ETModel
Console.WriteLine(e);
}
break;
case "exit":
switch (this.Mode)
{
case ConsoleMode.Repl:
this.RemoveComponent<ReplComponent>();
break;
}
this.Mode = ConsoleMode.None;
break;
case "reset":
switch (this.Mode)
{
case ConsoleMode.Repl:
this.RemoveComponent<ReplComponent>();
this.AddComponent<ReplComponent>();
break;
}
break;
default:
switch (this.Mode)
{
case ConsoleMode.Repl:
{
ReplComponent replComponent = this.GetComponent<ReplComponent>();
if (replComponent == null)
{
Console.WriteLine($"no command: {line}!");
break;
}
try
{
await replComponent.Run(line, this.CancellationTokenSource.Token);
}
catch (Exception e)
{
Console.WriteLine(e);
}
break;
}
}
Console.WriteLine($"no such command: {line}");
break;
}
}
......
......@@ -22,15 +22,40 @@ namespace ETModel
public ScriptOptions ScriptOptions;
public ScriptState ScriptState;
public async ETTask Run(string line, CancellationToken cancellationToken)
public async ETTask<bool> Run(string line, CancellationToken cancellationToken)
{
if (this.ScriptState == null)
switch (line)
{
this.ScriptState = await CSharpScript.RunAsync(line, this.ScriptOptions, cancellationToken: cancellationToken);
}
else
{
this.ScriptState = await this.ScriptState.ContinueWithAsync(line, cancellationToken: cancellationToken);
case "exit":
{
this.Entity.RemoveComponent<ReplComponent>();
return true;
}
case "reset":
{
this.ScriptState = null;
return false;
}
default:
{
try
{
if (this.ScriptState == null)
{
this.ScriptState = await CSharpScript.RunAsync(line, this.ScriptOptions, cancellationToken: cancellationToken);
}
else
{
this.ScriptState = await this.ScriptState.ContinueWithAsync(line, cancellationToken: cancellationToken);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return false;
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册