LANCommander/LANCommander.Launcher.Services/CommandLineService.cs

318 lines
12 KiB
C#
Raw Normal View History

2024-08-07 19:03:07 -05:00
using CommandLine;
using LANCommander.Launcher.Models;
using LANCommander.SDK.Helpers;
2024-09-11 00:24:34 -05:00
using Microsoft.Extensions.Logging;
2024-08-07 19:03:07 -05:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LANCommander.Launcher.Services
{
2024-09-11 00:24:34 -05:00
public class CommandLineService : BaseService
2024-08-07 19:03:07 -05:00
{
private readonly AuthenticationService AuthenticationService;
private readonly UserService UserService;
2024-08-07 19:03:07 -05:00
private readonly GameService GameService;
private readonly InstallService InstallService;
2024-08-07 19:03:07 -05:00
private readonly ImportService ImportService;
private readonly ProfileService ProfileService;
private Settings Settings = SettingService.GetSettings();
2024-09-11 00:24:34 -05:00
public CommandLineService(
SDK.Client client,
ILogger<CommandLineService> logger,
AuthenticationService authenticationService,
UserService userService,
GameService gameService,
InstallService installService,
ImportService importService,
2024-09-11 00:24:34 -05:00
ProfileService profileService) : base(client, logger)
2024-08-07 19:03:07 -05:00
{
AuthenticationService = authenticationService;
UserService = userService;
GameService = gameService;
InstallService = installService;
ImportService = importService;
2024-08-07 19:03:07 -05:00
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-09-27 19:13:31 -05:00
ImportCommandLineOptions,
ExportCommandLineOptions,
UploadCommandLineOptions,
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-26 20:19:51 -05:00
await result.WithParsedAsync<RunCommandLineOptions>(Run);
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);
2024-09-27 19:13:31 -05:00
await result.WithParsedAsync<ExportCommandLineOptions>(Export);
await result.WithParsedAsync<UploadCommandLineOptions>(Upload);
2024-08-07 19:58:37 -05:00
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)
{
2024-09-18 20:42:24 -05:00
Logger.LogInformation($"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.GetAsync(options.GameId);
2024-08-07 19:03:07 -05:00
await InstallService.Add(game, options.InstallDirectory);
await InstallService.Next();
2024-08-07 19:03:07 -05:00
game = await GameService.GetAsync(options.GameId);
2024-08-07 19:03:07 -05:00
2024-09-18 20:42:24 -05:00
Logger.LogInformation($"Successfully installed {game.Title} to directory {game.InstallDirectory}");
2024-08-07 19:58:37 -05:00
}
catch (Exception ex)
2024-08-07 19:03:07 -05:00
{
2024-09-18 20:42:24 -05:00
Logger.LogError(ex, "Game could not be installed");
2024-08-07 19:58:37 -05:00
}
}
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
private async Task Uninstall(UninstallCommandLineOptions options)
{
2024-09-18 20:42:24 -05:00
Logger.LogInformation($"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.GetAsync(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-09-18 20:42:24 -05:00
Logger.LogInformation($"Game successfully uninstalled from {game.InstallDirectory}");
2024-08-07 19:58:37 -05:00
}
catch (Exception ex)
2024-08-07 19:03:07 -05:00
{
2024-09-18 20:42:24 -05:00
Logger.LogError(ex, "Game could not be uninstalled");
2024-08-07 19:58:37 -05:00
}
}
2024-08-07 19:03:07 -05:00
2024-08-26 20:19:51 -05:00
private async Task Run(RunCommandLineOptions options)
{
2024-09-18 20:42:24 -05:00
Logger.LogInformation($"Running game with ID {options.GameId}...");
2024-08-26 20:19:51 -05:00
try
{
var game = await GameService.GetAsync(options.GameId);
var manifest = await ManifestHelper.ReadAsync(game.InstallDirectory, game.Id);
var action = manifest.Actions.FirstOrDefault(a => a.Id == options.ActionId);
2024-08-26 20:19:51 -05:00
if (action == null)
action = manifest.Actions.OrderBy(a => a.SortOrder).FirstOrDefault(a => a.IsPrimaryAction);
await GameService.Run(game, action);
2024-08-26 20:19:51 -05:00
}
catch (Exception ex)
{
2024-09-18 20:42:24 -05:00
Logger.LogError(ex, "Game could not run");
2024-08-26 20:19:51 -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-09-18 20:42:24 -05:00
Logger.LogInformation("Syncing games from server...");
2024-08-07 19:03:07 -05:00
2024-08-07 19:58:37 -05:00
ImportService.OnImportComplete += async () =>
{
2024-09-18 20:42:24 -05:00
Logger.LogInformation("Sync complete!");
2024-08-07 19:58:37 -05:00
};
2024-08-07 19:03:07 -05:00
2024-09-18 20:42:24 -05:00
ImportService.OnImportFailed += async (Exception ex) =>
2024-08-07 19:03:07 -05:00
{
2024-09-18 20:42:24 -05:00
Logger.LogError(ex, "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))
{
2024-09-18 20:42:24 -05:00
Logger.LogInformation("File not found! Check your path!");
2024-08-13 17:42:56 -05:00
return;
}
switch (options.Type)
{
case ArchiveType.Game:
2024-09-18 20:42:24 -05:00
Logger.LogInformation("Uploading game import file to server...");
2024-08-13 17:42:56 -05:00
await Client.Games.ImportAsync(options.Path);
break;
case ArchiveType.Redistributable:
Logger.LogInformation("Uploading redistributable archive file to server...");
await Client.Redistributables.ImportAsync(options.Path);
break;
case ArchiveType.Server:
Logger.LogInformation("Uploading server archive file to server...");
await Client.Servers.ImportAsync(options.Path);
break;
2024-08-13 17:42:56 -05:00
}
2024-09-18 20:42:24 -05:00
Logger.LogInformation("Import complete!");
2024-08-13 17:42:56 -05:00
}
2024-08-13 17:43:17 -05:00
private async Task Export(ExportCommandLineOptions options)
{
if (File.Exists(options.Path))
{
2024-09-18 20:42:24 -05:00
Logger.LogInformation("A file at the specific path already exists!");
2024-08-13 17:43:17 -05:00
return;
}
switch (options.Type)
{
case ArchiveType.Game:
2024-09-18 20:42:24 -05:00
Logger.LogInformation("Exporting game from server...");
2024-08-13 17:43:17 -05:00
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;
case ArchiveType.Redistributable:
Logger.LogInformation("Exporting redistributable from server...");
await Client.Redistributables.ExportAsync(options.Path, options.Id);
break;
case ArchiveType.Server:
Logger.LogInformation("Exporting server from server...");
await Client.Servers.ExportAsync(options.Path, options.Id);
break;
2024-08-13 17:43:17 -05:00
}
2024-09-18 20:42:24 -05:00
Logger.LogInformation($"Successfully exported game to {options.Path}");
2024-08-13 17:43:17 -05:00
}
private async Task Upload(UploadCommandLineOptions options)
{
if (!File.Exists(options.Path))
{
Logger.LogInformation("File not found! Check your path!");
return;
}
switch (options.Type)
{
case ArchiveType.Game:
Logger.LogInformation("Uploading game archive to server...");
await Client.Games.UploadArchiveAsync(options.Path, options.Id, options.Version, options.Changelog);
break;
case ArchiveType.Redistributable:
Logger.LogInformation("Uploading redistributable archive to server...");
await Client.Redistributables.UploadArchiveAsync(options.Path, options.Id, options.Version, options.Changelog);
break;
}
}
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-09-18 20:42:42 -05:00
var token = await Client.AuthenticateAsync(options.Username, options.Password);
Settings.Authentication.AccessToken = token.AccessToken;
Settings.Authentication.RefreshToken = token.RefreshToken;
Settings.Authentication.ServerAddress = Client.GetServerAddress();
SettingService.SaveSettings(Settings);
2024-08-07 19:03:07 -05:00
2024-09-18 20:42:24 -05:00
Logger.LogInformation("Logged in!");
2024-08-07 19:58:37 -05:00
}
catch (Exception ex)
2024-08-07 19:03:07 -05:00
{
2024-09-18 20:42:24 -05:00
Logger.LogError(ex, "Failed to log in!");
2024-08-07 19:58:37 -05:00
}
}
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)
{
var currentUser = await UserService.GetAsync(AuthenticationService.GetUserId());
2024-08-07 19:58:37 -05:00
await ProfileService.ChangeAlias(options.Alias);
2024-08-07 19:03:07 -05:00
Logger.LogInformation($"Changed current user's alias from {currentUser.Alias} to {options.Alias}");
2024-08-07 19:03:07 -05:00
}
}
}