From 4f33b11f42ec2a2bd9519ced628af821f0815d51 Mon Sep 17 00:00:00 2001 From: Isaak Date: Thu, 25 Apr 2024 21:56:47 +0200 Subject: [PATCH] cancel event, show progress, exception handling --- Editor/VRCMultiUploader.cs | 70 +++++++++++++++++++++++--- Editor/VRCMultiUploader_PopupWindow.cs | 59 +++++++++++++++++++--- package.json | 2 +- 3 files changed, 115 insertions(+), 16 deletions(-) diff --git a/Editor/VRCMultiUploader.cs b/Editor/VRCMultiUploader.cs index e3bf553..e135b64 100644 --- a/Editor/VRCMultiUploader.cs +++ b/Editor/VRCMultiUploader.cs @@ -6,6 +6,7 @@ using VRC.Core; using System.Threading; using System.Threading.Tasks; +using VRC.SDKBase.Editor; namespace VRCMultiUploader { @@ -46,6 +47,21 @@ public static async Task BuildAll(bool test = false) return; } + builder.OnSdkBuildProgress += (sender, message) => progressWindow.SetBottomLabel(message); + builder.OnSdkBuildFinish += (sender, message) => progressWindow.SetBottomLabel("Uploading...", Color.yellow); + // builder.OnSdkUploadProgress += (sender, message) => progressWindow.SetBottomLabel($"{message.status} {(int)(message.percentage * 100)}%"); // Makes the Upload Process fail for some reason? + + CancellationTokenSource cts = new(); + progressWindow.SetCancelEvent(() => + { + progressWindow.ShowOKButton(); + progressWindow.cancelled = true; + progressWindow.Progress(0, $"Cancelled!", true); + builder.CancelUpload(); + }); + + bool success = false; + for (int i = 0; i < avatarCount; i++) { GameObject avatarObject = avatars[i].gameObject; @@ -67,36 +83,76 @@ public static async Task BuildAll(bool test = false) { Debug.Log($"Uploading avatar: {avatarObject.name} with blueprintId: {blueprintId}"); VRCAvatar av = await VRCApi.GetAvatar(blueprintId); - await builder.BuildAndUpload(avatarObject, av); + await builder.BuildAndUpload(avatarObject, av, cancellationToken: cts.Token); + success = true; } else { Debug.Log($"Uploading avatar {avatarObject.name} for testing."); await builder.BuildAndTest(avatarObject); + success = true; } - if (progressWindow.cancelled) - { - progressWindow.Progress(i, $"Cancelled! ({i + 1}/{avatarCount})", true); - return; - } + if (progressWindow.cancelled) return; progressWindow.Progress(i, "Finished uploading avatar:\n" + avatarObject.name, true); - Thread.Sleep(2000); } catch (NullReferenceException e) { progressWindow.Close(); EditorUtility.DisplayDialog("MultiUploader - Error", "SDK Control Panel hasn't been opened yet. Please open the SDK Control Panel and try again.", "OK"); Debug.LogError(e.Message + e.StackTrace); + success = false; return; } + catch (BuilderException e) + { + progressWindow.SetBottomLabel("Upload Cancelled", new Color(255, 165, 0)); + progressWindow.cancelled = true; + Thread.Sleep(4000); + Debug.LogError(e.Message + e.StackTrace); + success = false; + } + catch (ValidationException e) + { + EditorUtility.DisplayDialog("MultiUploader - Error", e.Message + "\n" + string.Join("\n", e.Errors), "OK"); + Debug.LogError(e.Message + e.StackTrace); + success = false; + } + catch (OwnershipException e) + { + EditorUtility.DisplayDialog("MultiUploader - Error", e.Message, "OK"); + Debug.LogError(e.Message + e.StackTrace); + success = false; + } + catch (UploadException e) + { + EditorUtility.DisplayDialog("MultiUploader - Error", e.Message, "OK"); + Debug.LogError(e.Message + e.StackTrace); + success = false; + } catch (Exception e) { Debug.LogError(e.Message + e.StackTrace); + EditorUtility.DisplayDialog("MultiUploader - Unexpected Error", e.Message + "\n" + e.StackTrace, "OK"); + success = false; + } + finally + { + if (!success) + { + progressWindow.SetBottomLabel("Upload Failed", Color.red); + Thread.Sleep(4000); + } + else + { + progressWindow.SetBottomLabel("Upload Successful!", Color.green); + Thread.Sleep(2000); + } } } progressWindow.Progress(avatarCount, $"Finished uploading all Avatars! ({avatarCount}/{avatarCount})", true); progressWindow.ShowOKButton(); + cts.Dispose(); } } } diff --git a/Editor/VRCMultiUploader_PopupWindow.cs b/Editor/VRCMultiUploader_PopupWindow.cs index 24adbf8..482ae67 100644 --- a/Editor/VRCMultiUploader_PopupWindow.cs +++ b/Editor/VRCMultiUploader_PopupWindow.cs @@ -12,19 +12,28 @@ public class PopupWindow : EditorWindow { private Label toplabel; private Label label; + private Label bottomlabel; private Button ok_button; private Button cancel_button; private ProgressBar progress; private VisualElement infoBox; private static int amountofAvatars; + private static string version = "DEV"; public bool cancelled = false; public static PopupWindow Create(int amount) { + TextAsset package = AssetDatabase.LoadAssetAtPath("Packages/com.i5ucc.vrcmultiuploader/package.json"); + if (package != null) + { + var json = JsonUtility.FromJson(package.text); + version = json.version; + } amountofAvatars = amount; + var window = CreateInstance(); var mainWindowPos = GetEditorMainWindowPos(); - var size = new Vector2(500, 125); + var size = new Vector2(500, 150); window.position = new Rect(mainWindowPos.xMin + (mainWindowPos.width - size.x) * 0.5f, mainWindowPos.yMax * 0.5f + 150, size.x, size.y); window.ShowPopup(); return window; @@ -43,9 +52,13 @@ public void OnEnable() var root = rootVisualElement; infoBox = new VisualElement(); + infoBox.style.borderTopLeftRadius = 10; + infoBox.style.borderTopRightRadius = 10; + infoBox.style.borderBottomLeftRadius = 10; + infoBox.style.borderBottomRightRadius = 10; root.Add(infoBox); - toplabel = new Label("- VRCMultiUploader v0.1.4 by I5UCC -"); + toplabel = new Label($"- VRCMultiUploader v{version} by I5UCC -"); toplabel.style.paddingTop = 7; toplabel.style.paddingBottom = 10; toplabel.style.paddingLeft = 20; @@ -64,18 +77,24 @@ public void OnEnable() label = new Label(""); label.style.paddingTop = 5; - label.style.paddingBottom = 0; + label.style.paddingBottom = 2; label.style.paddingLeft = 20; label.style.paddingRight = 20; label.style.fontSize = 24; label.style.unityTextAlign = TextAnchor.MiddleCenter; infoBox.Add(label); - cancel_button = new Button(() => - { - cancelled = true; - ShowOKButton(); - }); + bottomlabel = new Label("Starting build process..."); + bottomlabel.style.paddingTop = 6; + bottomlabel.style.paddingBottom = 10; + bottomlabel.style.paddingLeft = 20; + bottomlabel.style.paddingRight = 20; + bottomlabel.style.fontSize = 13; + bottomlabel.style.backgroundColor = new Color(0, 0, 0, 0.1f); + bottomlabel.style.unityTextAlign = TextAnchor.MiddleCenter; + infoBox.Add(bottomlabel); + + cancel_button = new Button(); cancel_button.text = "X"; cancel_button.style.width = 20; cancel_button.style.height = 20; @@ -99,6 +118,7 @@ public void ShowOKButton() infoBox.Remove(progress); infoBox.Remove(cancel_button); + infoBox.Remove(bottomlabel); infoBox.Add(ok_button); RepaintNow(); } @@ -115,6 +135,19 @@ public void Progress(int current, string info, bool finished = false) RepaintNow(); } + public void SetCancelEvent(Action action) + { + cancel_button.clicked += action; + } + + public void SetBottomLabel(string text, Color color = default) + { + bottomlabel.text = text; + if (color == default) color = Color.white; + bottomlabel.style.color = color; + RepaintNow(); + } + private void RepaintNow() { GetType().GetMethod("RepaintImmediately", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Invoke(this, new object[] { }); @@ -166,4 +199,14 @@ public static Rect GetEditorMainWindowPos() } } + [Serializable] + class PackageJson + { + public string name; + public string displayName; + public string version; + public string description; + public string[] files; + public string type; + } } diff --git a/package.json b/package.json index 591cdca..f21432a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.i5ucc.vrcmultiuploader", "displayName": "VRCMultiUploader", - "version": "0.1.4", + "version": "0.1.5", "description": "Tool for uploading Multiple VRChat Avatars at once.", "files": [ "VRCMultiUploader.cs"