154 lines
6 KiB
C#
154 lines
6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
|
|
namespace RynthCore.App;
|
|
|
|
internal sealed class AcLaunchArgumentBuilder
|
|
{
|
|
/// <summary>Text shown in place of the password in previews.</summary>
|
|
public const string PasswordMask = "********";
|
|
|
|
/// <summary>
|
|
/// True when <paramref name="account"/> can't be launched until a password is entered: none
|
|
/// is saved, or the saved one can't be decrypted on this Windows user. Every supported
|
|
/// emulator logs on with a password, and the client refuses an empty one before it connects
|
|
/// ("You can only run the game from the launcher program").
|
|
/// </summary>
|
|
public static bool NeedsPassword(LaunchAccountProfile account)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(account);
|
|
return account.PasswordNeedsReentry || !account.HasSavedPassword;
|
|
}
|
|
|
|
/// <summary>Why an account without a password can't be launched (no secrets in it).</summary>
|
|
public static string MissingPasswordMessage(string? accountName) =>
|
|
$"No password is saved for account '{accountName}'. The AC client won't start without one " +
|
|
"(\"You can only run the game from the launcher program\"). Launch it by hand to enter it, or edit the account.";
|
|
|
|
/// <summary>
|
|
/// The client's command line, with the saved password decrypted now. The result carries the
|
|
/// password: hand it to the process and never log, display or keep it. Throws when no password
|
|
/// is saved or the saved one can't be decrypted on this Windows user (never launches with an
|
|
/// empty one instead).
|
|
/// </summary>
|
|
public string BuildArguments(LaunchServerProfile server, LaunchAccountProfile account)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(server);
|
|
ArgumentNullException.ThrowIfNull(account);
|
|
|
|
Validate(server, account);
|
|
|
|
if (!account.TryGetPasswordForLaunch(out string password, out string problem))
|
|
throw new InvalidOperationException(problem);
|
|
if (password.Length == 0)
|
|
throw new InvalidOperationException(MissingPasswordMessage(account.AccountName));
|
|
|
|
return Build(server, account, password);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The same command line for display: the password is never decrypted, a saved one shows as
|
|
/// <see cref="PasswordMask"/>.
|
|
/// </summary>
|
|
public string BuildMaskedArguments(LaunchServerProfile server, LaunchAccountProfile account)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(server);
|
|
ArgumentNullException.ThrowIfNull(account);
|
|
|
|
Validate(server, account);
|
|
|
|
if (account.PasswordNeedsReentry)
|
|
return LaunchAccountProfile.PasswordNeedsReentryMessage(account.AccountName);
|
|
if (!account.HasSavedPassword)
|
|
return MissingPasswordMessage(account.AccountName);
|
|
|
|
return Build(server, account, PasswordMask);
|
|
}
|
|
|
|
private static string Build(LaunchServerProfile server, LaunchAccountProfile account, string password)
|
|
{
|
|
return server.Emulator switch
|
|
{
|
|
AcEmulatorKind.Ace => string.Join(" ",
|
|
"-a", Quote(account.AccountName),
|
|
"-v", Quote(password),
|
|
"-h", Quote(server.ConnectionString),
|
|
"-rodat", server.RodatEnabled ? "on" : "off"),
|
|
AcEmulatorKind.Gdle => string.Join(" ",
|
|
"-h", Quote(server.Host),
|
|
"-p", server.Port.ToString(CultureInfo.InvariantCulture),
|
|
"-a", Quote($"{account.AccountName}:{password}"),
|
|
"-rodat", server.RodatEnabled ? "on" : "off"),
|
|
_ => throw new InvalidOperationException($"Unsupported emulator type '{server.Emulator}'.")
|
|
};
|
|
}
|
|
|
|
public string Describe(LaunchServerProfile server, LaunchAccountProfile account)
|
|
{
|
|
return $"{server.DisplayName} / {account.DisplayName}";
|
|
}
|
|
|
|
private static void Validate(LaunchServerProfile server, LaunchAccountProfile account)
|
|
{
|
|
var errors = new List<string>();
|
|
|
|
if (string.IsNullOrWhiteSpace(server.Name))
|
|
errors.Add("Server name is required.");
|
|
if (string.IsNullOrWhiteSpace(server.Host))
|
|
errors.Add("Server host is required.");
|
|
if (server.Port <= 0)
|
|
errors.Add("Server port must be greater than zero.");
|
|
if (string.IsNullOrWhiteSpace(account.AccountName))
|
|
errors.Add("Account name is required.");
|
|
else if (AccountNameProblem(account.AccountName, server.Emulator) is string nameProblem)
|
|
errors.Add(nameProblem);
|
|
|
|
if (errors.Count > 0)
|
|
throw new InvalidOperationException(string.Join(" ", errors));
|
|
}
|
|
|
|
/// <summary>Longest account name ACE accepts (AuthenticationHandler rejects longer ones).</summary>
|
|
public const int MaxAccountNameLength = 50;
|
|
|
|
/// <summary>
|
|
/// Why <paramref name="accountName"/> can't log on to a server of <paramref name="emulator"/>
|
|
/// (null = fine). GDLE takes "account:password" in one argument, so a colon in the name would
|
|
/// split it in the wrong place.
|
|
/// </summary>
|
|
public static string? AccountNameProblem(string? accountName, AcEmulatorKind emulator)
|
|
{
|
|
string name = accountName?.Trim() ?? string.Empty;
|
|
if (name.Length == 0)
|
|
return "Account name is required.";
|
|
if (name.Length > MaxAccountNameLength)
|
|
return $"Account name is longer than {MaxAccountNameLength} characters.";
|
|
if (emulator == AcEmulatorKind.Gdle && name.Contains(':'))
|
|
return "Account names can't contain ':' on GDLE servers.";
|
|
return null;
|
|
}
|
|
|
|
private static string Quote(string value)
|
|
{
|
|
value ??= string.Empty;
|
|
|
|
if (value.Length == 0)
|
|
return "\"\"";
|
|
|
|
if (!NeedsQuoting(value))
|
|
return value;
|
|
|
|
return "\"" + value.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"";
|
|
}
|
|
|
|
private static bool NeedsQuoting(string value)
|
|
{
|
|
foreach (char ch in value)
|
|
{
|
|
if (char.IsWhiteSpace(ch) || ch == '"')
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|