script_language.cpp 26 KB

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