WebAppDownloader.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Net;
  4. namespace Unity.RenderStreaming.Editor
  5. {
  6. internal static class WebAppDownloader
  7. {
  8. const string URLRoot = "https://github.com/Unity-Technologies/UnityRenderStreaming";
  9. const string LatestKnownVersion = "3.1.0-exp.6";
  10. // TODO::fix release process of webserver runtime.
  11. const string FileNameWebAppForMac = "webserver_mac";
  12. const string FileNameWebAppForLinux = "webserver";
  13. const string FileNameWebAppForWin = "webserver.exe";
  14. //
  15. const string PathWebAppSourceCode = "tree/release/{0}/WebApp";
  16. const string URLWebAppDocumentation = "https://docs.unity3d.com/Packages/com.unity.renderstreaming@{0}/manual/webapp.html";
  17. public static string GetFileName()
  18. {
  19. #if UNITY_EDITOR_WIN
  20. return FileNameWebAppForWin;
  21. #elif UNITY_EDITOR_OSX
  22. return FileNameWebAppForMac;
  23. #elif UNITY_EDITOR_LINUX
  24. return FileNameWebAppForLinux;
  25. #endif
  26. }
  27. public static string GetWebAppURL(string version)
  28. {
  29. if (version == null)
  30. {
  31. version = LatestKnownVersion;
  32. }
  33. string path = $"releases/download/{version}";
  34. string fileName = GetFileName();
  35. return $"{URLRoot}/{path}/{fileName}";
  36. }
  37. public static string GetURLDocumentation(string version)
  38. {
  39. var pattern = @"\d+.\d+";
  40. var result = System.Text.RegularExpressions.Regex.Match(version, pattern);
  41. return string.Format(URLWebAppDocumentation, result.Value);
  42. }
  43. public static string GetURLSourceCode(string version)
  44. {
  45. return System.IO.Path.Combine(URLRoot, string.Format(PathWebAppSourceCode, version));
  46. }
  47. public static void DownloadCurrentVersionWebApp(string dstPath, System.Action<bool> callback) {
  48. GetPackageVersion("com.unity.renderstreaming", (version) => {
  49. DownloadWebApp(version, dstPath, callback);
  50. });
  51. }
  52. public static void DownloadWebApp(string version, string dstPath, System.Action<bool> callback)
  53. {
  54. var url = GetWebAppURL(version);
  55. var client = new WebClient();
  56. var filename = System.IO.Path.GetFileName(url);
  57. var tmpFilePath = System.IO.Path.Combine(Application.temporaryCachePath, filename);
  58. if (string.IsNullOrEmpty(dstPath))
  59. {
  60. callback?.Invoke(false);
  61. return;
  62. }
  63. client.DownloadFileCompleted += (sender, e) =>
  64. {
  65. EditorUtility.ClearProgressBar();
  66. if (e.Error != null) {
  67. //Try downloading using the latest known version to work.
  68. if (version != LatestKnownVersion) {
  69. DownloadWebApp(LatestKnownVersion, dstPath, callback);
  70. } else {
  71. Debug.LogError($"Failed downloading web server from:{url}. Error: {e.Error}");
  72. }
  73. callback?.Invoke(false);
  74. return;
  75. }
  76. if (!System.IO.File.Exists(tmpFilePath))
  77. {
  78. Debug.LogError($"Download failed. url:{url}");
  79. callback?.Invoke(false);
  80. return;
  81. }
  82. dstPath = System.IO.Path.Combine(dstPath, filename);
  83. if (System.IO.File.Exists(dstPath))
  84. {
  85. System.IO.File.Delete(dstPath);
  86. }
  87. System.IO.File.Move(tmpFilePath, dstPath);
  88. EditorUtility.RevealInFinder(dstPath);
  89. callback?.Invoke(true);
  90. };
  91. client.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) =>
  92. {
  93. var progress = e.ProgressPercentage / 100f;
  94. if(EditorUtility.DisplayCancelableProgressBar("Downloading", url, progress))
  95. {
  96. client.CancelAsync();
  97. }
  98. };
  99. client.DownloadFileAsync(new System.Uri(url), tmpFilePath);
  100. }
  101. public static void GetPackageVersion(string packageName, System.Action<string> callback)
  102. {
  103. // request package list to get package version
  104. RequestJobManager.CreateListRequest(false, true, (req) =>
  105. {
  106. var packageInfo = req.FindPackage(packageName);
  107. if (null == packageInfo)
  108. {
  109. Debug.LogError($"Not found package \"{packageName}\"");
  110. return;
  111. }
  112. callback(packageInfo.version);
  113. }, null);
  114. }
  115. }
  116. }