-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclasslib.lua
executable file
·416 lines (363 loc) · 10.8 KB
/
classlib.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
-- Developer: sbx320
-- License: MIT
-- Github Repos: https://github.com/sbx320/lua_utils
--// classlib
--|| A library providing several tools to enhance OOP with MTA and Lua
--\\
SERVER = triggerServerEvent == nil
CLIENT = not SERVER
DEBUG = DEBUG or false
function enew(element, class, ...)
-- DEBUG: Validate that we are not instantiating a class with pure virtual methods
if DEBUG then
for k, v in pairs(class) do
assert(v ~= pure_virtual, "Attempted to instanciate a class with an unimplemented pure virtual method ("..tostring(k)..")")
end
end
local instance = setmetatable( { element = element },
{
__index = class;
__class = class;
__newindex = class.__newindex;
__call = class.__call;
__len = class.__len;
__unm = class.__unm;
__add = class.__add;
__sub = class.__sub;
__mul = class.__mul;
__div = class.__div;
__pow = class.__pow;
__concat = class.__concat;
})
oop.elementInfo[element] = instance
local callDerivedConstructor;
callDerivedConstructor = function(parentClasses, instance, ...)
for k, v in pairs(parentClasses) do
if rawget(v, "virtual_constructor") then
rawget(v, "virtual_constructor")(instance, ...)
end
local s = superMultiple(v)
callDerivedConstructor(s, instance, ...)
end
end
callDerivedConstructor(superMultiple(class), element, ...)
-- Call constructor
if rawget(class, "constructor") then
rawget(class, "constructor")(element, ...)
end
element.constructor = false
-- Add the destruction handler
if isElement(element) then
addEventHandler(
triggerClientEvent ~= nil and
"onElementDestroy" or
"onClientElementDestroy", element, __removeElementIndex, false, "low-999999")
end
return element
end
function new(class, ...)
assert(type(class) == "table", "first argument provided to new is not a table")
-- DEBUG: Validate that we are not instantiating a class with pure virtual methods
if DEBUG then
for k, v in pairs(class) do
assert(v ~= pure_virtual, "Attempted to instanciate a class with an unimplemented pure virtual method ("..tostring(k)..")")
end
end
local instance = setmetatable( { },
{
__index = class;
__class = class;
__newindex = class.__newindex;
__call = class.__call;
__len = class.__len;
__unm = class.__unm;
__add = class.__add;
__sub = class.__sub;
__mul = class.__mul;
__div = class.__div;
__pow = class.__pow;
__concat = class.__concat;
})
-- Call derived constructors
local callDerivedConstructor;
callDerivedConstructor = function(self, instance, ...)
for k, v in pairs(self) do
if rawget(v, "virtual_constructor") then
rawget(v, "virtual_constructor")(instance, ...)
end
local s = superMultiple(v)
callDerivedConstructor(s, instance, ...)
end
end
callDerivedConstructor(superMultiple(class), instance, ...)
-- Call constructor
if rawget(class, "constructor") then
rawget(class, "constructor")(instance, ...)
end
instance.constructor = false
return instance
end
function delete(self, ...)
if self.destructor then --if rawget(self, "destructor") then
self:destructor(...)
end
-- Prevent the destructor to be called twice
self.destructor = false
local callDerivedDestructor;
callDerivedDestructor = function(parentClasses, instance, ...)
for k, v in pairs(parentClasses) do
if rawget(v, "virtual_destructor") then
rawget(v, "virtual_destructor")(instance, ...)
end
local s = superMultiple(v)
callDerivedDestructor(s, instance, ...)
end
end
callDerivedDestructor(superMultiple(self), self, ...)
end
function superMultiple(self)
if isElement(self) then
assert(oop.elementInfo[self], "Cannot get the superclass of this element") -- at least: not yet
self = oop.elementInfo[self]
end
local metatable = getmetatable(self)
if not metatable then
return {}
end
if metatable.__class then -- we're dealing with a class object
return superMultiple(metatable.__class)
end
if metatable.__super then -- we're dealing with a class
return metatable.__super or {}
end
end
function super(self)
return superMultiple(self)[1]
end
function classof(self)
if isElement(self) then
assert(oop.elementInfo[self], "Cannot get the class of this element") -- at least: not yet
self = oop.elementInfo[self]
end
local metatable = getmetatable(self)
if metatable then
return metatable.__class
end
return {}
end
function inherit(from, what)
assert(from, "Attempt to inherit a nil table value")
if not what then
local classt = setmetatable({}, { __index = _inheritIndex, __super = { from } })
if from.onInherit then
from.onInherit(classt)
end
return classt
end
local metatable = getmetatable(what) or {}
local oldsuper = metatable and metatable.__super or {}
table.insert(oldsuper, 1, from)
metatable.__super = oldsuper
metatable.__index = _inheritIndex
-- Inherit __call
for k, v in ipairs(metatable.__super) do
if v.__call then
metatable.__call = v.__call
break
end
end
return setmetatable(what, metatable)
end
function _inheritIndex(self, key)
for k, v in pairs(superMultiple(self)) do
if v[key] then return v[key] end
end
return nil
end
---// __removeElementIndex()
---|| @desc: This function calls delete on the hidden source parameter to invoke the destructor
---|| !!! Avoid calling this function manually unless you know what you're doing! !!!
---\\
function __removeElementIndex()
delete(source)
end
function instanceof(self, class, direct)
if direct then
return classof(self) == class
end
for k, v in pairs(superMultiple(self)) do
if v == class then return true end
end
local check = false
-- Check if any of 'self's base classes is inheriting from 'class'
for k, v in pairs(superMultiple(self)) do
check = instanceof(v, class, false)
if check then
break
end
end
return check
end
function pure_virtual()
outputDebug(debug.traceback())
error("Function implementation missing")
end
function bind(func, ...)
if not func then
if DEBUG then
outputConsole(debug.traceback())
outputServerLog(debug.traceback())
end
error("Bad function pointer @ bind. See console for more details")
end
local boundParams = {...}
return
function(...)
local params = {}
local boundParamSize = select("#", unpack(boundParams))
for i = 1, boundParamSize do
params[i] = boundParams[i]
end
local funcParams = {...}
for i = 1, select("#", ...) do
params[boundParamSize + i] = funcParams[i]
end
return func(unpack(params))
end
end
function load(class, ...)
assert(type(class) == "table", "first argument provided to load is not a table")
local instance = setmetatable( { },
{
__index = class;
__class = class;
__newindex = class.__newindex;
__call = class.__call;
})
-- Call load
if rawget(class, "load") then
rawget(class, "load")(instance, ...)
end
instance.load = false
return instance
end
-- Magic to allow MTA elements to be used as data storage
-- e.g. localPlayer.foo = 12
oop = {}
oop.elementInfo = setmetatable({}, { __mode = "k" })
oop.elementClasses = {}
oop.prepareClass = function(name)
local mt = debug.getregistry().mt[name]
if not mt then
outputDebugString("No such class mt "..tostring(name))
return
end
-- Store MTA's metafunctions
local __mtaindex = mt.__index
local __mtanewindex = mt.__newindex
local __set= mt.__set
mt.__index = function(self, key)
if not oop.handled then
if not oop.elementInfo[self] and isElement(self) then
enew(self, oop.elementClasses[getElementType(self)] or {})
end
if oop.elementInfo[self] and oop.elementInfo[self][key] ~= nil then
oop.handled = false
return oop.elementInfo[self][key]
end
oop.handled = true
end
local value = __mtaindex(self, key)
oop.handled = false
return value
end
mt.__newindex = function(self, key, value)
if __set[key] ~= nil then
__mtanewindex(self, key, value)
return
end
if not oop.elementInfo[self] and isElement(self) then
enew(self, oop.elementClasses[getElementType(self)] or {})
end
oop.elementInfo[self][key] = value
end
end
function registerElementClass(name, class)
assert(type(name) == "string", "Bad argument #1 for registerElementClass")
assert(type(class) == "table", "Bad argument #2 for registerElementClass")
oop.elementClasses[name] = class
end
oop.initClasses = function()
-- this has to match
-- (Server) MTA10_Server\mods\deathmatch\logic\lua\CLuaMain.cpp
-- (Client) MTA10\mods\shared_logic\lua\CLuaMain.cpp
if SERVER then
oop.prepareClass("ACL")
oop.prepareClass("ACLGroup")
oop.prepareClass("Account")
oop.prepareClass("Ban")
oop.prepareClass("Connection")
oop.prepareClass("QueryHandle")
oop.prepareClass("TextDisplay")
oop.prepareClass("TextItem")
elseif CLIENT then
oop.prepareClass("Browser")
oop.prepareClass("Camera")
oop.prepareClass("Light")
oop.prepareClass("Projectile")
oop.prepareClass("SearchLight")
oop.prepareClass("Sound")
oop.prepareClass("Sound3D")
oop.prepareClass("Weapon")
oop.prepareClass("Effect")
oop.prepareClass("GuiElement")
oop.prepareClass("GuiWindow")
oop.prepareClass("GuiButton")
oop.prepareClass("GuiEdit")
oop.prepareClass("GuiLabel")
oop.prepareClass("GuiMemo")
oop.prepareClass("GuiStaticImage")
oop.prepareClass("GuiComboBox")
oop.prepareClass("GuiCheckBox")
oop.prepareClass("GuiRadioButton")
oop.prepareClass("GuiScrollPane")
oop.prepareClass("GuiScrollBar")
oop.prepareClass("GuiProgressBar")
oop.prepareClass("GuiGridList")
oop.prepareClass("GuiTabPanel")
oop.prepareClass("GuiTab")
oop.prepareClass("GuiFont")
oop.prepareClass("GuiBrowser")
oop.prepareClass("EngineCOL")
oop.prepareClass("EngineTXD")
oop.prepareClass("EngineDFF")
oop.prepareClass("DxMaterial")
oop.prepareClass("DxTexture")
oop.prepareClass("DxFont")
oop.prepareClass("DxShader")
oop.prepareClass("DxScreenSource")
oop.prepareClass("DxRenderTarget")
oop.prepareClass("Weapon")
end
oop.prepareClass("Object")
oop.prepareClass("Ped")
oop.prepareClass("Pickup")
oop.prepareClass("Player")
oop.prepareClass("RadarArea")
--oop.prepareClass("Vector2")
--oop.prepareClass("Vector3")
--oop.prepareClass("Vector4")
--oop.prepareClass("Matrix")
oop.prepareClass("Element")
oop.prepareClass("Blip")
oop.prepareClass("ColShape")
oop.prepareClass("File")
oop.prepareClass("Marker")
oop.prepareClass("Vehicle")
oop.prepareClass("Water")
oop.prepareClass("XML")
oop.prepareClass("Timer")
oop.prepareClass("Team")
oop.prepareClass("Resource")
end
oop.initClasses()