160 lines
5.2 KiB
Python
160 lines
5.2 KiB
Python
"""config.ini 读写 —— 复刻原程序:exe 同目录、GBK 编码、中文节名/键名。"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import sys
|
||
from collections import OrderedDict
|
||
from typing import Dict
|
||
|
||
if getattr(sys, "frozen", False):
|
||
APP_DIR = os.path.dirname(sys.executable)
|
||
else:
|
||
APP_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
CONFIG_PATH = os.path.join(APP_DIR, "config.ini")
|
||
|
||
# 与原程序默认值完全一致
|
||
DEFAULTS = OrderedDict([
|
||
("应用", OrderedDict([
|
||
("应用标题", "GM - NEW UI"),
|
||
("自动更新", "true"),
|
||
("主题", "dark"),
|
||
("启用MySQL", "false"),
|
||
("启用SSH", "false"),
|
||
("隐藏页面", ""),
|
||
("隐藏提示", "false"),
|
||
])),
|
||
("MUIP", OrderedDict([
|
||
("IP地址", "192.168.0.0"),
|
||
("端口", "20011"),
|
||
("区服", "dev_gio"),
|
||
("玩家UID", "1"),
|
||
("是否连通", "false"),
|
||
])),
|
||
("MySQL", OrderedDict([
|
||
("IP地址", "192.168.0.0"),
|
||
("端口", "3306"),
|
||
("用户名", "root"),
|
||
("密码", "123456"),
|
||
("User数据库名", "db_hk4e_user_gio"),
|
||
("Config数据库名", "db_hk4e_config_gio"),
|
||
("是否连通", "false"),
|
||
])),
|
||
("SSH", OrderedDict([
|
||
("IP地址", "192.168.0.0"),
|
||
("端口", "22"),
|
||
("用户名", "root"),
|
||
("密码", "123456"),
|
||
("服务目录", "/genshin/srv"),
|
||
("是否连通", "false"),
|
||
])),
|
||
("快捷启动", OrderedDict([
|
||
("游戏路径", ""),
|
||
("模组路径", ""),
|
||
("插件路径", ""),
|
||
("其它路径", ""),
|
||
("vmrun路径", ""),
|
||
("虚拟机路径", ""),
|
||
])),
|
||
])
|
||
|
||
SECTION_ORDER = list(DEFAULTS.keys())
|
||
|
||
|
||
class Config:
|
||
def __init__(self, path: str = CONFIG_PATH):
|
||
self.path = path
|
||
self.data: Dict[str, Dict[str, str]] = {}
|
||
self.load()
|
||
|
||
# ------------------------------------------------------------------ 读写
|
||
def load(self) -> None:
|
||
self.data = {s: OrderedDict(kv) for s, kv in DEFAULTS.items()}
|
||
if not os.path.isfile(self.path):
|
||
self.save()
|
||
return
|
||
section = None
|
||
for raw in self._read_lines():
|
||
line = raw.strip()
|
||
if not line:
|
||
continue
|
||
if line.startswith("[") and line.endswith("]"):
|
||
section = line[1:-1]
|
||
self.data.setdefault(section, OrderedDict())
|
||
continue
|
||
if section is None or "=" not in line:
|
||
continue
|
||
key, _, value = line.partition("=")
|
||
self.data[section][key.strip()] = value.strip()
|
||
# 补全缺失项
|
||
for sec, kv in DEFAULTS.items():
|
||
self.data.setdefault(sec, OrderedDict())
|
||
for k, v in kv.items():
|
||
self.data[sec].setdefault(k, v)
|
||
|
||
def _read_lines(self):
|
||
raw = open(self.path, "rb").read()
|
||
for enc in ("gbk", "utf-8-sig", "utf-8"):
|
||
try:
|
||
return raw.decode(enc).splitlines()
|
||
except UnicodeDecodeError:
|
||
continue
|
||
return raw.decode("gbk", "replace").splitlines()
|
||
|
||
def save(self) -> None:
|
||
lines = []
|
||
for sec in SECTION_ORDER + [s for s in self.data if s not in SECTION_ORDER]:
|
||
if sec not in self.data:
|
||
continue
|
||
lines.append(f"[{sec}]")
|
||
for k, v in self.data[sec].items():
|
||
lines.append(f"{k}={v}")
|
||
lines.append("")
|
||
with open(self.path, "wb") as f:
|
||
f.write("\r\n".join(lines).encode("gbk", "replace"))
|
||
|
||
# ------------------------------------------------------------------ 取值
|
||
def get(self, section: str, key: str, default: str = "") -> str:
|
||
return self.data.get(section, {}).get(key, default)
|
||
|
||
def set(self, section: str, key: str, value: str) -> None:
|
||
self.data.setdefault(section, OrderedDict())[key] = str(value)
|
||
|
||
def get_bool(self, section: str, key: str, default: bool = False) -> bool:
|
||
return self.get(section, key, "true" if default else "false").strip().lower() in ("1", "true", "yes", "on")
|
||
|
||
def get_int(self, section: str, key: str, default: int = 0) -> int:
|
||
try:
|
||
return int(self.get(section, key, str(default)))
|
||
except ValueError:
|
||
return default
|
||
|
||
# ------------------------------------------------------------------ 便捷
|
||
@property
|
||
def muip_host(self) -> str:
|
||
return self.get("MUIP", "IP地址", "127.0.0.1")
|
||
|
||
@property
|
||
def muip_port(self) -> int:
|
||
return self.get_int("MUIP", "端口", 20011)
|
||
|
||
@property
|
||
def muip_region(self) -> str:
|
||
return self.get("MUIP", "区服", "dev_gio")
|
||
|
||
@property
|
||
def uid(self) -> str:
|
||
return self.get("MUIP", "玩家UID", "1")
|
||
|
||
@property
|
||
def mysql_enabled(self) -> bool:
|
||
return self.get_bool("应用", "启用MySQL", False)
|
||
|
||
@property
|
||
def ssh_enabled(self) -> bool:
|
||
return self.get_bool("应用", "启用SSH", False)
|
||
|
||
@property
|
||
def hidden_pages(self) -> set:
|
||
raw = self.get("应用", "隐藏页面", "")
|
||
return {p.strip() for p in raw.replace(",", ",").split(",") if p.strip()}
|