LinuxCliController.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using Debug = UnityEngine.Debug;
  6. namespace SingularityGroup.HotReload.Editor.Cli {
  7. class LinuxCliController : ICliController {
  8. Process process;
  9. public string BinaryFileName => "CodePatcherCLI";
  10. public string PlatformName => "linux-x64";
  11. public bool CanOpenInBackground => true;
  12. public Task Start(StartArgs args) {
  13. var startScript = Path.Combine(args.executableSourceDir, "hotreload-start-script.sh");
  14. if (!File.Exists(startScript)) {
  15. throw new FileNotFoundException(startScript);
  16. }
  17. File.WriteAllText(startScript, File.ReadAllText(startScript).Replace("\r\n", "\n"));
  18. CliUtils.Chmod(startScript);
  19. var title = CodePatcher.TAG + "Server " + new DirectoryInfo(args.unityProjDir).Name;
  20. title = title.Replace(" ", "-");
  21. title = title.Replace("'", "");
  22. var cliargsfile = Path.GetTempFileName();
  23. File.WriteAllText(cliargsfile,args.cliArguments);
  24. var codePatcherProc = Process.Start(new ProcessStartInfo {
  25. FileName = startScript,
  26. Arguments =
  27. $"--title \"{title}\""
  28. + $" --executables-source-dir \"{args.executableSourceDir}\" "
  29. + $" --executable-taget-dir \"{args.executableTargetDir}\""
  30. + $" --pidfile \"{CliUtils.GetPidFilePath(args.hotreloadTempDir)}\""
  31. + $" --cli-arguments-file \"{cliargsfile}\""
  32. + $" --method-patch-dir \"{args.cliTempDir}\""
  33. + $" --create-no-window \"{args.createNoWindow}\"",
  34. UseShellExecute = false,
  35. RedirectStandardOutput = true,
  36. RedirectStandardError = true
  37. });
  38. if (codePatcherProc == null) {
  39. if (File.Exists(cliargsfile)) {
  40. File.Delete(cliargsfile);
  41. }
  42. throw new Exception("Could not start code patcher process.");
  43. }
  44. codePatcherProc.BeginErrorReadLine();
  45. codePatcherProc.BeginOutputReadLine();
  46. codePatcherProc.OutputDataReceived += (_, a) => {
  47. };
  48. // error data can also mean we kill the proc beningly
  49. codePatcherProc.ErrorDataReceived += (_, a) => {
  50. };
  51. process = codePatcherProc;
  52. return Task.CompletedTask;
  53. }
  54. public async Task Stop() {
  55. await RequestHelper.KillServer();
  56. try {
  57. // process.CloseMainWindow throws if proc already exited.
  58. // also we just rely on the pid file it is fine
  59. CliUtils.KillLastKnownHotReloadProcess();
  60. } catch {
  61. //ignored
  62. }
  63. process = null;
  64. }
  65. }
  66. }