topmostfriend/TopMostFriend/Settings.cs

113 lines
3.1 KiB
C#
Raw Normal View History

2022-08-25 23:54:01 +00:00
using Microsoft.Win32;
using System;
2022-08-25 23:56:27 +00:00
using System.Collections.Generic;
using System.IO;
using System.Text;
2022-08-25 23:54:01 +00:00
namespace TopMostFriend {
public static class Settings {
private const string ROOT = @"Software\flash.moe\TopMostFriend";
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<T>(string name, T fallback = default) {
try {
return (T)Convert.ChangeType(GetRoot().GetValue(name, fallback), typeof(T));
} catch {
return fallback;
}
}
2022-08-25 23:56:27 +00:00
public static string[] Get(string name, string[] fallback = null) {
byte[] buffer = GetRoot().GetValue(name, null) as byte[];
if (buffer == null)
return fallback;
List<string> strings = new List<string>();
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();
}
2022-08-25 23:54:01 +00:00
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);
}
2022-08-25 23:56:27 +00:00
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);
}
}
2022-08-25 23:54:01 +00:00
public static void SetDefault(string name, object value) {
if (!Has(name))
Set(name, value);
}
2022-08-25 23:56:27 +00:00
public static void SetDefault(string name, string[] value) {
if (!Has(name))
Set(name, value);
}
2022-08-25 23:54:01 +00:00
public static void Remove(string name) {
GetRoot().DeleteValue(name, false);
}
}
}