Debian Codegraph @

core/object/script_language.cpp

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