LANCommander/LANCommander.Launcher.Services/CommandLineService.cs

303 lines
11 KiB
C#
Raw Permalink 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;
using LANCommander.SDK;
using LANCommander.SDK.Services;
2024-08-07 19:03:07 -05:00
namespace LANCommander.Launcher.Services
{
public class CommandLineService(
ILogger<CommandLineService> logger,
AuthenticationService authenticationService,
UserService userService,
GameService gameService,
InstallService installService,
ImportService importService,
ProfileService profileService,
AuthenticationClient authenticationClient,
ScriptClient scriptClient,
GameClient gameClient,
RedistributableClient redistributableClient,
ServerClient serverClient,
IConnectionClient connectionClient,
SettingsProvider<Settings.Settings> settingsProvider) : BaseService(logger)
2024-08-07 19:03:07 -05:00
{
public async Task ParseCommandLineAsync(string[] args)
{
await authenticationClient.ValidateTokenAsync();
2024-08-07 19:03:07 -05:00
var result = Parser.Default.ParseArguments
<
RunScriptCommandLineOptions,
InstallCommandLineOptions,
2024-08-07 19:58:37 -05:00
UninstallCommandLineOptions,
2026-05-23 22:58:10 -05:00
RunCommandLineOptions,
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:
2026-02-10 17:34:35 -06:00
await scriptClient.Game_RunInstallScriptAsync(options.InstallDirectory, options.GameId);
2024-08-07 19:58:37 -05:00
break;
case SDK.Enums.ScriptType.Uninstall:
2026-02-10 17:34:35 -06:00
await scriptClient.Game_RunUninstallScriptAsync(options.InstallDirectory, options.GameId);
2024-08-07 19:58:37 -05:00
break;
case SDK.Enums.ScriptType.BeforeStart:
2026-02-10 17:34:35 -06:00
await scriptClient.Game_RunBeforeStartScriptAsync(options.InstallDirectory, options.GameId);
2024-08-07 19:58:37 -05:00
break;
case SDK.Enums.ScriptType.AfterStop:
2026-02-10 17:34:35 -06:00
await scriptClient.Game_RunAfterStopScriptAsync(options.InstallDirectory, options.GameId);
2024-08-07 19:58:37 -05:00
break;
case SDK.Enums.ScriptType.NameChange:
2026-02-10 17:34:35 -06:00
await scriptClient.Game_RunNameChangeScriptAsync(options.InstallDirectory, options.GameId, options.NewPlayerAlias ?? Settings.Settings.DEFAULT_GAME_USERNAME);
2024-08-07 19:58:37 -05:00
break;
case SDK.Enums.ScriptType.KeyChange:
2026-02-10 17:34:35 -06:00
await scriptClient.Game_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
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<SDK.Models.Manifest.Game>(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
/*importService.OnImportComplete += async () =>
2024-08-07 19:58:37 -05:00
{
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
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:58:37 -05:00
}
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 gameClient.ImportAsync(options.Path);
2024-08-13 17:42:56 -05:00
break;
case ArchiveType.Redistributable:
Logger.LogInformation("Uploading redistributable archive file to server...");
await redistributableClient.ImportAsync(options.Path);
break;
case ArchiveType.Server:
Logger.LogInformation("Uploading server archive file to server...");
await serverClient.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
await gameClient.ExportAsync(options.Path, options.Id);
2024-08-13 17:43:17 -05:00
break;
case ArchiveType.Redistributable:
Logger.LogInformation("Exporting redistributable from server...");
await redistributableClient.ExportAsync(options.Path, options.Id);
break;
case ArchiveType.Server:
Logger.LogInformation("Exporting server from server...");
await serverClient.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 gameClient.UploadArchiveAsync(options.Path, options.Id, options.Version, options.Changelog);
break;
case ArchiveType.Redistributable:
Logger.LogInformation("Uploading redistributable archive to server...");
await redistributableClient.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 = settingsProvider.CurrentValue.Authentication.ServerAddress.ToString();
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
await connectionClient.UpdateServerAddressAsync(options.ServerAddress);
2024-08-07 19:03:07 -05:00
var token = await authenticationClient.AuthenticateAsync(options.Username, options.Password, connectionClient.GetServerAddress());
2025-11-19 00:36:03 -06:00
settingsProvider.Update(s =>
{
2025-11-19 00:36:03 -06:00
s.Authentication.Token = token;
s.Authentication.ServerAddress = connectionClient.GetServerAddress();
});
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 authenticationClient.LogoutAsync();
settingsProvider.Update(s =>
{
2025-11-19 00:36:03 -06:00
s.Authentication.Token = null;
s.Authentication.OfflineModeEnabled = false;
});
2024-08-07 19:58:37 -05:00
}
private async Task ChangeAlias(ChangeAliasCommandLineOptions options)
{
var currentUser = await userService.GetAsync(authenticationService.GetUserId());
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
}
}
}