From ce32bc813764020b60d6cb2fc155a55fb7ce795a Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Tue, 6 Aug 2024 20:22:33 -0500 Subject: [PATCH] Add CLI verbs for running scripts and importing games --- .../Models/CommandLineOptions.cs | 12 ++++ LANCommander.Launcher/Program.cs | 66 +++++++++++++++++-- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/LANCommander.Launcher/Models/CommandLineOptions.cs b/LANCommander.Launcher/Models/CommandLineOptions.cs index 425dd54e..3a724e0c 100644 --- a/LANCommander.Launcher/Models/CommandLineOptions.cs +++ b/LANCommander.Launcher/Models/CommandLineOptions.cs @@ -19,5 +19,17 @@ namespace LANCommander.Launcher.Models [Option("Type", HelpText = "The type of script to run")] public ScriptType Type { get; set; } + + [Option("NewName", HelpText = "The new name to use in a name change script")] + public string NewName { get; set; } + + [Option("NewKey", HelpText = "The new key to use in a key change script")] + public string NewKey { get; set; } + } + + [Verb("Import", HelpText = "Import library items from the server")] + public class ImportCommandLineOptions + { + } } diff --git a/LANCommander.Launcher/Program.cs b/LANCommander.Launcher/Program.cs index be9001dd..8d6cc254 100644 --- a/LANCommander.Launcher/Program.cs +++ b/LANCommander.Launcher/Program.cs @@ -30,7 +30,7 @@ namespace LANCommander.Launcher static Logger Logger = LogManager.GetCurrentClassLogger(); [STAThread] - static void Main(string[] args) + static async Task Main(string[] args) { Logger?.Debug("Starting up launcher..."); Logger?.Debug("Loading settings from file"); @@ -243,7 +243,7 @@ namespace LANCommander.Launcher if (args.Length > 0) { - ParseCommandLine(args); + await ParseCommandLineAsync(args, app); return; } @@ -261,13 +261,67 @@ namespace LANCommander.Launcher } } - static void ParseCommandLine(string[] args) + static async Task ParseCommandLineAsync(string[] args, PhotinoBlazorApp app) { - var result = CommandLine.Parser.Default.ParseArguments(args) - .WithParsed(options => + using (var scope = app.Services.CreateScope()) + { + var client = scope.ServiceProvider.GetService(); + + await client.ValidateTokenAsync(); + + var result = CommandLine.Parser.Default.ParseArguments(args); + + await result.WithParsedAsync(async (options) => { - + var client = scope.ServiceProvider.GetService(); + + switch (options.Type) + { + case SDK.Enums.ScriptType.Install: + await client.Scripts.RunInstallScriptAsync(options.InstallDirectory, options.GameId); + break; + + case SDK.Enums.ScriptType.Uninstall: + await client.Scripts.RunUninstallScriptAsync(options.InstallDirectory, options.GameId); + break; + + case SDK.Enums.ScriptType.BeforeStart: + await client.Scripts.RunBeforeStartScriptAsync(options.InstallDirectory, options.GameId); + break; + + case SDK.Enums.ScriptType.AfterStop: + await client.Scripts.RunAfterStopScriptAsync(options.InstallDirectory, options.GameId); + break; + + case SDK.Enums.ScriptType.NameChange: + await client.Scripts.RunNameChangeScriptAsync(options.InstallDirectory, options.GameId, options.NewName); + break; + + case SDK.Enums.ScriptType.KeyChange: + await client.Scripts.RunKeyChangeScriptAsync(options.InstallDirectory, options.GameId, options.NewKey); + break; + } }); + + await result.WithParsedAsync(async (options) => + { + var importService = scope.ServiceProvider.GetService(); + + Console.WriteLine("Importing games from server..."); + + importService.OnImportComplete += async () => + { + Console.WriteLine("Import complete!"); + }; + + importService.OnImportFailed += async () => + { + Console.WriteLine("Import failed!"); + }; + + await importService.ImportAsync(); + }); + } } } }