Skip to content

Commit

Permalink
[UnrealEngine][SmartUEInstaller] use loadout to look for files
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeeWanderer committed Jul 10, 2024
1 parent dc067e8 commit 22ecff3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,10 +24,14 @@ namespace NexusMods.Games.UnrealEngine.Installers;
public class SmartUEInstaller : AModInstaller
{
private readonly ILogger<SmartUEInstaller> _logger;
private readonly IConnection _connection;
private readonly IDiskStateRegistry _diskStateRegistry;

public SmartUEInstaller(ILogger<SmartUEInstaller> logger, IServiceProvider serviceProvider) : base(serviceProvider)
public SmartUEInstaller(ILogger<SmartUEInstaller> logger, IConnection connection, IDiskStateRegistry diskStateRegistry, IServiceProvider serviceProvider) : base(serviceProvider)
{
_logger = logger;
_connection = connection;
_diskStateRegistry = diskStateRegistry;
}

public override async ValueTask<IEnumerable<ModInstallerResult>> GetModsAsync(
Expand All @@ -39,40 +48,67 @@ public override async ValueTask<IEnumerable<ModInstallerResult>> 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<TempEntity>().ToArray();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -18,7 +18,9 @@

<ItemGroup>
<ProjectReference Include="..\..\Abstractions\NexusMods.Abstractions.Games\NexusMods.Abstractions.Games.csproj" />
<ProjectReference Include="..\..\Abstractions\NexusMods.Abstractions.Loadouts\NexusMods.Abstractions.Loadouts.csproj" />
<ProjectReference Include="..\..\Extensions\NexusMods.Extensions.DependencyInjection\NexusMods.Extensions.DependencyInjection.csproj" />
<PackageReference Include="NexusMods.MnemonicDB.SourceGenerator" PrivateAssets="all" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>

0 comments on commit 22ecff3

Please sign in to comment.