@@ -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