script_language.cpp 25 KB

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