Skip to content

Commit

Permalink
reorganize cli handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AnakinRaW committed Aug 3, 2024
1 parent 34b075d commit bae82ea
Show file tree
Hide file tree
Showing 29 changed files with 715 additions and 319 deletions.
17 changes: 17 additions & 0 deletions src/ModVerify.CliApp/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using PG.StarWarsGame.Engine;
using PG.StarWarsGame.Infrastructure.Games;

namespace ModVerify.CliApp.ModSelectors;

internal static class ExtensionMethods
{
public static GameEngineType ToEngineType(this GameType type)
{
return type == GameType.Foc ? GameEngineType.Foc : GameEngineType.Eaw;
}

public static GameType FromEngineType(this GameEngineType type)
{
return type == GameEngineType.Foc ? GameType.Foc : GameType.EaW;
}
}
70 changes: 35 additions & 35 deletions src/ModVerify.CliApp/GameFinderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,28 @@ public GameFinderResult FindGames()
return FindGames(detectors);
}

public GameFinderResult FindGamesFromPath(string path)
public GameFinderResult FindGamesFromPathOrGlobal(string path)
{
// There are three common situations:
// There are four common situations:
// 1. path points to the actual game directory
// 2. path points to a local mod in game/Mods/ModDir
// 3. path points to a workshop mod
// 4. path points to a "detached mod" at a completely different location
var givenDirectory = _fileSystem.DirectoryInfo.New(path);
var possibleGameDir = givenDirectory.Parent?.Parent;
var possibleSteamAppsFolder = givenDirectory.Parent?.Parent?.Parent?.Parent?.Parent;

var detectors = new List<IGameDetector>
{
// Case 1
new DirectoryGameDetector(givenDirectory, _serviceProvider)
};

// Case 2
if (possibleGameDir is not null)
detectors.Add(new DirectoryGameDetector(possibleGameDir, _serviceProvider));

if (possibleSteamAppsFolder is not null && possibleSteamAppsFolder.Name == "steamapps" && uint.TryParse(givenDirectory.Name, out _))
detectors.Add(new SteamPetroglyphStarWarsGameDetector(_serviceProvider));

// Cases 3 & 4
detectors.Add(new SteamPetroglyphStarWarsGameDetector(_serviceProvider));
return FindGames(detectors);
}

Expand All @@ -80,44 +81,18 @@ private bool TryDetectGame(GameType gameType, IList<IGameDetector> detectors, ou
return true;
}


private void SetupMods(IGame game)
{
var modFinder = _serviceProvider.GetRequiredService<IModReferenceFinder>();
var modRefs = modFinder.FindMods(game);

var mods = new List<IMod>();

foreach (var modReference in modRefs)
{
var mod = _modFactory.FromReference(game, modReference);
mods.AddRange(mod);
}

foreach (var mod in mods)
game.AddMod(mod);

// Mods need to be added to the game first, before resolving their dependencies.
foreach (var mod in mods)
{
var resolver = _serviceProvider.GetRequiredService<IDependencyResolver>();
mod.ResolveDependencies(resolver,
new DependencyResolverOptions { CheckForCycle = true, ResolveCompleteChain = true });
}
}

private GameFinderResult FindGames(IList<IGameDetector> detectors)
{
// FoC needs to be tried first
if (!TryDetectGame(GameType.Foc, detectors, out var result))
{
_logger?.LogTrace("Unable to find FoC installation. Trying again with EaW...");
if (!TryDetectGame(GameType.EaW, detectors, out result))
throw new GameException("Unable to find game installation: Wrong install path?");
throw new GameNotFoundException("Unable to find game installation: Wrong install path?");
}

if (result.GameLocation is null)
throw new GameException("Unable to find game installation: Wrong install path?");
throw new GameNotFoundException("Unable to find game installation: Wrong install path?");

_logger?.LogTrace($"Found game installation: {result.GameIdentity} at {result.GameLocation.FullName}");

Expand All @@ -138,7 +113,7 @@ private GameFinderResult FindGames(IList<IGameDetector> detectors)
throw new NotImplementedException("Searching fallback game for non-Steam games is currently is not yet implemented.");

if (!TryDetectGame(GameType.EaW, fallbackDetectors, out var fallbackResult) || fallbackResult.GameLocation is null)
throw new GameException("Unable to find fallback game installation: Wrong install path?");
throw new GameNotFoundException("Unable to find fallback game installation: Wrong install path?");

_logger?.LogTrace($"Found fallback game installation: {fallbackResult.GameIdentity} at {fallbackResult.GameLocation.FullName}");

Expand All @@ -149,4 +124,29 @@ private GameFinderResult FindGames(IList<IGameDetector> detectors)

return new GameFinderResult(game, fallbackGame);
}

private void SetupMods(IGame game)
{
var modFinder = _serviceProvider.GetRequiredService<IModReferenceFinder>();
var modRefs = modFinder.FindMods(game);

var mods = new List<IMod>();

foreach (var modReference in modRefs)
{
var mod = _modFactory.FromReference(game, modReference);
mods.AddRange(mod);
}

foreach (var mod in mods)
game.AddMod(mod);

// Mods need to be added to the game first, before resolving their dependencies.
foreach (var mod in mods)
{
var resolver = _serviceProvider.GetRequiredService<IDependencyResolver>();
mod.ResolveDependencies(resolver,
new DependencyResolverOptions { CheckForCycle = true, ResolveCompleteChain = true });
}
}
}
5 changes: 5 additions & 0 deletions src/ModVerify.CliApp/GameNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using PG.StarWarsGame.Infrastructure.Games;

namespace ModVerify.CliApp;

internal class GameNotFoundException(string message) : GameException(message);
141 changes: 0 additions & 141 deletions src/ModVerify.CliApp/ModOrGameSelector.cs

This file was deleted.

Loading

0 comments on commit bae82ea

Please sign in to comment.