fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

blackops7799I lost track of what I'm even doing lola7d6bcc

GUI
3.3 KiB131 linesraw
1local class = {
2	structs = {},
3	inherits = {},
4}
5
6local constructor_method = "Initialize"
7
8local OBJECT = {}
9
10function OBJECT:__index(key)
11	-- Check for any specific, class-based, methods like "super"
12	local meta_index = rawget(getmetatable(self), key) -- OBJECT
13	if meta_index then
14		-- Only return if not nil
15		return meta_index
16	end
17
18	local accessors = rawget(self, "__accessors")
19	if accessors then
20		-- Only do the lookup once to prevent multiple __index calls
21		local value = accessors[key]
22		if value then
23			-- Only return if not nil
24			return value
25		end
26	end
27
28	-- Try the base class next
29	local base = rawget(self, "__baseclass")
30	if base then
31		-- Only do the lookup once to prevent multiple __index calls
32		local value = base[key]
33		if value then
34			-- Only return if not nil
35			return value
36		end
37	end
38
39	-- Welp, nothing to find
40	return nil
41end
42
43function OBJECT:super(method, ...)
44	local base = self
45
46	-- Attempt to determine the base via the current scope of the call
47	for i=1, self.__superscope do
48		base = rawget(base, "__baseclass")
49	end
50
51	-- Error if the scope of the call has no baseclass (AKA the main class)
52	if not base then return error("attempted to call method 'super' in class with no baseclass") end
53
54	local call = method or constructor_method
55
56	if base[call] == nil then
57		return error(string.format("attempted to call method '%s' (a nil value)", call))
58	end
59
60	-- +1 to the scope
61	self.__superscope = self.__superscope + 1
62
63	-- Call the method
64	-- This can trigger another super call, so thats why we need __superscope
65	base[call](self, ...)
66
67	-- -1 from the scope
68	self.__superscope = self.__superscope - 1
69end
70
71function OBJECT:MakeAccessor(name, internal, default)
72	self.__accessors[internal] = default
73	if type(default) == "boolean" then
74		self["Is" .. name] = function(this) return this.__accessors[internal] end
75	end
76	self["Get" .. name] = function(this) return this.__accessors[internal] end
77	self["Set" .. name] = function(this, value) this.__accessors[internal] = value end
78end
79
80function OBJECT:getBaseClass()
81	return self.__baseclass
82end
83
84function OBJECT:__tostring()
85	local base = self.__baseclass
86	return string.format("%s[%s]", base and base.__classname or "Object", self.__classname)
87end
88
89function class.new(name, ...)
90	if class.structs[name] then
91		local struct = class.structs[name]
92
93		-- Create a copy of the struct, with metatable and all
94		local obj = table.copy(struct)
95		-- Set values that are unique to this instance
96		obj.__superscope = 1
97		obj.__accessors = {}
98
99		-- Try to call the initializer
100		if obj[constructor_method] then
101			obj[constructor_method](obj, ...)
102		end
103		return obj
104	end
105
106	return error(("failed to create unknown class '%s'"):format(name))
107end
108
109function class.register(classname, basename)
110	return function(struct)
111		struct.__classname = classname
112		class.inherits[classname] = basename
113		class.structs[classname] = struct
114		return struct
115	end
116end
117
118function class.init()
119	-- We need to do this after registering our classes rather than during,
120	-- since we don't know the order in which classes will be registered.
121	-- We wan't to be sure that every classes baseclass is ready.
122
123	for classname, struct in pairs(class.structs) do
124		-- Set the baseclass
125		struct.__baseclass = class.structs[class.inherits[classname]]
126		-- Register it as a valid class
127		setmetatable(struct, OBJECT)
128	end
129end
130
131return class