Skip to content

Commit 06ab3f7

Browse files
committed
Beta 2.3.3.1
2 parents c30def4 + bc831b1 commit 06ab3f7

File tree

2 files changed

+62
-39
lines changed

2 files changed

+62
-39
lines changed

xivModdingFramework/Mods/FileTypes/TTMP.cs

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Threading;
2626
using System.Threading.Tasks;
2727
using xivModdingFramework.Cache;
28+
using xivModdingFramework.Exd.FileTypes;
2829
using xivModdingFramework.General.Enums;
2930
using xivModdingFramework.Helpers;
3031
using xivModdingFramework.Mods.DataContainers;
@@ -433,18 +434,13 @@ public string GetVersion(DirectoryInfo modPackDirectory)
433434
/// <param name="gameDirectory">The game directory</param>
434435
/// <param name="modListDirectory">The mod list directory</param>
435436
/// <param name="progress">The progress of the import</param>
436-
/// <param name="RootConversions">Roots to convert during import, if any conversions are required.</param>
437+
/// <param name="GetRootConversionsFunction">Function called part-way through import to resolve rood conversions, if any are desired. Function takes a List of files, the in-progress modified index and modlist files, and returns a dictionary of conversion data. If this function throws and OperationCancelledException, the import is cancelled.</param>
437438
/// <returns>The number of total mods imported</returns>
438439
public async Task<(int ImportCount, int ErrorCount, string Errors, float Duration)> ImportModPackAsync(DirectoryInfo modPackDirectory, List<ModsJson> modsJson,
439-
DirectoryInfo gameDirectory, DirectoryInfo modListDirectory, IProgress<(int current, int total, string message)> progress, Dictionary<XivDependencyRoot, XivDependencyRoot> RootConversions = null)
440+
DirectoryInfo gameDirectory, DirectoryInfo modListDirectory, IProgress<(int current, int total, string message)> progress, Func<HashSet<string>, Dictionary<XivDataFile, IndexFile>, ModList, Task<Dictionary<XivDependencyRoot, (XivDependencyRoot Root, int Variant)>>> GetRootConversionsFunction = null )
440441
{
441442
if (modsJson == null || modsJson.Count == 0) return (0, 0, "", 0);
442443

443-
if(RootConversions == null)
444-
{
445-
RootConversions = new Dictionary<XivDependencyRoot, XivDependencyRoot>();
446-
}
447-
448444
var startTime = DateTime.Now.Ticks;
449445

450446
var dat = new Dat(gameDirectory);
@@ -636,49 +632,69 @@ await Task.Run(async () =>
636632
}
637633
var modPack = modList.ModPacks.First(x => x.name == modsJson[0].ModPackEntry.name);
638634

639-
if (RootConversions.Count > 0)
635+
if (GetRootConversionsFunction != null)
640636
{
641-
// If we have roots to convert, we get to do some extra work here.
642637

643-
// We currently have all the files loaded into our in-memory indices in their default locations.
644-
var conversionsByDf = RootConversions.GroupBy(x => IOUtil.GetDataFileFromPath(x.Key.Info.GetRootFile()));
638+
Dictionary<XivDependencyRoot, (XivDependencyRoot Root, int Variant)> rootConversions = null;
639+
try
640+
{
641+
rootConversions = await GetRootConversionsFunction(filePaths, modifiedIndexFiles, modList);
642+
} catch(OperationCanceledException ex)
643+
{
644+
// User cancelled the function or otherwise a critical error happened in the conversion function.
645+
// Cancell the import without saving anything.
646+
ErroneousFiles.Add("n/a");
647+
totalFiles = 0;
648+
importErrors = "User Cancelled Import Process.";
649+
progress.Report((0, 0, "User Cancelled Import Process."));
650+
return;
651+
}
645652

646-
HashSet<string> filesToReset = new HashSet<string>();
647-
foreach(var dfe in conversionsByDf)
653+
if (rootConversions != null && rootConversions.Count > 0)
648654
{
649-
var df = dfe.Key;
650-
foreach(var conversion in dfe)
651-
{
652-
var source = conversion.Key;
653-
var destination = conversion.Value;
655+
progress.Report((count, totalFiles, "Updating Destination Items..."));
656+
// If we have roots to convert, we get to do some extra work here.
654657

655-
var variant = -1;
656-
var convertedFiles = await RootCloner.CloneRoot(source, destination, _source, variant, null, null, modifiedIndexFiles[df], modList);
658+
// We currently have all the files loaded into our in-memory indices in their default locations.
659+
var conversionsByDf = rootConversions.GroupBy(x => IOUtil.GetDataFileFromPath(x.Key.Info.GetRootFile()));
657660

658-
// We're going to reset the original files back to their pre-modpack install state after, as long as they got moved.
659-
foreach(var fileKv in convertedFiles)
661+
HashSet<string> filesToReset = new HashSet<string>();
662+
foreach (var dfe in conversionsByDf)
663+
{
664+
var df = dfe.Key;
665+
foreach (var conversion in dfe)
660666
{
661-
// Remove the file from our json list, the conversion already handled everything we needed to do with it.
662-
var json = modsJson.RemoveAll(x => x.FullPath == fileKv.Key);
667+
var source = conversion.Key;
668+
var destination = conversion.Value.Root;
669+
var variant = conversion.Value.Variant;
663670

664-
if (fileKv.Key != fileKv.Value)
671+
var convertedFiles = await RootCloner.CloneRoot(source, destination, _source, variant, null, null, modifiedIndexFiles[df], modList, modPack);
672+
673+
// We're going to reset the original files back to their pre-modpack install state after, as long as they got moved.
674+
foreach (var fileKv in convertedFiles)
665675
{
666-
filesToReset.Add(fileKv.Key);
676+
// Remove the file from our json list, the conversion already handled everything we needed to do with it.
677+
var json = modsJson.RemoveAll(x => x.FullPath == fileKv.Key);
667678

668-
}
679+
if (fileKv.Key != fileKv.Value)
680+
{
681+
filesToReset.Add(fileKv.Key);
669682

670-
var mod = modList.Mods.First(x => x.fullPath == fileKv.Value);
671-
mod.modPack = modPack;
672-
filePaths.Remove(fileKv.Key);
683+
}
684+
685+
var mod = modList.Mods.First(x => x.fullPath == fileKv.Value);
686+
mod.modPack = modPack;
687+
filePaths.Remove(fileKv.Key);
688+
}
673689
}
674-
}
675690

676-
// Reset the index pointers back to previous.
677-
foreach (var file in filesToReset)
678-
{
679-
var oldOffset = originalIndexFiles[df].Get8xDataOffset(file);
680-
modifiedIndexFiles[df].SetDataOffset(file, oldOffset);
691+
// Reset the index pointers back to previous.
692+
foreach (var file in filesToReset)
693+
{
694+
var oldOffset = originalIndexFiles[df].Get8xDataOffset(file);
695+
modifiedIndexFiles[df].SetDataOffset(file, oldOffset);
681696

697+
}
682698
}
683699
}
684700
}

xivModdingFramework/Mods/RootCloner.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static class RootCloner
3232
/// <param name="Destination">Destination root to copy to.</param>
3333
/// <param name="ApplicationSource">Application to list as the source for the resulting mod entries.</param>
3434
/// <returns>Returns a Dictionary of all the file conversion</returns>
35-
public static async Task<Dictionary<string, string>> CloneRoot(XivDependencyRoot Source, XivDependencyRoot Destination, string ApplicationSource, int singleVariant = -1, string saveDirectory = null, IProgress<string> ProgressReporter = null, IndexFile index = null, ModList modlist = null)
35+
public static async Task<Dictionary<string, string>> CloneRoot(XivDependencyRoot Source, XivDependencyRoot Destination, string ApplicationSource, int singleVariant = -1, string saveDirectory = null, IProgress<string> ProgressReporter = null, IndexFile index = null, ModList modlist = null, ModPack modPack = null)
3636
{
3737

3838
if (ProgressReporter != null)
@@ -437,7 +437,11 @@ public static async Task<Dictionary<string, string>> CloneRoot(XivDependencyRoot
437437
ProgressReporter.Report("Updating modlist...");
438438
}
439439

440-
var modPack = new ModPack() { author = "System", name = "Item Copy - " + srcItem.Name + " to " + iName, url = "", version = "1.0" };
440+
if (modPack == null)
441+
{
442+
modPack = new ModPack() { author = "System", name = "Item Copy - " + srcItem.Name + " to " + iName, url = "", version = "1.0" };
443+
}
444+
441445
List<Mod> mods = new List<Mod>();
442446
foreach (var mod in modlist.Mods)
443447
{
@@ -453,7 +457,10 @@ public static async Task<Dictionary<string, string>> CloneRoot(XivDependencyRoot
453457
}
454458
}
455459

456-
modlist.ModPacks.Add(modPack);
460+
if (!modlist.ModPacks.Any(x => x.name == modPack.name))
461+
{
462+
modlist.ModPacks.Add(modPack);
463+
}
457464

458465
if(doSave)
459466
{

0 commit comments

Comments
 (0)