From 63564d5fff2b721af6559b41af01d3303d47078d Mon Sep 17 00:00:00 2001 From: VaDiM Date: Sun, 2 Jul 2023 23:36:22 +0300 Subject: [PATCH] Add assembly folder support for Live2D export --- AssetStudioCLI/Exporter.cs | 43 ++++++------------- AssetStudioCLI/Options/CLIOptions.cs | 1 + AssetStudioCLI/Studio.cs | 17 ++++++-- AssetStudioGUI/Studio.cs | 2 +- .../CubismLive2DExtractor/Live2DExtractor.cs | 25 ++++++++--- 5 files changed, 49 insertions(+), 39 deletions(-) diff --git a/AssetStudioCLI/Exporter.cs b/AssetStudioCLI/Exporter.cs index 0b70cfd..c074def 100644 --- a/AssetStudioCLI/Exporter.cs +++ b/AssetStudioCLI/Exporter.cs @@ -9,8 +9,6 @@ namespace AssetStudioCLI { internal static class Exporter { - public static AssemblyLoader assemblyLoader = new AssemblyLoader(); - public static bool ExportTexture2D(AssetItem item, string exportPath, CLIOptions options) { var m_Texture2D = (Texture2D)item.Asset; @@ -162,7 +160,7 @@ namespace AssetStudioCLI return true; } - public static bool ExportMonoBehaviour(AssetItem item, string exportPath, CLIOptions options) + public static bool ExportMonoBehaviour(AssetItem item, string exportPath, AssemblyLoader assemblyLoader) { if (!TryExportFile(exportPath, item, ".json", out var exportFullPath)) return false; @@ -170,14 +168,18 @@ namespace AssetStudioCLI var type = m_MonoBehaviour.ToType(); if (type == null) { - var m_Type = MonoBehaviourToTypeTree(m_MonoBehaviour, options); + var m_Type = m_MonoBehaviour.ConvertToTypeTree(assemblyLoader); type = m_MonoBehaviour.ToType(m_Type); } - var str = JsonConvert.SerializeObject(type, Formatting.Indented); - File.WriteAllText(exportFullPath, str); + if (type != null) + { + var str = JsonConvert.SerializeObject(type, Formatting.Indented); + File.WriteAllText(exportFullPath, str); - Logger.Debug($"{item.TypeString}: \"{item.Text}\" exported to \"{exportFullPath}\""); - return true; + Logger.Debug($"{item.TypeString}: \"{item.Text}\" exported to \"{exportFullPath}\""); + return true; + } + return false; } public static bool ExportFont(AssetItem item, string exportPath) @@ -232,14 +234,14 @@ namespace AssetStudioCLI return true; } - public static bool ExportDumpFile(AssetItem item, string exportPath, CLIOptions options) + public static bool ExportDumpFile(AssetItem item, string exportPath, AssemblyLoader assemblyLoader) { if (!TryExportFile(exportPath, item, ".txt", out var exportFullPath)) return false; var str = item.Asset.Dump(); if (str == null && item.Asset is MonoBehaviour m_MonoBehaviour) { - var m_Type = MonoBehaviourToTypeTree(m_MonoBehaviour, options); + var m_Type = m_MonoBehaviour.ConvertToTypeTree(assemblyLoader); str = m_MonoBehaviour.Dump(m_Type); } if (str != null) @@ -367,7 +369,7 @@ namespace AssetStudioCLI return true; } - public static bool ExportConvertFile(AssetItem item, string exportPath, CLIOptions options) + public static bool ExportConvertFile(AssetItem item, string exportPath, CLIOptions options, AssemblyLoader assemblyLoader) { switch (item.Type) { @@ -384,7 +386,7 @@ namespace AssetStudioCLI case ClassIDType.TextAsset: return ExportTextAsset(item, exportPath, options); case ClassIDType.MonoBehaviour: - return ExportMonoBehaviour(item, exportPath, options); + return ExportMonoBehaviour(item, exportPath, assemblyLoader); case ClassIDType.Font: return ExportFont(item, exportPath); case ClassIDType.Sprite: @@ -396,23 +398,6 @@ namespace AssetStudioCLI } } - public static TypeTree MonoBehaviourToTypeTree(MonoBehaviour m_MonoBehaviour, CLIOptions options) - { - if (!assemblyLoader.Loaded) - { - var assemblyFolder = options.o_assemblyPath.Value; - if (assemblyFolder != "") - { - assemblyLoader.Load(assemblyFolder); - } - else - { - assemblyLoader.Loaded = true; - } - } - return m_MonoBehaviour.ConvertToTypeTree(assemblyLoader); - } - public static string FixFileName(string str) { if (str.Length >= 260) return Path.GetRandomFileName(); diff --git a/AssetStudioCLI/Options/CLIOptions.cs b/AssetStudioCLI/Options/CLIOptions.cs index 0480f65..00f9814 100644 --- a/AssetStudioCLI/Options/CLIOptions.cs +++ b/AssetStudioCLI/Options/CLIOptions.cs @@ -806,6 +806,7 @@ namespace AssetStudioCLI.Options sb.AppendLine($"# Log Level: {o_logLevel}"); sb.AppendLine($"# Log Output: {o_logOutput}"); sb.AppendLine($"# Export Asset List: {o_exportAssetList}"); + sb.AppendLine($"# Assebmly Path: \"{o_assemblyPath}\""); sb.AppendLine($"# Unity Version: \"{o_unityVersion}\""); break; default: diff --git a/AssetStudioCLI/Studio.cs b/AssetStudioCLI/Studio.cs index f63ee22..0e0b170 100644 --- a/AssetStudioCLI/Studio.cs +++ b/AssetStudioCLI/Studio.cs @@ -16,12 +16,21 @@ namespace AssetStudioCLI public AssetsManager assetsManager = new AssetsManager(); public List parsedAssetsList = new List(); private static Dictionary containers = new Dictionary(); + private static AssemblyLoader assemblyLoader = new AssemblyLoader(); private readonly CLIOptions options; - public Studio(CLIOptions cliOptions) + public Studio(CLIOptions cliOptions) { Progress.Default = new Progress(ShowCurProgressValue); options = cliOptions; + if (options.o_assemblyPath.Value != "") + { + assemblyLoader.Load(options.o_assemblyPath.Value); + } + else + { + assemblyLoader.Loaded = true; + } } private void ShowCurProgressValue(int value) @@ -308,14 +317,14 @@ namespace AssetStudioCLI break; case WorkMode.Dump: Logger.Debug($"{options.o_workMode}: {asset.Type} : {asset.Container} : {asset.Text}"); - if (ExportDumpFile(asset, exportPath, options)) + if (ExportDumpFile(asset, exportPath, assemblyLoader)) { exportedCount++; } break; case WorkMode.Export: Logger.Debug($"{options.o_workMode}: {asset.Type} : {asset.Container} : {asset.Text}"); - if (ExportConvertFile(asset, exportPath, options)) + if (ExportConvertFile(asset, exportPath, options, assemblyLoader)) { exportedCount++; } @@ -438,7 +447,7 @@ namespace AssetStudioCLI container = Path.HasExtension(container) ? container.Replace(Path.GetExtension(container), "") : container; var destPath = Path.Combine(baseDestPath, container) + Path.DirectorySeparatorChar; - ExtractLive2D(assets, destPath, modelName); + ExtractLive2D(assets, destPath, modelName, assemblyLoader); modelCounter++; } catch (Exception ex) diff --git a/AssetStudioGUI/Studio.cs b/AssetStudioGUI/Studio.cs index 33e05aa..505321d 100644 --- a/AssetStudioGUI/Studio.cs +++ b/AssetStudioGUI/Studio.cs @@ -786,7 +786,7 @@ namespace AssetStudioGUI container = Path.HasExtension(container) ? container.Replace(Path.GetExtension(container), "") : container; var destPath = Path.Combine(baseDestPath, container) + Path.DirectorySeparatorChar; - ExtractLive2D(assets, destPath, modelName); + ExtractLive2D(assets, destPath, modelName, assemblyLoader); modelCounter++; } catch (Exception ex) diff --git a/AssetStudioUtility/CubismLive2DExtractor/Live2DExtractor.cs b/AssetStudioUtility/CubismLive2DExtractor/Live2DExtractor.cs index a20aefd..d8b0a6f 100644 --- a/AssetStudioUtility/CubismLive2DExtractor/Live2DExtractor.cs +++ b/AssetStudioUtility/CubismLive2DExtractor/Live2DExtractor.cs @@ -18,7 +18,7 @@ namespace CubismLive2DExtractor { public static class Live2DExtractor { - public static void ExtractLive2D(IGrouping assets, string destPath, string modelName) + public static void ExtractLive2D(IGrouping assets, string destPath, string modelName, AssemblyLoader assemblyLoader) { var destTexturePath = Path.Combine(destPath, "textures") + Path.DirectorySeparatorChar; var destMotionPath = Path.Combine(destPath, "motions") + Path.DirectorySeparatorChar; @@ -63,7 +63,7 @@ namespace CubismLive2DExtractor { try { - var buff = ParsePhysics(physics); + var buff = ParsePhysics(physics, assemblyLoader); File.WriteAllText($"{destPath}{modelName}.physics3.json", buff); } catch (Exception e) @@ -227,7 +227,15 @@ namespace CubismLive2DExtractor var expressionName = fullName.Replace(".exp3", ""); var expressionObj = monoBehaviour.ToType(); if (expressionObj == null) - continue; + { + var m_Type = monoBehaviour.ConvertToTypeTree(assemblyLoader); + expressionObj = monoBehaviour.ToType(m_Type); + if (expressionObj == null) + { + Logger.Warning($"Expression \"{expressionName}\" is not readable."); + continue; + } + } var expression = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(expressionObj)); expressions.Add(new JObject @@ -311,11 +319,18 @@ namespace CubismLive2DExtractor File.WriteAllText($"{destPath}{modelName}.model3.json", JsonConvert.SerializeObject(model3, Formatting.Indented)); } - private static string ParsePhysics(MonoBehaviour physics) + private static string ParsePhysics(MonoBehaviour physics, AssemblyLoader assemblyLoader) { var physicsObj = physics.ToType(); if (physicsObj == null) - throw new Exception("MonoBehaviour is not readable."); + { + var m_Type = physics.ConvertToTypeTree(assemblyLoader); + physicsObj = physics.ToType(m_Type); + if (physicsObj == null) + { + throw new Exception("MonoBehaviour is not readable."); + } + } var cubismPhysicsRig = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(physicsObj))._rig; var physicsSettings = new CubismPhysics3Json.SerializablePhysicsSettings[cubismPhysicsRig.SubRigs.Length];