From 9a6ddae19e7834b864b205dad246497873302e66 Mon Sep 17 00:00:00 2001 From: Kevin Lyles Date: Wed, 16 Dec 2009 02:29:23 -0600 Subject: [PATCH] Changed the metatable for normalStats and friends to support adding and give 0 for nil values --- Regexps.lua | 12 ++++++------ WeightsWatcher.lua | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/Regexps.lua b/Regexps.lua index 98021ab..9e06c74 100644 --- a/Regexps.lua +++ b/Regexps.lua @@ -67,7 +67,7 @@ MultipleStatLines = { SingleStatLines = { {"^Rune of the Stoneskin Gargoyle", function() - return {["Defense"] = 25} + return setmetatable({["Defense"] = 25}, ww_normalStatsMetatable) end}, {"^Equip: Restores (%d+) mana per 5 sec%.", function(text, pattern) @@ -94,7 +94,7 @@ SingleStatLines = { function(text, pattern) local start, _, value, name = string.find(text, pattern) if start then - return {[name] = tonumber(value)} + return setmetatable({[name] = tonumber(value)}, ww_normalStatsMetatable) end end}, @@ -143,7 +143,7 @@ SingleSlotLines = { function WeightsWatcher:multipleStats(text) local stat, stringTable - local stats = {} + local stats = setmetatale({}, ww_normalStatsMetatable) text = string.gsub(string.gsub(text, ",? and ", "\a"), ", ", "\a") stringTable = { strsplit("\a", text) } @@ -166,7 +166,7 @@ end function WeightsWatcher:damageRange(textL, textR) local speed - local stats = {} + local stats = setmetatable({}, ww_normalStatsMetatable) local start, _, added, minVal, maxVal, name = string.find(textL, "^(%+?)(%d+) %- (%d+) (%a* ?Damage)") if start then if added == "+" then @@ -200,7 +200,7 @@ function WeightsWatcher:singleStat(text) else start, _, name, value = string.find(text, regex) if start then - stat = {[name] = tonumber(value)} + stat = setmetatable({[name] = tonumber(value)}, ww_normalStatsMetatable) break end end @@ -211,6 +211,6 @@ end function WeightsWatcher:singleStatValueOnly(text, pattern, name) local start, _, value = string.find(text, pattern) if start then - return {[name] = tonumber(value)} + return setmetatable({[name] = tonumber(value)}, ww_normalStatsMetatable) end end diff --git a/WeightsWatcher.lua b/WeightsWatcher.lua index 2715837..8ae85c4 100644 --- a/WeightsWatcher.lua +++ b/WeightsWatcher.lua @@ -4,6 +4,29 @@ end currentHooks = {} +ww_normalStatsMetatable = { + -- Allows us to skip the nil check + __index = function() + return 0 + end, + __add = function(tbl1, tbl2) + local tbl = setmetatable({}, ww_normalStatsMetatable) + + if tbl ~= nil then + for k, v in pairs(tbl1) do + tbl[k] = v + end + end + if tbl2 ~= nil then + for k, v in pairs(tbl2) do + tbl[k] = tbl[k] + v + end + end + + return tbl + end, +} + ww_bareItemCacheMetatable = { __index = function(tbl, key) tbl[key] = {WeightsWatcher:getItemStats(key)} @@ -526,7 +549,7 @@ end function WeightsWatcher:getItemStats(link) local ttleft, ttright, origTextL, textL, textR, pattern, func, start - local normalStats, socketList, socketBonusStat = {}, {} + local normalStats, socketList, socketBonusStat = setmetatable({}, ww_normalStatsMetatable), {} local ranged = false -- Populate hidden tooltip @@ -595,13 +618,7 @@ function WeightsWatcher:getItemStats(link) if string.find(textL, pattern) then statsList = func(textL, textR) if statsList then - for name, value in pairs(statsList) do - if normalStats[name] then - normalStats[name] = normalStats[name] + value - else - normalStats[name] = value - end - end + normalStats = normalStats + statsList matched = true break end @@ -610,13 +627,7 @@ function WeightsWatcher:getItemStats(link) if not matched then stat = WeightsWatcher:singleStat(textL) if stat then - for name, value in pairs(stat) do - if normalStats[name] then - normalStats[name] = normalStats[name] + value - else - normalStats[name] = value - end - end + normalStats = normalStats + stat end end end -- 1.7.9.5