script_language.cpp 27 KB

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