1616
1717namespace AET . ModVerify . App . ModSelectors ;
1818
19- internal class AutomaticModSelector ( IServiceProvider serviceProvider ) : ModSelectorBase ( serviceProvider )
19+ internal class AutomaticSelector ( IServiceProvider serviceProvider ) : VerificationTargetSelectorBase ( serviceProvider )
2020{
2121 private readonly IFileSystem _fileSystem = serviceProvider . GetRequiredService < IFileSystem > ( ) ;
2222
23- public override GameLocations ? Select (
24- GameInstallationsSettings settings ,
25- out IPhysicalPlayableObject ? targetObject ,
26- out GameEngineType ? actualEngineType )
23+ public override VerificationTarget Select ( GameInstallationsSettings settings )
2724 {
28- var pathToVerify = settings . AutoPath ;
29- if ( pathToVerify is null )
25+ var targetPath = settings . AutoPath ;
26+ if ( targetPath is null )
3027 throw new InvalidOperationException ( "path to verify cannot be null." ) ;
3128
32- actualEngineType = settings . EngineType ;
29+ var engine = settings . Engine ;
3330
3431 GameFinderResult finderResult ;
3532 try
3633 {
37- finderResult = GameFinderService . FindGamesFromPathOrGlobal ( pathToVerify ) ;
34+ finderResult = GameFinderService . FindGamesFromPathOrGlobal ( targetPath , GameFinderSettings . Default ) ;
3835 }
3936 catch ( GameNotFoundException )
4037 {
41- Logger ? . LogError ( ModVerifyConstants . ConsoleEventId , "Unable to find games based of the given location '{SettingsGamePath}'. Consider specifying all paths manually." , settings . GamePath ) ;
42- targetObject = null ;
43- return null ;
38+ Logger ? . LogError ( ModVerifyConstants . ConsoleEventId , "Unable to find games based of the specified target path '{Path}'. Consider specifying all paths manually." , settings . GamePath ) ;
39+ throw ;
4440 }
4541
4642
47- var modOrGame = GetAttachedModOrGame ( finderResult , actualEngineType , pathToVerify ) ;
43+ GameLocations locations ;
44+
45+ var targetObject = GetAttachedModOrGame ( finderResult , engine , targetPath ) ;
4846
49- if ( modOrGame is not null )
47+
48+ if ( targetObject is null )
5049 {
51- var actualType = modOrGame . Game . Type . ToEngineType ( ) ;
52- actualEngineType ??= actualType ;
53- if ( actualEngineType != actualType )
54- throw new ArgumentException ( $ "The specified game type '{ actualEngineType } ' does not match the actual type of the game or mod to verify.") ;
50+ if ( ! settings . Engine . HasValue )
51+ throw new ArgumentException ( "Game engine not specified. Use --engine argument to set it." ) ;
5552
56- targetObject = modOrGame ;
57- return GetLocations ( targetObject , finderResult , settings . AdditionalFallbackPaths ) ;
58- }
53+ Logger ? . LogDebug ( "The requested mod at '{TargetPath}' is detached from its games." , targetPath ) ;
5954
60- if ( ! settings . EngineType . HasValue )
61- throw new ArgumentException ( "Unable to determine game type. Use --type argument to set the game type." ) ;
55+ // The path is a detached mod, that exists on a different location than the game.
56+ locations = GetDetachedModLocations ( targetPath , finderResult , settings , out var mod ) ;
57+ targetObject = mod ;
58+ }
59+ else
60+ {
61+ var actualType = targetObject . Game . Type . ToEngineType ( ) ;
62+ engine ??= actualType ;
63+ if ( engine != actualType )
64+ throw new ArgumentException ( $ "The specified game type '{ engine } ' does not match the actual type of the game or mod to verify.") ;
65+ locations = GetLocations ( targetObject , finderResult . FallbackGame , settings . AdditionalFallbackPaths ) ;
66+ }
6267
63- Logger ? . LogDebug ( "The requested mod at '{PathToVerify}' is detached from its games." , pathToVerify ) ;
68+ return new VerificationTarget
69+ {
70+ Engine = engine . Value ,
71+ Location = locations ,
72+ Name = GetTargetName ( targetObject , locations ) ,
73+ Version = GetTargetVersion ( targetObject )
74+ } ;
6475
65- // The path is a detached mod, that exists on a different location than the game.
66- var result = GetDetachedModLocations ( pathToVerify , finderResult , settings , out var mod ) ;
67- targetObject = mod ;
68- return result ;
6976 }
7077
71- private IPhysicalPlayableObject ? GetAttachedModOrGame ( GameFinderResult finderResult , GameEngineType ? requestedEngineType , string searchPath )
78+ private IPhysicalPlayableObject ? GetAttachedModOrGame ( GameFinderResult finderResult , GameEngineType ? requestedEngineType , string targetPath )
7279 {
73- var fullSearchPath = _fileSystem . Path . GetFullPath ( searchPath ) ;
80+ var targetFullPath = _fileSystem . Path . GetFullPath ( targetPath ) ;
7481
75- if ( finderResult . Game . Directory . FullName . Equals ( fullSearchPath , StringComparison . OrdinalIgnoreCase ) )
82+ // If the target is the game directory itself.
83+ if ( targetFullPath . Equals ( finderResult . Game . Directory . FullName , StringComparison . OrdinalIgnoreCase ) )
7684 {
7785 if ( finderResult . Game . Type . ToEngineType ( ) != requestedEngineType )
78- throw new ArgumentException ( $ "The specified game type '{ requestedEngineType } ' does not match the actual type of the game '{ searchPath } ' to verify.") ;
86+ throw new ArgumentException ( $ "The specified game type '{ requestedEngineType } ' does not match the actual type of the game '{ targetPath } ' to verify.") ;
7987 return finderResult . Game ;
8088 }
8189
8290 if ( finderResult . FallbackGame is not null &&
83- finderResult . FallbackGame . Directory . FullName . Equals ( fullSearchPath , StringComparison . OrdinalIgnoreCase ) )
91+ targetFullPath . Equals ( finderResult . FallbackGame . Directory . FullName , StringComparison . OrdinalIgnoreCase ) )
8492 {
93+ throw new NotImplementedException ( "When does this actually happen???" ) ;
94+
8595 if ( finderResult . FallbackGame . Type . ToEngineType ( ) != requestedEngineType )
86- throw new ArgumentException ( $ "The specified game type '{ requestedEngineType } ' does not match the actual type of the game '{ searchPath } ' to verify.") ;
96+ throw new ArgumentException ( $ "The specified game type '{ requestedEngineType } ' does not match the actual type of the game '{ targetPath } ' to verify.") ;
8797 return finderResult . FallbackGame ;
8898 }
8999
90- return GetMatchingModFromGame ( finderResult . Game , requestedEngineType , fullSearchPath ) ??
91- GetMatchingModFromGame ( finderResult . FallbackGame , requestedEngineType , fullSearchPath ) ;
100+ return GetMatchingModFromGame ( finderResult . Game , requestedEngineType , targetFullPath ) ??
101+ GetMatchingModFromGame ( finderResult . FallbackGame , requestedEngineType , targetFullPath ) ;
92102 }
93103
94104 private GameLocations GetDetachedModLocations ( string modPath , GameFinderResult gameResult , GameInstallationsSettings settings , out IPhysicalMod mod )
95105 {
96106 IGame game = null ! ;
97107
98- if ( gameResult . Game . Type . ToEngineType ( ) == settings . EngineType )
108+ if ( gameResult . Game . Type . ToEngineType ( ) == settings . Engine )
99109 game = gameResult . Game ;
100- if ( gameResult . FallbackGame is not null && gameResult . FallbackGame . Type . ToEngineType ( ) == settings . EngineType )
110+ if ( gameResult . FallbackGame is not null && gameResult . FallbackGame . Type . ToEngineType ( ) == settings . Engine )
101111 game = gameResult . FallbackGame ;
102112
103113 if ( game is null )
104- throw new GameNotFoundException ( $ "Unable to find game of type '{ settings . EngineType } '") ;
114+ throw new GameNotFoundException ( $ "Unable to find game of type '{ settings . Engine } '") ;
105115
106116 var modFinder = ServiceProvider . GetRequiredService < IModFinder > ( ) ;
107117 var modRef = modFinder . FindMods ( game , _fileSystem . DirectoryInfo . New ( modPath ) ) . FirstOrDefault ( ) ;
@@ -116,7 +126,7 @@ private GameLocations GetDetachedModLocations(string modPath, GameFinderResult g
116126
117127 mod . ResolveDependencies ( ) ;
118128
119- return GetLocations ( mod , gameResult , settings . AdditionalFallbackPaths ) ;
129+ return GetLocations ( mod , gameResult . FallbackGame , settings . AdditionalFallbackPaths ) ;
120130 }
121131
122132 private static IPhysicalMod ? GetMatchingModFromGame ( IGame ? game , GameEngineType ? requestedEngineType , string modPath )
@@ -132,7 +142,7 @@ private GameLocations GetDetachedModLocations(string modPath, GameFinderResult g
132142 if ( physicalMod . Directory . FullName . Equals ( modPath , StringComparison . OrdinalIgnoreCase ) )
133143 {
134144 if ( ! isGameSupported )
135- throw new ArgumentException ( $ "The specified game type '{ requestedEngineType } ' does not match the actual type of the mod '{ modPath } ' to verify.") ;
145+ throw new ArgumentException ( $ "The specified engine type '{ requestedEngineType } ' does not match the required of the mod '{ modPath } ' to verify.") ;
136146 return physicalMod ;
137147 }
138148 }
0 commit comments