From 1e54210ba317ab85d8cb605b1c8b31cabeec32d5 Mon Sep 17 00:00:00 2001 From: Aethys256 Date: Sat, 14 Oct 2017 01:59:36 +0200 Subject: [PATCH] [Class] Small refactor --- AethysCore/Class/Main.lua | 59 +++++++-------- AethysCore/Class/Spell/Artifact.lua | 56 +++++++------- AethysCore/Class/Spell/Book.lua | 111 ++++++++++++++-------------- 3 files changed, 112 insertions(+), 114 deletions(-) diff --git a/AethysCore/Class/Main.lua b/AethysCore/Class/Main.lua index a5d17480..9e707b6f 100644 --- a/AethysCore/Class/Main.lua +++ b/AethysCore/Class/Main.lua @@ -14,31 +14,28 @@ --- ============================ CONTENT ============================ --- ======= PSEUDO-CLASS ======= - -- Defines a Class. - local function NewInstance ( self, ... ) - local Object = {}; - setmetatable( Object, self ); - Object:Constructor( ... ); - return Object; - end - function AC.Class () + local function Class () local Class = {}; Class.__index = Class; - setmetatable( Class, { __call = NewInstance } ); + setmetatable( Class, { __call = + function ( self, ... ) + local Object = {}; + setmetatable( Object, self ); + Object:New( ... ); + return Object; + end + } ); return Class; end - local Class = AC.Class; --- ======= UNIT ======= - -- Defines the Unit Class. - AC.Unit = Class(); - local Unit = AC.Unit; - -- Unit Constructor - function Unit:Constructor ( UnitID ) +do + local Unit = Class(); + AC.Unit = Unit; + function Unit:New ( UnitID ) if type( UnitID ) ~= "string" then error( "Invalid UnitID." ); end self.UnitID = UnitID; end - -- Defines Unit Objects. -- Unique Units Unit.Player = Unit( "Player" ); Unit.Pet = Unit( "Pet" ); @@ -55,21 +52,20 @@ { "Raid", 40 } }; for _, UnitID in pairs( UnitIDs ) do - local UnitType = UnitID[1]; - local UnitCount = UnitID[2]; + local UnitType = UnitID[ 1 ]; + local UnitCount = UnitID[ 2 ]; Unit[ UnitType ] = {}; for i = 1, UnitCount do - Unit[ UnitType ][ i ] = Unit( stringformat( "%s%d", UnitType, i) ); + Unit[ UnitType ][ i ] = Unit( stringformat( "%s%d", UnitType, i ) ); end end - UnitIDs = nil; +end --- ======= SPELL ======= - -- Defines the Spell Class. - AC.Spell = Class(); - local Spell = AC.Spell; - -- Spell Constructor - function Spell:Constructor ( SpellID, SpellType ) +do + local Spell = Class(); + AC.Spell = Spell; + function Spell:New ( SpellID, SpellType ) if type( SpellID ) ~= "number" then error( "Invalid SpellID." ); end if SpellType and type( SpellType ) ~= "string" then error( "Invalid Spell Type." ); end self.SpellID = SpellID; @@ -79,18 +75,19 @@ self.LastHitTime = 0; self.LastBuffTime = 0; end +end --- ======= ITEM ======= - -- Defines the Item Class. - AC.Item = Class(); - local Item = AC.Item; - -- Item Constructor - function Item:Constructor ( ItemID, ItemSlotID ) +do + local Item = Class(); + AC.Item = Item; + function Item:New ( ItemID, ItemSlotID ) if type( ItemID ) ~= "number" then error( "Invalid ItemID." ); end if ItemSlotID and type( ItemSlotID ) ~= "table" then error( "Invalid ItemSlotID." ); end self.ItemID = ItemID; - self.ItemSlotID = ItemSlotID or {0}; + self.ItemSlotID = ItemSlotID or { 0 }; self.LastCastTime = 0; self.LastDisplayTime = 0; self.LastHitTime = 0; end +end diff --git a/AethysCore/Class/Spell/Artifact.lua b/AethysCore/Class/Spell/Artifact.lua index 26ef2859..ae9cf97c 100644 --- a/AethysCore/Class/Spell/Artifact.lua +++ b/AethysCore/Class/Spell/Artifact.lua @@ -15,56 +15,56 @@ local pairs = pairs; local wipe = table.wipe; -- File Locals - + local PowerTableByPowerID, PowerTableBySpellID = {}, {}; --- ============================ CONTENT ============================ - --- Artifact Traits Scan - -- Get every traits informations and stores them. - local ArtifactUI = _G.C_ArtifactUI; - local HasArtifactEquipped, SocketInventoryItem = _G.HasArtifactEquipped, _G.SocketInventoryItem; - local ArtifactFrame = _G.ArtifactFrame; - local Powers, PowerTableByPowerID, PowerTableBySpellID = {}, {}, {}; + -- Get every traits informations and stores them. + do + local GetPowers, GetPowerInfo, Clear = C_ArtifactUI.GetPowers, C_ArtifactUI.GetPowerInfo, C_ArtifactUI.Clear; + local HasArtifactEquipped, SocketInventoryItem = HasArtifactEquipped, SocketInventoryItem; + local UIParent = UIParent; --local PowerTable = {}; -- Uncomment for debug purpose in case they changes the Artifact API function Spell:ArtifactScan () - ArtifactFrame = _G.ArtifactFrame; + local ArtifactFrame = ArtifactFrame; -- Does the scan only if the artifact is equipped and the artifact frame not opened. if HasArtifactEquipped() and not (ArtifactFrame and ArtifactFrame:IsShown()) then -- Unregister the event to prevent unwanted call(s). UIParent:UnregisterEvent("ARTIFACT_UPDATE"); SocketInventoryItem(INVSLOT_MAINHAND); - Powers = ArtifactUI.GetPowers(); + local Powers = GetPowers(); if Powers then - --wipe(PowerTable); wipe(PowerTableByPowerID); wipe(PowerTableBySpellID); - local PowerInfo; - for Index, Power in pairs(Powers) do + for _, Power in pairs(Powers) do -- GetPowerInfo() returns a table and not multiple values unlike most WoW API. -- offset, prereqsMet, cost, bonusRanks, maxRanks, linearIndex, position, isFinal, numMaxRankBonusFromTier, tier, isGoldMedal, isStart, currentRank, spellID - PowerInfo = ArtifactUI.GetPowerInfo(Power); + local PowerInfo = GetPowerInfo(Power); PowerTableByPowerID[Power] = PowerInfo; PowerTableBySpellID[PowerInfo.spellID] = PowerInfo; end end - ArtifactUI.Clear(); + Clear(); -- Register back the event. UIParent:RegisterEvent("ARTIFACT_UPDATE"); end end + end - -- artifact.foo.rank - function Spell:ArtifactRank () - return PowerTableBySpellID[self.SpellID] and PowerTableBySpellID[self.SpellID].currentRank or 0; - end - function Spell:ArtifactRankPowerID () - return PowerTableByPowerID[self.SpellID] and PowerTableByPowerID[self.SpellID].currentRank or 0; - end + -- artifact.foo.rank + function Spell:ArtifactRank () + local Power = PowerTableBySpellID[self.SpellID]; + return Power and Power.currentRank or 0; + end + function Spell:ArtifactRankPowerID () + local Power = PowerTableByPowerID[self.SpellID]; + return Power and Power.currentRank or 0; + end - -- artifact.foo.enabled - function Spell:ArtifactEnabled () - return self:ArtifactRank() > 0; - end - function Spell:ArtifactEnabledPowerID () - return self:ArtifactRankPowerID() > 0; - end + -- artifact.foo.enabled + function Spell:ArtifactEnabled () + return self:ArtifactRank() > 0; + end + function Spell:ArtifactEnabledPowerID () + return self:ArtifactRankPowerID() > 0; + end diff --git a/AethysCore/Class/Spell/Book.lua b/AethysCore/Class/Spell/Book.lua index dd5c4425..e0bc92c4 100644 --- a/AethysCore/Class/Spell/Book.lua +++ b/AethysCore/Class/Spell/Book.lua @@ -13,6 +13,9 @@ local Item = AC.Item; -- Lua local select = select; + -- WoW API + local GetSpellInfo, GetSpellTabInfo = GetSpellInfo, GetSpellTabInfo; + local BOOKTYPE_PET, BOOKTYPE_SPELL = BOOKTYPE_PET, BOOKTYPE_SPELL; -- File Locals @@ -20,26 +23,26 @@ --- ============================ CONTENT ============================ -- Get the spell BookIndex along with BookType. function Spell:BookIndex () - local CurrentSpellID; -- Pet Book - local NumPetSpells = HasPetSpells(); - if NumPetSpells then - for i = 1, NumPetSpells do - CurrentSpellID = select(7, GetSpellInfo(i, BOOKTYPE_PET)); - if CurrentSpellID and CurrentSpellID == self:ID() then - return i, BOOKTYPE_PET; + do + local NumPetSpells = HasPetSpells(); + if NumPetSpells then + for i = 1, NumPetSpells do + local CurrentSpellID = select(7, GetSpellInfo(i, BOOKTYPE_PET)); + if CurrentSpellID and CurrentSpellID == self:ID() then + return i, BOOKTYPE_PET; + end end end end -- Player Book - local Offset, NumSpells, OffSpec; for i = 1, GetNumSpellTabs() do - Offset, NumSpells, _, OffSpec = select(3, GetSpellTabInfo(i)); + local Offset, NumSpells, _, OffSpec = select(3, GetSpellTabInfo(i)); -- GetSpellTabInfo has been updated, it now returns the OffSpec ID. -- If the OffSpec ID is set to 0, then it's the Main Spec. if OffSpec == 0 then for j = 1, (Offset + NumSpells) do - CurrentSpellID = select(7, GetSpellInfo(j, BOOKTYPE_SPELL)); + local CurrentSpellID = select(7, GetSpellInfo(j, BOOKTYPE_SPELL)); if CurrentSpellID and CurrentSpellID == self:ID() then return j, BOOKTYPE_SPELL; end @@ -50,61 +53,59 @@ -- Scan the Book to cache every Spell Learned. function Spell:BookScan (BlankScan) - local CurrentSpellID, CurrentSpell; + local SpellLearned = Cache.Persistent.SpellLearned; + -- Pet Book - local NumPetSpells = HasPetSpells(); - if NumPetSpells then - for i = 1, NumPetSpells do - CurrentSpellID = select(7, GetSpellInfo(i, BOOKTYPE_PET)) - if CurrentSpellID then - CurrentSpell = Spell(CurrentSpellID, "Pet"); - if CurrentSpell:IsAvailable(true) and (CurrentSpell:IsKnown( true ) or IsTalentSpell(i, BOOKTYPE_PET)) then - if not BlankScan then - Cache.Persistent.SpellLearned.Pet[CurrentSpell:ID()] = true; + do + local NumPetSpells = HasPetSpells(); + if NumPetSpells then + local SpellLearned = SpellLearned.Pet; + local IsTalentSpell = IsTalentSpell; + for i = 1, NumPetSpells do + local CurrentSpellID = select(7, GetSpellInfo(i, BOOKTYPE_PET)) + if CurrentSpellID then + local CurrentSpell = Spell(CurrentSpellID, "Pet"); + if CurrentSpell:IsAvailable(true) and (CurrentSpell:IsKnown(true) or IsTalentSpell(i, BOOKTYPE_PET)) then + if not BlankScan then + SpellLearned[CurrentSpell:ID()] = true; + end end end end end end - -- Player Book (except Flyout Spells) - local Offset, NumSpells, OffSpec; - for i = 1, GetNumSpellTabs() do - Offset, NumSpells, _, OffSpec = select(3, GetSpellTabInfo(i)); - -- GetSpellTabInfo has been updated, it now returns the OffSpec ID. - -- If the OffSpec ID is set to 0, then it's the Main Spec. - if OffSpec == 0 then - for j = 1, (Offset + NumSpells) do - CurrentSpellID = select(7, GetSpellInfo(j, BOOKTYPE_SPELL)) - if CurrentSpellID and GetSpellBookItemInfo(j, BOOKTYPE_SPELL) == "SPELL" then - --[[ Debug Code - CurrentSpell = Spell(CurrentSpellID); - print( - tostring(CurrentSpell:ID()) .. " | " .. - tostring(CurrentSpell:Name()) .. " | " .. - tostring(CurrentSpell:IsAvailable()) .. " | " .. - tostring(CurrentSpell:IsKnown()) .. " | " .. - tostring(IsTalentSpell(j, BOOKTYPE_SPELL)) .. " | " .. - tostring(GetSpellBookItemInfo(j, BOOKTYPE_SPELL)) .. " | " .. - tostring(GetSpellLevelLearned(CurrentSpell:ID())) - ); - ]] - if not BlankScan then - Cache.Persistent.SpellLearned.Player[CurrentSpellID] = true; + -- Player Book + do + local SpellLearned = SpellLearned.Player; + + local GetSpellBookItemInfo = GetSpellBookItemInfo; + for i = 1, GetNumSpellTabs() do + local Offset, NumSpells, _, OffSpec = select(3, GetSpellTabInfo(i)); + -- GetSpellTabInfo has been updated, it now returns the OffSpec ID. + -- If the OffSpec ID is set to 0, then it's the Main Spec. + if OffSpec == 0 then + for j = 1, (Offset + NumSpells) do + local CurrentSpellID = select(7, GetSpellInfo(j, BOOKTYPE_SPELL)) + if CurrentSpellID and GetSpellBookItemInfo(j, BOOKTYPE_SPELL) == "SPELL" then + if not BlankScan then + SpellLearned[CurrentSpellID] = true; + end end end end end - end - -- Flyout Spells - local FlyoutID, NumSlots, IsKnown, IsKnownSpell; - for i = 1, GetNumFlyouts() do - FlyoutID = GetFlyoutID(i); - NumSlots, IsKnown = select(3, GetFlyoutInfo(FlyoutID)); - if IsKnown and NumSlots > 0 then - for j = 1, NumSlots do - CurrentSpellID, _, IsKnownSpell = GetFlyoutSlotInfo(FlyoutID, j); - if CurrentSpellID and IsKnownSpell then - Cache.Persistent.SpellLearned.Player[CurrentSpellID] = true; + + -- Flyout Spells + local GetFlyoutInfo, GetFlyoutSlotInfo = GetFlyoutInfo, GetFlyoutSlotInfo; + for i = 1, GetNumFlyouts() do + local FlyoutID = GetFlyoutID(i); + local NumSlots, IsKnown = select(3, GetFlyoutInfo(FlyoutID)); + if IsKnown and NumSlots > 0 then + for j = 1, NumSlots do + local CurrentSpellID, _, IsKnownSpell = GetFlyoutSlotInfo(FlyoutID, j); + if CurrentSpellID and IsKnownSpell then + SpellLearned[CurrentSpellID] = true; + end end end end