Skip to content

Commit

Permalink
[CLI] Add support of Extract mode
Browse files Browse the repository at this point in the history
  • Loading branch information
aelurum committed Jan 23, 2025
1 parent e3e3433 commit cc21d4f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 9 deletions.
29 changes: 21 additions & 8 deletions AssetStudioCLI/Options/CLIOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal enum HelpGroups

internal enum WorkMode
{
Extract,
Export,
ExportRaw,
Dump,
Expand Down Expand Up @@ -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<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);
Expand Down Expand Up @@ -188,7 +189,8 @@ private static void InitOptions()
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" +
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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}");
Expand Down Expand Up @@ -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}");
Expand All @@ -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}\"");
Expand Down
6 changes: 5 additions & 1 deletion AssetStudioCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
99 changes: 99 additions & 0 deletions AssetStudioCLI/Studio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit cc21d4f

Please sign in to comment.