From 8460ecef8d799f17f1b048f010ada321c01cdbba Mon Sep 17 00:00:00 2001 From: VaDiM Date: Sun, 11 Feb 2024 19:00:03 +0300 Subject: [PATCH] Show/Export object dump if typetree dump is not available --- AssetStudio/AssetStudio.csproj | 4 +++ AssetStudio/Classes/Object.cs | 45 +++++++++++++++++++++++++++- AssetStudio/TypeTreeHelper.cs | 2 +- AssetStudioCLI/Exporter.cs | 4 +++ AssetStudioGUI/AssetStudioGUIForm.cs | 2 +- AssetStudioGUI/Exporter.cs | 4 +++ AssetStudioGUI/Studio.cs | 7 ++++- 7 files changed, 64 insertions(+), 4 deletions(-) diff --git a/AssetStudio/AssetStudio.csproj b/AssetStudio/AssetStudio.csproj index d1bd35e..e7d3c21 100644 --- a/AssetStudio/AssetStudio.csproj +++ b/AssetStudio/AssetStudio.csproj @@ -17,4 +17,8 @@ + + + + diff --git a/AssetStudio/Classes/Object.cs b/AssetStudio/Classes/Object.cs index 8ce7293..87ea4fc 100644 --- a/AssetStudio/Classes/Object.cs +++ b/AssetStudio/Classes/Object.cs @@ -1,4 +1,8 @@ -using System.Collections.Specialized; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Reflection; namespace AssetStudio { @@ -51,6 +55,25 @@ namespace AssetStudio return null; } + public string DumpObject() + { + string str = null; + try + { + str = JsonConvert.SerializeObject(this, new JsonSerializerSettings + { + Formatting = Formatting.Indented, + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + ContractResolver = new IgnorePropertiesResolver() + }).Replace(" ", " "); + } + catch + { + //ignore + } + return str; + } + public OrderedDictionary ToType() { if (serializedType?.m_Type != null) @@ -74,5 +97,25 @@ namespace AssetStudio reader.Reset(); return reader.ReadBytes((int)byteSize); } + + private class IgnorePropertiesResolver : DefaultContractResolver + { + private static readonly HashSet _ignoreProps; + + static IgnorePropertiesResolver() + { + _ignoreProps = new HashSet { "assetsFile", "reader", "version", "platform", "serializedType" }; + } + + protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) + { + JsonProperty property = base.CreateProperty(member, memberSerialization); + if (_ignoreProps.Contains(property.PropertyName)) + { + property.ShouldSerialize = _ => false; + } + return property; + } + } } } diff --git a/AssetStudio/TypeTreeHelper.cs b/AssetStudio/TypeTreeHelper.cs index 1814b29..3f3eebd 100644 --- a/AssetStudio/TypeTreeHelper.cs +++ b/AssetStudio/TypeTreeHelper.cs @@ -116,7 +116,7 @@ namespace AssetStudio { append = false; var size = reader.ReadInt32(); - reader.ReadBytes(size); + reader.BaseStream.Position += size; i += 2; sb.AppendFormat("{0}{1} {2}\r\n", (new string('\t', level)), varTypeStr, varNameStr); sb.AppendFormat("{0}{1} {2} = {3}\r\n", (new string('\t', level)), "int", "size", size); diff --git a/AssetStudioCLI/Exporter.cs b/AssetStudioCLI/Exporter.cs index e44e389..1302a90 100644 --- a/AssetStudioCLI/Exporter.cs +++ b/AssetStudioCLI/Exporter.cs @@ -303,6 +303,10 @@ namespace AssetStudioCLI var m_Type = m_MonoBehaviour.ConvertToTypeTree(Studio.assemblyLoader); str = m_MonoBehaviour.Dump(m_Type); } + if (string.IsNullOrEmpty(str)) + { + str = item.Asset.DumpObject(); + } if (str != null) { File.WriteAllText(exportFullPath, str); diff --git a/AssetStudioGUI/AssetStudioGUIForm.cs b/AssetStudioGUI/AssetStudioGUIForm.cs index 443c9da..09d93af 100644 --- a/AssetStudioGUI/AssetStudioGUIForm.cs +++ b/AssetStudioGUI/AssetStudioGUIForm.cs @@ -286,7 +286,7 @@ namespace AssetStudioGUI filterTypeToolStripMenuItem.DropDownItems.Add(typeItem); } allToolStripMenuItem.Checked = true; - var log = $"Finished loading {assetsManager.assetsFileList.Count} files with {assetListView.Items.Count} exportable assets"; + var log = $"Finished loading {assetsManager.assetsFileList.Count} file(s) with {assetListView.Items.Count} exportable assets"; var unityVer = assetsManager.assetsFileList[0].version; var m_ObjectsCount = unityVer[0] > 2020 ? assetsManager.assetsFileList.Sum(x => x.m_Objects.LongCount(y => y.classID != (int)ClassIDType.Shader)) : diff --git a/AssetStudioGUI/Exporter.cs b/AssetStudioGUI/Exporter.cs index b81ccaa..9b724bd 100644 --- a/AssetStudioGUI/Exporter.cs +++ b/AssetStudioGUI/Exporter.cs @@ -359,6 +359,10 @@ namespace AssetStudioGUI var m_Type = Studio.MonoBehaviourToTypeTree(m_MonoBehaviour); str = m_MonoBehaviour.Dump(m_Type); } + if (string.IsNullOrEmpty(str)) + { + str = item.Asset.DumpObject(); + } if (str != null) { File.WriteAllText(exportFullPath, str); diff --git a/AssetStudioGUI/Studio.cs b/AssetStudioGUI/Studio.cs index 543f136..ec7ec0d 100644 --- a/AssetStudioGUI/Studio.cs +++ b/AssetStudioGUI/Studio.cs @@ -486,7 +486,8 @@ namespace AssetStudioGUI break; } exportPath += Path.DirectorySeparatorChar; - Logger.Info($"[{exportedCount + 1}/{toExportCount}] Exporting {asset.TypeString}: {asset.Text}"); + var mode = exportType == ExportType.Dump ? "Dumping" : "Exporting"; + Logger.Info($"[{exportedCount + 1}/{toExportCount}] {mode} {asset.TypeString}: {asset.Text}"); try { switch (exportType) @@ -785,6 +786,10 @@ namespace AssetStudioGUI var type = MonoBehaviourToTypeTree(m_MonoBehaviour); str = m_MonoBehaviour.Dump(type); } + if (string.IsNullOrEmpty(str)) + { + str = obj.DumpObject(); + } return str; }