11using System ;
22using System . Collections . Generic ;
3+ using System . Globalization ;
34using System . IO . Abstractions ;
45using Microsoft . Extensions . DependencyInjection ;
56using Microsoft . Extensions . Logging ;
1011using PG . StarWarsGame . Infrastructure . Services . Dependencies ;
1112using PG . StarWarsGame . Infrastructure . Services . Detection ;
1213
13- namespace ModVerify . CliApp ;
14+ namespace AET . ModVerifyTool . GameFinder ;
1415
1516internal class GameFinderService
1617{
@@ -40,33 +41,34 @@ public GameFinderResult FindGames()
4041 return FindGames ( detectors ) ;
4142 }
4243
43- public GameFinderResult FindGamesFromPath ( string path )
44+ public GameFinderResult FindGamesFromPathOrGlobal ( string path )
4445 {
45- // There are three common situations:
46+ // There are four common situations:
4647 // 1. path points to the actual game directory
4748 // 2. path points to a local mod in game/Mods/ModDir
4849 // 3. path points to a workshop mod
50+ // 4. path points to a "detached mod" at a completely different location
4951 var givenDirectory = _fileSystem . DirectoryInfo . New ( path ) ;
5052 var possibleGameDir = givenDirectory . Parent ? . Parent ;
51- var possibleSteamAppsFolder = givenDirectory . Parent ? . Parent ? . Parent ? . Parent ? . Parent ;
5253
5354 var detectors = new List < IGameDetector >
5455 {
56+ // Case 1
5557 new DirectoryGameDetector ( givenDirectory , _serviceProvider )
5658 } ;
5759
60+ // Case 2
5861 if ( possibleGameDir is not null )
5962 detectors . Add ( new DirectoryGameDetector ( possibleGameDir , _serviceProvider ) ) ;
6063
61- if ( possibleSteamAppsFolder is not null && possibleSteamAppsFolder . Name == "steamapps" && uint . TryParse ( givenDirectory . Name , out _ ) )
62- detectors . Add ( new SteamPetroglyphStarWarsGameDetector ( _serviceProvider ) ) ;
63-
64+ // Cases 3 & 4
65+ detectors . Add ( new SteamPetroglyphStarWarsGameDetector ( _serviceProvider ) ) ;
6466 return FindGames ( detectors ) ;
6567 }
6668
6769 private bool TryDetectGame ( GameType gameType , IList < IGameDetector > detectors , out GameDetectionResult result )
6870 {
69- var gd = new CompositeGameDetector ( detectors , _serviceProvider , true ) ;
71+ var gd = new CompositeGameDetector ( detectors , _serviceProvider ) ;
7072 result = gd . Detect ( new GameDetectorOptions ( gameType ) ) ;
7173
7274 if ( result . Error is not null )
@@ -80,48 +82,22 @@ private bool TryDetectGame(GameType gameType, IList<IGameDetector> detectors, ou
8082 return true ;
8183 }
8284
83-
84- private void SetupMods ( IGame game )
85- {
86- var modFinder = _serviceProvider . GetRequiredService < IModReferenceFinder > ( ) ;
87- var modRefs = modFinder . FindMods ( game ) ;
88-
89- var mods = new List < IMod > ( ) ;
90-
91- foreach ( var modReference in modRefs )
92- {
93- var mod = _modFactory . FromReference ( game , modReference ) ;
94- mods . AddRange ( mod ) ;
95- }
96-
97- foreach ( var mod in mods )
98- game . AddMod ( mod ) ;
99-
100- // Mods need to be added to the game first, before resolving their dependencies.
101- foreach ( var mod in mods )
102- {
103- var resolver = _serviceProvider . GetRequiredService < IDependencyResolver > ( ) ;
104- mod . ResolveDependencies ( resolver ,
105- new DependencyResolverOptions { CheckForCycle = true , ResolveCompleteChain = true } ) ;
106- }
107- }
108-
10985 private GameFinderResult FindGames ( IList < IGameDetector > detectors )
11086 {
11187 // FoC needs to be tried first
11288 if ( ! TryDetectGame ( GameType . Foc , detectors , out var result ) )
11389 {
11490 _logger ? . LogTrace ( "Unable to find FoC installation. Trying again with EaW..." ) ;
115- if ( ! TryDetectGame ( GameType . EaW , detectors , out result ) )
116- throw new GameException ( "Unable to find game installation: Wrong install path?" ) ;
91+ if ( ! TryDetectGame ( GameType . Eaw , detectors , out result ) )
92+ throw new GameNotFoundException ( "Unable to find game installation: Wrong install path?" ) ;
11793 }
11894
11995 if ( result . GameLocation is null )
120- throw new GameException ( "Unable to find game installation: Wrong install path?" ) ;
96+ throw new GameNotFoundException ( "Unable to find game installation: Wrong install path?" ) ;
12197
12298 _logger ? . LogTrace ( $ "Found game installation: { result . GameIdentity } at { result . GameLocation . FullName } ") ;
12399
124- var game = _gameFactory . CreateGame ( result ) ;
100+ var game = _gameFactory . CreateGame ( result , CultureInfo . InvariantCulture ) ;
125101
126102 SetupMods ( game ) ;
127103
@@ -137,16 +113,41 @@ private GameFinderResult FindGames(IList<IGameDetector> detectors)
137113 else
138114 throw new NotImplementedException ( "Searching fallback game for non-Steam games is currently is not yet implemented." ) ;
139115
140- if ( ! TryDetectGame ( GameType . EaW , fallbackDetectors , out var fallbackResult ) || fallbackResult . GameLocation is null )
141- throw new GameException ( "Unable to find fallback game installation: Wrong install path?" ) ;
116+ if ( ! TryDetectGame ( GameType . Eaw , fallbackDetectors , out var fallbackResult ) || fallbackResult . GameLocation is null )
117+ throw new GameNotFoundException ( "Unable to find fallback game installation: Wrong install path?" ) ;
142118
143119 _logger ? . LogTrace ( $ "Found fallback game installation: { fallbackResult . GameIdentity } at { fallbackResult . GameLocation . FullName } ") ;
144120
145- fallbackGame = _gameFactory . CreateGame ( fallbackResult ) ;
121+ fallbackGame = _gameFactory . CreateGame ( fallbackResult , CultureInfo . InvariantCulture ) ;
146122
147123 SetupMods ( fallbackGame ) ;
148124 }
149125
150126 return new GameFinderResult ( game , fallbackGame ) ;
151127 }
128+
129+ private void SetupMods ( IGame game )
130+ {
131+ var modFinder = _serviceProvider . GetRequiredService < IModReferenceFinder > ( ) ;
132+ var modRefs = modFinder . FindMods ( game ) ;
133+
134+ var mods = new List < IMod > ( ) ;
135+
136+ foreach ( var modReference in modRefs )
137+ {
138+ var mod = _modFactory . FromReference ( game , modReference , CultureInfo . InvariantCulture ) ;
139+ mods . AddRange ( mod ) ;
140+ }
141+
142+ foreach ( var mod in mods )
143+ game . AddMod ( mod ) ;
144+
145+ // Mods need to be added to the game first, before resolving their dependencies.
146+ foreach ( var mod in mods )
147+ {
148+ var resolver = _serviceProvider . GetRequiredService < IDependencyResolver > ( ) ;
149+ mod . ResolveDependencies ( resolver ,
150+ new DependencyResolverOptions { CheckForCycle = true , ResolveCompleteChain = true } ) ;
151+ }
152+ }
152153}
0 commit comments