166 lines
5.4 KiB
C#
166 lines
5.4 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace Hk4eGm.Services;
|
|
|
|
/// <summary>
|
|
/// config.ini 读写:与原程序一致(exe 同目录、GBK、中文节名/键名)。
|
|
/// </summary>
|
|
public sealed class AppConfig
|
|
{
|
|
static AppConfig()
|
|
{
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
}
|
|
|
|
public static string AppDir => AppContext.BaseDirectory;
|
|
public static string ConfigPath => Path.Combine(AppDir, "config.ini");
|
|
|
|
private static readonly Dictionary<string, List<KeyValuePair<string, string>>> Defaults = new()
|
|
{
|
|
["应用"] = new()
|
|
{
|
|
new("应用标题", "GM - NEW UI"),
|
|
new("自动更新", "true"),
|
|
new("主题", "dark"),
|
|
new("启用MySQL", "false"),
|
|
new("启用SSH", "false"),
|
|
new("隐藏页面", ""),
|
|
new("隐藏提示", "false"),
|
|
},
|
|
["MUIP"] = new()
|
|
{
|
|
new("IP地址", "192.168.0.0"),
|
|
new("端口", "20011"),
|
|
new("区服", "dev_gio"),
|
|
new("玩家UID", "1"),
|
|
new("是否连通", "false"),
|
|
},
|
|
["MySQL"] = new()
|
|
{
|
|
new("IP地址", "192.168.0.0"),
|
|
new("端口", "3306"),
|
|
new("用户名", "root"),
|
|
new("密码", "123456"),
|
|
new("User数据库名", "db_hk4e_user_gio"),
|
|
new("Config数据库名", "db_hk4e_config_gio"),
|
|
new("是否连通", "false"),
|
|
},
|
|
["SSH"] = new()
|
|
{
|
|
new("IP地址", "192.168.0.0"),
|
|
new("端口", "22"),
|
|
new("用户名", "root"),
|
|
new("密码", "123456"),
|
|
new("服务目录", "/genshin/srv"),
|
|
new("是否连通", "false"),
|
|
},
|
|
["快捷启动"] = new()
|
|
{
|
|
new("游戏路径", ""),
|
|
new("模组路径", ""),
|
|
new("插件路径", ""),
|
|
new("其它路径", ""),
|
|
new("vmrun路径", ""),
|
|
new("虚拟机路径", ""),
|
|
},
|
|
};
|
|
|
|
private readonly Dictionary<string, List<KeyValuePair<string, string>>> _data = new();
|
|
|
|
public AppConfig()
|
|
{
|
|
Load();
|
|
}
|
|
|
|
public void Load()
|
|
{
|
|
_data.Clear();
|
|
foreach (var (defSection, pairs) in Defaults)
|
|
_data[defSection] = new List<KeyValuePair<string, string>>(pairs);
|
|
|
|
if (!File.Exists(ConfigPath))
|
|
{
|
|
Save();
|
|
return;
|
|
}
|
|
|
|
var encoding = Encoding.GetEncoding(936);
|
|
string text;
|
|
try { text = File.ReadAllText(ConfigPath, encoding); }
|
|
catch { text = File.ReadAllText(ConfigPath, Encoding.UTF8); }
|
|
|
|
string? section = null;
|
|
foreach (var raw in text.Split('\n'))
|
|
{
|
|
var line = raw.Trim().TrimEnd('\r');
|
|
if (line.Length == 0) continue;
|
|
if (line.StartsWith('[') && line.EndsWith(']'))
|
|
{
|
|
section = line[1..^1];
|
|
if (!_data.ContainsKey(section)) _data[section] = new();
|
|
continue;
|
|
}
|
|
if (section is null) continue;
|
|
var idx = line.IndexOf('=');
|
|
if (idx <= 0) continue;
|
|
var key = line[..idx].Trim();
|
|
var value = line[(idx + 1)..].Trim();
|
|
var list = _data[section];
|
|
var existing = list.FindIndex(kv => kv.Key == key);
|
|
if (existing >= 0) list[existing] = new(key, value);
|
|
else list.Add(new KeyValuePair<string, string>(key, value));
|
|
}
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
var sb = new StringBuilder();
|
|
foreach (var (sec, pairs) in _data)
|
|
{
|
|
sb.Append('[').Append(sec).Append("]\r\n");
|
|
foreach (var kv in pairs) sb.Append(kv.Key).Append('=').Append(kv.Value).Append("\r\n");
|
|
sb.Append("\r\n");
|
|
}
|
|
File.WriteAllText(ConfigPath, sb.ToString(), Encoding.GetEncoding(936));
|
|
}
|
|
|
|
public string Get(string section, string key, string fallback = "")
|
|
{
|
|
if (_data.TryGetValue(section, out var list))
|
|
{
|
|
var item = list.FirstOrDefault(kv => kv.Key == key);
|
|
if (item.Key == key) return item.Value;
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
public void Set(string section, string key, string value)
|
|
{
|
|
if (!_data.TryGetValue(section, out var list))
|
|
{
|
|
list = new List<KeyValuePair<string, string>>();
|
|
_data[section] = list;
|
|
}
|
|
var idx = list.FindIndex(kv => kv.Key == key);
|
|
if (idx >= 0) list[idx] = new(key, value);
|
|
else list.Add(new KeyValuePair<string, string>(key, value));
|
|
}
|
|
|
|
public bool GetBool(string section, string key, bool fallback = false)
|
|
{
|
|
var v = Get(section, key, fallback ? "true" : "false").Trim().ToLowerInvariant();
|
|
return v is "1" or "true" or "yes" or "on";
|
|
}
|
|
|
|
public int GetInt(string section, string key, int fallback = 0)
|
|
=> int.TryParse(Get(section, key, fallback.ToString()), out var v) ? v : fallback;
|
|
|
|
public string MuipHost => Get("MUIP", "IP地址", "127.0.0.1");
|
|
public int MuipPort => GetInt("MUIP", "端口", 20011);
|
|
public string MuipRegion => Get("MUIP", "区服", "dev_gio");
|
|
public string Uid => Get("MUIP", "玩家UID", "1");
|
|
public bool MySqlEnabled => GetBool("应用", "启用MySQL");
|
|
public bool SshEnabled => GetBool("应用", "启用SSH");
|
|
}
|