-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
/
Copy pathclass_db.cpp
2333 lines (1881 loc) · 66.9 KB
/
class_db.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
/**************************************************************************/
/* class_db.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "class_db.h"
#include "core/config/engine.h"
#include "core/io/resource_loader.h"
#include "core/object/script_language.h"
#include "core/version.h"
#define OBJTYPE_RLOCK RWLockRead _rw_lockr_(lock);
#define OBJTYPE_WLOCK RWLockWrite _rw_lockw_(lock);
#ifdef DEBUG_METHODS_ENABLED
MethodDefinition D_METHODP(const char *p_name, const char *const **p_args, uint32_t p_argcount) {
MethodDefinition md;
md.name = StaticCString::create(p_name);
md.args.resize(p_argcount);
for (uint32_t i = 0; i < p_argcount; i++) {
md.args.write[i] = StaticCString::create(*p_args[i]);
}
return md;
}
#endif
ClassDB::APIType ClassDB::current_api = API_CORE;
HashMap<ClassDB::APIType, uint32_t> ClassDB::api_hashes_cache;
void ClassDB::set_current_api(APIType p_api) {
DEV_ASSERT(!api_hashes_cache.has(p_api)); // This API type may not be suitable for caching of hash if it can change later.
current_api = p_api;
}
ClassDB::APIType ClassDB::get_current_api() {
return current_api;
}
HashMap<StringName, ClassDB::ClassInfo> ClassDB::classes;
HashMap<StringName, StringName> ClassDB::resource_base_extensions;
HashMap<StringName, StringName> ClassDB::compat_classes;
#ifdef TOOLS_ENABLED
HashMap<StringName, ObjectGDExtension> ClassDB::placeholder_extensions;
class PlaceholderExtensionInstance {
StringName class_name;
HashMap<StringName, Variant> properties;
// Checks if a property is from a runtime class, and not a non-runtime base class.
bool is_runtime_property(const StringName &p_property_name) {
StringName current_class_name = class_name;
while (ClassDB::is_class_runtime(current_class_name)) {
if (ClassDB::has_property(current_class_name, p_property_name, true)) {
return true;
}
current_class_name = ClassDB::get_parent_class(current_class_name);
}
return false;
}
public:
PlaceholderExtensionInstance(const StringName &p_class_name) {
class_name = p_class_name;
}
~PlaceholderExtensionInstance() {}
void set(const StringName &p_name, const Variant &p_value, bool &r_valid) {
r_valid = is_runtime_property(p_name);
if (r_valid) {
properties[p_name] = p_value;
}
}
Variant get(const StringName &p_name, bool &r_valid) {
const Variant *value = properties.getptr(p_name);
Variant ret;
if (value) {
ret = *value;
r_valid = true;
} else {
r_valid = is_runtime_property(p_name);
if (r_valid) {
ret = ClassDB::class_get_default_property_value(class_name, p_name);
}
}
return ret;
}
static GDExtensionBool placeholder_instance_set(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value) {
PlaceholderExtensionInstance *self = (PlaceholderExtensionInstance *)p_instance;
const StringName &name = *(StringName *)p_name;
const Variant &value = *(const Variant *)p_value;
bool valid = false;
self->set(name, value, valid);
return valid;
}
static GDExtensionBool placeholder_instance_get(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) {
PlaceholderExtensionInstance *self = (PlaceholderExtensionInstance *)p_instance;
const StringName &name = *(StringName *)p_name;
Variant *value = (Variant *)r_ret;
bool valid = false;
*value = self->get(name, valid);
return valid;
}
static const GDExtensionPropertyInfo *placeholder_instance_get_property_list(GDExtensionClassInstancePtr p_instance, uint32_t *r_count) {
*r_count = 0;
return nullptr;
}
static void placeholder_instance_free_property_list(GDExtensionClassInstancePtr p_instance, const GDExtensionPropertyInfo *p_list, uint32_t p_count) {
}
static GDExtensionBool placeholder_instance_property_can_revert(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name) {
return false;
}
static GDExtensionBool placeholder_instance_property_get_revert(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) {
return false;
}
static GDExtensionBool placeholder_instance_validate_property(GDExtensionClassInstancePtr p_instance, GDExtensionPropertyInfo *p_property) {
return false;
}
static void placeholder_instance_notification(GDExtensionClassInstancePtr p_instance, int32_t p_what, GDExtensionBool p_reversed) {
}
static void placeholder_instance_to_string(GDExtensionClassInstancePtr p_instance, GDExtensionBool *r_is_valid, GDExtensionStringPtr p_out) {
*r_is_valid = true;
}
static void placeholder_instance_reference(GDExtensionClassInstancePtr p_instance) {
}
static void placeholder_instance_unreference(GDExtensionClassInstancePtr p_instance) {
}
static uint64_t placeholder_instance_get_rid(GDExtensionClassInstancePtr p_instance) {
return 0;
}
static GDExtensionObjectPtr placeholder_class_create_instance(void *p_class_userdata, GDExtensionBool p_notify_postinitialize) {
ClassDB::ClassInfo *ti = (ClassDB::ClassInfo *)p_class_userdata;
// Find the closest native parent, that isn't a runtime class.
ClassDB::ClassInfo *native_parent = ti->inherits_ptr;
while (native_parent->gdextension || native_parent->is_runtime) {
native_parent = native_parent->inherits_ptr;
}
ERR_FAIL_NULL_V(native_parent->creation_func, nullptr);
// Construct a placeholder.
Object *obj = native_parent->creation_func(static_cast<bool>(p_notify_postinitialize));
// ClassDB::set_object_extension_instance() won't be called for placeholders.
// We need need to make sure that all the things it would have done (even if
// done in a different way to support placeholders) will also be done here.
obj->_extension = ClassDB::get_placeholder_extension(ti->name);
obj->_extension_instance = memnew(PlaceholderExtensionInstance(ti->name));
#ifdef TOOLS_ENABLED
if (obj->_extension->track_instance) {
obj->_extension->track_instance(obj->_extension->tracking_userdata, obj);
}
#endif
return obj;
}
static GDExtensionObjectPtr placeholder_class_recreate_instance(void *p_class_userdata, GDExtensionObjectPtr p_object) {
ClassDB::ClassInfo *ti = (ClassDB::ClassInfo *)p_class_userdata;
return memnew(PlaceholderExtensionInstance(ti->name));
}
static void placeholder_class_free_instance(void *p_class_userdata, GDExtensionClassInstancePtr p_instance) {
PlaceholderExtensionInstance *instance = (PlaceholderExtensionInstance *)p_instance;
memdelete(instance);
}
static GDExtensionClassCallVirtual placeholder_class_get_virtual(void *p_class_userdata, GDExtensionConstStringNamePtr p_name) {
return nullptr;
}
};
#endif
bool ClassDB::_is_parent_class(const StringName &p_class, const StringName &p_inherits) {
ClassInfo *c = classes.getptr(p_class);
while (c) {
if (c->name == p_inherits) {
return true;
}
c = c->inherits_ptr;
}
return false;
}
bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) {
OBJTYPE_RLOCK;
return _is_parent_class(p_class, p_inherits);
}
void ClassDB::get_class_list(List<StringName> *p_classes) {
OBJTYPE_RLOCK;
for (const KeyValue<StringName, ClassInfo> &E : classes) {
p_classes->push_back(E.key);
}
p_classes->sort_custom<StringName::AlphCompare>();
}
#ifdef TOOLS_ENABLED
void ClassDB::get_extensions_class_list(List<StringName> *p_classes) {
OBJTYPE_RLOCK;
for (const KeyValue<StringName, ClassInfo> &E : classes) {
if (E.value.api != API_EXTENSION && E.value.api != API_EDITOR_EXTENSION) {
continue;
}
p_classes->push_back(E.key);
}
p_classes->sort_custom<StringName::AlphCompare>();
}
void ClassDB::get_extension_class_list(const Ref<GDExtension> &p_extension, List<StringName> *p_classes) {
OBJTYPE_RLOCK;
for (const KeyValue<StringName, ClassInfo> &E : classes) {
if (E.value.api != API_EXTENSION && E.value.api != API_EDITOR_EXTENSION) {
continue;
}
if (!E.value.gdextension || E.value.gdextension->library != p_extension.ptr()) {
continue;
}
p_classes->push_back(E.key);
}
p_classes->sort_custom<StringName::AlphCompare>();
}
#endif
void ClassDB::get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
OBJTYPE_RLOCK;
for (const KeyValue<StringName, ClassInfo> &E : classes) {
if (E.key != p_class && _is_parent_class(E.key, p_class)) {
p_classes->push_back(E.key);
}
}
}
void ClassDB::get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
OBJTYPE_RLOCK;
for (const KeyValue<StringName, ClassInfo> &E : classes) {
if (E.key != p_class && _get_parent_class(E.key) == p_class) {
p_classes->push_back(E.key);
}
}
}
StringName ClassDB::get_parent_class_nocheck(const StringName &p_class) {
OBJTYPE_RLOCK;
ClassInfo *ti = classes.getptr(p_class);
if (!ti) {
return StringName();
}
return ti->inherits;
}
bool ClassDB::get_inheritance_chain_nocheck(const StringName &p_class, Vector<StringName> &r_result) {
OBJTYPE_RLOCK;
ClassInfo *start = classes.getptr(p_class);
if (!start) {
return false;
}
int classes_to_add = 0;
for (ClassInfo *ti = start; ti; ti = ti->inherits_ptr) {
classes_to_add++;
}
int64_t old_size = r_result.size();
r_result.resize(old_size + classes_to_add);
StringName *w = r_result.ptrw() + old_size;
for (ClassInfo *ti = start; ti; ti = ti->inherits_ptr) {
*w++ = ti->name;
}
return true;
}
StringName ClassDB::get_compatibility_remapped_class(const StringName &p_class) {
if (classes.has(p_class)) {
return p_class;
}
if (compat_classes.has(p_class)) {
return compat_classes[p_class];
}
return p_class;
}
StringName ClassDB::_get_parent_class(const StringName &p_class) {
ClassInfo *ti = classes.getptr(p_class);
ERR_FAIL_NULL_V_MSG(ti, StringName(), vformat("Cannot get class '%s'.", String(p_class)));
return ti->inherits;
}
StringName ClassDB::get_parent_class(const StringName &p_class) {
OBJTYPE_RLOCK;
return _get_parent_class(p_class);
}
ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {
OBJTYPE_RLOCK;
ClassInfo *ti = classes.getptr(p_class);
ERR_FAIL_NULL_V_MSG(ti, API_NONE, vformat("Cannot get class '%s'.", String(p_class)));
return ti->api;
}
uint32_t ClassDB::get_api_hash(APIType p_api) {
#ifdef DEBUG_METHODS_ENABLED
OBJTYPE_WLOCK;
if (api_hashes_cache.has(p_api)) {
return api_hashes_cache[p_api];
}
uint64_t hash = hash_murmur3_one_64(HashMapHasherDefault::hash(VERSION_FULL_CONFIG));
List<StringName> class_list;
for (const KeyValue<StringName, ClassInfo> &E : classes) {
class_list.push_back(E.key);
}
// Must be alphabetically sorted for hash to compute.
class_list.sort_custom<StringName::AlphCompare>();
for (const StringName &E : class_list) {
ClassInfo *t = classes.getptr(E);
ERR_FAIL_NULL_V_MSG(t, 0, vformat("Cannot get class '%s'.", String(E)));
if (t->api != p_api || !t->exposed) {
continue;
}
hash = hash_murmur3_one_64(t->name.hash(), hash);
hash = hash_murmur3_one_64(t->inherits.hash(), hash);
{ //methods
List<StringName> snames;
for (const KeyValue<StringName, MethodBind *> &F : t->method_map) {
String name = F.key.operator String();
ERR_CONTINUE(name.is_empty());
if (name[0] == '_') {
continue; // Ignore non-virtual methods that start with an underscore
}
snames.push_back(F.key);
}
snames.sort_custom<StringName::AlphCompare>();
for (const StringName &F : snames) {
MethodBind *mb = t->method_map[F];
hash = hash_murmur3_one_64(mb->get_name().hash(), hash);
hash = hash_murmur3_one_64(mb->get_argument_count(), hash);
hash = hash_murmur3_one_64(mb->get_argument_type(-1), hash); //return
for (int i = 0; i < mb->get_argument_count(); i++) {
const PropertyInfo info = mb->get_argument_info(i);
hash = hash_murmur3_one_64(info.type, hash);
hash = hash_murmur3_one_64(info.name.hash(), hash);
hash = hash_murmur3_one_64(info.hint, hash);
hash = hash_murmur3_one_64(info.hint_string.hash(), hash);
}
hash = hash_murmur3_one_64(mb->get_default_argument_count(), hash);
for (int i = 0; i < mb->get_argument_count(); i++) {
if (mb->has_default_argument(i)) {
Variant da = mb->get_default_argument(i);
hash = hash_murmur3_one_64(da.hash(), hash);
}
}
hash = hash_murmur3_one_64(mb->get_hint_flags(), hash);
}
}
{ //constants
List<StringName> snames;
for (const KeyValue<StringName, int64_t> &F : t->constant_map) {
snames.push_back(F.key);
}
snames.sort_custom<StringName::AlphCompare>();
for (const StringName &F : snames) {
hash = hash_murmur3_one_64(F.hash(), hash);
hash = hash_murmur3_one_64(uint64_t(t->constant_map[F]), hash);
}
}
{ //signals
List<StringName> snames;
for (const KeyValue<StringName, MethodInfo> &F : t->signal_map) {
snames.push_back(F.key);
}
snames.sort_custom<StringName::AlphCompare>();
for (const StringName &F : snames) {
MethodInfo &mi = t->signal_map[F];
hash = hash_murmur3_one_64(F.hash(), hash);
for (const PropertyInfo &pi : mi.arguments) {
hash = hash_murmur3_one_64(pi.type, hash);
}
}
}
{ //properties
List<StringName> snames;
for (const KeyValue<StringName, PropertySetGet> &F : t->property_setget) {
snames.push_back(F.key);
}
snames.sort_custom<StringName::AlphCompare>();
for (const StringName &F : snames) {
PropertySetGet *psg = t->property_setget.getptr(F);
ERR_FAIL_NULL_V(psg, 0);
hash = hash_murmur3_one_64(F.hash(), hash);
hash = hash_murmur3_one_64(psg->setter.hash(), hash);
hash = hash_murmur3_one_64(psg->getter.hash(), hash);
}
}
//property list
for (const PropertyInfo &F : t->property_list) {
hash = hash_murmur3_one_64(F.name.hash(), hash);
hash = hash_murmur3_one_64(F.type, hash);
hash = hash_murmur3_one_64(F.hint, hash);
hash = hash_murmur3_one_64(F.hint_string.hash(), hash);
hash = hash_murmur3_one_64(F.usage, hash);
}
}
hash = hash_fmix32(hash);
// Extension API changes at runtime; let's just not cache them by now.
if (p_api != API_EXTENSION && p_api != API_EDITOR_EXTENSION) {
api_hashes_cache[p_api] = hash;
}
return hash;
#else
return 0;
#endif
}
bool ClassDB::class_exists(const StringName &p_class) {
OBJTYPE_RLOCK;
return classes.has(p_class);
}
void ClassDB::add_compatibility_class(const StringName &p_class, const StringName &p_fallback) {
OBJTYPE_WLOCK;
compat_classes[p_class] = p_fallback;
}
StringName ClassDB::get_compatibility_class(const StringName &p_class) {
if (compat_classes.has(p_class)) {
return compat_classes[p_class];
}
return StringName();
}
Object *ClassDB::_instantiate_internal(const StringName &p_class, bool p_require_real_class, bool p_notify_postinitialize) {
ClassInfo *ti;
{
OBJTYPE_RLOCK;
ti = classes.getptr(p_class);
if (!_can_instantiate(ti)) {
if (compat_classes.has(p_class)) {
ti = classes.getptr(compat_classes[p_class]);
}
}
ERR_FAIL_NULL_V_MSG(ti, nullptr, vformat("Cannot get class '%s'.", String(p_class)));
ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, vformat("Class '%s' is disabled.", String(p_class)));
ERR_FAIL_NULL_V_MSG(ti->creation_func, nullptr, vformat("Class '%s' or its base class cannot be instantiated.", String(p_class)));
}
#ifdef TOOLS_ENABLED
if ((ti->api == API_EDITOR || ti->api == API_EDITOR_EXTENSION) && !Engine::get_singleton()->is_editor_hint()) {
ERR_PRINT(vformat("Class '%s' can only be instantiated by editor.", String(p_class)));
return nullptr;
}
#endif
#ifdef TOOLS_ENABLED
// Try to create placeholder.
if (!p_require_real_class && ti->is_runtime && Engine::get_singleton()->is_editor_hint()) {
bool can_create_placeholder = false;
if (ti->gdextension) {
if (ti->gdextension->create_instance2) {
can_create_placeholder = true;
}
#ifndef DISABLE_DEPRECATED
else if (ti->gdextension->create_instance) {
can_create_placeholder = true;
}
#endif // DISABLE_DEPRECATED
} else if (!ti->inherits_ptr || !ti->inherits_ptr->creation_func) {
ERR_PRINT(vformat("Cannot make a placeholder instance of runtime class %s because its parent cannot be constructed.", ti->name));
} else {
can_create_placeholder = true;
}
if (can_create_placeholder) {
ObjectGDExtension *extension = get_placeholder_extension(ti->name);
return (Object *)extension->create_instance2(extension->class_userdata, p_notify_postinitialize);
}
}
#endif // TOOLS_ENABLED
if (ti->gdextension && ti->gdextension->create_instance2) {
ObjectGDExtension *extension = ti->gdextension;
return (Object *)extension->create_instance2(extension->class_userdata, p_notify_postinitialize);
}
#ifndef DISABLE_DEPRECATED
else if (ti->gdextension && ti->gdextension->create_instance) {
ObjectGDExtension *extension = ti->gdextension;
return (Object *)extension->create_instance(extension->class_userdata);
}
#endif // DISABLE_DEPRECATED
else {
return ti->creation_func(p_notify_postinitialize);
}
}
bool ClassDB::_can_instantiate(ClassInfo *p_class_info) {
if (!p_class_info) {
return false;
}
if (p_class_info->disabled || !p_class_info->creation_func) {
return false;
}
if (!p_class_info->gdextension) {
return true;
}
if (p_class_info->gdextension->create_instance2) {
return true;
}
#ifndef DISABLE_DEPRECATED
if (p_class_info->gdextension->create_instance) {
return true;
}
#endif // DISABLE_DEPRECATED
return false;
}
Object *ClassDB::instantiate(const StringName &p_class) {
return _instantiate_internal(p_class);
}
Object *ClassDB::instantiate_no_placeholders(const StringName &p_class) {
return _instantiate_internal(p_class, true);
}
Object *ClassDB::instantiate_without_postinitialization(const StringName &p_class) {
return _instantiate_internal(p_class, true, false);
}
#ifdef TOOLS_ENABLED
ObjectGDExtension *ClassDB::get_placeholder_extension(const StringName &p_class) {
ObjectGDExtension *placeholder_extension = placeholder_extensions.getptr(p_class);
if (placeholder_extension) {
return placeholder_extension;
}
ClassInfo *ti;
{
OBJTYPE_RLOCK;
ti = classes.getptr(p_class);
if (!_can_instantiate(ti)) {
if (compat_classes.has(p_class)) {
ti = classes.getptr(compat_classes[p_class]);
}
}
ERR_FAIL_NULL_V_MSG(ti, nullptr, vformat("Cannot get class '%s'.", String(p_class)));
ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, vformat("Class '%s' is disabled.", String(p_class)));
}
// Make a "fake" extension to act as a placeholder.
placeholder_extensions[p_class] = ObjectGDExtension();
placeholder_extension = placeholder_extensions.getptr(p_class);
placeholder_extension->is_runtime = true;
placeholder_extension->is_placeholder = true;
if (ti->gdextension) {
placeholder_extension->library = ti->gdextension->library;
placeholder_extension->parent = ti->gdextension->parent;
placeholder_extension->children = ti->gdextension->children;
placeholder_extension->parent_class_name = ti->gdextension->parent_class_name;
placeholder_extension->class_name = ti->gdextension->class_name;
placeholder_extension->editor_class = ti->gdextension->editor_class;
placeholder_extension->reloadable = ti->gdextension->reloadable;
placeholder_extension->is_virtual = ti->gdextension->is_virtual;
placeholder_extension->is_abstract = ti->gdextension->is_abstract;
placeholder_extension->is_exposed = ti->gdextension->is_exposed;
placeholder_extension->tracking_userdata = ti->gdextension->tracking_userdata;
placeholder_extension->track_instance = ti->gdextension->track_instance;
placeholder_extension->untrack_instance = ti->gdextension->untrack_instance;
} else {
placeholder_extension->library = nullptr;
placeholder_extension->parent = nullptr;
placeholder_extension->parent_class_name = ti->inherits;
placeholder_extension->class_name = ti->name;
placeholder_extension->editor_class = ti->api == API_EDITOR;
placeholder_extension->reloadable = false;
placeholder_extension->is_virtual = ti->is_virtual;
placeholder_extension->is_abstract = false;
placeholder_extension->is_exposed = ti->exposed;
}
placeholder_extension->set = &PlaceholderExtensionInstance::placeholder_instance_set;
placeholder_extension->get = &PlaceholderExtensionInstance::placeholder_instance_get;
placeholder_extension->get_property_list = &PlaceholderExtensionInstance::placeholder_instance_get_property_list;
placeholder_extension->free_property_list2 = &PlaceholderExtensionInstance::placeholder_instance_free_property_list;
placeholder_extension->property_can_revert = &PlaceholderExtensionInstance::placeholder_instance_property_can_revert;
placeholder_extension->property_get_revert = &PlaceholderExtensionInstance::placeholder_instance_property_get_revert;
placeholder_extension->validate_property = &PlaceholderExtensionInstance::placeholder_instance_validate_property;
#ifndef DISABLE_DEPRECATED
placeholder_extension->notification = nullptr;
placeholder_extension->free_property_list = nullptr;
#endif // DISABLE_DEPRECATED
placeholder_extension->notification2 = &PlaceholderExtensionInstance::placeholder_instance_notification;
placeholder_extension->to_string = &PlaceholderExtensionInstance::placeholder_instance_to_string;
placeholder_extension->reference = &PlaceholderExtensionInstance::placeholder_instance_reference;
placeholder_extension->unreference = &PlaceholderExtensionInstance::placeholder_instance_unreference;
placeholder_extension->get_rid = &PlaceholderExtensionInstance::placeholder_instance_get_rid;
placeholder_extension->class_userdata = ti;
#ifndef DISABLE_DEPRECATED
placeholder_extension->create_instance = nullptr;
#endif // DISABLE_DEPRECATED
placeholder_extension->create_instance2 = &PlaceholderExtensionInstance::placeholder_class_create_instance;
placeholder_extension->free_instance = &PlaceholderExtensionInstance::placeholder_class_free_instance;
placeholder_extension->get_virtual = &PlaceholderExtensionInstance::placeholder_class_get_virtual;
placeholder_extension->get_virtual_call_data = nullptr;
placeholder_extension->call_virtual_with_data = nullptr;
placeholder_extension->recreate_instance = &PlaceholderExtensionInstance::placeholder_class_recreate_instance;
return placeholder_extension;
}
#endif
void ClassDB::set_object_extension_instance(Object *p_object, const StringName &p_class, GDExtensionClassInstancePtr p_instance) {
ERR_FAIL_NULL(p_object);
ClassInfo *ti;
{
OBJTYPE_RLOCK;
ti = classes.getptr(p_class);
if (!_can_instantiate(ti)) {
if (compat_classes.has(p_class)) {
ti = classes.getptr(compat_classes[p_class]);
}
}
ERR_FAIL_NULL_MSG(ti, vformat("Cannot get class '%s'.", String(p_class)));
ERR_FAIL_COND_MSG(ti->disabled, vformat("Class '%s' is disabled.", String(p_class)));
ERR_FAIL_NULL_MSG(ti->gdextension, vformat("Class '%s' has no native extension.", String(p_class)));
}
p_object->_extension = ti->gdextension;
p_object->_extension_instance = p_instance;
#ifdef TOOLS_ENABLED
if (p_object->_extension->track_instance) {
p_object->_extension->track_instance(p_object->_extension->tracking_userdata, p_object);
}
#endif
}
bool ClassDB::can_instantiate(const StringName &p_class) {
String script_path;
{
OBJTYPE_RLOCK;
ClassInfo *ti = classes.getptr(p_class);
if (!ti) {
if (!ScriptServer::is_global_class(p_class)) {
ERR_FAIL_V_MSG(false, vformat("Cannot get class '%s'.", String(p_class)));
}
script_path = ScriptServer::get_global_class_path(p_class);
goto use_script; // Open the lock for resource loading.
}
#ifdef TOOLS_ENABLED
if ((ti->api == API_EDITOR || ti->api == API_EDITOR_EXTENSION) && !Engine::get_singleton()->is_editor_hint()) {
return false;
}
#endif
return _can_instantiate(ti);
}
use_script:
Ref<Script> scr = ResourceLoader::load(script_path);
return scr.is_valid() && scr->is_valid() && !scr->is_abstract();
}
bool ClassDB::is_abstract(const StringName &p_class) {
String script_path;
{
OBJTYPE_RLOCK;
ClassInfo *ti = classes.getptr(p_class);
if (!ti) {
if (!ScriptServer::is_global_class(p_class)) {
ERR_FAIL_V_MSG(false, vformat("Cannot get class '%s'.", String(p_class)));
}
script_path = ScriptServer::get_global_class_path(p_class);
goto use_script; // Open the lock for resource loading.
}
if (ti->creation_func != nullptr) {
return false;
}
if (!ti->gdextension) {
return true;
}
#ifndef DISABLE_DEPRECATED
return ti->gdextension->create_instance2 == nullptr && ti->gdextension->create_instance == nullptr;
#else
return ti->gdextension->create_instance2 == nullptr;
#endif // DISABLE_DEPRECATED
}
use_script:
Ref<Script> scr = ResourceLoader::load(script_path);
return scr.is_valid() && scr->is_valid() && scr->is_abstract();
}
bool ClassDB::is_virtual(const StringName &p_class) {
String script_path;
{
OBJTYPE_RLOCK;
ClassInfo *ti = classes.getptr(p_class);
if (!ti) {
if (!ScriptServer::is_global_class(p_class)) {
ERR_FAIL_V_MSG(false, vformat("Cannot get class '%s'.", String(p_class)));
}
script_path = ScriptServer::get_global_class_path(p_class);
goto use_script; // Open the lock for resource loading.
}
#ifdef TOOLS_ENABLED
if ((ti->api == API_EDITOR || ti->api == API_EDITOR_EXTENSION) && !Engine::get_singleton()->is_editor_hint()) {
return false;
}
#endif
return (_can_instantiate(ti) && ti->is_virtual);
}
use_script:
Ref<Script> scr = ResourceLoader::load(script_path);
return scr.is_valid() && scr->is_valid() && scr->is_abstract();
}
void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherits) {
OBJTYPE_WLOCK;
const StringName &name = p_class;
ERR_FAIL_COND_MSG(classes.has(name), vformat("Class '%s' already exists.", String(p_class)));
classes[name] = ClassInfo();
ClassInfo &ti = classes[name];
ti.name = name;
ti.inherits = p_inherits;
ti.api = current_api;
if (ti.inherits) {
ERR_FAIL_COND(!classes.has(ti.inherits)); //it MUST be registered.
ti.inherits_ptr = &classes[ti.inherits];
} else {
ti.inherits_ptr = nullptr;
}
}
static MethodInfo info_from_bind(MethodBind *p_method) {
MethodInfo minfo;
minfo.name = p_method->get_name();
minfo.id = p_method->get_method_id();
for (int i = 0; i < p_method->get_argument_count(); i++) {
minfo.arguments.push_back(p_method->get_argument_info(i));
}
minfo.return_val = p_method->get_return_info();
minfo.flags = p_method->get_hint_flags();
for (int i = 0; i < p_method->get_argument_count(); i++) {
if (p_method->has_default_argument(i)) {
minfo.default_arguments.push_back(p_method->get_default_argument(i));
}
}
return minfo;
}
void ClassDB::get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {
OBJTYPE_RLOCK;
ClassInfo *type = classes.getptr(p_class);
while (type) {
if (type->disabled) {
if (p_no_inheritance) {
break;
}
type = type->inherits_ptr;
continue;
}
#ifdef DEBUG_METHODS_ENABLED
for (const MethodInfo &E : type->virtual_methods) {
p_methods->push_back(E);
}
for (const StringName &E : type->method_order) {
if (p_exclude_from_properties && type->methods_in_properties.has(E)) {
continue;
}
MethodBind *method = type->method_map.get(E);
MethodInfo minfo = info_from_bind(method);
p_methods->push_back(minfo);
}
#else
for (KeyValue<StringName, MethodBind *> &E : type->method_map) {
MethodBind *m = E.value;
MethodInfo minfo = info_from_bind(m);
p_methods->push_back(minfo);
}
#endif
if (p_no_inheritance) {
break;
}
type = type->inherits_ptr;
}
}
void ClassDB::get_method_list_with_compatibility(const StringName &p_class, List<Pair<MethodInfo, uint32_t>> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {
OBJTYPE_RLOCK;
ClassInfo *type = classes.getptr(p_class);
while (type) {
if (type->disabled) {
if (p_no_inheritance) {
break;
}
type = type->inherits_ptr;
continue;
}
#ifdef DEBUG_METHODS_ENABLED
for (const MethodInfo &E : type->virtual_methods) {
Pair<MethodInfo, uint32_t> pair(E, 0);
p_methods->push_back(pair);
}
for (const StringName &E : type->method_order) {
if (p_exclude_from_properties && type->methods_in_properties.has(E)) {
continue;
}
MethodBind *method = type->method_map.get(E);
MethodInfo minfo = info_from_bind(method);
Pair<MethodInfo, uint32_t> pair(minfo, method->get_hash());
p_methods->push_back(pair);
}
#else
for (KeyValue<StringName, MethodBind *> &E : type->method_map) {
MethodBind *method = E.value;
MethodInfo minfo = info_from_bind(method);
Pair<MethodInfo, uint32_t> pair(minfo, method->get_hash());
p_methods->push_back(pair);
}
#endif
for (const KeyValue<StringName, LocalVector<MethodBind *, unsigned int, false, false>> &E : type->method_map_compatibility) {
LocalVector<MethodBind *> compat = E.value;
for (MethodBind *method : compat) {
MethodInfo minfo = info_from_bind(method);
Pair<MethodInfo, uint32_t> pair(minfo, method->get_hash());
p_methods->push_back(pair);
}
}
if (p_no_inheritance) {
break;
}
type = type->inherits_ptr;
}
}
bool ClassDB::get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance, bool p_exclude_from_properties) {
OBJTYPE_RLOCK;
ClassInfo *type = classes.getptr(p_class);
while (type) {
if (type->disabled) {
if (p_no_inheritance) {
break;
}
type = type->inherits_ptr;
continue;
}
#ifdef DEBUG_METHODS_ENABLED
MethodBind **method = type->method_map.getptr(p_method);