Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/object/script_language.cpp
20852 views
1
/**************************************************************************/
2
/* script_language.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "script_language.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/core_bind.h"
35
#include "core/debugger/engine_debugger.h"
36
#include "core/debugger/script_debugger.h"
37
#include "core/io/resource_loader.h"
38
#include "core/templates/sort_array.h"
39
40
ScriptLanguage *ScriptServer::_languages[MAX_LANGUAGES];
41
int ScriptServer::_language_count = 0;
42
bool ScriptServer::languages_ready = false;
43
Mutex ScriptServer::languages_mutex;
44
thread_local bool ScriptServer::thread_entered = false;
45
46
bool ScriptServer::scripting_enabled = true;
47
bool ScriptServer::reload_scripts_on_save = false;
48
49
// These need to be the last static variables in this file, since we're exploiting the reverse-order destruction of static variables.
50
static bool is_program_exiting = false;
51
struct ProgramExitGuard {
52
~ProgramExitGuard() {
53
is_program_exiting = true;
54
}
55
};
56
static ProgramExitGuard program_exit_guard;
57
58
void Script::_notification(int p_what) {
59
switch (p_what) {
60
case NOTIFICATION_POSTINITIALIZE: {
61
if (EngineDebugger::is_active()) {
62
callable_mp(this, &Script::_set_debugger_break_language).call_deferred();
63
}
64
} break;
65
}
66
}
67
68
Variant Script::_get_property_default_value(const StringName &p_property) {
69
Variant ret;
70
get_property_default_value(p_property, ret);
71
return ret;
72
}
73
74
TypedArray<Dictionary> Script::_get_script_property_list() {
75
TypedArray<Dictionary> ret;
76
List<PropertyInfo> list;
77
get_script_property_list(&list);
78
for (const PropertyInfo &E : list) {
79
ret.append(E.operator Dictionary());
80
}
81
return ret;
82
}
83
84
TypedArray<Dictionary> Script::_get_script_method_list() {
85
TypedArray<Dictionary> ret;
86
List<MethodInfo> list;
87
get_script_method_list(&list);
88
for (const MethodInfo &E : list) {
89
ret.append(E.operator Dictionary());
90
}
91
return ret;
92
}
93
94
TypedArray<Dictionary> Script::_get_script_signal_list() {
95
TypedArray<Dictionary> ret;
96
List<MethodInfo> list;
97
get_script_signal_list(&list);
98
for (const MethodInfo &E : list) {
99
ret.append(E.operator Dictionary());
100
}
101
return ret;
102
}
103
104
Dictionary Script::_get_script_constant_map() {
105
Dictionary ret;
106
HashMap<StringName, Variant> map;
107
get_constants(&map);
108
for (const KeyValue<StringName, Variant> &E : map) {
109
ret[E.key] = E.value;
110
}
111
return ret;
112
}
113
114
void Script::_set_debugger_break_language() {
115
if (EngineDebugger::is_active()) {
116
EngineDebugger::get_script_debugger()->set_break_language(get_language());
117
}
118
}
119
120
int Script::get_script_method_argument_count(const StringName &p_method, bool *r_is_valid) const {
121
MethodInfo mi = get_method_info(p_method);
122
123
if (mi == MethodInfo()) {
124
if (r_is_valid) {
125
*r_is_valid = false;
126
}
127
return 0;
128
}
129
130
if (r_is_valid) {
131
*r_is_valid = true;
132
}
133
return mi.arguments.size();
134
}
135
136
#ifdef TOOLS_ENABLED
137
138
PropertyInfo Script::get_class_category() const {
139
String path = get_path();
140
String scr_name;
141
142
if (is_built_in()) {
143
if (get_name().is_empty()) {
144
scr_name = TTR("Built-in script");
145
} else {
146
scr_name = vformat("%s (%s)", get_name(), TTR("Built-in"));
147
}
148
} else {
149
if (get_name().is_empty()) {
150
scr_name = path.get_file();
151
} else {
152
scr_name = get_name();
153
}
154
}
155
156
return PropertyInfo(Variant::NIL, scr_name, PROPERTY_HINT_NONE, path, PROPERTY_USAGE_CATEGORY);
157
}
158
159
#endif // TOOLS_ENABLED
160
161
void Script::_bind_methods() {
162
ClassDB::bind_method(D_METHOD("can_instantiate"), &Script::can_instantiate);
163
//ClassDB::bind_method(D_METHOD("instance_create","base_object"),&Script::instance_create);
164
ClassDB::bind_method(D_METHOD("instance_has", "base_object"), &Script::instance_has);
165
ClassDB::bind_method(D_METHOD("has_source_code"), &Script::has_source_code);
166
ClassDB::bind_method(D_METHOD("get_source_code"), &Script::get_source_code);
167
ClassDB::bind_method(D_METHOD("set_source_code", "source"), &Script::set_source_code);
168
ClassDB::bind_method(D_METHOD("reload", "keep_state"), &Script::reload, DEFVAL(false));
169
ClassDB::bind_method(D_METHOD("get_base_script"), &Script::get_base_script);
170
ClassDB::bind_method(D_METHOD("get_instance_base_type"), &Script::get_instance_base_type);
171
172
ClassDB::bind_method(D_METHOD("get_global_name"), &Script::get_global_name);
173
174
ClassDB::bind_method(D_METHOD("has_script_signal", "signal_name"), &Script::has_script_signal);
175
176
ClassDB::bind_method(D_METHOD("get_script_property_list"), &Script::_get_script_property_list);
177
ClassDB::bind_method(D_METHOD("get_script_method_list"), &Script::_get_script_method_list);
178
ClassDB::bind_method(D_METHOD("get_script_signal_list"), &Script::_get_script_signal_list);
179
ClassDB::bind_method(D_METHOD("get_script_constant_map"), &Script::_get_script_constant_map);
180
ClassDB::bind_method(D_METHOD("get_property_default_value", "property"), &Script::_get_property_default_value);
181
182
ClassDB::bind_method(D_METHOD("is_tool"), &Script::is_tool);
183
ClassDB::bind_method(D_METHOD("is_abstract"), &Script::is_abstract);
184
185
ClassDB::bind_method(D_METHOD("get_rpc_config"), &Script::_get_rpc_config_bind);
186
187
ADD_PROPERTY(PropertyInfo(Variant::STRING, "source_code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_source_code", "get_source_code");
188
}
189
190
void Script::reload_from_file() {
191
#ifdef TOOLS_ENABLED
192
// Replicates how the ScriptEditor reloads script resources, which generally handles it.
193
// However, when scripts are to be reloaded but aren't open in the internal editor, we go through here instead.
194
const Ref<Script> rel = ResourceLoader::load(ResourceLoader::path_remap(get_path()), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
195
if (rel.is_null()) {
196
return;
197
}
198
199
set_source_code(rel->get_source_code());
200
set_last_modified_time(rel->get_last_modified_time());
201
202
// Only reload the script when there are no compilation errors to prevent printing the error messages twice.
203
if (rel->is_valid()) {
204
if (Engine::get_singleton()->is_editor_hint() && is_tool()) {
205
get_language()->reload_tool_script(this, true);
206
} else {
207
// It's important to set p_keep_state to true in order to manage reloading scripts
208
// that are currently instantiated.
209
reload(true);
210
}
211
}
212
213
#else
214
Resource::reload_from_file();
215
#endif
216
}
217
218
void ScriptServer::set_scripting_enabled(bool p_enabled) {
219
scripting_enabled = p_enabled;
220
}
221
222
bool ScriptServer::is_scripting_enabled() {
223
return scripting_enabled;
224
}
225
226
ScriptLanguage *ScriptServer::get_language(int p_idx) {
227
MutexLock lock(languages_mutex);
228
ERR_FAIL_INDEX_V(p_idx, _language_count, nullptr);
229
return _languages[p_idx];
230
}
231
232
ScriptLanguage *ScriptServer::get_language_for_extension(const String &p_extension) {
233
MutexLock lock(languages_mutex);
234
235
for (int i = 0; i < _language_count; i++) {
236
if (_languages[i] && _languages[i]->get_extension() == p_extension) {
237
return _languages[i];
238
}
239
}
240
241
return nullptr;
242
}
243
244
Error ScriptServer::register_language(ScriptLanguage *p_language) {
245
MutexLock lock(languages_mutex);
246
ERR_FAIL_NULL_V(p_language, ERR_INVALID_PARAMETER);
247
ERR_FAIL_COND_V_MSG(_language_count >= MAX_LANGUAGES, ERR_UNAVAILABLE, "Script languages limit has been reach, cannot register more.");
248
for (int i = 0; i < _language_count; i++) {
249
const ScriptLanguage *other_language = _languages[i];
250
ERR_FAIL_COND_V_MSG(other_language->get_extension() == p_language->get_extension(), ERR_ALREADY_EXISTS, vformat("A script language with extension '%s' is already registered.", p_language->get_extension()));
251
ERR_FAIL_COND_V_MSG(other_language->get_name() == p_language->get_name(), ERR_ALREADY_EXISTS, vformat("A script language with name '%s' is already registered.", p_language->get_name()));
252
ERR_FAIL_COND_V_MSG(other_language->get_type() == p_language->get_type(), ERR_ALREADY_EXISTS, vformat("A script language with type '%s' is already registered.", p_language->get_type()));
253
}
254
_languages[_language_count++] = p_language;
255
256
// Make sure the new language is initialized in case languages have already been initialized before
257
// This happens when importing the GDExtension for the first time in the editor
258
if (languages_ready) {
259
p_language->init();
260
}
261
262
return OK;
263
}
264
265
Error ScriptServer::unregister_language(const ScriptLanguage *p_language) {
266
MutexLock lock(languages_mutex);
267
268
for (int i = 0; i < _language_count; i++) {
269
if (_languages[i] == p_language) {
270
_language_count--;
271
if (i < _language_count) {
272
SWAP(_languages[i], _languages[_language_count]);
273
}
274
return OK;
275
}
276
}
277
return ERR_DOES_NOT_EXIST;
278
}
279
280
void ScriptServer::init_languages() {
281
{ // Load global classes.
282
global_classes_clear();
283
#ifndef DISABLE_DEPRECATED
284
if (ProjectSettings::get_singleton()->has_setting("_global_script_classes")) {
285
Array script_classes = GLOBAL_GET("_global_script_classes");
286
287
for (const Variant &script_class : script_classes) {
288
Dictionary c = script_class;
289
if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base") || !c.has("is_abstract") || !c.has("is_tool")) {
290
continue;
291
}
292
add_global_class(c["class"], c["base"], c["language"], c["path"], c["is_abstract"], c["is_tool"]);
293
}
294
ProjectSettings::get_singleton()->clear("_global_script_classes");
295
}
296
#endif
297
298
Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
299
for (const Variant &script_class : script_classes) {
300
Dictionary c = script_class;
301
if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base") || !c.has("is_abstract") || !c.has("is_tool")) {
302
continue;
303
}
304
add_global_class(c["class"], c["base"], c["language"], c["path"], c["is_abstract"], c["is_tool"]);
305
}
306
}
307
308
HashSet<ScriptLanguage *> langs_to_init;
309
{
310
MutexLock lock(languages_mutex);
311
for (int i = 0; i < _language_count; i++) {
312
if (_languages[i]) {
313
langs_to_init.insert(_languages[i]);
314
}
315
}
316
}
317
318
for (ScriptLanguage *E : langs_to_init) {
319
E->init();
320
}
321
322
{
323
MutexLock lock(languages_mutex);
324
languages_ready = true;
325
}
326
}
327
328
void ScriptServer::finish_languages() {
329
HashSet<ScriptLanguage *> langs_to_finish;
330
331
{
332
MutexLock lock(languages_mutex);
333
for (int i = 0; i < _language_count; i++) {
334
if (_languages[i]) {
335
langs_to_finish.insert(_languages[i]);
336
}
337
}
338
}
339
340
for (ScriptLanguage *E : langs_to_finish) {
341
if (CoreBind::OS::get_singleton()) {
342
CoreBind::OS::get_singleton()->remove_script_loggers(E); // Unregister loggers using this script language.
343
}
344
E->finish();
345
}
346
347
{
348
MutexLock lock(languages_mutex);
349
languages_ready = false;
350
}
351
352
global_classes_clear();
353
}
354
355
bool ScriptServer::are_languages_initialized() {
356
MutexLock lock(languages_mutex);
357
return languages_ready;
358
}
359
360
bool ScriptServer::thread_is_entered() {
361
return thread_entered;
362
}
363
364
void ScriptServer::set_reload_scripts_on_save(bool p_enable) {
365
reload_scripts_on_save = p_enable;
366
}
367
368
bool ScriptServer::is_reload_scripts_on_save_enabled() {
369
return reload_scripts_on_save;
370
}
371
372
void ScriptServer::thread_enter() {
373
if (thread_entered) {
374
return;
375
}
376
377
MutexLock lock(languages_mutex);
378
if (!languages_ready) {
379
return;
380
}
381
for (int i = 0; i < _language_count; i++) {
382
_languages[i]->thread_enter();
383
}
384
385
thread_entered = true;
386
}
387
388
void ScriptServer::thread_exit() {
389
if (!thread_entered) {
390
return;
391
}
392
393
MutexLock lock(languages_mutex);
394
if (!languages_ready) {
395
return;
396
}
397
for (int i = 0; i < _language_count; i++) {
398
_languages[i]->thread_exit();
399
}
400
401
thread_entered = false;
402
}
403
404
HashMap<StringName, ScriptServer::GlobalScriptClass> ScriptServer::global_classes;
405
HashMap<StringName, Vector<StringName>> ScriptServer::inheriters_cache;
406
bool ScriptServer::inheriters_cache_dirty = true;
407
408
void ScriptServer::global_classes_clear() {
409
global_classes.clear();
410
inheriters_cache.clear();
411
}
412
413
void ScriptServer::add_global_class(const StringName &p_class, const StringName &p_base, const StringName &p_language, const String &p_path, bool p_is_abstract, bool p_is_tool) {
414
ERR_FAIL_COND_MSG(p_class == p_base || (global_classes.has(p_base) && get_global_class_native_base(p_base) == p_class), "Cyclic inheritance in script class.");
415
GlobalScriptClass *existing = global_classes.getptr(p_class);
416
if (existing) {
417
// Update an existing class (only set dirty if something changed).
418
if (existing->base != p_base || existing->path != p_path || existing->language != p_language) {
419
existing->base = p_base;
420
existing->path = p_path;
421
existing->language = p_language;
422
existing->is_abstract = p_is_abstract;
423
existing->is_tool = p_is_tool;
424
inheriters_cache_dirty = true;
425
}
426
} else {
427
// Add new class.
428
GlobalScriptClass g;
429
g.language = p_language;
430
g.path = p_path;
431
g.base = p_base;
432
g.is_abstract = p_is_abstract;
433
g.is_tool = p_is_tool;
434
global_classes[p_class] = g;
435
inheriters_cache_dirty = true;
436
}
437
}
438
439
void ScriptServer::remove_global_class(const StringName &p_class) {
440
global_classes.erase(p_class);
441
inheriters_cache_dirty = true;
442
}
443
444
void ScriptServer::get_inheriters_list(const StringName &p_base_type, List<StringName> *r_classes) {
445
if (inheriters_cache_dirty) {
446
inheriters_cache.clear();
447
for (const KeyValue<StringName, GlobalScriptClass> &K : global_classes) {
448
if (!inheriters_cache.has(K.value.base)) {
449
inheriters_cache[K.value.base] = Vector<StringName>();
450
}
451
inheriters_cache[K.value.base].push_back(K.key);
452
}
453
for (KeyValue<StringName, Vector<StringName>> &K : inheriters_cache) {
454
K.value.sort_custom<StringName::AlphCompare>();
455
}
456
inheriters_cache_dirty = false;
457
}
458
459
if (!inheriters_cache.has(p_base_type)) {
460
return;
461
}
462
463
const Vector<StringName> &v = inheriters_cache[p_base_type];
464
for (int i = 0; i < v.size(); i++) {
465
r_classes->push_back(v[i]);
466
}
467
}
468
469
void ScriptServer::get_indirect_inheriters_list(const StringName &p_base_type, List<StringName> *r_classes) {
470
List<StringName> direct_inheritors;
471
get_inheriters_list(p_base_type, &direct_inheritors);
472
for (const StringName &inheritor : direct_inheritors) {
473
r_classes->push_back(inheritor);
474
get_indirect_inheriters_list(inheritor, r_classes);
475
}
476
}
477
478
void ScriptServer::remove_global_class_by_path(const String &p_path) {
479
for (const KeyValue<StringName, GlobalScriptClass> &kv : global_classes) {
480
if (kv.value.path == p_path) {
481
global_classes.erase(kv.key);
482
inheriters_cache_dirty = true;
483
return;
484
}
485
}
486
}
487
488
bool ScriptServer::is_global_class(const StringName &p_class) {
489
return global_classes.has(p_class);
490
}
491
492
StringName ScriptServer::get_global_class_language(const StringName &p_class) {
493
ERR_FAIL_COND_V(!global_classes.has(p_class), StringName());
494
return global_classes[p_class].language;
495
}
496
497
String ScriptServer::get_global_class_path(const String &p_class) {
498
ERR_FAIL_COND_V(!global_classes.has(p_class), String());
499
return global_classes[p_class].path;
500
}
501
502
StringName ScriptServer::get_global_class_base(const String &p_class) {
503
ERR_FAIL_COND_V(!global_classes.has(p_class), String());
504
return global_classes[p_class].base;
505
}
506
507
StringName ScriptServer::get_global_class_native_base(const String &p_class) {
508
ERR_FAIL_COND_V(!global_classes.has(p_class), String());
509
String base = global_classes[p_class].base;
510
while (global_classes.has(base)) {
511
base = global_classes[base].base;
512
}
513
return base;
514
}
515
516
bool ScriptServer::is_global_class_abstract(const String &p_class) {
517
ERR_FAIL_COND_V(!global_classes.has(p_class), false);
518
return global_classes[p_class].is_abstract;
519
}
520
521
bool ScriptServer::is_global_class_tool(const String &p_class) {
522
ERR_FAIL_COND_V(!global_classes.has(p_class), false);
523
return global_classes[p_class].is_tool;
524
}
525
526
// This function only sorts items added by this function.
527
// If `r_global_classes` is not empty before calling and a global sort is needed, caller must handle that separately.
528
void ScriptServer::get_global_class_list(LocalVector<StringName> &r_global_classes) {
529
if (global_classes.is_empty()) {
530
return;
531
}
532
r_global_classes.reserve(r_global_classes.size() + global_classes.size());
533
for (const KeyValue<StringName, GlobalScriptClass> &global_class : global_classes) {
534
r_global_classes.push_back(global_class.key);
535
}
536
SortArray<StringName, StringName::AlphCompare> sorter;
537
sorter.sort(&r_global_classes[r_global_classes.size() - global_classes.size()], global_classes.size());
538
}
539
540
void ScriptServer::save_global_classes() {
541
Dictionary class_icons;
542
543
Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
544
for (const Variant &script_class : script_classes) {
545
Dictionary d = script_class;
546
if (!d.has("name") || !d.has("icon")) {
547
continue;
548
}
549
class_icons[d["name"]] = d["icon"];
550
}
551
552
LocalVector<StringName> gc;
553
get_global_class_list(gc);
554
Array gcarr;
555
for (const StringName &class_name : gc) {
556
const GlobalScriptClass &global_class = global_classes[class_name];
557
Dictionary d;
558
d["class"] = class_name;
559
d["language"] = global_class.language;
560
d["path"] = global_class.path;
561
d["base"] = global_class.base;
562
d["icon"] = class_icons.get(class_name, "");
563
d["is_abstract"] = global_class.is_abstract;
564
d["is_tool"] = global_class.is_tool;
565
gcarr.push_back(d);
566
}
567
ProjectSettings::get_singleton()->store_global_class_list(gcarr);
568
}
569
570
Vector<Ref<ScriptBacktrace>> ScriptServer::capture_script_backtraces(bool p_include_variables) {
571
if (is_program_exiting) {
572
return Vector<Ref<ScriptBacktrace>>();
573
}
574
575
MutexLock lock(languages_mutex);
576
if (!languages_ready) {
577
return Vector<Ref<ScriptBacktrace>>();
578
}
579
580
Vector<Ref<ScriptBacktrace>> result;
581
result.resize(_language_count);
582
for (int i = 0; i < _language_count; i++) {
583
result.write[i].instantiate(_languages[i], p_include_variables);
584
}
585
586
return result;
587
}
588
589
////////////////////
590
591
void ScriptLanguage::get_core_type_words(List<String> *p_core_type_words) const {
592
p_core_type_words->push_back("String");
593
p_core_type_words->push_back("Vector2");
594
p_core_type_words->push_back("Vector2i");
595
p_core_type_words->push_back("Rect2");
596
p_core_type_words->push_back("Rect2i");
597
p_core_type_words->push_back("Vector3");
598
p_core_type_words->push_back("Vector3i");
599
p_core_type_words->push_back("Transform2D");
600
p_core_type_words->push_back("Vector4");
601
p_core_type_words->push_back("Vector4i");
602
p_core_type_words->push_back("Plane");
603
p_core_type_words->push_back("Quaternion");
604
p_core_type_words->push_back("AABB");
605
p_core_type_words->push_back("Basis");
606
p_core_type_words->push_back("Transform3D");
607
p_core_type_words->push_back("Projection");
608
p_core_type_words->push_back("Color");
609
p_core_type_words->push_back("StringName");
610
p_core_type_words->push_back("NodePath");
611
p_core_type_words->push_back("RID");
612
p_core_type_words->push_back("Callable");
613
p_core_type_words->push_back("Signal");
614
p_core_type_words->push_back("Dictionary");
615
p_core_type_words->push_back("Array");
616
p_core_type_words->push_back("PackedByteArray");
617
p_core_type_words->push_back("PackedInt32Array");
618
p_core_type_words->push_back("PackedInt64Array");
619
p_core_type_words->push_back("PackedFloat32Array");
620
p_core_type_words->push_back("PackedFloat64Array");
621
p_core_type_words->push_back("PackedStringArray");
622
p_core_type_words->push_back("PackedVector2Array");
623
p_core_type_words->push_back("PackedVector3Array");
624
p_core_type_words->push_back("PackedColorArray");
625
p_core_type_words->push_back("PackedVector4Array");
626
}
627
628
void ScriptLanguage::frame() {
629
}
630
631
TypedArray<int> ScriptLanguage::CodeCompletionOption::get_option_characteristics(const String &p_base) {
632
// Return characteristics of the match found by order of importance.
633
// Matches will be ranked by a lexicographical order on the vector returned by this function.
634
// The lower values indicate better matches and that they should go before in the order of appearance.
635
if (!matches_dirty) {
636
return charac;
637
}
638
charac.clear();
639
// Ensure base is not empty and at the same time that matches is not empty too.
640
if (p_base.length() == 0) {
641
matches_dirty = false;
642
charac.push_back(location);
643
return charac;
644
}
645
charac.push_back(matches.size());
646
charac.push_back((matches[0].first == 0) ? 0 : 1);
647
const char32_t *target_char = &p_base[0];
648
int bad_case = 0;
649
for (const Pair<int, int> &match_segment : matches) {
650
const char32_t *string_to_complete_char = &display[match_segment.first];
651
for (int j = 0; j < match_segment.second; j++, string_to_complete_char++, target_char++) {
652
if (*string_to_complete_char != *target_char) {
653
bad_case++;
654
}
655
}
656
}
657
charac.push_back(bad_case);
658
charac.push_back(location);
659
charac.push_back(matches[0].first);
660
matches_dirty = false;
661
return charac;
662
}
663
664
void ScriptLanguage::CodeCompletionOption::clear_characteristics() {
665
charac = TypedArray<int>();
666
}
667
668
TypedArray<int> ScriptLanguage::CodeCompletionOption::get_option_cached_characteristics() const {
669
// Only returns the cached value and warns if it was not updated since the last change of matches.
670
if (matches_dirty) {
671
WARN_PRINT("Characteristics are not up to date.");
672
}
673
674
return charac;
675
}
676
677
void ScriptLanguage::_bind_methods() {
678
BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_AUTO);
679
BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_PASCAL_CASE);
680
BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_SNAKE_CASE);
681
BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_KEBAB_CASE);
682
BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_CAMEL_CASE);
683
}
684
685
bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_value) {
686
if (script->is_placeholder_fallback_enabled()) {
687
return false;
688
}
689
690
if (values.has(p_name)) {
691
Variant defval;
692
if (script->get_property_default_value(p_name, defval)) {
693
// The evaluate function ensures that a NIL variant is equal to e.g. an empty Resource.
694
// Simply doing defval == p_value does not do this.
695
if (Variant::evaluate(Variant::OP_EQUAL, defval, p_value)) {
696
values.erase(p_name);
697
return true;
698
}
699
}
700
values[p_name] = p_value;
701
return true;
702
} else {
703
Variant defval;
704
if (script->get_property_default_value(p_name, defval)) {
705
if (Variant::evaluate(Variant::OP_NOT_EQUAL, defval, p_value)) {
706
values[p_name] = p_value;
707
}
708
return true;
709
}
710
}
711
return false;
712
}
713
714
bool PlaceHolderScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
715
if (values.has(p_name)) {
716
r_ret = values[p_name];
717
return true;
718
}
719
720
if (constants.has(p_name)) {
721
r_ret = constants[p_name];
722
return true;
723
}
724
725
if (!script->is_placeholder_fallback_enabled()) {
726
Variant defval;
727
if (script->get_property_default_value(p_name, defval)) {
728
r_ret = defval;
729
return true;
730
}
731
}
732
733
return false;
734
}
735
736
void PlaceHolderScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
737
if (script->is_placeholder_fallback_enabled()) {
738
for (const PropertyInfo &E : properties) {
739
p_properties->push_back(E);
740
}
741
} else {
742
for (const PropertyInfo &E : properties) {
743
PropertyInfo pinfo = E;
744
p_properties->push_back(E);
745
}
746
}
747
}
748
749
Variant::Type PlaceHolderScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
750
if (values.has(p_name)) {
751
if (r_is_valid) {
752
*r_is_valid = true;
753
}
754
return values[p_name].get_type();
755
}
756
757
if (constants.has(p_name)) {
758
if (r_is_valid) {
759
*r_is_valid = true;
760
}
761
return constants[p_name].get_type();
762
}
763
764
if (r_is_valid) {
765
*r_is_valid = false;
766
}
767
768
return Variant::NIL;
769
}
770
771
void PlaceHolderScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
772
if (script->is_placeholder_fallback_enabled()) {
773
return;
774
}
775
776
if (script.is_valid()) {
777
script->get_script_method_list(p_list);
778
}
779
}
780
781
bool PlaceHolderScriptInstance::has_method(const StringName &p_method) const {
782
if (script->is_placeholder_fallback_enabled()) {
783
return false;
784
}
785
786
if (script.is_valid()) {
787
Ref<Script> scr = script;
788
while (scr.is_valid()) {
789
if (scr->has_method(p_method)) {
790
return true;
791
}
792
scr = scr->get_base_script();
793
}
794
}
795
return false;
796
}
797
798
Variant PlaceHolderScriptInstance::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
799
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
800
#if TOOLS_ENABLED
801
if (Engine::get_singleton()->is_editor_hint()) {
802
return String("Attempt to call a method on a placeholder instance. Check if the script is in tool mode.");
803
} else {
804
return String("Attempt to call a method on a placeholder instance. Probably a bug, please report.");
805
}
806
#else
807
return Variant();
808
#endif // TOOLS_ENABLED
809
}
810
811
void PlaceHolderScriptInstance::update(const List<PropertyInfo> &p_properties, const HashMap<StringName, Variant> &p_values) {
812
HashSet<StringName> new_values;
813
for (const PropertyInfo &E : p_properties) {
814
if (E.usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_CATEGORY)) {
815
continue;
816
}
817
818
StringName n = E.name;
819
new_values.insert(n);
820
821
if (!values.has(n) || (E.type != Variant::NIL && values[n].get_type() != E.type)) {
822
if (p_values.has(n)) {
823
values[n] = p_values[n];
824
}
825
}
826
}
827
828
properties = p_properties;
829
List<StringName> to_remove;
830
831
for (KeyValue<StringName, Variant> &E : values) {
832
if (!new_values.has(E.key)) {
833
to_remove.push_back(E.key);
834
}
835
836
Variant defval;
837
if (script->get_property_default_value(E.key, defval)) {
838
//remove because it's the same as the default value
839
if (defval == E.value) {
840
to_remove.push_back(E.key);
841
}
842
}
843
}
844
845
while (to_remove.size()) {
846
values.erase(to_remove.front()->get());
847
to_remove.pop_front();
848
}
849
850
if (owner && owner->get_script_instance() == this) {
851
owner->notify_property_list_changed();
852
}
853
//change notify
854
855
constants.clear();
856
script->get_constants(&constants);
857
}
858
859
void PlaceHolderScriptInstance::property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid) {
860
if (script->is_placeholder_fallback_enabled()) {
861
HashMap<StringName, Variant>::Iterator E = values.find(p_name);
862
863
if (E) {
864
E->value = p_value;
865
} else {
866
values.insert(p_name, p_value);
867
}
868
869
bool found = false;
870
for (const PropertyInfo &F : properties) {
871
if (F.name == p_name) {
872
found = true;
873
break;
874
}
875
}
876
if (!found) {
877
PropertyHint hint = PROPERTY_HINT_NONE;
878
const Object *obj = p_value.get_validated_object();
879
if (obj && obj->is_class("Node")) {
880
hint = PROPERTY_HINT_NODE_TYPE;
881
}
882
properties.push_back(PropertyInfo(p_value.get_type(), p_name, hint, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE));
883
}
884
}
885
886
if (r_valid) {
887
*r_valid = false; // Cannot change the value in either case
888
}
889
}
890
891
Variant PlaceHolderScriptInstance::property_get_fallback(const StringName &p_name, bool *r_valid) {
892
if (script->is_placeholder_fallback_enabled()) {
893
HashMap<StringName, Variant>::ConstIterator E = values.find(p_name);
894
895
if (E) {
896
if (r_valid) {
897
*r_valid = true;
898
}
899
return E->value;
900
}
901
902
E = constants.find(p_name);
903
if (E) {
904
if (r_valid) {
905
*r_valid = true;
906
}
907
return E->value;
908
}
909
}
910
911
if (r_valid) {
912
*r_valid = false;
913
}
914
915
return Variant();
916
}
917
918
PlaceHolderScriptInstance::PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script, Object *p_owner) :
919
owner(p_owner),
920
language(p_language),
921
script(p_script) {
922
}
923
924
PlaceHolderScriptInstance::~PlaceHolderScriptInstance() {
925
if (script.is_valid()) {
926
script->_placeholder_erased(this);
927
}
928
}
929
930