Skip to content

Commit 8528f8d

Browse files
committed
Update v2.3.5.6
2 parents d8e8f66 + a47e0a5 commit 8528f8d

File tree

7 files changed

+182
-18
lines changed

7 files changed

+182
-18
lines changed

xivModdingFramework/Cache/XivCache.cs

Lines changed: 56 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,47 @@ 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);
454478

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+
}
455506

456507
/// <summary>
457508
/// Populate the ui table.

xivModdingFramework/General/CMP.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal static async Task ApplyRgspFile(string filePath, IndexFile index = null
4040
var _dat = new Dat(XivCache.GameInfo.GameDirectory);
4141
var rgspData = await _dat.GetType2Data(filePath, false, index, modlist);
4242

43-
await ApplyRgspFile(rgspData);
43+
await ApplyRgspFile(rgspData, index, modlist);
4444
}
4545

4646
/// <summary>

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

xivModdingFramework/Items/DataContainers/XivUi.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public class XivUi : IItem
7777
/// </summary>
7878
public int IconNumber { get; set; }
7979

80+
/// <summary>
81+
/// Whether or not this file has a hi-res equivalent
82+
/// </summary>
83+
public bool HasHiRes { get; private set; }
8084

8185
/// <summary>
8286
/// Gets the item's name as it should be written to the modlist/modpack files.
@@ -119,9 +123,12 @@ public override int GetHashCode()
119123
return this.Name.GetHashCode() ^ this.IconNumber.GetHashCode();
120124
}
121125

126+
private const string HiResUiExt = "_hr1";
122127

123-
public async Task<Dictionary<string, string>> GetTexPaths()
128+
public async Task<Dictionary<string, string>> GetTexPaths(bool addLowRes, bool addHiRes)
124129
{
130+
var resPaths = new Dictionary<string, string>();
131+
125132
if(SecondaryCategory == XivStrings.Maps)
126133
{
127134
var _tex = new Tex(XivCache.GameInfo.GameDirectory);
@@ -131,14 +138,29 @@ public async Task<Dictionary<string, string>> GetTexPaths()
131138
} else if(SecondaryCategory == XivStrings.HUD)
132139
{
133140
//ui/uld/aozactionlearned.tex
134-
return new Dictionary<string, string>() { { Name, "ui/uld/" + Name.ToLower() + ".tex" } };
141+
HasHiRes = true;
142+
143+
if (addLowRes)
144+
resPaths.Add(Name, "ui/uld/" + Name.ToLower() + ".tex");
145+
146+
if (addHiRes)
147+
resPaths.Add(Name, "ui/uld/" + Name.ToLower() + HiResUiExt + ".tex");
135148
}
136149
else
137150
{
151+
HasHiRes = true;
152+
138153
var block = ((IconNumber / 1000) * 1000).ToString().PadLeft(6,'0');
139154
var icon = IconNumber.ToString().PadLeft(6, '0');
140-
return new Dictionary<string, string>() { { Name, "ui/icon/" + block + '/' + icon + ".tex" } };
155+
156+
if (addLowRes)
157+
resPaths.Add(Name, "ui/icon/" + block + '/' + icon + ".tex");
158+
159+
if (addHiRes)
160+
resPaths.Add(Name, "ui/icon/" + block + '/' + icon + HiResUiExt + ".tex");
141161
}
162+
163+
return resPaths;
142164
}
143165
}
144166
}

xivModdingFramework/Mods/FileTypes/TTMP.cs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// xivModdingFramework
1+
// xivModdingFramework
22
// Copyright © 2018 Rafael Gonzalez - All Rights Reserved
33
//
44
// This program is free software: you can redistribute it and/or modify
@@ -48,9 +48,9 @@ public class TTMP
4848
".cmp", ".imc", ".eqdp", ".eqp", ".gmp", ".est"
4949
};
5050

51-
private readonly string _currentWizardTTMPVersion = "1.2w";
52-
private readonly string _currentSimpleTTMPVersion = "1.2s";
53-
private const string _minimumAssembly = "1.2.0.0";
51+
private readonly string _currentWizardTTMPVersion = "1.3w";
52+
private readonly string _currentSimpleTTMPVersion = "1.3s";
53+
private const string _minimumAssembly = "1.3.0.0";
5454

5555
private string _tempMPD, _tempMPL, _source;
5656
private readonly DirectoryInfo _modPackDirectory;
@@ -423,7 +423,7 @@ public Task<List<OriginalModPackJson>> GetOriginalModPackJsonData(DirectoryInfo
423423
/// </summary>
424424
/// <param name="modPackDirectory">The mod pack directory</param>
425425
/// <returns>The version of the mod pack as a string</returns>
426-
public string GetVersion(DirectoryInfo modPackDirectory)
426+
public static string GetVersion(DirectoryInfo modPackDirectory)
427427
{
428428
ModPackJson modPackJson = null;
429429

@@ -461,6 +461,11 @@ public string GetVersion(DirectoryInfo modPackDirectory)
461461
{
462462
if (modsJson == null || modsJson.Count == 0) return (0, 0, "", 0);
463463

464+
if(XivCache.GameInfo.UseLumina)
465+
{
466+
throw new Exception("Cannot import modpacks via TexTools in Lumina mode.");
467+
}
468+
464469
var startTime = DateTime.Now.Ticks;
465470
long endTime = 0;
466471
long part1Duration = 0;
@@ -529,6 +534,7 @@ await Task.Run(async () =>
529534

530535
var _modding = new Modding(XivCache.GameInfo.GameDirectory);
531536
var modList = _modding.GetModList();
537+
var needsTexFix = DoesTexNeedFixing(modPackDirectory);
532538

533539
// 0 - Extract the MPD file.
534540
using (var zf = ZipFile.Read(modPackDirectory.FullName))
@@ -572,6 +578,10 @@ await Task.Run(async () =>
572578
{
573579
binaryReader.BaseStream.Seek(modJson.ModOffset, SeekOrigin.Begin);
574580
var data = binaryReader.ReadBytes(modJson.ModSize);
581+
582+
if (modJson.FullPath.EndsWith(".tex") && needsTexFix)
583+
FixupTextoolsTex(data);
584+
575585
var df = IOUtil.GetDataFileFromPath(modJson.FullPath);
576586

577587
var size = data.Length;
@@ -953,6 +963,46 @@ await Task.Run(async () =>
953963
return (count, errorCount, importErrors, seconds);
954964
}
955965

966+
/// <summary>
967+
/// Parse the version out of this modpack to determine whether or not we need
968+
/// to add 80 to the uncompressed size of the Tex files contained within.
969+
/// </summary>
970+
/// <param name="mpd">The path to the modpack.</param>
971+
/// <returns>True if we must modify tex header uncompressed sizes, false otherwise.</returns>
972+
private static bool DoesTexNeedFixing(DirectoryInfo mpd) {
973+
974+
var ver = GetVersion(mpd);
975+
if (string.IsNullOrEmpty(ver))
976+
return true;
977+
978+
var newVer = ver;
979+
980+
var lastChar = ver.Substring(ver.Length - 1)[0];
981+
if (char.IsLetter(lastChar))
982+
newVer = ver.Substring(0, ver.Length - 1);
983+
984+
double.TryParse(newVer, out var verDouble);
985+
986+
return verDouble < 1.3;
987+
}
988+
989+
/// <summary>
990+
/// Fix xivModdingFramework TEX quirks.
991+
/// </summary>
992+
/// <param name="tex">The TEX data to be fixed up.</param>
993+
public static void FixupTextoolsTex(byte[] tex) {
994+
995+
// Read the uncompressed size from the file
996+
var size = BitConverter.ToInt32(tex, 8);
997+
var newSize = size + 80;
998+
999+
byte[] buffer = BitConverter.GetBytes(newSize);
1000+
tex[8] = buffer[0];
1001+
tex[9] = buffer[1];
1002+
tex[10] = buffer[2];
1003+
tex[11] = buffer[3];
1004+
}
1005+
9561006
/// <summary>
9571007
/// Gets the data type from an item path
9581008
/// </summary>

xivModdingFramework/SqPack/FileTypes/Dat.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// xivModdingFramework
1+
// xivModdingFramework
22
// Copyright © 2018 Rafael Gonzalez - All Rights Reserved
33
//
44
// This program is free software: you can redistribute it and/or modify
@@ -847,6 +847,47 @@ await Task.Run(async () =>
847847
return (meshCount, materialCount, byteList.ToArray());
848848
}
849849

850+
public async Task<uint> GetReportedType4UncompressedSize(XivDataFile df, long offsetWithDatNumber)
851+
{
852+
// This formula is used to obtain the dat number in which the offset is located
853+
var datNum = (int)((offsetWithDatNumber / 8) & 0x0F) / 2;
854+
855+
var offset = OffsetCorrection(datNum, offsetWithDatNumber);
856+
857+
var datPath = Path.Combine(_gameDirectory.FullName, $"{df.GetDataFileName()}{DatExtension}{datNum}");
858+
859+
return await Task.Run(async () =>
860+
{
861+
using (var br = new BinaryReader(File.OpenRead(datPath)))
862+
{
863+
br.BaseStream.Seek(offset+8, SeekOrigin.Begin);
864+
865+
var size = br.ReadUInt32();
866+
return size;
867+
}
868+
});
869+
}
870+
871+
public async Task UpdateType4UncompressedSize(XivDataFile df, long offsetWithDatNumber, uint correctedFileSize)
872+
{
873+
// This formula is used to obtain the dat number in which the offset is located
874+
var datNum = (int)((offsetWithDatNumber / 8) & 0x0F) / 2;
875+
876+
var offset = OffsetCorrection(datNum, offsetWithDatNumber);
877+
878+
var datPath = Path.Combine(_gameDirectory.FullName, $"{df.GetDataFileName()}{DatExtension}{datNum}");
879+
880+
await Task.Run(async () =>
881+
{
882+
using (var br = new BinaryWriter(File.OpenWrite(datPath)))
883+
{
884+
br.BaseStream.Seek(offset + 8, SeekOrigin.Begin);
885+
br.Write(correctedFileSize);
886+
}
887+
});
888+
}
889+
890+
850891
/// <summary>
851892
/// Gets the original or modded data for type 4 files based on the path specified.
852893
/// </summary>
@@ -1266,7 +1307,7 @@ public byte[] MakeType4DatHeader(XivTexFormat format, List<short> mipPartOffsets
12661307

12671308
headerData.AddRange(BitConverter.GetBytes(headerSize + headerPadding));
12681309
headerData.AddRange(BitConverter.GetBytes(4));
1269-
headerData.AddRange(BitConverter.GetBytes(uncompressedLength));
1310+
headerData.AddRange(BitConverter.GetBytes(uncompressedLength + 80));
12701311
headerData.AddRange(BitConverter.GetBytes(0));
12711312
headerData.AddRange(BitConverter.GetBytes(0));
12721313
headerData.AddRange(BitConverter.GetBytes(newMipCount));

xivModdingFramework/xivModdingFramework.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<AssemblyVersion>1.2.0.0</AssemblyVersion>
5+
<AssemblyVersion>1.3.0.0</AssemblyVersion>
66
<Version>1.0.8</Version>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<LangVersion>latest</LangVersion>

0 commit comments

Comments
 (0)