From 5f7806a279c79da294ff4f4e5cd0b500bf2a77d8 Mon Sep 17 00:00:00 2001 From: Alexander Zeier Date: Thu, 18 Nov 2021 09:56:48 -0800 Subject: [PATCH] Cache commonly used Card properties --- HearthDb/Card.cs | 113 +++++++++++++++++++++++---------------- HearthDb/HearthDb.csproj | 1 + 2 files changed, 69 insertions(+), 45 deletions(-) diff --git a/HearthDb/Card.cs b/HearthDb/Card.cs index 4ccbfdda..a3e9bcf3 100644 --- a/HearthDb/Card.cs +++ b/HearthDb/Card.cs @@ -31,67 +31,87 @@ internal Card(Entity entity) public string FlavorText => GetLocFlavorText(DefaultLanguage); - public CardClass Class => (CardClass)Entity.GetTag(CLASS); + private CardClass? _class; + public CardClass Class => _class ??= (CardClass)Entity.GetTag(CLASS); - public Rarity Rarity => (Rarity)Entity.GetTag(RARITY); + private Rarity? _rarity; + public Rarity Rarity => _rarity ??= (Rarity)Entity.GetTag(RARITY); - public CardType Type => (CardType)Entity.GetTag(CARDTYPE); + private CardType? _type; + public CardType Type => _type ?? (CardType)Entity.GetTag(CARDTYPE); - public Race Race => (Race)Entity.GetTag(CARDRACE); + private Race? _race; + public Race Race => _race ?? (Race)Entity.GetTag(CARDRACE); + private CardSet? _set; public CardSet Set { get { - // HACK to fix missing set value on Hall of Fame cards - if(new[] + if (_set == null) { - CardIds.Collectible.Mage.IceBlock, - CardIds.Collectible.Neutral.ColdlightOracle, - CardIds.Collectible.Neutral.MoltenGiant, - - //2019 - CardIds.Collectible.Druid.Naturalize, - CardIds.Collectible.Warlock.Doomguard, - CardIds.Collectible.Paladin.DivineFavor, - CardIds.Collectible.Neutral.BakuTheMooneater, - CardIds.Collectible.Neutral.GennGreymane, - CardIds.Collectible.Druid.GloomStag, - CardIds.Collectible.Mage.BlackCat, - CardIds.Collectible.Priest.GlitterMoth, - CardIds.Collectible.Shaman.MurksparkEel, - - //2020 - CardIds.Collectible.Priest.AuchenaiSoulpriest, - CardIds.Collectible.Priest.HolyFire, - CardIds.Collectible.Priest.Shadowform, - CardIds.Collectible.Priest.ProphetVelen, - CardIds.Collectible.Priest.DivineSpirit, - CardIds.Collectible.Priest.NorthshireCleric, - CardIds.Collectible.Neutral.AcolyteOfPain, - CardIds.Collectible.Neutral.Spellbreaker, - CardIds.Collectible.Neutral.MindControlTech, - CardIds.Collectible.Neutral.MountainGiant, - CardIds.Collectible.Neutral.LeeroyJenkins, - }.Contains(Id)) - return CardSet.HOF; - return (CardSet)Entity.GetTag(CARD_SET); + // HACK to fix missing set value on Hall of Fame cards + if (new[] + { + CardIds.Collectible.Mage.IceBlock, + CardIds.Collectible.Neutral.ColdlightOracle, + CardIds.Collectible.Neutral.MoltenGiant, + + //2019 + CardIds.Collectible.Druid.Naturalize, + CardIds.Collectible.Warlock.Doomguard, + CardIds.Collectible.Paladin.DivineFavor, + CardIds.Collectible.Neutral.BakuTheMooneater, + CardIds.Collectible.Neutral.GennGreymane, + CardIds.Collectible.Druid.GloomStag, + CardIds.Collectible.Mage.BlackCat, + CardIds.Collectible.Priest.GlitterMoth, + CardIds.Collectible.Shaman.MurksparkEel, + + //2020 + CardIds.Collectible.Priest.AuchenaiSoulpriest, + CardIds.Collectible.Priest.HolyFire, + CardIds.Collectible.Priest.Shadowform, + CardIds.Collectible.Priest.ProphetVelen, + CardIds.Collectible.Priest.DivineSpirit, + CardIds.Collectible.Priest.NorthshireCleric, + CardIds.Collectible.Neutral.AcolyteOfPain, + CardIds.Collectible.Neutral.Spellbreaker, + CardIds.Collectible.Neutral.MindControlTech, + CardIds.Collectible.Neutral.MountainGiant, + CardIds.Collectible.Neutral.LeeroyJenkins, + }.Contains(Id)) + { + _set = CardSet.HOF; + } + else + { + _set = (CardSet)Entity.GetTag(CARD_SET); + } + } + return _set.Value; } } public Faction Faction => (Faction)Entity.GetTag(FACTION); - public int Cost => Entity.GetTag(COST); + private int? _cost; + public int Cost => _cost ??= Entity.GetTag(COST); - public int Attack => Entity.GetTag(ATK); + private int? _attack; + public int Attack => _attack ??= Entity.GetTag(ATK); - public int Health => Entity.GetTag(HEALTH); + private int? _health; + public int Health => _health ??= Entity.GetTag(HEALTH); - public int Durability => Entity.GetTag(DURABILITY); + private int? _durability; + public int Durability => _durability ??= Entity.GetTag(DURABILITY); - public int Armor => Entity.GetTag(ARMOR); + private int? _armor; + public int Armor => _armor ??= Entity.GetTag(ARMOR); - public int SpellSchool => Entity.GetTag(SPELL_SCHOOL); + private int? _spellSchool; + public int SpellSchool => _spellSchool ??= Entity.GetTag(SPELL_SCHOOL); public string[] Mechanics { @@ -111,7 +131,8 @@ public string[] Mechanics public Locale DefaultLanguage { get; set; } = Locale.enUS; - public bool Collectible => Convert.ToBoolean(Entity.GetTag(COLLECTIBLE)); + private bool? _collectible; + public bool Collectible => _collectible ??= Convert.ToBoolean(Entity.GetTag(COLLECTIBLE)); public string GetLocName(Locale lang) => Entity.GetLocString(CARDNAME, lang); @@ -150,8 +171,10 @@ public string GetLocText(Locale lang) public string GetLocFlavorText(Locale lang) => Entity.GetLocString(FLAVORTEXT, lang); - public bool IsWild => Helper.WildSets.Contains(Set); + private bool? _isWild; + public bool IsWild => _isWild ??= Helper.WildSets.Contains(Set); - public bool IsClassic => Helper.ClassicSets.Contains(Set); + private bool? _isClassic; + public bool IsClassic => _isClassic ??= Helper.ClassicSets.Contains(Set); } } diff --git a/HearthDb/HearthDb.csproj b/HearthDb/HearthDb.csproj index 93b6a315..5c5c29f9 100644 --- a/HearthDb/HearthDb.csproj +++ b/HearthDb/HearthDb.csproj @@ -8,6 +8,7 @@ Copyright © HearthSim 2021 21.8.0.0 21.8.0.0 + 8