Skip to content

Commit fc3a579

Browse files
committed
feat: Preserve project meta files
1 parent 4799daa commit fc3a579

File tree

3 files changed

+66
-27
lines changed

3 files changed

+66
-27
lines changed

UnityPackager.Tests/PackerTest.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ public void PackTest()
3737
Assert.True(File.Exists("sample_out/images/box.meta"), "box.meta should have been decompressed");
3838

3939
// let's make sure the file was not modified
40-
byte[] md5 = GetMD5();
41-
40+
byte[] md5 = GetMD5("sample_out/images/box.png");
4241
Assert.Equal("A6-04-78-87-FC-41-65-97-76-D5-CB-4A-18-2F-33-7A", BitConverter.ToString(md5));
42+
43+
md5 = GetMD5("sample_out/sample2.meta");
44+
Assert.Equal("f6-81-93-ef-62-fe-49-7c-e0-4a-23-8b-9f-12-08-37".ToUpper(), BitConverter.ToString(md5));
4345
}
4446

45-
private static byte[] GetMD5()
47+
private static byte[] GetMD5(string file)
4648
{
4749
using (MD5 md5 = MD5.Create())
4850
{
49-
using (var stream = File.OpenRead("sample_out/images/box.png"))
51+
using (var stream = File.OpenRead(file))
5052
{
5153
return md5.ComputeHash(stream);
5254
}

UnityPackager.Tests/UnityPacker.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727
<None Update="sample\box.png">
2828
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2929
</None>
30+
<None Update="sample\childfolder\sample2.meta">
31+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
32+
</None>
3033
</ItemGroup>
3134
</Project>

UnityPackager/Packer.cs

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,73 @@ private static void AddAssets(IDictionary<string, string> files, string tempPath
3030
{
3131
foreach (KeyValuePair<string, string> fileEntry in files)
3232
{
33-
string guid = Utils.CreateGuid(fileEntry.Value);
33+
YamlDocument meta = GetMeta(fileEntry.Key) ?? GenerateMeta(fileEntry.Value);
34+
35+
string guid = GetGuid(meta);
3436

3537
Directory.CreateDirectory(Path.Combine(tempPath, guid));
36-
File.Copy(fileEntry.Key, Path.Combine(tempPath, guid, "asset"));
3738

38-
File.WriteAllText(Path.Combine(tempPath, guid, "pathname"), fileEntry.Value);
39+
string assetPath = Path.Combine(tempPath, guid, "asset");
40+
File.Copy(fileEntry.Key, assetPath);
3941

40-
string metaPath = Path.ChangeExtension(fileEntry.Key, ".meta");
42+
string pathnamePath = Path.Combine(tempPath, guid, "pathname");
43+
File.WriteAllText(pathnamePath, fileEntry.Value);
4144

42-
if (File.Exists(metaPath))
43-
{
44-
File.Copy(metaPath, Path.Combine(tempPath, guid, "asset.meta"));
45-
}
46-
else
47-
{
48-
// TODO: If a meta file exists, grab it instead of creating this barebones version.
49-
// Requires research on how Unity does it, to fully mimic the real packing behaviour
50-
using (StreamWriter writer = new StreamWriter(Path.Combine(tempPath, guid, "asset.meta")))
51-
{
52-
new YamlStream(new YamlDocument(new YamlMappingNode
45+
string metaPath = Path.Combine(tempPath, guid, "asset.meta");
46+
SaveMeta(metaPath, meta);
47+
}
48+
}
49+
50+
private static void SaveMeta(string metaPath, YamlDocument meta)
51+
{
52+
using (StreamWriter writer = new StreamWriter(metaPath))
53+
{
54+
new YamlStream(meta).Save(writer);
55+
}
56+
57+
FileInfo metaFile = new FileInfo(metaPath);
58+
59+
using (FileStream metaFileStream = metaFile.Open(FileMode.Open))
60+
{
61+
metaFileStream.SetLength(metaFile.Length - 3 - Environment.NewLine.Length);
62+
}
63+
}
64+
65+
private static string GetGuid(YamlDocument meta)
66+
{
67+
YamlMappingNode mapping = (YamlMappingNode)meta.RootNode;
68+
69+
YamlScalarNode key = new YamlScalarNode("guid");
70+
71+
YamlScalarNode value = (YamlScalarNode)mapping[key];
72+
return value.Value;
73+
}
74+
75+
private static YamlDocument GenerateMeta(string filename)
76+
{
77+
string guid = Utils.CreateGuid(filename);
78+
79+
return new YamlDocument(new YamlMappingNode
5380
{
5481
{"guid", guid},
5582
{"fileFormatVersion", "2"}
56-
})).Save(writer);
57-
}
83+
});
84+
}
5885

59-
FileInfo metaFile = new FileInfo(Path.Combine(Path.Combine(tempPath, guid, "asset.meta")));
86+
private static YamlDocument GetMeta(string filename)
87+
{
88+
// do we have a .meta file?
89+
string metaPath = Path.ChangeExtension(filename, ".meta");
6090

61-
using (FileStream metaFileStream = metaFile.Open(FileMode.Open))
62-
{
63-
metaFileStream.SetLength(metaFile.Length - 3 - Environment.NewLine.Length);
64-
}
65-
}
91+
if (!File.Exists(metaPath))
92+
return null;
93+
94+
using (StreamReader reader = new StreamReader(metaPath))
95+
{
96+
var yaml = new YamlStream();
97+
yaml.Load(reader);
98+
99+
return yaml.Documents[0];
66100
}
67101
}
68102

0 commit comments

Comments
 (0)