script_language.cpp 25 KB

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