LANCommander/LANCommander.Launcher.Services/CommandLineService.cs

295 lines
11 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;
using LANCommander.SDK;
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,
SDK.Client client) : BaseService(logger)
2024-08-07 19:03:07 -05:00
{
public async Task ParseCommandLineAsync(string[] args)
{
await client.Authentication.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,
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);
2024-08-07 19:58:37 -05:00
break;
case SDK.Enums.ScriptType.Uninstall:
await client.Scripts.RunUninstallScriptAsync(options.InstallDirectory, options.GameId);
2024-08-07 19:58:37 -05:00
break;
case SDK.Enums.ScriptType.BeforeStart:
await client.Scripts.RunBeforeStartScriptAsync(options.InstallDirectory, options.GameId);
2024-08-07 19:58:37 -05:00
break;
case SDK.Enums.ScriptType.AfterStop:
await client.Scripts.RunAfterStopScriptAsync(options.InstallDirectory, options.GameId);
2024-08-07 19:58:37 -05:00
break;
case SDK.Enums.ScriptType.NameChange:
await client.Scripts.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:
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
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 client.Games.ImportAsync(options.Path);
2024-08-13 17:42:56 -05:00
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
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 = client.Settings.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 client.Connection.UpdateServerAddressAsync(options.ServerAddress);
2024-08-07 19:03:07 -05:00
2025-10-01 18:45:30 -05:00
var token = await client.Authentication.AuthenticateAsync(options.Username, options.Password, client.Connection.GetServerAddress());
2025-11-19 00:36:03 -06:00
client.Settings.Update(s =>
{
2025-11-19 00:36:03 -06:00
s.Authentication.Token = token;
s.Authentication.ServerAddress = client.Connection.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 client.Authentication.LogoutAsync();
client.Settings.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
}
}
}