-
Notifications
You must be signed in to change notification settings - Fork 1
/
oo_native.cpp
1140 lines (946 loc) · 28.4 KB
/
oo_native.cpp
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "oo_natives.h"
#include "oo_defs.h"
#include "oo_forward.h"
#include "oo_call.h"
#include "oo_manager.h"
#include <string>
extern int g_ObjectCreated;
extern int g_ObjectDeleted;
namespace oo {
namespace native
{
cell AMX_NATIVE_CALL native_class(AMX *amx, cell *params)
{
const char *_class = MF_GetAmxString(amx, params[1], 0, nullptr);
if (!Manager::Instance()->ToClass(_class).expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s: Duplicate class", _class);
return 0;
}
auto num_params = params[0] / sizeof(cell) - 1;
if (num_params == 0)
{
auto cls = Manager::Instance()->NewClass(_class);
cls.lock()->InitMRO();
return 1;
}
std::vector<std::weak_ptr<Class>> supers;
supers.reserve(num_params);
for (unsigned long i = 1; i <= num_params; i++)
{
const char *_base = MF_GetAmxString(amx, params[i+1], 1, nullptr);
auto super = Manager::Instance()->ToClass(_base);
if (super.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s: Base class (%s) not found", _class, _base);
return 0;
}
supers.emplace_back(super);
}
auto cls = Manager::Instance()->NewClass(_class, std::move(supers));
cls.lock()->InitMRO();
return 1;
}
cell AMX_NATIVE_CALL native_var(AMX *amx, cell *params)
{
const char *_class = MF_GetAmxString(amx, params[1], 0, nullptr);
const char *_name = MF_GetAmxString(amx, params[2], 1, nullptr);
int size = params[3];
auto cls = Manager::Instance()->ToClass(_class);
if (cls.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s@%s(...): Class not found", _class, _name);
return 0;
}
cls.lock()->AddVariable(_name, size);
return 1;
}
cell AMX_NATIVE_CALL native_ctor(AMX *amx, cell *params)
{
std::string _class = MF_GetAmxString(amx, params[1], 0, nullptr);
std::string _name = MF_GetAmxString(amx, params[2], 1, nullptr);
auto cls = Manager::Instance()->ToClass(_class);
if (cls.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s@%s(...): Class not found", _class.c_str(), _name.c_str());
return 0;
}
std::string public_name(_class + "@" + _name);
int num_args = params[0] / sizeof(cell) - 2;
Constructor ctor;
{
if (num_args > 0)
ctor.args.reserve(num_args);
for (auto i = 1; i <= num_args; i++)
{
int size = *MF_GetAmxAddr(amx, params[i + 2]);
ctor.args.emplace_back(size);
}
ctor.forward = Forward::Create(amx, public_name.c_str(), &ctor.args);
if (ctor.forward == NO_FORWARD)
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s(...): Public not found", public_name.c_str());
return 0;
}
}
cls.lock()->AddConstructor(std::move(ctor), _name);
return 1;
}
cell AMX_NATIVE_CALL native_dtor(AMX *amx, cell *params)
{
std::string _class = MF_GetAmxString(amx, params[1], 0, nullptr);
std::string _name = MF_GetAmxString(amx, params[2], 1, nullptr);
auto cls = Manager::Instance()->ToClass(_class);
if (cls.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s@%s(...): Class not found", _class.c_str(), _name.c_str());
return 0;
}
std::string public_name(_class + "@" + _name);
Destructor dtor;
{
dtor.forward = Forward::Create(amx, public_name.c_str(), nullptr);
if (dtor.forward == NO_FORWARD)
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s(...): Public not found", public_name.c_str());
return 0;
}
}
cls.lock()->SetDestructor(std::move(dtor));
return 1;
}
cell AMX_NATIVE_CALL native_mthd(AMX *amx, cell *params)
{
std::string _class = MF_GetAmxString(amx, params[1], 0, nullptr);
std::string _name = MF_GetAmxString(amx, params[2], 1, nullptr);
auto cls = Manager::Instance()->ToClass(_class);
if (cls.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s@%s(...): Class not found", _class.c_str(), _name.c_str());
return 0;
}
std::string public_name(_class + "@" + _name);
Method mthd;
{
auto num_args = params[0] / sizeof(cell) - 2;
if (num_args > 0)
mthd.args.reserve(num_args);
for (auto i = 1u; i <= num_args; i++)
{
int size = (*MF_GetAmxAddr(amx, params[i + 2]));
mthd.args.emplace_back(size);
}
mthd.forward = Forward::Create(amx, public_name.c_str(), &mthd.args);
mthd.is_static = false;
if (mthd.forward == NO_FORWARD)
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s(...): Public not found", public_name.c_str());
return 0;
}
}
cls.lock()->AddMethod(_name, std::move(mthd));
return 1;
}
cell AMX_NATIVE_CALL native_smthd(AMX *amx, cell *params)
{
std::string _class = MF_GetAmxString(amx, params[1], 0, nullptr);
std::string _name = MF_GetAmxString(amx, params[2], 1, nullptr);
auto cls = Manager::Instance()->ToClass(_class);
if (cls.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s@%s(...): Class not found", _class.c_str(), _name.c_str());
return 0;
}
std::string public_name(_class + "@" + _name);
Method mthd;
{
auto num_args = params[0] / sizeof(cell) - 2;
if (num_args > 0)
mthd.args.reserve(num_args);
for (auto i = 1u; i <= num_args; i++)
{
int size = (*MF_GetAmxAddr(amx, params[i + 2]));
mthd.args.emplace_back(size);
}
mthd.forward = Forward::Create(amx, public_name.c_str(), &mthd.args);
mthd.is_static = true;
if (mthd.forward == NO_FORWARD)
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s(...): Public not found", public_name.c_str());
return 0;
}
}
cls.lock()->AddMethod(_name, std::move(mthd));
return 1;
}
cell AMX_NATIVE_CALL native_this(AMX *amx, cell *params)
{
return CallStack::Instance()->Get()->_this;
}
cell AMX_NATIVE_CALL native_get(AMX *amx, cell *params)
{
ObjectHash hash = params[1];
const char *_name = MF_GetAmxString(amx, params[2], 0, nullptr);
auto obj = Manager::Instance()->ToObject(hash);
if (obj.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: Object (%d) not found", _name, hash);
return 0;
}
auto var = Manager::Instance()->FindVariable(obj, _name);
if (var == nullptr)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: Not found", _name);
return 0;
}
auto num_args = params[0] / sizeof(cell) - 2;
auto var_size = static_cast<int>(var->size());
if (num_args == 0)
{
if (var_size == 1)
{
return (*var)[0];
}
else
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: Is not a cell (size: %d), please copy by value instead", _name, var_size);
return 0;
}
}
else
{
if (var_size == 1 && num_args == 3)
{
MF_CopyAmxMemory(MF_GetAmxAddr(amx, params[3]), var->data(), 1);
return (*var)[0];
}
else
{
if (num_args < 5)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: Required at least 5 args to get array values (now: %d)", _name, num_args);
return 0;
}
int from_begin = *MF_GetAmxAddr(amx, params[3]);
if (from_begin < 0)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: The SRC begin (%d) must be >= 0", _name, from_begin);
return 0;
}
int from_end = *MF_GetAmxAddr(amx, params[4]);
if (from_begin > from_end)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: The SRC begin (%d) is bigger than the end (%d)", _name, from_begin, from_end);
return 0;
}
if (from_end > var_size)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: The SRC end (%d) is bigger than actual variable size (%d)", _name, from_end, var_size);
return 0;
}
int to_begin = *MF_GetAmxAddr(amx, params[6]);
if (to_begin < 0)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: The DEST begin (%d) must be >= 0", _name, to_begin);
return 0;
}
int to_end = *MF_GetAmxAddr(amx, params[7]);
if (to_begin > to_end)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: The DEST begin (%d) is bigger than the end(%d)", _name, to_begin, to_end);
return 0;
}
int from_diff = (from_end == 0) ? var_size : from_end - from_begin;
int to_diff = (to_end == 0) ? var_size : to_end - to_begin;
if (from_diff != to_diff)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: The SRC size (%d) differs from the DEST size (%d)", _name, from_diff, to_diff);
return 0;
}
MF_CopyAmxMemory(MF_GetAmxAddr(amx, params[5]) + to_begin, var->data() + from_begin, to_diff);
return (*var)[0];
}
}
return 0;
}
cell AMX_NATIVE_CALL native_get_str(AMX *amx, cell *params)
{
ObjectHash hash = params[1];
const char *_name = MF_GetAmxString(amx, params[2], 0, nullptr);
auto obj = Manager::Instance()->ToObject(hash);
if (obj.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: Object (%d) not found", _name, hash);
return 0;
}
auto var = Manager::Instance()->FindVariable(obj, _name);
if (var == nullptr)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: Not found", _name);
return 0;
}
auto num_args = params[0] / sizeof(cell) - 2;
auto var_size = static_cast<int>(var->size());
cell *arr = MF_GetAmxAddr(amx, params[3]);
int begin = std::max(static_cast<int>(params[5]), 0);
int slen = MF_GetAmxStringLen(var->data());
int len = (params[4] > 0) ? std::min(slen, static_cast<int>(params[4])) : slen;
len = std::min(len, var_size - begin);
MF_CopyAmxMemory(arr, var->data() + begin, len+1);
return len;
}
cell AMX_NATIVE_CALL native_set(AMX *amx, cell *params)
{
ObjectHash hash = params[1];
const char* _name = MF_GetAmxString(amx, params[2], 0, nullptr);
auto obj = Manager::Instance()->ToObject(hash);
if (obj.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Setting Var %s: Object (%d) not found", _name, hash);
return 0;
}
auto var = Manager::Instance()->FindVariable(obj, _name);
if (var == nullptr)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Setting Var %s: Not found", _name);
return 0;
}
auto num_args = params[0] / sizeof(cell) - 2;
auto var_size = static_cast<int>(var->size());
if (var_size == 1 && num_args == 1)
{
(*var)[0] = *MF_GetAmxAddr(amx, params[3]);
return 1;
}
else
{
if (num_args < 5)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Setting Var %s: Required at least 5 args to set array values (now: %d)", _name, num_args);
return 0;
}
int to_begin = *MF_GetAmxAddr(amx, params[3]);
if (to_begin < 0)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Setting Var %s: The DEST begin (%d) must be >= 0", _name, to_begin);
return 0;
}
int to_end = *MF_GetAmxAddr(amx, params[4]);
if (to_begin > to_end)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Setting Var %s: The DEST begin (%d) is bigger than the end (%d)", _name, to_begin, to_end);
return 0;
}
if (to_end > var_size)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Setting Var %s: The DEST end (%d) is bigger than actual variable size (%d)", _name, to_end, var_size);
return 0;
}
int from_begin = *MF_GetAmxAddr(amx, params[6]);
if (from_begin < 0)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Setting Var %s: The SRC begin (%d) must be >= 0", _name, from_begin);
return 0;
}
int from_end = *MF_GetAmxAddr(amx, params[7]);
if (from_begin > from_end)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Setting Var %s: The SRC begin (%d) is bigger than the end(%d)", _name, from_begin, from_begin);
return 0;
}
int from_diff = (from_end == 0) ? var_size : from_end - from_begin;
int to_diff = (to_end == 0) ? var_size : to_end - to_begin;
if (from_diff != to_diff)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Setting Var %s: The SRC size (%d) differs from the DEST size (%d)", _name, from_diff, to_diff);
return 0;
}
memcpy(var->data() + to_begin, MF_GetAmxAddr(amx, params[5]) + from_begin, sizeof(cell) * to_diff);
return 1;
}
}
cell AMX_NATIVE_CALL native_set_str(AMX *amx, cell *params)
{
ObjectHash hash = params[1];
const char *_name = MF_GetAmxString(amx, params[2], 0, nullptr);
auto obj = Manager::Instance()->ToObject(hash);
if (obj.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Setting Var %s: Object (%d) not found", _name, hash);
return 0;
}
auto var = Manager::Instance()->FindVariable(obj, _name);
if (var == nullptr)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Setting Var %s: Not found", _name);
return 0;
}
auto num_args = params[0] / sizeof(cell) - 2;
auto var_size = static_cast<int>(var->size());
cell *arr = MF_GetAmxAddr(amx, params[3]);
int begin = std::max(static_cast<int>(params[4]), 0);
int len = params[5];
int slen = MF_GetAmxStringLen(arr);
len = (len > 0) ? std::min(len, slen) : slen;
len = std::min(len, var_size - begin);
memcpy(var->data() + begin, arr, sizeof(cell) * (len+1));
return len;
}
cell AMX_NATIVE_CALL native_get_var_size(AMX *amx, cell *params)
{
ObjectHash hash = params[1];
const char *_name = MF_GetAmxString(amx, params[2], 0, nullptr);
auto obj = Manager::Instance()->ToObject(hash);
if (obj.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: Object (%d) not found", _name, hash);
return 0;
}
auto var = Manager::Instance()->FindVariable(obj, _name);
if (var == nullptr)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: Not found", _name);
return 0;
}
auto var_size = static_cast<int>(var->size());
return var_size;
}
cell AMX_NATIVE_CALL native_get_str_len(AMX *amx, cell *params)
{
ObjectHash hash = params[1];
const char *_name = MF_GetAmxString(amx, params[2], 0, nullptr);
auto obj = Manager::Instance()->ToObject(hash);
if (obj.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: Object (%d) not found", _name, hash);
return 0;
}
auto var = Manager::Instance()->FindVariable(obj, _name);
if (var == nullptr)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Getting Var %s: Not found", _name);
return 0;
}
return MF_GetAmxStringLen(var->data());
}
cell AMX_NATIVE_CALL native_new(AMX *amx, cell *params)
{
std::string _class = MF_GetAmxString(amx, params[1], 0, nullptr);
auto cls = Manager::Instance()->ToClass(_class).lock();
if (cls == nullptr)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Class (%s) not found", _class.c_str());
return OBJ_NULL;
}
auto hash = Manager::Instance()->NewObject(cls);
auto num_args = params[0] / sizeof(cell) - 1;
MF_ExecuteForward(g_ObjectCreated, _class.c_str(), num_args, hash);
auto ctor = Manager::Instance()->FindConstructor(cls, num_args);
if (ctor != nullptr)
{
Forward fwd(amx, params+1+1, &(ctor->pre), &(ctor->post), hash, &(ctor->args));
fwd.Call(ctor->forward);
}
return hash;
}
cell AMX_NATIVE_CALL native_hook_ctor(AMX *amx, cell *params)
{
const char *_class = MF_GetAmxString(amx, params[1], 0, nullptr);
const char *_name = MF_GetAmxString(amx, params[2], 1, nullptr);
auto cls = Manager::Instance()->ToClass(_class);
if (cls.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s@%s(...): Class not found", _class, _name);
return 0;
}
auto &ctors = cls.lock()->ctors_map;
auto iter = ctors.find(_name);
if (iter == ctors.end())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s@%s(...): Ctor not found", _class, _name);
return 0;
}
const char *_public = MF_GetAmxString(amx, params[3], 2, nullptr);
auto ctor = iter->second;
std::vector<AmxxForward> *fwds;
if (!params[4])
fwds = &ctor->pre;
else
fwds = &ctor->post;
AmxxForward fwd_id = Forward::Create(amx, _public, &ctor->args);
if (fwd_id == NO_FORWARD)
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s(...): Public not found", _public);
return 0;
}
fwds->emplace_back(fwd_id);
return 1;
}
cell AMX_NATIVE_CALL native_hook_mthd(AMX *amx, cell *params)
{
const char *_class = MF_GetAmxString(amx, params[1], 0, nullptr);
const char *_name = MF_GetAmxString(amx, params[2], 1, nullptr);
auto cls = Manager::Instance()->ToClass(_class);
if (cls.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s@%s(...): Class not found", _class, _name);
return 0;
}
auto &mthds = cls.lock()->mthds;
auto iter = mthds.find(_name);
if (iter == mthds.end())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s@%s(...): Method not found", _class, _name);
return 0;
}
const char *_public = MF_GetAmxString(amx, params[3], 2, nullptr);
auto mthd = &iter->second;
std::vector<AmxxForward> *fwds;
if (!params[4])
fwds = &mthd->pre;
else
fwds = &mthd->post;
AmxxForward fwd_id = Forward::Create(amx, _public, &mthd->args);
if (fwd_id == NO_FORWARD)
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s(...): Public not found", _public);
return 0;
}
fwds->emplace_back(fwd_id);
return 1;
}
cell AMX_NATIVE_CALL native_hook_dtor(AMX *amx, cell *params)
{
const char *_class = MF_GetAmxString(amx, params[1], 0, nullptr);
auto cls = Manager::Instance()->ToClass(_class);
if (cls.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s(...): Class not found", _class);
return 0;
}
auto *dtor = &cls.lock()->dtor;
if (dtor->forward == NO_FORWARD)
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s(...): No dtor in this class", _class);
return 0;
}
const char *_public = MF_GetAmxString(amx, params[2], 2, nullptr);
std::vector<AmxxForward> *fwds;
if (!params[3])
fwds = &dtor->pre;
else
fwds = &dtor->post;
AmxxForward fwd_id = Forward::Create(amx, _public, nullptr);
if (fwd_id == NO_FORWARD)
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s(...): Public not found", _public);
return 0;
}
fwds->emplace_back(fwd_id);
return 1;
}
cell AMX_NATIVE_CALL native_hook_set_return(AMX *amx, cell *params)
{
CallStack().Instance()->Get()->result = params[1];
return 0;
}
cell AMX_NATIVE_CALL native_hook_get_return(AMX *amx, cell *params)
{
return CallStack().Instance()->Get()->result;
}
cell AMX_NATIVE_CALL native_hook_set_param(AMX *amx, cell *params)
{
// oo_hook_set_param(1, OO_CELL, 3);
// oo_hook_set_param(2, OO_FLOAT, 5.5);
// oo_hook_set_param(3, OO_STRING, "test");
int pi = params[1] - 1;
auto call = CallStack().Instance()->Get();
auto list = call->list;
if (call == nullptr)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Current callstack has no param.");
return 0;
}
if (pi < 0 || pi >= static_cast<int>(list->size()))
{
MF_LogError(amx, AMX_ERR_NATIVE, "Param (%d) out of range (0 ~ %d)", pi + 1, list->size());
return 0;
}
int type = params[2];
if (type != list->at(pi))
{
MF_LogError(amx, AMX_ERR_NATIVE, "Param (%d) type (%d) doesn't match (expected: %d)", pi + 1, type, list->at(pi));
return 0;
}
long *data = &call->data->at(pi);
switch (type)
{
case OO_CELL:
*data = *MF_GetAmxAddr(amx, params[3]);
return 1;
case OO_BYREF:
MF_LogError(amx, AMX_ERR_NATIVE, "Param (%d) type is OO_BYREF, just change the value directly in the hook.", pi + 1);
return 0;
case OO_STRING:
{
int len = 0;
const char *newstr = MF_GetAmxString(amx, params[3], 0, &len);
auto &oldstr = call->strdata->at(pi);
if (len > static_cast<int>(oldstr.size()))
{
oldstr = newstr;
*data = reinterpret_cast<long>(oldstr.data());
}
else
{
memcpy(oldstr.data(), newstr, sizeof(char) * (len + 1));
oldstr.data()[len] = '\0';
}
return 1;
}
case OO_STRING_REF:
MF_LogError(amx, AMX_ERR_NATIVE, "Param (%d) type is OO_STRING_REF, just change the value directly in the hook.", pi + 1);
return 0;
default:
MF_LogError(amx, AMX_ERR_NATIVE, "Param (%d) type is OO_ARRAY, just change the value directly in the hook.", pi + 1);
return 0;
}
MF_LogError(amx, AMX_ERR_NATIVE, "Param (%d) type is invalid.", pi + 1);
return 0;
}
cell AMX_NATIVE_CALL native_delete(AMX *amx, cell *params)
{
ObjectHash hash = params[1];
auto obj = Manager::Instance()->ToObject(hash);
if (obj.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Object (%d) not found", hash);
return 0;
}
auto isa = obj.lock()->isa.lock();
for (auto cls : isa->mro)
{
const auto &dtor = cls.lock()->dtor;
if (dtor.forward != NO_FORWARD)
{
Forward fwd(amx, params+1, &(dtor.pre), &(dtor.post), hash, nullptr);
fwd.Call(dtor.forward);
}
MF_ExecuteForward(g_ObjectDeleted, cls.lock()->name.c_str(), hash);
}
Manager::Instance()->DeleteObject(hash);
return 0;
}
cell AMX_NATIVE_CALL native_call(AMX *amx, cell *params)
{
ObjectHash hash = params[1];
std::string _name(MF_GetAmxString(amx, params[2], 0, nullptr));
std::string super_name;
size_t super_pos = _name.find("@");
if (super_pos != std::string::npos)
super_name = _name.substr(0, _name.find("@"));
std::shared_ptr<Class> isa;
if (hash != 0)
{
auto obj = Manager::Instance()->ToObject(hash);
if (obj.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Object (%d) not found", hash);
return 0;
}
isa = obj.lock()->isa.lock();
if (!super_name.empty())
{
auto super = Manager::Instance()->ToClass(super_name);
if (super.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Call of %s (super): No such class (%s)", _name.c_str(), super_name.c_str());
return 0;
}
if (!isa->IsSubclassOf(super))
{
const auto &classes = Manager::Instance()->GetClasses();
MF_LogError(amx, AMX_ERR_NATIVE, "Call of %s (super): %s is not a super class of %s", _name.c_str(), super_name.c_str(), isa->name.c_str());
return 0;
}
isa = super.lock();
}
_name = _name.substr(super_pos + 1);
}
else
{
if (super_pos == std::string::npos)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Call of static method %s: Class name is required", _name.c_str());
return 0;
}
const char *cls_name = super_name.c_str();
auto cls = Manager::Instance()->ToClass(cls_name);
if (cls.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Call of %s: No such class (%s)", _name.c_str(), cls_name);
return 0;
}
_name = _name.substr(super_pos + 1);
isa = cls.lock();
}
auto num_args = params[0] / sizeof(cell) - 2;
auto mthd = Manager::Instance()->FindMethod(isa, _name);
if (mthd == nullptr)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Call of %s@%s: No such method!", isa->name.c_str(), _name.c_str());
return 0;
}
if (num_args != mthd->args.size())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Call of %s@%s: #args doesn't match (expected: %d, now: %d)", isa->name.c_str(), _name.c_str(), mthd->args.size(), num_args);
return 0;
}
if (hash == 0 && !mthd->is_static)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Call of %s@%s: Not a static method", isa->name.c_str(), _name.c_str());
return 0;
}
Forward fwd(amx, params+1+2, &(mthd->pre), &(mthd->post), hash, &(mthd->args));
cell ret = fwd.Call(mthd->forward);
return ret;
}
cell AMX_NATIVE_CALL native_super_ctor(AMX *amx, cell *params)
{
ObjectHash hash = CallStack::Instance()->Get()->_this;
auto obj = Manager::Instance()->ToObject(hash);
if (obj.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "*This* object is invalid");
return 0;
}
const char *super_class = MF_GetAmxString(amx, params[1], 0, nullptr);
auto super = Manager::Instance()->ToClass(super_class);
if (super.expired())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Class (%s) not found", super_class);
return 0;
}
auto num_args = params[0] / sizeof(cell) - 1;
auto ctor = Manager::Instance()->FindConstructor(super, num_args);
if (ctor == nullptr)
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s: No such constructor (#args: %d)", super_class, num_args);
return 0;
}
Forward fwd(amx, params+1+1, &(ctor->pre), &(ctor->post), hash, &(ctor->args));
fwd.Call(ctor->forward);
return 1;
}
cell AMX_NATIVE_CALL native_version(AMX *amx, cell *params)
{
MF_SetAmxString(amx, params[1], MODULE_VERSION, params[2]);
return 0;
}
void PrintArgs(const ArgTypeList *list)
{
MF_PrintSrvConsole("(");
bool first = true;
for (auto type : *list)
{
if (!first)
MF_PrintSrvConsole(", ");
switch (type)
{
case OO_CELL:
MF_PrintSrvConsole("cell");
break;
case OO_BYREF:
MF_PrintSrvConsole("byref");
break;
case OO_STRING:
MF_PrintSrvConsole("str");
break;
case OO_STRING_REF:
MF_PrintSrvConsole("stref");
break;
default:
MF_PrintSrvConsole("arr[%d]", type);
break;
}
first = false;
}
MF_PrintSrvConsole(")\n");
}
void PrintClass(std::weak_ptr<Class> _class)
{
auto cl = _class.lock();
MF_PrintSrvConsole("%s (#%p)", cl->name.c_str(), cl.get());
bool first2 = true;
for (auto su : cl->supers)
{
if (first2)
MF_PrintSrvConsole(" -> ");
else
MF_PrintSrvConsole(", ");
auto sc = su.lock();
MF_PrintSrvConsole("%s (#%p)", sc->name.c_str(), sc.get());
first2 = false;
}
MF_PrintSrvConsole("\n");
if (!cl->vars.empty())
{
MF_PrintSrvConsole(" Variables:\n");
for (const auto &pair : cl->vars)
{
MF_PrintSrvConsole(" %s[%d]\n", pair.first.c_str(), pair.second);
}
}
if (!cl->ctors.empty())
{
MF_PrintSrvConsole(" Constructors:\n");
for (const auto &pair : cl->ctors)
{
const auto &ct = pair.second;
for (const auto &cpair : cl->ctors_map)
{
if (&ct == cpair.second)
{
MF_PrintSrvConsole(" %s", cpair.first.c_str());
PrintArgs(&ct.args);
break;
}
}
}
}
if (!cl->mthds.empty())
{
MF_PrintSrvConsole(" Methods:\n");
for (const auto &pair : cl->mthds)
{
const auto &mt = pair.second;
MF_PrintSrvConsole(" %s", pair.first.c_str());
PrintArgs(&mt.args);
}
}
if (cl->dtor.forward != NO_FORWARD)
{
MF_PrintSrvConsole(" Destructor: yes\n");
}
if (cl->mro.size() > 1)
{
bool fi = true;
MF_PrintSrvConsole(" MRO Order: ");
for (auto cur : cl->mro)
{
if (!fi)
MF_PrintSrvConsole(" -> ");