11using System ;
22using System . Collections . Generic ;
3+ using System . Diagnostics . CodeAnalysis ;
34using System . Globalization ;
45using System . IO . Abstractions ;
56using 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 ) ;
0 commit comments