Skip to content

Commit af858e6

Browse files
committed
Logic for when to create a new DAT file cleaned up and compressed into a single function.
1 parent 1481bb5 commit af858e6

File tree

1 file changed

+46
-54
lines changed
  • xivModdingFramework/SqPack/FileTypes

1 file changed

+46
-54
lines changed

xivModdingFramework/SqPack/FileTypes/Dat.cs

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,50 @@ public byte[] MakeType4DatHeader(XivTex xivTex, List<short> mipPartOffsets, List
12291229
return headerData.ToArray();
12301230
}
12311231

1232+
1233+
/// <summary>
1234+
/// Gets the first DAT file with space to add a new file to it.
1235+
/// Ignores default DAT files, and creates a new DAT file if necessary.
1236+
/// </summary>
1237+
/// <param name="dataFile"></param>
1238+
/// <returns></returns>
1239+
private async Task<int> GetFirstDatWithSpace(XivDataFile dataFile)
1240+
{
1241+
var targetDat = -1;
1242+
1243+
// Scan all the dat numbers...
1244+
var largestExisting = GetLargestDatNumber(dataFile);
1245+
for (int i = 0; i < largestExisting; i++)
1246+
{
1247+
var original = await IsOriginalDat(dataFile);
1248+
1249+
// Don't let us inject to original dat files.
1250+
if (original) continue;
1251+
1252+
var datPath = Path.Combine(_gameDirectory.FullName, $"{dataFile.GetDataFileName()}{DatExtension}{i}");
1253+
var fileSize = new FileInfo(datPath).Length;
1254+
1255+
// Dat is already too large, can't write to it.
1256+
if (fileSize > 2000000000) continue;
1257+
1258+
// Found an existing dat that has space.
1259+
targetDat = i;
1260+
break;
1261+
}
1262+
1263+
// Didn't find a DAT file with space, gotta create a new one.
1264+
if (targetDat < 0)
1265+
{
1266+
targetDat = CreateNewDat(dataFile);
1267+
}
1268+
1269+
if(targetDat > 7 || targetDat < 0)
1270+
{
1271+
throw new NotSupportedException("Maximum data size limit reached for DAT: " + dataFile.GetDataFileName());
1272+
}
1273+
return targetDat;
1274+
}
1275+
12321276
/// <summary>
12331277
/// Writes the newly imported data to the .dat for modifications.
12341278
/// </summary>
@@ -1261,7 +1305,8 @@ public async Task<int> WriteToDat(List<byte> importData, Mod modEntry, string in
12611305
source = "FilesAddedByTexTools";
12621306

12631307

1264-
var datNum = GetLargestDatNumber(dataFile);
1308+
// This finds the first dat with space, OR creates one if needed.
1309+
var datNum = await GetFirstDatWithSpace(dataFile);
12651310

12661311
var modDatPath = Path.Combine(_gameDirectory.FullName, $"{dataFile.GetDataFileName()}{DatExtension}{datNum}");
12671312

@@ -1289,33 +1334,6 @@ public async Task<int> WriteToDat(List<byte> importData, Mod modEntry, string in
12891334
var fileLength = new FileInfo(modDatPath).Length;
12901335
_lock.Release();
12911336

1292-
// Creates a new Dat if the current dat is at the 2GB limit
1293-
if (modEntry == null)
1294-
{
1295-
if (fileLength >= 2000000000)
1296-
{
1297-
datNum = CreateNewDat(dataFile);
1298-
1299-
modDatPath = Path.Combine(_gameDirectory.FullName, $"{dataFile.GetDataFileName()}{DatExtension}{datNum}");
1300-
}
1301-
else
1302-
{
1303-
// If it is an original dat file, then create a new mod dat file
1304-
if (await IsOriginalDat(dataFile))
1305-
{
1306-
datNum = CreateNewDat(dataFile);
1307-
1308-
modDatPath = Path.Combine(_gameDirectory.FullName, $"{dataFile.GetDataFileName()}{DatExtension}{datNum}");
1309-
}
1310-
}
1311-
}
1312-
1313-
if (datNum >= 8)
1314-
{
1315-
throw new NotSupportedException($"Mod data limit has been reached, no new mods can be imported for dat file {dataFile.GetDataFileName()}.\n\n" +
1316-
$"Back up any mods you'd like to keep and perform a start over to be able to import new mods.");
1317-
}
1318-
13191337
// Checks to make sure the offsets in the mod list are not 0
13201338
// If they are 0, something went wrong in the import proccess (Technically shouldn't happen)
13211339
if (modEntry != null)
@@ -1480,32 +1498,6 @@ where entry.fullPath.Equals(modEntry.fullPath)
14801498
// If there was no mod entry overwritten, write the new import data at the end of the dat file
14811499
if (!dataOverwritten)
14821500
{
1483-
/*
1484-
* If the item has been previously modified, but the new compressed data to be imported is larger than the existing data,
1485-
* and no empty slot was found for it, then write the data to the highest dat,
1486-
* or create a new one if necessary
1487-
*/
1488-
if (modEntry != null)
1489-
{
1490-
modDatPath = Path.Combine(_gameDirectory.FullName, $"{dataFile.GetDataFileName()}{DatExtension}{datNum}");
1491-
1492-
fileLength = new FileInfo(modDatPath).Length;
1493-
1494-
if (fileLength >= 2000000000)
1495-
{
1496-
_lock.Release();
1497-
datNum = CreateNewDat(dataFile);
1498-
await _lock.WaitAsync();
1499-
1500-
modDatPath = Path.Combine(_gameDirectory.FullName, $"{dataFile.GetDataFileName()}{DatExtension}{datNum}");
1501-
}
1502-
1503-
if (datNum >= 8)
1504-
{
1505-
throw new NotSupportedException($"Dat limit has been reached, no new mods can be imported for {dataFile.GetDataFileName()}");
1506-
}
1507-
}
1508-
15091501
using (var bw = new BinaryWriter(File.OpenWrite(modDatPath)))
15101502
{
15111503
bw.BaseStream.Seek(0, SeekOrigin.End);

0 commit comments

Comments
 (0)