diff --git a/aux-addon.lua b/aux-addon.lua index 5389c73..559ad26 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -1,7 +1,65 @@ --aux_account_settings = {} -- TODO clean up the mess of savedvariables --aux_character_settings = {} -module() +do + local modules = {} + aux = function(name) + if not modules[name] then + module() + modules[name] = M + import (green_t) + private.aux = setmetatable({}, {__metatable=false, __index=function(_, key) return modules[key].I end, __newindex=error}) + end + modules[name].import (modules.core.I) + setfenv(2, modules[name]) + end +end + +--do +-- local call_method, call_self +-- local function generic_call(...) +-- return call_method(call_self, unpack(arg)) +-- end +-- +-- local state = setmetatable({}, {__mode='k'}) +-- local mt = {__metatable=false} +-- function mt:__index(self, key) self=state[self] +-- local getter = self.class.getters[key] +-- if getter then return getter(self.self) end +-- local method = self.class.methods[key] +-- if method then +-- call_method, call_self = method, self.self +-- return generic_call +-- end +-- end +-- function mt:__newindex(self, key, value) self=state[self] +-- local setter = self.class.setters[key] +-- if setter then return setter(self.self, value) end +-- end +-- -- function mt:__unm(self) self=state[self] +-- -- reclaim() +-- -- end +-- local function instantiate(methods, getters, setters, init, arg) +-- local interface = setmetatable({}, mt) +-- local self = {I=interface}; init(self, unpack(arg)) +-- state[interface] = {class={methods=methods, getters=getters, setters=setters}, self=self} +-- return interface +-- end +-- function public.class(methods, getters, setters, init) +-- return function(arg) instantiate(methods, getters, setters, init, arg) end +-- end +--end +-- +--do +-- local state = {} +-- local mt = {__metatable=false, __newindex=error} +-- function mt:__index(key) return state[self][key] end +-- function public.immutable(t) +-- return class({}, t, {}, function() end, {}) +-- end +--end + +aux 'core' public.version = '5.0.0' @@ -11,25 +69,6 @@ function public.log(...) temp=arg DEFAULT_CHAT_FRAME:AddMessage(LIGHTYELLOW_FONT_COLOR_CODE..msg) end -do - local modules = {core={env=M, interface=I}} - _G.aux = setmetatable({}, { - __metatable = false, - __index = function(_, key) return modules[key].interface end, - __newindex = error, - __call = function(self, name) - if not modules[name] then - module() - private.aux = self - modules[name] = {env=M, interface=I} - end - modules[name].env.import(modules.core.interface) - setfenv(2, modules[name].env) - end, - }) -end -local aux = aux - local bids_loaded function public.bids_loaded.get() return bids_loaded end @@ -41,177 +80,6 @@ function public.current_owner_page.get() return current_owner_page end -- function public.F.get(arg) arg.f = tremove(arg, 1) return setmetatable(arg, mt) end --end -do - local next, getn, setn, setmetatable, rawget = next, getn, table.setn, setmetatable, rawget - local wipe, recycle, table, temp_table - local pool, pool_size = {}, 0 - local overflow_pool = setmetatable({}, {__mode='k'}) - local tmp = {} - - CreateFrame('Frame'):SetScript('OnUpdate', function() - for t in tmp do recycle(t) end - wipe(tmp) - end) - - function wipe(t) -- like with a cloth or something - for k in t do t[k] = nil end - t.reset = 1 - t.reset = nil - setn(t, 0) - return setmetatable(t, nil) - end - public.wipe = wipe - - function recycle(t) - wipe(t) - if pool_size < 50 then - pool_size = pool_size + 1 - pool[pool_size] = t - else - overflow_pool[t] = true - end - end - public.recycle = recycle - - function table() - if pool_size > 0 then - pool_size = pool_size - 1 - return pool[pool_size + 1] - end - local t = next(overflow_pool) - if t then - overflow_pool[t] = nil - return t - end - return {} - end - public.t.get = table - - function public.tt.get() - local t - if pool_size > 0 then - t = pool[pool_size] - pool_size = pool_size - 1 - else - t = next(overflow_pool) - if t then - overflow_pool[t] = nil - else - t = {} - end - end - tmp[t] = true - return t - end - - do - local function modifier_mt(f) - local function apply(self, value) - recycle(self) - return f(value) - end - return {__call=apply, __sub=apply} - end - do - local mt = modifier_mt(function(t) tmp[t] = true; return t end) - public.temp - { - get = function() return setmetatable(table(), mt) end, - set = function(t) tmp[t] = true end, - } - end - do - local mt = modifier_mt(function(t) tmp[t] = nil; return t end) - public.perm - { - get = function() return setmetatable(table(), mt) end, - set = function(t) tmp[t] = nil end, - } - end - end - do - 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) - if k1 == nil then return end; t[k1] = true - if k2 == nil then return end; t[k2] = true - if k3 == nil then return end; t[k3] = true - if k4 == nil then return end; t[k4] = true - if k5 == nil then return end; t[k5] = true - if k6 == nil then return end; t[k6] = true - if k7 == nil then return end; t[k7] = true - if k8 == nil then return end; t[k8] = true - if k9 == nil then return end; t[k9] = true - if k10 == nil then return end; t[k10] = true - if k11 == nil then return end; t[k11] = true - if k12 == nil then return end; t[k12] = true - if k13 == nil then return end; t[k13] = true - if k14 == nil then return end; t[k14] = true - if k15 == nil then return end; t[k15] = true - if k16 == nil then return end; t[k16] = true - if k17 == nil then return end; t[k17] = true - if k18 == nil then return end; t[k18] = true - if k19 == nil then return end; t[k19] = true - if k20 == nil then return end; t[k20] = true - if overflow ~= nil then error('Overflow.') end - end - local function insert_values(t,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,overflow) - local n = getn(t) - if v1 == nil then return t end; t[n + 1] = v1 - if v2 == nil then setn(t, n + 1); return end; t[n + 2] = v2 - if v3 == nil then setn(t, n + 2); return end; t[n + 3] = v3 - if v4 == nil then setn(t, n + 3); return end; t[n + 4] = v4 - if v5 == nil then setn(t, n + 4); return end; t[n + 5] = v5 - if v6 == nil then setn(t, n + 5); return end; t[n + 6] = v6 - if v7 == nil then setn(t, n + 6); return end; t[n + 7] = v7 - if v8 == nil then setn(t, n + 7); return end; t[n + 8] = v8 - if v9 == nil then setn(t, n + 8); return end; t[n + 9] = v9 - if v10 == nil then setn(t, n + 9); return end; t[n + 10] = v10 - if v11 == nil then setn(t, n + 10); return end; t[n + 11] = v11 - if v12 == nil then setn(t, n + 11); return end; t[n + 12] = v12 - if v13 == nil then setn(t, n + 12); return end; t[n + 13] = v13 - if v14 == nil then setn(t, n + 13); return end; t[n + 14] = v14 - if v15 == nil then setn(t, n + 14); return end; t[n + 15] = v15 - if v16 == nil then setn(t, n + 15); return end; t[n + 16] = v16 - if v17 == nil then setn(t, n + 16); return end; t[n + 17] = v17 - if v18 == nil then setn(t, n + 17); return end; t[n + 18] = v18 - if v19 == nil then setn(t, n + 18); return end; t[n + 19] = v19 - if v20 == nil then setn(t, n + 19); return end; t[n + 20] = v20; setn(t, n + 20) - if overflow ~= nil then error('Overflow.') end - end - local function insert_pairs(t,k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8,k9,v9,k10,v10,overflow) - if k1 == nil then return end t[k1] = v1 - if k2 == nil then return end t[k2] = v2 - if k3 == nil then return end t[k3] = v3 - if k4 == nil then return end t[k4] = v4 - if k5 == nil then return end t[k5] = v5 - if k6 == nil then return end t[k6] = v6 - if k7 == nil then return end t[k7] = v7 - if k8 == nil then return end t[k8] = v8 - if k9 == nil then return end t[k9] = v9 - if k10 == nil then return end t[k10] = v10 - if overflow ~= nil then error('Overflow.') end - end - local function constructor_mt(insert) - return { - __call=function(self,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,overflow) - local t, n = rawget(self, 't') or table(), rawget(self, 'n') or 1 - insert(t,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,overflow) - if n > 1 then - self.t, self.n = t, n - 1 - return self - end - recycle(self) - return t - end, - __index=function(self, key) self.n = key; return self end, - } - end - local set_mt, list_mt, table_mt = constructor_mt(insert_keys), constructor_mt(insert_values), constructor_mt(insert_pairs) - function public.set.get() return setmetatable(table(), set_mt) end - function public.list.get() return setmetatable(table(), list_mt) end - function public.T.get() return setmetatable(table(), table_mt) end - end -end - 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 event_frame:RegisterEvent(event) @@ -240,7 +108,7 @@ tab_info = t do for _, info in temp-list(temp-list('search_tab', 'Search'), temp-list('post_tab', 'Post'), temp-list('auctions_tab', 'Auctions'), temp-list('bids_tab', 'Bids')) do local tab = T('name', info[2]) - local env = (function() aux(info[1]) return M end)() + local env = (function() _G.aux(info[1]) 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/aux-addon.toc b/aux-addon.toc index d4319e0..d284bb1 100644 --- a/aux-addon.toc +++ b/aux-addon.toc @@ -5,6 +5,7 @@ ## SavedVariables: aux_datasets, aux_items, aux_item_ids, aux_auctionable_items, aux_merchant_buy, aux_merchant_sell, aux_characters, aux_recent_searches, aux_favorite_searches, aux_ignore_owner, aux_price_per_unit libs\module.lua +libs\green_t.lua aux-addon.lua util\core.lua diff --git a/core/tooltip.lua b/core/tooltip.lua index f2d55a9..c5f204d 100644 --- a/core/tooltip.lua +++ b/core/tooltip.lua @@ -101,7 +101,7 @@ function extend_tooltip(tooltip, link, quantity) end if _G.aux_tooltip_daily then local market_value = history.market_value(item_key) - tooltip:AddLine('Today: '..(market_value and money.to_string2(market_value * quantity)..' ('..auction_listing.percentage_historical(round(market_value / value * 100))..')' or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) + tooltip:AddLine('Today: '..(market_value and money.to_string2(market_value * quantity)..' ('..aux.auction_listing.percentage_historical(round(market_value / value * 100))..')' or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) end end diff --git a/libs/class.lua b/libs/class.lua deleted file mode 100644 index 9cecc0e..0000000 --- a/libs/class.lua +++ /dev/null @@ -1,46 +0,0 @@ -if module then return end -local typeof, setmetatable, setfenv, unpack, next, pcall, _G = type, setmetatable, setfenv, unpack, next, pcall, getfenv(0) - -local PUBLIC, PRIVATE = 1, 2 -local INDEX, NEWINDEX, CALL = 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 -local function declaration_error() error 'Invalid declaration.' end -local function collision_error(key) error('"%s" already exists.', key) end - -local function nop() end -local function id(v) return v end -local function const(v) return function() return v end end - -local _state = {} - -local env_mt, start_declaration - -local interface_mt = {__metatable=false} -function interface_mt:__index(key) - return generic_function end - - - local state=_state[self] -if state.access[key] == PUBLIC then - local index = state[INDEX][key] - if index then return index() else return state[CALL][key] end -end - -function interface_mt:__newindex(key, value) local state=_state[self] -if state.access[key] == PUBLIC then (state[NEWINDEX][key] or state[CALL][key])(value) end -end - - -function class() - local self = {} - - return self, create_new_object -end - -local _ - -local message - -function handle() end \ No newline at end of file diff --git a/libs/green_t.lua b/libs/green_t.lua new file mode 100644 index 0000000..a244f23 --- /dev/null +++ b/libs/green_t.lua @@ -0,0 +1,175 @@ +if module 'green_t' then return end + +local next, getn, setn, setmetatable, rawget = next, getn, table.setn, setmetatable, rawget +local wipe, recycle, table +local pool, pool_size = {}, 0 +local overflow_pool = setmetatable({}, {__mode='k'}) +local tmp = {} + +CreateFrame('Frame'):SetScript('OnUpdate', function() + for t in tmp do recycle(t) end + wipe(tmp) +end) + +--do +-- local t = setmetatable({}, {__metatable=false, newindex=error}) +-- public.O.get = t +--end + +function wipe(t) -- like with a cloth or something + for k in t do t[k] = nil end + t.reset = 1 + t.reset = nil + setn(t, 0) + return setmetatable(t, nil) +end +public.wipe = wipe + +function recycle(t) + wipe(t) + if pool_size < 50 then + pool_size = pool_size + 1 + pool[pool_size] = t + else + overflow_pool[t] = true + end +end +public.recycle = recycle + +function table() + if pool_size > 0 then + pool_size = pool_size - 1 + return pool[pool_size + 1] + end + local t = next(overflow_pool) + if t then + overflow_pool[t] = nil + return t + end + return {} +end +public.t.get = table + +function public.tt.get() + local t + if pool_size > 0 then + t = pool[pool_size] + pool_size = pool_size - 1 + else + t = next(overflow_pool) + if t then + overflow_pool[t] = nil + else + t = {} + end + end + tmp[t] = true + return t +end + +do + local function modifier_mt(f) + local function apply(self, value) + recycle(self) + return f(value) + end + return {__call=apply, __sub=apply} + end + do + local mt = modifier_mt(function(t) tmp[t] = true; return t end) + public.temp + { + get = function() return setmetatable(table(), mt) end, + set = function(t) tmp[t] = true end, + } + end + do + local mt = modifier_mt(function(t) tmp[t] = nil; return t end) + public.perm + { + get = function() return setmetatable(table(), mt) end, + set = function(t) tmp[t] = nil end, + } + end +end +do + 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) + if k1 == nil then return end; t[k1] = true + if k2 == nil then return end; t[k2] = true + if k3 == nil then return end; t[k3] = true + if k4 == nil then return end; t[k4] = true + if k5 == nil then return end; t[k5] = true + if k6 == nil then return end; t[k6] = true + if k7 == nil then return end; t[k7] = true + if k8 == nil then return end; t[k8] = true + if k9 == nil then return end; t[k9] = true + if k10 == nil then return end; t[k10] = true + if k11 == nil then return end; t[k11] = true + if k12 == nil then return end; t[k12] = true + if k13 == nil then return end; t[k13] = true + if k14 == nil then return end; t[k14] = true + if k15 == nil then return end; t[k15] = true + if k16 == nil then return end; t[k16] = true + if k17 == nil then return end; t[k17] = true + if k18 == nil then return end; t[k18] = true + if k19 == nil then return end; t[k19] = true + if k20 == nil then return end; t[k20] = true + if overflow ~= nil then error('Overflow.') end + end + local function insert_values(t,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,overflow) + local n = getn(t) + if v1 == nil then return t end; t[n + 1] = v1 + if v2 == nil then setn(t, n + 1); return end; t[n + 2] = v2 + if v3 == nil then setn(t, n + 2); return end; t[n + 3] = v3 + if v4 == nil then setn(t, n + 3); return end; t[n + 4] = v4 + if v5 == nil then setn(t, n + 4); return end; t[n + 5] = v5 + if v6 == nil then setn(t, n + 5); return end; t[n + 6] = v6 + if v7 == nil then setn(t, n + 6); return end; t[n + 7] = v7 + if v8 == nil then setn(t, n + 7); return end; t[n + 8] = v8 + if v9 == nil then setn(t, n + 8); return end; t[n + 9] = v9 + if v10 == nil then setn(t, n + 9); return end; t[n + 10] = v10 + if v11 == nil then setn(t, n + 10); return end; t[n + 11] = v11 + if v12 == nil then setn(t, n + 11); return end; t[n + 12] = v12 + if v13 == nil then setn(t, n + 12); return end; t[n + 13] = v13 + if v14 == nil then setn(t, n + 13); return end; t[n + 14] = v14 + if v15 == nil then setn(t, n + 14); return end; t[n + 15] = v15 + if v16 == nil then setn(t, n + 15); return end; t[n + 16] = v16 + if v17 == nil then setn(t, n + 16); return end; t[n + 17] = v17 + if v18 == nil then setn(t, n + 17); return end; t[n + 18] = v18 + if v19 == nil then setn(t, n + 18); return end; t[n + 19] = v19 + if v20 == nil then setn(t, n + 19); return end; t[n + 20] = v20; setn(t, n + 20) + if overflow ~= nil then error('Overflow.') end + end + local function insert_pairs(t,k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8,k9,v9,k10,v10,overflow) + if k1 == nil then return end t[k1] = v1 + if k2 == nil then return end t[k2] = v2 + if k3 == nil then return end t[k3] = v3 + if k4 == nil then return end t[k4] = v4 + if k5 == nil then return end t[k5] = v5 + if k6 == nil then return end t[k6] = v6 + if k7 == nil then return end t[k7] = v7 + if k8 == nil then return end t[k8] = v8 + if k9 == nil then return end t[k9] = v9 + if k10 == nil then return end t[k10] = v10 + if overflow ~= nil then error('Overflow.') end + end + local function constructor_mt(insert) + return { + __call=function(self,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,overflow) + local t, n = rawget(self, 't') or table(), rawget(self, 'n') or 1 + insert(t,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,overflow) + if n > 1 then + self.t, self.n = t, n - 1 + return self + end + recycle(self) + return t + end, + __index=function(self, key) self.n = key; return self end, + } + end + local set_mt, list_mt, table_mt = constructor_mt(insert_keys), constructor_mt(insert_values), constructor_mt(insert_pairs) + function public.set.get() return setmetatable(table(), set_mt) end + function public.list.get() return setmetatable(table(), list_mt) end + function public.T.get() return setmetatable(table(), table_mt) end +end diff --git a/libs/module.lua b/libs/module.lua index fc1b9aa..d67d8a3 100644 --- a/libs/module.lua +++ b/libs/module.lua @@ -57,13 +57,13 @@ do end end -local environment_mt = {__metatable=false} -function environment_mt:__index(key) self=state[self] +local env_mt = {__metatable=false} +function env_mt:__index(key) self=state[self] local getter = self[INDEX][key] if getter then return getter() end return self[CALL][key] or _G[key] or self.declarator[key] end -function environment_mt:__newindex(key, value) self=state[self] +function env_mt:__newindex(key, value) self=state[self] if self.access[key] then local setter = self[NEWINDEX][key] or collision_error(key) setter(value) @@ -82,10 +82,12 @@ end function interface_mt:__newindex(key, value) self=state[self] if self.access[key] == PUBLIC then (self[NEWINDEX][key] or nop)(value) end end +function interface_mt:__eq() return true end +local INTERFACE = setmetatable({}, interface_mt) local function import(self, ...) for i = 1, arg.n do - local module = state[arg[i] or import_error()] + local module = state[arg[i] == INTERFACE and arg[i] or import_error()] for k, v in module.access do if v == PUBLIC and not self.access[k] then self.access[k], self[CALL][k], self[INDEX][k], self[NEWINDEX][k] = PRIVATE, module[CALL][k], module[INDEX][k], module[NEWINDEX][k] @@ -94,16 +96,18 @@ local function import(self, ...) end end -function module(...) - local declarator, environment, interface = setmetatable({}, declarator_mt), setmetatable({}, environment_mt), setmetatable({}, interface_mt) +function module(name) + if name and _G[name] then return true end + local declarator, env, interface = setmetatable({}, declarator_mt), setmetatable({}, env_mt), setmetatable({}, interface_mt) local self; self = { access = {_=PRIVATE, error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, public=PRIVATE, private=PRIVATE}, [CALL] = {import=function(...) import(self, unpack(arg)) end, error=error, nop=nop}, - [INDEX] = {_G=const(_G), M=const(environment), I=const(interface), public=function() return declarator.public end, private=function() return declarator.private end}, + [INDEX] = {_G=const(_G), M=const(env), I=const(interface), public=function() return declarator.public end, private=function() return declarator.private end}, [NEWINDEX] = {_=nop}, declarator = declarator, default_access = PRIVATE, } - state[declarator], state[environment], state[interface] = self, self, self - setfenv(2, environment) + state[declarator], state[env], state[interface] = self, self, self + if name then _G[name] = interface end + setfenv(2, env) end \ No newline at end of file