XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
using Newtonsoft.Json.Linq;
using XFEWarframe.Core.Data;

namespace XFEWarframe.Core.Services;

/// <summary>Versioned extension to the game's single AbilityOverride, scoped to one owned configuration.</summary>
public static class ExperimentalAbilities
{
    public const string Field = "XFEAbilityOverrides";
    public const string BanksField = "XFEAbilityBanks";

    public static JObject ReadBanks(JToken? config) => config?[BanksField] is JObject banks
        ? NormalizeBanks(banks)
        : new JObject(Read(config).Properties().Select(p => new JProperty(p.Name, new JObject { ["0"] = p.Value.DeepClone() })));

    public static JObject NormalizeBanks(JObject input)
    {
        if (input.Count > 4 || input.Properties().Any(p => p.Name is not ("0" or "1" or "2" or "3")))
            throw new ApiException(400, "技能槽必须为 1–4。");
        var result = new JObject();
        foreach (var slot in input.Properties().OrderBy(p => p.Name, StringComparer.Ordinal))
        {
            if (slot.Value is not JObject bank || bank.Count is < 1 or > 3 ||
                !bank.Properties().Select(p => p.Name).Order(StringComparer.Ordinal).SequenceEqual(Enumerable.Range(0, bank.Count).Select(i => i.ToString())))
                throw new ApiException(400, "每个技能槽可装载 1–3 个技能,顺序必须连续。");
            var normalized = new JObject(); var seen = new HashSet<string>(StringComparer.Ordinal);
            for (var index = 0; index < bank.Count; index++)
            {
                var value = bank[index.ToString()];
                if (value?.Type != JTokenType.String || !Known(value.ToString()) || !seen.Add(value.ToString()))
                    throw new ApiException(400, "请从候选项选择技能,同一槽位内不能重复。");
                normalized[index.ToString()] = value.DeepClone();
            }
            result[slot.Name] = normalized;
        }
        return result;
    }

    public static void SetBanks(JObject config, JObject input)
    {
        var banks = NormalizeBanks(input);
        var selected = new JObject(banks.Properties().Select(p => new JProperty(p.Name, p.Value["0"]!.DeepClone())));
        Set(config, selected);
        if (banks.Properties().Any(p => p.Value.Count() > 1))
        {
            // Keep the first choice readable by the previous four-slot component.
            config[Field] = selected;
            config[BanksField] = banks;
        }
    }

    public static bool Known(string ability) => GameData.Find("Abilities", ability) is not null ||
        GameData.Table("Warframes").Properties().Any(x => (x.Value["abilities"] as JArray ?? [])
            .Any(a => a.Value<string>("uniqueName") == ability));

    public static JObject Read(JToken? config)
    {
        if (config?[Field] is JObject multiple) return Normalize(multiple);
        if (config?["AbilityOverride"] is JObject single && single.Value<int>("Index") is >= 0 and < 4 &&
            single.Value<string>("Ability") is { Length: > 0 } ability)
            return new JObject { [single.Value<int>("Index").ToString()] = ability };
        return new JObject();
    }

    public static JObject Normalize(JObject input)
    {
        if (input.Count > 4 || input.Properties().Any(p => p.Name is not ("0" or "1" or "2" or "3")))
            throw new ApiException(400, "技能槽必须为 1–4,且不可重复。");
        var result = new JObject();
        foreach (var p in input.Properties().OrderBy(p => p.Name, StringComparer.Ordinal))
        {
            if (p.Value.Type != JTokenType.String) throw new ApiException(400, "技能必须从候选项选择。");
            var ability = p.Value.ToString();
            if (ability.Length == 0) continue;
            if (!Known(ability)) throw new ApiException(400, "未知技能。");
            result[p.Name] = ability;
        }
        return result;
    }

    public static void Set(JObject config, JObject input)
    {
        var selected = Normalize(input); // Validate the complete plan before changing the save.
        config.Remove(BanksField);
        config.Remove(Field);
        config.Remove("AbilityOverride");
        if (selected.Count == 0) return;
        var first = selected.Properties().First();
        config["AbilityOverride"] = new JObject { ["Ability"] = first.Value.DeepClone(), ["Index"] = int.Parse(first.Name) };
        // Older components continue to see a valid single override. They cannot apply the extension.
        if (selected.Count > 1) config[Field] = selected;
    }

    public static void PreserveOnGameSave(JObject incoming, JToken? prior)
    {
        // The game neither owns nor edits this extension. Never accept a forged map through saveLoadout.
        incoming.Remove(Field);
        incoming.Remove(BanksField);
        if (prior?[Field] is not JObject saved) return;
        incoming[Field] = saved.DeepClone();
        if (prior?[BanksField] is JObject banks) incoming[BanksField] = banks.DeepClone();
        if (prior?["AbilityOverride"] is JToken single) incoming["AbilityOverride"] = single.DeepClone();
        else incoming.Remove("AbilityOverride");
    }
}