RynthCore as of 2026-10-05 (internal 0d3c210): dashboard slide-outs on the left (Ranges with the selected monster, Files, Patrol), fixed draggable vital bar, click an item to add a loot rule, loot rules by WCID, chat and loot editor typing no longer leaks into the game, title-bar text size, plugin API v76 (close container), plugin exception traces, plates drawn under the game UI (in testing), launcher account passwords encrypted with Windows DPAPI (in testing), release 2026.10.5.1

This commit is contained in:
Tom Bohar 2026-10-05 08:03:08 -04:00
commit c3e15480e5
50 changed files with 3315 additions and 490 deletions

View file

@ -6,6 +6,14 @@ namespace RynthCore.App;
internal sealed class AcLaunchArgumentBuilder
{
/// <summary>Text shown in place of the password in previews.</summary>
public const string PasswordMask = "********";
/// <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 the saved
/// password 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);
@ -13,17 +21,42 @@ internal sealed class AcLaunchArgumentBuilder
Validate(server, account);
if (!account.TryGetPasswordForLaunch(out string password, out string problem))
throw new InvalidOperationException(problem);
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);
return Build(server, account, account.HasSavedPassword ? PasswordMask : string.Empty);
}
private static string Build(LaunchServerProfile server, LaunchAccountProfile account, string password)
{
return server.Emulator switch
{
AcEmulatorKind.Ace => string.Join(" ",
"-a", Quote(account.AccountName),
"-v", Quote(account.Password),
"-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}:{account.Password}"),
"-a", Quote($"{account.AccountName}:{password}"),
"-rodat", server.RodatEnabled ? "on" : "off"),
_ => throw new InvalidOperationException($"Unsupported emulator type '{server.Emulator}'.")
};

View file

@ -0,0 +1,138 @@
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
namespace RynthCore.App;
/// <summary>
/// Saved account passwords are encrypted with Windows DPAPI (CryptProtectData, CurrentUser
/// scope, the same call System.Security.Cryptography.ProtectedData makes) plus an app-specific
/// entropy constant, and kept in settings as base64 (LaunchAccountProfile.PasswordProtected).
/// Only the Windows user that saved a password can decrypt it; settings copied to another user
/// or PC can't be decrypted, and such an account shows "password needs re-entering".
///
/// Decrypt only at the moment of use (building the client's launch arguments) and never keep
/// the result in a long-lived object. Never log, display or copy a password.
///
/// Shared source: compiled into the launcher, RynthCore.Injector (--launch) and
/// tools\LauncherSettingsTests.
/// </summary>
internal static class AccountPasswordProtection
{
// Changing this makes every saved password unreadable. Bump the suffix only with a migration.
private static readonly byte[] Entropy = Encoding.UTF8.GetBytes("RynthCore.Launcher.AccountPassword.v1");
private const int CryptProtectUiForbidden = 0x1;
/// <summary>Encrypts a password for this Windows user. Empty in, empty out.</summary>
public static string Protect(string? plainText)
{
if (string.IsNullOrEmpty(plainText))
return string.Empty;
byte[] plain = Encoding.UTF8.GetBytes(plainText);
try
{
return Convert.ToBase64String(Transform(plain, protect: true));
}
finally
{
Array.Clear(plain);
}
}
/// <summary>
/// Decrypts a value written by <see cref="Protect"/>. False when it can't be read on this
/// Windows user (copied from another user or PC, damaged base64, wrong entropy).
/// </summary>
public static bool TryUnprotect(string? protectedBase64, out string plainText)
{
plainText = string.Empty;
if (string.IsNullOrEmpty(protectedBase64))
return false;
byte[] blob;
try
{
blob = Convert.FromBase64String(protectedBase64);
}
catch (FormatException)
{
return false;
}
byte[]? plain = null;
try
{
plain = Transform(blob, protect: false);
plainText = Encoding.UTF8.GetString(plain);
return true;
}
catch (Exception ex) when (ex is Win32Exception or PlatformNotSupportedException or EntryPointNotFoundException or DllNotFoundException)
{
return false;
}
finally
{
if (plain != null)
Array.Clear(plain);
}
}
/// <summary>True when the value decrypts on this Windows user (the password itself is discarded).</summary>
public static bool CanUnprotect(string? protectedBase64) => TryUnprotect(protectedBase64, out _);
private static byte[] Transform(byte[] input, bool protect)
{
GCHandle inputHandle = GCHandle.Alloc(input, GCHandleType.Pinned);
GCHandle entropyHandle = GCHandle.Alloc(Entropy, GCHandleType.Pinned);
var output = new DataBlob();
try
{
var inputBlob = new DataBlob { cbData = input.Length, pbData = inputHandle.AddrOfPinnedObject() };
var entropyBlob = new DataBlob { cbData = Entropy.Length, pbData = entropyHandle.AddrOfPinnedObject() };
bool ok = protect
? CryptProtectData(ref inputBlob, null, ref entropyBlob, IntPtr.Zero, IntPtr.Zero, CryptProtectUiForbidden, ref output)
: CryptUnprotectData(ref inputBlob, IntPtr.Zero, ref entropyBlob, IntPtr.Zero, IntPtr.Zero, CryptProtectUiForbidden, ref output);
if (!ok)
throw new Win32Exception(Marshal.GetLastWin32Error());
byte[] result = new byte[output.cbData];
if (output.cbData > 0)
Marshal.Copy(output.pbData, result, 0, output.cbData);
return result;
}
finally
{
if (output.pbData != IntPtr.Zero)
{
// Wipe the native copy (the plain text, when decrypting) before freeing it.
for (int i = 0; i < output.cbData; i++)
Marshal.WriteByte(output.pbData, i, 0);
LocalFree(output.pbData);
}
inputHandle.Free();
entropyHandle.Free();
}
}
[StructLayout(LayoutKind.Sequential)]
private struct DataBlob
{
public int cbData;
public IntPtr pbData;
}
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool CryptProtectData(ref DataBlob pDataIn, string? szDataDescr, ref DataBlob pOptionalEntropy,
IntPtr pvReserved, IntPtr pPromptStruct, int dwFlags, ref DataBlob pDataOut);
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool CryptUnprotectData(ref DataBlob pDataIn, IntPtr ppszDataDescr, ref DataBlob pOptionalEntropy,
IntPtr pvReserved, IntPtr pPromptStruct, int dwFlags, ref DataBlob pDataOut);
[DllImport("kernel32.dll")]
private static extern IntPtr LocalFree(IntPtr hMem);
}

View file

@ -46,11 +46,112 @@ internal static class AppSettingsStore
/// </summary>
public static string? LastMigrationNote { get; private set; }
/// <summary>
/// Set when the last Load() encrypted plain-text account passwords from an older settings
/// file ("migrated N account password(s) to encrypted storage"), or could not. Never holds a
/// password. Null otherwise.
/// </summary>
public static string? LastPasswordMigrationNote { get; private set; }
/// <summary>
/// Set when the last Load() found saved passwords this Windows user can't decrypt (settings
/// copied from another user or PC); those accounts have PasswordNeedsReentry set. Null otherwise.
/// </summary>
public static string? LastPasswordProblemNote { get; private set; }
public static AppSettings Load()
{
LastLoadDiagnostic = null;
LastMigrationNote = null;
return ApplyDecalAccounts(LoadShared());
LastPasswordMigrationNote = null;
LastPasswordProblemNote = null;
// Passwords after the Decal accounts: a save before ApplyDecalAccounts would drop
// decal-accounts.json (every account still reads as RynthCore at that point).
AppSettings settings = ApplyDecalAccounts(LoadShared());
MigratePasswords(settings);
return settings;
}
/// <summary>
/// Encrypts plain-text passwords from an older appsettings.json (LaunchAccountProfile
/// "Password") into PasswordProtected and saves once, .bak included, so the plain text is
/// gone from both files. Also marks accounts whose encrypted password this Windows user can't
/// decrypt. Never logs or reports a password.
/// </summary>
private static void MigratePasswords(AppSettings settings)
{
int migrated = 0, cleared = 0, failed = 0;
var unreadable = new List<string>();
foreach (LaunchAccountProfile account in settings.AccountProfiles ?? new List<LaunchAccountProfile>())
{
if (account.LegacyPlainPassword != null)
{
if (account.LegacyPlainPassword.Length == 0)
{
account.LegacyPlainPassword = null;
cleared++;
continue;
}
try
{
account.PasswordProtected = AccountPasswordProtection.Protect(account.LegacyPlainPassword);
account.LegacyPlainPassword = null;
account.PasswordNeedsReentry = false;
migrated++;
}
catch
{
failed++; // kept as it was (still usable); the next load tries again
}
continue;
}
if (!string.IsNullOrEmpty(account.PasswordProtected) &&
!AccountPasswordProtection.CanUnprotect(account.PasswordProtected))
{
account.PasswordNeedsReentry = true;
unreadable.Add(string.IsNullOrWhiteSpace(account.AccountName) ? account.Id : account.AccountName);
}
}
if (unreadable.Count > 0)
{
LastPasswordProblemNote = $"{unreadable.Count} saved account password(s) can't be decrypted on this Windows user " +
$"(settings copied from another user or PC?) and need re-entering: {string.Join(", ", unreadable)}.";
}
if (migrated + cleared == 0)
{
if (failed > 0)
LastPasswordMigrationNote = $"Could not encrypt {failed} account password(s); they stay as they were and the next start tries again.";
return;
}
try
{
Save(settings);
// Save() moved the old file (plain text) to .bak: replace it with the encrypted one.
File.Copy(SettingsPath, BackupPath, overwrite: true);
if (migrated == 0 && failed == 0)
return; // only empty plain-text fields removed
LastPasswordMigrationNote = $"migrated {migrated} account password(s) to encrypted storage";
if (failed > 0)
LastPasswordMigrationNote += $"; {failed} could not be encrypted and stay as they were";
int quarantined = CountQuarantinedFiles();
if (quarantined > 0)
LastPasswordMigrationNote += $". {quarantined} older appsettings.json.corrupt-* file(s) in {SettingsDirectory} " +
"may still hold plain-text passwords; delete them when no longer needed";
}
catch (Exception ex)
{
LastPasswordMigrationNote = $"Could not save the encrypted account passwords ({ex.GetType().Name}); the next save retries.";
}
}
private static int CountQuarantinedFiles()
{
try { return Directory.GetFiles(SettingsDirectory, "appsettings.json.corrupt-*").Length; }
catch { return 0; }
}
private static AppSettings LoadShared()

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace RynthCore.App;
@ -12,7 +13,26 @@ internal sealed class LaunchAccountProfile
public string Id { get; set; } = Guid.NewGuid().ToString("N");
public string AccountName { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
/// The saved password, DPAPI-encrypted for the current Windows user (base64, see
/// AccountPasswordProtection). Empty = no password saved. Decrypt only when launching:
/// TryGetPasswordForLaunch.
public string PasswordProtected { get; set; } = string.Empty;
/// Plain-text "Password" from a settings file written before passwords were encrypted.
/// Read only: AppSettingsStore.Load encrypts it into PasswordProtected and clears this, so it
/// is never written again (null is skipped on save). Stays set only if encryption failed.
[JsonPropertyName("Password")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? LegacyPlainPassword { get; set; }
/// Set when the saved password can't be decrypted on this Windows user (settings copied
/// from another user or PC). The UI asks for the password again; launching is refused.
[JsonIgnore]
public bool PasswordNeedsReentry { get; set; }
[JsonIgnore]
public bool HasSavedPassword => !string.IsNullOrEmpty(PasswordProtected) || !string.IsNullOrEmpty(LegacyPlainPassword);
public string CharacterName { get; set; } = string.Empty;
public string Alias { get; set; } = string.Empty;
public string ServerId { get; set; } = string.Empty;
@ -65,7 +85,9 @@ internal sealed class LaunchAccountProfile
{
Id = Id,
AccountName = AccountName,
Password = Password,
PasswordProtected = PasswordProtected,
LegacyPlainPassword = LegacyPlainPassword,
PasswordNeedsReentry = PasswordNeedsReentry,
CharacterName = CharacterName,
Alias = Alias,
ServerId = ServerId,
@ -84,7 +106,9 @@ internal sealed class LaunchAccountProfile
{
Id = source.Id;
AccountName = source.AccountName;
Password = source.Password;
PasswordProtected = source.PasswordProtected;
LegacyPlainPassword = source.LegacyPlainPassword;
PasswordNeedsReentry = source.PasswordNeedsReentry;
CharacterName = source.CharacterName;
Alias = source.Alias;
ServerId = source.ServerId;
@ -98,6 +122,55 @@ internal sealed class LaunchAccountProfile
OnLoginWaitMs = source.OnLoginWaitMs;
}
/// <summary>Saves a new password (encrypted at once; the plain text is not kept).</summary>
public void SetPassword(string? plainText)
{
PasswordProtected = AccountPasswordProtection.Protect(plainText);
LegacyPlainPassword = null;
PasswordNeedsReentry = false;
}
public void ClearPassword()
{
PasswordProtected = string.Empty;
LegacyPlainPassword = null;
PasswordNeedsReentry = false;
}
/// <summary>
/// The password to hand the client, decrypted now. True with "" when none is saved. False
/// (and PasswordNeedsReentry set) when the saved one can't be decrypted on this Windows user;
/// the caller must not launch with an empty password instead. Use the result at once and
/// don't store it.
/// </summary>
public bool TryGetPasswordForLaunch(out string password, out string problem)
{
problem = string.Empty;
if (!string.IsNullOrEmpty(LegacyPlainPassword))
{
password = LegacyPlainPassword;
return true;
}
if (string.IsNullOrEmpty(PasswordProtected))
{
password = string.Empty;
return true;
}
if (AccountPasswordProtection.TryUnprotect(PasswordProtected, out password))
{
PasswordNeedsReentry = false;
return true;
}
PasswordNeedsReentry = true;
problem = PasswordNeedsReentryMessage(AccountName);
return false;
}
public static string PasswordNeedsReentryMessage(string? accountName) =>
$"The saved password for account '{accountName}' can't be read on this Windows user " +
"(settings copied from another user or PC?). Password needs re-entering: edit the account and type it again.";
public override string ToString()
{
return DisplayName;