LANCommander/LANCommander.Launcher.Services/CommandLineService.cs

181 lines
6.4 KiB
C#
Raw Normal View History

2024-08-07 19:03:07 -05:00
using CommandLine;
using LANCommander.Launcher.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LANCommander.Launcher.Services
{
public class CommandLineService
{
private readonly SDK.Client Client;
private readonly GameService GameService;
private readonly DownloadService DownloadService;
private readonly ImportService ImportService;
private readonly ProfileService ProfileService;
private Settings Settings = SettingService.GetSettings();
public CommandLineService(SDK.Client Client, GameService GameService, ImportService ImportService, ProfileService profileService)
{
Client = Client;
GameService = GameService;
ImportService = ImportService;
ProfileService = profileService;
}
public async Task ParseCommandLineAsync(string[] args)
{
await Client.ValidateTokenAsync();
var result = Parser.Default.ParseArguments
<
RunScriptCommandLineOptions,
InstallCommandLineOptions,
2024-08-07 19:58:37 -05:00
UninstallCommandLineOptions,
2024-08-07 19:03:07 -05:00
ImportCommandLineOptions,
LoginCommandLineOptions,
LogoutCommandLineOptions,
ChangeAliasCommandLineOptions
>(args);
2024-08-07 19:58:37 -05:00
await result.WithParsedAsync<RunScriptCommandLineOptions>(RunScript);
await result.WithParsedAsync<InstallCommandLineOptions>(Install);
await result.WithParsedAsync<UninstallCommandLineOptions>(Uninstall);
await result.WithParsedAsync<ImportCommandLineOptions>(Import);
await result.WithParsedAsync<LoginCommandLineOptions>(Login);
await result.WithParsedAsync<LogoutCommandLineOptions>(Logout);
await result.WithParsedAsync<ChangeAliasCommandLineOptions>(ChangeAlias);
}
private async Task RunScript(RunScriptCommandLineOptions options)
{
switch (options.Type)
2024-08-07 19:03:07 -05:00
{
2024-08-07 19:58:37 -05:00
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:
2024-08-08 17:59:15 -05:00
await Client.Scripts.RunNameChangeScriptAsync(options.InstallDirectory, options.GameId, options.NewPlayerAlias);
2024-08-07 19:58:37 -05:00
break;
case SDK.Enums.ScriptType.KeyChange:
2024-08-08 17:59:15 -05:00
await Client.Scripts.RunKeyChangeScriptAsync(options.InstallDirectory, options.GameId, options.AllocatedKey);
2024-08-07 19:58:37 -05:00
break;
}
}
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
private async Task Install(InstallCommandLineOptions options)
{
Console.WriteLine($"Downloading and installing game with ID {options.GameId}...");
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
try
{
var game = await GameService.Get(options.GameId);
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
await DownloadService.Add(game);
await DownloadService.Install();
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
game = await GameService.Get(options.GameId);
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
Console.WriteLine($"Successfully installed {game.Title} to directory {game.InstallDirectory}");
}
catch (Exception ex)
2024-08-07 19:03:07 -05:00
{
2024-08-07 19:58:37 -05:00
Console.WriteLine($"Game could not be installed: {ex.Message}");
}
}
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
private async Task Uninstall(UninstallCommandLineOptions options)
{
Console.WriteLine($"Uninstalling game with ID {options.GameId}...");
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
try
{
var game = await GameService.Get(options.GameId);
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
await GameService.UninstallAsync(game);
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
Console.WriteLine($"Game successfully uninstalled from {game.InstallDirectory}");
}
catch (Exception ex)
2024-08-07 19:03:07 -05:00
{
2024-08-07 19:58:37 -05:00
Console.WriteLine($"Game could not be uninstalled: {ex.Message}");
}
}
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
private async Task Import(ImportCommandLineOptions options)
{
Console.WriteLine("Importing games from server...");
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
ImportService.OnImportComplete += async () =>
{
Console.WriteLine("Import complete!");
};
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
ImportService.OnImportFailed += async () =>
2024-08-07 19:03:07 -05:00
{
2024-08-07 19:58:37 -05:00
Console.WriteLine("Import failed!");
};
await ImportService.ImportAsync();
}
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
private async Task Login(LoginCommandLineOptions options)
{
try
{
if (String.IsNullOrWhiteSpace(options.ServerAddress))
options.ServerAddress = Settings.Authentication.ServerAddress;
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
if (String.IsNullOrWhiteSpace(options.ServerAddress))
throw new ArgumentException("A server address must be specified");
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
Client.UseServerAddress(options.ServerAddress);
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
await Client.AuthenticateAsync(options.Username, options.Password);
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
Console.WriteLine("Logged in!");
}
catch (Exception ex)
2024-08-07 19:03:07 -05:00
{
2024-08-07 19:58:37 -05:00
Console.WriteLine(ex.Message);
}
}
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
private async Task Logout(LogoutCommandLineOptions options)
{
await Client.LogoutAsync();
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
Settings.Authentication.AccessToken = "";
Settings.Authentication.RefreshToken = "";
Settings.Authentication.OfflineMode = false;
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
SettingService.SaveSettings(Settings);
}
private async Task ChangeAlias(ChangeAliasCommandLineOptions options)
{
await ProfileService.ChangeAlias(options.Alias);
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
Console.WriteLine($"Changed current user's alias from {Settings.Profile.Alias} to {options.Alias}");
2024-08-07 19:03:07 -05:00
}
}
}