script_language.cpp 22 KB

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