Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/object/script_language.cpp
9903 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
39
ScriptLanguage *ScriptServer::_languages[MAX_LANGUAGES];
40
int ScriptServer::_language_count = 0;
41
bool ScriptServer::languages_ready = false;
42
Mutex ScriptServer::languages_mutex;
43
thread_local bool ScriptServer::thread_entered = false;
44
45
bool ScriptServer::scripting_enabled = true;
46
bool ScriptServer::reload_scripts_on_save = false;
47
ScriptEditRequestFunction ScriptServer::edit_request_func = nullptr;
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
return OK;
256
}
257
258
Error ScriptServer::unregister_language(const ScriptLanguage *p_language) {
259
MutexLock lock(languages_mutex);
260
261
for (int i = 0; i < _language_count; i++) {
262
if (_languages[i] == p_language) {
263
_language_count--;
264
if (i < _language_count) {
265
SWAP(_languages[i], _languages[_language_count]);
266
}
267
return OK;
268
}
269
}
270
return ERR_DOES_NOT_EXIST;
271
}
272
273
void ScriptServer::init_languages() {
274
{ // Load global classes.
275
global_classes_clear();
276
#ifndef DISABLE_DEPRECATED
277
if (ProjectSettings::get_singleton()->has_setting("_global_script_classes")) {
278
Array script_classes = GLOBAL_GET("_global_script_classes");
279
280
for (const Variant &script_class : script_classes) {
281
Dictionary c = script_class;
282
if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base") || !c.has("is_abstract") || !c.has("is_tool")) {
283
continue;
284
}
285
add_global_class(c["class"], c["base"], c["language"], c["path"], c["is_abstract"], c["is_tool"]);
286
}
287
ProjectSettings::get_singleton()->clear("_global_script_classes");
288
}
289
#endif
290
291
Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
292
for (const Variant &script_class : script_classes) {
293
Dictionary c = script_class;
294
if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base") || !c.has("is_abstract") || !c.has("is_tool")) {
295
continue;
296
}
297
add_global_class(c["class"], c["base"], c["language"], c["path"], c["is_abstract"], c["is_tool"]);
298
}
299
}
300
301
HashSet<ScriptLanguage *> langs_to_init;
302
{
303
MutexLock lock(languages_mutex);
304
for (int i = 0; i < _language_count; i++) {
305
if (_languages[i]) {
306
langs_to_init.insert(_languages[i]);
307
}
308
}
309
}
310
311
for (ScriptLanguage *E : langs_to_init) {
312
E->init();
313
}
314
315
{
316
MutexLock lock(languages_mutex);
317
languages_ready = true;
318
}
319
}
320
321
void ScriptServer::finish_languages() {
322
HashSet<ScriptLanguage *> langs_to_finish;
323
324
{
325
MutexLock lock(languages_mutex);
326
for (int i = 0; i < _language_count; i++) {
327
if (_languages[i]) {
328
langs_to_finish.insert(_languages[i]);
329
}
330
}
331
}
332
333
for (ScriptLanguage *E : langs_to_finish) {
334
if (CoreBind::OS::get_singleton()) {
335
CoreBind::OS::get_singleton()->remove_script_loggers(E); // Unregister loggers using this script language.
336
}
337
E->finish();
338
}
339
340
{
341
MutexLock lock(languages_mutex);
342
languages_ready = false;
343
}
344
345
global_classes_clear();
346
}
347
348
bool ScriptServer::are_languages_initialized() {
349
MutexLock lock(languages_mutex);
350
return languages_ready;
351
}
352
353
bool ScriptServer::thread_is_entered() {
354
return thread_entered;
355
}
356
357
void ScriptServer::set_reload_scripts_on_save(bool p_enable) {
358
reload_scripts_on_save = p_enable;
359
}
360
361
bool ScriptServer::is_reload_scripts_on_save_enabled() {
362
return reload_scripts_on_save;
363
}
364
365
void ScriptServer::thread_enter() {
366
if (thread_entered) {
367
return;
368
}
369
370
MutexLock lock(languages_mutex);
371
if (!languages_ready) {
372
return;
373
}
374
for (int i = 0; i < _language_count; i++) {
375
_languages[i]->thread_enter();
376
}
377
378
thread_entered = true;
379
}
380
381
void ScriptServer::thread_exit() {
382
if (!thread_entered) {
383
return;
384
}
385
386
MutexLock lock(languages_mutex);
387
if (!languages_ready) {
388
return;
389
}
390
for (int i = 0; i < _language_count; i++) {
391
_languages[i]->thread_exit();
392
}
393
394
thread_entered = false;
395
}
396
397
HashMap<StringName, ScriptServer::GlobalScriptClass> ScriptServer::global_classes;
398
HashMap<StringName, Vector<StringName>> ScriptServer::inheriters_cache;
399
bool ScriptServer::inheriters_cache_dirty = true;
400
401
void ScriptServer::global_classes_clear() {
402
global_classes.clear();
403
inheriters_cache.clear();
404
}
405
406
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) {
407
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.");
408
GlobalScriptClass *existing = global_classes.getptr(p_class);
409
if (existing) {
410
// Update an existing class (only set dirty if something changed).
411
if (existing->base != p_base || existing->path != p_path || existing->language != p_language) {
412
existing->base = p_base;
413
existing->path = p_path;
414
existing->language = p_language;
415
existing->is_abstract = p_is_abstract;
416
existing->is_tool = p_is_tool;
417
inheriters_cache_dirty = true;
418
}
419
} else {
420
// Add new class.
421
GlobalScriptClass g;
422
g.language = p_language;
423
g.path = p_path;
424
g.base = p_base;
425
g.is_abstract = p_is_abstract;
426
g.is_tool = p_is_tool;
427
global_classes[p_class] = g;
428
inheriters_cache_dirty = true;
429
}
430
}
431
432
void ScriptServer::remove_global_class(const StringName &p_class) {
433
global_classes.erase(p_class);
434
inheriters_cache_dirty = true;
435
}
436
437
void ScriptServer::get_inheriters_list(const StringName &p_base_type, List<StringName> *r_classes) {
438
if (inheriters_cache_dirty) {
439
inheriters_cache.clear();
440
for (const KeyValue<StringName, GlobalScriptClass> &K : global_classes) {
441
if (!inheriters_cache.has(K.value.base)) {
442
inheriters_cache[K.value.base] = Vector<StringName>();
443
}
444
inheriters_cache[K.value.base].push_back(K.key);
445
}
446
for (KeyValue<StringName, Vector<StringName>> &K : inheriters_cache) {
447
K.value.sort_custom<StringName::AlphCompare>();
448
}
449
inheriters_cache_dirty = false;
450
}
451
452
if (!inheriters_cache.has(p_base_type)) {
453
return;
454
}
455
456
const Vector<StringName> &v = inheriters_cache[p_base_type];
457
for (int i = 0; i < v.size(); i++) {
458
r_classes->push_back(v[i]);
459
}
460
}
461
462
void ScriptServer::remove_global_class_by_path(const String &p_path) {
463
for (const KeyValue<StringName, GlobalScriptClass> &kv : global_classes) {
464
if (kv.value.path == p_path) {
465
global_classes.erase(kv.key);
466
inheriters_cache_dirty = true;
467
return;
468
}
469
}
470
}
471
472
bool ScriptServer::is_global_class(const StringName &p_class) {
473
return global_classes.has(p_class);
474
}
475
476
StringName ScriptServer::get_global_class_language(const StringName &p_class) {
477
ERR_FAIL_COND_V(!global_classes.has(p_class), StringName());
478
return global_classes[p_class].language;
479
}
480
481
String ScriptServer::get_global_class_path(const String &p_class) {
482
ERR_FAIL_COND_V(!global_classes.has(p_class), String());
483
return global_classes[p_class].path;
484
}
485
486
StringName ScriptServer::get_global_class_base(const String &p_class) {
487
ERR_FAIL_COND_V(!global_classes.has(p_class), String());
488
return global_classes[p_class].base;
489
}
490
491
StringName ScriptServer::get_global_class_native_base(const String &p_class) {
492
ERR_FAIL_COND_V(!global_classes.has(p_class), String());
493
String base = global_classes[p_class].base;
494
while (global_classes.has(base)) {
495
base = global_classes[base].base;
496
}
497
return base;
498
}
499
500
bool ScriptServer::is_global_class_abstract(const String &p_class) {
501
ERR_FAIL_COND_V(!global_classes.has(p_class), false);
502
return global_classes[p_class].is_abstract;
503
}
504
505
bool ScriptServer::is_global_class_tool(const String &p_class) {
506
ERR_FAIL_COND_V(!global_classes.has(p_class), false);
507
return global_classes[p_class].is_tool;
508
}
509
510
void ScriptServer::get_global_class_list(List<StringName> *r_global_classes) {
511
List<StringName> classes;
512
for (const KeyValue<StringName, GlobalScriptClass> &E : global_classes) {
513
classes.push_back(E.key);
514
}
515
classes.sort_custom<StringName::AlphCompare>();
516
for (const StringName &E : classes) {
517
r_global_classes->push_back(E);
518
}
519
}
520
521
void ScriptServer::save_global_classes() {
522
Dictionary class_icons;
523
524
Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
525
for (const Variant &script_class : script_classes) {
526
Dictionary d = script_class;
527
if (!d.has("name") || !d.has("icon")) {
528
continue;
529
}
530
class_icons[d["name"]] = d["icon"];
531
}
532
533
List<StringName> gc;
534
get_global_class_list(&gc);
535
Array gcarr;
536
for (const StringName &E : gc) {
537
const GlobalScriptClass &global_class = global_classes[E];
538
Dictionary d;
539
d["class"] = E;
540
d["language"] = global_class.language;
541
d["path"] = global_class.path;
542
d["base"] = global_class.base;
543
d["icon"] = class_icons.get(E, "");
544
d["is_abstract"] = global_class.is_abstract;
545
d["is_tool"] = global_class.is_tool;
546
gcarr.push_back(d);
547
}
548
ProjectSettings::get_singleton()->store_global_class_list(gcarr);
549
}
550
551
Vector<Ref<ScriptBacktrace>> ScriptServer::capture_script_backtraces(bool p_include_variables) {
552
if (is_program_exiting) {
553
return Vector<Ref<ScriptBacktrace>>();
554
}
555
556
MutexLock lock(languages_mutex);
557
if (!languages_ready) {
558
return Vector<Ref<ScriptBacktrace>>();
559
}
560
561
Vector<Ref<ScriptBacktrace>> result;
562
result.resize(_language_count);
563
for (int i = 0; i < _language_count; i++) {
564
result.write[i].instantiate(_languages[i], p_include_variables);
565
}
566
567
return result;
568
}
569
570
////////////////////
571
572
void ScriptLanguage::get_core_type_words(List<String> *p_core_type_words) const {
573
p_core_type_words->push_back("String");
574
p_core_type_words->push_back("Vector2");
575
p_core_type_words->push_back("Vector2i");
576
p_core_type_words->push_back("Rect2");
577
p_core_type_words->push_back("Rect2i");
578
p_core_type_words->push_back("Vector3");
579
p_core_type_words->push_back("Vector3i");
580
p_core_type_words->push_back("Transform2D");
581
p_core_type_words->push_back("Vector4");
582
p_core_type_words->push_back("Vector4i");
583
p_core_type_words->push_back("Plane");
584
p_core_type_words->push_back("Quaternion");
585
p_core_type_words->push_back("AABB");
586
p_core_type_words->push_back("Basis");
587
p_core_type_words->push_back("Transform3D");
588
p_core_type_words->push_back("Projection");
589
p_core_type_words->push_back("Color");
590
p_core_type_words->push_back("StringName");
591
p_core_type_words->push_back("NodePath");
592
p_core_type_words->push_back("RID");
593
p_core_type_words->push_back("Callable");
594
p_core_type_words->push_back("Signal");
595
p_core_type_words->push_back("Dictionary");
596
p_core_type_words->push_back("Array");
597
p_core_type_words->push_back("PackedByteArray");
598
p_core_type_words->push_back("PackedInt32Array");
599
p_core_type_words->push_back("PackedInt64Array");
600
p_core_type_words->push_back("PackedFloat32Array");
601
p_core_type_words->push_back("PackedFloat64Array");
602
p_core_type_words->push_back("PackedStringArray");
603
p_core_type_words->push_back("PackedVector2Array");
604
p_core_type_words->push_back("PackedVector3Array");
605
p_core_type_words->push_back("PackedColorArray");
606
p_core_type_words->push_back("PackedVector4Array");
607
}
608
609
void ScriptLanguage::frame() {
610
}
611
612
TypedArray<int> ScriptLanguage::CodeCompletionOption::get_option_characteristics(const String &p_base) {
613
// Return characteristics of the match found by order of importance.
614
// Matches will be ranked by a lexicographical order on the vector returned by this function.
615
// The lower values indicate better matches and that they should go before in the order of appearance.
616
if (last_matches == matches) {
617
return charac;
618
}
619
charac.clear();
620
// Ensure base is not empty and at the same time that matches is not empty too.
621
if (p_base.length() == 0) {
622
last_matches = matches;
623
charac.push_back(location);
624
return charac;
625
}
626
charac.push_back(matches.size());
627
charac.push_back((matches[0].first == 0) ? 0 : 1);
628
const char32_t *target_char = &p_base[0];
629
int bad_case = 0;
630
for (const Pair<int, int> &match_segment : matches) {
631
const char32_t *string_to_complete_char = &display[match_segment.first];
632
for (int j = 0; j < match_segment.second; j++, string_to_complete_char++, target_char++) {
633
if (*string_to_complete_char != *target_char) {
634
bad_case++;
635
}
636
}
637
}
638
charac.push_back(bad_case);
639
charac.push_back(location);
640
charac.push_back(matches[0].first);
641
last_matches = matches;
642
return charac;
643
}
644
645
void ScriptLanguage::CodeCompletionOption::clear_characteristics() {
646
charac = TypedArray<int>();
647
}
648
649
TypedArray<int> ScriptLanguage::CodeCompletionOption::get_option_cached_characteristics() const {
650
// Only returns the cached value and warns if it was not updated since the last change of matches.
651
if (last_matches != matches) {
652
WARN_PRINT("Characteristics are not up to date.");
653
}
654
655
return charac;
656
}
657
658
void ScriptLanguage::_bind_methods() {
659
BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_AUTO);
660
BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_PASCAL_CASE);
661
BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_SNAKE_CASE);
662
BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_KEBAB_CASE);
663
BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_CAMEL_CASE);
664
}
665
666
bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_value) {
667
if (script->is_placeholder_fallback_enabled()) {
668
return false;
669
}
670
671
if (values.has(p_name)) {
672
Variant defval;
673
if (script->get_property_default_value(p_name, defval)) {
674
// The evaluate function ensures that a NIL variant is equal to e.g. an empty Resource.
675
// Simply doing defval == p_value does not do this.
676
if (Variant::evaluate(Variant::OP_EQUAL, defval, p_value)) {
677
values.erase(p_name);
678
return true;
679
}
680
}
681
values[p_name] = p_value;
682
return true;
683
} else {
684
Variant defval;
685
if (script->get_property_default_value(p_name, defval)) {
686
if (Variant::evaluate(Variant::OP_NOT_EQUAL, defval, p_value)) {
687
values[p_name] = p_value;
688
}
689
return true;
690
}
691
}
692
return false;
693
}
694
695
bool PlaceHolderScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
696
if (values.has(p_name)) {
697
r_ret = values[p_name];
698
return true;
699
}
700
701
if (constants.has(p_name)) {
702
r_ret = constants[p_name];
703
return true;
704
}
705
706
if (!script->is_placeholder_fallback_enabled()) {
707
Variant defval;
708
if (script->get_property_default_value(p_name, defval)) {
709
r_ret = defval;
710
return true;
711
}
712
}
713
714
return false;
715
}
716
717
void PlaceHolderScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
718
if (script->is_placeholder_fallback_enabled()) {
719
for (const PropertyInfo &E : properties) {
720
p_properties->push_back(E);
721
}
722
} else {
723
for (const PropertyInfo &E : properties) {
724
PropertyInfo pinfo = E;
725
p_properties->push_back(E);
726
}
727
}
728
}
729
730
Variant::Type PlaceHolderScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
731
if (values.has(p_name)) {
732
if (r_is_valid) {
733
*r_is_valid = true;
734
}
735
return values[p_name].get_type();
736
}
737
738
if (constants.has(p_name)) {
739
if (r_is_valid) {
740
*r_is_valid = true;
741
}
742
return constants[p_name].get_type();
743
}
744
745
if (r_is_valid) {
746
*r_is_valid = false;
747
}
748
749
return Variant::NIL;
750
}
751
752
void PlaceHolderScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
753
if (script->is_placeholder_fallback_enabled()) {
754
return;
755
}
756
757
if (script.is_valid()) {
758
script->get_script_method_list(p_list);
759
}
760
}
761
762
bool PlaceHolderScriptInstance::has_method(const StringName &p_method) const {
763
if (script->is_placeholder_fallback_enabled()) {
764
return false;
765
}
766
767
if (script.is_valid()) {
768
Ref<Script> scr = script;
769
while (scr.is_valid()) {
770
if (scr->has_method(p_method)) {
771
return true;
772
}
773
scr = scr->get_base_script();
774
}
775
}
776
return false;
777
}
778
779
Variant PlaceHolderScriptInstance::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
780
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
781
#if TOOLS_ENABLED
782
if (Engine::get_singleton()->is_editor_hint()) {
783
return String("Attempt to call a method on a placeholder instance. Check if the script is in tool mode.");
784
} else {
785
return String("Attempt to call a method on a placeholder instance. Probably a bug, please report.");
786
}
787
#else
788
return Variant();
789
#endif // TOOLS_ENABLED
790
}
791
792
void PlaceHolderScriptInstance::update(const List<PropertyInfo> &p_properties, const HashMap<StringName, Variant> &p_values) {
793
HashSet<StringName> new_values;
794
for (const PropertyInfo &E : p_properties) {
795
if (E.usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_CATEGORY)) {
796
continue;
797
}
798
799
StringName n = E.name;
800
new_values.insert(n);
801
802
if (!values.has(n) || (E.type != Variant::NIL && values[n].get_type() != E.type)) {
803
if (p_values.has(n)) {
804
values[n] = p_values[n];
805
}
806
}
807
}
808
809
properties = p_properties;
810
List<StringName> to_remove;
811
812
for (KeyValue<StringName, Variant> &E : values) {
813
if (!new_values.has(E.key)) {
814
to_remove.push_back(E.key);
815
}
816
817
Variant defval;
818
if (script->get_property_default_value(E.key, defval)) {
819
//remove because it's the same as the default value
820
if (defval == E.value) {
821
to_remove.push_back(E.key);
822
}
823
}
824
}
825
826
while (to_remove.size()) {
827
values.erase(to_remove.front()->get());
828
to_remove.pop_front();
829
}
830
831
if (owner && owner->get_script_instance() == this) {
832
owner->notify_property_list_changed();
833
}
834
//change notify
835
836
constants.clear();
837
script->get_constants(&constants);
838
}
839
840
void PlaceHolderScriptInstance::property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid) {
841
if (script->is_placeholder_fallback_enabled()) {
842
HashMap<StringName, Variant>::Iterator E = values.find(p_name);
843
844
if (E) {
845
E->value = p_value;
846
} else {
847
values.insert(p_name, p_value);
848
}
849
850
bool found = false;
851
for (const PropertyInfo &F : properties) {
852
if (F.name == p_name) {
853
found = true;
854
break;
855
}
856
}
857
if (!found) {
858
PropertyHint hint = PROPERTY_HINT_NONE;
859
const Object *obj = p_value.get_validated_object();
860
if (obj && obj->is_class("Node")) {
861
hint = PROPERTY_HINT_NODE_TYPE;
862
}
863
properties.push_back(PropertyInfo(p_value.get_type(), p_name, hint, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE));
864
}
865
}
866
867
if (r_valid) {
868
*r_valid = false; // Cannot change the value in either case
869
}
870
}
871
872
Variant PlaceHolderScriptInstance::property_get_fallback(const StringName &p_name, bool *r_valid) {
873
if (script->is_placeholder_fallback_enabled()) {
874
HashMap<StringName, Variant>::ConstIterator E = values.find(p_name);
875
876
if (E) {
877
if (r_valid) {
878
*r_valid = true;
879
}
880
return E->value;
881
}
882
883
E = constants.find(p_name);
884
if (E) {
885
if (r_valid) {
886
*r_valid = true;
887
}
888
return E->value;
889
}
890
}
891
892
if (r_valid) {
893
*r_valid = false;
894
}
895
896
return Variant();
897
}
898
899
PlaceHolderScriptInstance::PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script, Object *p_owner) :
900
owner(p_owner),
901
language(p_language),
902
script(p_script) {
903
}
904
905
PlaceHolderScriptInstance::~PlaceHolderScriptInstance() {
906
if (script.is_valid()) {
907
script->_placeholder_erased(this);
908
}
909
}
910
911