using Godot; using System.Collections.Generic; using System.IO; class GodotConfigManager { private readonly string ConfigFilePath; private readonly ConfigFile config = new(); private readonly bool successful; public GodotConfigManager(string ConfigFilePath) { this.ConfigFilePath = ConfigFilePath; Error err = config.Load(ConfigFilePath); if (err == Error.Ok) { successful = true; } else { successful = false; GD.PrintErr("config.Load", err); // OS.Alert("配置文件加载失败!", "错误"); return; } } public void LoadConfig(string SessionName, Dictionary data) { // 如果文件没有加载,忽略它。 if (!successful) { return; } // 迭代所有小节。 foreach (string session in config.GetSections()) { if (session == SessionName) { foreach (var key in config.GetSectionKeys(session)) { if (data.ContainsKey(key)) { data[key] = config.GetValue(session, key); } } } } } public void SaveConfig(string SessionName, Dictionary data) { // 将 GlobalConfig 中的键值对写入配置文件 foreach (var key in data.Keys) { GD.Print(data[key]); config.SetValue(SessionName, key, data[key]); } // 保存配置文件 if (config.Save(ConfigFilePath) != Error.Ok) { OS.Alert("配置文件保存失败!", "错误"); } } }