From 98eaa48480592afd2f80fac080e81d35cf615017 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Fri, 26 Aug 2016 07:58:48 +0200 Subject: [PATCH] refactoring --- aux-addon.lua | 68 ++++++++++++++++++++++++++++++++--------- core/cache.lua | 3 +- core/disenchant.lua | 4 +-- core/history.lua | 8 +---- core/scan.lua | 6 +--- core/slash.lua | 2 +- core/stack.lua | 2 +- core/tooltip.lua | 2 +- frame.lua | 2 +- gui/auction_listing.lua | 2 +- gui/item_listing.lua | 2 +- gui/listing.lua | 2 +- libs/module.lua | 1 + tabs/auctions/core.lua | 2 +- tabs/auctions/frame.lua | 2 +- tabs/bids/core.lua | 2 +- tabs/bids/frame.lua | 2 +- tabs/post/core.lua | 3 +- tabs/post/frame.lua | 2 +- tabs/search/core.lua | 2 +- tabs/search/filter.lua | 8 ++--- tabs/search/frame.lua | 2 +- tabs/search/results.lua | 2 +- util/completion.lua | 2 +- util/core.lua | 8 ----- util/filter.lua | 12 +++----- util/scan.lua | 6 +--- 27 files changed, 86 insertions(+), 73 deletions(-) diff --git a/aux-addon.lua b/aux-addon.lua index cd2463d..ef53d32 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -41,7 +41,7 @@ end public.empty = setmetatable({}, {__metatable=false, __newindex=error}) do - local pool, weak_pool, transient = {}, setmetatable({}, {__mode='v'}), {} + local pool, overflow_pool, transient = {}, setmetatable({}, {__mode='v'}), {} CreateFrame'Frame':SetScript('OnUpdate', function() -- local t = tremove(transient) for _, t in transient do recycle(t) end @@ -53,8 +53,7 @@ do return t end function public.recycle(t) - while getn(t) > 0 do tremove(t) end -- TODO or is setn enough? - if next(t) then return end -- no hashtables, they can't be shrunk -- TODO "n" field? + wipe(t) if getn(pool) < 50 then tinsert(pool, t) else @@ -79,16 +78,47 @@ do return {__unm=function(self) self.raw = true end, __call=apply, __sub=apply} end do - local make_transient, temp_mt - function make_transient(t) tinsert(transient, t) return t end + local make_transient, make_persistent, temp_mt, perm_mt + function make_transient(t) transient[t] = true return t end + function make_persistent(t) transient[t] = nil return t end temp_mt = operator_mt(make_transient) + perm_mt = operator_mt(make_persistent) public.temp { get = function() return setmetatable(t, temp_mt) end, set = function(t) return make_transient(t) end, } + public.perm + { + get = function() return setmetatable(t, perm_mt) end, + set = function(t) return make_persistent(t) end, + } end - local function fill(t,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,overflow) + local function insert_keys(t,k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16,k17,k18,k19,k20,overflow) + if overflow ~= nil then error 'Overflow.' end + if k1 ~= nil then t[k1] = true end + if k2 ~= nil then t[k2] = true end + if k3 ~= nil then t[k3] = true end + if k4 ~= nil then t[k4] = true end + if k5 ~= nil then t[k5] = true end + if k6 ~= nil then t[k6] = true end + if k7 ~= nil then t[k7] = true end + if k8 ~= nil then t[k8] = true end + if k9 ~= nil then t[k9] = true end + if k10 ~= nil then t[k10] = true end + if k11 ~= nil then t[k11] = true end + if k12 ~= nil then t[k12] = true end + if k13 ~= nil then t[k13] = true end + if k14 ~= nil then t[k14] = true end + if k15 ~= nil then t[k15] = true end + if k16 ~= nil then t[k16] = true end + if k17 ~= nil then t[k17] = true end + if k18 ~= nil then t[k18] = true end + if k19 ~= nil then t[k19] = true end + if k20 ~= nil then t[k20] = true end + return t + end + local function insert_values(t,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,overflow) if overflow ~= nil then error 'Overflow.' end if v1 ~= nil then tinsert(t, v1) end if v2 ~= nil then tinsert(t, v2) end @@ -112,24 +142,32 @@ do if v20 ~= nil then tinsert(t, v20) end return t end + local function insert_pairs(t,k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8,k9,v9,k10,v10,overflow) + if overflow ~= nil then error 'Overflow.' end + if k1 ~= nil then t[k1] = v1 end + if k2 ~= nil then t[k2] = v2 end + if k3 ~= nil then t[k3] = v3 end + if k4 ~= nil then t[k4] = v4 end + if k5 ~= nil then t[k5] = v5 end + if k6 ~= nil then t[k6] = v6 end + if k7 ~= nil then t[k7] = v7 end + if k8 ~= nil then t[k8] = v8 end + if k9 ~= nil then t[k9] = v9 end + if k10 ~= nil then t[k10] = v10 end + return t + end local function collector_mt(f) return {__call=f, __unm=function(self) return setmetatable(self, nil) end} end - local object_mt = { - __newindex() - } --- table-from(1,2,3) - local set_mt, list_mt, object_mt = collector_mt(keys), collector_mt(values), collector_mt(pairs) - modifier = public.property + local set_mt, list_mt, object_mt = collector_mt(insert_keys), collector_mt(insert_values), collector_mt(insert_pairs) public.property() --- list, object = {get=function() return setmetatable(t, list_mt) end}, {get=function() return setmetatable(t, object_mt) end} - modifier = nil + set, list, object = {get=function() return setmetatable(t, set_mt) end}, {get=function() return setmetatable(t, list_mt) end}, {get=function() return setmetatable(t, object_mt) end} private() -- TODO or 'auto' 'transient'? end local event_frame = CreateFrame 'Frame' -for _, event in -temp-list('ADDON_LOADED', 'VARIABLES_LOADED', 'PLAYER_LOGIN', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE') do +for event in -temp-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 diff --git a/core/cache.lua b/core/cache.lua index dd516fc..2880e04 100644 --- a/core/cache.lua +++ b/core/cache.lua @@ -1,4 +1,4 @@ -aux 'cache' import 'info' +aux 'cache' local info, persistence = aux.info, aux.persistence MIN_ITEM_ID = 1 MAX_ITEM_ID = 30000 @@ -16,7 +16,6 @@ _G.aux_merchant_sell = t _G.aux_characters = t function LOAD() - import 'persistence' 'info' scan_wdb() event_listener('MERCHANT_SHOW', on_merchant_show) diff --git a/core/disenchant.lua b/core/disenchant.lua index fd8de2c..5b624b3 100644 --- a/core/disenchant.lua +++ b/core/disenchant.lua @@ -39,7 +39,7 @@ do end function LOAD() - ARMOR = set( + ARMOR = -set( 'INVTYPE_HEAD', 'INVTYPE_NECK', 'INVTYPE_SHOULDER', @@ -56,7 +56,7 @@ function LOAD() 'INVTYPE_CLOAK', 'INVTYPE_HOLDABLE' ) - WEAPON = set( + WEAPON = -set( 'INVTYPE_2HWEAPON', 'INVTYPE_WEAPONMAINHAND', 'INVTYPE_WEAPON', diff --git a/core/history.lua b/core/history.lua index a78bf88..fa658cc 100644 --- a/core/history.lua +++ b/core/history.lua @@ -1,10 +1,4 @@ -aux 'history' import 'persistence' - ---local history_schema = tuple('#') --- :next_push 'number' --- :daily_min_buyout 'number' --- :daily_max_price 'number' --- :data_points (list(';') (tuple('@') :market_value 'number' :time 'number')) +aux 'history' local persistence = aux.persistence local history_schema = {'record', '#', {next_push='number'}, {daily_min_buyout='number'}, {daily_max_price='number'}, {data_points={'list', ';', {'record', '@', {market_value='number'}, {time='number'}}}} } diff --git a/core/scan.lua b/core/scan.lua index 472f8c2..308ded1 100644 --- a/core/scan.lua +++ b/core/scan.lua @@ -1,8 +1,4 @@ -aux 'scan' import 'info' - -function LOAD() - import 'history' -end +aux 'scan' local info, history = aux.info, aux.history PAGE_SIZE = 50 diff --git a/core/slash.lua b/core/slash.lua index a403d13..726765a 100644 --- a/core/slash.lua +++ b/core/slash.lua @@ -1,4 +1,4 @@ -aux 'slash' import 'persistence' +aux 'slash' local persistence = aux.persistence _G.SLASH_AUX1 = '/aux' function _G.SlashCmdList.AUX(command) diff --git a/core/stack.lua b/core/stack.lua index 63a4103..07f3be7 100644 --- a/core/stack.lua +++ b/core/stack.lua @@ -1,4 +1,4 @@ -aux 'stack' import 'info' +aux 'stack' local info = aux.info state = nil diff --git a/core/tooltip.lua b/core/tooltip.lua index 01d81b6..2bf8139 100644 --- a/core/tooltip.lua +++ b/core/tooltip.lua @@ -1,4 +1,4 @@ -aux 'tooltip' import 'info' 'disenchant' 'cache' 'money' 'history' +aux 'tooltip' local info, disenchant, cache, money, history = aux.info, aux.disenchant, aux.cache, aux.money, aux.history _G.aux_tooltip_value = true diff --git a/frame.lua b/frame.lua index e100008..899df51 100644 --- a/frame.lua +++ b/frame.lua @@ -1,4 +1,4 @@ -aux 'core' import 'gui' +aux 'core' local gui = aux.gui function LOAD() for _, info in tab_info do tabs:create_tab(info.name) end diff --git a/gui/auction_listing.lua b/gui/auction_listing.lua index 8866e91..6134a11 100644 --- a/gui/auction_listing.lua +++ b/gui/auction_listing.lua @@ -1,4 +1,4 @@ -aux 'auction_listing' import 'gui' 'sorting' 'history' 'money' 'info' +aux 'auction_listing' local gui, sorting, history, money, info = aux.gui, aux.sorting, aux.history, aux.money, aux.info _G.aux_price_per_unit = false diff --git a/gui/item_listing.lua b/gui/item_listing.lua index 9dbf06f..b4c44cb 100644 --- a/gui/item_listing.lua +++ b/gui/item_listing.lua @@ -1,4 +1,4 @@ -aux 'item_listing' import 'gui' 'info' +aux 'item_listing' local gui, info = aux.gui, aux.info local ROW_HEIGHT = 39 diff --git a/gui/listing.lua b/gui/listing.lua index b51ad86..151fbbb 100644 --- a/gui/listing.lua +++ b/gui/listing.lua @@ -1,4 +1,4 @@ -aux 'listing' import 'gui' +aux 'listing' local gui = aux.gui local ST_COUNT = 0 diff --git a/libs/module.lua b/libs/module.lua index 2a2e6cc..4f80d57 100644 --- a/libs/module.lua +++ b/libs/module.lua @@ -1,3 +1,4 @@ +if module then return end local type, setmetatable, setfenv, unpack, next, intersection, union, pcall, _G = type, setmetatable, setfenv, unpack, next, bit.band, bit.bor, pcall, getfenv(0) local error, import_error, declaration_error, collision_error, mutability_error local pass, start_declaration, advance_declaration, env_mt, interface_mt, metadata_mt, property_mt diff --git a/tabs/auctions/core.lua b/tabs/auctions/core.lua index b0cbbc5..ad0229a 100644 --- a/tabs/auctions/core.lua +++ b/tabs/auctions/core.lua @@ -1,4 +1,4 @@ -aux 'auctions_tab' import 'scan' +aux 'auctions_tab' local scan = aux.scan auction_records = {} diff --git a/tabs/auctions/frame.lua b/tabs/auctions/frame.lua index 7d37b7e..ca6a6d1 100644 --- a/tabs/auctions/frame.lua +++ b/tabs/auctions/frame.lua @@ -1,4 +1,4 @@ -aux 'auctions_tab' import 'gui' 'auction_listing' +aux 'auctions_tab' local gui, auction_listing = aux.gui, aux.auction_listing function create_frames() frame = CreateFrame('Frame', nil, aux_frame) diff --git a/tabs/bids/core.lua b/tabs/bids/core.lua index 19e6ae5..6f73033 100644 --- a/tabs/bids/core.lua +++ b/tabs/bids/core.lua @@ -1,4 +1,4 @@ -aux 'bids_tab' import 'scan' +aux 'bids_tab' local scan = aux.scan auction_records = {} diff --git a/tabs/bids/frame.lua b/tabs/bids/frame.lua index 50eb7b8..79c26a0 100644 --- a/tabs/bids/frame.lua +++ b/tabs/bids/frame.lua @@ -1,4 +1,4 @@ -aux 'bids_tab' import 'gui' 'auction_listing' +aux 'bids_tab' local gui, auction_listing = aux.gui, aux.auction_listing function create_frames() frame = CreateFrame('Frame', nil, aux_frame) diff --git a/tabs/post/core.lua b/tabs/post/core.lua index 18fd145..11f82c1 100644 --- a/tabs/post/core.lua +++ b/tabs/post/core.lua @@ -1,4 +1,5 @@ -aux 'post_tab' import 'scan' 'scan_util' 'post' 'history' 'info' 'persistence' 'item_listing' :al 'auction_listing' 'sorting' +aux 'post_tab' local scan, scan_util, post, history, info, persistence, item_listing, al, sorting = aux.scan, aux.scan_util, aux.post, aux.history, aux.info, aux.persistence, aux.item_listing, aux.auction_listing, aux.sorting + local DURATION_4, DURATION_8, DURATION_24 = 120, 480, 1440 local settings_schema = {'record', '#', {stack_size='number'}, {duration='number'}, {start_price='number'}, {buyout_price='number'}, {hidden='boolean'}} diff --git a/tabs/post/frame.lua b/tabs/post/frame.lua index 840c85f..d45f3df 100644 --- a/tabs/post/frame.lua +++ b/tabs/post/frame.lua @@ -1,4 +1,4 @@ -aux 'post_tab' import [''] 'core' 'gui' 'listing' 'item_listing' 'money' 'search_tab' +aux 'post_tab' local gui, listing, item_listing, money, search_tab = aux.gui, aux.listing, aux.item_listing, aux.money, aux.search_tab function create_frames() frame = CreateFrame('Frame', nil, aux_frame) diff --git a/tabs/search/core.lua b/tabs/search/core.lua index 839f46f..aee0146 100644 --- a/tabs/search/core.lua +++ b/tabs/search/core.lua @@ -1,4 +1,4 @@ -aux 'search_tab' import 'scan' 'scan_util' +aux 'search_tab' local scan, scan_util = aux.scan, aux.scan_util StaticPopupDialogs['AUX_SEARCH_TABLE_FULL'] = { text = 'Table full!\nFurther results from this search will still be processed but no longer displayed in the table.', diff --git a/tabs/search/filter.lua b/tabs/search/filter.lua index b429896..a1a5ae1 100644 --- a/tabs/search/filter.lua +++ b/tabs/search/filter.lua @@ -1,4 +1,4 @@ -aux 'search_tab' import 'filter_util' +aux 'search_tab' local filter_util = aux.filter_util function valid_level(str) local level = tonumber(str) @@ -72,14 +72,14 @@ function update_form() end if blizzard_query.exact then - for _, key in -temp-list('class', 'subclass', 'slot', 'quality') do + for key in -temp-set('class', 'subclass', 'slot', 'quality') do M[key..'_dropdown'].button:Disable() end else class_dropdown.button:Enable() quality_dropdown.button:Enable() end - for _, key in -temp-list('min_level', 'max_level') do + for key in -temp-set('min_level', 'max_level') do if blizzard_query.exact then M[key..'_input']:Disable() else @@ -313,7 +313,7 @@ function set_filter_display_offset(x_offset, y_offset) end function initialize_filter_dropdown() - for _, filter in -temp-list('and', 'or', 'not', 'min-unit-bid', 'min-unit-buy', 'max-unit-bid', 'max-unit-buy', 'bid-profit', 'buy-profit', 'bid-vend-profit', 'buy-vend-profit', 'bid-dis-profit', 'buy-dis-profit', 'bid-pct', 'buy-pct', 'item', 'tooltip', 'min-lvl', 'max-lvl', 'rarity')('left', 'utilizable', 'discard') do + for filter in -temp-set('and', 'or', 'not', 'min-unit-bid', 'min-unit-buy', 'max-unit-bid', 'max-unit-buy', 'bid-profit', 'buy-profit', 'bid-vend-profit', 'buy-vend-profit', 'bid-dis-profit', 'buy-dis-profit', 'bid-pct', 'buy-pct', 'item', 'tooltip', 'min-lvl', 'max-lvl', 'rarity')('left', 'utilizable', 'discard') do UIDropDownMenu_AddButton(-object( 'text', filter, 'value', filter, diff --git a/tabs/search/frame.lua b/tabs/search/frame.lua index b5790e0..b79dbf6 100644 --- a/tabs/search/frame.lua +++ b/tabs/search/frame.lua @@ -1,4 +1,4 @@ -aux 'search_tab' import 'gui' [''] 'core' 'completion' 'listing' 'auction_listing' 'filter_util' +aux 'search_tab' local gui, completion, listing, auction_listing, filter_util = aux.gui, aux.completion, aux.listing, aux.auction_listing, aux.filter_util FILTER_SPACING = 28.5 diff --git a/tabs/search/results.lua b/tabs/search/results.lua index 04e657e..0297dea 100644 --- a/tabs/search/results.lua +++ b/tabs/search/results.lua @@ -1,4 +1,4 @@ -aux 'search_tab' import 'info' +aux 'search_tab' local info = aux.info _G.aux_auto_buy_filter = '' diff --git a/util/completion.lua b/util/completion.lua index 37f9eda..6de7194 100644 --- a/util/completion.lua +++ b/util/completion.lua @@ -1,4 +1,4 @@ -aux 'completion' import 'filter_util' +aux 'completion' local filter_util = aux.filter_util function public:complete_filter() if IsControlKeyDown() then -- TODO problem is ctrl-v, maybe find a better solution diff --git a/util/core.lua b/util/core.lua index 7300e4b..e984e36 100644 --- a/util/core.lua +++ b/util/core.lua @@ -64,14 +64,6 @@ function public.index(t, ...) temp=arg return t end -function public.set(...) temp=arg - local t = t - for i=1,arg.n do - t[arg[i]] = true - end - return t -end - public.huge = 1.8*10^308 function public.modified.get() return IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown() end diff --git a/util/filter.lua b/util/filter.lua index d131e49..94b3cc4 100644 --- a/util/filter.lua +++ b/util/filter.lua @@ -1,8 +1,4 @@ -aux 'filter_util' import 'info' - -function LOAD() - import 'cache' -end +aux 'filter_util' local info, cache = aux.info, aux.cache function default_filter(str) return { @@ -208,7 +204,7 @@ public.filters = { function operator(str) local operator = str == 'not' and -list('operator', 'not', 1) - for _, name in -temp-list('and', 'or') do + for name in -temp-set('and', 'or') do for arity in present(select(3, strfind(str, '^'..name..'(%d*)$'))) do arity = tonumber(arity) operator = not (arity and arity < 2) and -list('operator', name, arity) @@ -229,7 +225,7 @@ do end for number in present(tonumber(select(3, strfind(str, '^(%d+)$')))) do if number >= 1 and number <= 60 then - for _, filter in -temp-list('min_level', 'max_level') do + for filter in -temp-set('min_level', 'max_level') do if not self[filter] then self[filter] = {str, number} return true @@ -517,7 +513,7 @@ function blizzard_query(components) query.slot = slot_index query.quality = item_info.quality else - for _, key in -temp-list('min_level', 'max_level', 'class', 'subclass', 'slot', 'usable', 'quality') do + for key in -temp-set('min_level', 'max_level', 'class', 'subclass', 'slot', 'usable', 'quality') do query[key] = index(filters[key], 2) end end diff --git a/util/scan.lua b/util/scan.lua index dd6ec96..86c9463 100644 --- a/util/scan.lua +++ b/util/scan.lua @@ -1,8 +1,4 @@ -aux 'scan_util' import 'info' - -function LOAD() - import 'filter_util' 'scan' -end +aux 'scan_util' local info , filter_util, scan = aux.info, aux.filter_util, aux.scan function public.find(auction_record, status_bar, on_abort, on_failure, on_success)