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_string_names.h" |
35 | #include "core/debugger/engine_debugger.h" |
36 | #include "core/debugger/script_debugger.h" |
37 | |
38 | #include <stdint.h> |
39 | |
40 | ScriptLanguage *ScriptServer::_languages[MAX_LANGUAGES]; |
41 | int ScriptServer::_language_count = 0; |
42 | |
43 | bool ScriptServer::scripting_enabled = true; |
44 | bool ScriptServer::reload_scripts_on_save = false; |
45 | SafeFlag ScriptServer::languages_finished; // Used until GH-76581 is fixed properly. |
46 | ScriptEditRequestFunction ScriptServer::edit_request_func = nullptr; |
47 | |
48 | void Script::_notification(int p_what) { |
49 | switch (p_what) { |
50 | case NOTIFICATION_POSTINITIALIZE: { |
51 | if (EngineDebugger::is_active()) { |
52 | EngineDebugger::get_script_debugger()->set_break_language(get_language()); |
53 | } |
54 | } break; |
55 | } |
56 | } |
57 | |
58 | Variant Script::_get_property_default_value(const StringName &p_property) { |
59 | Variant ret; |
60 | get_property_default_value(p_property, ret); |
61 | return ret; |
62 | } |
63 | |
64 | TypedArray<Dictionary> Script::_get_script_property_list() { |
65 | TypedArray<Dictionary> ret; |
66 | List<PropertyInfo> list; |
67 | get_script_property_list(&list); |
68 | for (const PropertyInfo &E : list) { |
69 | ret.append(E.operator Dictionary()); |
70 | } |
71 | return ret; |
72 | } |
73 | |
74 | TypedArray<Dictionary> Script::_get_script_method_list() { |
75 | TypedArray<Dictionary> ret; |
76 | List<MethodInfo> list; |
77 | get_script_method_list(&list); |
78 | for (const MethodInfo &E : list) { |
79 | ret.append(E.operator Dictionary()); |
80 | } |
81 | return ret; |
82 | } |
83 | |
84 | TypedArray<Dictionary> Script::_get_script_signal_list() { |
85 | TypedArray<Dictionary> ret; |
86 | List<MethodInfo> list; |
87 | get_script_signal_list(&list); |
88 | for (const MethodInfo &E : list) { |
89 | ret.append(E.operator Dictionary()); |
90 | } |
91 | return ret; |
92 | } |
93 | |
94 | Dictionary Script::_get_script_constant_map() { |
95 | Dictionary ret; |
96 | HashMap<StringName, Variant> map; |
97 | get_constants(&map); |
98 | for (const KeyValue<StringName, Variant> &E : map) { |
99 | ret[E.key] = E.value; |
100 | } |
101 | return ret; |
102 | } |
103 | |
104 | #ifdef TOOLS_ENABLED |
105 | |
106 | PropertyInfo Script::get_class_category() const { |
107 | String path = get_path(); |
108 | String scr_name; |
109 | |
110 | if (is_built_in()) { |
111 | if (get_name().is_empty()) { |
112 | scr_name = TTR("Built-in script" ); |
113 | } else { |
114 | scr_name = vformat("%s (%s)" , get_name(), TTR("Built-in" )); |
115 | } |
116 | } else { |
117 | if (get_name().is_empty()) { |
118 | scr_name = path.get_file(); |
119 | } else { |
120 | scr_name = get_name(); |
121 | } |
122 | } |
123 | |
124 | return PropertyInfo(Variant::NIL, scr_name, PROPERTY_HINT_NONE, path, PROPERTY_USAGE_CATEGORY); |
125 | } |
126 | |
127 | #endif // TOOLS_ENABLED |
128 | |
129 | void Script::_bind_methods() { |
130 | ClassDB::bind_method(D_METHOD("can_instantiate" ), &Script::can_instantiate); |
131 | //ClassDB::bind_method(D_METHOD("instance_create","base_object"),&Script::instance_create); |
132 | ClassDB::bind_method(D_METHOD("instance_has" , "base_object" ), &Script::instance_has); |
133 | ClassDB::bind_method(D_METHOD("has_source_code" ), &Script::has_source_code); |
134 | ClassDB::bind_method(D_METHOD("get_source_code" ), &Script::get_source_code); |
135 | ClassDB::bind_method(D_METHOD("set_source_code" , "source" ), &Script::set_source_code); |
136 | ClassDB::bind_method(D_METHOD("reload" , "keep_state" ), &Script::reload, DEFVAL(false)); |
137 | ClassDB::bind_method(D_METHOD("get_base_script" ), &Script::get_base_script); |
138 | ClassDB::bind_method(D_METHOD("get_instance_base_type" ), &Script::get_instance_base_type); |
139 | |
140 | ClassDB::bind_method(D_METHOD("has_script_signal" , "signal_name" ), &Script::has_script_signal); |
141 | |
142 | ClassDB::bind_method(D_METHOD("get_script_property_list" ), &Script::_get_script_property_list); |
143 | ClassDB::bind_method(D_METHOD("get_script_method_list" ), &Script::_get_script_method_list); |
144 | ClassDB::bind_method(D_METHOD("get_script_signal_list" ), &Script::_get_script_signal_list); |
145 | ClassDB::bind_method(D_METHOD("get_script_constant_map" ), &Script::_get_script_constant_map); |
146 | ClassDB::bind_method(D_METHOD("get_property_default_value" , "property" ), &Script::_get_property_default_value); |
147 | |
148 | ClassDB::bind_method(D_METHOD("is_tool" ), &Script::is_tool); |
149 | |
150 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "source_code" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NONE), "set_source_code" , "get_source_code" ); |
151 | } |
152 | |
153 | void ScriptServer::set_scripting_enabled(bool p_enabled) { |
154 | scripting_enabled = p_enabled; |
155 | } |
156 | |
157 | bool ScriptServer::is_scripting_enabled() { |
158 | return scripting_enabled; |
159 | } |
160 | |
161 | ScriptLanguage *ScriptServer::get_language(int p_idx) { |
162 | ERR_FAIL_INDEX_V(p_idx, _language_count, nullptr); |
163 | |
164 | return _languages[p_idx]; |
165 | } |
166 | |
167 | Error ScriptServer::register_language(ScriptLanguage *p_language) { |
168 | ERR_FAIL_NULL_V(p_language, ERR_INVALID_PARAMETER); |
169 | ERR_FAIL_COND_V_MSG(_language_count >= MAX_LANGUAGES, ERR_UNAVAILABLE, "Script languages limit has been reach, cannot register more." ); |
170 | for (int i = 0; i < _language_count; i++) { |
171 | const ScriptLanguage *other_language = _languages[i]; |
172 | ERR_FAIL_COND_V_MSG(other_language->get_extension() == p_language->get_extension(), ERR_ALREADY_EXISTS, "A script language with extension '" + p_language->get_extension() + "' is already registered." ); |
173 | ERR_FAIL_COND_V_MSG(other_language->get_name() == p_language->get_name(), ERR_ALREADY_EXISTS, "A script language with name '" + p_language->get_name() + "' is already registered." ); |
174 | ERR_FAIL_COND_V_MSG(other_language->get_type() == p_language->get_type(), ERR_ALREADY_EXISTS, "A script language with type '" + p_language->get_type() + "' is already registered." ); |
175 | } |
176 | _languages[_language_count++] = p_language; |
177 | return OK; |
178 | } |
179 | |
180 | Error ScriptServer::unregister_language(const ScriptLanguage *p_language) { |
181 | for (int i = 0; i < _language_count; i++) { |
182 | if (_languages[i] == p_language) { |
183 | _language_count--; |
184 | if (i < _language_count) { |
185 | SWAP(_languages[i], _languages[_language_count]); |
186 | } |
187 | return OK; |
188 | } |
189 | } |
190 | return ERR_DOES_NOT_EXIST; |
191 | } |
192 | |
193 | void ScriptServer::init_languages() { |
194 | { // Load global classes. |
195 | global_classes_clear(); |
196 | #ifndef DISABLE_DEPRECATED |
197 | if (ProjectSettings::get_singleton()->has_setting("_global_script_classes" )) { |
198 | Array script_classes = GLOBAL_GET("_global_script_classes" ); |
199 | |
200 | for (int i = 0; i < script_classes.size(); i++) { |
201 | Dictionary c = script_classes[i]; |
202 | if (!c.has("class" ) || !c.has("language" ) || !c.has("path" ) || !c.has("base" )) { |
203 | continue; |
204 | } |
205 | add_global_class(c["class" ], c["base" ], c["language" ], c["path" ]); |
206 | } |
207 | ProjectSettings::get_singleton()->clear("_global_script_classes" ); |
208 | } |
209 | #endif |
210 | |
211 | Array script_classes = ProjectSettings::get_singleton()->get_global_class_list(); |
212 | for (int i = 0; i < script_classes.size(); i++) { |
213 | Dictionary c = script_classes[i]; |
214 | if (!c.has("class" ) || !c.has("language" ) || !c.has("path" ) || !c.has("base" )) { |
215 | continue; |
216 | } |
217 | add_global_class(c["class" ], c["base" ], c["language" ], c["path" ]); |
218 | } |
219 | } |
220 | |
221 | for (int i = 0; i < _language_count; i++) { |
222 | _languages[i]->init(); |
223 | } |
224 | } |
225 | |
226 | void ScriptServer::finish_languages() { |
227 | for (int i = 0; i < _language_count; i++) { |
228 | _languages[i]->finish(); |
229 | } |
230 | global_classes_clear(); |
231 | languages_finished.set(); |
232 | } |
233 | |
234 | void ScriptServer::set_reload_scripts_on_save(bool p_enable) { |
235 | reload_scripts_on_save = p_enable; |
236 | } |
237 | |
238 | bool ScriptServer::is_reload_scripts_on_save_enabled() { |
239 | return reload_scripts_on_save; |
240 | } |
241 | |
242 | void ScriptServer::thread_enter() { |
243 | if (!languages_finished.is_set()) { |
244 | return; |
245 | } |
246 | for (int i = 0; i < _language_count; i++) { |
247 | _languages[i]->thread_enter(); |
248 | } |
249 | } |
250 | |
251 | void ScriptServer::thread_exit() { |
252 | if (!languages_finished.is_set()) { |
253 | return; |
254 | } |
255 | for (int i = 0; i < _language_count; i++) { |
256 | _languages[i]->thread_exit(); |
257 | } |
258 | } |
259 | |
260 | HashMap<StringName, ScriptServer::GlobalScriptClass> ScriptServer::global_classes; |
261 | HashMap<StringName, Vector<StringName>> ScriptServer::inheriters_cache; |
262 | bool ScriptServer::inheriters_cache_dirty = true; |
263 | |
264 | void ScriptServer::global_classes_clear() { |
265 | global_classes.clear(); |
266 | inheriters_cache.clear(); |
267 | } |
268 | |
269 | void ScriptServer::add_global_class(const StringName &p_class, const StringName &p_base, const StringName &p_language, const String &p_path) { |
270 | 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." ); |
271 | GlobalScriptClass g; |
272 | g.language = p_language; |
273 | g.path = p_path; |
274 | g.base = p_base; |
275 | global_classes[p_class] = g; |
276 | inheriters_cache_dirty = true; |
277 | } |
278 | |
279 | void ScriptServer::remove_global_class(const StringName &p_class) { |
280 | global_classes.erase(p_class); |
281 | inheriters_cache_dirty = true; |
282 | } |
283 | |
284 | void ScriptServer::get_inheriters_list(const StringName &p_base_type, List<StringName> *r_classes) { |
285 | if (inheriters_cache_dirty) { |
286 | inheriters_cache.clear(); |
287 | for (const KeyValue<StringName, GlobalScriptClass> &K : global_classes) { |
288 | if (!inheriters_cache.has(K.value.base)) { |
289 | inheriters_cache[K.value.base] = Vector<StringName>(); |
290 | } |
291 | inheriters_cache[K.value.base].push_back(K.key); |
292 | } |
293 | for (KeyValue<StringName, Vector<StringName>> &K : inheriters_cache) { |
294 | K.value.sort_custom<StringName::AlphCompare>(); |
295 | } |
296 | inheriters_cache_dirty = false; |
297 | } |
298 | |
299 | if (!inheriters_cache.has(p_base_type)) { |
300 | return; |
301 | } |
302 | |
303 | const Vector<StringName> &v = inheriters_cache[p_base_type]; |
304 | for (int i = 0; i < v.size(); i++) { |
305 | r_classes->push_back(v[i]); |
306 | } |
307 | } |
308 | |
309 | void ScriptServer::remove_global_class_by_path(const String &p_path) { |
310 | for (const KeyValue<StringName, GlobalScriptClass> &kv : global_classes) { |
311 | if (kv.value.path == p_path) { |
312 | global_classes.erase(kv.key); |
313 | inheriters_cache_dirty = true; |
314 | return; |
315 | } |
316 | } |
317 | } |
318 | |
319 | bool ScriptServer::is_global_class(const StringName &p_class) { |
320 | return global_classes.has(p_class); |
321 | } |
322 | |
323 | StringName ScriptServer::get_global_class_language(const StringName &p_class) { |
324 | ERR_FAIL_COND_V(!global_classes.has(p_class), StringName()); |
325 | return global_classes[p_class].language; |
326 | } |
327 | |
328 | String ScriptServer::get_global_class_path(const String &p_class) { |
329 | ERR_FAIL_COND_V(!global_classes.has(p_class), String()); |
330 | return global_classes[p_class].path; |
331 | } |
332 | |
333 | StringName ScriptServer::get_global_class_base(const String &p_class) { |
334 | ERR_FAIL_COND_V(!global_classes.has(p_class), String()); |
335 | return global_classes[p_class].base; |
336 | } |
337 | |
338 | StringName ScriptServer::get_global_class_native_base(const String &p_class) { |
339 | ERR_FAIL_COND_V(!global_classes.has(p_class), String()); |
340 | String base = global_classes[p_class].base; |
341 | while (global_classes.has(base)) { |
342 | base = global_classes[base].base; |
343 | } |
344 | return base; |
345 | } |
346 | |
347 | void ScriptServer::get_global_class_list(List<StringName> *r_global_classes) { |
348 | List<StringName> classes; |
349 | for (const KeyValue<StringName, GlobalScriptClass> &E : global_classes) { |
350 | classes.push_back(E.key); |
351 | } |
352 | classes.sort_custom<StringName::AlphCompare>(); |
353 | for (const StringName &E : classes) { |
354 | r_global_classes->push_back(E); |
355 | } |
356 | } |
357 | |
358 | void ScriptServer::save_global_classes() { |
359 | Dictionary class_icons; |
360 | |
361 | Array script_classes = ProjectSettings::get_singleton()->get_global_class_list(); |
362 | for (int i = 0; i < script_classes.size(); i++) { |
363 | Dictionary d = script_classes[i]; |
364 | if (!d.has("name" ) || !d.has("icon" )) { |
365 | continue; |
366 | } |
367 | class_icons[d["name" ]] = d["icon" ]; |
368 | } |
369 | |
370 | List<StringName> gc; |
371 | get_global_class_list(&gc); |
372 | Array gcarr; |
373 | for (const StringName &E : gc) { |
374 | Dictionary d; |
375 | d["class" ] = E; |
376 | d["language" ] = global_classes[E].language; |
377 | d["path" ] = global_classes[E].path; |
378 | d["base" ] = global_classes[E].base; |
379 | d["icon" ] = class_icons.get(E, "" ); |
380 | gcarr.push_back(d); |
381 | } |
382 | ProjectSettings::get_singleton()->store_global_class_list(gcarr); |
383 | } |
384 | |
385 | String ScriptServer::get_global_class_cache_file_path() { |
386 | return ProjectSettings::get_singleton()->get_global_class_list_path(); |
387 | } |
388 | |
389 | //////////////////// |
390 | |
391 | ScriptCodeCompletionCache *ScriptCodeCompletionCache::singleton = nullptr; |
392 | ScriptCodeCompletionCache::ScriptCodeCompletionCache() { |
393 | singleton = this; |
394 | } |
395 | |
396 | void ScriptLanguage::get_core_type_words(List<String> *p_core_type_words) const { |
397 | p_core_type_words->push_back("String" ); |
398 | p_core_type_words->push_back("Vector2" ); |
399 | p_core_type_words->push_back("Vector2i" ); |
400 | p_core_type_words->push_back("Rect2" ); |
401 | p_core_type_words->push_back("Rect2i" ); |
402 | p_core_type_words->push_back("Vector3" ); |
403 | p_core_type_words->push_back("Vector3i" ); |
404 | p_core_type_words->push_back("Transform2D" ); |
405 | p_core_type_words->push_back("Vector4" ); |
406 | p_core_type_words->push_back("Vector4i" ); |
407 | p_core_type_words->push_back("Plane" ); |
408 | p_core_type_words->push_back("Quaternion" ); |
409 | p_core_type_words->push_back("AABB" ); |
410 | p_core_type_words->push_back("Basis" ); |
411 | p_core_type_words->push_back("Transform3D" ); |
412 | p_core_type_words->push_back("Projection" ); |
413 | p_core_type_words->push_back("Color" ); |
414 | p_core_type_words->push_back("StringName" ); |
415 | p_core_type_words->push_back("NodePath" ); |
416 | p_core_type_words->push_back("RID" ); |
417 | p_core_type_words->push_back("Callable" ); |
418 | p_core_type_words->push_back("Signal" ); |
419 | p_core_type_words->push_back("Dictionary" ); |
420 | p_core_type_words->push_back("Array" ); |
421 | p_core_type_words->push_back("PackedByteArray" ); |
422 | p_core_type_words->push_back("PackedInt32Array" ); |
423 | p_core_type_words->push_back("PackedInt64Array" ); |
424 | p_core_type_words->push_back("PackedFloat32Array" ); |
425 | p_core_type_words->push_back("PackedFloat64Array" ); |
426 | p_core_type_words->push_back("PackedStringArray" ); |
427 | p_core_type_words->push_back("PackedVector2Array" ); |
428 | p_core_type_words->push_back("PackedVector3Array" ); |
429 | p_core_type_words->push_back("PackedColorArray" ); |
430 | } |
431 | |
432 | void ScriptLanguage::frame() { |
433 | } |
434 | |
435 | TypedArray<int> ScriptLanguage::CodeCompletionOption::get_option_characteristics(const String &p_base) { |
436 | // Return characacteristics of the match found by order of importance. |
437 | // Matches will be ranked by a lexicographical order on the vector returned by this function. |
438 | // The lower values indicate better matches and that they should go before in the order of appearance. |
439 | if (last_matches == matches) { |
440 | return charac; |
441 | } |
442 | charac.clear(); |
443 | // Ensure base is not empty and at the same time that matches is not empty too. |
444 | if (p_base.length() == 0) { |
445 | last_matches = matches; |
446 | charac.push_back(location); |
447 | return charac; |
448 | } |
449 | charac.push_back(matches.size()); |
450 | charac.push_back((matches[0].first == 0) ? 0 : 1); |
451 | const char32_t *target_char = &p_base[0]; |
452 | int bad_case = 0; |
453 | for (const Pair<int, int> &match_segment : matches) { |
454 | const char32_t *string_to_complete_char = &display[match_segment.first]; |
455 | for (int j = 0; j < match_segment.second; j++, string_to_complete_char++, target_char++) { |
456 | if (*string_to_complete_char != *target_char) { |
457 | bad_case++; |
458 | } |
459 | } |
460 | } |
461 | charac.push_back(bad_case); |
462 | charac.push_back(location); |
463 | charac.push_back(matches[0].first); |
464 | last_matches = matches; |
465 | return charac; |
466 | } |
467 | |
468 | void ScriptLanguage::CodeCompletionOption::clear_characteristics() { |
469 | charac = TypedArray<int>(); |
470 | } |
471 | |
472 | TypedArray<int> ScriptLanguage::CodeCompletionOption::get_option_cached_characteristics() const { |
473 | // Only returns the cached value and warns if it was not updated since the last change of matches. |
474 | if (last_matches != matches) { |
475 | WARN_PRINT("Characteristics are not up to date." ); |
476 | } |
477 | |
478 | return charac; |
479 | } |
480 | |
481 | bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_value) { |
482 | if (script->is_placeholder_fallback_enabled()) { |
483 | return false; |
484 | } |
485 | |
486 | if (values.has(p_name)) { |
487 | Variant defval; |
488 | if (script->get_property_default_value(p_name, defval)) { |
489 | // The evaluate function ensures that a NIL variant is equal to e.g. an empty Resource. |
490 | // Simply doing defval == p_value does not do this. |
491 | if (Variant::evaluate(Variant::OP_EQUAL, defval, p_value)) { |
492 | values.erase(p_name); |
493 | return true; |
494 | } |
495 | } |
496 | values[p_name] = p_value; |
497 | return true; |
498 | } else { |
499 | Variant defval; |
500 | if (script->get_property_default_value(p_name, defval)) { |
501 | if (Variant::evaluate(Variant::OP_NOT_EQUAL, defval, p_value)) { |
502 | values[p_name] = p_value; |
503 | } |
504 | return true; |
505 | } |
506 | } |
507 | return false; |
508 | } |
509 | |
510 | bool PlaceHolderScriptInstance::get(const StringName &p_name, Variant &r_ret) const { |
511 | if (values.has(p_name)) { |
512 | r_ret = values[p_name]; |
513 | return true; |
514 | } |
515 | |
516 | if (constants.has(p_name)) { |
517 | r_ret = constants[p_name]; |
518 | return true; |
519 | } |
520 | |
521 | if (!script->is_placeholder_fallback_enabled()) { |
522 | Variant defval; |
523 | if (script->get_property_default_value(p_name, defval)) { |
524 | r_ret = defval; |
525 | return true; |
526 | } |
527 | } |
528 | |
529 | return false; |
530 | } |
531 | |
532 | void PlaceHolderScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const { |
533 | if (script->is_placeholder_fallback_enabled()) { |
534 | for (const PropertyInfo &E : properties) { |
535 | p_properties->push_back(E); |
536 | } |
537 | } else { |
538 | for (const PropertyInfo &E : properties) { |
539 | PropertyInfo pinfo = E; |
540 | if (!values.has(pinfo.name)) { |
541 | pinfo.usage |= PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE; |
542 | } |
543 | p_properties->push_back(E); |
544 | } |
545 | } |
546 | } |
547 | |
548 | Variant::Type PlaceHolderScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const { |
549 | if (values.has(p_name)) { |
550 | if (r_is_valid) { |
551 | *r_is_valid = true; |
552 | } |
553 | return values[p_name].get_type(); |
554 | } |
555 | |
556 | if (constants.has(p_name)) { |
557 | if (r_is_valid) { |
558 | *r_is_valid = true; |
559 | } |
560 | return constants[p_name].get_type(); |
561 | } |
562 | |
563 | if (r_is_valid) { |
564 | *r_is_valid = false; |
565 | } |
566 | |
567 | return Variant::NIL; |
568 | } |
569 | |
570 | void PlaceHolderScriptInstance::get_method_list(List<MethodInfo> *p_list) const { |
571 | if (script->is_placeholder_fallback_enabled()) { |
572 | return; |
573 | } |
574 | |
575 | if (script.is_valid()) { |
576 | script->get_script_method_list(p_list); |
577 | } |
578 | } |
579 | |
580 | bool PlaceHolderScriptInstance::has_method(const StringName &p_method) const { |
581 | if (script->is_placeholder_fallback_enabled()) { |
582 | return false; |
583 | } |
584 | |
585 | if (script.is_valid()) { |
586 | return script->has_method(p_method); |
587 | } |
588 | return false; |
589 | } |
590 | |
591 | void PlaceHolderScriptInstance::update(const List<PropertyInfo> &p_properties, const HashMap<StringName, Variant> &p_values) { |
592 | HashSet<StringName> new_values; |
593 | for (const PropertyInfo &E : p_properties) { |
594 | StringName n = E.name; |
595 | new_values.insert(n); |
596 | |
597 | if (!values.has(n) || values[n].get_type() != E.type) { |
598 | if (p_values.has(n)) { |
599 | values[n] = p_values[n]; |
600 | } |
601 | } |
602 | } |
603 | |
604 | properties = p_properties; |
605 | List<StringName> to_remove; |
606 | |
607 | for (KeyValue<StringName, Variant> &E : values) { |
608 | if (!new_values.has(E.key)) { |
609 | to_remove.push_back(E.key); |
610 | } |
611 | |
612 | Variant defval; |
613 | if (script->get_property_default_value(E.key, defval)) { |
614 | //remove because it's the same as the default value |
615 | if (defval == E.value) { |
616 | to_remove.push_back(E.key); |
617 | } |
618 | } |
619 | } |
620 | |
621 | while (to_remove.size()) { |
622 | values.erase(to_remove.front()->get()); |
623 | to_remove.pop_front(); |
624 | } |
625 | |
626 | if (owner && owner->get_script_instance() == this) { |
627 | owner->notify_property_list_changed(); |
628 | } |
629 | //change notify |
630 | |
631 | constants.clear(); |
632 | script->get_constants(&constants); |
633 | } |
634 | |
635 | void PlaceHolderScriptInstance::property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid) { |
636 | if (script->is_placeholder_fallback_enabled()) { |
637 | HashMap<StringName, Variant>::Iterator E = values.find(p_name); |
638 | |
639 | if (E) { |
640 | E->value = p_value; |
641 | } else { |
642 | values.insert(p_name, p_value); |
643 | } |
644 | |
645 | bool found = false; |
646 | for (const PropertyInfo &F : properties) { |
647 | if (F.name == p_name) { |
648 | found = true; |
649 | break; |
650 | } |
651 | } |
652 | if (!found) { |
653 | properties.push_back(PropertyInfo(p_value.get_type(), p_name, PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE)); |
654 | } |
655 | } |
656 | |
657 | if (r_valid) { |
658 | *r_valid = false; // Cannot change the value in either case |
659 | } |
660 | } |
661 | |
662 | Variant PlaceHolderScriptInstance::property_get_fallback(const StringName &p_name, bool *r_valid) { |
663 | if (script->is_placeholder_fallback_enabled()) { |
664 | HashMap<StringName, Variant>::ConstIterator E = values.find(p_name); |
665 | |
666 | if (E) { |
667 | if (r_valid) { |
668 | *r_valid = true; |
669 | } |
670 | return E->value; |
671 | } |
672 | |
673 | E = constants.find(p_name); |
674 | if (E) { |
675 | if (r_valid) { |
676 | *r_valid = true; |
677 | } |
678 | return E->value; |
679 | } |
680 | } |
681 | |
682 | if (r_valid) { |
683 | *r_valid = false; |
684 | } |
685 | |
686 | return Variant(); |
687 | } |
688 | |
689 | PlaceHolderScriptInstance::PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script, Object *p_owner) : |
690 | owner(p_owner), |
691 | language(p_language), |
692 | script(p_script) { |
693 | } |
694 | |
695 | PlaceHolderScriptInstance::~PlaceHolderScriptInstance() { |
696 | if (script.is_valid()) { |
697 | script->_placeholder_erased(this); |
698 | } |
699 | } |
700 | |