feat: 重构多人游戏界面和代码,使用文件存储游戏配置,重构底层代码
This commit is contained in:
59
Scripts/Utilities/GodotConfigManager.cs
Normal file
59
Scripts/Utilities/GodotConfigManager.cs
Normal file
@ -0,0 +1,59 @@
|
||||
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<string, Variant> 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<string, Variant> 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("配置文件保存失败!", "错误");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user