update: 更新到godot4.4.1,并大量重构代码

This commit is contained in:
ZZY
2025-06-09 18:17:06 +08:00
parent b27abb55a2
commit 3fa39fc71e
39 changed files with 801 additions and 790 deletions

View File

@ -1,6 +1,5 @@
using Godot;
using System.Collections.Generic;
using System.IO;
class GodotConfigManager {
private readonly string ConfigFilePath;
@ -41,8 +40,8 @@ class GodotConfigManager {
}
}
public void SaveConfig(string SessionName,
Dictionary<string, Variant> data) {
public void SaveConfig(string SessionName, Dictionary<string, Variant> data)
{
// 将 GlobalConfig 中的键值对写入配置文件
foreach (var key in data.Keys)
{

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
public interface IConfigManager {
void SetFilePath(string filePath);
void SetDefault(Dictionary<string, object> defalutConfig);
T GetValue<T>(string key);
void SetValue<T>(string key, [NotNull]T value) {
ArgumentNullException.ThrowIfNull(key);
SetValue(key, (object?)value);
}
void SetValue(string key, [NotNull]object? value);
bool HasKey(string key);
void SaveToFile();
void LoadFromFile();
}

View File

@ -0,0 +1 @@
uid://b8jvplvi4a7iv

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text.Json;
public class JsonConfigManager : IConfigManager {
private string? filePath;
private Dictionary<string, object> config = [];
private Dictionary<string, object>? defaultConfig;
public T GetValue<T>(string key) {
if (config.TryGetValue(key, out object? val)) {
if (val is T t) {
return t;
} else if (val is JsonElement element) {
return element.Deserialize<T>() ??
throw new InvalidCastException($"无法将键'{key}'的值转换为类型{typeof(T).Name}");
}
}
if (
defaultConfig != null &&
defaultConfig.TryGetValue(key, out object? dval) &&
dval is T ret) {
return ret;
}
throw new KeyNotFoundException($"Key '{key}' not found in config.");
}
public bool HasKey(string key) => config.ContainsKey(key);
public void SaveToFile() {
ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
File.WriteAllText(filePath, JsonSerializer.Serialize(config));
}
public void LoadFromFile() {
ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
if (!File.Exists(filePath)) {
return;
}
config = JsonSerializer.Deserialize
<Dictionary<string, object>>(File.ReadAllText(filePath)) ?? [];
}
public void SetDefault(Dictionary<string, object> defaultConfig) => this.defaultConfig = defaultConfig;
public void SetFilePath(string filePath) => this.filePath = filePath;
public void SetValue(string key, [NotNull]object? value) {
ArgumentNullException.ThrowIfNull(value);
config[key] = value;
}
}

View File

@ -0,0 +1 @@
uid://ddvrwflje2x87