script_language.cpp 27 KB

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