[CLI] Add support of Extract mode

This commit is contained in:
VaDiM 2025-01-21 02:18:22 +03:00
parent e3e343320c
commit cc21d4fa4d
3 changed files with 125 additions and 9 deletions

View File

@ -20,6 +20,7 @@ namespace AssetStudioCLI.Options
internal enum WorkMode
{
Extract,
Export,
ExportRaw,
Dump,
@ -136,7 +137,7 @@ namespace AssetStudioCLI.Options
}
var optionDesc = desc + example.Color(ColorConsole.BrightBlack);
var optionDict = new Dictionary<string, string>() { { name, optionDesc } };
var optionDict = new Dictionary<string, string> { { name, optionDesc } };
if (optionGroups.TryGetValue(helpGroup, out Dictionary<string, string> groupDict))
{
groupDict.Add(name, optionDesc);
@ -188,7 +189,8 @@ namespace AssetStudioCLI.Options
optionDefaultValue: WorkMode.Export,
optionName: "-m, --mode <value>",
optionDescription: "Specify working mode\n" +
"<Value: export(default) | exportRaw | dump | info | live2d | splitObjects>\n" +
"<Value: extract | export(default) | exportRaw | dump | info | live2d | splitObjects>\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 @@ namespace AssetStudioCLI.Options
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 @@ namespace AssetStudioCLI.Options
#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 @@ namespace AssetStudioCLI.Options
}
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 @@ namespace AssetStudioCLI.Options
{
sb.AppendLine($"# Custom Compression Type: {o_customCompressionType}");
}
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 @@ namespace AssetStudioCLI.Options
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 @@ namespace AssetStudioCLI.Options
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}\"");

View File

@ -32,7 +32,11 @@ namespace AssetStudioCLI
try
{
if (Studio.LoadAssets())
if (CLIOptions.o_workMode.Value == WorkMode.Extract)
{
Studio.ExtractBundles();
}
else if (Studio.LoadAssets())
{
Studio.ParseAssets();
if (CLIOptions.filterBy != FilterBy.None)

View File

@ -35,6 +35,105 @@ namespace AssetStudioCLI
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;