S3UploadController.cs 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 || EditorApplication.isCompiling || EditorApplication.isPlayingOrWillChangePlaymode || BuildPipeline.isBuildingPlayer) return;
  19. bool locked = false;
  20. try
  21. {
  22. var prefix = S3ResourceUploader.TargetPrefix(downloadUrl);
  23. var bucket = S3UploadSettings.Bucket;
  24. var region = S3UploadSettings.Region;
  25. S3UploadSettings.ValidateDestination(bucket, region);
  26. var credentials = S3UploadSettings.LoadCredentials();
  27. S3UploadSettings.ValidateCredentials(credentials);
  28. var directory = EditorUtility.OpenFolderPanel("选择上传资源文件夹", EditorPrefs.GetString("AppRuntimes.S3.LastFolder", Path.GetFullPath("Build/HotUpdate")), "");
  29. if (string.IsNullOrEmpty(directory)) return;
  30. var files = S3ResourceUploader.CreatePlan(directory, prefix);
  31. EditorPrefs.SetString("AppRuntimes.S3.LastFolder", directory);
  32. IsUploading = true;
  33. cancellation = new CancellationTokenSource();
  34. totalFiles = files.Count; completedFiles = 0; sentBytes = totalBytes = 0; currentKey = "准备上传"; nextProgress = 0;
  35. EditorApplication.LockReloadAssemblies(); locked = true;
  36. EditorApplication.update += ShowProgress;
  37. EditorApplication.quitting += Cancel;
  38. EditorApplication.playModeStateChanged += OnPlayMode;
  39. Debug.Log("[资源上传] 开始上传到 s3://" + bucket + "/" + prefix + "/,对象数: " + files.Count);
  40. await Task.Run(() => S3ResourceUploader.UploadAsync(files, bucket, region, credentials, SetProgress, cancellation.Token));
  41. EditorApplication.update -= ShowProgress;
  42. Debug.Log("[资源上传] 上传完成: s3://" + bucket + "/" + prefix + "/");
  43. EditorUtility.ClearProgressBar();
  44. EditorUtility.DisplayDialog("上传资源", "上传完成,共 " + files.Count + " 个对象。\ns3://" + bucket + "/" + prefix + "/", "确定");
  45. }
  46. catch (OperationCanceledException) { Debug.LogWarning("[资源上传] 上传已取消或请求超时,可重新上传。"); }
  47. catch (Exception exception)
  48. {
  49. EditorApplication.update -= ShowProgress;
  50. Debug.LogError("[资源上传] " + exception.Message);
  51. EditorUtility.ClearProgressBar();
  52. EditorUtility.DisplayDialog("上传资源失败", exception.Message, "确定");
  53. }
  54. finally
  55. {
  56. EditorApplication.update -= ShowProgress;
  57. EditorApplication.quitting -= Cancel;
  58. EditorApplication.playModeStateChanged -= OnPlayMode;
  59. EditorUtility.ClearProgressBar();
  60. cancellation?.Dispose(); cancellation = null;
  61. IsUploading = false;
  62. if (locked) EditorApplication.UnlockReloadAssemblies();
  63. }
  64. }
  65. static void SetProgress(int count, string key, long sent, long total)
  66. { lock (progressLock) { completedFiles = count; currentKey = key; sentBytes = sent; totalBytes = total; } }
  67. static void ShowProgress()
  68. {
  69. if (EditorApplication.timeSinceStartup < nextProgress) return;
  70. nextProgress = EditorApplication.timeSinceStartup + .1;
  71. lock (progressLock)
  72. {
  73. var progress = totalBytes > 0 ? (float)((double)sentBytes / totalBytes) : (float)completedFiles / Math.Max(1, totalFiles);
  74. if (EditorUtility.DisplayCancelableProgressBar("上传资源到 S3", completedFiles + "/" + totalFiles + " " + currentKey, progress)) Cancel();
  75. }
  76. }
  77. static void Cancel() => cancellation?.Cancel();
  78. static void OnPlayMode(PlayModeStateChange state) { if (state == PlayModeStateChange.ExitingEditMode) Cancel(); }
  79. }