From aa47d8bf5ffa1a77ccb9913bb07398b6679e4ad9 Mon Sep 17 00:00:00 2001 From: JLChnToZ Date: Mon, 20 Nov 2023 21:38:20 +0800 Subject: [PATCH] Auto repaints inspector when version check/trusted URL list is ready --- .../Editor/Common/PackageSelfUpdater.cs | 8 ++++++++ .../Editor/Common/TrustedUrlUtils.cs | 11 +++++++++-- Packages/idv.jlchntoz.vvmw/Editor/VVMW/EditorBase.cs | 5 +++++ .../Editor/VVMW/FrontendHandlerEditor.cs | 3 ++- .../Editor/VVMW/PlayListEditorWindow.cs | 6 +++++- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Packages/idv.jlchntoz.vvmw/Editor/Common/PackageSelfUpdater.cs b/Packages/idv.jlchntoz.vvmw/Editor/Common/PackageSelfUpdater.cs index 2224c78..4dddadd 100644 --- a/Packages/idv.jlchntoz.vvmw/Editor/Common/PackageSelfUpdater.cs +++ b/Packages/idv.jlchntoz.vvmw/Editor/Common/PackageSelfUpdater.cs @@ -32,6 +32,8 @@ public class PackageSelfUpdater { public bool IsInstalledManually => isInstalledManually; + public event Action OnVersionRefreshed; + public PackageSelfUpdater(Assembly assembly, string listingsID, string listingsURL) : this(PackageManagerPackageInfo.FindForAssembly(assembly), listingsID, listingsURL) { } @@ -67,6 +69,12 @@ public void CheckInstallation() { var manifest = VPMProjectManifest.Load(Resolver.ProjectDir); isInstalledManually = !manifest.locked.ContainsKey(packageName) && !manifest.dependencies.ContainsKey(packageName); #endif + CheckInstallationCallback().Forget(); + } + + async UniTask CheckInstallationCallback() { + await UniTask.SwitchToMainThread(); + OnVersionRefreshed?.Invoke(); } public void ResolveInstallation() { diff --git a/Packages/idv.jlchntoz.vvmw/Editor/Common/TrustedUrlUtils.cs b/Packages/idv.jlchntoz.vvmw/Editor/Common/TrustedUrlUtils.cs index 312822a..5e533be 100644 --- a/Packages/idv.jlchntoz.vvmw/Editor/Common/TrustedUrlUtils.cs +++ b/Packages/idv.jlchntoz.vvmw/Editor/Common/TrustedUrlUtils.cs @@ -10,8 +10,9 @@ namespace JLChnToZ.VRC.VVMW.Editors { public sealed class TrustedUrlUtils { + public static event Action OnTrustedUrlsReady; static readonly Dictionary instances = new Dictionary(); - static readonly AsyncLazy getTrustedUrlsTask = UniTask.Lazy(GetTrustedUrlsLazy); + static AsyncLazy getTrustedUrlsTask = UniTask.Lazy(GetTrustedUrlsLazy); static GUIContent tempContent, warningContent; readonly Dictionary trustedDomains = new Dictionary(); readonly Dictionary messageCache = new Dictionary(); @@ -65,7 +66,12 @@ static async UniTask GetTrustedUrlsLazy() { () => initState.TrySetResult(), () => initState.TrySetException(new Exception("Failed to initialize VRCSDK config.")) ); - await initState.Task; + try { + await initState.Task; + } catch (Exception ex) { + getTrustedUrlsTask = UniTask.Lazy(GetTrustedUrlsLazy); // Retry on next time. + throw ex; + } } if (vrcsdkConfig.HasKey("urlList")) { var trustedUrls = vrcsdkConfig.GetList("urlList"); @@ -77,6 +83,7 @@ static async UniTask GetTrustedUrlsLazy() { instances[TrustedUrlTypes.ImageUrl].trustedUrls = vrcsdkConfig.GetList("imageHostUrlList"); if (vrcsdkConfig.HasKey("stringHostUrlList")) instances[TrustedUrlTypes.StringUrl].trustedUrls = vrcsdkConfig.GetList("stringHostUrlList"); + OnTrustedUrlsReady?.Invoke(); } public static void CopyTrustedUrlsToStringArray(SerializedProperty stringArray, TrustedUrlTypes urlType) => diff --git a/Packages/idv.jlchntoz.vvmw/Editor/VVMW/EditorBase.cs b/Packages/idv.jlchntoz.vvmw/Editor/VVMW/EditorBase.cs index bb44cec..f87869a 100644 --- a/Packages/idv.jlchntoz.vvmw/Editor/VVMW/EditorBase.cs +++ b/Packages/idv.jlchntoz.vvmw/Editor/VVMW/EditorBase.cs @@ -27,6 +27,11 @@ protected virtual void OnEnable() { var assetPath = AssetDatabase.GUIDToAssetPath(fontGUID); if (!string.IsNullOrEmpty(assetPath)) font = AssetDatabase.LoadAssetAtPath(assetPath); } + selfUpdater.OnVersionRefreshed += Repaint; + } + + protected virtual void OnDisable() { + if (selfUpdater != null) selfUpdater.OnVersionRefreshed -= Repaint; } public override void OnInspectorGUI() { diff --git a/Packages/idv.jlchntoz.vvmw/Editor/VVMW/FrontendHandlerEditor.cs b/Packages/idv.jlchntoz.vvmw/Editor/VVMW/FrontendHandlerEditor.cs index 266a1d6..0e94691 100644 --- a/Packages/idv.jlchntoz.vvmw/Editor/VVMW/FrontendHandlerEditor.cs +++ b/Packages/idv.jlchntoz.vvmw/Editor/VVMW/FrontendHandlerEditor.cs @@ -37,7 +37,8 @@ protected override void OnEnable() { PlayListEditorWindow.OnFrontendUpdated += OnFrontEndUpdated; } - protected virtual void OnDisable() { + protected override void OnDisable() { + base.OnDisable(); PlayListEditorWindow.OnFrontendUpdated -= OnFrontEndUpdated; } diff --git a/Packages/idv.jlchntoz.vvmw/Editor/VVMW/PlayListEditorWindow.cs b/Packages/idv.jlchntoz.vvmw/Editor/VVMW/PlayListEditorWindow.cs index 7e043b3..46aaaf5 100644 --- a/Packages/idv.jlchntoz.vvmw/Editor/VVMW/PlayListEditorWindow.cs +++ b/Packages/idv.jlchntoz.vvmw/Editor/VVMW/PlayListEditorWindow.cs @@ -57,9 +57,13 @@ void OnEnable() { elementHeight = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing, showDefaultBackground = false, }; + TrustedUrlUtils.OnTrustedUrlsReady += Repaint; } - void OnDisable() => SaveIfRequired(); + void OnDisable() { + TrustedUrlUtils.OnTrustedUrlsReady -= Repaint; + SaveIfRequired(); + } void OnGUI() { using (new EditorGUILayout.HorizontalScope(EditorStyles.toolbar)) {