From 62ef2683f51ebbdaa4e980d67bb9ee39ba2d2d7f Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Sun, 15 Oct 2017 14:56:43 +0200 Subject: [PATCH] module refactoring --- aux-addon.lua | 24 ++++++------- control.lua | 4 +-- core/history.lua | 4 +-- core/scan.lua | 4 +-- frame.lua | 2 +- gui/core.lua | 2 +- libs/T.lua | 2 +- libs/module.lua | 76 +++++++++++++---------------------------- tabs/post/core.lua | 26 +++++++------- tabs/search/core.lua | 4 +-- tabs/search/filter.lua | 6 ++-- tabs/search/results.lua | 6 ++-- tabs/search/saved.lua | 2 +- util.lua | 2 +- util/info.lua | 2 +- 15 files changed, 69 insertions(+), 97 deletions(-) diff --git a/aux-addon.lua b/aux-addon.lua index 93fdaef..738dd6b 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -25,10 +25,10 @@ M.print = vararg-function(arg) end local bids_loaded -function M.get_bids_loaded() return bids_loaded end +function M.get.bids_loaded() return bids_loaded end local current_owner_page -function M.get_current_owner_page() return current_owner_page end +function M.get.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 @@ -37,10 +37,10 @@ end do local handlers, handlers2 = {}, {} - function M.set_LOAD(f) + function M.set.LOAD(f) tinsert(handlers, f) end - function M.set_LOAD2(f) + function M.set.LOAD2(f) tinsert(handlers2, f) end event_frame:SetScript('OnEvent', function() @@ -88,7 +88,7 @@ do for scope in pairs(temp-S('character', 'faction', 'realm', 'account')) do local scope = scope M[scope .. '_data'] = function(key, init) - if not cache[scope] then error('Cache not ready', 2) end + 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 if cache[scope][key][k] == nil then @@ -104,16 +104,16 @@ 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 + 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 tinsert(tab_info, tab) 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 @@ -159,7 +159,7 @@ end do local locked - function M.get_bid_in_progress() return locked end + function M.get.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 +184,7 @@ end do local locked - function M.get_cancel_in_progress() return locked end + function M.get.cancel_in_progress() return locked end function M.cancel_auction(index, on_success) if locked then return end locked = true diff --git a/control.lua b/control.lua index c9177d6..6d4c28e 100644 --- a/control.lua +++ b/control.lua @@ -5,7 +5,7 @@ local event_frame = CreateFrame'Frame' local listeners, threads = T, T local thread_id -function M.get_thread_id() return thread_id end +function M.get.thread_id() return thread_id end function LOAD() event_frame:SetScript('OnUpdate', UPDATE) @@ -50,7 +50,7 @@ end do local id = 0 - function get_unique_id() + function get.unique_id() id = id + 1 return id end diff --git a/core/history.lua b/core/history.lua index c130838..b016060 100644 --- a/core/history.lua +++ b/core/history.lua @@ -15,7 +15,7 @@ end do local next_push = 0 - function get_next_push() + function get.next_push() if time() > next_push then local date = date('*t') date.hour, date.min, date.sec = 24, 0, 0 @@ -25,7 +25,7 @@ do end end -function get_new_record() +function get.new_record() return temp-O('next_push', next_push, 'data_points', T) end diff --git a/core/scan.lua b/core/scan.lua index 41d85cc..734b185 100644 --- a/core/scan.lua +++ b/core/scan.lua @@ -49,7 +49,7 @@ do do (on_complete or nop)() end end - function get_state() + function get.state() for _, state in pairs(scan_states) do if state.id == thread_id then return state @@ -58,7 +58,7 @@ do end end -function get_query() +function get.query() return state.params.queries[state.query_index] end diff --git a/frame.lua b/frame.lua index 8347d12..c52f776 100644 --- a/frame.lua +++ b/frame.lua @@ -32,7 +32,7 @@ end do tabs = gui.tabs(AuxFrame, 'DOWN') tabs._on_select = on_tab_click - function M.set_tab(id) tabs:select(id) end + function M.set.tab(id) tabs:select(id) end end do local btn = gui.button(AuxFrame) diff --git a/gui/core.lua b/gui/core.lua index f14cbf9..aee58fa 100644 --- a/gui/core.lua +++ b/gui/core.lua @@ -88,7 +88,7 @@ M.font_size = immutable-{ do local id = 1 - function M.get_unique_name() + function M.get.unique_name() id = id + 1 return 'AuxFrame' .. id end diff --git a/libs/T.lua b/libs/T.lua index c82520e..f1995f2 100644 --- a/libs/T.lua +++ b/libs/T.lua @@ -56,7 +56,7 @@ do M.static = setmetatable({}, {__metatable=false, __newindex=nop, __call=f, __sub=f}) end -M.get_T = acquire +M.get.T = acquire do local function ret(t) diff --git a/libs/module.lua b/libs/module.lua index 80240de..943b571 100644 --- a/libs/module.lua +++ b/libs/module.lua @@ -1,62 +1,34 @@ if module then return end -local strfind, type, setmetatable, setfenv, _G = strfind, type, setmetatable, setfenv, getfenv(0) -local error, nop, define, include, create_module, nop_default_mt, public_modifier_mt, proxy_mt +local _G, setfenv, setmetatable, rawget = getfenv(0), setfenv, setmetatable, rawget local loaded, defined, interfaces, environments = {}, {}, {}, {} -function nop() end -function error(msg, ...) return _G.error(format(msg or '', unpack(arg)) .. '\n' .. debugstack(), 0) end +local function nop() end -function define(self, k, v, private) - if type(k) ~= 'string' or not strfind(k, '^[_%a][_%w]*') then error('Invalid identifier "%s".', k) end - local _, _, prefix, suffix = strfind(k, '^(.?.?.?.?)([_%a].*)') - local module = loaded[self] - local signature = (private and '-' or '+') .. k - module.defined[signature] = module.defined[signature] and error('Duplicate identifier "%s".', signature) or true - if private or not module.defined['-' .. k] then - module.fields[k] = v - if prefix == 'get_' then module.accessors[suffix] = v elseif prefix == 'set_' then module.mutators[suffix] = v end - end - if not private then - module.public_fields[k] = v - if prefix == 'get_' then module.public_accessors[suffix] = v elseif prefix == 'set_' then module.public_mutators[suffix] = v end - end +local function include(private, name) + local P = loaded[name] or error('No such module.', 2) + for k, v in P.public do private[k] = v end end -function include(self, name) - local module = name and loaded[name] or error('No module "%s".', name) - for k, v in pairs(module.public_fields) do define(self, k, v, true) end -end - -public_modifier_mt = {__metatable=false, __newindex=define} - -nop_default_mt = {__index=function() return nop end} - -function proxy_mt(fields, mutators) - return {__metatable=false, __index=fields, __newindex=function(_, k, v) return mutators[k](v) end} -end - -function create_module(name) - if type(name) ~= 'string' then error('Invalid module name "%s".', name) end - local P, environment, interface, public_modifier, accessors, mutators, fields, public_accessors, public_mutators, public_fields - environment, interface, public_modifier = {}, {}, setmetatable({}, public_modifier_mt) - accessors = {M=function() return public_modifier end} - mutators = setmetatable({_=nop}, {__index=function(_, k) return function(v) define(name, k, v, true) end end}) - fields = setmetatable( - {_M=environment, _G=_G, include=function(interface) include(name, interface) end, nop=nop}, - {__index=function(_, k) local accessor = accessors[k]; if accessor then return accessor() else return _G[k] end end} +local function create_module(name) + local P, private, public, modifier + local modifier = setmetatable({}, { + __metatable=false, + __newindex=function(_, k, v) + public[k] = v + if rawget(private, k) == nil then + private[k] = v + end + end + }) + local env = setmetatable( + {_G=_G, _M=private, M=modifier, include=function(name) include(private, name) end, nop=nop}, + {__index=_G} ) - public_accessors = setmetatable({}, nop_default_mt) - public_mutators = setmetatable({}, nop_default_mt) - public_fields = setmetatable({}, {__index=function(_, k) return public_accessors[k]() end}) - setmetatable(environment, proxy_mt(fields, mutators)) - setmetatable(interface, proxy_mt(public_fields, public_mutators)) - P = { - defined = {['-_M']=true, ['-_G']=true, ['-include']=true, ['-nop']=true, ['-M']=true, ['-set__']=true, ['-require']=true}, - fields = fields, accessors = accessors, mutators = mutators, - public_fields = public_fields, public_accessors = public_accessors, public_mutators = public_mutators, - } - environments[name], interfaces[name] = environment, interface - loaded[name], loaded[public_modifier] = P, P + local public = {} + local interface = setmetatable({}, {__metatable=false, __index=public}) + P = {private=private, public=public} + environments[name], interfaces[name] = env, interface + loaded[name] = P end function module(name) if not loaded[name] then create_module(name) end defined[name] = true; setfenv(2, environments[name]) end diff --git a/tabs/post/core.lua b/tabs/post/core.lua index 56f004e..b5bf7b4 100644 --- a/tabs/post/core.lua +++ b/tabs/post/core.lua @@ -22,7 +22,7 @@ local settings_schema = {'tuple', '#', {duration='number'}, {start_price='number local scan_id, inventory_records, bid_records, buyout_records = 0, {}, {}, {} -function get_default_settings() +function get.default_settings() return O('duration', DURATION_8, 'start_price', 0, 'buyout_price', 0, 'hidden', false) end @@ -41,16 +41,16 @@ end do local bid_selections, buyout_selections = {}, {} - function get_bid_selection() + function get.bid_selection() return bid_selections[selected_item.key] end - function set_bid_selection(record) + function set.bid_selection(record) bid_selections[selected_item.key] = record end - function get_buyout_selection() + function get.buyout_selection() return buyout_selections[selected_item.key] end - function set_buyout_selection(record) + function set.buyout_selection(record) buyout_selections[selected_item.key] = record end end @@ -63,14 +63,14 @@ end do local item - function get_selected_item() return item end - function set_selected_item(v) item = v end + function get.selected_item() return item end + function set.selected_item(v) item = v end end do local c = 0 - function get_refresh() return c end - function set_refresh(v) c = v end + function get.refresh() return c end + function set.refresh(v) c = v end end function OPEN() @@ -88,21 +88,21 @@ function USE_ITEM(item_info) select_item(item_info.item_key) end -function get_unit_start_price() +function get.unit_start_price() return selected_item and read_settings().start_price or 0 end -function set_unit_start_price(amount) +function set.unit_start_price(amount) local settings = read_settings() settings.start_price = amount write_settings(settings) end -function get_unit_buyout_price() +function get.unit_buyout_price() return selected_item and read_settings().buyout_price or 0 end -function set_unit_buyout_price(amount) +function set.unit_buyout_price(amount) local settings = read_settings() settings.buyout_price = amount write_settings(settings) diff --git a/tabs/search/core.lua b/tabs/search/core.lua index 215c52c..d455c46 100644 --- a/tabs/search/core.lua +++ b/tabs/search/core.lua @@ -44,7 +44,7 @@ function USE_ITEM(item_info) execute(nil, false) end -function set_subtab(tab) +function set.subtab(tab) CloseDropDownMenus() search_results_button:UnlockHighlight() saved_searches_button:UnlockHighlight() @@ -65,7 +65,7 @@ function set_subtab(tab) end end -function M.set_filter(filter_string) +function M.set.filter(filter_string) search_box:SetFocus() search_box:SetText(filter_string) end diff --git a/tabs/search/filter.lua b/tabs/search/filter.lua index 2f97dfa..1031626 100644 --- a/tabs/search/filter.lua +++ b/tabs/search/filter.lua @@ -105,7 +105,7 @@ function update_form() end end -function get_filter_builder_query() +function get.filter_builder_query() local filter_string local function add(part) @@ -150,7 +150,7 @@ function get_filter_builder_query() return filter_string or '' end -function set_form(filter) +function set.form(filter) clear_form() for _, component in ipairs(filter.components) do if component[1] == 'blizzard' then @@ -184,7 +184,7 @@ end function import_filter_string() local filter, error = filter_util.parse_filter_string(select(3, strfind(search_box:GetText(), '^([^;]*)'))) if filter or print(error) then - set_form(filter) + set.form(filter) end update_form() end diff --git a/tabs/search/results.lua b/tabs/search/results.lua index c9b6efe..4378f20 100644 --- a/tabs/search/results.lua +++ b/tabs/search/results.lua @@ -12,10 +12,10 @@ end do local id = 0 - function get_search_scan_id() + function get.search_scan_id() return id end - function set_search_scan_id(v) + function set.search_scan_id(v) id = v end end @@ -36,7 +36,7 @@ do local searches = {} local search_index = 1 - function get_current_search() + function get.current_search() return searches[search_index] end diff --git a/tabs/search/saved.lua b/tabs/search/saved.lua index 4f79a0b..5bd1a16 100644 --- a/tabs/search/saved.lua +++ b/tabs/search/saved.lua @@ -82,7 +82,7 @@ handlers = { end } -function get_auto_buy_validator() +function get.auto_buy_validator() local validators = T for _, search in pairs(favorite_searches) do if search.auto_buy then diff --git a/util.lua b/util.lua index c30d9ea..04fc4ec 100644 --- a/util.lua +++ b/util.lua @@ -40,7 +40,7 @@ end M.huge = 1.8 * 10 ^ 308 -function M.get_modified() +function M.get.modified() return IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown() end diff --git a/util/info.lua b/util/info.lua index 661ea14..de6b05d 100644 --- a/util/info.lua +++ b/util/info.lua @@ -418,7 +418,7 @@ function M.item_quality_index(item_quality) end end -function M.get_inventory() +function M.get.inventory() local bag, slot = 0, 0 return function() if not GetBagName(bag) or slot >= GetContainerNumSlots(bag) then