-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.lua
724 lines (493 loc) · 13.6 KB
/
temp.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
local config = {
coolPrintDebugClassStuff = false,
disableInnerCoolPrint = false,
disableCoolPrint = true,
useObjects = false
}
local _, rbx = pcall(function() return not not game end);
local Prototype = {};
Prototype.__index = Prototype;
local PrototypeItem = {};
PrototypeItem.__index = PrototypeItem;
local original_tostring = tostring;
function Prototype:__tostring()
return "[class " .. self.__name .. "]";
end
function PrototypeItem.new(parent, value, t)
if not t then t = {}; end;
t.__value = value;
t.__parent = parent;
if not t.__writable then t.__writable = true; end;
if not t.__configurable then t.__configurable = true; end;
if not t.__enumerable then t.__enumerable = true; end;
function t.__iter(tbl)
print('a');
return tbl.__value;
end
return setmetatable(t, PrototypeItem);
end
local p = {};
local baseprint = p.__tostring;
function PrototypeItem.__tostring(tbl)
if type(tbl.__value) == "table" and not config.disableInnerCoolPrint then
return dump(tbl.__value);
elseif type(tbl.__value) == "table" and config.disableInnerCoolPrint and baseprint then
return baseprint(tbl.__value)
else
return tostring(tbl.__value);
end
end
function PrototypeItem.__len(tbl)
local i = 0;
local success = pcall(function()
i = #tbl.__value;
end);
if not success then
for _ in ipairs(tbl.__value) do i = i + 1; end
end
return i;
end
function PrototypeItem.__iter(tbl)
return next, tbl.__value
end
function PrototypeItem.__le(tbl, b) return tbl.__value <= b; end
function PrototypeItem.__lt(tbl, b) return tbl.__value < b; end
function PrototypeItem.__ge(tbl, b) return tbl.__value >= b; end
function PrototypeItem.__gt(tbl, b) return tbl.__value > b; end
function PrototypeItem.__eq(tbl, b) return tbl.__value == b; end
function PrototypeItem.__add(tbl, b) return PrototypeItem.new(tbl.__parent, (tbl.__value + b), tbl); end
function PrototypeItem.__sub(tbl, b) return PrototypeItem.new(tbl.__parent, (tbl.__value - b), tbl); end
function PrototypeItem.__mul(tbl, b) return PrototypeItem.new(tbl.__parent, (tbl.__value * b), tbl); end
function PrototypeItem.__div(tbl, b) return PrototypeItem.new(tbl.__parent, (tbl.__value / b), tbl); end
function PrototypeItem.__mod(tbl, b) return PrototypeItem.new(tbl.__parent, (tbl.__value * b), tbl); end
function PrototypeItem.__pow(tbl, b) return PrototypeItem.new(tbl.__parent, (tbl.__value ^ b), tbl); end
function PrototypeItem.__idiv(tbl, b) return PrototypeItem.new(tbl.__parent, (tbl.__value // b), tbl); end
function PrototypeItem.__band(tbl, b) return PrototypeItem.new(tbl.__parent, (tbl.__value .. b), tbl); end
function PrototypeItem.__unm(tbl) return PrototypeItem.new(tbl.__parent, (-tbl.__value), tbl); end;
function PrototypeItem.__index(tbl, key)
print(tbl);
-- nab from the item itself
if rawget(tbl, key) then
return rawget(tbl, key);
-- nab from the item's value
elseif pcall(function() return rawget(tbl.__value, key) end) then
return rawget(tbl.__value, key);
-- weird fix for it calling __metaindex on the parent table
elseif rawget(tbl, "__value") and type(tbl.__value) == "function" then
local parent = rawget(tbl, "__parent") or tbl;
return rawget(tbl, "__value")(parent, key);
end
end
function PrototypeItem.__newindex(tbl, key, value)
-- nab from the item itself
if rawget(tbl, key) then
rawset(tbl, key, value);
-- nab from the item's value
elseif pcall(function() return rawget(tbl.__value, key) end) and tbl.__writable then
rawset(tbl.__value, key, value);
elseif pcall(function() return rawget(tbl.__value, key) end) and not tbl.__writable then
error("Class entry \"" .. key .. "\" not writeable");
end
end
function PrototypeItem.__call(tbl, ...)
print(tbl);
for k, v in pairs({...}) do
if type(v) == "table" then
print(v, v.__name, tbl.__parent.__name)
end
if type(v) == "table" and v.__name and v.__name == tbl.__parent.__name then
return rawget(tbl, "__value")(...);
end
end
return rawget(tbl, "__value")(rawget(tbl, "__parent"), ...);
end
local function shallow(t)
local nt = {};
for k, v in pairs(t) do nt[k] = v end
return nt;
end
local function rawtostring(val)
local mt = getmetatable(val)
local __tostring = mt and mt.__tostring
if __tostring then mt.__tostring = nil end
local str = original_tostring(val)
if __tostring then mt.__tostring = __tostring end
return str
end
local function pad(o, t, l)
local x = o;
x = (t):rep(l-x:len())..x;
return x;
end
local function tid(t)
return rawtostring(t):gsub("table: ", "", 1)
end
local function fid(f)
return tostring(f):gsub("function: ", "", 1);
end
local function fname(m)
for k, v in pairs(_G) do
if m == v then return k; end
end
end
local function dump(o, layer)
if not layer then layer = 1; end
local t = pad("", "\t", layer);
local lastt = pad("", "\t", layer-1);
if type(o) == 'table' and not o.__base then
o = shallow(o);
if o.__prototype then
for k, v in pairs(o.__prototype) do o[k] = v end
end
local s = '{';
local li = 0;
local i = 0;
local function dothe(k)
return (k ~= "__instances" and (not string.match(k, "__") or config.coolPrintDebugClassStuff))
end
for k, _ in pairs(o) do
if dothe(k) then
li = li + 1;
end
end
if li > 0 then
s = s.."\n";
end
for k,v in pairs(o) do
if dothe(k) then
-- if not string.match(k, "__") then
i = i + 1;
local e = "\n";
if i ~= li then
e = ","..e;
end
if type(k) ~= 'number' and string.match(k, " ") then k = '"'..k..'"' end;
if type(v) == "string" then v = '"'..v..'"' end;
if type(v) == "function" then
local mName = fname(v);
if mName then
v = mName .. "() (" .. fid(v) .. ")";
else
v = "function() (" .. fid(v) .. ")";
end
end
if v ~= o then
s = s .. t .. k ..': ' .. dump(v, layer+1) .. e;
end
-- print(i, li);
if i == li then
break;
end
end
end
if li > 0 then
return s .. lastt .. '}'
else
return s .. '}'
end
else
return tostring(o)
end
end
local function __prototostring(tbl)
local d = dump(tbl);
if tbl.__name then
return tbl.__name .. " ("..tid(tbl)..") " .. d
end
return d;
end
local function __protonewindex(tbl, key, value)
local output = nil;
if key == "constructor" then
return rawset(tbl, "constructor", nil);
end
if key == "__name" then
local oldName = rawget(tbl, "__name");
if oldName then
_G.Protos[oldName] = nil;
end
_G.Protos[value] = tbl;
if config.useObjects then
return rawset(tbl, "__name", PrototypeItem.new(tbl, value));
else
return rawset(tbl, "__name", value);
end
end
if type(value) == "function" and not string.match(key, "__") then
if config.useObjects then
tbl.__prototype[key] = PrototypeItem.new(tbl, function(...)
return value(...);
end)
else
tbl.__prototype[key] = function(...)
return value(...);
end
end
elseif not string.match(key, "__") then
local item;
if config.useObjects then
item = PrototypeItem.new(tbl, value);
else
item = value;
end
return rawset(tbl, key, item);
else
return rawset(tbl, key, value);
end
end
local function __protoindex(tbl, key)
local protoGot = rawget(tbl.__prototype, key);
if protoGot and not rawget(tbl, key) then
if type(protoGot) == "function" then
return function(...)
return protoGot(tbl, ...);
end
else
return protoGot;
end
else
return rawget(tbl, key);
end
end
local function __metanewindex(tbl, key, value)
if key == "constructor" then
rawset(tbl, "constructor", nil);
end
if type(value) == "function" and not string.match(key, "__") then
if config.useObjects then
tbl[key] = PrototypeItem.new(tbl, function(...)
return value(...);
end)
else
tbl[key] = function(...)
return value(...);
end
end
elseif not string.match(key, "__") then
local item;
if config.useObjects then
item = PrototypeItem.new(tbl, value);
else
item = value;
end
return rawset(tbl, key, item);
else
return rawset(tbl, key, value);
end
end
local function __metaindex(tbl, key)
local protoGot = rawget(tbl.__prototype, key);
local normalGot = rawget(tbl, key);
local self = rawget(tbl, "__parent") or tbl;
if protoGot and not rawget(tbl, key) then
if type(protoGot) == "function" then
return function(...)
return protoGot(self, ...);
end
else
return protoGot
end
else
if type(normalGot) == "function" then
return function(...)
return normalGot(self, ...);
end
else
return normalGot;
end
end
end
setmetatable(_G, {
__newindex = function(t, k, v)
--[[if rawget(t.Protos, k) then
rawset(t.Protos, k, v);
else]]
rawset(t, k, v);
-- end
end,
__index = function(t, k)
if rawget(t.Protos, k) then
return rawget(t.Protos, k);
else
return rawget(t, k)
end
end,
});
_G.Protos = {};
function Prototype.new(name) return function(p)
if not p then p = {} end;
Prototype.__index = Prototype;
local proto = {};
for k, v in pairs(p) do
proto[k] = config.useObjects and PrototypeItem.new(nil, v) or v;
p[k] = nil;
end
p.__index = __protoindex;
p.__newindex = __protonewindex;
if not rbx or not config.disableCoolPrint then
p.__tostring = dump;
end
local self = setmetatable(p, Prototype);
self.__name = name;
self.__instances = {};
self.__prototype = proto;
if config.useObjects then
for k, v in pairs(proto) do
if type(v) == "table" then
rawset(v, "__parent", self);
end
end
end
function self.__prototype.__isa(self, n)
if type(n) == "table" then
return n.__name == self.__name;
else
return n == self.__name;
end
end
Prototype.__index = __protoindex;
Prototype.__newindex = __protonewindex;
self.__index = __protoindex;
self.__newindex = __protonewindex;
--[[if not rbx then
self.__tostring = __prototostring(self);
end]]
if not rawget(self, "constructor") and not rawget(self.__prototype, "constructor") then
local item
if config.useObjects then
item = PrototypeItem.new(self, function() end);
else
item = (function() end);
end
self.__prototype.constructor = item;
elseif rawget(self, "constructor") or rawget(self.__prototype, "constructor") then
local constructor;
if rawget(self, "constructor") then
constructor = rawget(self, "constructor");
else
constructor = rawget(self.__prototype, "constructor");
end
rawset(self, "constructor", nil);
rawset(self.__prototype, "constructor", nil);
if config.useObjects then
self.__prototype.constructor = PrototypeItem.new(self, function(...)
return constructor(...);
end)
else
self.__prototype.constructor = function(...)
return constructor(...);
end
end
end
_G.Protos[self.__name] = self;
return self;
end end
function new(name) return (function(...)
local c = _G.Protos[name];
-- if it can't find it in _G.Protos
if not c then
error("Class \""..name.."\" not found in globals while trying to create a new instance.");
end
local exter = {
__name = c.__name;
__prototype = c.__prototype;
__class = c
};
local ext = setmetatable(exter, c);
function ext:__index(...)
return __metaindex(self, ...);
end
function ext:__newindex(...)
return __metanewindex(self, ...);
end
if not rbx or not config.disableCoolPrint then
function ext:__tostring()
return __prototostring(self);
end
end
local self = setmetatable(exter, ext);
-- self.__under = ext;
if c.__instances then
-- table.insert(c.__instances, self);
end
if config.useObjects then
for k, v in pairs(self.__prototype) do
if type(v) == "table" then
rawset(v, "__parent", self);
end
end
end
local ret = self.__prototype.constructor(self, ...);
if not ret then
return self;
else
return ret;
end
end) end
function class(n)
return Prototype.new(n);
end
function getclass(n)
return _G.Protos[n];
end
function isa(t, n)
return t.__isa(n);
end
function extend(nfrom) return (function(nto) return (function(p)
local base = _G.Protos[nfrom];
local proto = shallow(base.__prototype);
for k, v in pairs(p) do
proto[k] = v;
end
local self = Prototype.new(nto)(proto);
function self.__prototype.__super(...)
local constructor = rawget(base, "constructor") and rawget(base, "constructor") or rawget(base.__prototype, "constructor");
return constructor(...);
end
for k, v in pairs(self.__prototype) do
if type(v) == "table" and rawget(v, "__parent") then
rawset(v, "__parent", self);
end
if type(v) == "function" then
--[[local f = v(self);
print(f)]]
--[[v = function(...)
return f(...);
end
self.__prototype[k] = v;]]
end
end
self.__extendee = base;
return self;
end) end) end
-- function new(name) return __new end
local stuff = { class = class, new = new, extend = extend, getclass = getclass, isa = isa };
class "BaseEnemy" {
constructor = function(self, health)
self.Health = health;
end,
attack = function(self, damage)
print("attacking or something for " ..damage .. " damage");
end,
Stamina = 100,
}
extend "BaseEnemy" "Bob" {
constructor = function(self, health)
self.__super(health);
self.Something = "idk what to put here";
end,
dosomething = function(self)
print('idk what to put here either');
end
};
local bob = new "Bob"(100);
print(bob);
bob.attack(50);
bob.dosomething();
return (function(...)
local args = {...};
for i, v in ipairs(args) do
args[i] = stuff[v];
end
return table.unpack(args);
end)