ReplComponent.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Threading;
  3. using Microsoft.CodeAnalysis.CSharp.Scripting;
  4. using Microsoft.CodeAnalysis.Scripting;
  5. namespace ETModel
  6. {
  7. [ObjectSystem]
  8. public class ReplComponentAwakeSystem : AwakeSystem<ReplComponent>
  9. {
  10. public override void Awake(ReplComponent self)
  11. {
  12. self.ScriptOptions = ScriptOptions.Default
  13. .WithMetadataResolver(ScriptMetadataResolver.Default.WithBaseDirectory(Environment.CurrentDirectory))
  14. .AddReferences(typeof (ReplComponent).Assembly)
  15. .AddImports("System");
  16. }
  17. }
  18. public class ReplComponent: Component
  19. {
  20. public ScriptOptions ScriptOptions;
  21. public ScriptState ScriptState;
  22. public async ETTask Run(string line, CancellationToken cancellationToken)
  23. {
  24. if (this.ScriptState == null)
  25. {
  26. this.ScriptState = await CSharpScript.RunAsync(line, this.ScriptOptions, cancellationToken: cancellationToken);
  27. }
  28. else
  29. {
  30. this.ScriptState = await this.ScriptState.ContinueWithAsync(line, cancellationToken: cancellationToken);
  31. }
  32. }
  33. public override void Dispose()
  34. {
  35. if (this.IsDisposed)
  36. {
  37. return;
  38. }
  39. base.Dispose();
  40. this.ScriptOptions = null;
  41. this.ScriptState = null;
  42. }
  43. }
  44. }