From c854265ff214bf06348194d5adf2cee5272978c9 Mon Sep 17 00:00:00 2001 From: NGnius Date: Sat, 9 Nov 2019 16:33:08 -0500 Subject: [PATCH] Add asset bundle inspection functionality --- Dissonance/Dissonance.csproj | 3 + Dissonance/DissonancePlugin.cs | 6 +- Dissonance/Inspector/AssetInspector.cs | 103 ++++++++++++++++++++++++ Dissonance/Inspector/ObjectInspector.cs | 2 +- 4 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 Dissonance/Inspector/AssetInspector.cs diff --git a/Dissonance/Dissonance.csproj b/Dissonance/Dissonance.csproj index 443fb59..2a702f7 100644 --- a/Dissonance/Dissonance.csproj +++ b/Dissonance/Dissonance.csproj @@ -18,6 +18,9 @@ ..\ref\Gamecraft_Data\Managed\UnityEngine.CoreModule.dll + + ..\ref\Gamecraft_Data\Managed\UnityEngine.AssetBundleModule.dll + diff --git a/Dissonance/DissonancePlugin.cs b/Dissonance/DissonancePlugin.cs index c1a2044..a18dd3e 100644 --- a/Dissonance/DissonancePlugin.cs +++ b/Dissonance/DissonancePlugin.cs @@ -14,14 +14,13 @@ namespace Dissonance public string Name { get; } = "Dissonance"; - public string Version { get; } = "A"; + public string Version { get; } = "B"; public string HarmonyID { get; } = "org.git.exmods.modtainers.dissonance"; public void OnApplicationQuit() { - harmony.UnpatchAll(HarmonyID); - Inspector.ObjectInspector.LogAll(); + harmony.UnpatchAll(HarmonyID); // likely unnecessary Debug.Log(Name + " shutdown complete"); } @@ -32,7 +31,6 @@ namespace Dissonance harmony = HarmonyInstance.Create(HarmonyID); harmony.PatchAll(Assembly.GetExecutingAssembly()); } - Inspector.ObjectInspector.LogAll(); Debug.Log(Name + " start & patch complete"); } diff --git a/Dissonance/Inspector/AssetInspector.cs b/Dissonance/Inspector/AssetInspector.cs new file mode 100644 index 0000000..0bad368 --- /dev/null +++ b/Dissonance/Inspector/AssetInspector.cs @@ -0,0 +1,103 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UnityEngine; + +namespace Dissonance.Inspector +{ + public static class AssetInspector + { + private static AssetBundle bundle; + + public static bool Init() + { + foreach(AssetBundle ab in AssetBundle.GetAllLoadedAssetBundles()) + { + if (ab != null) + { + bundle = ab; + } + } + Debug.Log($"Dissonance: Found AssetBundle {bundle.name}"); + return bundle != null; + } + + public static void Log() where T : UnityEngine.Object + { + Instance[] instances = Inspect(); + string[] filelines = new string[instances.Length]; + for (int i = 0; i < instances.Length; i++) + { + filelines[i] = instances[i].ToString() + ","; + } + Debug.Log("AssetInspector Log dump\n[" + string.Join(",\n", filelines) + "]"); + } + + public static void LogNames() where T : UnityEngine.Object + { + Debug.Log(string.Join("\n", InspectNames())); + } + + public static void LogAll() + { + Log(); + } + + public static void LogNamesAll() + { + LogNames(); + } + + public static void Dump(string filename) where T : UnityEngine.Object + { + Instance[] instances = Inspect(); + string[] filelines = new string[instances.Length]; + for (int i = 0; i < instances.Length; i++) + { + filelines[i] = instances[i].ToString() + ","; + } + File.WriteAllLines(filename, filelines); + } + + public static void DumpNames(string filename) where T : UnityEngine.Object + { + File.WriteAllLines(filename, InspectNames()); + } + + public static void DumpAll(string filename) + { + Dump(filename); + } + + public static void DumpNamesAll(string filename) + { + DumpNames(filename); + } + + public static Instance[] Inspect() where T : UnityEngine.Object + { + if (bundle == null) Init(); + UnityEngine.Object[] assets = bundle.LoadAllAssets(); + Instance[] instances = new Instance[assets.Length]; + for (int i = 0; i < assets.Length; i++) + { + instances[i] = new Instance(assets[i]); + } + return instances; + } + + public static string[] InspectNames() where T : UnityEngine.Object + { + if (bundle == null) Init(); + UnityEngine.Object[] assets = bundle.LoadAllAssets(); + string[] names = new string[assets.Length]; + for (int i = 0; i < assets.Length; i++) + { + names[i] = assets[i].name; + } + return names; + } + } +} diff --git a/Dissonance/Inspector/ObjectInspector.cs b/Dissonance/Inspector/ObjectInspector.cs index 292a4a3..0445edd 100644 --- a/Dissonance/Inspector/ObjectInspector.cs +++ b/Dissonance/Inspector/ObjectInspector.cs @@ -6,7 +6,7 @@ using UnityEngine; namespace Dissonance.Inspector { - public class ObjectInspector + public static class ObjectInspector { public static void Log() where T : UnityEngine.Object {