11using System . Text . Json ;
22using System . Text . Json . Nodes ;
3+ using TACTSharp . Interfaces ;
34
45namespace TACTSharp
56{
@@ -10,7 +11,7 @@ public class BuildInstance
1011 public string ? ProductConfig { get ; private set ; }
1112
1213 public EncodingInstance ? Encoding { get ; private set ; }
13- public RootInstance ? Root { get ; private set ; }
14+ public IRootInstance ? Root { get ; private set ; }
1415 public InstallInstance ? Install { get ; private set ; }
1516 public IndexInstance ? GroupIndex { get ; private set ; }
1617 public IndexInstance ? FileIndex { get ; private set ; }
@@ -189,17 +190,64 @@ public void Load()
189190 }
190191
191192 timer . Restart ( ) ;
192- if ( ! BuildConfig . Values . TryGetValue ( "root" , out var rootKey ) )
193- throw new Exception ( "No root key found in build config" ) ;
194193
195- var rootEncodingKeys = Encoding . FindContentKey ( Convert . FromHexString ( rootKey [ 0 ] ) ) ;
196- if ( ! rootEncodingKeys )
197- throw new Exception ( "Root key not found in encoding" ) ;
194+ var useTVFS = Settings . ManifestType == AssetManifestType . TVFS ;
195+ if ( ! BuildConfig . Values . TryGetValue ( "root" , out var rootKey ) || rootKey [ 0 ] == "00000000000000000000000000000000" )
196+ {
197+ if ( Settings . LogLevel <= TSLogLevel . Info )
198+ Console . WriteLine ( "No root key found in build config, falling back to TVFS." ) ;
199+
200+ useTVFS = true ;
201+ }
202+
203+ if ( ! useTVFS )
204+ {
205+ var rootEncodingKeys = Encoding . FindContentKey ( Convert . FromHexString ( rootKey [ 0 ] ) ) ;
206+ if ( ! rootEncodingKeys )
207+ throw new Exception ( "Root key not found in encoding" ) ;
208+
209+ Root = new RootInstance ( cdn . GetDecodedFilePath ( "data" , Convert . ToHexStringLower ( rootEncodingKeys [ 0 ] ) , 0 , rootEncodingKeys . DecodedFileSize ) , Settings ) ;
210+
211+ if ( Settings . LogLevel <= TSLogLevel . Info )
212+ Console . WriteLine ( "Root loaded in " + Math . Ceiling ( timer . Elapsed . TotalMilliseconds ) + "ms" ) ;
213+ }
214+ else
215+ {
216+ var vfsKeys = new Dictionary < uint , ( string cKey , string eKey ) > ( ) ;
217+ var vfsSizes = new Dictionary < uint , ( uint decodedSize , uint encodedSize ) > ( ) ;
218+
219+ foreach ( var bcKey in BuildConfig . Values )
220+ {
221+ if ( bcKey . Key . StartsWith ( "vfs" ) && ! bcKey . Key . StartsWith ( "vfs-root" ) ) // vfs-1 always seems to be the same as vfs-root
222+ {
223+ if ( bcKey . Key . EndsWith ( "-size" ) )
224+ {
225+ if ( uint . TryParse ( bcKey . Key . Replace ( "-size" , "" ) . Replace ( "vfs-" , "" ) , out var vfsIndex ) )
226+ vfsSizes . Add ( vfsIndex , ( uint . Parse ( bcKey . Value [ 0 ] ) , uint . Parse ( bcKey . Value [ 1 ] ) ) ) ;
227+ else
228+ if ( Settings . LogLevel <= TSLogLevel . Warn )
229+ Console . WriteLine ( "Warning: Failed to parse buildconfig VFS size " + bcKey . Key + ", skipping.." ) ;
230+ }
231+ else
232+ {
233+ if ( uint . TryParse ( bcKey . Key . Replace ( "vfs-" , "" ) , out var vfsIndex ) )
234+ vfsKeys . Add ( vfsIndex , ( bcKey . Value [ 0 ] , bcKey . Value [ 1 ] ) ) ;
235+ else
236+ if ( Settings . LogLevel <= TSLogLevel . Warn )
237+ Console . WriteLine ( "Warning: Failed to parse buildconfig VFS key " + bcKey . Key + ", skipping.." ) ;
238+ }
239+ }
240+ }
241+
242+ // TODO: Not great to feed all this stuff into it
243+ Root = new TVFSInstance ( vfsKeys , vfsSizes , CDNConfig , GroupIndex , FileIndex , cdn , Settings ) ;
198244
199- Root = new RootInstance ( cdn . GetDecodedFilePath ( "data" , Convert . ToHexStringLower ( rootEncodingKeys [ 0 ] ) , 0 , rootEncodingKeys . DecodedFileSize ) , Settings ) ;
245+ if ( Settings . LogLevel <= TSLogLevel . Info )
246+ Console . WriteLine ( "TVFS loaded in " + Math . Ceiling ( timer . Elapsed . TotalMilliseconds ) + "ms" ) ;
247+ }
248+
200249 timer . Stop ( ) ;
201- if ( Settings . LogLevel <= TSLogLevel . Info )
202- Console . WriteLine ( "Root loaded in " + Math . Ceiling ( timer . Elapsed . TotalMilliseconds ) + "ms" ) ;
250+
203251
204252 timer . Restart ( ) ;
205253 if ( ! BuildConfig . Values . TryGetValue ( "install" , out var installKey ) )
0 commit comments