diff --git a/game/engines/default/engine/class.lua b/game/engines/default/engine/class.lua index 48fe9c2..5631043 100644 --- a/game/engines/default/engine/class.lua +++ b/game/engines/default/engine/class.lua @@ -41,20 +41,42 @@ function make(c) return c end -function inherit(base, ...) - local ifs = {...} +function inherit(...) + local bases = {...} return function(c) - if #ifs == 0 then - setmetatable(c, {__index=base}) + c._BASES = bases + if #bases == 1 then + setmetatable(c, {__index=bases[1]}) else - for i, _if in ipairs(ifs) do - for k, e in pairs(_if) do - if k ~= "init" and k ~= "_NAME" and k ~= "_M" and k ~= "_PACKAGE" and k ~= "new" then - c[k] = e --- print(("caching interface value %s (%s) from %s to %s"):format(k, tostring(e), _if._NAME, c._NAME)) + local skip_key = {init=true, _NAME=true, _M=true, _PACKAGE=true, new=true, _BASES=true, castAs=true} + local completed_bases = {} + local cache_inheritance + cache_inheritance = function(c, base) + -- Only cache a base class once + if not completed_bases[base] then + -- Cache all the immediate variables + for k, e in pairs(base) do + if not skip_key[k] and (c[k] == nil) and (base[k] ~= nil) then + print(("caching interface value %s (%s) from %s to %s"):format(k, tostring(e), base._NAME, c._NAME)) + c[k] = base[k] + end + end + completed_bases[base] = true + -- Add all of this base class' base classes to the queue + if base._BASES and type(base._BASES) == "table" then + for i, base in ipairs(base._BASES) do + print(("adding %s to cache queue"):format(base._NAME)) + bases[#bases+1] = base + end end end end + local i = 1 + while i <= #bases do + print(i) + cache_inheritance(c, bases[i]) + i = i + 1 + end setmetatable(c, {__index=base}) end c.new = function(...)