1
- using System ;
1
+ using System ;
2
2
using System . Collections . Generic ;
3
3
using System . ComponentModel ;
4
4
using System . Data . SQLite ;
15
15
using xivModdingFramework . Items . Interfaces ;
16
16
using xivModdingFramework . Models . FileTypes ;
17
17
using xivModdingFramework . Mods ;
18
+ using xivModdingFramework . Mods . DataContainers ;
18
19
using xivModdingFramework . Resources ;
19
20
using xivModdingFramework . SqPack . FileTypes ;
20
21
@@ -31,7 +32,7 @@ public static class XivCache
31
32
private static GameInfo _gameInfo ;
32
33
private static DirectoryInfo _dbPath ;
33
34
private static DirectoryInfo _rootCachePath ;
34
- public static readonly Version CacheVersion = new Version ( "1.0.2.1 " ) ;
35
+ public static readonly Version CacheVersion = new Version ( "1.0.2.2 " ) ;
35
36
private const string dbFileName = "mod_cache.db" ;
36
37
private const string rootCacheFileName = "item_sets.db" ;
37
38
private const string creationScript = "CreateCacheDB.sql" ;
@@ -187,7 +188,11 @@ public static void SetGameInfo(GameInfo gameInfo = null, bool enableCacheWorker
187
188
var reason = CacheNeedsRebuild ( ) ;
188
189
if ( reason != CacheRebuildReason . CacheOK && ! _REBUILDING )
189
190
{
190
- RebuildCache ( reason ) ;
191
+ var ver =
192
+ reason == CacheRebuildReason . CacheVersionUpdate
193
+ ? new Version ( GetMetaValue ( "cache_version" ) ) : CacheVersion ;
194
+
195
+ RebuildCache ( ver , reason ) ;
191
196
}
192
197
}
193
198
@@ -277,7 +282,7 @@ private static CacheRebuildReason CacheNeedsRebuild()
277
282
/// help ensure it's never accidentally called
278
283
/// without an await.
279
284
/// </summary>
280
- public static void RebuildCache ( CacheRebuildReason reason = CacheRebuildReason . ManualRequest )
285
+ public static void RebuildCache ( Version previousVersion , CacheRebuildReason reason = CacheRebuildReason . ManualRequest )
281
286
{
282
287
CacheWorkerEnabled = false ;
283
288
_REBUILDING = true ;
@@ -309,7 +314,13 @@ public static void RebuildCache(CacheRebuildReason reason = CacheRebuildReason.M
309
314
tasks . Add ( RebuildFurnitureCache ( ) ) ;
310
315
tasks . Add ( BuildModdedItemDependencies ( ) ) ;
311
316
312
- await Task . WhenAll ( tasks ) ;
317
+ // This was originally running only if the reason was cache update,
318
+ // but if the cache gets messed up in one way or another, and has to
319
+ // rebuild on a new TT version for any reason other than CacheUpdate
320
+ // or whatever, it will prevent the migration from occurring properly
321
+ tasks . Add ( MigrateCache ( previousVersion ) ) ;
322
+
323
+ await Task . WhenAll ( tasks ) ;
313
324
314
325
var post = ( DateTime . UtcNow . Subtract ( new DateTime ( 1970 , 1 , 1 ) ) ) . TotalSeconds ;
315
326
@@ -451,7 +462,47 @@ private static void CreateCache()
451
462
}
452
463
}
453
464
465
+ private static async Task MigrateCache ( Version lastCacheVersion ) {
466
+
467
+ // Tex file fix migration
468
+ // This technically has nothing to do with the cache version,
469
+ // but I think this is one of the only places that this can go
470
+ if ( lastCacheVersion < new Version ( 1 , 0 , 2 , 2 ) ) {
471
+ var m = new Modding ( _gameInfo . GameDirectory ) ;
472
+ var modList = m . GetModList ( ) ;
473
+ foreach ( var mod in modList . Mods ) {
474
+ if ( mod . data . dataType != 4 ) continue ;
475
+
476
+ var datNum = ( int ) ( ( mod . data . modOffset / 8 ) & 0x0F ) / 2 ;
477
+ var dat = XivDataFiles . GetXivDataFile ( mod . datFile ) ;
454
478
479
+ var datPath = $ "{ _gameInfo . GameDirectory } /{ dat . GetDataFileName ( ) } { Dat . DatExtension } { datNum } ";
480
+
481
+ int uncompressedSize = - 1 ;
482
+ long seekTo = Dat . OffsetCorrection ( datNum , mod . data . modOffset ) + 8 ;
483
+
484
+ // Seek to and read the uncompressed texture size catching any exceptions
485
+ // because we handle further processing with the initial value of -1
486
+ try
487
+ {
488
+ using var reader = new BinaryReader ( File . OpenRead ( datPath ) ) ;
489
+ reader . BaseStream . Position = seekTo ;
490
+
491
+ uncompressedSize = reader . ReadInt32 ( ) ;
492
+ } catch ( Exception ) { }
493
+
494
+ // If we read an uncompressed size, seek to the same position and write the fixed uncompressed texture size
495
+ if ( uncompressedSize != - 1 )
496
+ {
497
+ using var writer = new BinaryWriter ( File . OpenWrite ( datPath ) ) ;
498
+ writer . BaseStream . Position = seekTo ;
499
+
500
+ var tmp = BitConverter . GetBytes ( uncompressedSize + 80 ) ;
501
+ writer . Write ( tmp ) ;
502
+ }
503
+ }
504
+ }
505
+ }
455
506
456
507
/// <summary>
457
508
/// Populate the ui table.
0 commit comments