Skip to content

Commit 4d4bb7e

Browse files
committed
feat: Now it can pack whole folders
1 parent 1034c73 commit 4d4bb7e

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

UnityPackager.Tests/PackerTest.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ public void PackTest()
2323
Packer.Pack(fileMap, "sample.unitypackage");
2424

2525
Assert.True(File.Exists("sample.unitypackage"), "Package should have been created");
26-
// now let's unpack and check it out
27-
string fullpath = Path.GetFullPath("sample.unitypackage");
28-
Console.WriteLine($"Full path is {fullpath}");
2926

3027
Unpacker.Unpack("sample.unitypackage", "sample_out");
3128

@@ -44,6 +41,32 @@ public void PackTest()
4441
Assert.True(meta2.Contains("somethingelse"), "Packer should preserve our custom yaml files");
4542
}
4643

44+
[Fact]
45+
public void RecursivePackTest()
46+
{
47+
Dictionary<string, string> fileMap = new Dictionary<string, string>
48+
{
49+
["sample"] = "Assets/UnityPacker",
50+
};
51+
52+
Packer.Pack(fileMap, "recursivesample.unitypackage");
53+
54+
Assert.True(File.Exists("recursivesample.unitypackage"), "Package should have been created");
55+
56+
Unpacker.Unpack("recursivesample.unitypackage", "rsample_out");
57+
58+
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/sample1.txt"), "sample1.txt should have been decompressed");
59+
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/sample1.meta"), "sample1.meta should have been generated");
60+
61+
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/childfolder/sample2.txt"), "sample2.txt should have been decompressed");
62+
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/childfolder/sample2.meta"), "sample2.meta should have been generated");
63+
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/box.png"), "box.png should have been decompressed");
64+
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/box.meta"), "box.meta should have been decompressed");
65+
66+
string meta2 = File.ReadAllText("rsample_out/Assets/UnityPacker/childfolder/sample2.meta");
67+
Assert.True(meta2.Contains("somethingelse"), "Packer should preserve our custom yaml files");
68+
}
69+
4770
private static byte[] GetMD5(string file)
4871
{
4972
using (MD5 md5 = MD5.Create())

UnityPackager/Packer.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,28 @@ private static void AddAssets(IDictionary<string, string> files, string tempPath
3030
{
3131
foreach (KeyValuePair<string, string> fileEntry in files)
3232
{
33-
AddAsset(tempPath, fileEntry.Key, fileEntry.Value);
33+
if (File.Exists(fileEntry.Key))
34+
AddAsset(tempPath, fileEntry.Key, fileEntry.Value);
35+
else if (Directory.Exists(fileEntry.Key))
36+
AddFolder(tempPath, fileEntry.Key, fileEntry.Value);
37+
else
38+
throw new FileNotFoundException($"Could not find file or directory {fileEntry.Key}");
39+
}
40+
}
41+
42+
private static void AddFolder(string tempPath, string folder, string destination)
43+
{
44+
string[] files = Directory.GetFiles(folder, "*", SearchOption.AllDirectories);
45+
46+
foreach (string filename in files)
47+
{
48+
// metas will be copied with their asset
49+
if (Path.GetExtension(filename) == ".meta")
50+
continue;
51+
52+
string destinationPath = Path.Combine(destination, Path.GetRelativePath(folder, filename));
53+
54+
AddAsset(tempPath, filename, destinationPath);
3455
}
3556
}
3657

0 commit comments

Comments
 (0)