|
25 | 25 | using System.Threading;
|
26 | 26 | using System.Threading.Tasks;
|
27 | 27 | using xivModdingFramework.Cache;
|
| 28 | +using xivModdingFramework.Exd.FileTypes; |
28 | 29 | using xivModdingFramework.General.Enums;
|
29 | 30 | using xivModdingFramework.Helpers;
|
30 | 31 | using xivModdingFramework.Mods.DataContainers;
|
@@ -433,18 +434,13 @@ public string GetVersion(DirectoryInfo modPackDirectory)
|
433 | 434 | /// <param name="gameDirectory">The game directory</param>
|
434 | 435 | /// <param name="modListDirectory">The mod list directory</param>
|
435 | 436 | /// <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> |
437 | 438 | /// <returns>The number of total mods imported</returns>
|
438 | 439 | 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 ) |
440 | 441 | {
|
441 | 442 | if (modsJson == null || modsJson.Count == 0) return (0, 0, "", 0);
|
442 | 443 |
|
443 |
| - if(RootConversions == null) |
444 |
| - { |
445 |
| - RootConversions = new Dictionary<XivDependencyRoot, XivDependencyRoot>(); |
446 |
| - } |
447 |
| - |
448 | 444 | var startTime = DateTime.Now.Ticks;
|
449 | 445 |
|
450 | 446 | var dat = new Dat(gameDirectory);
|
@@ -636,49 +632,69 @@ await Task.Run(async () =>
|
636 | 632 | }
|
637 | 633 | var modPack = modList.ModPacks.First(x => x.name == modsJson[0].ModPackEntry.name);
|
638 | 634 |
|
639 |
| - if (RootConversions.Count > 0) |
| 635 | + if (GetRootConversionsFunction != null) |
640 | 636 | {
|
641 |
| - // If we have roots to convert, we get to do some extra work here. |
642 | 637 |
|
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 | + } |
645 | 652 |
|
646 |
| - HashSet<string> filesToReset = new HashSet<string>(); |
647 |
| - foreach(var dfe in conversionsByDf) |
| 653 | + if (rootConversions != null && rootConversions.Count > 0) |
648 | 654 | {
|
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. |
654 | 657 |
|
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())); |
657 | 660 |
|
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) |
660 | 666 | {
|
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; |
663 | 670 |
|
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) |
665 | 675 | {
|
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); |
667 | 678 |
|
668 |
| - } |
| 679 | + if (fileKv.Key != fileKv.Value) |
| 680 | + { |
| 681 | + filesToReset.Add(fileKv.Key); |
669 | 682 |
|
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 | + } |
673 | 689 | }
|
674 |
| - } |
675 | 690 |
|
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); |
681 | 696 |
|
| 697 | + } |
682 | 698 | }
|
683 | 699 | }
|
684 | 700 | }
|
|
0 commit comments