Skip to content

Commit 0898a80

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

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

UnityDataTool.Tests/UnityDataToolTests.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class UnityDataToolTests : AssetBundleTestFixture
1818
public UnityDataToolTests(Context context) : base(context)
1919
{
2020
}
21-
21+
2222
protected override void OnLoadExpectedData(Context context)
2323
{
2424
// Uncomment to regenerate expected data.
@@ -41,16 +41,17 @@ public void Teardown()
4141
file.Delete();
4242
}
4343
}
44-
44+
4545
[Test]
46-
public void ArchiveExtract_FilesExtractedSuccessfully()
46+
public void ArchiveExtract_FilesExtractedSuccessfully(
47+
[Values("", "-o archive", "--output-path archive")] string options)
4748
{
4849
var path = Path.Combine(Context.UnityDataFolder, "assetbundle");
4950

50-
Assert.AreEqual(0, Program.Main(new string[] { "archive", "extract", path }));
51-
Assert.IsTrue(File.Exists(Path.Combine(m_TestOutputFolder, "CAB-5d40f7cad7c871cf2ad2af19ac542994")));
52-
Assert.IsTrue(File.Exists(Path.Combine(m_TestOutputFolder, "CAB-5d40f7cad7c871cf2ad2af19ac542994.resS")));
53-
Assert.IsTrue(File.Exists(Path.Combine(m_TestOutputFolder, "CAB-5d40f7cad7c871cf2ad2af19ac542994.resource")));
51+
Assert.AreEqual(0, Program.Main(new string[] { "archive", "extract", path }.Concat(options.Split(" ", StringSplitOptions.RemoveEmptyEntries)).ToArray()));
52+
Assert.IsTrue(File.Exists(Path.Combine(m_TestOutputFolder, "archive", "CAB-5d40f7cad7c871cf2ad2af19ac542994")));
53+
Assert.IsTrue(File.Exists(Path.Combine(m_TestOutputFolder, "archive", "CAB-5d40f7cad7c871cf2ad2af19ac542994.resS")));
54+
Assert.IsTrue(File.Exists(Path.Combine(m_TestOutputFolder, "archive", "CAB-5d40f7cad7c871cf2ad2af19ac542994.resource")));
5455
}
5556

5657
[Test]
@@ -197,7 +198,7 @@ private void ValidateDatabase(string databasePath, bool withRefs)
197198
using (var cmd = db.CreateCommand())
198199
{
199200
cmd.CommandText =
200-
@"SELECT
201+
@"SELECT
201202
(SELECT COUNT(*) FROM animation_clips),
202203
(SELECT COUNT(*) FROM asset_bundles),
203204
(SELECT COUNT(*) FROM assets),
@@ -259,21 +260,21 @@ public void Teardown()
259260
file.Delete();
260261
}
261262
}
262-
263+
263264
[Test]
264265
public void Analyze_PlayerData_DatabaseCorrect()
265266
{
266267
var databasePath = Path.Combine(m_TestOutputFolder, "database.db");
267268
var analyzePath = Path.Combine(Context.UnityDataFolder);
268269

269270
Assert.AreEqual(0, Program.Main(new string[] { "analyze", analyzePath, "-r" }));
270-
271+
271272
using var db = new SQLiteConnection($"Data Source={databasePath};Version=3;New=True;Foreign Keys=False;");
272273
db.Open();
273274
using var cmd = db.CreateCommand();
274275

275276
cmd.CommandText =
276-
@"SELECT
277+
@"SELECT
277278
(SELECT COUNT(*) FROM asset_bundles),
278279
(SELECT COUNT(*) FROM assets),
279280
(SELECT COUNT(*) FROM objects),
@@ -290,7 +291,7 @@ public void Analyze_PlayerData_DatabaseCorrect()
290291
Assert.Greater(reader.GetInt32(3), 0);
291292
Assert.AreEqual(1, reader.GetInt32(4));
292293
}
293-
294+
294295
[Test]
295296
public void DumpText_PlayerData_TextFileCreatedCorrectly()
296297
{

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
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ information about the content of the output file.**
8888
The archive command offers a set of archive-related sub-commands.
8989

9090
**extract** This sub-command extracts the content of an archive. It takes the archive path as
91-
argument.
91+
argument and also provides the following option:
92+
* -o, --output-path \<path\>: Output directory of the extracted archive (default: archive)
9293

9394
**list** This sub-command lists the content of an archive. It takes the archive path as argument.

0 commit comments

Comments
 (0)