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

XFEWarframe

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

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json.Serialization;

namespace XFEWarframe.Client.Runtime;

public sealed record NativeCardIdentity(string Type, string Fingerprint)
{
    public NativeCardIdentity ToGame()
    {
        // Old imported inventories used this pre-Golem resource name. The
        // current client's Arcane Fury artwork lives at its canonical path.
        if (Type == "/Lotus/Upgrades/CosmeticEnhancers/Offensive/MeleeDamageOnCrit")
            return new("/Lotus/Upgrades/CosmeticEnhancers/Offensive/GolemArcaneMeleeDamageOnCrit", Fingerprint);
        if (Type != "/Lotus/Upgrades/XFEWarframe/InfestationKing" && Type != XFEWarframe.Mods.PetFetchMod.Type) return this;
        var data = System.Text.Json.Nodes.JsonNode.Parse(Fingerprint) as System.Text.Json.Nodes.JsonObject
            ?? throw new InvalidDataException("Invalid custom card fingerprint.");
        data["xfe"] = Type == XFEWarframe.Mods.PetFetchMod.Type ? XFEWarframe.Mods.PetFetchMod.Marker : "InfestationKing";
        return new(Type == XFEWarframe.Mods.PetFetchMod.Type ? XFEWarframe.Mods.PetFetchMod.Carrier : "/Lotus/Powersuits/Infestation/InfestPassiveAugmentCard", data.ToJsonString());
    }
    public static NativeCardIdentity FromGame(string type, string fingerprint)
    {
        if (type == XFEWarframe.Mods.PetFetchMod.Carrier && XFEWarframe.Mods.PetFetchMod.HasMarker(fingerprint))
        {
            var custom = (System.Text.Json.Nodes.JsonObject)System.Text.Json.Nodes.JsonNode.Parse(fingerprint)!;
            custom.Remove("xfe");
            return new(XFEWarframe.Mods.PetFetchMod.Type, custom.ToJsonString());
        }
        // The separate custom card is projected onto a native carrier for the game.
        // Cache the rendered custom face under its persistent server identity.
        if (type == "/Lotus/Powersuits/Infestation/InfestPassiveAugmentCard" &&
            System.Text.Json.Nodes.JsonNode.Parse(fingerprint) is System.Text.Json.Nodes.JsonObject data &&
            data["xfe"]?.GetValue<string>() == "InfestationKing")
        {
            data.Remove("xfe");
            return new("/Lotus/Upgrades/XFEWarframe/InfestationKing", data.ToJsonString());
        }
        return new(type, fingerprint);
    }
}
public sealed record NativeCardCacheRequest(string Language, NativeCardIdentity[] Cards, bool RefreshExisting = false)
{
    public const int MaximumFingerprintLength = 16384;
    public const int MaximumBatchBytes = 65536;

    // Native rendering runs in groups of 20. Pack HTTP requests by their UTF-8
    // size so unusually long Riven attributes cannot reject an entire batch.
    public static IEnumerable<NativeCardCacheRequest> Batches(string language, IEnumerable<NativeCardIdentity> cards, bool refreshExisting = false)
    {
        var batch = new List<NativeCardIdentity>();
        var envelopeBytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(new NativeCardCacheRequest(language, [], refreshExisting), VisualCacheJson.Default.NativeCardCacheRequest).Length;
        var bytes = envelopeBytes;
        foreach (var card in cards)
        {
            if (card.Fingerprint.Length > MaximumFingerprintLength) throw new InvalidDataException("原版卡面属性数据过长。");
            var cardBytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(card, VisualCacheJson.Default.NativeCardIdentity).Length;
            if (envelopeBytes + cardBytes > MaximumBatchBytes) throw new InvalidDataException("原版卡面请求过大。");
            if (batch.Count == 200 || bytes + cardBytes + (batch.Count == 0 ? 0 : 1) > MaximumBatchBytes)
            {
                yield return new(language, batch.ToArray(), refreshExisting);
                batch.Clear(); bytes = envelopeBytes;
            }
            bytes += cardBytes + (batch.Count == 0 ? 0 : 1); batch.Add(card);
        }
        if (batch.Count > 0) yield return new(language, batch.ToArray(), refreshExisting);
    }
}
public sealed record NativeCardCacheStatus(bool Available, int Pending, int Writing, string Language, string Build, string Source, string Message,
    string Reason = "", int Completed = 0, int Failed = 0);

[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(NativeCardIdentity))]
[JsonSerializable(typeof(NativeCardCacheRequest))]
[JsonSerializable(typeof(NativeCardCacheStatus))]
public partial class VisualCacheJson : JsonSerializerContext;