using Newtonsoft.Json.Linq; namespace XFEWarframe.Desktop.Editing; public sealed record EditorCategory(string Key, string Label) { public string Label { get; init; } = UiText.Translate(Label); } public sealed record EditorItem(string Key, string Name, string Detail, string Tags = "", bool CanEdit = true); public sealed record EditorOption(JToken Value, string Label) { public string Label { get; init; } = UiText.Translate(Label); public string Icon { get; init; } = ""; } public sealed record EditorAction(string Key, string Label, string Description, JArray Fields, string Preview) { public string Label { get; init; } = UiText.Translate(Label); public string Description { get; init; } = UiText.Translate(Description); } public sealed class EditorField(JObject schema) { public string Key { get; } = schema.Value("key") ?? ""; public string Label { get; } = UiText.Translate(schema.Value("label") ?? ""); public string Kind { get; } = schema.Value("type") ?? "text"; public double Minimum { get; } = schema.Value("min") ?? 0; public double Maximum { get; } = schema.Value("max") ?? 1000000; public double Step { get; } = schema.Value("step") ?? 1; public bool Integer { get; } = schema.Value("integer") ?? false; public JArray Ranges { get; } = schema["ranges"] as JArray ?? []; public IReadOnlyList Options { get; } = (schema["options"] as JArray ?? []).Select(x => new EditorOption(x["value"]!.DeepClone(), x.Value("label") ?? "") { Icon = x.Value("icon") ?? "" }).ToArray(); private readonly JToken original = schema["value"]?.DeepClone() ?? JValue.CreateNull(); private JToken value = schema["value"]?.DeepClone() ?? JValue.CreateNull(); public JToken Value { get => value; set { if (JToken.DeepEquals(this.value, value)) return; this.value = value; ValueChanged?.Invoke(this, EventArgs.Empty); } } public event EventHandler? ValueChanged; public string ChoiceQuery { get; set; } = ""; private bool hasUnresolvedChoice; public bool HasUnresolvedChoice { get => hasUnresolvedChoice; set { if (hasUnresolvedChoice == value) return; hasUnresolvedChoice = value; ValueChanged?.Invoke(this, EventArgs.Empty); } } public bool Changed => HasUnresolvedChoice || !JToken.DeepEquals(Value, original); public void Reset() { ChoiceQuery = ""; HasUnresolvedChoice = false; Value = original.DeepClone(); } public string DisplayValue => Kind switch { "bool" => UiText.Translate(Value.Value() ? "开启" : "关闭"), "choice" => Options.FirstOrDefault(x => JToken.DeepEquals(x.Value, Value))?.Label ?? Value.ToString(), "date" => Value.Value() == 0 ? UiText.Translate("未设置") : DateTimeOffset.FromUnixTimeMilliseconds(Value.Value()).ToLocalTime().ToString("g"), _ => Value.ToString(Newtonsoft.Json.Formatting.None).Trim('"') }; public JToken Serialize() { if (Kind == "choice" && (HasUnresolvedChoice || !Options.Any(x => JToken.DeepEquals(x.Value, Value)))) throw new InvalidOperationException(Label + ":请选择列表中的选项。"); if (Kind is "number" or "date" or "stat") { var number = Value.Value(); if (!double.IsFinite(number) || number < Minimum || number > Maximum || Integer && number != Math.Truncate(number)) throw new InvalidOperationException(Label + " 必须在允许范围内" + (Integer ? ",且为整数。" : "。")); return Integer ? new JValue((long)number) : new JValue(number); } return Value.DeepClone(); } }