Merge pull request #9 from davschne-unity/david.schneider/extract-outdir · unitycoder/UnityDataTools@0898a80 · GitHub
Skip to content

Commit 0898a80

Browse files
authored
Merge pull request Unity-Technologies#9 from davschne-unity/david.schneider/extract-outdir
Add output directory option to `archive extract`
2 parents 8150dfa + 858011a commit 0898a80

3 files changed

Lines changed: 27 additions & 21 deletions

File tree

UnityDataTool.Tests/UnityDataToolTests.cs

Lines changed: 13 additions & 12 deletions

UnityDataTool/Program.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static class Program
1414
public static int Main(string[] args)
1515
{
1616
UnityFileSystem.Init();
17-
17+
1818
var rootCommand = new RootCommand();
1919

2020
{
@@ -86,15 +86,17 @@ public static int Main(string[] args)
8686

8787
{
8888
var pathArg = new Argument<FileInfo>("filename", "The path of the archive file").ExistingOnly();
89+
var oOpt = new Option<DirectoryInfo>(aliases: new[] { "--output-path", "-o" }, description: "Output directory of the extracted archive", getDefaultValue: () => new DirectoryInfo("archive"));
8990

9091
var extractArchiveCommand = new Command("extract", "Extract the archive.")
9192
{
9293
pathArg,
94+
oOpt,
9395
};
9496

9597
extractArchiveCommand.SetHandler(
96-
(FileInfo fi) => HandleExtractArchive(fi),
97-
pathArg);
98+
(FileInfo fi, DirectoryInfo o) => HandleExtractArchive(fi, o),
99+
pathArg, oOpt);
98100

99101
var listArchiveCommand = new Command("list", "List the content of an archive.")
100102
{
@@ -115,7 +117,7 @@ public static int Main(string[] args)
115117
}
116118

117119
var r = rootCommand.Invoke(args);
118-
120+
119121
UnityFileSystem.Cleanup();
120122

121123
return r;
@@ -129,7 +131,7 @@ enum DumpFormat
129131
static int HandleAnalyze(DirectoryInfo path, string outputFile, bool extractReferences, string searchPattern)
130132
{
131133
var analyzer = new AnalyzerTool();
132-
134+
133135
return analyzer.Analyze(path.FullName, outputFile, searchPattern, extractReferences);
134136
}
135137

@@ -167,15 +169,15 @@ static int HandleDump(FileInfo filename, DumpFormat format, bool skipLargeArrays
167169
return 1;
168170
}
169171

170-
static int HandleExtractArchive(FileInfo filename)
172+
static int HandleExtractArchive(FileInfo filename, DirectoryInfo outputFolder)
171173
{
172174
try
173175
{
174176
using var archive = UnityFileSystem.MountArchive(filename.FullName, "/");
175177
foreach (var node in archive.Nodes)
176178
{
177179
Console.WriteLine($"Extracting {node.Path}...");
178-
CopyFile("/" + node.Path, Path.GetFileName(node.Path));
180+
CopyFile("/" + node.Path, Path.Combine(outputFolder.FullName, node.Path));
179181
}
180182
}
181183
catch (NotSupportedException)
@@ -212,6 +214,8 @@ static int HandleListArchive(FileInfo filename)
212214
static void CopyFile(string source, string dest)
213215
{
214216
using var sourceFile = UnityFileSystem.OpenFile(source);
217+
// Create the containing directory if it doesn't exist.
218+
Directory.CreateDirectory(Path.GetDirectoryName(dest));
215219
using var destFile = new FileStream(dest, FileMode.Create);
216220

217221
const int blockSize = 256 * 1024;
@@ -225,4 +229,4 @@ static void CopyFile(string source, string dest)
225229
}
226230
while (actualSize == blockSize);
227231
}
228-
}
232+
}

UnityDataTool/README.md

Lines changed: 2 additions & 1 deletion

0 commit comments

Comments
 (0)