From cc21d4fa4dfb643a5a07cbf1ff2b4313ddf3c1ef Mon Sep 17 00:00:00 2001 From: VaDiM Date: Tue, 21 Jan 2025 02:18:22 +0300 Subject: [PATCH] [CLI] Add support of Extract mode --- AssetStudioCLI/Options/CLIOptions.cs | 29 +++++--- AssetStudioCLI/Program.cs | 6 +- AssetStudioCLI/Studio.cs | 99 ++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 9 deletions(-) diff --git a/AssetStudioCLI/Options/CLIOptions.cs b/AssetStudioCLI/Options/CLIOptions.cs index 96aff73b..423f8a59 100644 --- a/AssetStudioCLI/Options/CLIOptions.cs +++ b/AssetStudioCLI/Options/CLIOptions.cs @@ -20,6 +20,7 @@ internal enum HelpGroups internal enum WorkMode { + Extract, Export, ExportRaw, Dump, @@ -136,7 +137,7 @@ private static void OptionGrouping(string name, string desc, string example, Hel } var optionDesc = desc + example.Color(ColorConsole.BrightBlack); - var optionDict = new Dictionary() { { name, optionDesc } }; + var optionDict = new Dictionary { { name, optionDesc } }; if (optionGroups.TryGetValue(helpGroup, out Dictionary groupDict)) { groupDict.Add(name, optionDesc); @@ -188,7 +189,8 @@ private static void InitOptions() optionDefaultValue: WorkMode.Export, optionName: "-m, --mode ", optionDescription: "Specify working mode\n" + - "\n" + + "\n" + + "Extract - Extracts(Decompresses) asset bundles\n" + "Export - Exports converted assets\n" + "ExportRaw - Exports raw data\n" + "Dump - Makes asset dumps\n" + @@ -551,6 +553,9 @@ public static void ParseArgs(string[] args) var value = resplittedArgs[workModeOptionIndex + 1]; switch (value.ToLower()) { + case "extract": + o_workMode.Value = WorkMode.Extract; + break; case "export": o_workMode.Value = WorkMode.Export; break; @@ -649,7 +654,7 @@ public static void ParseArgs(string[] args) #endregion #region Parse Options - for (int i = 0; i < resplittedArgs.Count; i++) + for (var i = 0; i < resplittedArgs.Count; i++) { var option = resplittedArgs[i].ToLower(); try @@ -1084,7 +1089,10 @@ public static void ParseArgs(string[] args) } if (o_outputFolder.Value == o_outputFolder.DefaultValue) { - var fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, o_outputFolder.DefaultValue + Path.DirectorySeparatorChar); + var defaultFolder = o_workMode.Value == WorkMode.Extract + ? "ASExtract" + : o_outputFolder.DefaultValue; + var fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, defaultFolder + Path.DirectorySeparatorChar); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); @@ -1205,14 +1213,20 @@ public static void ShowCurrentOptions() { sb.AppendLine($"# Custom Compression Type: {o_customCompressionType}"); } - sb.AppendLine($"# Parse Assets Using TypeTree: {!f_avoidLoadingViaTypetree.Value}"); + if (o_workMode.Value != WorkMode.Extract) + { + sb.AppendLine($"# Parse Assets Using TypeTree: {!f_avoidLoadingViaTypetree.Value}"); + } sb.AppendLine($"# Input Path: \"{inputPath}\""); + if (o_workMode.Value != WorkMode.Info) + { + sb.AppendLine($"# Output Path: \"{o_outputFolder}\""); + } switch (o_workMode.Value) { case WorkMode.Export: case WorkMode.ExportRaw: case WorkMode.Dump: - sb.AppendLine($"# Output Path: \"{o_outputFolder}\""); if (o_workMode.Value != WorkMode.Export) { sb.AppendLine($"# Load All Assets: {f_loadAllAssets}"); @@ -1248,7 +1262,6 @@ public static void ShowCurrentOptions() break; case WorkMode.Live2D: case WorkMode.SplitObjects: - sb.AppendLine($"# Output Path: \"{o_outputFolder}\""); sb.AppendLine($"# Log Level: {o_logLevel}"); sb.AppendLine($"# Log Output: {o_logOutput}"); sb.AppendLine($"# Export Asset List: {o_exportAssetList}"); @@ -1260,7 +1273,7 @@ public static void ShowCurrentOptions() else { sb.AppendLine($"# Model Group Option: {o_l2dGroupOption}"); - sb.AppendFormat("# Search model-related assets by: {0}\n", f_l2dAssetSearchByFilename.Value ? "FileName" : "Container"); + sb.AppendFormat("# Search Model-related Assets by: {0}\n", f_l2dAssetSearchByFilename.Value ? "FileName" : "Container"); sb.AppendLine($"# Motion Export Method: {o_l2dMotionMode}"); sb.AppendLine($"# Force Bezier: {f_l2dForceBezier }"); sb.AppendLine($"# Assembly Path: \"{o_assemblyPath}\""); diff --git a/AssetStudioCLI/Program.cs b/AssetStudioCLI/Program.cs index 4c61fd45..3aa73e25 100644 --- a/AssetStudioCLI/Program.cs +++ b/AssetStudioCLI/Program.cs @@ -32,7 +32,11 @@ private static void CLIRun() try { - if (Studio.LoadAssets()) + if (CLIOptions.o_workMode.Value == WorkMode.Extract) + { + Studio.ExtractBundles(); + } + else if (Studio.LoadAssets()) { Studio.ParseAssets(); if (CLIOptions.filterBy != FilterBy.None) diff --git a/AssetStudioCLI/Studio.cs b/AssetStudioCLI/Studio.cs index c3075404..12ed0882 100644 --- a/AssetStudioCLI/Studio.cs +++ b/AssetStudioCLI/Studio.cs @@ -35,6 +35,105 @@ private static void ShowCurProgressValue(int value) Console.Write($"[{value:000}%]\r"); } + public static void ExtractBundles() + { + var extractedCount = 0; + var path = CLIOptions.inputPath; + var savePath = CLIOptions.o_outputFolder.Value; + Progress.Reset(); + if (Directory.Exists(path)) + { + var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); + var totalCount = files.Length; + for (var i = 0; i < totalCount; i++) + { + var file = files[i]; + var fileOriPath = Path.GetDirectoryName(file); + var fileSavePath = fileOriPath.Replace(path, savePath); + extractedCount += ExtractFile(file, fileSavePath); + Progress.Report(i + 1, totalCount); + } + } + else if (File.Exists(path)) + { + extractedCount += ExtractFile(path, savePath); + } + + var status = extractedCount > 0 + ? $"Finished extracting {extractedCount} file(s) to \"{savePath.Color(Ansi.BrightCyan)}\"" + : "Nothing extracted (not extractable or file(s) already exist)"; + Logger.Default.Log(LoggerEvent.Info, status, ignoreLevel: true); + } + + public static int ExtractFile(string fileName, string savePath) + { + var extractedCount = 0; + var reader = new FileReader(fileName); + switch (reader.FileType) + { + case FileType.BundleFile: + extractedCount += ExtractBundleFile(reader, savePath); + break; + case FileType.WebFile: + extractedCount += ExtractWebDataFile(reader, savePath); + break; + default: + reader.Dispose(); + break; + } + return extractedCount; + } + + private static int ExtractBundleFile(FileReader reader, string savePath) + { + Logger.Info($"Decompressing {reader.FileName} ..."); + var bundleFile = new BundleFile(reader, assetsManager.ZstdEnabled, assetsManager.SpecifyUnityVersion); + reader.Dispose(); + if (bundleFile.fileList.Length > 0) + { + var extractPath = Path.Combine(savePath, reader.FileName + "_unpacked"); + return ExtractStreamFile(extractPath, bundleFile.fileList); + } + return 0; + } + + private static int ExtractWebDataFile(FileReader reader, string savePath) + { + Logger.Info($"Decompressing {reader.FileName} ..."); + var webFile = new WebFile(reader); + reader.Dispose(); + if (webFile.fileList.Length > 0) + { + var extractPath = Path.Combine(savePath, reader.FileName + "_unpacked"); + return ExtractStreamFile(extractPath, webFile.fileList); + } + return 0; + } + + private static int ExtractStreamFile(string extractPath, StreamFile[] fileList) + { + var extractedCount = 0; + foreach (var file in fileList) + { + var filePath = Path.Combine(extractPath, file.path); + var fileDirectory = Path.GetDirectoryName(filePath); + if (!Directory.Exists(fileDirectory)) + { + Directory.CreateDirectory(fileDirectory); + } + if (!File.Exists(filePath)) + { + using (var fileStream = File.Create(filePath)) + { + file.stream.CopyTo(fileStream); + } + extractedCount += 1; + } + file.stream.Dispose(); + } + return extractedCount; + } + public static bool LoadAssets() { var isLoaded = false;