Skip to content

Commit

Permalink
[Class] Current implementation of the new cache rework to reduce garbage
Browse files Browse the repository at this point in the history
  • Loading branch information
aethys256 committed Oct 14, 2017
1 parent 1e54210 commit 41d71f9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 46 deletions.
30 changes: 18 additions & 12 deletions AethysCore/Class/Spell/Cooldown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,30 @@

--- ============================ CONTENT ============================
-- Get the ChargesInfo (from GetSpellCharges) and cache it.
function Spell:GetChargesInfo ()
if not Cache.SpellInfo[self.SpellID] then Cache.SpellInfo[self.SpellID] = {}; end
do
-- charges, maxCharges, chargeStart, chargeDuration, chargeModRate
Cache.SpellInfo[self.SpellID].Charges = {GetSpellCharges(self.SpellID)};
local GetSpellCharges = GetSpellCharges;
local SpellID;
local function _GetSpellCharges () return {GetSpellCharges(SpellID)}; end
function Spell:GetChargesInfo ()
SpellID = self.SpellID;
return Cache.Get("SpellInfo", SpellID, "Charges", _GetSpellCharges);
end
end

-- Get the ChargesInfos from the Cache.
function Spell:ChargesInfo (Index)
if not Cache.SpellInfo[self.SpellID] or not Cache.SpellInfo[self.SpellID].Charges then
self:GetChargesInfo();
end
if Index then
return Cache.SpellInfo[self.SpellID].Charges[Index];
return self:GetChargesInfo()[Index];
else
return unpack(Cache.SpellInfo[self.SpellID].Charges);
return unpack(self:GetChargesInfo());
end
end

-- Get the CooldownInfo (from GetSpellCooldown) and cache it.
function Spell:CooldownInfo ()
if not Cache.SpellInfo[self.SpellID] then Cache.SpellInfo[self.SpellID] = {}; end
local SpellInfo = Cache.SpellInfo[self.SpellID];
if not SpellInfo then SpellInfo = {}; Cache.SpellInfo[self.SpellID] = SpellInfo; end
if not Cache.SpellInfo[self.SpellID].CooldownInfo then
-- start, duration, enable, modRate
Cache.SpellInfo[self.SpellID].CooldownInfo = {GetSpellCooldown(self.SpellID)};
Expand Down Expand Up @@ -83,7 +86,8 @@

-- action.foo.recharge_time or cooldown.foo.recharge_time
function Spell:Recharge (BypassRecovery)
if not Cache.SpellInfo[self.SpellID] then Cache.SpellInfo[self.SpellID] = {}; end
local SpellInfo = Cache.SpellInfo[self.SpellID];
if not SpellInfo then SpellInfo = {}; Cache.SpellInfo[self.SpellID] = SpellInfo; end
if (not BypassRecovery and not Cache.SpellInfo[self.SpellID].Recharge)
or (BypassRecovery and not Cache.SpellInfo[self.SpellID].RechargeNoRecovery) then
if BypassRecovery then
Expand All @@ -98,7 +102,8 @@
-- action.foo.charges_fractional or cooldown.foo.charges_fractional
-- TODO : Changes function to avoid using the cache directly
function Spell:ChargesFractional (BypassRecovery)
if not Cache.SpellInfo[self.SpellID] then Cache.SpellInfo[self.SpellID] = {}; end
local SpellInfo = Cache.SpellInfo[self.SpellID];
if not SpellInfo then SpellInfo = {}; Cache.SpellInfo[self.SpellID] = SpellInfo; end
if (not BypassRecovery and not Cache.SpellInfo[self.SpellID].ChargesFractional)
or (BypassRecovery and not Cache.SpellInfo[self.SpellID].ChargesFractionalNoRecovery) then
if self:Charges() == self:MaxCharges() then
Expand Down Expand Up @@ -135,7 +140,8 @@
* @returns {number}
*]]
function Spell:CooldownRemains ( BypassRecovery, Offset )
if not Cache.SpellInfo[self.SpellID] then Cache.SpellInfo[self.SpellID] = {}; end
local SpellInfo = Cache.SpellInfo[self.SpellID];
if not SpellInfo then SpellInfo = {}; Cache.SpellInfo[self.SpellID] = SpellInfo; end
local Cooldown = Cache.SpellInfo[self.SpellID].Cooldown;
local CooldownNoRecovery = Cache.SpellInfo[self.SpellID].CooldownNoRecovery;
if ( not BypassRecovery and not Cooldown ) or ( BypassRecovery and not CooldownNoRecovery ) then
Expand Down
80 changes: 58 additions & 22 deletions AethysCore/Class/Unit/Main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,31 @@
end

-- Get the unit GUID.
function Unit:GUID ()
return Cache.Get("GUIDInfo", self.UnitID,
function() return UnitGUID(self.UnitID); end);
do
-- guid
local UnitGUID = UnitGUID;
local UnitID;
local function _UnitGUID () return UnitGUID(UnitID); end
function Unit:GUID ()
UnitID = self.UnitID;
return Cache.Get("GUIDInfo", UnitID, _UnitGUID);
end
end

-- Get the unit Name.
function Unit:Name ()
local GUID = self:GUID();
if GUID then
return Cache.Get("UnitInfo", GUID, "Name",
function() return UnitName(self.UnitID); end);
do
-- name
local UnitName = UnitName;
local UnitID;
local function _UnitName () return UnitName(UnitID); end
function Unit:Name ()
local GUID = self:GUID();
if GUID then
UnitID = self.UnitID;
return Cache.Get("UnitInfo", GUID, "Name", _UnitName);
end
return nil;
end
return nil;
end

-- Get if the unit Exists and is visible.
Expand Down Expand Up @@ -98,12 +110,19 @@
end

-- Get if the unit CanAttack the other one.
function Unit:CanAttack (Other)
if self:GUID() and Other:GUID() then
return Cache.Get("UnitInfo", self:GUID(), "CanAttack", Other:GUID(),
function() return UnitCanAttack(self.UnitID, Other.UnitID) end);
do
-- canAttack
local UnitCanAttack = UnitCanAttack;
local UnitID, OtherUnitID;
local function _UnitCanAttack () return UnitCanAttack(UnitID, OtherUnitID); end
function Unit:CanAttack (Other)
local GUID, OtherGUID = self:GUID(), Other:GUID();
if GUID and OtherGUID then
UnitID, OtherUnitID = self.UnitID, Other.UnitID;
return Cache.Get("UnitInfo", GUID, "CanAttack", OtherGUID, _UnitCanAttack);
end
return nil;
end
return nil;
end

local DummyUnits = {
Expand Down Expand Up @@ -144,12 +163,19 @@
end

-- Get if the unit is a Player or not.
function Unit:IsAPlayer ()
if self:GUID() then
return Cache.Get("UnitInfo", self:GUID(), "IsAPlayer",
function() return UnitIsPlayer(self.UnitID) end);
do
-- isPlayer
local UnitIsPlayer = UnitIsPlayer;
local UnitID;
local function _UnitIsPlayer () return UnitIsPlayer(UnitID); end
function Unit:IsAPlayer ()
local GUID = self:GUID();
if GUID then
UnitID = self.UnitID;
return Cache.Get("UnitInfo", GUID, "IsAPlayer", _UnitIsPlayer);
end
return nil;
end
return nil;
end

-- Get the unit Health.
Expand Down Expand Up @@ -253,7 +279,17 @@
end

-- Get if the unit is moving or not.
function Unit:IsMoving()
return Cache.Get("UnitInfo", self:GUID(), "IsMoving",
function() return GetUnitSpeed(self.UnitID) ~= 0; end)
do
-- speed, groundSpeed, flightSpeed, swimSpeed
local GetUnitSpeed = GetUnitSpeed;
local UnitID;
local function _GetUnitSpeed () return GetUnitSpeed(UnitID) ~= 0; end
function Unit:IsMoving ()
local GUID = self:GUID();
if GUID then
UnitID = self.UnitID;
return Cache.Get("UnitInfo", GUID, "IsMoving", _GetUnitSpeed);
end
return nil;
end
end
25 changes: 16 additions & 9 deletions AethysCore/Class/Unit/Player/Instance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
-- Get the instance information about the current area.
-- Returns
-- name - Name of the instance or world area (string)
-- type - Type of the instance (string)
-- instanceType - Type of the instance (string)
-- arena - A PvP Arena instance
-- none - Normal world area (e.g. Northrend, Kalimdor, Deeprun Tram)
-- party - An instance for 5-man groups
Expand Down Expand Up @@ -58,15 +58,22 @@
-- maxPlayers - Maximum number of players allowed in the instance (number)
-- playerDifficulty - Unknown (number)
-- isDynamicInstance - True for raid instances that can support multiple maxPlayers values (10 and 25) - eg. ToC, DS, ICC, etc (boolean)
-- mapID - (number)
-- mapID - Unknown (number)
-- instanceGroupSize - maxPlayers for fixed size raids, holds the actual raid size for the new flexible raid (between (8?)10 and 25) (number)
function Player:InstanceInfo (Index)
if Index then
return Cache.Get("UnitInfo", self:GUID(), "InstanceInfo",
function() return {GetInstanceInfo()}; end)[Index];
else
return unpack(Cache.Get("UnitInfo", self:GUID(), "InstanceInfo",
function() return {GetInstanceInfo()}; end));
-- lfgID - Unknown (number)
do
-- name, instanceType, difficulty, difficultyName, maxPlayers, playerDifficulty, isDynamicInstance, mapID, instanceGroupSize, lfgID
local GetInstanceInfo = GetInstanceInfo;
local function _GetInstanceInfo () return {GetInstanceInfo()}; end
function Player:InstanceInfo ()
local Infos = Cache.Get("UnitInfo", self:GUID(), "InstanceInfo", _GetInstanceInfo);
if Infos then
if Index then
return Infos[Index];
else
return unpack(Infos);
end
end
end
end

Expand Down
11 changes: 8 additions & 3 deletions AethysCore/Class/Unit/Player/Main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@
-- Dwarf, Draenei, Gnome, Human, NightElf, Worgen
-- BloodElf, Goblin, Orc, Tauren, Troll, Scourge
-- Pandaren
function Player:Race ()
do
-- race, raceEn
return Cache.Get("UnitInfo", self:GUID(), "Race",
function() return {UnitRace(self.UnitID)}; end)[2];
local UnitRace = UnitRace;
local UnitID;
local function _UnitRace () return select(2, UnitRace(UnitID)); end
function Player:Race ()
UnitID = self.UnitID;
return Cache.Get("UnitInfo", self:GUID(), "Race", _UnitRace);
end
end

-- Get if the player is on a combat mount or not.
Expand Down

0 comments on commit 41d71f9

Please sign in to comment.