@@ -0,0 +1,56 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace BSO.Launcher;
|
||||
|
||||
/// <summary>
|
||||
/// Einstellungen und Anmeldung in %LocalAppData%\BSO Launcher\settings.json. Der Token ist mit DPAPI verschluesselt
|
||||
/// (nur dieser Windows-Benutzer auf diesem PC kann ihn lesen).
|
||||
/// </summary>
|
||||
public sealed class Settings
|
||||
{
|
||||
public static string Folder => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "BSO Launcher");
|
||||
static string FilePath => Path.Combine(Folder, "settings.json");
|
||||
static readonly JsonSerializerOptions Options = new() { WriteIndented = true };
|
||||
|
||||
public string? Username { get; set; }
|
||||
public string? ProtectedToken { get; set; }
|
||||
/// <summary>Spielordner von Hand gewaehlt (sonst automatisch aus den Steam-Bibliotheken).</summary>
|
||||
public string? GameFolder { get; set; }
|
||||
public string Server { get; set; } = "127.0.0.1:7777";
|
||||
public bool AutoConnect { get; set; } = true;
|
||||
public bool KeepOpen { get; set; }
|
||||
public bool AutoUpdateMod { get; set; } = true;
|
||||
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public string? Token
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(ProtectedToken)) return null;
|
||||
try { return Encoding.UTF8.GetString(ProtectedData.Unprotect(Convert.FromBase64String(ProtectedToken), null, DataProtectionScope.CurrentUser)); }
|
||||
catch { return null; }
|
||||
}
|
||||
set => ProtectedToken = value == null ? null
|
||||
: Convert.ToBase64String(ProtectedData.Protect(Encoding.UTF8.GetBytes(value), null, DataProtectionScope.CurrentUser));
|
||||
}
|
||||
|
||||
public static Settings Load()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(FilePath)) return JsonSerializer.Deserialize<Settings>(File.ReadAllText(FilePath)) ?? new Settings();
|
||||
}
|
||||
catch (Exception e) { LauncherLog.Error("settings.json nicht lesbar: " + e.Message); }
|
||||
return new Settings();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
Directory.CreateDirectory(Folder);
|
||||
string tmp = FilePath + ".tmp";
|
||||
File.WriteAllText(tmp, JsonSerializer.Serialize(this, Options));
|
||||
File.Move(tmp, FilePath, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user