using System.Collections.Concurrent; using Newtonsoft.Json.Linq; using XFEWarframe.Core.Services; namespace XFEWarframe.Core.Data; public sealed class ItemCatalog { private readonly Dictionary items = new(StringComparer.Ordinal); private static readonly ConcurrentDictionary> Dictionaries = new(StringComparer.Ordinal); private readonly JObject translations; private readonly string language = "zh"; public ItemCatalog() { translations = Dictionary("zh"); foreach (var (file, category) in new[] { ("Warframes", "Suits"), ("Weapons", "LongGuns"), ("Sentinels", "Sentinels"), ("Resources", "MiscItems"), ("Recipes", "Recipes"), ("Upgrades", "RawUpgrades"), ("Gear", "Consumables"), ("Keys", "LevelKeys"), ("Relics", "MiscItems"), ("Flavour", "FlavourItems"), ("Customs", "WeaponSkins"), ("FusionBundles", "FusionBundles"), ("CreditBundles", "CreditBundles"), ("Bundles", "Bundles"), ("Boosters", "Boosters"), ("Arcanes", "RawUpgrades"), ("EmailItems", "EmailItems"), ("Drones", "Drones") }) foreach (var prop in GameData.Table(file).Properties()) if (prop.Value is JObject meta) items.TryAdd(prop.Name, (file == "Weapons" && meta.Value("totalDamage") == 0 ? "MiscItems" : meta.Value("productCategory") ?? category, meta)); foreach (var prop in GameData.Table("BoosterPacks").Properties()) if (prop.Value is JObject meta) items.TryAdd(prop.Name, ("BoosterPacks", meta)); foreach (var prop in GameData.Table("Avionics").Properties()) if (prop.Value is JObject meta) items.TryAdd(prop.Name, ("RawUpgrades", meta)); foreach (var prop in GameData.Table("RailjackWeapons").Properties()) if (prop.Value is JObject meta) items.TryAdd(prop.Name, ("CrewShipWeapons", meta)); foreach (var table in new[] { "Warframes", "Sentinels" }) foreach (var prop in GameData.Table(table).Properties()) foreach (var extra in prop.Value["exalted"] as JArray ?? []) items.TryAdd(extra.ToString(), ("SpecialItems", new JObject { ["excludeFromMarket"] = true })); foreach (var prop in ((JObject)WorldData.Get("supplementalUpgrades")).Properties()) if (prop.Value is JObject meta) items.TryAdd(prop.Name, ("RawUpgrades", meta)); foreach (var prop in LegacyMods.All.Properties()) items.TryAdd(prop.Name, ("RawUpgrades", (JObject)prop.Value)); foreach (var rarity in new[] { "Common", "Uncommon", "Rare", "Legendary" }) items.TryAdd("/Lotus/Upgrades/Mods/Fusers/" + rarity + "ModFuser", ("RawUpgrades", new JObject { ["rarity"] = rarity.ToUpperInvariant(), ["fusionLimit"] = rarity == "Legendary" ? 0 : 5, ["excludeFromMarket"] = true })); items.TryAdd("/Lotus/Weapons/Tenno/HackingDevices/TnHackingDevice/TnHackingDeviceWeapon", ("DataKnives", new JObject { ["excludeFromMarket"] = true })); items.TryAdd("/Lotus/Types/NeutralCreatures/ErsatzHorse/ErsatzHorsePowerSuit", ("Horses", new JObject { ["excludeFromMarket"] = true })); items.TryAdd("/Lotus/Types/Vehicles/Motorcycle/MotorcyclePowerSuit", ("Motorcycles", new JObject { ["excludeFromMarket"] = true })); foreach (var entry in items.Where(x => x.Value.Category == "SupplyDrop").ToArray()) items[entry.Key] = ("Consumables", entry.Value.Meta); foreach (var slot in ((JObject)WorldData.Get("slotPurchaseData")).Properties()) if (slot.Value["price"]?.Type == JTokenType.Integer) items.TryAdd("/Lotus/Types/SlotItems/" + slot.Name, ("SlotItems", new JObject { ["name"] = slot.Name, ["platinumCost"] = slot.Value["price"]!.DeepClone(), ["bin"] = slot.Value["bin"]!.DeepClone(), ["purchaseQuantity"] = slot.Value["amount"]!.DeepClone() })); } private ItemCatalog(ItemCatalog source, string language) { items = source.items; this.language = language; translations = Dictionary(language); } // A request gets an immutable language view; another user's language never changes this catalog. public ItemCatalog WithLanguage(string? language) { var key = language?.Trim().ToLowerInvariant() ?? "zh"; return new(this, PublicExportApi.Languages.Contains(key) ? key : "en"); } private static JObject Dictionary(string language) => Dictionaries.GetOrAdd(language, static key => new Lazy(() => { var dictionary = (JObject)Assets.Json($"export/dict.{key}.json"); dictionary.Merge(Assets.Json($"fixed_responses/supplementalDict/{key}.json")); return dictionary; })).Value; public string Localize(string key, string fallback) => translations.Value(key) ?? fallback; public string InstanceName(JObject item) { var custom = item.Value("ItemName") ?? item["Details"]?.Value("Name"); if (!string.IsNullOrWhiteSpace(custom)) return custom; var type = item.Value("ItemType") ?? "未知物品"; if (type.StartsWith(RivenCalculator.Prefix, StringComparison.Ordinal) && item["UpgradeFingerprint"] is { } fingerprint) { try { return RivenCalculator.DisplayName(this, type, RivenCalculator.Decode(fingerprint)); } catch (ApiException) { /* A malformed imported mod must not hide the rest of the inventory. */ } } try { return Name(Get(type).Meta, type); } catch (ApiException) { return type.Split('/').Last(); } } public (string Category, JObject Meta) Get(string type) { type = ResolveType(type); if ((type.StartsWith("/Lotus/Types/SlotItems/", StringComparison.Ordinal) || type.StartsWith("/Lotus/Types/StoreItems/SlotItems/", StringComparison.Ordinal)) && WorldData.Get("slotPurchaseData")[type.Split('/').Last()] is JObject slot) return ("SlotItems", new JObject { ["bin"] = slot["bin"]!.DeepClone(), ["platinumCost"] = slot["price"]!.DeepClone(), ["purchaseQuantity"] = slot["amount"]!.DeepClone() }); if (type.StartsWith("/Lotus/Types/Game/CrewShip/CrewMember/", StringComparison.Ordinal) || type.StartsWith("/Lotus/Types/JadeShadowsPart2Mission/CrewMembers/", StringComparison.Ordinal)) return ("CrewMembers", new JObject()); if (type == "/Lotus/Types/Game/CrewShip/RailJack/DefaultHarness") return ("CrewShipHarnesses", new JObject()); if (type == FocusApi.TrainingAmpType) return ("OperatorAmps", new JObject()); return items.TryGetValue(type, out var item) ? item : throw new ApiException(400, $"Unknown catalog item: {type}"); } public string ResolveType(string type) => items.ContainsKey(type) ? type : GameData.ItemType(type); public IEnumerable<(string Type, string Category, JObject Meta)> Entries => items.Select(x => (x.Key, x.Value.Category, x.Value.Meta)); public JObject Search(string query, string? category, int skip = 0, int take = 80) { var filtered = items.Where(x => (string.IsNullOrEmpty(category) || x.Value.Category == category) && (x.Key.Contains(query, StringComparison.OrdinalIgnoreCase) || Name(x.Value.Meta, x.Key).Contains(query, StringComparison.OrdinalIgnoreCase))).ToArray(); return new JObject { ["total"] = filtered.Length, ["items"] = new JArray(filtered.Skip(Math.Max(0, skip)).Take(Math.Clamp(take, 1, 200)).Select(x => new JObject { ["type"] = x.Key, ["name"] = Name(x.Value.Meta, x.Key), ["category"] = x.Value.Category, ["presentation"] = Presentation(x.Key), ["creditsCost"] = x.Value.Meta["creditsCost"], ["platinumCost"] = x.Value.Meta["platinumCost"] })) }; } public string Name(JObject meta, string fallback) { var name = meta.Value("name"); var text = name is not null ? translations.Value(name) ?? name : fallback.Split('/').Last(); text = System.Text.RegularExpressions.Regex.Replace(text, "<[^>]*>", "").Trim(); if (text.Length == 0) text = fallback.Split('/').Last(); var reason = BadReason(meta, fallback); if (reason == "starter") text += " (" + (translations.Value("/Lotus/Language/Labels/DAMAGED") ?? "Flawed") + ")"; if (reason == "frivolous") text += language == "zh" ? "(仿制物品)" : " (Imposter)"; if (LegacyVersion(fallback) is int version) text += $" (U{version})"; return text; } // These annotations describe the item; they never change its identity or saved data. public JObject Presentation(string type) { if (!items.TryGetValue(ResolveType(type), out var entry)) return new JObject(); var rarity = entry.Meta.Value("rarity") ?? ""; var reason = BadReason(entry.Meta, type); var warning = reason switch { "starter" => translations.Value("/Lotus/Language/Labels/DAMAGED") ?? "Flawed", "frivolous" => language == "zh" ? "仿制物品" : "Imposter", "notraw" => language == "zh" ? "特殊物品,不能作为普通未升级 MOD 使用" : "Special item; not a regular unranked mod", "metadataPatch" => language == "zh" ? "需要客户端元数据补丁" : "Requires a client metadata patch", "u5mod" => language == "zh" ? "U5 旧版 MOD" : "U5 legacy mod", _ => "" }; var rarityLabel = translations.Value("/Lotus/Language/Labels/" + rarity) ?? rarity; return new JObject { ["rarity"] = rarity, ["rarityLabel"] = rarityLabel, ["badReason"] = reason, ["warning"] = warning, ["legacyVersion"] = LegacyVersion(type), ["tags"] = string.Join(" · ", new[] { rarityLabel, warning, LegacyVersion(type) is int version && reason != "u5mod" ? $"U{version}" : "" }.Where(x => x.Length > 0)) }; } private static int? LegacyVersion(string type) => LegacyMods.Find(type) is not null || type is "/Lotus/Types/Recipes/KevinTestRecipe" or "/Lotus/Types/Recipes/IncendiaryRifleModBlueprint" ? 5 : null; private static string BadReason(JObject meta, string type) => LegacyMods.Find(type) is not null ? "u5mod" : meta.Value("isStarter") ? "starter" : meta.Value("isFrivolous") || GameData.Table("Arcanes")[type] is not null && meta.Value("excludeFromCodex") ? "frivolous" : meta["upgradeEntries"] is not null || meta["bayonetOtherWeaponType"] is not null && meta.Value("excludeFromMarket") ? "notraw" : WorldData.Get("supplementalSuits")[type] is not null ? "metadataPatch" : ""; }