using System.Security.Cryptography; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using XFEWarframe.Core.Data; namespace XFEWarframe.Core.Services; public static class ServerSettingsEditor { private static readonly Lazy Schema = new(() => (JArray)Assets.Json("editor/settings.json")["groups"]!["server"]!); private static readonly HashSet Pending = []; public static readonly string[] CircuitModes = ["Survival", "VoidFlood", "Excavation", "Defense", "Exterminate", "Assassination", "Alchemy"]; private static readonly string[] CircuitLabels = ["生存", "虚空洪流", "挖掘", "防御", "歼灭", "刺杀", "炼金术"]; public static bool Knows(string key) => Schema.Value.Any(x => x.Value("key") == key); public static string Version(JObject state) => Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(new JObject { ["config"] = state["config"]?.DeepClone(), ["world"] = state["worldState"]?.DeepClone() }.ToString(Formatting.None)))); private static JObject Field(JObject source, JObject state, ServerOptions options) { var field = (JObject)source.DeepClone(); var key = field.RequiredString("key"); var type = field.RequiredString("type"); JToken? value = key.StartsWith("worldState.", StringComparison.Ordinal) ? state["worldState"]?[key[11..]] : state["config"]?.SelectToken(key); if (key == "noHubDiscrimination") value ??= new JValue(options.NoHubDiscrimination); if (key == "fullyStockedVendors") value ??= state["worldState"]?["fullyStockedVendors"]; if (key == "worldState.officialEventsEnabled") value ??= new JValue(options.SyncOfficialEvents); if (key == "worldState.nightwaveOverride") field["options"] = new JArray(new JObject { ["label"] = "按游戏版本自动选择", ["value"] = JValue.CreateNull() }, new JObject { ["label"] = "关闭午夜电波", ["value"] = "disabled" }); if (key == "worldState.nightwaveOverride") foreach (var entry in ((JObject)WorldData.Get("nightwaveTagToSeasonName")).Properties()) field.Array("options").Add(new JObject { ["label"] = entry.Name, ["value"] = entry.Name }); if (key == "worldState.varziaOverride") field["options"] = new JArray(new[] { new JObject { ["label"] = "自动轮换", ["value"] = JValue.CreateNull() } }.Concat(((JObject)WorldData.Get("varzia")["primeDualPacks"]!).Properties().Select(x => new JObject { ["label"] = x.Name, ["value"] = x.Name }))); if (key == "worldState.circuitGameModes") field["options"] = new JArray(CircuitModes.Select((mode, i) => new JObject { ["label"] = CircuitLabels[i], ["value"] = mode })); field["value"] = value?.DeepClone() ?? (type switch { "bool" => new JValue(false), "strings" => new JArray(), "choice" => field["options"]?[0]?["value"]?.DeepClone() ?? JValue.CreateNull(), _ => new JValue(key is "worldState.nightwaveEpisode" or "worldState.darvoStockMultiplier" ? 1 : 0) }); return field; } public static JObject Read(JObject state, ServerOptions options) => new() { ["version"] = Version(state), ["total"] = Schema.Value.Count, ["fields"] = new JArray(Schema.Value.OfType().Where(x => !Pending.Contains(x.RequiredString("key"))).Select(x => Field(x, state, options))), ["pending"] = new JArray(Schema.Value.OfType().Where(x => Pending.Contains(x.RequiredString("key"))).Select(x => new JObject { ["key"] = x["key"]!.DeepClone(), ["label"] = x["label"]!.DeepClone() })) }; public static JObject Save(JObject state, JObject request, ServerOptions options) { if (!Wire.SecretEquals(request.RequiredString("version", 64), Version(state))) throw new ApiException(409, "服务器设置已被其他操作更新,请刷新后再保存。"); var values = request["values"] as JObject ?? throw new ApiException(400, "缺少设置值。"); if (values.Count < 1 || values.Count > Schema.Value.Count) throw new ApiException(400, "设置数量无效。"); foreach (var value in values.Properties()) Set(state, value.Name, value.Value, options); return Read(state, options); } public static void Set(JObject state, string key, JToken value, ServerOptions options) { var source = Schema.Value.OfType().FirstOrDefault(x => x.Value("key") == key) ?? throw new ApiException(400, "未知服务器设置。"); if (Pending.Contains(key)) throw new ApiException(501, "该设置的游戏行为尚未完成迁移。"); var field = Field(source, state, options); if (field.Value("type") == "strings") { if (value is not JArray modes || modes.Count > 30 || modes.Any(x => x.Type != JTokenType.String || !CircuitModes.Contains(x.ToString()))) throw new ApiException(400, "请选择合法的回廊任务类型,最多 30 项。"); } else AccountEditor.Validate(field, value); var root = key.StartsWith("worldState.", StringComparison.Ordinal) ? state.Obj("worldState") : state.Obj("config"); var path = (key.StartsWith("worldState.", StringComparison.Ordinal) ? key[11..] : key).Split('.'); foreach (var part in path.SkipLast(1)) root = root.Obj(part); if (value.Type == JTokenType.Null || value is JArray { Count: 0 }) root.Remove(path[^1]); else root[path[^1]] = value.DeepClone(); } }