refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-27 11:50:42 +02:00
parent bf8f8250d3
commit 77caf8c063
3 changed files with 50 additions and 71 deletions
+21 -36
View File
@@ -2,36 +2,29 @@ aux 'tooltip' local info, disenchant, cache, money, history = aux.info, aux.dise
_G.aux_tooltip_value = true
game_tooltip_hooks = t
do
local c
hooked_setter
{
get = function() return c end,
set = function(v) c = v end,
}
end
do
local amount
game_tooltip_money
{
get = function() return amount end,
set = function(v) amount = v end,
}
end
local game_tooltip_hooks, game_tooltip_money, inside_hook = t, 0, false
function LOAD()
for name, f in game_tooltip_hooks do
local name, f = name, f
M.hook(name, GameTooltip, function(...) temp=arg
hooked_setter = true
game_tooltip_money = 0
local ret = {orig[GameTooltip][name](unpack(arg))}
hooked_setter = false
f(unpack(arg))
return unpack(ret)
end)
do
for name, f in game_tooltip_hooks do
local name, f = name, f
hook(name, GameTooltip, function(...) temp=arg
inside_hook = true
game_tooltip_money = 0
local ret = {orig[GameTooltip][name](unpack(arg))}
inside_hook = false
f(unpack(arg))
return unpack(ret)
end)
end
local orig = GameTooltip:GetScript 'OnTooltipAddMoney'
GameTooltip:SetScript('OnTooltipAddMoney', function(...) temp=arg
if inside_hook then
game_tooltip_money = arg1
else
return orig(unpack(arg))
end
end)
end
local orig = SetItemRef
setglobal('SetItemRef', function(...) temp=arg
@@ -44,14 +37,6 @@ function LOAD()
end
return result
end)
local orig = GameTooltip:GetScript 'OnTooltipAddMoney'
GameTooltip:SetScript('OnTooltipAddMoney', function(...) temp=arg
if hooked_setter then
game_tooltip_money = arg1
else
return orig(unpack(arg))
end
end)
end
function extend_tooltip(tooltip, link, quantity)
+28 -25
View File
@@ -2,7 +2,7 @@ if module then return end
local typeof, tostring, setmetatable, setfenv, unpack, next, pcall, _G = type, tostring, setmetatable, setfenv, unpack, next, pcall, getfenv(0)
local PUBLIC, PRIVATE = 1, 2
local GETTER, SETTER, FUNCTION = 1, 2, 3
local FUNCTION, GETTER, SETTER = 1, 2, 3
local function error(msg, ...) return _G.error(format(msg or '', unpack(arg))..'\n'..debugstack(), 0) end
local function import_error() error 'Invalid imports.' end
@@ -14,15 +14,14 @@ local function const(v) return function() return v end end
local _state = {}
local declarator_mt, env_mt = {__metatable=false}, {__metatable=false}
local declarator_mt = {__metatable=false}
do
local ACCESS, TYPE = {public=PUBLIC, private=PRIVATE}, {call=FUNCTION, get=GETTER, set=SETTER}
local access, name
local function extract(v)
local call, get, set
call, get, set, v.call, v.get, v.set = v.call, v.get, v.set, nil, nil, nil
if next(v) or call ~= nil and get ~= nil then error() end
return call, get, set
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(state, access, name, data)
state.access[name] = state.access[name] and collision_error(name) or access or state.default_access
@@ -33,37 +32,38 @@ do
state[type][name] = value
end
end
function declarator_mt:__index(key) local state=_state[self]
if ACCESS[key] and not access then
access = ACCESS[key]
elseif not name or declaration_error() then
name = key
if ACCESS[key] and not state.declaration_access then
state.declaration_access = ACCESS[key]
elseif not state.declarator_name or declaration_error() then
state.declarator_name = key
end
return self
end
function declarator_mt:__newindex(key, value) local state=_state[self]
-- if module.access[key] then return end
local type
if name then
if state.declarator_name then
type = TYPE[key] or declaration_error()
else
type = typeof(value) == 'function' and FUNCTION or GETTER
end
declare(state, access, key, {[type]=value})
access, name = nil, nil
declare(state, state.declaration_access, key, {[type]=value})
state.declaration_access, state.declarator_name = nil, nil
end
function declarator_mt:__call(value) local state=_state[self]
if name then
local success, call, get, set = pcall(extract, value)
if state.declarator_name then
local success, f, get, set = pcall(extract, value)
if not success then declaration_error() end
declare(state, access, name, {[FUNCTION]=call, [GETTER]=get, [SETTER]=set})
elseif access or declaration_error() then
state.default_access = access
declare(state, state.declaration_access, state.declarator_name, {[FUNCTION]=f, [GETTER]=get, [SETTER]=set})
elseif state.declaration_access or declaration_error() then
state.default_access = state.declaration_access
end
access, name = nil, nil
state.declaration_access, state.declarator_name = nil, nil
end
end
local env_mt = {__metatable=false}
function env_mt:__index(key) local state=_state[self]
local get = state[GETTER][key]
if get then return get() end
@@ -88,9 +88,12 @@ function interface_mt:__newindex(key, value) local state=_state[self]
end
function module(...)
local declarator, env, interface = setmetatable({}, declarator_mt), setmetatable({}, env_mt), setmetatable({}, interface_mt)
local access = {error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, public=PRIVATE, private=PRIVATE}
local functions, getters, setters = {error=error, nop=nop}, {_G=const(_G), M=const(env), I=const(interface)}, {}
local state, declarator, env, interface, access, functions, getters, setters
declarator, env, interface = setmetatable({}, declarator_mt), setmetatable({}, env_mt), setmetatable({}, interface_mt)
access = {error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, public=PRIVATE, private=PRIVATE}
functions = {error=error, nop=nop}
getters = {_G=const(_G), M=const(env), I=const(interface), public=function() state.declaration_access = PUBLIC return declarator end, private=function() state.declaration_access = PRIVATE return declarator end}
setters = {}
for i=1,arg.n do
local module = _state[arg[i] or import_error()] or import_error()
local import_functions, import_getters, import_setters = module[FUNCTION], module[GETTER], module[SETTER]
@@ -101,7 +104,7 @@ function module(...)
end
end
end
local state = {access=access, [FUNCTION]=functions, [GETTER]=getters, [SETTER]=setters, default_access=PRIVATE}
state = {access=access, [FUNCTION]=functions, [GETTER]=getters, [SETTER]=setters, default_access=PRIVATE}
_state[declarator], _state[env], _state[interface] = state, state, state
setfenv(2, env)
end
+1 -10
View File
@@ -30,17 +30,8 @@ function write_settings(settings, item_key)
data[item_key] = persistence.write(settings_schema, settings)
end
existing_auctions = t
inventory_records = t
local scan_id, inventory_records, existing_auctions = 0, t, t
do
local id = 0
scan_id
{
get = function() return id end,
set = function(v) id = v end,
}
end
do
local item
selected_item