Skip to content

Commit

Permalink
Fix Victoria 3 import and mod type indicators
Browse files Browse the repository at this point in the history
Make Irony able to locate mods with differently named descriptors but still pointing towards the same path
  • Loading branch information
bcssov committed Dec 1, 2022
1 parent 51023c8 commit 40656e5
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 114 deletions.
20 changes: 15 additions & 5 deletions src/IronyModManager.IO/Mods/Exporter/BaseExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
// Created : 08-11-2020
//
// Last Modified By : Mario
// Last Modified On : 10-06-2020
// Last Modified On : 12-01-2022
// ***********************************************************************
// <copyright file="BaseExporter.cs" company="Mario">
// Mario
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using IronyModManager.IO.Mods.Models.Paradox;
Expand All @@ -29,6 +29,16 @@ internal abstract class BaseExporter
{
#region Methods

/// <summary>
/// Maps the name of the file.
/// </summary>
/// <param name="mod">The mod.</param>
/// <returns>System.String.</returns>
protected virtual string MapFileName(IMod mod)
{
return mod.FileName;
}

/// <summary>
/// Maps the mod data.
/// </summary>
Expand Down Expand Up @@ -79,15 +89,15 @@ protected virtual void MapPdxPath(IPdxMod pdxMod, IMod mod)
if (mod.FileName.EndsWith(Shared.Constants.ZipExtension, StringComparison.OrdinalIgnoreCase) ||
mod.FileName.EndsWith(Shared.Constants.BinExtension, StringComparison.OrdinalIgnoreCase))
{
pdxMod.ArchivePath = mod.FileName;
pdxMod.ArchivePath = MapFileName(mod);
if (mod.Source != ModSource.Local)
{
pdxMod.DirPath = Path.GetDirectoryName(mod.FileName);
pdxMod.DirPath = Path.GetDirectoryName(MapFileName(mod));
}
}
else
{
pdxMod.DirPath = mod.FileName;
pdxMod.DirPath = MapFileName(mod);
}
}

Expand Down
12 changes: 3 additions & 9 deletions src/IronyModManager.IO/Mods/Exporter/JsonExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 08-11-2020
//
// Last Modified By : Mario
// Last Modified On : 10-29-2022
// Last Modified On : 12-01-2022
// ***********************************************************************
// <copyright file="JsonExporter.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -175,26 +175,20 @@ private async Task<bool> ExportContentLoadModsAsync(ModWriterParameters paramete
contentLoad.EnabledMods.Clear();
}

if (parameters.EnabledMods != null)
{
parameters.EnabledMods.ToList().ForEach(p =>
parameters.EnabledMods?.ToList().ForEach(p =>
{
contentLoad.EnabledMods.Add(new EnabledMod()
{
Path = p.FullPath
});
});
}
if (parameters.TopPriorityMods != null)
{
parameters.TopPriorityMods.ToList().ForEach(p =>
parameters.TopPriorityMods?.ToList().ForEach(p =>
{
contentLoad.EnabledMods.Add(new EnabledMod()
{
Path = p.FullPath
});
});
}
return await WritePdxModelAsync(contentLoad, contentPath);
}

Expand Down
87 changes: 56 additions & 31 deletions src/IronyModManager.IO/Mods/Exporter/SQLiteExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 08-11-2020
//
// Last Modified By : Mario
// Last Modified On : 10-29-2022
// Last Modified On : 12-01-2022
// ***********************************************************************
// <copyright file="SQLiteExporter.cs" company="Mario">
// Mario
Expand All @@ -18,6 +18,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using IronyModManager.IO.Common;
using IronyModManager.IO.Common.Mods;
using IronyModManager.Models.Common;
using IronyModManager.Shared;
Expand Down Expand Up @@ -451,17 +452,19 @@ Models.Paradox.v4.Playsets getDefaultIronyCollection()
/// </summary>
/// <param name="pdxMod">The PDX mod.</param>
/// <param name="mod">The mod.</param>
/// <param name="descriptorType">Type of the descriptor.</param>
/// <returns>Models.Paradox.v2.Mods.</returns>
private Models.Paradox.v2.Mods MapPdxModV2(Models.Paradox.v2.Mods pdxMod, IMod mod)
private Models.Paradox.v2.Mods MapPdxModV2(Models.Paradox.v2.Mods pdxMod, IMod mod, DescriptorType descriptorType)
{
if (pdxMod == null)
pdxMod ??= new Models.Paradox.v2.Mods()
{
pdxMod = new Models.Paradox.v2.Mods()
{
Id = Guid.NewGuid().ToString()
};
}
Id = Guid.NewGuid().ToString()
};
MapModData(pdxMod, mod);
if (descriptorType == DescriptorType.JsonMetadata)
{
pdxMod.GameRegistryId = null;
}
return pdxMod;
}

Expand All @@ -470,20 +473,32 @@ private Models.Paradox.v2.Mods MapPdxModV2(Models.Paradox.v2.Mods pdxMod, IMod m
/// </summary>
/// <param name="pdxMod">The PDX mod.</param>
/// <param name="mod">The mod.</param>
/// <param name="descriptorType">Type of the descriptor.</param>
/// <returns>Models.Paradox.v4.Mods.</returns>
private Models.Paradox.v4.Mods MapPdxModV4(Models.Paradox.v4.Mods pdxMod, IMod mod)
private Models.Paradox.v4.Mods MapPdxModV4(Models.Paradox.v4.Mods pdxMod, IMod mod, DescriptorType descriptorType)
{
if (pdxMod == null)
pdxMod ??= new Models.Paradox.v4.Mods()
{
pdxMod = new Models.Paradox.v4.Mods()
{
Id = Guid.NewGuid().ToString()
};
}
Id = Guid.NewGuid().ToString()
};
MapModData(pdxMod, mod);
if (descriptorType == DescriptorType.JsonMetadata)
{
pdxMod.GameRegistryId = null;
}
return pdxMod;
}

/// <summary>
/// Maps the name of the file.
/// </summary>
/// <param name="mod">The mod.</param>
/// <returns>System.String.</returns>
protected override string MapFileName(IMod mod)
{
return mod.FileName.StandardizeDirectorySeparator();
}

/// <summary>
/// prepare mods transaction as an asynchronous operation.
/// </summary>
Expand All @@ -492,8 +507,9 @@ private Models.Paradox.v4.Mods MapPdxModV4(Models.Paradox.v4.Mods pdxMod, IMod m
/// <param name="exportMods">The export mods.</param>
/// <param name="mods">The mods.</param>
/// <param name="removeInvalid">if set to <c>true</c> [remove invalid].</param>
/// <param name="descriptorType">Type of the descriptor.</param>
/// <returns>IEnumerable&lt;Models.Paradox.v2.Mods&gt;.</returns>
private async Task<IEnumerable<Models.Paradox.v2.Mods>> PrepareModsTransactionV2Async(IDbConnection con, IDbTransaction transaction, List<IMod> exportMods, IEnumerable<Models.Paradox.v2.Mods> mods, bool removeInvalid)
private async Task<IEnumerable<Models.Paradox.v2.Mods>> PrepareModsTransactionV2Async(IDbConnection con, IDbTransaction transaction, List<IMod> exportMods, IEnumerable<Models.Paradox.v2.Mods> mods, bool removeInvalid, DescriptorType descriptorType)
{
var result = new HashSet<Models.Paradox.v2.Mods>();
if (mods?.Count() > 0)
Expand All @@ -513,10 +529,10 @@ private Models.Paradox.v4.Mods MapPdxModV4(Models.Paradox.v4.Mods pdxMod, IMod m
}
foreach (var item in exportMods)
{
var pdxMod = mods.FirstOrDefault(p => p.GameRegistryId.Equals(item.DescriptorFile, StringComparison.OrdinalIgnoreCase));
var pdxMod = descriptorType == DescriptorType.DescriptorMod ? mods.FirstOrDefault(p => p.GameRegistryId.Equals(item.DescriptorFile, StringComparison.OrdinalIgnoreCase)) : mods.FirstOrDefault(p => p.DirPath.StandardizeDirectorySeparator().Equals(item.FileName.StandardizeDirectorySeparator(), StringComparison.OrdinalIgnoreCase));
if (pdxMod == null)
{
var newPdxMod = MapPdxModV2(null, item);
var newPdxMod = MapPdxModV2(null, item, descriptorType);
toInsert.Add(newPdxMod);
result.Add(newPdxMod);
}
Expand All @@ -525,13 +541,13 @@ private Models.Paradox.v4.Mods MapPdxModV4(Models.Paradox.v4.Mods pdxMod, IMod m
// Pending delete, insert a new entry instead
if (toRemove.Contains(pdxMod))
{
var newPdxMod = MapPdxModV2(null, item);
var newPdxMod = MapPdxModV2(null, item, descriptorType);
toInsert.Add(newPdxMod);
result.Add(newPdxMod);
}
else
{
var updatedPdxMod = MapPdxModV2(pdxMod, item);
var updatedPdxMod = MapPdxModV2(pdxMod, item, descriptorType);
toUpdate.Add(updatedPdxMod);
result.Add(updatedPdxMod);
}
Expand All @@ -555,7 +571,7 @@ private Models.Paradox.v4.Mods MapPdxModV4(Models.Paradox.v4.Mods pdxMod, IMod m
var toInsert = new HashSet<Models.Paradox.v2.Mods>();
foreach (var mod in exportMods)
{
var pdxMod = MapPdxModV2(null, mod);
var pdxMod = MapPdxModV2(null, mod, descriptorType);
toInsert.Add(pdxMod);
result.Add(pdxMod);
}
Expand All @@ -575,8 +591,9 @@ private Models.Paradox.v4.Mods MapPdxModV4(Models.Paradox.v4.Mods pdxMod, IMod m
/// <param name="exportMods">The export mods.</param>
/// <param name="mods">The mods.</param>
/// <param name="removeInvalid">if set to <c>true</c> [remove invalid].</param>
/// <param name="descriptorType">Type of the descriptor.</param>
/// <returns>A Task&lt;IEnumerable`1&gt; representing the asynchronous operation.</returns>
private async Task<IEnumerable<Models.Paradox.v4.Mods>> PrepareModsTransactionV4Async(IDbConnection con, IDbTransaction transaction, List<IMod> exportMods, IEnumerable<Models.Paradox.v4.Mods> mods, bool removeInvalid)
private async Task<IEnumerable<Models.Paradox.v4.Mods>> PrepareModsTransactionV4Async(IDbConnection con, IDbTransaction transaction, List<IMod> exportMods, IEnumerable<Models.Paradox.v4.Mods> mods, bool removeInvalid, DescriptorType descriptorType)
{
var result = new HashSet<Models.Paradox.v4.Mods>();
if (mods?.Count() > 0)
Expand All @@ -596,10 +613,10 @@ private Models.Paradox.v4.Mods MapPdxModV4(Models.Paradox.v4.Mods pdxMod, IMod m
}
foreach (var item in exportMods)
{
var pdxMod = mods.FirstOrDefault(p => p.GameRegistryId.Equals(item.DescriptorFile, StringComparison.OrdinalIgnoreCase));
var pdxMod = descriptorType == DescriptorType.DescriptorMod ? mods.FirstOrDefault(p => p.GameRegistryId.Equals(item.DescriptorFile, StringComparison.OrdinalIgnoreCase)) : mods.FirstOrDefault(p => p.DirPath.StandardizeDirectorySeparator().Equals(item.FileName.StandardizeDirectorySeparator(), StringComparison.OrdinalIgnoreCase));
if (pdxMod == null)
{
var newPdxMod = MapPdxModV4(null, item);
var newPdxMod = MapPdxModV4(null, item, descriptorType);
toInsert.Add(newPdxMod);
result.Add(newPdxMod);
}
Expand All @@ -608,13 +625,13 @@ private Models.Paradox.v4.Mods MapPdxModV4(Models.Paradox.v4.Mods pdxMod, IMod m
// Pending delete, insert a new entry instead
if (toRemove.Contains(pdxMod))
{
var newPdxMod = MapPdxModV4(null, item);
var newPdxMod = MapPdxModV4(null, item, descriptorType);
toInsert.Add(newPdxMod);
result.Add(newPdxMod);
}
else
{
var updatedPdxMod = MapPdxModV4(pdxMod, item);
var updatedPdxMod = MapPdxModV4(pdxMod, item, descriptorType);
toUpdate.Add(updatedPdxMod);
result.Add(updatedPdxMod);
}
Expand All @@ -638,7 +655,7 @@ private Models.Paradox.v4.Mods MapPdxModV4(Models.Paradox.v4.Mods pdxMod, IMod m
var toInsert = new HashSet<Models.Paradox.v4.Mods>();
foreach (var mod in exportMods)
{
var pdxMod = MapPdxModV4(null, mod);
var pdxMod = MapPdxModV4(null, mod, descriptorType);
toInsert.Add(pdxMod);
result.Add(pdxMod);
}
Expand Down Expand Up @@ -864,8 +881,12 @@ private async Task SyncModsV2Async(Models.Paradox.v2.Playsets collection, ModWri
using var transaction = con.BeginTransaction();
try
{
var allMods = await PrepareModsTransactionV2Async(con, transaction, exportMods, mods, !parameters.AppendOnly);
await PreparePlaysetModsTransactionV2Async(con, transaction, collection, allMods.Where(p => enabledMods.Any(s => s.DescriptorFile.Equals(p.GameRegistryId))), !parameters.AppendOnly);
var allMods = await PrepareModsTransactionV2Async(con, transaction, exportMods, mods, !parameters.AppendOnly, parameters.DescriptorType);
await PreparePlaysetModsTransactionV2Async(con, transaction, collection,
parameters.DescriptorType == DescriptorType.DescriptorMod ?
allMods.Where(p => enabledMods.Any(s => s.DescriptorFile.Equals(p.GameRegistryId, StringComparison.OrdinalIgnoreCase))) :
allMods.Where(p => enabledMods.Any(s => s.FileName.StandardizeDirectorySeparator().Equals(p.DirPath.StandardizeDirectorySeparator(), StringComparison.OrdinalIgnoreCase))),
!parameters.AppendOnly);
transaction.Commit();
}
catch (Exception ex)
Expand Down Expand Up @@ -906,8 +927,12 @@ private async Task SyncModsV4Async(Models.Paradox.v4.Playsets collection, ModWri
using var transaction = con.BeginTransaction();
try
{
var allMods = await PrepareModsTransactionV4Async(con, transaction, exportMods, mods, !parameters.AppendOnly);
await PreparePlaysetModsTransactionV4Async(con, transaction, collection, allMods.Where(p => enabledMods.Any(s => s.DescriptorFile.Equals(p.GameRegistryId))), !parameters.AppendOnly);
var allMods = await PrepareModsTransactionV4Async(con, transaction, exportMods, mods, !parameters.AppendOnly, parameters.DescriptorType);
await PreparePlaysetModsTransactionV4Async(con, transaction, collection,
parameters.DescriptorType == DescriptorType.DescriptorMod ?
allMods.Where(p => enabledMods.Any(s => s.DescriptorFile.Equals(p.GameRegistryId, StringComparison.OrdinalIgnoreCase))) :
allMods.Where(p => enabledMods.Any(s => s.FileName.StandardizeDirectorySeparator().Equals(p.DirPath.StandardizeDirectorySeparator(), StringComparison.OrdinalIgnoreCase))),
!parameters.AppendOnly);
transaction.Commit();
}
catch (Exception ex)
Expand Down
20 changes: 17 additions & 3 deletions src/IronyModManager.IO/Mods/Importers/ParadoxLauncherImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 08-12-2020
//
// Last Modified By : Mario
// Last Modified On : 11-29-2022
// Last Modified On : 12-01-2022
// ***********************************************************************
// <copyright file="ParadoxLauncherImporter.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -229,7 +229,14 @@ protected virtual async Task<ICollectionImportResult> DatabaseImportv2Async(ModC
{
var result = DIResolver.Get<ICollectionImportResult>();
result.Name = activeCollection.Name;
result.Descriptors = validMods.Select(p => p.GameRegistryId).ToList();
if (parameters.DescriptorType == Common.DescriptorType.DescriptorMod)
{
result.Descriptors = validMods.Select(p => p.GameRegistryId).ToList();
}
else
{
result.FullPaths = validMods.Select(p => p.DirPath.StandardizeDirectorySeparator()).ToList();
}
result.ModNames = validMods.Select(p => p.DisplayName).ToList();
return result;
}
Expand Down Expand Up @@ -266,7 +273,14 @@ protected virtual async Task<ICollectionImportResult> DatabaseImportv3Async(ModC
{
var result = DIResolver.Get<ICollectionImportResult>();
result.Name = activeCollection.Name;
result.Descriptors = validMods.Select(p => p.GameRegistryId).ToList();
if (parameters.DescriptorType == Common.DescriptorType.DescriptorMod)
{
result.Descriptors = validMods.Select(p => p.GameRegistryId).ToList();
}
else
{
result.FullPaths = validMods.Select(p => p.DirPath.StandardizeDirectorySeparator()).ToList();
}
result.ModNames = validMods.Select(p => p.DisplayName).ToList();
return result;
}
Expand Down
8 changes: 7 additions & 1 deletion src/IronyModManager.Models.Common/IModCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 03-04-2020
//
// Last Modified By : Mario
// Last Modified On : 07-12-2022
// Last Modified On : 12-01-2022
// ***********************************************************************
// <copyright file="IModCollection.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -57,6 +57,12 @@ public interface IModCollection : IModel, IQueryableModel
/// <value>The mod names.</value>
IEnumerable<string> ModNames { get; set; }

/// <summary>
/// Gets or sets the mod paths.
/// </summary>
/// <value>The mod paths.</value>
IEnumerable<string> ModPaths { get; set; }

/// <summary>
/// Gets or sets the mods.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/IronyModManager.Models/ModCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 03-04-2020
//
// Last Modified By : Mario
// Last Modified On : 07-12-2022
// Last Modified On : 12-01-2022
// ***********************************************************************
// <copyright file="ModCollection.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -36,6 +36,7 @@ public ModCollection()
Mods = new List<string>();
ModNames = new List<string>();
ModIds = new List<IModCollectionSourceInfo>();
ModPaths = new List<string>();
}

#endregion Constructors
Expand Down Expand Up @@ -72,6 +73,12 @@ public ModCollection()
/// <value>The mod names.</value>
public virtual IEnumerable<string> ModNames { get; set; }

/// <summary>
/// Gets or sets the mod paths.
/// </summary>
/// <value>The mod paths.</value>
public virtual IEnumerable<string> ModPaths { get; set; }

/// <summary>
/// Gets or sets the mods.
/// </summary>
Expand Down
Loading

0 comments on commit 40656e5

Please sign in to comment.