Skip to content

Commit c2e70a2

Browse files
committed
fix: folders also need a .meta file
1 parent bb2555e commit c2e70a2

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

UnityPackager.Tests/PackerTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void RecursivePackTest()
6060

6161
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/childfolder/sample2.txt"), "sample2.txt should have been decompressed");
6262
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/childfolder/sample2.txt.meta"), "sample2.meta should have been generated");
63+
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/childfolder.meta"), "childfolder.meta should have been generated");
6364
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/box.png"), "box.png should have been decompressed");
6465
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/box.png.meta"), "box.meta should have been decompressed");
6566

UnityPackager/Packer.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ private static void AddAssets(IDictionary<string, string> files, string tempPath
4141

4242
private static void AddFolder(string tempPath, string folder, string destination)
4343
{
44-
string[] files = Directory.GetFiles(folder, "*", SearchOption.AllDirectories);
44+
string[] folders = Directory.GetDirectories(folder, "*", SearchOption.AllDirectories);
45+
string[] files = Directory.GetFiles(folder, "*", SearchOption.AllDirectories );
4546

46-
foreach (string filename in files)
47+
List<string> entries = new List<string>(folders);
48+
entries.AddRange(files);
49+
50+
foreach (string filename in entries)
4751
{
4852
// metas will be copied with their asset
4953
if (Path.GetExtension(filename) == ".meta")
@@ -57,14 +61,17 @@ private static void AddFolder(string tempPath, string folder, string destination
5761

5862
private static void AddAsset(string tempPath, string fromFile, string toPath)
5963
{
60-
YamlDocument meta = GetMeta(fromFile) ?? GenerateMeta(toPath);
64+
YamlDocument meta = GetMeta(fromFile) ?? GenerateMeta(fromFile, toPath);
6165

6266
string guid = GetGuid(meta);
6367

6468
Directory.CreateDirectory(Path.Combine(tempPath, guid));
6569

66-
string assetPath = Path.Combine(tempPath, guid, "asset");
67-
File.Copy(fromFile, assetPath);
70+
if (File.Exists(fromFile))
71+
{
72+
string assetPath = Path.Combine(tempPath, guid, "asset");
73+
File.Copy(fromFile, assetPath);
74+
}
6875

6976
string pathnamePath = Path.Combine(tempPath, guid, "pathname");
7077
File.WriteAllText(pathnamePath, toPath);
@@ -98,15 +105,29 @@ private static string GetGuid(YamlDocument meta)
98105
return value.Value;
99106
}
100107

101-
private static YamlDocument GenerateMeta(string filename)
108+
private static YamlDocument GenerateMeta(string fromFile, string toFile)
102109
{
103-
string guid = Utils.CreateGuid(filename);
110+
string guid = Utils.CreateGuid(toFile);
104111

105-
return new YamlDocument(new YamlMappingNode
112+
if (Directory.Exists(fromFile))
113+
{
114+
// this is a folder
115+
return new YamlDocument(new YamlMappingNode
116+
{
117+
{"guid", guid},
118+
{"fileFormatVersion", "2"},
119+
{"folderAsset", "yes"}
120+
});
121+
}
122+
else
123+
{
124+
// this is a file
125+
return new YamlDocument(new YamlMappingNode
106126
{
107127
{"guid", guid},
108128
{"fileFormatVersion", "2"}
109129
});
130+
}
110131
}
111132

112133
private static YamlDocument GetMeta(string filename)

UnityPackager/Unpacker.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static void Unpack(string inputFile, string outputFolder)
1919

2020
foreach (string dirEntry in dirEntries)
2121
{
22-
if (!File.Exists(Path.Combine(dirEntry, "pathname")) || !File.Exists(Path.Combine(dirEntry, "asset")) || !File.Exists(Path.Combine(dirEntry, "asset.meta")))
22+
if (!File.Exists(Path.Combine(dirEntry, "pathname")) || !File.Exists(Path.Combine(dirEntry, "asset.meta")))
2323
{
2424
// Invalid format
2525
continue;
@@ -38,7 +38,14 @@ public static void Unpack(string inputFile, string outputFolder)
3838
if (File.Exists(targetMetaPath))
3939
File.Delete(targetMetaPath);
4040

41-
File.Copy(Path.Combine(dirEntry, "asset"), targetPath);
41+
if (File.Exists(Path.Combine(dirEntry, "asset")))
42+
{
43+
File.Copy(Path.Combine(dirEntry, "asset"), targetPath);
44+
}
45+
else
46+
{
47+
Directory.CreateDirectory(targetPath);
48+
}
4249
File.WriteAllText(targetMetaPath, File.ReadAllText(Path.Combine(dirEntry, "asset.meta")));
4350
}
4451

0 commit comments

Comments
 (0)