From 93e3ba81753ff8b00f007138bf0e0a369b3ba4d8 Mon Sep 17 00:00:00 2001 From: Canon Date: Wed, 4 Feb 2026 13:35:36 +0800 Subject: [PATCH] Mod Director Resolver --- Commands/ScanCommand.cs | 33 +----------------- Commands/UpdateCommand.cs | 31 +---------------- Services/ModsDirectoryResolver.cs | 58 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 62 deletions(-) create mode 100644 Services/ModsDirectoryResolver.cs diff --git a/Commands/ScanCommand.cs b/Commands/ScanCommand.cs index b868d7f..e5af3b5 100644 --- a/Commands/ScanCommand.cs +++ b/Commands/ScanCommand.cs @@ -14,7 +14,7 @@ public class ScanCommand : AsyncCommand try { // Resolve mods directory - var modsDirectory = ResolveModsDirectory(settings); + var modsDirectory = ModsDirectoryResolver.Resolve(settings.ModDirectory?.FullName, interactive: true); if (modsDirectory == null) { return 1; @@ -50,35 +50,4 @@ public class ScanCommand : AsyncCommand return 1; } } - - private static string? ResolveModsDirectory(ModPathSettings settings) - { - if (settings.ModDirectory != null) - { - return settings.ModDirectory.FullName; - } - - // Auto-discover - var pathDiscovery = new PathDiscoveryService(); - var paths = pathDiscovery.DiscoverModPaths(); - - if (paths.Count == 0) - { - AnsiConsole.MarkupLine("[red]Error: Could not find RimWorld Mods folder.[/]"); - AnsiConsole.MarkupLine("[grey]Please specify the path using --directory[/]"); - return null; - } - - if (paths.Count == 1) - { - return paths[0]; - } - - // Multiple paths found - let user choose - return AnsiConsole.Prompt( - new SelectionPrompt() - .Title("[bold]Multiple RimWorld installations found. Select one:[/]") - .PageSize(10) - .AddChoices(paths)); - } } diff --git a/Commands/UpdateCommand.cs b/Commands/UpdateCommand.cs index e8b7abc..a6db99e 100644 --- a/Commands/UpdateCommand.cs +++ b/Commands/UpdateCommand.cs @@ -17,7 +17,7 @@ public class UpdateCommand : AsyncCommand try { // Resolve mods directory - var modsDirectory = ResolveModsDirectory(settings); + var modsDirectory = ModsDirectoryResolver.Resolve(settings.ModDirectory?.FullName, interactive: false); if (modsDirectory == null) { return 1; @@ -90,33 +90,4 @@ public class UpdateCommand : AsyncCommand return 1; } } - - private static string? ResolveModsDirectory(ModPathSettings settings) - { - if (settings.ModDirectory != null) - { - return settings.ModDirectory.FullName; - } - - // Auto-discover - var pathDiscovery = new PathDiscoveryService(); - var paths = pathDiscovery.DiscoverModPaths(); - - if (paths.Count == 0) - { - AnsiConsole.MarkupLine("[red]Error: Could not find RimWorld Mods folder.[/]"); - AnsiConsole.MarkupLine("[grey]Please specify the path using --directory[/]"); - return null; - } - - if (paths.Count == 1) - { - return paths[0]; - } - - // Multiple paths found - use first one for non-interactive mode - AnsiConsole.MarkupLine($"[yellow]Multiple installations found, using: {paths[0]}[/]"); - AnsiConsole.MarkupLine("[grey]Use --directory to specify a different path[/]"); - return paths[0]; - } } diff --git a/Services/ModsDirectoryResolver.cs b/Services/ModsDirectoryResolver.cs new file mode 100644 index 0000000..f8f1c6e --- /dev/null +++ b/Services/ModsDirectoryResolver.cs @@ -0,0 +1,58 @@ +using Spectre.Console; + +namespace FCPModUpdater.Services; + +public static class ModsDirectoryResolver +{ + /// + /// Resolves the mods directory from an explicit path, auto-discovery, or user prompt. + /// + /// Explicit path provided via --directory, or null for auto-discovery. + /// If true, prompts user to select when multiple paths found. If false, uses first path. + /// The resolved path, or null if resolution failed. + public static string Resolve(string? explicitPath, bool interactive) + { + if (explicitPath != null) + return explicitPath; + + var pathDiscovery = new PathDiscoveryService(); + var paths = pathDiscovery.DiscoverModPaths(); + + switch (paths.Count) + { + case 0: + return PromptForPath(); + case 1: + return paths[0]; + } + + // Multiple paths found + if (interactive) + { + return AnsiConsole.Prompt( + new SelectionPrompt() + .Title("[bold]Multiple RimWorld installations found. Select one:[/]") + .PageSize(10) + .AddChoices(paths)); + } + + // Non-interactive: use first one with warning + AnsiConsole.MarkupLine($"[yellow]Multiple installations found, using: {paths[0]}[/]"); + AnsiConsole.MarkupLine("[grey]Use --directory to specify a different path[/]"); + return paths[0]; + } + + private static string PromptForPath() + { + AnsiConsole.MarkupLine("[yellow]Could not auto-detect RimWorld Mods folder.[/]"); + AnsiConsole.MarkupLine("[grey]Tip: Use --directory to skip this prompt in the future[/]"); + AnsiConsole.WriteLine(); + + return AnsiConsole.Prompt( + new TextPrompt("[bold]Enter the path to your RimWorld Mods folder (ctrl + c to exit):[/]") + .ValidationErrorMessage("[red]That directory does not exist[/]") + .Validate(path => Directory.Exists(path) + ? ValidationResult.Success() + : ValidationResult.Error())); + } +}