script_language.cpp 27 KB

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