Skip to content

Commit 0887111

Browse files
committed
fix and test automatic selector
1 parent 0a674e6 commit 0887111

11 files changed

Lines changed: 678 additions & 222 deletions

File tree

src/ModVerify.CliApp/GameFinder/GameFinderService.cs

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Globalization;
45
using System.IO.Abstractions;
56
using Microsoft.Extensions.DependencyInjection;
@@ -51,7 +52,7 @@ public GameFinderResult FindGames(GameFinderSettings settings)
5152

5253
return FindGames(detectors, settings);
5354
}
54-
55+
5556
public IGame FindGame(string gamePath, GameFinderSettings settings)
5657
{
5758
var detectors = new List<IGameDetector>
@@ -61,6 +62,25 @@ public IGame FindGame(string gamePath, GameFinderSettings settings)
6162
return FindGames(detectors, settings).Game;
6263
}
6364

65+
public bool TryFindGame(string gamePath, GameFinderSettings settings, [NotNullWhen(true)]out IGame? game)
66+
{
67+
var detectors = new List<IGameDetector>
68+
{
69+
new DirectoryGameDetector(_fileSystem.DirectoryInfo.New(gamePath), _serviceProvider),
70+
};
71+
72+
try
73+
{
74+
game = FindGames(detectors, settings).Game;
75+
return true;
76+
}
77+
catch (GameNotFoundException)
78+
{
79+
game = null;
80+
return false;
81+
}
82+
}
83+
6484
public GameFinderResult FindGamesFromPathOrGlobal(string path, GameFinderSettings settings)
6585
{
6686
// There are four common situations:
@@ -88,55 +108,79 @@ public GameFinderResult FindGamesFromPathOrGlobal(string path, GameFinderSetting
88108

89109
private GameFinderResult FindGames(IList<IGameDetector> detectors, GameFinderSettings settings)
90110
{
91-
// FoC needs to be tried first
92-
if (!TryDetectGame(GameType.Foc, detectors, out var result))
111+
GameDetectionResult? detectionResult = null;
112+
if (settings.Engine is GameEngineType.Eaw)
113+
{
114+
_logger?.LogTrace("Trying to find requested EaW installation.");
115+
if (!TryDetectGame(GameType.Eaw, detectors, out detectionResult))
116+
{
117+
var e = new GameNotFoundException($"Unable to find requested game installation '{settings.Engine}'. Wrong install path?");
118+
_logger?.LogTrace(e, e.Message);
119+
throw e;
120+
}
121+
}
122+
123+
if (detectionResult is null && !TryDetectGame(GameType.Foc, detectors, out detectionResult))
93124
{
125+
if (settings.Engine is GameEngineType.Foc)
126+
{
127+
var e = new GameNotFoundException($"Unable to find requested game installation '{settings.Engine}'. Wrong install path?");
128+
_logger?.LogTrace(e, e.Message);
129+
throw e;
130+
}
131+
132+
// If the engine is unspecified, we also need to check for EaW.
94133
_logger?.LogTrace("Unable to find FoC installation. Trying again with EaW...");
95-
if (!TryDetectGame(GameType.Eaw, detectors, out result))
134+
if (!TryDetectGame(GameType.Eaw, detectors, out detectionResult))
96135
throw new GameNotFoundException("Unable to find game installation: Wrong install path?");
97136
}
98137

99-
if (result.GameLocation is null)
138+
if (detectionResult.GameLocation is null)
100139
throw new GameNotFoundException("Unable to find game installation: Wrong install path?");
101140

102141
_logger?.LogInformation(ModVerifyConstants.ConsoleEventId,
103-
"Found game installation: {ResultGameIdentity} at {GameLocationFullName}", result.GameIdentity, result.GameLocation.FullName);
142+
"Found game installation: {ResultGameIdentity} at {GameLocationFullName}", detectionResult.GameIdentity, detectionResult.GameLocation.FullName);
104143

105-
var game = _gameFactory.CreateGame(result, CultureInfo.InvariantCulture);
144+
var game = _gameFactory.CreateGame(detectionResult, CultureInfo.InvariantCulture);
106145

107146
if (settings.InitMods)
108147
SetupMods(game);
109148

110149

111150
IGame? fallbackGame = null;
112-
if (settings.SearchFallbackGame)
151+
if (SearchForFallbackGame(settings, detectionResult))
113152
{
114-
// If the game is Foc we want to set up Eaw as well as the fallbackGame
115-
if (game.Type == GameType.Foc)
116-
{
117-
var fallbackDetectors = new List<IGameDetector>();
153+
var fallbackDetectors = new List<IGameDetector>();
118154

119-
if (game.Platform == GamePlatform.SteamGold)
120-
fallbackDetectors.Add(new SteamPetroglyphStarWarsGameDetector(_serviceProvider));
121-
else
122-
throw new NotImplementedException("Searching fallback game for non-Steam games is currently is not yet implemented.");
155+
if (game.Platform == GamePlatform.SteamGold)
156+
fallbackDetectors.Add(new SteamPetroglyphStarWarsGameDetector(_serviceProvider));
157+
else
158+
throw new NotImplementedException("Searching fallback game for non-Steam games is currently is not yet implemented.");
123159

124-
if (!TryDetectGame(GameType.Eaw, fallbackDetectors, out var fallbackResult) || fallbackResult.GameLocation is null)
125-
throw new GameNotFoundException("Unable to find fallback game installation: Wrong install path?");
160+
if (!TryDetectGame(GameType.Eaw, fallbackDetectors, out var fallbackResult) || fallbackResult.GameLocation is null)
161+
throw new GameNotFoundException("Unable to find fallback game installation: Wrong install path?");
126162

127-
_logger?.LogInformation(ModVerifyConstants.ConsoleEventId,
128-
"Found fallback game installation: {FallbackResultGameIdentity} at {GameLocationFullName}", fallbackResult.GameIdentity, fallbackResult.GameLocation.FullName);
163+
_logger?.LogInformation(ModVerifyConstants.ConsoleEventId,
164+
"Found fallback game installation: {FallbackResultGameIdentity} at {GameLocationFullName}", fallbackResult.GameIdentity, fallbackResult.GameLocation.FullName);
129165

130-
fallbackGame = _gameFactory.CreateGame(fallbackResult, CultureInfo.InvariantCulture);
166+
fallbackGame = _gameFactory.CreateGame(fallbackResult, CultureInfo.InvariantCulture);
131167

132-
if (settings.InitMods)
133-
SetupMods(fallbackGame);
134-
}
168+
if (settings.InitMods)
169+
SetupMods(fallbackGame);
135170
}
136171

137172
return new GameFinderResult(game, fallbackGame);
138173
}
139174

175+
private static bool SearchForFallbackGame(GameFinderSettings settings, GameDetectionResult? foundGame)
176+
{
177+
if (settings.Engine is GameEngineType.Eaw)
178+
return false;
179+
if (foundGame is { Installed: true, GameIdentity.Type: GameType.Eaw })
180+
return false;
181+
return settings.SearchFallbackGame;
182+
}
183+
140184
private bool TryDetectGame(GameType gameType, IList<IGameDetector> detectors, out GameDetectionResult result)
141185
{
142186
var gd = new CompositeGameDetector(detectors, _serviceProvider);

src/ModVerify.CliApp/ModVerifyApplication.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ private async Task<int> RunVerify()
7676
.CreateSelector(targetSettings)
7777
.Select(targetSettings);
7878
}
79+
catch (ArgumentException ex)
80+
{
81+
ConsoleUtilities.WriteApplicationFatalError(_appEnvironment.ApplicationName,
82+
$"The specified arguments are not correct: {ex.Message}");
83+
_logger?.LogError(ex, "Invalid application arguments: {Message}", ex.Message);
84+
return ex.HResult;
85+
}
86+
catch (TargetNotFoundException ex)
87+
{
88+
ConsoleUtilities.WriteApplicationFatalError(_appEnvironment.ApplicationName, ex.Message);
89+
_logger?.LogError(ex, ex.Message);
90+
return ex.HResult;
91+
}
7992
catch (GameNotFoundException ex)
8093
{
8194
ConsoleUtilities.WriteApplicationFatalError(_appEnvironment.ApplicationName,

src/ModVerify.CliApp/Settings/VerificationTargetSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal sealed record VerificationTargetSettings
99
public bool Interactive => string.IsNullOrEmpty(TargetPath) && ModPaths.Count == 0 && string.IsNullOrEmpty(GamePath) && string.IsNullOrEmpty(FallbackGamePath);
1010

1111
[MemberNotNullWhen(true, nameof(TargetPath))]
12-
public bool UseAutoDetection => !string.IsNullOrEmpty(TargetPath);
12+
public bool UseAutoDetection => !string.IsNullOrEmpty(TargetPath) && ModPaths.Count == 0 && string.IsNullOrEmpty(GamePath) && string.IsNullOrEmpty(FallbackGamePath);
1313

1414
[MemberNotNullWhen(true, nameof(GamePath))]
1515
public bool ManualSetup => !string.IsNullOrEmpty(GamePath);

0 commit comments

Comments
 (0)