From 60d4fc85b3efab88c156262bafa051be438d786d Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Sun, 28 Aug 2016 14:41:19 +0200 Subject: [PATCH] refactoring --- aux-addon.lua | 76 ++++++++++++++++++++++--------------------- core/scan.lua | 6 ++-- core/stack.lua | 2 +- gui/core.lua | 18 +++++----- libs/module.lua | 21 +++++++----- tabs/search/frame.lua | 4 +-- util/color.lua | 1 + util/core.lua | 4 +-- util/filter.lua | 6 ++-- util/persistence.lua | 2 +- 10 files changed, 74 insertions(+), 66 deletions(-) diff --git a/aux-addon.lua b/aux-addon.lua index 97cbc9c..a84a8a2 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -5,6 +5,12 @@ module() public.version = '5.0.0' +function public.log(...) --temp=arg + local msg = '[aux]' + for i = 1, arg.n do msg = msg..' '..tostring(arg[i]) end + DEFAULT_CHAT_FRAME:AddMessage(LIGHTYELLOW_FONT_COLOR_CODE..msg) +end + do local modules = {core={env=M, interface=I}} _G.aux = setmetatable({}, { @@ -29,14 +35,14 @@ function public.bids_loaded.get() return bids_loaded end local current_owner_page function public.current_owner_page.get() return current_owner_page end -public.empty = setmetatable({}, {__metatable=false, __newindex=error}) +function public.F() end do - local pool, overflow_pool, transient = {}, setmetatable({}, {__mode='v'}), {} + local pool, overflow_pool, tmp = {}, setmetatable({}, {__mode='v'}), {} CreateFrame'Frame':SetScript('OnUpdate', function() - for t in transient do recycle(t) end - wipe(transient) + for t in tmp do recycle(t) end + wipe(tmp) end) function public.wipe(t) -- like with a cloth or something @@ -54,16 +60,15 @@ do else tinsert(overflow_pool, t) end --- log(getn(table_pool), '-', getn(weak_pool)) + log(getn(pool), '-', getn(overflow_pool)) end - function public.t.get() return tremove(pool) or tremove(overflow_pool, next(overflow_pool)) or {} end function public.tt.get() local t = tremove(pool) or tremove(overflow_pool, next(overflow_pool)) or {} - transient[t] = true + tmp[t] = true return t end @@ -76,24 +81,20 @@ do return {__unm=function(self) self.raw = true; return self end, __call=apply, __sub=apply} end do - local function make_transient(t) transient[t] = true; return t end - local function make_persistent(t) transient[t] = nil; return t end - do - local mt = operator_mt(make_transient) - public.temp - { - get = function() return setmetatable(t, mt) end, - set = function(t) return make_transient(t) end, - } - end - do - local mt = operator_mt(make_persistent) - public.perm - { - get = function() return setmetatable(t, mt) end, - set = function(t) return make_persistent(t) end, - } - end + local mt = operator_mt(function(t) tmp[t] = true; return t end) + public.temp + { + get = function() return setmetatable(t, mt) end, + set = function(t) tmp[t] = true end, + } + end + do + local mt = operator_mt(function(t) tmp[t] = nil; return t end) + public.perm + { + get = function() return setmetatable(t, mt) end, + set = function(t) tmp[t] = nil end, + } end 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) @@ -162,19 +163,20 @@ do return {__call=f, __unm=function(self) return setmetatable(self, nil) end} end local set_mt, list_mt, object_mt = collector_mt(insert_keys), collector_mt(insert_values), collector_mt(insert_pairs) - - if public then - function set.get() return setmetatable(t, set_mt) end - function list.get() return setmetatable(t, list_mt) end - function object.get() return setmetatable(t, object_mt) end - end + public() + function set.get() return setmetatable(t, set_mt) end + function list.get() return setmetatable(t, list_mt) end + function object.get() return setmetatable(t, object_mt) end + private() end -function public.log(...) temp=arg - local msg = '[aux]' - for i = 1, arg.n do msg = msg..' '..tostring(arg[i]) end - DEFAULT_CHAT_FRAME:AddMessage(LIGHTYELLOW_FONT_COLOR_CODE..msg) -end +--local wt = setmetatable(t, {__mode='kv'}) +--local a, b = t, t +--tinsert(wt, a) +--tinsert(wt, t) +--tinsert(wt, b) +--collectgarbage() +--log (getn(wt)) local event_frame = CreateFrame('Frame') 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 @@ -206,7 +208,7 @@ tab_info = t do for k, v in -temp-object('search_tab', 'Search', 'post_tab', 'Post', 'auctions_tab', 'Auctions', 'bids_tab', 'Bids') do local tab = -object('name', v) - local env = (function() aux(v) return M end)() + local env = (function() aux(k) return M end)() function env.private.OPEN.set(f) tab.OPEN = f end function env.private.CLOSE.set(f) tab.CLOSE = f end function env.private.USE_ITEM.set(f) tab.USE_ITEM = f end diff --git a/core/scan.lua b/core/scan.lua index d246ba7..e19748f 100644 --- a/core/scan.lua +++ b/core/scan.lua @@ -3,7 +3,7 @@ aux 'scan' local info, history = aux.info, aux.history PAGE_SIZE = 50 do - local scan_states = {} + local scan_states = t function public.start(params) for old_state in present(scan_states[params.type]) do @@ -18,7 +18,7 @@ do end function public.abort(scan_id) - local aborted = {} + local aborted = t for type, state in scan_states do if not scan_id or state.id == scan_id then kill_thread(state.id) @@ -108,7 +108,7 @@ function submit_query() elseif state.params.type == 'owner' then GetOwnerAuctionItems(state.page) else - local blizzard_query = query.blizzard_query or {} + local blizzard_query = query.blizzard_query or t QueryAuctionItems( blizzard_query.name, blizzard_query.min_level, diff --git a/core/stack.lua b/core/stack.lua index 2f7d94a..6220ea1 100644 --- a/core/stack.lua +++ b/core/stack.lua @@ -1,6 +1,6 @@ aux 'stack' local info = aux.info -local state = nil +local state function stack_size(slot) local container_item_info = info.container_item(unpack(slot)) diff --git a/gui/core.lua b/gui/core.lua index 49332ad..454aeaf 100644 --- a/gui/core.lua +++ b/gui/core.lua @@ -14,14 +14,14 @@ function LOAD() aux_background:SetAllPoints(DropDownList1Backdrop) blizzard_backdrop = DropDownList1Backdrop:GetBackdrop() hook('ToggleDropDownMenu', function(...) temp=arg - local ret = {orig.ToggleDropDownMenu(unpack(arg)) } - local dropdown = _G[arg[4] or ''] or this:GetParent() - if strfind(dropdown:GetName() or '', '^aux_frame%d+$') then - set_aux_dropdown_style(dropdown) - else - set_blizzard_dropdown_style() - end - return unpack(ret) + local ret = {orig.ToggleDropDownMenu(unpack(arg)) } + local dropdown = _G[arg[4] or ''] or this:GetParent() + if strfind(dropdown:GetName() or '', '^aux_frame%d+$') then + set_aux_dropdown_style(dropdown) + else + set_blizzard_dropdown_style() + end + return unpack(ret) end) function set_aux_dropdown_style(dropdown) @@ -203,7 +203,7 @@ function public.resize_tab(tab, width, padding) end do - local mt = {__index={}} + local mt = {__index=t} function mt.__index:create_tab(text) local id = getn(self._tabs) + 1 diff --git a/libs/module.lua b/libs/module.lua index 9ebf782..577b31f 100644 --- a/libs/module.lua +++ b/libs/module.lua @@ -17,14 +17,14 @@ local state = {} local declarator_mt = {__metatable=false} do local ACCESS, EVENT = {public=PUBLIC, private=PRIVATE}, {call=CALL, get=INDEX, set=NEWINDEX} - local function extract(v) + local function extract_handlers(v) local f, getter, setter f, getter, setter, v.call, v.get, v.set = v.call, v.get, v.set, nil, nil, nil if next(v) or f ~= nil and getter ~= nil then error() end return f, getter, setter end local function declare(self, access, name, handlers) - self.access[name] = self.access[name] and collision_error(name) or access or PRIVATE + self.access[name] = self.access[name] and collision_error(name) or access or self.default_access for event, handler in handlers do if type(handler) ~= 'function' and (event == INDEX or declaration_error()) then handler = const(handler == nil and tostring(name) or handler) @@ -33,7 +33,7 @@ do end end function declarator_mt:__index(key) self=state[self] - if ACCESS[key] and not self.declaration_access then + if ACCESS[key] and (not (self.declaration_access or self.declaration_name) or declaration_error()) then self.declaration_access = ACCESS[key] elseif not self.declaration_name or declaration_error() then self.declaration_name = type(key) == 'string' and key or declaration_error() @@ -48,20 +48,24 @@ do name, event = key, type(value) == 'function' and CALL or INDEX end declare(self, self.declaration_access, name, {[event]=value}) - self.declaration_access, self.declaration_name = nil, nil + self.declaration_access, self.declaration_name = nil, nil end function declarator_mt:__call(value) self=state[self] if self.declaration_name then - local success, f, getter, setter = pcall(extract, value) + local success, f, getter, setter = pcall(extract_handlers, value) if not success then declaration_error() end declare(self, self.declaration_access, self.declaration_name, {[CALL]=f, [INDEX]=getter, [NEWINDEX]=setter}) + elseif self.declaration_access or declaration_error() then + self.default_access = self.declaration_access end - self.declaration_access, self.declaration_name = nil, nil + self.declaration_access, self.declaration_name = nil, nil end end local env_mt = {__metatable=false} function env_mt:__index(key) self=state[self] + local quote = state.quote + if quote then return quote(key) end local getter = self[INDEX][key] if getter then return getter() end return self[CALL][key] or _G[key] or self.declarator[key] @@ -88,12 +92,13 @@ end function module(...) local declarator, env, interface = setmetatable({}, declarator_mt), setmetatable({}, env_mt), setmetatable({}, interface_mt) - local self = { + local self; self = { access = {_=PRIVATE, error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, public=PRIVATE, private=PRIVATE}, [CALL] = {error=error, nop=nop}, [INDEX] = {_G=const(_G), M=const(env), I=const(interface), public=function() return declarator.public end, private=function() return declarator.private end}, - [NEWINDEX] = {_=nop}, + [NEWINDEX] = {_=nop, quote=function(enable) self.quote = enable and type(enable) == 'function' and enable or tostring end}, declarator = declarator, + default_access = PRIVATE, } for i = 1, arg.n do local module = state[arg[i] or import_error()] diff --git a/tabs/search/frame.lua b/tabs/search/frame.lua index d3a5f99..d31bf1e 100644 --- a/tabs/search/frame.lua +++ b/tabs/search/frame.lua @@ -588,8 +588,8 @@ function create_frames() filter_display = scroll_child end - status_bars = {} - tables = {} + status_bars = t + tables = t for _=1,5 do local status_bar = gui.status_bar(frame) status_bar:SetAllPoints(status_bar_frame) diff --git a/util/color.lua b/util/color.lua index c8c8a55..4aaf9c1 100644 --- a/util/color.lua +++ b/util/color.lua @@ -50,6 +50,7 @@ do end } public.color.get = color_accessor(function(color) + log'kek' local r, g, b, a = unpack(color) return setmetatable({r/255, g/255, b/255, a}, mt) end) diff --git a/util/core.lua b/util/core.lua index 09fd007..73c67e6 100644 --- a/util/core.lua +++ b/util/core.lua @@ -81,12 +81,12 @@ function public.modified.get() return IsShiftKeyDown() or IsControlKeyDown() or --end do - local _state = setmetatable({}, {__mode='kv'}) + local _state = setmetatable(t, -object('__mode', 'kv')) local __index = function(self, key) return _state[self].handler({public=self, private=_state[self].state}, key) end function public.index_function(state, handler) -- TODO rename table-accessor, use predicate to stop - local state, self = {handler=handler, state=state}, {} + local state, self = {handler=handler, state=state}, t _state[self] = state return setmetatable(self, {__metatable=false, __index=__index, state=state}) end diff --git a/util/filter.lua b/util/filter.lua index 510abf9..228b063 100644 --- a/util/filter.lua +++ b/util/filter.lua @@ -279,7 +279,7 @@ function parse_parameter(input_type, str) end function public.parse_query_string(str) - local post_filter = {} + local post_filter = t local blizzard_filter_parser = blizzard_filter_parser() local parts = map(split(str, '/'), function(part) return strlower(trim(part)) end) @@ -533,11 +533,11 @@ function validator(components) if components.blizzard.exact and strlower(info.item(record.item_id).name) ~= components.blizzard.name[2] then return false end - local stack = {} + local stack = tt for i = getn(components.post), 1, -1 do local type, name, param = unpack(components.post[i]) if type == 'operator' then - local args = {} + local args = tt while (not param or param > 0) and getn(stack) > 0 do tinsert(args, tremove(stack)) param = param and param - 1 diff --git a/util/persistence.lua b/util/persistence.lua index c89d21c..cc76bd3 100644 --- a/util/persistence.lua +++ b/util/persistence.lua @@ -79,7 +79,7 @@ end function public.write_record(schema, record) local separator = schema[2] local parts = tt - for i = 3, getn(schema) do + for i = 3 , getn(schema) do local key, type = next(schema[i]) tinsert(parts, write(type, record[key])) end