LANCommander/LANCommander.Launcher.Services/CommandLineService.cs

222 lines
7.6 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-12 19:33:26 -05:00
SyncCommandLineOptions,
2024-08-07 19:03:07 -05:00
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);
2024-08-12 19:33:26 -05:00
await result.WithParsedAsync<SyncCommandLineOptions>(Sync);
2024-08-07 19:58:37 -05:00
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-12 19:33:26 -05:00
private async Task Sync(SyncCommandLineOptions options)
2024-08-07 19:58:37 -05:00
{
2024-08-13 17:42:43 -05:00
Console.WriteLine("Syncing games from server...");
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
ImportService.OnImportComplete += async () =>
{
2024-08-13 17:42:43 -05:00
Console.WriteLine("Sync complete!");
2024-08-07 19:58:37 -05:00
};
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-13 17:42:43 -05:00
Console.WriteLine("Sync failed!");
2024-08-07 19:58:37 -05:00
};
await ImportService.ImportAsync();
}
2024-08-07 19:03:07 -05:00
2024-08-13 17:42:56 -05:00
private async Task Import(ImportCommandLineOptions options)
{
if (!File.Exists(options.Path))
{
Console.WriteLine("File not found! Check your path!");
return;
}
switch (options.Type)
{
case ImportArchiveType.Game:
Console.WriteLine("Uploading game import file to server...");
await Client.Games.ImportAsync(options.Path);
break;
}
Console.WriteLine("Import complete!");
}
2024-08-13 17:43:17 -05:00
private async Task Export(ExportCommandLineOptions options)
{
if (File.Exists(options.Path))
{
Console.WriteLine("A file at the specific path already exists!");
return;
}
switch (options.Type)
{
case ImportArchiveType.Game:
Console.WriteLine("Exporting game from server...");
2024-08-13 18:39:28 -05:00
await Client.Games.ExportAsync(options.Path, options.Id);
2024-08-13 17:43:17 -05:00
break;
}
Console.WriteLine($"Successfully exported game to {options.Path}");
}
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
}
}
}