Skip to content

Commit

Permalink
fix: folders also need a .meta file
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpach committed Mar 28, 2019
1 parent bb2555e commit c2e70a2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions UnityPackager.Tests/PackerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void RecursivePackTest()

Assert.True(File.Exists("rsample_out/Assets/UnityPacker/childfolder/sample2.txt"), "sample2.txt should have been decompressed");
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/childfolder/sample2.txt.meta"), "sample2.meta should have been generated");
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/childfolder.meta"), "childfolder.meta should have been generated");
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/box.png"), "box.png should have been decompressed");
Assert.True(File.Exists("rsample_out/Assets/UnityPacker/box.png.meta"), "box.meta should have been decompressed");

Expand Down
37 changes: 29 additions & 8 deletions UnityPackager/Packer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ private static void AddAssets(IDictionary<string, string> files, string tempPath

private static void AddFolder(string tempPath, string folder, string destination)
{
string[] files = Directory.GetFiles(folder, "*", SearchOption.AllDirectories);
string[] folders = Directory.GetDirectories(folder, "*", SearchOption.AllDirectories);
string[] files = Directory.GetFiles(folder, "*", SearchOption.AllDirectories );

foreach (string filename in files)
List<string> entries = new List<string>(folders);
entries.AddRange(files);

foreach (string filename in entries)
{
// metas will be copied with their asset
if (Path.GetExtension(filename) == ".meta")
Expand All @@ -57,14 +61,17 @@ private static void AddFolder(string tempPath, string folder, string destination

private static void AddAsset(string tempPath, string fromFile, string toPath)
{
YamlDocument meta = GetMeta(fromFile) ?? GenerateMeta(toPath);
YamlDocument meta = GetMeta(fromFile) ?? GenerateMeta(fromFile, toPath);

string guid = GetGuid(meta);

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

string assetPath = Path.Combine(tempPath, guid, "asset");
File.Copy(fromFile, assetPath);
if (File.Exists(fromFile))
{
string assetPath = Path.Combine(tempPath, guid, "asset");
File.Copy(fromFile, assetPath);
}

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

private static YamlDocument GenerateMeta(string filename)
private static YamlDocument GenerateMeta(string fromFile, string toFile)
{
string guid = Utils.CreateGuid(filename);
string guid = Utils.CreateGuid(toFile);

return new YamlDocument(new YamlMappingNode
if (Directory.Exists(fromFile))
{
// this is a folder
return new YamlDocument(new YamlMappingNode
{
{"guid", guid},
{"fileFormatVersion", "2"},
{"folderAsset", "yes"}
});
}
else
{
// this is a file
return new YamlDocument(new YamlMappingNode
{
{"guid", guid},
{"fileFormatVersion", "2"}
});
}
}

private static YamlDocument GetMeta(string filename)
Expand Down
11 changes: 9 additions & 2 deletions UnityPackager/Unpacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void Unpack(string inputFile, string outputFolder)

foreach (string dirEntry in dirEntries)
{
if (!File.Exists(Path.Combine(dirEntry, "pathname")) || !File.Exists(Path.Combine(dirEntry, "asset")) || !File.Exists(Path.Combine(dirEntry, "asset.meta")))
if (!File.Exists(Path.Combine(dirEntry, "pathname")) || !File.Exists(Path.Combine(dirEntry, "asset.meta")))
{
// Invalid format
continue;
Expand All @@ -38,7 +38,14 @@ public static void Unpack(string inputFile, string outputFolder)
if (File.Exists(targetMetaPath))
File.Delete(targetMetaPath);

File.Copy(Path.Combine(dirEntry, "asset"), targetPath);
if (File.Exists(Path.Combine(dirEntry, "asset")))
{
File.Copy(Path.Combine(dirEntry, "asset"), targetPath);
}
else
{
Directory.CreateDirectory(targetPath);
}
File.WriteAllText(targetMetaPath, File.ReadAllText(Path.Combine(dirEntry, "asset.meta")));
}

Expand Down

0 comments on commit c2e70a2

Please sign in to comment.