-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModFinder.cs
More file actions
120 lines (97 loc) · 5.07 KB
/
Copy pathModFinder.cs
File metadata and controls
120 lines (97 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Linq;
using AET.Modinfo.File;
using AET.Modinfo.Utilities;
using AnakinRaW.CommonUtilities.FileSystem;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PG.StarWarsGame.Infrastructure.Games;
using PG.StarWarsGame.Infrastructure.Services.Steam;
namespace PG.StarWarsGame.Infrastructure.Services.Detection;
internal class ModFinder(IServiceProvider serviceProvider) : IModFinder
{
private readonly ISteamGameHelpers _steamHelper = serviceProvider.GetRequiredService<ISteamGameHelpers>();
private readonly IModGameTypeResolver _gameTypeResolver = serviceProvider.GetRequiredService<IModGameTypeResolver>();
private readonly IFileSystem _fileSystem = serviceProvider.GetRequiredService<IFileSystem>();
private readonly ILogger? _logger = serviceProvider.GetService<ILoggerFactory>()?.CreateLogger(typeof(ModFinder));
public IEnumerable<DetectedModReference> FindMods(IGame game)
{
if (game == null)
throw new ArgumentNullException(nameof(game));
if (!game.Exists())
throw new GameException("The game does not exist");
_logger?.LogDebug("Searching mods for game '{Game}'", game);
return GetNormalMods(game).Union(GetWorkshopsMods(game));
}
public IEnumerable<DetectedModReference> FindMods(IGame game, IDirectoryInfo directory)
{
if (game == null)
throw new ArgumentNullException(nameof(game));
if (directory == null)
throw new ArgumentNullException(nameof(directory));
if (!game.Exists())
throw new GameException($"The game '{game}' does not exist");
var modLocationKind = GetModLocationKind(game, directory);
_logger?.LogTrace("Searching mods with at location '{Directory}' of location kind '{Kind}' for game '{Game}'", directory.FullName, modLocationKind, game);
return GetModsFromDirectory(directory, modLocationKind, game.Type);
}
private IEnumerable<DetectedModReference> GetNormalMods(IGame game)
{
_logger?.LogTrace("Searching normal mods for game '{Game}'", game);
return GetAllModsFromContainerPath(game.ModsLocation, ModReferenceBuilder.ModLocationKind.GameModsDirectory, game.Type);
}
private IEnumerable<DetectedModReference> GetWorkshopsMods(IGame game)
{
if (game.Platform != GamePlatform.SteamGold)
return [];
_logger?.LogTrace("Searching Steam Workshop mods for game '{Game}'", game);
return GetAllModsFromContainerPath(_steamHelper.GetWorkshopsLocation(game),
ModReferenceBuilder.ModLocationKind.SteamWorkshops, game.Type);
}
private IEnumerable<DetectedModReference> GetAllModsFromContainerPath(IDirectoryInfo lookupDirectory, ModReferenceBuilder.ModLocationKind locationKind, GameType requestedGameType)
{
if (!lookupDirectory.Exists)
return [];
return lookupDirectory.EnumerateDirectories()
.SelectMany(x => GetModsFromDirectory(x, locationKind, requestedGameType));
}
private IEnumerable<DetectedModReference> GetModsFromDirectory(
IDirectoryInfo modDirectory,
ModReferenceBuilder.ModLocationKind locationKind,
GameType requestedGameType)
{
if (!modDirectory.Exists)
yield break;
if (locationKind == ModReferenceBuilder.ModLocationKind.SteamWorkshops && !_steamHelper.ToSteamWorkshopsId(modDirectory.Name, out _))
yield break;
_logger?.LogTrace("Searching for mods at location '{Location}'", modDirectory.FullName);
var modinfoFiles = ModinfoFileFinder.FindModinfoFiles(modDirectory);
foreach (var modRef in ModReferenceBuilder.CreateIdentifiers(modinfoFiles, locationKind))
{
if (_gameTypeResolver.IsDefinitelyNotCompatibleToGame(modRef, requestedGameType))
{
_logger?.LogTrace("Skipping mod reference '{ModRef}' because it is not compatible to '{GameType}'", modRef.ModReference, requestedGameType);
continue;
}
yield return modRef;
}
}
private ModReferenceBuilder.ModLocationKind GetModLocationKind(IGame game, IDirectoryInfo directory)
{
var gameModsPath = game.ModsLocation.FullName;
var modPath = directory.FullName;
if (_fileSystem.Path.IsChildOf(gameModsPath, modPath))
return ModReferenceBuilder.ModLocationKind.GameModsDirectory;
if (game.Platform is not GamePlatform.SteamGold)
return ModReferenceBuilder.ModLocationKind.External;
if (!_steamHelper.ToSteamWorkshopsId(directory.Name, out _))
return ModReferenceBuilder.ModLocationKind.External;
if (!_steamHelper.TryGetWorkshopsLocation(game, out var steamWsDir))
return ModReferenceBuilder.ModLocationKind.External;
if (_fileSystem.Path.IsChildOf(steamWsDir.FullName, modPath))
return ModReferenceBuilder.ModLocationKind.SteamWorkshops;
return ModReferenceBuilder.ModLocationKind.External;
}
}