script_language.cpp 28 KB

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