diff --git a/components/scan.lua b/components/scan.lua index 86dbc8e..5245b59 100644 --- a/components/scan.lua +++ b/components/scan.lua @@ -10,7 +10,7 @@ end function private.last_page(total_auctions) local last_page = max(m.total_pages(total_auctions) - 1, 0) - local last_page_limit = Aux.util.safe_index(m.current_query().blizzard_query, 'last_page') or last_page + local last_page_limit = Aux.safe(m.current_query().blizzard_query).last_page/last_page return min(last_page_limit, last_page) end @@ -226,16 +226,17 @@ function private.submit_query(k) elseif m.current_thread().params.type == 'owner' then GetOwnerAuctionItems(m.current_thread().page) else + local blizzard_query = Aux.safe(m.current_query().blizzard_query)/{} QueryAuctionItems( - Aux.util.safe_index(m.current_query().blizzard_query, 'name'), - Aux.util.safe_index(m.current_query().blizzard_query, 'min_level'), - Aux.util.safe_index(m.current_query().blizzard_query, 'max_level'), - Aux.util.safe_index(m.current_query().blizzard_query, 'slot'), - Aux.util.safe_index(m.current_query().blizzard_query, 'class'), - Aux.util.safe_index(m.current_query().blizzard_query, 'subclass'), + blizzard_query.name, + blizzard_query.min_level, + blizzard_query.max_level, + blizzard_query.slot, + blizzard_query.class, + blizzard_query.subclass, m.current_thread().page, - Aux.util.safe_index(m.current_query().blizzard_query, 'usable'), - Aux.util.safe_index(m.current_query().blizzard_query, 'quality') + blizzard_query.usable, + blizzard_query.quality ) end m.wait_for_results(function() diff --git a/core.lua b/core.lua index 06a6281..c91d95f 100644 --- a/core.lua +++ b/core.lua @@ -38,12 +38,64 @@ do end end +function public.get(table, key) + return table[key] +end + +function public.set(table, key, value) + table[key] = value +end + +function public.f(func, ...) + local params = arg + return function(...) + for i=1,arg.n do + tinsert(params, arg[i]) + end + return func(unpack(params)) + end +end + +do + local safe_nil = setmetatable({}, { + __index = function(self) return self end, + __call = function(self) return self end, + __div = function(_, value) return value end + }) + + function public.safe(object) + return (object == nil or object == safe_nil) and safe_nil or setmetatable({}, { + __index = function(_, key) + return m.safe(key) ~= safe_nil and m.safe(object[key]) or safe_nil + end, + __call = function(_, ...) + for i=1,arg.n do + if safe(arg[i]) == safe_nil then + return safe_nil + end + end + return object(unpack(arg)) + end, + __div = function() return object end + }) + end +end + +do + local x = 0 + + function public.unique() + x = x + 1 + return x + end +end + function public.on_load() public.version = '3.5.2' - public.blizzard_ui_shown = false public.bids_loaded = false public.current_owner_page = nil public.last_owner_page_requested = nil + private.auction_frame_loaded = nil m.log('Aux v'..m.version..' loaded.') @@ -71,7 +123,7 @@ function public.on_load() btn:SetWidth(65) btn:SetHeight(24) btn:SetText('Close') - btn:SetScript('OnClick', Aux.f(HideUIPanel, AuxFrame)) + btn:SetScript('OnClick', m.f(HideUIPanel, AuxFrame)) public.close_button = btn end @@ -83,10 +135,8 @@ function public.on_load() btn:SetText('Default UI') btn:SetScript('OnClick',function() if AuctionFrame:IsVisible() then - m.blizzard_ui_shown = false HideUIPanel(AuctionFrame) else - m.blizzard_ui_shown = true ShowUIPanel(AuctionFrame) end end) @@ -103,7 +153,7 @@ function public.on_event() if event == 'VARIABLES_LOADED' then m.on_load() elseif event == 'ADDON_LOADED' then - m.on_addon_loaded() + m.safe(m.on_addon_load[arg1])() elseif event == 'AUCTION_HOUSE_SHOW' then m.on_auction_house_show() elseif event == 'AUCTION_HOUSE_CLOSED' then @@ -117,101 +167,96 @@ function public.on_event() end end -function private.on_addon_loaded() - if arg1 == 'Blizzard_AuctionUI' then - m.setup_hooks() - end +private.on_addon_load = {} +function m.on_addon_load.Blizzard_AuctionUI() - do - local function cost_label(cost) - local label = LIGHTYELLOW_FONT_COLOR_CODE..'(Total Cost: '..FONT_COLOR_CODE_CLOSE - label = label..(cost and m.util.format_money(cost, nil, LIGHTYELLOW_FONT_COLOR_CODE) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE) - label = label..LIGHTYELLOW_FONT_COLOR_CODE..')'..FONT_COLOR_CODE_CLOSE - return label + AuctionFrame:UnregisterEvent('AUCTION_HOUSE_SHOW') + AuctionFrame:SetScript('OnHide', nil) + + m.hook('ShowUIPanel', function(...) + if arg[1] == AuctionFrame then + return AuctionFrame:Show() end + return m.orig.ShowUIPanel(unpack(arg)) + end) - if arg1 == 'Blizzard_CraftUI' then - m.hook('CraftFrame_SetSelection', function(...) - local results = {m.orig.CraftFrame_SetSelection(unpack(arg)) } - - local id = GetCraftSelectionIndex() - local reagent_count = GetCraftNumReagents(id) - - local total_cost = 0 - for i=1,reagent_count do - local link = GetCraftReagentItemLink(id, i) - if not link then - total_cost = nil - break - end - local item_id, suffix_id = m.info.parse_hyperlink(link) - local count = ({GetCraftReagentInfo(id, i)})[3] - local _, price, limited = m.cache.merchant_info(item_id) - local value = price and not limited and price or m.history.value(item_id..':'..suffix_id) - if not value then - total_cost = nil - break - else - total_cost = total_cost + value * count - end - end - - CraftReagentLabel:SetText(SPELL_REAGENTS..' '..cost_label(total_cost)) - - return unpack(results) - end) - end - - if arg1 == 'Blizzard_TradeSkillUI' then - m.hook('TradeSkillFrame_SetSelection', function(...) - local results = {m.orig.TradeSkillFrame_SetSelection(unpack(arg)) } - - local id = GetTradeSkillSelectionIndex() - local reagent_count = GetTradeSkillNumReagents(id) - - local total_cost = 0 - for i=1,reagent_count do - local link = GetTradeSkillReagentItemLink(id, i) - if not link then - total_cost = nil - break - end - local item_id, suffix_id = m.info.parse_hyperlink(link) - local count = ({GetTradeSkillReagentInfo(id, i)})[3] - local _, price, limited = m.cache.merchant_info(item_id) - local value = price and not limited and price or m.history.value(item_id..':'..suffix_id) - if not value then - total_cost = nil - break - else - total_cost = total_cost + value * count - end - end - - TradeSkillReagentLabel:SetText(SPELL_REAGENTS..' '..cost_label(total_cost)) - - return unpack(results) - end) - end - end + m.hook('GetOwnerAuctionItems', m.GetOwnerAuctionItems) + m.hook('PickupContainerItem', m.PickupContainerItem) + m.hook('PickupInventoryItem', m.PickupInventoryItem) + m.hook('SetItemRef', m.SetItemRef) + m.hook('UseContainerItem', m.UseContainerItem) + m.hook('AuctionFrameAuctions_OnEvent', m.AuctionFrameAuctions_OnEvent) end - -function public.f(func, ...) - return function() - return func(unpack(arg)) - end -end - -function public.m(object, method, ...) - return m.f(object[method], object, unpack(arg)) -end - do - local x = 0 + local function cost_label(cost) + local label = LIGHTYELLOW_FONT_COLOR_CODE..'(Total Cost: '..FONT_COLOR_CODE_CLOSE + label = label..(cost and m.util.format_money(cost, nil, LIGHTYELLOW_FONT_COLOR_CODE) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE) + label = label..LIGHTYELLOW_FONT_COLOR_CODE..')'..FONT_COLOR_CODE_CLOSE + return label + end - function public.unique() - x = x + 1 - return x + function m.on_addon_load.Blizzard_CraftUI() + m.hook('CraftFrame_SetSelection', function(...) + local results = {m.orig.CraftFrame_SetSelection(unpack(arg)) } + + local id = GetCraftSelectionIndex() + local reagent_count = GetCraftNumReagents(id) + + local total_cost = 0 + for i=1,reagent_count do + local link = GetCraftReagentItemLink(id, i) + if not link then + total_cost = nil + break + end + local item_id, suffix_id = m.info.parse_hyperlink(link) + local count = ({GetCraftReagentInfo(id, i)})[3] + local _, price, limited = m.cache.merchant_info(item_id) + local value = price and not limited and price or m.history.value(item_id..':'..suffix_id) + if not value then + total_cost = nil + break + else + total_cost = total_cost + value * count + end + end + + CraftReagentLabel:SetText(SPELL_REAGENTS..' '..cost_label(total_cost)) + + return unpack(results) + end) + end + + function m.on_addon_load.Blizzard_TradeSkillUI() + m.hook('TradeSkillFrame_SetSelection', function(...) + local results = {m.orig.TradeSkillFrame_SetSelection(unpack(arg)) } + + local id = GetTradeSkillSelectionIndex() + local reagent_count = GetTradeSkillNumReagents(id) + + local total_cost = 0 + for i=1,reagent_count do + local link = GetTradeSkillReagentItemLink(id, i) + if not link then + total_cost = nil + break + end + local item_id, suffix_id = m.info.parse_hyperlink(link) + local count = ({GetTradeSkillReagentInfo(id, i)})[3] + local _, price, limited = m.cache.merchant_info(item_id) + local value = price and not limited and price or m.history.value(item_id..':'..suffix_id) + if not value then + total_cost = nil + break + else + total_cost = total_cost + value * count + end + end + + TradeSkillReagentLabel:SetText(SPELL_REAGENTS..' '..cost_label(total_cost)) + + return unpack(results) + end) end end @@ -278,31 +323,12 @@ do end end -function public.log(msg) - DEFAULT_CHAT_FRAME:AddMessage('[aux] '..msg, 1, 1, 0) -end - -function private.setup_hooks() - - local blizzard_ui_on_hide = function() - m.blizzard_ui_shown = false +function public.log(...) + local msg = '[aux]' + for i=1,arg.n do + msg = msg..' '..tostring(arg[i]) end - AuctionFrame:SetScript('OnHide', blizzard_ui_on_hide) - - m.hook('AuctionFrame_OnShow', function(...) - if not m.blizzard_ui_shown then - m.control.as_soon_as(function() return AuctionFrame:GetScript('OnHide') == blizzard_ui_on_hide end, Aux.f(HideUIPanel, AuctionFrame)) - end - return m.orig.AuctionFrame_OnShow(unpack(arg)) - end) - - m.hook('GetOwnerAuctionItems', m.GetOwnerAuctionItems) - m.hook('PickupContainerItem', m.PickupContainerItem) - m.hook('PickupInventoryItem', m.PickupInventoryItem) - m.hook('SetItemRef', m.SetItemRef) - m.hook('UseContainerItem', m.UseContainerItem) - m.hook('AuctionFrameAuctions_OnEvent', m.AuctionFrameAuctions_OnEvent) - + DEFAULT_CHAT_FRAME:AddMessage(msg, 1, 1, 0) end function private.GetOwnerAuctionItems(...) @@ -322,6 +348,9 @@ function public.neutral_faction() end function private.on_auction_house_show() + if AuctionFrame:IsVisible() then + AuctionFrame:Hide() + end AuxFrame:Show() m.tab_group:set_tab(1) end @@ -449,7 +478,7 @@ end function public.is_player(name, current) local realm = GetCVar('realmName') - return (not current and m.util.safe_index(aux_characters, realm, name)) or UnitName('player') == name + return (not current and m.safe(aux_characters)[realm][name]/false) or UnitName('player') == name end function public.unmodified() diff --git a/gui.lua b/gui.lua index a2adf42..4680ef7 100644 --- a/gui.lua +++ b/gui.lua @@ -230,14 +230,14 @@ function public.editbox(parent) editbox:SetShadowColor(0, 0, 0, 0) m.set_content_style(editbox) - editbox:SetScript('OnEditFocusLost', Aux.m(editbox, 'HighlightText', 0, 0)) + editbox:SetScript('OnEditFocusLost', Aux.f(editbox.HighlightText, editbox, 0, 0)) - editbox:SetScript('OnEscapePressed', Aux.m(editbox, 'ClearFocus')) + editbox:SetScript('OnEscapePressed', Aux.f(editbox.ClearFocus, editbox)) do local last_time, last_x, last_y - editbox:SetScript('OnEditFocusGained', Aux.m(editbox, 'HighlightText')) + editbox:SetScript('OnEditFocusGained', Aux.f(editbox.HighlightText, editbox)) editbox:SetScript('OnMouseUp', function() local x, y = GetCursorPosition() diff --git a/tabs/search/core.lua b/tabs/search/core.lua index 40fea10..7777111 100644 --- a/tabs/search/core.lua +++ b/tabs/search/core.lua @@ -535,7 +535,7 @@ do m.record_remover(search.table, record)() end, function(index) - if Aux.util.safe_index(search.table:GetSelection(), 'record') ~= record then + if Aux.safe(search.table:GetSelection()).record/nil ~= record then return end diff --git a/util/core.lua b/util/core.lua index 04723bd..bf21560 100644 --- a/util/core.lua +++ b/util/core.lua @@ -96,19 +96,6 @@ function public.bag_type(bag) end end -function public.safe_index(...) - local target = arg[1] - - for i=2,arg.n do - if not target then - return - end - target = target[arg[i]] - end - - return target -end - function public.without_errors(f) local orig = UIErrorsFrame.AddMessage UIErrorsFrame.AddMessage = m.pass