| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- 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.WithFilePath(Environment.CurrentDirectory)
- .AddReferences(typeof (ReplComponent).Assembly);
-
- 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 == "quit")
- {
- 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);
- }
- }
- }
- }
- }
|