Skip to content

Commit 54f1738

Browse files
committed
refactor: improve weight data loading and table creation in LibObjectiveProgress
1 parent a397b84 commit 54f1738

1 file changed

Lines changed: 32 additions & 13 deletions

File tree

LibObjectiveProgress-1.0.lua

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
--[[
2-
LibObjectiveProgress: Core API
3-
Developed by: Simca@Malfurion (MMOSimca)
4-
Updated by: Dack
5-
]]--
1+
-- -----------------------------------------------------------------------------
2+
-- LibObjectiveProgress: Core API
3+
-- Developed by: Simca@Malfurion (MMOSimca)
4+
-- Updated by: Dack
5+
-- -----------------------------------------------------------------------------
66

77
-- Set major/minor version
88
local MAJOR, MINOR = "LibObjectiveProgress", @project-revision@
@@ -13,13 +13,25 @@ local LOP, oldversion = LibStub:NewLibrary(MAJOR, MINOR)
1313
if not LOP then return end
1414

1515
-- Localized function references
16+
local table_create = _G.table.create
1617
local CQL_GetNumQuestLogEntries = _G.C_QuestLog.GetNumQuestLogEntries
1718
local CQL_GetInfo = _G.C_QuestLog.GetInfo
1819

20+
--- Precreate a table for generated weight data (``table.create``).
21+
--- @param arraySizeHint number
22+
--- @param hashSizeHint? number # default 0
23+
--- @return table
24+
function LOP:CreateWeightsTable(arraySizeHint, hashSizeHint)
25+
return table_create(arraySizeHint, hashSizeHint or 0)
26+
end
1927

2028
function LOP:GetNPCWeightByMap(mapID, npcID, isTeeming, isAlternate)
21-
-- Load map-based weight data if needed
22-
if not LOP.MapBasedWeights then LOP:LoadWeightDataByMap() end
29+
-- MapBasedWeights is preallocated below; loaders replace or fill it. Use a flag so we still run
30+
-- LoadWeightDataByMap when the root table already exists but data has not been loaded yet.
31+
if not LOP._weightDataByMapLoaded then
32+
LOP:LoadWeightDataByMap()
33+
LOP._weightDataByMapLoaded = true
34+
end
2335

2436
-- Ensure that specified map/NPC is valid
2537
local tableIndex = 1
@@ -33,10 +45,14 @@ function LOP:GetNPCWeightByMap(mapID, npcID, isTeeming, isAlternate)
3345
return LOP.MapBasedWeights[mapID][tableIndex][npcID]
3446
end
3547

36-
3748
function LOP:GetNPCWeightByQuest(questID, npcID)
38-
-- Load map-based weight data if needed
39-
if not LOP.QuestBasedWeights then LOP:LoadWeightDataByQuest() end
49+
-- QuestBasedWeights is preallocated below. Merged data assigns per-quest keys into that root;
50+
-- stock data replaces the root. Use a load flag so we do not skip loading when the root exists
51+
-- but is still empty.
52+
if not LOP._weightDataByQuestLoaded then
53+
LOP:LoadWeightDataByQuest()
54+
LOP._weightDataByQuestLoaded = true
55+
end
4056

4157
-- Ensure that specified map / NPC is valid
4258
if not LOP.QuestBasedWeights[questID] or not LOP.QuestBasedWeights[questID][npcID] then return nil end
@@ -45,11 +61,10 @@ function LOP:GetNPCWeightByQuest(questID, npcID)
4561
return LOP.QuestBasedWeights[questID][npcID]
4662
end
4763

48-
4964
function LOP:GetNPCWeightByCurrentQuests(npcID)
5065
-- Table variable declared here
5166
local questTable = nil
52-
67+
5368
-- Get NPC weight for all quests in log
5469
local numEntries = CQL_GetNumQuestLogEntries()
5570
for questLogIndex = 1, numEntries do
@@ -64,7 +79,11 @@ function LOP:GetNPCWeightByCurrentQuests(npcID)
6479
end
6580
end
6681
end
67-
82+
6883
-- Return completed table (or nil if no quests reference the NPC in question)
6984
return questTable
7085
end
86+
87+
-- Presized hash roots for weight data loaders (hints >= distinct map IDs / quest IDs in shipped data).
88+
LOP.MapBasedWeights = LOP.MapBasedWeights or LOP:CreateWeightsTable(0, 83)
89+
LOP.QuestBasedWeights = LOP.QuestBasedWeights or LOP:CreateWeightsTable(0, 4364)

0 commit comments

Comments
 (0)