diff --git a/src/Games/NexusMods.Games.UnrealEngine/Installers/SmartUEInstaller.cs b/src/Games/NexusMods.Games.UnrealEngine/Installers/SmartUEInstaller.cs index 20afd47f16..0ee169bb1a 100644 --- a/src/Games/NexusMods.Games.UnrealEngine/Installers/SmartUEInstaller.cs +++ b/src/Games/NexusMods.Games.UnrealEngine/Installers/SmartUEInstaller.cs @@ -1,11 +1,16 @@ using System.Diagnostics.CodeAnalysis; using Microsoft.Extensions.Logging; +using NexusMods.Abstractions.DiskState; using NexusMods.Abstractions.FileStore.Trees; using NexusMods.Abstractions.GameLocators; using NexusMods.Abstractions.Installers; using NexusMods.Abstractions.Loadouts; +using NexusMods.Abstractions.Loadouts.Extensions; using NexusMods.Abstractions.Loadouts.Files; +using NexusMods.Abstractions.Loadouts.Mods; using NexusMods.Abstractions.Loadouts.Synchronizers; +using File = NexusMods.Abstractions.Loadouts.Files.File; +using NexusMods.MnemonicDB.Abstractions; using NexusMods.MnemonicDB.Abstractions.Models; using NexusMods.Paths; using NexusMods.Paths.Extensions; @@ -19,10 +24,14 @@ namespace NexusMods.Games.UnrealEngine.Installers; public class SmartUEInstaller : AModInstaller { private readonly ILogger _logger; + private readonly IConnection _connection; + private readonly IDiskStateRegistry _diskStateRegistry; - public SmartUEInstaller(ILogger logger, IServiceProvider serviceProvider) : base(serviceProvider) + public SmartUEInstaller(ILogger logger, IConnection connection, IDiskStateRegistry diskStateRegistry, IServiceProvider serviceProvider) : base(serviceProvider) { _logger = logger; + _connection = connection; + _diskStateRegistry = diskStateRegistry; } public override async ValueTask> GetModsAsync( @@ -39,40 +48,67 @@ public override async ValueTask> GetModsAsync( var achiveFiles = info.ArchiveFiles.GetFiles(); - var gameDLLs = gameFolderPath.EnumerateFiles("*.dll").ToLookup(el => el.FileName.ToRelativePath()); // TODO: [1] - if (achiveFiles.Length == 0) { _logger.LogError("Archive contains 0 files"); return NoResults; } + var loadout = Loadout.All(_connection.Db) + .First(x => x.IsVisible() && x.Installation.Path == gameFolderPath.ToString()); + + var gameFilesLookup = loadout.Mods.First(mod => mod.Category == ModCategory.GameFiles).Files.Select(file => file.To).ToLookup(x => x.Path.FileName); + var modFiles = achiveFiles.Select(kv => { switch (kv.Path().Extension) { case Extension ext when Constants.ContentExts.Contains(ext): - return kv.ToStoredFile( - new GamePath(Constants.GameMainUE, Constants.PakModsPath.Join(kv.FileName())) - ); - case Extension ext when ext == Constants.DLLExt: - var matchesGameDlls = gameDLLs[kv.FileName()]; - if (matchesGameDlls.Any()) // if DLL exists in game dir replace it { - var matchedDll = matchesGameDlls.First(); - var (location, relativePath) = matchedDll.InFolder(gameMainUEFolderPath) ? (Constants.GameMainUE, matchedDll.RelativeTo(gameMainUEFolderPath)) - : (LocationId.Game, matchedDll.RelativeTo(gameFolderPath)); - return kv.ToStoredFile( - new GamePath(location, relativePath) + var matchesGameContentFles = gameFilesLookup[kv.FileName()]; + if (matchesGameContentFles.Any()) // if Content file exists in game dir replace it + { + var matchedContentFIle = matchesGameContentFles.First(); + return kv.ToStoredFile( + matchedContentFIle + ); + } + else + return kv.ToStoredFile( + new GamePath(Constants.GameMainUE, Constants.PakModsPath.Join(kv.FileName())) ); } - else - return kv.ToStoredFile( - new GamePath(LocationId.Game, Constants.InjectorModsPath.Join(kv.FileName())) - ); + case Extension ext when ext == Constants.DLLExt: + { + var matchesGameDlls = gameFilesLookup[kv.FileName()]; + if (matchesGameDlls.Any()) // if DLL exists in game dir replace it + { + var matchedDll = matchesGameDlls.First(); + return kv.ToStoredFile( + matchedDll + ); + } + else + return kv.ToStoredFile( + new GamePath(Constants.GameMainUE, Constants.InjectorModsPath.Join(kv.FileName())) + ); + } default: - _logger.LogWarning("File {} is of unrecognized type {}, skipping", kv.Path().FileName, kv.Path().Extension); - return null; + { + var matchesGameFles = gameFilesLookup[kv.FileName()]; + if (matchesGameFles.Any()) // if File exists in game dir replace it + { + var matchedFile = matchesGameFles.First(); + return kv.ToStoredFile( + matchedFile + ); + } + else + { + _logger.LogWarning("File {} is of unrecognized type {}, skipping", kv.Path().FileName, kv.Path().Extension); + return null; + } + } } }).OfType().ToArray(); diff --git a/src/Games/NexusMods.Games.UnrealEngine/NexusMods.Games.UnrealEngine.csproj b/src/Games/NexusMods.Games.UnrealEngine/NexusMods.Games.UnrealEngine.csproj index 25b3104fda..fcd504e1f1 100644 --- a/src/Games/NexusMods.Games.UnrealEngine/NexusMods.Games.UnrealEngine.csproj +++ b/src/Games/NexusMods.Games.UnrealEngine/NexusMods.Games.UnrealEngine.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -18,7 +18,9 @@ + +