Skip to content

Commit

Permalink
cancel event, show progress, exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
I5UCC committed Apr 25, 2024
1 parent e45e4bd commit 4f33b11
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 16 deletions.
70 changes: 63 additions & 7 deletions Editor/VRCMultiUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using VRC.Core;
using System.Threading;
using System.Threading.Tasks;
using VRC.SDKBase.Editor;

namespace VRCMultiUploader
{
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
}
}
59 changes: 51 additions & 8 deletions Editor/VRCMultiUploader_PopupWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextAsset>("Packages/com.i5ucc.vrcmultiuploader/package.json");
if (package != null)
{
var json = JsonUtility.FromJson<PackageJson>(package.text);
version = json.version;
}
amountofAvatars = amount;

var window = CreateInstance<PopupWindow>();
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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -99,6 +118,7 @@ public void ShowOKButton()

infoBox.Remove(progress);
infoBox.Remove(cancel_button);
infoBox.Remove(bottomlabel);
infoBox.Add(ok_button);
RepaintNow();
}
Expand All @@ -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[] { });
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 4f33b11

Please sign in to comment.