CliUtils.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Diagnostics;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using SingularityGroup.HotReload.Newtonsoft.Json;
  6. using UnityEngine;
  7. using System;
  8. namespace SingularityGroup.HotReload.Editor.Cli {
  9. internal static class CliUtils {
  10. static readonly string projectIdentifier = GetProjectIdentifier();
  11. class Config {
  12. public bool singleInstance;
  13. }
  14. public static string GetProjectIdentifier() {
  15. if (File.Exists(PackageConst.ConfigFileName)) {
  16. var config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(PackageConst.ConfigFileName));
  17. if (config.singleInstance) {
  18. return null;
  19. }
  20. }
  21. var path = Path.GetDirectoryName(UnityHelper.DataPath);
  22. var name = new DirectoryInfo(path).Name;
  23. using (SHA256 sha256 = SHA256.Create()) {
  24. byte[] inputBytes = Encoding.UTF8.GetBytes(path);
  25. byte[] hashBytes = sha256.ComputeHash(inputBytes);
  26. var hash = BitConverter.ToString(hashBytes).Replace("-", "").Substring(0, 6).ToUpper();
  27. return $"{name}-{hash}";
  28. }
  29. }
  30. public static string GetTempDownloadFilePath(string osxFileName) {
  31. if (UnityHelper.Platform == RuntimePlatform.OSXEditor) {
  32. // project specific temp directory that is writeable on MacOS (Path.GetTempPath() wasn't when run through HotReload.app)
  33. return Path.GetFullPath(PackageConst.LibraryCachePath + $"/HotReloadServerTemp/{osxFileName}");
  34. } else {
  35. return Path.GetTempFileName();
  36. }
  37. }
  38. public static string GetHotReloadTempDir() {
  39. if (UnityHelper.Platform == RuntimePlatform.OSXEditor) {
  40. // project specific temp directory that is writeable on MacOS (Path.GetTempPath() wasn't when run through HotReload.app)
  41. return Path.GetFullPath(PackageConst.LibraryCachePath + "/HotReloadServerTemp");
  42. } else {
  43. if (projectIdentifier != null) {
  44. return Path.Combine(Path.GetTempPath(), "HotReloadTemp", projectIdentifier);
  45. } else {
  46. return Path.Combine(Path.GetTempPath(), "HotReloadTemp");
  47. }
  48. }
  49. }
  50. public static string GetAppDataPath() {
  51. # if (UNITY_EDITOR_OSX)
  52. var baseDir = "/Users/Shared";
  53. # elif (UNITY_EDITOR_LINUX)
  54. var baseDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  55. # else
  56. var baseDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  57. #endif
  58. return Path.Combine(baseDir, "singularitygroup-hotreload");
  59. }
  60. public static string GetExecutableTargetDir() {
  61. if (PackageConst.IsAssetStoreBuild) {
  62. return Path.Combine(GetAppDataPath(), "asset-store", $"executables_{PackageConst.ServerVersion.Replace('.', '-')}");
  63. }
  64. return Path.Combine(GetAppDataPath(), $"executables_{PackageConst.ServerVersion.Replace('.', '-')}");
  65. }
  66. public static string GetCliTempDir() {
  67. return Path.Combine(GetHotReloadTempDir(), "MethodPatches");
  68. }
  69. public static void Chmod(string targetFile, string flags = "+x") {
  70. // ReSharper disable once PossibleNullReferenceException
  71. Process.Start(new ProcessStartInfo("chmod", $"{flags} \"{targetFile}\"") {
  72. UseShellExecute = false,
  73. }).WaitForExit(2000);
  74. }
  75. public static bool TryFindServerDir(out string path) {
  76. const string serverBasePath = "Packages/com.singularitygroup.hotreload/Server";
  77. if(Directory.Exists(serverBasePath)) {
  78. path = Path.GetFullPath(serverBasePath);
  79. return true;
  80. }
  81. //Not found in packages. Try to find in assets folder.
  82. //fast path - this is the expected folder
  83. const string alternativeExecutablePath = "Assets/HotReload/Server";
  84. if(Directory.Exists(alternativeExecutablePath)) {
  85. path = Path.GetFullPath(alternativeExecutablePath);
  86. return true;
  87. }
  88. //slow path - try to find the server directory somewhere in the assets folder
  89. var candidates = Directory.GetDirectories("Assets", "HotReload", SearchOption.AllDirectories);
  90. foreach(var candidate in candidates) {
  91. var serverDir = Path.Combine(candidate, "Server");
  92. if(Directory.Exists(serverDir)) {
  93. path = Path.GetFullPath(serverDir);
  94. return true;
  95. }
  96. }
  97. path = null;
  98. return false;
  99. }
  100. public static string GetPidFilePath(string hotreloadTempDir) {
  101. return Path.GetFullPath(Path.Combine(hotreloadTempDir, "server.pid"));
  102. }
  103. public static void KillLastKnownHotReloadProcess() {
  104. var pidPath = GetPidFilePath(GetHotReloadTempDir());
  105. try {
  106. var pid = int.Parse(File.ReadAllText(pidPath));
  107. Process.GetProcessById(pid).Kill();
  108. }
  109. catch {
  110. //ignore
  111. }
  112. File.Delete(pidPath);
  113. }
  114. }
  115. }