module refactoring

This commit is contained in:
Simon Hirsig
2017-11-01 15:01:41 +01:00
parent 9989ceb42f
commit 8823b34904
36 changed files with 669 additions and 646 deletions
+51 -47
View File
@@ -1,10 +1,9 @@
module 'aux'
include 'T'
local T = require 'T'
local info = require 'aux.util.info'
local money = require 'aux.util.money'
local cache = require 'aux.core.cache'
local history = require 'aux.core.history'
local stack = require 'aux.core.stack'
local post = require 'aux.core.post'
@@ -20,27 +19,30 @@ _G.aux = {
account = {},
}
M.print = vararg-function(arg)
M.print = T.vararg-function(arg)
DEFAULT_CHAT_FRAME:AddMessage(LIGHTYELLOW_FONT_COLOR_CODE .. '<aux> ' .. join(map(arg, tostring), ' '))
end
local bids_loaded
function M.get.bids_loaded() return bids_loaded end
function M.bids_loaded() return bids_loaded end
local current_owner_page
function M.get.current_owner_page() return current_owner_page end
function M.current_owner_page() return current_owner_page end
local event_frame = CreateFrame'Frame'
for event in pairs(temp-S('ADDON_LOADED', 'VARIABLES_LOADED', 'PLAYER_LOGIN', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE')) do
for event in pairs(T.temp-T.set('ADDON_LOADED', 'VARIABLES_LOADED', 'PLAYER_LOGIN', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE')) do
event_frame:RegisterEvent(event)
end
local set_handler = {}
M.handle = setmetatable({}, {__metatable=false, __newindex=function(_, k, v) set_handler[k](v) end})
do
local handlers, handlers2 = {}, {}
function M.set.LOAD(f)
function set_handler.LOAD(f)
tinsert(handlers, f)
end
function M.set.LOAD2(f)
function set_handler.LOAD2(f)
tinsert(handlers2, f)
end
event_frame:SetScript('OnEvent', function()
@@ -65,7 +67,7 @@ end
do
local cache = {}
function LOAD()
function handle.LOAD()
cache.account = aux.account
do
local key = format('%s|%s', GetCVar'realmName', UnitName'player')
@@ -78,19 +80,19 @@ do
cache.realm = aux.realm[key]
end
end
function LOAD2()
function handle.LOAD2()
do
local key = format('%s|%s', GetCVar'realmName', UnitFactionGroup'player')
aux.faction[key] = aux.faction[key] or {}
cache.faction = aux.faction[key]
end
end
for scope in pairs(temp-S('character', 'faction', 'realm', 'account')) do
for scope in pairs(T.temp-T.set('character', 'faction', 'realm', 'account')) do
local scope = scope
M[scope .. '_data'] = function(key, init)
if not cache[scope] then error('Cache for ' .. scope .. ' data not ready.', 2) end
cache[scope][key] = cache[scope][key] or {}
for k, v in pairs(init or empty) do
for k, v in pairs(init or T.empty) do
if cache[scope][key][k] == nil then
cache[scope][key][k] = v
end
@@ -102,48 +104,50 @@ end
tab_info = {}
function M.TAB(name)
local tab = O('name', name)
local env = getfenv(2)
function env.set.OPEN(f) tab.OPEN = f end
function env.set.CLOSE(f) tab.CLOSE = f end
function env.set.USE_ITEM(f) tab.USE_ITEM = f end
function env.set.CLICK_LINK(f) tab.CLICK_LINK = f end
local tab = T.map('name', name)
local tab_event = {
OPEN = function(f) tab.OPEN = f end,
CLOSE = function(f) tab.CLOSE = f end,
USE_ITEM = function(f) tab.USE_ITEM = f end,
CLICK_LINK = function(f) tab.CLICK_LINK = f end,
}
tinsert(tab_info, tab)
return setmetatable({}, {__metatable=false, __newindex=function(_, k, v) tab_event[k](v) end})
end
do
local index
function get.active_tab() return tab_info[index] end
function get_active_tab() return tab_info[index] end
function on_tab_click(i)
CloseDropDownMenus()
do (index and active_tab.CLOSE or nop)() end
do (index and get_active_tab().CLOSE or nop)() end
index = i
do (index and active_tab.OPEN or nop)() end
do (index and get_active_tab().OPEN or nop)() end
end
end
SetItemRef = vararg-function(arg)
if arg[3] ~= 'RightButton' or not index(active_tab, 'CLICK_LINK') or not strfind(arg[1], '^item:%d+') then
SetItemRef = T.vararg-function(arg)
if arg[3] ~= 'RightButton' or not index(get_active_tab(), 'CLICK_LINK') or not strfind(arg[1], '^item:%d+') then
return orig.SetItemRef(unpack(arg))
end
local item_info = info.item(tonumber(select(3, strfind(arg[1], '^item:(%d+)'))))
if item_info then
return active_tab.CLICK_LINK(item_info)
return get_active_tab().CLICK_LINK(item_info)
end
end
UseContainerItem = vararg-function(arg)
if modified or not active_tab then
UseContainerItem = T.vararg-function(arg)
if modified() or not get_active_tab() then
return orig.UseContainerItem(unpack(arg))
end
local item_info = info.container_item(arg[1], arg[2])
if item_info and active_tab.USE_ITEM then
active_tab.USE_ITEM(item_info)
if item_info and get_active_tab().USE_ITEM then
get_active_tab().USE_ITEM(item_info)
end
end
M.orig = setmetatable({[_G]=T}, {__index=function(self, key) return self[_G][key] end})
M.hook = vararg-function(arg)
M.orig = setmetatable({[_G]=T.acquire()}, {__index=function(self, key) return self[_G][key] end})
M.hook = T.vararg-function(arg)
local name, object, handler
if getn(arg) == 3 then
name, object, handler = unpack(arg)
@@ -151,7 +155,7 @@ M.hook = vararg-function(arg)
object, name, handler = _G, unpack(arg)
end
handler = handler or getfenv(3)[name]
orig[object] = orig[object] or T
orig[object] = orig[object] or T.acquire()
assert(not orig[object][name], '"' .. name .. '" is already hooked into.')
orig[object][name], object[name] = object[name], handler
return hook
@@ -159,7 +163,7 @@ end
do
local locked
function M.get.bid_in_progress() return locked end
function M.bid_in_progress() return locked end
function M.place_bid(type, index, amount, on_success)
if locked then return end
local money = GetMoney()
@@ -184,7 +188,7 @@ end
do
local locked
function M.get.cancel_in_progress() return locked end
function M.cancel_in_progress() return locked end
function M.cancel_auction(index, on_success)
if locked then return end
locked = true
@@ -204,14 +208,14 @@ do
end
end
function LOAD2()
function handle.LOAD2()
AuxFrame:SetScale(aux_scale)
end
function AUCTION_HOUSE_SHOW()
AuctionFrame:Hide()
AuxFrame:Show()
tab = 1
set_tab(1)
end
function AUCTION_HOUSE_CLOSED()
@@ -220,7 +224,7 @@ function AUCTION_HOUSE_CLOSED()
post.stop()
stack.stop()
scan.abort()
tab = nil
set_tab()
AuxFrame:Hide()
end
@@ -243,7 +247,7 @@ end
function Blizzard_AuctionUI()
AuctionFrame:UnregisterEvent('AUCTION_HOUSE_SHOW')
AuctionFrame:SetScript('OnHide', nil)
hook('ShowUIPanel', vararg-function(arg)
hook('ShowUIPanel', T.vararg-function(arg)
if arg[1] == AuctionFrame then return AuctionFrame:Show() end
return orig.ShowUIPanel(unpack(arg))
end)
@@ -260,17 +264,17 @@ do
local function hook_quest_item(f)
f:SetScript('OnMouseUp', function()
if arg1 == 'RightButton' then
if active_tab then
tab = 1
search_tab.filter = _G[this:GetName() .. 'Name']:GetText() .. '/exact'
if get_active_tab() then
set_tab(1)
search_tab.set_filter(_G[this:GetName() .. 'Name']:GetText() .. '/exact')
search_tab.execute(nil, false)
end
end
end)
end
function Blizzard_CraftUI()
hook('CraftFrame_SetSelection', vararg-function(arg)
local ret = temp-A(orig.CraftFrame_SetSelection(unpack(arg)))
hook('CraftFrame_SetSelection', T.vararg-function(arg)
local ret = T.temp-T.list(orig.CraftFrame_SetSelection(unpack(arg)))
local id = GetCraftSelectionIndex()
local total_cost = 0
for i = 1, GetCraftNumReagents(id) do
@@ -281,7 +285,7 @@ do
end
local item_id, suffix_id = info.parse_link(link)
local count = select(3, GetCraftReagentInfo(id, i))
local _, price, limited = cache.merchant_info(item_id)
local _, price, limited = info.merchant_info(item_id)
local value = price and not limited and price or history.value(item_id .. ':' .. suffix_id)
if not value then
total_cost = nil
@@ -298,8 +302,8 @@ do
end
end
function Blizzard_TradeSkillUI()
hook('TradeSkillFrame_SetSelection', vararg-function(arg)
local ret = temp-A(orig.TradeSkillFrame_SetSelection(unpack(arg)))
hook('TradeSkillFrame_SetSelection', T.vararg-function(arg)
local ret = T.temp-T.list(orig.TradeSkillFrame_SetSelection(unpack(arg)))
local id = GetTradeSkillSelectionIndex()
local total_cost = 0
for i = 1, GetTradeSkillNumReagents(id) do
@@ -310,7 +314,7 @@ do
end
local item_id, suffix_id = info.parse_link(link)
local count = select(3, GetTradeSkillReagentInfo(id, i))
local _, price, limited = cache.merchant_info(item_id)
local _, price, limited = info.merchant_info(item_id)
local value = price and not limited and price or history.value(item_id .. ':' .. suffix_id)
if not value then
total_cost = nil
@@ -328,7 +332,7 @@ do
end
end
AuctionFrameAuctions_OnEvent = vararg-function(arg)
AuctionFrameAuctions_OnEvent = T.vararg-function(arg)
if AuctionFrameAuctions:IsVisible() then
return orig.AuctionFrameAuctions_OnEvent(unpack(arg))
end