From d686b6af59994c715c9fd540086ee5f77e517824 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Tue, 6 Sep 2016 07:43:37 +0200 Subject: [PATCH] refactoring --- aux-addon.lua | 18 ++++----- control.lua | 8 ++-- core/cache.lua | 2 +- core/scan.lua | 2 +- core/tooltip.lua | 8 ++-- gui/auction_listing.lua | 2 +- gui/core.lua | 2 +- libs/green_t.lua | 83 +++++++++++++++++++++-------------------- tabs/post/core.lua | 9 ++--- tabs/search/filter.lua | 2 +- tabs/search/results.lua | 5 +-- util/core.lua | 4 +- 12 files changed, 71 insertions(+), 74 deletions(-) diff --git a/aux-addon.lua b/aux-addon.lua index 92bf298..eb16ce9 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -22,7 +22,7 @@ aux 'core' public.version = '5.0.0' -function public.print(...) temp=arg +function public.print(...) auto[arg] = true DEFAULT_CHAT_FRAME:AddMessage(LIGHTYELLOW_FONT_COLOR_CODE .. '[aux] ' .. join(map(arg, tostring), ' ')) end @@ -81,7 +81,7 @@ do end end -function SetItemRef(...) temp=arg +function SetItemRef(...) auto[arg] = true if arg[3] ~= 'RightButton' or not index(active_tab, 'CLICK_LINK') or not strfind(arg[1], '^item:%d+') then return orig.SetItemRef(unpack(arg)) end @@ -90,7 +90,7 @@ function SetItemRef(...) temp=arg end end -function UseContainerItem(...) temp=arg +function UseContainerItem(...) auto[arg] = true if modified or not index(active_tab, 'USE_ITEM') then return orig.UseContainerItem(unpack(arg)) end @@ -100,7 +100,7 @@ function UseContainerItem(...) temp=arg end public.orig = setmetatable({[_G]=t}, {__index=function(self, key) return self[_G][key] end}) -function public.hook(...) temp=arg +function public.hook(...) auto[arg] = true local name, object, handler if arg.n == 3 then name, object, handler = unpack(arg) @@ -186,7 +186,7 @@ end do local last_owner_page_requested - function GetOwnerAuctionItems(...) temp=arg + function GetOwnerAuctionItems(...) auto[arg] = true local page = arg[1] last_owner_page_requested = page return orig.GetOwnerAuctionItems(unpack(arg)) @@ -199,7 +199,7 @@ end function ADDON_LOADED.Blizzard_AuctionUI() AuctionFrame:UnregisterEvent('AUCTION_HOUSE_SHOW') AuctionFrame:SetScript('OnHide', nil) - hook('ShowUIPanel', function(...) temp=arg + hook('ShowUIPanel', function(...) auto[arg] = true if arg[1] == AuctionFrame then return AuctionFrame:Show() end return orig.ShowUIPanel(unpack(arg)) end) @@ -214,7 +214,7 @@ do return label end function ADDON_LOADED.Blizzard_CraftUI() - hook('CraftFrame_SetSelection', function(...) temp=arg + hook('CraftFrame_SetSelection', function(...) auto[arg] = true local ret = temp-A(orig.CraftFrame_SetSelection(unpack(arg))) local id = GetCraftSelectionIndex() local reagent_count = GetCraftNumReagents(id) @@ -241,7 +241,7 @@ do end) end function ADDON_LOADED.Blizzard_TradeSkillUI() - hook('TradeSkillFrame_SetSelection', function(...) temp=arg + hook('TradeSkillFrame_SetSelection', function(...) auto[arg] = true local ret = temp-A(orig.TradeSkillFrame_SetSelection(unpack(arg))) local id = GetTradeSkillSelectionIndex() local reagent_count = GetTradeSkillNumReagents(id) @@ -269,7 +269,7 @@ do end end -function AuctionFrameAuctions_OnEvent(...) temp=arg +function AuctionFrameAuctions_OnEvent(...) auto[arg] = true if AuctionFrameAuctions:IsVisible() then return orig.AuctionFrameAuctions_OnEvent(unpack(arg)) end diff --git a/control.lua b/control.lua index f353225..67d3919 100644 --- a/control.lua +++ b/control.lua @@ -58,7 +58,7 @@ end function public.event_listener(event, cb) local listener_id = unique_id - listeners[listener_id] = {event=event, cb=cb, kill=function(...) temp=arg if arg.n == 0 or arg[1] then kill_listener(listener_id) end end} + listeners[listener_id] = {event=event, cb=cb, kill=function(...) auto[arg] = true if arg.n == 0 or arg[1] then kill_listener(listener_id) end end} event_frame:RegisterEvent(event) return listener_id end @@ -67,13 +67,13 @@ function public.on_next_event(event, callback) event_listener(event, function(kill) callback(); kill() end) end -function public.thread(k, ...) temp=arg +function public.thread(k, ...) auto[arg] = true local thread_id = unique_id threads[thread_id] = T('k', L(k, unpack(arg))) return thread_id end -function public.wait(k, ...) temp=arg +function public.wait(k, ...) auto[arg] = true if type(k) == 'number' then when(function() k = k - 1 return k <= 1 end, unpack(arg)) else @@ -81,7 +81,7 @@ function public.wait(k, ...) temp=arg end end -function public.when(c, k, ...) temp=arg +function public.when(c, k, ...) auto[arg] = true if c() then return k(unpack(arg)) else diff --git a/core/cache.lua b/core/cache.lua index 264b0dc..cf997e7 100644 --- a/core/cache.lua +++ b/core/cache.lua @@ -144,7 +144,7 @@ function merchant_buy_scan() end function merchant_sell_scan() - for slot in info.inventory do temp=slot + for slot in info.inventory do auto[slot] = true local item_info = temp-info.container_item(unpack(slot)) if item_info then _G.aux_merchant_sell[item_info.item_id] = item_info.tooltip_money / item_info.aux_quantity diff --git a/core/scan.lua b/core/scan.lua index 29c106c..b968a11 100644 --- a/core/scan.lua +++ b/core/scan.lua @@ -44,7 +44,7 @@ end function private.query.get() return state.params.queries[state.query_index] end -function wait_for_callback(...) temp=arg +function wait_for_callback(...) auto[arg] = true local send_signal, signal_received = signal() local suspended, ret diff --git a/core/tooltip.lua b/core/tooltip.lua index 801565f..0873026 100644 --- a/core/tooltip.lua +++ b/core/tooltip.lua @@ -9,7 +9,7 @@ function LOAD() local inside_hook = false for name, f in game_tooltip_hooks do local name, f = name, f - hook(name, GameTooltip, function(...) temp=arg + hook(name, GameTooltip, function(...) auto[arg] = true inside_hook = true game_tooltip_money = 0 local ret = temp-A0(orig[GameTooltip][name](unpack(arg))) @@ -19,7 +19,7 @@ function LOAD() end) end local orig = GameTooltip:GetScript('OnTooltipAddMoney') - GameTooltip:SetScript('OnTooltipAddMoney', function(...) temp=arg + GameTooltip:SetScript('OnTooltipAddMoney', function(...) auto[arg] = true if inside_hook then game_tooltip_money = arg1 else @@ -28,7 +28,7 @@ function LOAD() end) end local orig = SetItemRef - setglobal('SetItemRef', function(...) temp=arg + setglobal('SetItemRef', function(...) auto[arg] = true local result = orig(unpack(arg)) local name, _, quality = GetItemInfo(arg[1]) if not IsShiftKeyDown() and not IsControlKeyDown() and name then @@ -206,7 +206,7 @@ end function game_tooltip_hooks:SetAuctionSellItem() local name, _, quantity = GetAuctionSellItemInfo() if name then - for slot in info.inventory do temp=slot + for slot in info.inventory do auto[slot] = true local link = GetContainerItemLink(unpack(slot)) if link and select(5, info.parse_link(link)) == name then extend_tooltip(GameTooltip, link, quantity) diff --git a/gui/auction_listing.lua b/gui/auction_listing.lua index 8d71a94..01cb70f 100644 --- a/gui/auction_listing.lua +++ b/gui/auction_listing.lua @@ -914,7 +914,7 @@ local methods = { end end, - SetSort = function(self, ...) temp=arg + SetSort = function(self, ...) auto[arg] = true for k=1,arg.n do for i, sort in self.sorts do if sort.index == abs(arg[k]) then diff --git a/gui/core.lua b/gui/core.lua index 526e8a3..f9ed6bb 100644 --- a/gui/core.lua +++ b/gui/core.lua @@ -13,7 +13,7 @@ function LOAD() aux_background:SetTexture(color.content.background()) aux_background:SetAllPoints(DropDownList1Backdrop) blizzard_backdrop = DropDownList1Backdrop:GetBackdrop() - hook('ToggleDropDownMenu', function(...) temp=arg + hook('ToggleDropDownMenu', function(...) auto[arg] = true local ret = temp-A0(orig.ToggleDropDownMenu(unpack(arg))) local dropdown = _G[arg[4] or ''] or this:GetParent() if strfind(dropdown:GetName() or '', '^aux_frame%d+$') then diff --git a/libs/green_t.lua b/libs/green_t.lua index a3932c6..b46b334 100644 --- a/libs/green_t.lua +++ b/libs/green_t.lua @@ -1,8 +1,8 @@ green_t = module --setglobal('green_t', green_t and error(nil) or module) -local next, setn, type, setmetatable = next, table.setn, type, setmetatable -local wipe, release, acquire, acquire_auto, empty +local next, getn, setn, type, setmetatable = next, getn, table.setn, type, setmetatable +local wipe, release, acquire, acquire_auto -- TODO mandatory operation table mandate + comply/fulfill functions local pool, pool_size, overflow_pool, auto_release = {}, 0, setmetatable({}, { __mode='k' }), {} @@ -17,10 +17,20 @@ function wipe(t) for k in t do t[k] = nil end t.reset, t.reset = nil, 1 setn(t, 0) - return t end public.wipe = wipe +public.init = setmetatable({}, { + __metatable = false, + __newindex = function(_, t, init) + wipe(t) + for k, v in init do + t[k] = v + setn(t, getn(init)) + end + end +}) + function release(t) wipe(t) auto_release[t] = nil @@ -33,6 +43,14 @@ function release(t) end public.release = release +function public.ret(t) + if getn(t) > 0 then + return tremove(t, 1), ret(t) + else + release(t) + end +end + function acquire() if pool_size > 0 then pool_size = pool_size - 1 @@ -54,40 +72,25 @@ function acquire_auto() end public.tt.get = acquire_auto -function public.ret(t) - if getn(t) > 0 then - return tremove(t, 1), ret(t) - else - release(t) - end -end +public.empty = setmetatable({}, { __newindex=nop }) do - local mt = { __newindex=nop } - function empty() return setmetatable(acquire_auto(), mt) end - public.empty.get = empty -end - -do - local function apply(v, enable) + local function set_auto_release(v, enable) if type(v) ~= 'table' then return end auto_release[v] = enable and true or nil end - local function define_modifier(name, enable) - local f = function(_, v) apply(v, enable); return v end - local mt = { __call=f, __sub=f } - public[name] { get=function() return setmetatable(acquire_auto(), mt) end, set=function(v) apply(v, enable) end } - end - define_modifier('temp', true) - define_modifier('perm', false) -end - -do - local mt, key = {}, nil - function mt:__unm() local temp = mt.__index; mt.__index = nil; return temp end - function mt:__index(k) key = k; return self end - function mt:__call(v) self[key] = v; key = nil; return self end - function public.__(t) mt.__newindex = wipe(t); return setmetatable(acquire_auto(), mt) end + public.auto = setmetatable({}, { + __metatable = false, + __newindex=function(_, k, v) set_auto_release(k, v) end, + }) + public.temp = setmetatable({}, { + __metatable=false, + __sub=function(_, v) set_auto_release(v, false); return v end, + }) + public.perm = setmetatable({}, { + __metatable=false, + __sub=function(_, v) set_auto_release(v, true); return v end, + }) end local function arg_chunk(k, n) @@ -100,11 +103,11 @@ end function public.pseudo_vararg_function(body, upvals) local upval_chunk = '' - for k in upvals or empty() do + for k in upvals or empty do upval_chunk = upval_chunk .. format('local %1$s = %1$s;', k) end local f = loadstring(format('%s return function(%s) %s end', upval_chunk, arg_chunk(), body)) or error() - setfenv(f, upvals or empty()) + setfenv(f, upvals or empty) return f() end @@ -160,15 +163,15 @@ local function insert_chunk(mode) end do - local function pseudo_literal(mode) - local upvals = {setmetatable=setmetatable, setn=table.setn, error=error} + local function pseudo_table_literal(mode) + local upvals = {setmetatable=setmetatable, setn=setn, error=error} local mt = {__call = pseudo_vararg_function(insert_chunk(mode) .. 'setmetatable(a1, nil); return a1', upvals)} return function() return setmetatable(acquire(), mt) end end - public.S.get = pseudo_literal('k') - public.A.get = pseudo_literal('v') - public.A0.get = pseudo_literal('v0') - public.T.get = pseudo_literal('kv') + public.S.get = pseudo_table_literal('k') + public.A.get = pseudo_table_literal('v') + public.A0.get = pseudo_table_literal('v0') + public.T.get = pseudo_table_literal('kv') end do diff --git a/tabs/post/core.lua b/tabs/post/core.lua index 14e0acf..0703963 100644 --- a/tabs/post/core.lua +++ b/tabs/post/core.lua @@ -332,7 +332,7 @@ function quantity_update(max_count) end function unit_vendor_price(item_key) - for slot in info.inventory do temp=slot + for slot in info.inventory do auto[slot] = true local item_info = info.container_item(unpack(slot)) if item_info and item_info.item_key == item_key then if info.auctionable(item_info.tooltip, nil, item_info.lootable) then @@ -396,7 +396,7 @@ end function update_inventory_records() local auctionable_map = tt - for slot in info.inventory do temp=slot + for slot in info.inventory do auto[slot] = true for item_info in present(info.container_item(unpack(slot))) do local charge_class = item_info.charges or 0 if info.auctionable(item_info.tooltip, nil, item_info.lootable) then @@ -427,10 +427,7 @@ function update_inventory_records() end end end - wipe(inventory_records) - for _, auctionable in auctionable_map do - tinsert(inventory_records, auctionable) - end + init[inventory_records] = temp-values(auctionable_map) sort(inventory_records, function(a, b) return a.name < b.name end) refresh = true end diff --git a/tabs/search/filter.lua b/tabs/search/filter.lua index 16bb113..631b852 100644 --- a/tabs/search/filter.lua +++ b/tabs/search/filter.lua @@ -170,7 +170,7 @@ function clear_form() UIDropDownMenu_ClearAll(quality_dropdown) filter_parameter_input:ClearFocus() wipe(post_filter) - wipe(filter_builder_state).selected = 0 + init[filter_builder_state] = temp-T('selected', 0) update_filter_display() end diff --git a/tabs/search/results.lua b/tabs/search/results.lua index d289662..298b992 100644 --- a/tabs/search/results.lua +++ b/tabs/search/results.lua @@ -186,10 +186,7 @@ function start_real_time_scan(query, search, continuation) for _, record in new_records do map[record.sniping_signature] = record end - wipe(new_records) - for _, record in map do - tinsert(new_records, record) - end + init[new_records] = temp-values(map) if getn(new_records) > 1000 then StaticPopup_Show('AUX_SEARCH_TABLE_FULL') diff --git a/util/core.lua b/util/core.lua index 4476eba..b351ff1 100644 --- a/util/core.lua +++ b/util/core.lua @@ -97,7 +97,7 @@ do end end -function public.index(t, ...) temp=arg +function public.index(t, ...) auto[arg] = true for i = 1, arg.n do t = t and t[arg[i]] end return t end @@ -134,7 +134,7 @@ function public.key(value, t) for k, v in t do if v == value then return k end end end -function public.keys(t) -- TODO recursive return instead of new table +function public.keys(t) local keys = M.t for k in t do tinsert(keys, k) end return keys