From d54d186cc38b3f5ebbbeddace3f0e22306122f23 Mon Sep 17 00:00:00 2001 From: cnlohr Date: Fri, 3 Sep 2021 22:36:03 -0700 Subject: [PATCH 1/2] Add function to refresh all assets in tree, for when udonsharp assets become unassociated. --- .../UdonSharp/Editor/UdonSharpProgramAsset.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs b/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs index f17ea1ad..2dbb370a 100644 --- a/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs +++ b/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs @@ -300,6 +300,40 @@ public static UdonSharpProgramAsset[] GetAllUdonSharpPrograms() return (UdonSharpProgramAsset[])_programAssetCache.Clone(); } + [MenuItem("Window/Udon Sharp/Refresh All UdonSharp Assets")] + static public void UdonSharpCheckAbsent() + { + Debug.Log( "Checking Absent" ); + + string[] udonSharpDataAssets = AssetDatabase.FindAssets($"t:{nameof(UdonSharpProgramAsset)}"); + string[] udonSharpNames = new string[udonSharpDataAssets.Length]; + Debug.Log( $"Found {udonSharpDataAssets.Length} assets." ); + + _programAssetCache = new UdonSharpProgramAsset[udonSharpDataAssets.Length]; + + for (int i = 0; i < _programAssetCache.Length; ++i) + { + udonSharpDataAssets[i] = AssetDatabase.GUIDToAssetPath(udonSharpDataAssets[i]); + } + + foreach(string s in AssetDatabase.GetAllAssetPaths() ) + { + if(!udonSharpDataAssets.Contains(s)) + { + Type t = AssetDatabase.GetMainAssetTypeAtPath(s); + if (t != null && t.FullName == "UdonSharp.UdonSharpProgramAsset") + { + Debug.Log( $"Trying to recover {s}" ); + Selection.activeObject = AssetDatabase.LoadAssetAtPath(s); + } + } + } + + ClearProgramAssetCache(); + + GetAllUdonSharpPrograms(); + } + [PublicAPI] public static bool AnyUdonSharpScriptHasError() { From f7a5d083b0c5bc1ff9a5768a013bc7822bed6e4d Mon Sep 17 00:00:00 2001 From: cnlohr Date: Sun, 5 Sep 2021 20:02:40 -0700 Subject: [PATCH 2/2] * Double-check for possible nulls when reloading. * Keep refreshing until no additional scripts get picked up. --- .../UdonSharp/Editor/UdonSharpProgramAsset.cs | 86 +++++++++++-------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs b/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs index 2dbb370a..4fd5bd4d 100644 --- a/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs +++ b/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs @@ -257,7 +257,7 @@ public static UdonSharpProgramAsset[] GetAllUdonSharpPrograms() foreach (UdonSharpProgramAsset fallbackAsset in fallbackAssets1) { - if (!_programAssetCache.Contains(fallbackAsset)) + if (_programAssetCache != null && fallbackAsset != null && !_programAssetCache.Contains(fallbackAsset)) { Debug.LogWarning($"Repairing program asset {fallbackAsset} which Unity has broken"); neededFallback = true; @@ -271,7 +271,7 @@ public static UdonSharpProgramAsset[] GetAllUdonSharpPrograms() var fallbackAssets2 = AssetDatabase.FindAssets($"t:{nameof(UdonProgramAsset)}").Select(e => AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(e))).OfType(); foreach (UdonSharpProgramAsset fallbackAsset in fallbackAssets2) { - if (!_programAssetCache.Contains(fallbackAsset)) + if (_programAssetCache != null && fallbackAsset != null && !_programAssetCache.Contains(fallbackAsset)) { Debug.LogWarning($"Repairing program asset {fallbackAsset} which Unity has broken pass 2"); neededFallback = true; @@ -300,39 +300,55 @@ public static UdonSharpProgramAsset[] GetAllUdonSharpPrograms() return (UdonSharpProgramAsset[])_programAssetCache.Clone(); } - [MenuItem("Window/Udon Sharp/Refresh All UdonSharp Assets")] - static public void UdonSharpCheckAbsent() - { - Debug.Log( "Checking Absent" ); - - string[] udonSharpDataAssets = AssetDatabase.FindAssets($"t:{nameof(UdonSharpProgramAsset)}"); - string[] udonSharpNames = new string[udonSharpDataAssets.Length]; - Debug.Log( $"Found {udonSharpDataAssets.Length} assets." ); - - _programAssetCache = new UdonSharpProgramAsset[udonSharpDataAssets.Length]; - - for (int i = 0; i < _programAssetCache.Length; ++i) - { - udonSharpDataAssets[i] = AssetDatabase.GUIDToAssetPath(udonSharpDataAssets[i]); - } - - foreach(string s in AssetDatabase.GetAllAssetPaths() ) - { - if(!udonSharpDataAssets.Contains(s)) - { - Type t = AssetDatabase.GetMainAssetTypeAtPath(s); - if (t != null && t.FullName == "UdonSharp.UdonSharpProgramAsset") - { - Debug.Log( $"Trying to recover {s}" ); - Selection.activeObject = AssetDatabase.LoadAssetAtPath(s); - } - } - } - - ClearProgramAssetCache(); - - GetAllUdonSharpPrograms(); - } + [MenuItem("Window/Udon Sharp/Refresh All UdonSharp Assets")] + static public void UdonSharpCheckAbsent() + { + Debug.Log( "Checking Absent" ); + + int cycles = -1; + int lastNumAssets; + int currentNumAssets; + string[] udonSharpDataAssets; + + // Loop until we stop picking up assets. + do + { + udonSharpDataAssets = AssetDatabase.FindAssets($"t:{nameof(UdonSharpProgramAsset)}"); + lastNumAssets = udonSharpDataAssets.Length; + string[] udonSharpNames = new string[udonSharpDataAssets.Length]; + Debug.Log( $"Found {udonSharpDataAssets.Length} assets." ); + + _programAssetCache = new UdonSharpProgramAsset[udonSharpDataAssets.Length]; + + for (int i = 0; i < _programAssetCache.Length; ++i) + { + udonSharpDataAssets[i] = AssetDatabase.GUIDToAssetPath(udonSharpDataAssets[i]); + } + + foreach(string s in AssetDatabase.GetAllAssetPaths() ) + { + if(!udonSharpDataAssets.Contains(s)) + { + Type t = AssetDatabase.GetMainAssetTypeAtPath(s); + if (t != null && t.FullName == "UdonSharp.UdonSharpProgramAsset") + { + Debug.Log( $"Trying to recover {s}" ); + Selection.activeObject = AssetDatabase.LoadAssetAtPath(s); + } + } + } + + ClearProgramAssetCache(); + + GetAllUdonSharpPrograms(); + + currentNumAssets = AssetDatabase.FindAssets($"t:{nameof(UdonSharpProgramAsset)}").Length; + Debug.Log( $"Checking to see if we need to re-run. Last: {lastNumAssets}, This: {currentNumAssets}" ); + cycles++; + } while( lastNumAssets != currentNumAssets ); + + Debug.Log( $"Completed {cycles} refresh cycles, found {lastNumAssets} assets." ); + } [PublicAPI] public static bool AnyUdonSharpScriptHasError()