Skip to content

Commit 5d597ab

Browse files
committed
fix currently installed Tex mods as part of cache rebuild
1 parent 524e08f commit 5d597ab

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

xivModdingFramework/Cache/XivCache.cs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.Data.SQLite;
@@ -15,6 +15,7 @@
1515
using xivModdingFramework.Items.Interfaces;
1616
using xivModdingFramework.Models.FileTypes;
1717
using xivModdingFramework.Mods;
18+
using xivModdingFramework.Mods.DataContainers;
1819
using xivModdingFramework.Resources;
1920
using xivModdingFramework.SqPack.FileTypes;
2021

@@ -31,7 +32,7 @@ public static class XivCache
3132
private static GameInfo _gameInfo;
3233
private static DirectoryInfo _dbPath;
3334
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");
3536
private const string dbFileName = "mod_cache.db";
3637
private const string rootCacheFileName = "item_sets.db";
3738
private const string creationScript = "CreateCacheDB.sql";
@@ -187,7 +188,11 @@ public static void SetGameInfo(GameInfo gameInfo = null, bool enableCacheWorker
187188
var reason = CacheNeedsRebuild();
188189
if (reason != CacheRebuildReason.CacheOK && !_REBUILDING)
189190
{
190-
RebuildCache(reason);
191+
var ver =
192+
reason == CacheRebuildReason.CacheVersionUpdate
193+
? new Version(GetMetaValue("cache_version")) : CacheVersion;
194+
195+
RebuildCache(ver, reason);
191196
}
192197
}
193198

@@ -277,7 +282,7 @@ private static CacheRebuildReason CacheNeedsRebuild()
277282
/// help ensure it's never accidentally called
278283
/// without an await.
279284
/// </summary>
280-
public static void RebuildCache(CacheRebuildReason reason = CacheRebuildReason.ManualRequest)
285+
public static void RebuildCache(Version previousVersion, CacheRebuildReason reason = CacheRebuildReason.ManualRequest)
281286
{
282287
CacheWorkerEnabled = false;
283288
_REBUILDING = true;
@@ -309,7 +314,13 @@ public static void RebuildCache(CacheRebuildReason reason = CacheRebuildReason.M
309314
tasks.Add(RebuildFurnitureCache());
310315
tasks.Add(BuildModdedItemDependencies());
311316

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);
313324

314325
var post = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
315326

@@ -451,7 +462,45 @@ private static void CreateCache()
451462
}
452463
}
453464

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);
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
485+
{
486+
using var reader = new BinaryReader(File.OpenRead(datPath));
487+
reader.BaseStream.Position = seekTo;
488+
489+
uncompressedSize = reader.ReadInt32();
490+
}
491+
492+
// If we read an uncompressed size, seek to the same position and write the fixed uncompressed texture size
493+
if (uncompressedSize != -1)
494+
{
495+
using var writer = new BinaryWriter(File.OpenWrite(datPath));
496+
writer.BaseStream.Position = seekTo;
454497

498+
var tmp = BitConverter.GetBytes(uncompressedSize + 80);
499+
writer.Write(tmp);
500+
}
501+
}
502+
}
503+
}
455504

456505
/// <summary>
457506
/// Populate the ui table.

xivModdingFramework/Helpers/ProblemChecker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public Task PerformStartOver(DirectoryInfo backupsDirectory, IProgress<string> p
258258

259259
await Task.Run(async () =>
260260
{
261-
XivCache.RebuildCache();
261+
XivCache.RebuildCache(XivCache.CacheVersion);
262262
});
263263
}
264264
});

0 commit comments

Comments
 (0)