fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
27686d7
settings-overhaul
1local class =require ( "class" ) 2 3local OBJECT ={} 4 5function OBJECT : __call ( ... ) 6if not self [ self . __constructor ] then 7return error ( string . format ( "can not create object for class '%s' (no constructor)" , self )) 8end 9self [ self . __constructor ]( self , ... ) 10return self 11end 12 13function OBJECT : __index ( key ) 14-- Check for any specific, class-based, methods like "super" 15local meta_index =rawget ( getmetatable ( self ), key ) -- OBJECT 16if meta_index ~= nil then 17-- Only return if not nil 18return meta_index 19end 20 21-- Try the base class next 22local base =rawget ( self , "__baseclass" ) 23if base then 24-- Only do the lookup once to prevent infinite __index calls 25local value =base [ key ] 26if value ~= nil then 27-- Only return if not nil 28return value 29end 30end 31 32-- Welp, nothing to find 33return nil 34end 35 36function OBJECT : super ( method , ... ) 37local base =self 38 39-- Attempt to determine the base via the current scope of the call 40for i =1 , self . __superscope do 41base =rawget ( base , "__baseclass" ) 42end 43 44-- Error if the scope of the call has no baseclass (AKA the main class) 45if not base then return error ( "attempted to call method 'super' in class with no baseclass" ) end 46 47local call =method or base . __constructor 48 49if base [ call ] == nil then 50return error ( string . format ( "attempted to call method '%s' (a nil value)" , call )) 51end 52 53-- +1 to the scope 54self . __superscope =self . __superscope + 1 55 56-- Call the method 57-- This can trigger another super call, so thats why we need __superscope 58local ret =base [ call ]( self , ... ) 59 60-- -1 from the scope 61self . __superscope =self . __superscope - 1 62 63-- return super return 64return ret 65end 66 67function OBJECT : getBaseClass () 68return self . __baseclass 69end 70 71function OBJECT : getClass () 72local super =getmetatable ( self ) 73return ( super ~= OBJECT and super or nil ) 74end 75 76function OBJECT : __tostring () 77return string . format ( "%s::%s" , self . __baseclass or "" , self . __classname ) 78end 79 80local object ={} 81 82do 83local function copy ( object ) 84local lookup_table ={} 85local function _copy ( object ) 86if type ( object ) ~= "table" then 87return object 88elseif lookup_table [ object ] then 89return lookup_table [ object ] 90end 91local new_table ={} 92lookup_table [ object ] =new_table 93for index , value in pairs ( object ) do 94new_table [ _copy ( index )] =_copy ( value ) 95end 96return setmetatable ( new_table , getmetatable ( object )) 97end 98return _copy ( object ) 99end 100 101local function createObject ( name ) 102local main =class . getStruct ( name ) 103local base =class . getBaseClassname ( name ) 104local obj =setmetatable ( copy ( main ), OBJECT ) 105if base then 106obj . __baseclass =createObject ( base ) 107end 108return obj 109end 110 111function object . new ( name , ... ) 112if not class . getStruct ( name ) then 113return error (( "failed to create object for unknown class '%s'" ): format ( name )) 114end 115 116-- Create a copy of the struct, with metatable and all 117local obj =createObject ( name ) 118 119-- Set values that are unique to this instance 120obj . __superscope =1 121 122-- Return our new object and call constructor 123return obj ( ... ) 124end 125end 126 127return object