big refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-22 16:24:00 +02:00
parent 54c082cff9
commit c0f8aece7e
11 changed files with 94 additions and 106 deletions
+16 -18
View File
@@ -41,25 +41,23 @@ do
end end
function LOAD() function LOAD()
armor = set( armor = set-from
'INVTYPE_HEAD', 'INVTYPE_HEAD'
'INVTYPE_NECK', 'INVTYPE_NECK'
'INVTYPE_SHOULDER', 'INVTYPE_SHOULDER'
'INVTYPE_BODY', 'INVTYPE_BODY'
'INVTYPE_CHEST', 'INVTYPE_CHEST'
'INVTYPE_ROBE', 'INVTYPE_ROBE'
'INVTYPE_WAIST', 'INVTYPE_WAIST'
'INVTYPE_LEGS', 'INVTYPE_LEGS'
'INVTYPE_FEET', 'INVTYPE_FEET'
'INVTYPE_WRIST', 'INVTYPE_WRIST'
'INVTYPE_HAND', 'INVTYPE_HAND'
'INVTYPE_FINGER', 'INVTYPE_FINGER'
'INVTYPE_TRINKET', 'INVTYPE_TRINKET'
'INVTYPE_CLOAK', 'INVTYPE_CLOAK'
'INVTYPE_HOLDABLE' 'INVTYPE_HOLDABLE'
) weapon = set-from
weapon = set
'INVTYPE_2HWEAPON' 'INVTYPE_2HWEAPON'
'INVTYPE_WEAPONMAINHAND' 'INVTYPE_WEAPONMAINHAND'
'INVTYPE_WEAPON' 'INVTYPE_WEAPON'
+2 -2
View File
@@ -37,13 +37,13 @@ do
call(on_complete) call(on_complete)
end end
function getter.state() function accessor.state()
local _, state = next(filter(scan_states, function(state) return state.id == thread_id end)) local _, state = next(filter(scan_states, function(state) return state.id == thread_id end))
return state return state
end end
end end
function getter.query() return state.params.queries[state.query_index] end function accessor.query() return state.params.queries[state.query_index] end
function wait_for_callback(...) function wait_for_callback(...)
local send_signal, signal_received = signal() local send_signal, signal_received = signal()
+1 -1
View File
@@ -44,7 +44,7 @@ end
do do
local id = 0 local id = 0
function getter.id() id = id + 1; return id end function accessor.id() id = id + 1; return id end
end end
function public.kill_listener(listener_id) function public.kill_listener(listener_id)
+5 -5
View File
@@ -64,13 +64,13 @@ tabs = {}
function public.tab(index, name) function public.tab(index, name)
local module_env = getfenv(2) local module_env = getfenv(2)
local tab = {name=name, env=module_env} local tab = {name=name, env=module_env}
function module_env.public.getter.ACTIVE() return tab == active_tab end function module_env.public.accessor.ACTIVE() return tab == active_tab end
for _, handler in tmp-{'OPEN', 'CLOSE', 'CLICK_LINK', 'USE_ITEM'} do module_env.mutable[handler] = nil end for handler in tmp-set-from . OPEN . CLOSE . CLICK_LINK . USE_ITEM do module_env.mutable[handler] = nil end
tabs[index] = tab tabs[index] = tab
end end
do do
local active_tab_index local active_tab_index
function getter.active_tab() return tabs[active_tab_index] end function accessor.active_tab() return tabs[active_tab_index] end
function on_tab_click(index) function on_tab_click(index)
call(active_tab_index and active_tab.env.CLOSE) call(active_tab_index and active_tab.env.CLOSE)
active_tab_index = index active_tab_index = index
@@ -89,7 +89,7 @@ end
do do
local locked local locked
function public.getter.bid_in_progress() return locked end function public.accessor.bid_in_progress() return locked end
function public.place_bid(type, index, amount, on_success) function public.place_bid(type, index, amount, on_success)
if locked then return end if locked then return end
local money = GetMoney() local money = GetMoney()
@@ -109,7 +109,7 @@ end
do do
local locked local locked
function public.getter.cancel_in_progress() return locked end function public.accessor.cancel_in_progress() return locked end
function public.cancel_auction(index, on_success) function public.cancel_auction(index, on_success)
if locked then return end if locked then return end
locked = true locked = true
+3 -3
View File
@@ -60,20 +60,20 @@ do
end end
end end
} }
public.getter.color = color_accessor(function(color) public.accessor.color = color_accessor(function(color)
local r, g, b, a = unpack(color) local r, g, b, a = unpack(color)
return setmetatable({r/255, g/255, b/255, a}, mt) return setmetatable({r/255, g/255, b/255, a}, mt)
end) end)
end end
public.getter.inline_color = color_accessor(function(color) public.accessor.inline_color = color_accessor(function(color)
local r, g, b, a = unpack(color) local r, g, b, a = unpack(color)
return format('|c%02X%02X%02X%02X', a, r, g, b) return format('|c%02X%02X%02X%02X', a, r, g, b)
end) end)
do do
local id = 0 local id = 0
function public.getter.name() id = id + 1; return 'aux_frame'..id end function public.accessor.name() id = id + 1; return 'aux_frame'..id end
end end
do do
+40 -50
View File
@@ -1,5 +1,5 @@
function INIT() function INIT()
import :_ 'core' :_ 'control' :_ 'util' import :_ 'core' :_ 'control' :_ 'util' -- TODO auto import 'core' instead
end end
module 'core' module 'core'
@@ -8,7 +8,7 @@ public.version = '5.0.0'
do do
local table_pool, temporary = {}, {} local table_pool, temporary = {}, {}
CreateFrame 'Frame' :SetScript('OnUpdate', function() CreateFrame'Frame':SetScript('OnUpdate', function()
for t in temporary do for t in temporary do
recycle(t) recycle(t)
log(getn(table_pool)) log(getn(table_pool))
@@ -24,48 +24,57 @@ do
end end
function public.recycle(t) function public.recycle(t)
auto_recycle[t] = nil temporary[t] = nil
wipe(t) wipe(t)
tinsert(table_pool, t) tinsert(table_pool, t)
end end
function public.getter.t() return function public.accessor.t() return
tremove(table_pool) or {} tremove(table_pool) or {}
end end
function public.getter.tt() function public.accessor.tt()
local t = tremove(table_pool) or {} local t = tremove(table_pool) or {}
temporary[t] = true temporary[t] = true
return t return t
end end
public.tmp = setmetatable({}, {__sub = function(_, t) temporary[t] = true return t end}) do
local mt = {
__call=function(self, arg) return self[1](arg) end,
__sub=function(self, arg) return self[1](arg) end,
}
function public.modifier(f)
local self = t
self[1] = f
return setmetatable(self, mt)
end
end
public.tmp = modifier(function(t)
temporary[t] = true
return t
end)
do do
local mt = { local mt = {
__call=function(self, arg1, arg2) tinsert(self, arg2 or arg1); return self; end, __call=function(self, arg1, arg2)
__index=function(self, key) tinsert(self, key); return self; end, tinsert(self, arg2 or arg1)
return self
end,
__index=function(self, key)
tinsert(self, key)
return self
end,
} }
function public.getter.from() function public.accessor.from()
return setmetatable(tt, mt) return setmetatable(tt, mt)
end end
end end
do
local mt = {
__call=function(self, arg) return self[1](arg) end,
__unm=function(self, arg) return self[1](arg) end,
}
function public.modifier(f)
local self = t; t[1] = f
return setmetatable(self, mt)
end
end
do do
local mt = {__call = function(self, key) return self[key] end} local mt = {__call = function(self, key) return self[key] end}
public.getter.set = modifier(function(table) public.set = modifier(function(table)
local self = t local self = t
for _, v in table do self[v] = true end for _, v in table do self[v] = true end
recycle(table) recycle(table)
@@ -73,45 +82,26 @@ do
end) end)
end end
public.array = modifier(function(table)
local self = t
for _, v in table do tinsert(self, v) end
recycle(table)
return self
end)
-- TODO map
function public.array(table)
local array = t
for k, v in table do tinsert(array, v) end
return array
end
do
local mt = {
__call=function(self, arg1, arg2) self[arg2 or arg1] = true; return self; end,
__index=function(self, key) self[key] = true; return self; end,
}
function public.getter.metaset()
return setmetatable(t, mt)
end
end
do
local mt = {
__call=function(self, arg1, arg2) self[arg2 or arg1] = true; return self; end,
__index=function(self, key) tinsert(self, key); return self; end,
}
function public.getter.metamap()
return setmetatable(t, mt)
end
end
end end
local event_frame = CreateFrame 'Frame' local event_frame = CreateFrame 'Frame'
for _, event in tmp-{'ADDON_LOADED', 'VARIABLES_LOADED', 'PLAYER_LOGIN', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE'} do for event in tmp-set-from . 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) event_frame:RegisterEvent(event)
end end
ADDON_LOADED = t ADDON_LOADED = t
do do
local variables_loaded_hooks, player_login_hooks = t, t local variables_loaded_hooks, player_login_hooks = t, t
function public.setter.LOAD(f) tinsert(variables_loaded_hooks, f) end function public.mutator.LOAD(f) tinsert(variables_loaded_hooks, f) end
function public.setter.LOAD2(f) tinsert(player_login_hooks, f) end function public.mutator.LOAD2(f) tinsert(player_login_hooks, f) end
event_frame:SetScript('OnEvent', function() event_frame:SetScript('OnEvent', function()
if event == 'ADDON_LOADED' then if event == 'ADDON_LOADED' then
if ADDON_LOADED[arg1] then ADDON_LOADED[arg1]() end if ADDON_LOADED[arg1] then ADDON_LOADED[arg1]() end
+17 -17
View File
@@ -1,7 +1,7 @@
local type, setmetatable, setfenv, unpack, mask, _g = type, setmetatable, setfenv, unpack, bit.band, getfenv(0) local type, setmetatable, setfenv, unpack, mask, _g = type, setmetatable, setfenv, unpack, bit.band, getfenv(0)
local PRIVATE, PUBLIC, MUTABLE, PROPERTY, GETTER, SETTER = 0, 1, 2, 4, 8, 16 local PRIVATE, PUBLIC, MUTABLE, PROPERTY, ACCESSOR, MUTATOR = 0, 1, 2, 4, 8, 16
local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE, getter=GETTER+PROPERTY, setter=SETTER+PROPERTY} local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE, accessor=ACCESSOR+PROPERTY, mutator=MUTATOR+PROPERTY}
local MODIFIER_MASK, PROPERTY_MASK = {private=MUTABLE+GETTER+SETTER, public=MUTABLE+GETTER+SETTER, mutable=PRIVATE+PUBLIC, getter=PRIVATE+PUBLIC, setter=PRIVATE+PUBLIC}, PRIVATE+PUBLIC local MODIFIER_MASK, PROPERTY_MASK = {private=MUTABLE+ACCESSOR+MUTATOR, public=MUTABLE+ACCESSOR+MUTATOR, mutable=PRIVATE+PUBLIC, accessor=PRIVATE+PUBLIC, mutator=PRIVATE+PUBLIC}, PRIVATE+PUBLIC
local error, declaration_error, immutable_error, collision_error, void_error, set_property, lock_mt, env_mt, interface_mt, declarator_mt, importer_mt local error, declaration_error, immutable_error, collision_error, void_error, set_property, lock_mt, env_mt, interface_mt, declarator_mt, importer_mt
lock_mt = {} lock_mt = {}
local _state, _modules = {}, setmetatable({}, lock_mt) local _state, _modules = {}, setmetatable({}, lock_mt)
@@ -21,7 +21,7 @@ function importer_mt.__call(self, arg1, arg2)
if alias == '_' then if alias == '_' then
for key, modifiers in module.metadata do for key, modifiers in module.metadata do
if not state.metadata[key] and mask(PUBLIC, modifiers) ~= 0 then if not state.metadata[key] and mask(PUBLIC, modifiers) ~= 0 then
state.metadata[key], state.data[key], state.getters[key], state.setters[key] = modifiers, module.data[key], module.getters[key], module.setters[key] state.metadata[key], state.data[key], state.accessors[key], state.mutators[key] = modifiers, module.data[key], module.accessors[key], module.mutators[key]
end end
end end
elseif not state.metadata[alias] then elseif not state.metadata[alias] then
@@ -53,7 +53,7 @@ function declarator_mt.__newindex(self, key, value)
if mask(PROPERTY, state.modifiers) == 0 then if mask(PROPERTY, state.modifiers) == 0 then
state.data[key] = value state.data[key] = value
elseif type(value) == 'function' or declaration_error(2) then elseif type(value) == 'function' or declaration_error(2) then
local data = mask(GETTER, state.modifiers) ~= 0 and state.getters or state.setters local data = mask(ACCESSOR, state.modifiers) ~= 0 and state.accessors or state.mutators
state.property, data[key] = key, value state.property, data[key] = key, value
end end
state.modifiers = PRIVATE state.modifiers = PRIVATE
@@ -66,8 +66,8 @@ do
if modifiers and mask(access+PROPERTY, modifiers) == access then if modifiers and mask(access+PROPERTY, modifiers) == access then
return state.data[key] return state.data[key]
else else
local getter = state.getters[key] local accessor = state.accessors[key]
if getter then return getter() else return default[key] or void_error(key, 2) end if accessor then return accessor() else return default[key] or void_error(key, 2) end
end end
end end
end end
@@ -75,8 +75,8 @@ do
function env_mt.__newindex(self, key, value) function env_mt.__newindex(self, key, value)
local state = _state[self]; local modifiers = state.metadata[key] local state = _state[self]; local modifiers = state.metadata[key]
if modifiers then if modifiers then
local setter = state.setters[key] local mutator = state.mutators[key]
if setter then return setter(value) end if mutator then return mutator(value) end
if mask(MUTABLE, modifiers) == 0 then immutable_error(key, 2) end if mask(MUTABLE, modifiers) == 0 then immutable_error(key, 2) end
else else
state.metadata[key] = state.modifiers state.metadata[key] = state.modifiers
@@ -86,8 +86,8 @@ do
interface_mt = {__metatable=false, __index=index(PUBLIC, {})} interface_mt = {__metatable=false, __index=index(PUBLIC, {})}
function interface_mt.__newindex(self, key, value) function interface_mt.__newindex(self, key, value)
local state = _state[self]; local metadata = state.metadata or void_error(key, 2) local state = _state[self]; local metadata = state.metadata or void_error(key, 2)
if mask(PUBLIC+SETTER, metadata) == PUBLIC+SETTER then if mask(PUBLIC+MUTATOR, metadata) == PUBLIC+MUTATOR then
return state.setters[key](value) return state.mutators[key](value)
elseif mask(PUBLIC, metadata) == PUBLIC or immutable_error(key, 2) then elseif mask(PUBLIC, metadata) == PUBLIC or immutable_error(key, 2) then
state.data[key] = value state.data[key] = value
end end
@@ -96,17 +96,17 @@ end
function INIT() end function INIT() end
function module(name) function module(name)
if not _modules[name] then if not _modules[name] then
local state, getters, setters, env, interface, declarator, importer local state, accessors, mutators, env, interface, declarator, importer
env, interface, declarator, importer = setmetatable({}, env_mt), setmetatable({}, interface_mt), setmetatable({}, declarator_mt), setmetatable({}, importer_mt) env, interface, declarator, importer = setmetatable({}, env_mt), setmetatable({}, interface_mt), setmetatable({}, declarator_mt), setmetatable({}, importer_mt)
getters = { accessors = {
private=function() state.modifiers = PRIVATE return declarator end, public=function() state.modifiers = PUBLIC return declarator end, private=function() state.modifiers = PRIVATE return declarator end, public=function() state.modifiers = PUBLIC return declarator end,
mutable=function() state.modifiers = MUTABLE return declarator end, mutable=function() state.modifiers = MUTABLE return declarator end,
getter=function() state.modifiers = PROPERTY+GETTER return declarator end, setter=function() state.modifiers = PROPERTY+SETTER return declarator end} accessor=function() state.modifiers = PROPERTY+ACCESSOR return declarator end, mutator=function() state.modifiers = PROPERTY+MUTATOR return declarator end}
setters = {getter=function(value) set_property(getters, state.property, value) end, setter=function(value) set_property(setters, state.property, value) end} mutators = {accessor=function(value) set_property(accessors, state.property, value) end, mutator=function(value) set_property(mutators, state.property, value) end}
state = { state = {
env=env, interface=interface, modifiers=PRIVATE, env=env, interface=interface, modifiers=PRIVATE,
metadata = setmetatable({_g=PRIVATE, _m=PRIVATE, _i=PRIVATE, import=PRIVATE, private=PROPERTY+GETTER, public=PROPERTY+GETTER, mutable=PROPERTY+GETTER, getter=PROPERTY+GETTER+SETTER, setter=PROPERTY+GETTER+SETTER}, lock_mt), metadata = setmetatable({_g=PRIVATE, _m=PRIVATE, _i=PRIVATE, import=PRIVATE, private=PROPERTY+ACCESSOR, public=PROPERTY+ACCESSOR, mutable=PROPERTY+ACCESSOR, accessor=PROPERTY+ACCESSOR+MUTATOR, mutator=PROPERTY+ACCESSOR+MUTATOR}, lock_mt),
data = {_g=_g, _m=env, _i=interface, import=importer}, getters=getters, setters=setters, 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 _modules[name], _state[env], _state[interface], _state[declarator], _state[importer] = state, state, state, state, state
setfenv(INIT, env); INIT() setfenv(INIT, env); INIT()
+1 -1
View File
@@ -9,7 +9,7 @@ do
local searches = {} local searches = {}
local search_index = 1 local search_index = 1
function getter.current_search() return searches[search_index] end function accessor.current_search() return searches[search_index] end
function update_search(index) function update_search(index)
searches[search_index].status_bar:Hide() searches[search_index].status_bar:Hide()
+5 -5
View File
@@ -15,15 +15,15 @@ end
do do
local value, charges local value, charges
local function setter(n) local function mutator(n)
return function(v) return function(v)
assert(charges == 0) assert(charges == 0)
value, charges = v, n value, charges = v, n
return v return v
end end
end end
for i=1,9 do public[join{replicate(i, 'x')}] = setter(i) end for i=1,9 do public[join{replicate(i, 'x')}] = mutator(i) end
function public.getter.__() function public.accessor.__()
assert(charges > 0) assert(charges > 0)
charges = charges - 1 charges = charges - 1
return value return value
@@ -85,7 +85,7 @@ end
public.huge = 1.8*10^308 public.huge = 1.8*10^308
function public.getter.modified() return IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown() end function public.accessor.modified() return IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown() end
--do TODO --do TODO
-- local _state = setmetatable({}, {__mode='kv'}) -- local _state = setmetatable({}, {__mode='kv'})
@@ -233,7 +233,7 @@ end
function public.round(x) return floor(x + 0.5) end function public.round(x) return floor(x + 0.5) end
function public.getter.inventory() function public.accessor.inventory()
local bag, slot = 0, 0 local bag, slot = 0, 0
return function() return function()
if not GetBagName(bag) or slot >= GetContainerNumSlots(bag) then if not GetBagName(bag) or slot >= GetContainerNumSlots(bag) then
+2 -2
View File
@@ -267,10 +267,10 @@ function public.auctionable(tooltip, quality, lootable)
and not (durability and durability < max_durability) and not (durability and durability < max_durability)
end end
function public.tooltip(setter) function public.tooltip(mutator)
AuxTooltip:SetOwner(UIParent, 'ANCHOR_NONE') AuxTooltip:SetOwner(UIParent, 'ANCHOR_NONE')
AuxTooltip.money = 0 AuxTooltip.money = 0
setter(AuxTooltip) mutator(AuxTooltip)
AuxTooltip:Show() AuxTooltip:Show()
local tooltip = {} local tooltip = {}
+2 -2
View File
@@ -10,7 +10,7 @@ do
realm = GetCVar 'realmName' realm = GetCVar 'realmName'
end end
function getter.dataset_key() return realm..'|'..faction end function accessor.dataset_key() return realm..'|'..faction end
end end
function public.load_dataset() function public.load_dataset()
@@ -26,7 +26,7 @@ end
-- end, -- end,
-- __call = function(self, field) end -- __call = function(self, field) end
-- } -- }
-- function public.getter.record() -- function public.accessor.record()
-- --
-- end -- end
--end --end