using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; namespace RynthCore.Plugin.RynthOracle.Data; /// /// Names for the numbers the Gear tab reads: spell families (portal.dat SpellBase.Category, ACE /// SpellCategory) and equipment sets (PropertyInt.EquipmentSetId 265, ACE EquipmentSet), from /// Resources\spellcategories.csv and equipmentsets.csv (Tools\RynthOracle.QuestPackGen\Make-GearNames.ps1). /// A number missing from a table still shows ("Family 812", "Set 301"). /// internal static class GearNames { private static Dictionary? _families, _sets, _materials, _masteries, _skills; /// A material ("Black Garnet") by PropertyInt.MaterialType (131); "" when unknown or none. public static string Material(int id) => Named(ref _materials, "materials.csv", id); /// A weapon mastery ("Two Handed") by PropertyInt.WeaponType (353); "" when unknown or none. public static string Mastery(int id) => Named(ref _masteries, "masteries.csv", id); /// A skill ("Melee Defense") by its number; "" when unknown. public static string Skill(int id) => Named(ref _skills, "skills.csv", id); private static string Named(ref Dictionary? table, string file, int id) { if (id <= 0) return ""; table ??= Load(file); return table.TryGetValue(id, out string? n) ? Pretty(n) : ""; } /// ACE's name for a spell family ("SlashWardProtection"), or "". public static string FamilyRaw(uint category) { _families ??= Load("spellcategories.csv"); return _families.TryGetValue((int)category, out string? n) ? n : ""; } /// /// A family for a narrow column: "Slash Ward", "Extra Fire Resistance" as "Fire Resistance", /// "Strength" for StrengthRaising (UB-ILT's FormatSpellFamilyShort, with the spaces put in). /// public static string FamilyLabel(uint category) { if (category == 0) return "?"; // no family known (ACE's 0 is "Undef") string fam = FamilyRaw(category); if (fam.Length == 0) return "Family " + category.ToString(CultureInfo.InvariantCulture); if (fam.EndsWith("Protection", StringComparison.Ordinal) && fam.Length > "Protection".Length) fam = fam[..^"Protection".Length]; else if (fam.StartsWith("Extra", StringComparison.Ordinal) && fam.EndsWith("ResistanceRaising", StringComparison.Ordinal) && fam.Length > "Extra".Length + "ResistanceRaising".Length) fam = fam["Extra".Length..^"Raising".Length]; else if (fam.EndsWith("Raising", StringComparison.Ordinal) && fam.Length > "Raising".Length) fam = fam[..^"Raising".Length]; return Pretty(fam); } /// An equipment set's name ("Shimmering Shadows Set", "Paragon Melee"); "-" for none. public static string SetName(int setId) { if (setId <= 0) return "-"; _sets ??= Load("equipmentsets.csv"); return _sets.TryGetValue(setId, out string? n) ? Pretty(n) : "Set " + setId.ToString(CultureInfo.InvariantCulture); } /// CamelCase to words: "ShimmeringShadowsSet" as "Shimmering Shadows Set", "PKDamage" as "PK Damage". public static string Pretty(string camel) { if (string.IsNullOrEmpty(camel)) return ""; var sb = new StringBuilder(camel.Length + 8); for (int i = 0; i < camel.Length; i++) { char c = camel[i]; if (i > 0 && c != '_') { char p = camel[i - 1]; bool next = i + 1 < camel.Length && char.IsLower(camel[i + 1]); // A capital after a lower-case letter or a digit, or the last capital of a run before lower case. if (char.IsUpper(c) && (char.IsLower(p) || char.IsDigit(p) || (char.IsUpper(p) && next))) sb.Append(' '); else if (char.IsDigit(c) && char.IsLetter(p)) sb.Append(' '); } sb.Append(c == '_' ? ' ' : c); } return sb.ToString(); } private static Dictionary Load(string file) { var map = new Dictionary(); try { using StreamReader r = Csv.OpenEmbedded(file); r.ReadLine(); // header while (r.ReadLine() is string line) { int comma = line.IndexOf(','); if (comma <= 0) continue; if (int.TryParse(line.AsSpan(0, comma), NumberStyles.None, CultureInfo.InvariantCulture, out int id)) map[id] = line[(comma + 1)..].Trim(); } } catch { // A missing table only costs names ("Family 287"). } return map; } }