using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; using System.Text; namespace SoFii { public static class Settings { public const string LAST_VERSION = "LastVersion"; public const string GAME_PATH = "GamePath"; public const string USE_DEFAULT_SERVER = "UseDefaultServer"; public const string SERVER_ADDRESS = "ServerAddress"; public const string CUSTOM_DNS_SERVERS = "CustomDNSServers"; public const string PLAYER_NAME = "PlayerName"; public const string GFX_FULLSCREEN = "GfxFullscreen"; public const string GFX_MODE = "GfxMode"; public const string IGNORE_NEW_VERSION = "IgnoreNewVersion"; public static int LastVersion { get => Get(LAST_VERSION, 0); set => Set(LAST_VERSION, value); } public static string GamePath { get => Get(GAME_PATH, string.Empty); set => Set(GAME_PATH, value); } public static bool UseDefaultServer { get => Get(USE_DEFAULT_SERVER, true); set => Set(USE_DEFAULT_SERVER, value); } public static string ServerAddress { get => Get(SERVER_ADDRESS, string.Empty); set => Set(SERVER_ADDRESS, value); } public static string[] CustomDNSServers { get => Get(CUSTOM_DNS_SERVERS, string.Empty).Split(' '); set { if(value == null || value.Length < 1) Remove(CUSTOM_DNS_SERVERS); else Set(CUSTOM_DNS_SERVERS, string.Join(" ", value)); } } public static string PlayerName { get => Get(PLAYER_NAME, "Speler"); set => Set(PLAYER_NAME, value); } public static bool GfxFullscreen { get => Get(GFX_FULLSCREEN, false); set => Set(GFX_FULLSCREEN, value); } public static int GfxMode { get => Get(GFX_MODE, -1); set => Set(GFX_MODE, value); } public static int IgnoreNewVersion { get => Get(IGNORE_NEW_VERSION, 0); set => Set(IGNORE_NEW_VERSION, value); } private const string ROOT = @"Software\flash.moe\SoFii"; private static RegistryKey GetRoot() { RegistryKey root = Registry.CurrentUser.OpenSubKey(ROOT, true); if(root == null) root = Registry.CurrentUser.CreateSubKey(ROOT); return root; } public static T Get(string name, T fallback = default) { try { return (T)Convert.ChangeType(GetRoot().GetValue(name, fallback), typeof(T)); } catch { return fallback; } } public static string[] Get(string name, string[] fallback = null) { if(!(GetRoot().GetValue(name, null) is byte[] buffer)) return fallback; List strings = new List(); using(MemoryStream src = new MemoryStream(buffer)) using(MemoryStream ms = new MemoryStream()) { int b; for(; ; ) { b = src.ReadByte(); if(b == -1) break; else if(b != 0) ms.WriteByte((byte)b); else { strings.Add(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); } } } return strings.ToArray(); } public static bool Has(string name) { try { GetRoot().GetValueKind(name); return true; } catch { return false; } } public static void Set(string name, object value) { if(value == null) { Remove(name); return; } switch(value) { case bool b: value = b ? 1 : 0; break; } GetRoot().SetValue(name, value); } public static void Set(string name, string[] values) { if(values == null || values.Length < 1) { Remove(name); return; } using(MemoryStream ms = new MemoryStream()) { foreach(string value in values) { byte[] buffer = Encoding.UTF8.GetBytes(value); ms.Write(buffer, 0, buffer.Length); ms.WriteByte(0); } GetRoot().SetValue(name, ms.ToArray(), RegistryValueKind.Binary); } } public static void SetDefault(string name, object value) { if(!Has(name)) Set(name, value); } public static void SetDefault(string name, string[] value) { if(!Has(name)) Set(name, value); } public static void Remove(string name) { GetRoot().DeleteValue(name, false); } } }