@@ -1229,6 +1229,50 @@ public byte[] MakeType4DatHeader(XivTex xivTex, List<short> mipPartOffsets, List
1229
1229
return headerData . ToArray ( ) ;
1230
1230
}
1231
1231
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
+
1232
1276
/// <summary>
1233
1277
/// Writes the newly imported data to the .dat for modifications.
1234
1278
/// </summary>
@@ -1261,7 +1305,8 @@ public async Task<int> WriteToDat(List<byte> importData, Mod modEntry, string in
1261
1305
source = "FilesAddedByTexTools" ;
1262
1306
1263
1307
1264
- var datNum = GetLargestDatNumber ( dataFile ) ;
1308
+ // This finds the first dat with space, OR creates one if needed.
1309
+ var datNum = await GetFirstDatWithSpace ( dataFile ) ;
1265
1310
1266
1311
var modDatPath = Path . Combine ( _gameDirectory . FullName , $ "{ dataFile . GetDataFileName ( ) } { DatExtension } { datNum } ") ;
1267
1312
@@ -1289,33 +1334,6 @@ public async Task<int> WriteToDat(List<byte> importData, Mod modEntry, string in
1289
1334
var fileLength = new FileInfo ( modDatPath ) . Length ;
1290
1335
_lock . Release ( ) ;
1291
1336
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
-
1319
1337
// Checks to make sure the offsets in the mod list are not 0
1320
1338
// If they are 0, something went wrong in the import proccess (Technically shouldn't happen)
1321
1339
if ( modEntry != null )
@@ -1480,32 +1498,6 @@ where entry.fullPath.Equals(modEntry.fullPath)
1480
1498
// If there was no mod entry overwritten, write the new import data at the end of the dat file
1481
1499
if ( ! dataOverwritten )
1482
1500
{
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
-
1509
1501
using ( var bw = new BinaryWriter ( File . OpenWrite ( modDatPath ) ) )
1510
1502
{
1511
1503
bw . BaseStream . Seek ( 0 , SeekOrigin . End ) ;
0 commit comments