S3UploadController.cs 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using UnityEditor;
  6. using UnityEngine;
  7. public static class S3UploadController
  8. {
  9. public static bool IsUploading { get; private set; }
  10. static CancellationTokenSource cancellation;
  11. static readonly object progressLock = new object();
  12. static string currentKey;
  13. static long sentBytes, totalBytes;
  14. static int completedFiles, totalFiles;
  15. static double nextProgress;
  16. public static async void ChooseAndUpload(string downloadUrl)
  17. {
  18. if (IsUploading || HotPatchResourceActions.IsBuilding || EditorApplication.isCompiling || EditorApplication.isPlayingOrWillChangePlaymode || BuildPipeline.isBuildingPlayer) return;
  19. bool locked = false;
  20. try
  21. {
  22. var latest = HotPatchResourceActions.LatestDirectory;
  23. if (string.IsNullOrEmpty(latest)) throw new InvalidOperationException("没有可用的最近打包资源,请先执行一键打包资源。");
  24. var prefix = S3ResourceUploader.TargetPrefix(downloadUrl);
  25. var configuration = S3UploadSettings.LoadConfiguration();
  26. var bucket = configuration.Bucket;
  27. var region = configuration.Region;
  28. S3UploadSettings.ValidateDestination(bucket, region);
  29. var credentials = S3UploadSettings.LoadCredentials();
  30. S3UploadSettings.ValidateCredentials(credentials);
  31. var directory = EditorUtility.OpenFolderPanel("选择上传资源文件夹", latest, "");
  32. if (string.IsNullOrEmpty(directory)) return;
  33. var files = S3ResourceUploader.CreatePlan(directory, prefix);
  34. EditorPrefs.SetString("AppRuntimes.S3.LastFolder", directory);
  35. IsUploading = true;
  36. cancellation = new CancellationTokenSource();
  37. totalFiles = files.Count; completedFiles = 0; sentBytes = totalBytes = 0; currentKey = "准备上传"; nextProgress = 0;
  38. EditorApplication.LockReloadAssemblies(); locked = true;
  39. EditorApplication.update += ShowProgress;
  40. EditorApplication.quitting += Cancel;
  41. EditorApplication.playModeStateChanged += OnPlayMode;
  42. Debug.Log("[资源上传] 开始上传到 s3://" + bucket + "/" + prefix + "/,对象数: " + files.Count);
  43. await Task.Run(() => S3ResourceUploader.UploadAsync(files, bucket, region, credentials, SetProgress, cancellation.Token));
  44. EditorApplication.update -= ShowProgress;
  45. Debug.Log("[资源上传] 上传完成: s3://" + bucket + "/" + prefix + "/");
  46. EditorUtility.ClearProgressBar();
  47. EditorUtility.DisplayDialog("上传资源", "上传完成,共 " + files.Count + " 个对象。\ns3://" + bucket + "/" + prefix + "/", "确定");
  48. }
  49. catch (OperationCanceledException) { Debug.LogWarning("[资源上传] 上传已取消或请求超时,可重新上传。"); }
  50. catch (Exception exception)
  51. {
  52. EditorApplication.update -= ShowProgress;
  53. Debug.LogError("[资源上传] " + exception.Message);
  54. EditorUtility.ClearProgressBar();
  55. EditorUtility.DisplayDialog("上传资源失败", exception.Message, "确定");
  56. }
  57. finally
  58. {
  59. EditorApplication.update -= ShowProgress;
  60. EditorApplication.quitting -= Cancel;
  61. EditorApplication.playModeStateChanged -= OnPlayMode;
  62. EditorUtility.ClearProgressBar();
  63. cancellation?.Dispose(); cancellation = null;
  64. IsUploading = false;
  65. if (locked) EditorApplication.UnlockReloadAssemblies();
  66. }
  67. }
  68. static void SetProgress(int count, string key, long sent, long total)
  69. { lock (progressLock) { completedFiles = count; currentKey = key; sentBytes = sent; totalBytes = total; } }
  70. static void ShowProgress()
  71. {
  72. if (EditorApplication.timeSinceStartup < nextProgress) return;
  73. nextProgress = EditorApplication.timeSinceStartup + .1;
  74. lock (progressLock)
  75. {
  76. var progress = totalBytes > 0 ? (float)((double)sentBytes / totalBytes) : (float)completedFiles / Math.Max(1, totalFiles);
  77. if (EditorUtility.DisplayCancelableProgressBar("上传资源到 S3", completedFiles + "/" + totalFiles + " " + currentKey, progress)) Cancel();
  78. }
  79. }
  80. static void Cancel() => cancellation?.Cancel();
  81. static void OnPlayMode(PlayModeStateChange state) { if (state == PlayModeStateChange.ExitingEditMode) Cancel(); }
  82. }