From c2cef5b14218caf93b1413191bb28b8a08ce3090 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Mon, 22 Aug 2016 22:30:16 +0200 Subject: [PATCH] big refactoring --- aux-addon.lua | 59 ++++--------------------------------------- control.lua | 4 +-- core/disenchant.lua | 18 ++++++------- core/slash.lua | 6 ++--- module.lua | 19 +++++++------- tabs/search/frame.lua | 2 +- util/core.lua | 44 ++++++++++++++++---------------- util/info.lua | 2 +- 8 files changed, 52 insertions(+), 102 deletions(-) diff --git a/aux-addon.lua b/aux-addon.lua index c2004d3..60b4bdc 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -65,12 +65,11 @@ do end) do - local mt = {__call = function(self, key) return self[key] end} - public.set = modifier(function(table) - local self = t - for _, v in table do self[v] = true end - recycle(table) - return setmetatable(self, mt) + public.set = modifier(function(t) + local self = _m.t + for _, v in t do self[v] = true end + recycle(t) + return self end) end @@ -113,54 +112,6 @@ do end) end -function LOAD() - import :_ 'util' 'gui' - do - local frame = CreateFrame('Frame', gui.name, UIParent) - tinsert(UISpecialFrames, 'aux_frame1') - gui.set_window_style(frame) - gui.set_size(frame, 768, 447) - frame:SetPoint('LEFT', 100, 0) - frame:SetToplevel(true) - frame:SetMovable(true) - frame:EnableMouse(true) - frame:SetClampedToScreen(true) - frame:RegisterForDrag 'LeftButton' - frame:SetScript('OnDragStart', function() this:StartMoving() end) - frame:SetScript('OnDragStop', function() this:StopMovingOrSizing() end) - frame:SetScript('OnShow', function() PlaySound 'AuctionWindowOpen' end) - frame:SetScript('OnHide', function() PlaySound 'AuctionWindowClose' CloseAuctionHouse() end) - frame.content = CreateFrame('Frame', nil, frame) - frame.content:SetPoint('TOPLEFT', 4, -80) - frame.content:SetPoint('BOTTOMRIGHT', -4, 35) - frame:Hide() - public.frame = frame - end - do - local tabs = gui.tabs(frame, 'DOWN') - tabs._on_select = on_tab_click - for _, tab in _m.tabs do tabs:create_tab(tab.name) end - function public.set_tab(id) tabs:select(id) end - end - do - local btn = gui.button(frame, 16) - btn:SetPoint('BOTTOMRIGHT', -6, 6) - gui.set_size(btn, 65, 24) - btn:SetText 'Close' - btn:SetScript('OnClick', L(frame.Hide, frame)) - public.close_button = btn - end - do - local btn = gui.button(frame, 16) - btn:SetPoint('RIGHT', close_button, 'LEFT' , -5, 0) - gui.set_size(btn, 65, 24) - btn:SetText 'Default UI' - btn:SetScript('OnClick',function() - if AuctionFrame:IsVisible() then HideUIPanel(AuctionFrame) else ShowUIPanel(AuctionFrame) end - end) - end -end - function public.log(...) local msg = '[aux]' for i=1,arg.n do msg = msg..' '..tostring(arg[i]) end diff --git a/control.lua b/control.lua index eaff7ed..95dbcf2 100644 --- a/control.lua +++ b/control.lua @@ -1,8 +1,8 @@ module 'aux' event_frame = CreateFrame 'Frame' -listeners = {} -threads = {} +listeners = t +threads = t public.thread_id = nil function LOAD() diff --git a/core/disenchant.lua b/core/disenchant.lua index 3a07105..84915a7 100644 --- a/core/disenchant.lua +++ b/core/disenchant.lua @@ -1,8 +1,6 @@ module 'disenchant' -UNCOMMON = 2 -RARE = 3 -EPIC = 4 +UNCOMMON, RARE, EPIC = 2, 3, 4 do local data = { @@ -41,7 +39,7 @@ do end function LOAD() - armor = set-from + ARMOR = set-from 'INVTYPE_HEAD' 'INVTYPE_NECK' 'INVTYPE_SHOULDER' @@ -57,7 +55,7 @@ function LOAD() 'INVTYPE_TRINKET' 'INVTYPE_CLOAK' 'INVTYPE_HOLDABLE' - weapon = set-from + WEAPON = set-from 'INVTYPE_2HWEAPON' 'INVTYPE_WEAPONMAINHAND' 'INVTYPE_WEAPON' @@ -81,14 +79,14 @@ function public.value(slot, quality, level) end function public.distribution(slot, quality, level) - if not (armor(slot) or weapon(slot)) or level == 0 then - return {} + if not (ARMOR[slot] or WEAPON[slot]) or level == 0 then + return tt end local function p(probability_armor, probability_weapon) - if armor(slot) then + if ARMOR[slot] then return probability_armor - elseif weapon(slot) then + elseif WEAPON[slot] then return probability_weapon end end @@ -193,5 +191,5 @@ function public.distribution(slot, quality, level) return {{item_id=20725, min_quantity=1, max_quantity=2, probability=1}} end end - return {} + return tt end \ No newline at end of file diff --git a/core/slash.lua b/core/slash.lua index e6ef51e..ef0a630 100644 --- a/core/slash.lua +++ b/core/slash.lua @@ -52,7 +52,7 @@ function SlashCmdList.AUX(command) local realm = GetCVar 'realmName' _g.aux_characters[realm] = _g.aux_characters[realm] or {} for i=3,getn(arguments) do - local name = string.gsub(strlower(arguments[i]), '^%l', strupper) + local name = gsub(strlower(arguments[i]), '^%l', strupper) if not _g.aux_characters[realm][name] then _g.aux_characters[realm][name] = true log('Character "'..name..'" added.') @@ -64,7 +64,7 @@ function SlashCmdList.AUX(command) return end for i=3,getn(arguments) do - local name = string.gsub(strlower(arguments[i]), '^%l', strupper) + local name = gsub(strlower(arguments[i]), '^%l', strupper) if _g.aux_characters[realm][name] then _g.aux_characters[realm][name] = nil log('Character "'..name..'" removed.') @@ -77,7 +77,7 @@ function SlashCmdList.AUX(command) tinsert(chars, name) end if getn(chars) > 0 then - log('Your characters: "'..table.concat(chars, ', ')..'".') + log('Your characters: "'..join(chars, ', ')..'".') else log 'You don\'t have any additional characters. To add your characters type "/aux chars add NAME1 NAME2 NAME3...".' end diff --git a/module.lua b/module.lua index 731799f..4f6327d 100644 --- a/module.lua +++ b/module.lua @@ -1,21 +1,23 @@ local type, setmetatable, setfenv, unpack, mask, _g = type, setmetatable, setfenv, unpack, bit.band, getfenv(0) local PRIVATE, PUBLIC, MUTABLE, PROPERTY, ACCESSOR, MUTATOR = 0, 1, 2, 4, 8, 16 -local error, modifier_error, property_error, immutable_error, collision_error, set_property, env_mt, interface_mt, declarator_mt, importer_mt +local error, import_error, modifier_error, property_error, immutable_error, collision_error, set_property, env_mt, interface_mt, declarator_mt, importer_mt local _state, _modules = {}, {} function error(message, ...) return function() _g.error(format(message, unpack(arg))..'\n'..debugstack(3, 10, 0), 0) end end -modifier_error = error 'Invalid modifiers.' -property_error = error 'Accessor/Mutator must be function.' -function immutable_error(key) return error('Field "%s" is immutable.', key) end -function collision_error(key) return error('Field "%s" already exists.', key) end +import_error, modifier_error, property_error = error 'Invalid modifiers.', error 'Invalid modifiers.', error 'Accessor/Mutator must be function.' +immutable_error, collision_error = function(key) return error('Field "%s" is immutable.', key) end, function(key) return error('Field "%s" already exists.', key) end importer_mt = {__metatable=false} -function importer_mt.__index(self, key) _state[self][self] = key; return self end +function importer_mt.__index(self, key) + if type(key) ~= 'string' then import_error() end + _state[self][self] = key; return self +end function importer_mt.__call(self, arg1, arg2) local name, state, module, alias name = arg2 or arg1 + if type(name) ~= 'string' then import_error() end state, module = _state[self], _modules[name] alias, state[self] = state[self] or name, nil if module then - if alias == '_' then + if alias == '' then for key, modifiers in module.metadata do if not state.metadata[key] and mask(PUBLIC, modifiers) ~= 0 then state.metadata[key], state.data[key], state.accessors[key], state.mutators[key] = modifiers, module.data[key], module.accessors[key], module.mutators[key] @@ -90,7 +92,6 @@ do end end end -function INIT() end function module(name) if not _modules[name] then local state, accessors, mutators, env, interface, declarator, importer @@ -106,7 +107,7 @@ function module(name) data = {_g=_g, _m=env, _i=interface, import=importer}, accessors=accessors, mutators=mutators, } _modules[name], _state[env], _state[interface], _state[declarator], _state[importer] = state, state, state, state, state - setfenv(INIT, env); INIT() + importer [''] (ADDON) end setfenv(2, _modules[name].env) end \ No newline at end of file diff --git a/tabs/search/frame.lua b/tabs/search/frame.lua index 5afbf2e..5aa876f 100644 --- a/tabs/search/frame.lua +++ b/tabs/search/frame.lua @@ -315,7 +315,7 @@ function create_frames() if filters then tinsert(_g.aux_favorite_searches, 1, { filter_string = search_box:GetText(), - prettified = table.concat(map(filters, function(filter) return filter.prettified end), ';'), + prettified = join(map(filters, function(filter) return filter.prettified end), ';'), }) end update_search_listings() diff --git a/util/core.lua b/util/core.lua index 5a3b827..3e244d5 100644 --- a/util/core.lua +++ b/util/core.lua @@ -1,13 +1,6 @@ module 'util' -function public.copy(t) - local copy = {} - for k, v in t do copy[k] = v end - table.setn(t, getn(t)) - return copy -end - -public.join = _g.table.concat +public.join = _g.join function public.replicate(count, value) if count > 0 then return value, replicate(count - 1, value) end @@ -112,9 +105,16 @@ do end function public.expand(array, ...) - local table = {} - for i=1,arg.n do table[arg[i]] = array[i] end - return table + local t = t + for i=1,arg.n do t[arg[i]] = array[i] end + return t +end + +function public.copy(t) + local copy = _m.t + for k, v in t do copy[k] = v end + table.setn(t, getn(t)) + return copy end function public.select(i, ...) @@ -135,13 +135,13 @@ function public.key(value, t) end function public.keys(t) - local ks = {} + local ks = _m.t for k in t do tinsert(ks, k) end return ks end function public.values(t) - local vs = {} + local vs = _m.t for _, v in t do tinsert(vs, v) end return vs end @@ -185,20 +185,20 @@ function public.all(xs, p) return true end -function public.filter(xs, p) - local ys = {} - for k, x in xs do +function public.filter(t0, p) + local t = t + for k, x in t0 do if p(x, k) then - ys[k] = x + t[k] = x end end - return ys + return t end -function public.map(xs, f) - local ys = {} - for k, x in xs do ys[k] = f(x, k) end - return ys +function public.map(t0, f) + local t = t + for k, x in t0 do t[k] = f(x, k) end + return t end function public.trim(str) diff --git a/util/info.lua b/util/info.lua index 7575f7e..6f5208a 100644 --- a/util/info.lua +++ b/util/info.lua @@ -166,7 +166,7 @@ function public.bid_update(auction_record) auction_record.unit_blizzard_bid = auction_record.blizzard_bid / auction_record.aux_quantity auction_record.unit_bid_price = auction_record.bid_price / auction_record.aux_quantity auction_record.high_bidder = 1 - auction_record.search_signature = table.concat({auction_record.item_id, auction_record.suffix_id, auction_record.enchant_id, auction_record.start_price, auction_record.buyout_price, auction_record.bid_price, auction_record.aux_quantity, auction_record.duration, 1, _g.aux_ignore_owner and (is_player(auction_record.owner) and 0 or 1) or (auction_record.owner or '?')}, ':') + auction_record.search_signature = join({auction_record.item_id, auction_record.suffix_id, auction_record.enchant_id, auction_record.start_price, auction_record.buyout_price, auction_record.bid_price, auction_record.aux_quantity, auction_record.duration, 1, _g.aux_ignore_owner and (is_player(auction_record.owner) and 0 or 1) or (auction_record.owner or '?')}, ':') end function public.set_tooltip(itemstring, owner, anchor)