script_language.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /**************************************************************************/
  2. /* script_language.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "script_language.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/debugger/script_debugger.h"
  34. #include "core/io/resource_loader.h"
  35. ScriptLanguage *ScriptServer::_languages[MAX_LANGUAGES];
  36. int ScriptServer::_language_count = 0;
  37. bool ScriptServer::languages_ready = false;
  38. Mutex ScriptServer::languages_mutex;
  39. thread_local bool ScriptServer::thread_entered = false;
  40. bool ScriptServer::scripting_enabled = true;
  41. bool ScriptServer::reload_scripts_on_save = false;
  42. ScriptEditRequestFunction ScriptServer::edit_request_func = nullptr;
  43. 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. ClassDB::bind_method(D_METHOD("get_rpc_config"), &Script::get_rpc_config);
  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. // Only reload the script when there are no compilation errors to prevent printing the error messages twice.
  164. if (rel->is_valid()) {
  165. if (Engine::get_singleton()->is_editor_hint() && is_tool()) {
  166. get_language()->reload_tool_script(this, true);
  167. } else {
  168. // It's important to set p_keep_state to true in order to manage reloading scripts
  169. // that are currently instantiated.
  170. reload(true);
  171. }
  172. }
  173. #else
  174. Resource::reload_from_file();
  175. #endif
  176. }
  177. void ScriptServer::set_scripting_enabled(bool p_enabled) {
  178. scripting_enabled = p_enabled;
  179. }
  180. bool ScriptServer::is_scripting_enabled() {
  181. return scripting_enabled;
  182. }
  183. ScriptLanguage *ScriptServer::get_language(int p_idx) {
  184. MutexLock lock(languages_mutex);
  185. ERR_FAIL_INDEX_V(p_idx, _language_count, nullptr);
  186. return _languages[p_idx];
  187. }
  188. ScriptLanguage *ScriptServer::get_language_for_extension(const String &p_extension) {
  189. MutexLock lock(languages_mutex);
  190. for (int i = 0; i < _language_count; i++) {
  191. if (_languages[i] && _languages[i]->get_extension() == p_extension) {
  192. return _languages[i];
  193. }
  194. }
  195. return nullptr;
  196. }
  197. Error ScriptServer::register_language(ScriptLanguage *p_language) {
  198. MutexLock lock(languages_mutex);
  199. ERR_FAIL_NULL_V(p_language, ERR_INVALID_PARAMETER);
  200. ERR_FAIL_COND_V_MSG(_language_count >= MAX_LANGUAGES, ERR_UNAVAILABLE, "Script languages limit has been reach, cannot register more.");
  201. for (int i = 0; i < _language_count; i++) {
  202. const ScriptLanguage *other_language = _languages[i];
  203. 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()));
  204. 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()));
  205. 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()));
  206. }
  207. _languages[_language_count++] = p_language;
  208. return OK;
  209. }
  210. Error ScriptServer::unregister_language(const ScriptLanguage *p_language) {
  211. MutexLock lock(languages_mutex);
  212. for (int i = 0; i < _language_count; i++) {
  213. if (_languages[i] == p_language) {
  214. _language_count--;
  215. if (i < _language_count) {
  216. SWAP(_languages[i], _languages[_language_count]);
  217. }
  218. return OK;
  219. }
  220. }
  221. return ERR_DOES_NOT_EXIST;
  222. }
  223. void ScriptServer::init_languages() {
  224. { // Load global classes.
  225. global_classes_clear();
  226. #ifndef DISABLE_DEPRECATED
  227. if (ProjectSettings::get_singleton()->has_setting("_global_script_classes")) {
  228. Array script_classes = GLOBAL_GET("_global_script_classes");
  229. for (const Variant &script_class : script_classes) {
  230. Dictionary c = script_class;
  231. if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base")) {
  232. continue;
  233. }
  234. add_global_class(c["class"], c["base"], c["language"], c["path"]);
  235. }
  236. ProjectSettings::get_singleton()->clear("_global_script_classes");
  237. }
  238. #endif
  239. Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
  240. for (const Variant &script_class : script_classes) {
  241. Dictionary c = script_class;
  242. if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base")) {
  243. continue;
  244. }
  245. add_global_class(c["class"], c["base"], c["language"], c["path"]);
  246. }
  247. }
  248. HashSet<ScriptLanguage *> langs_to_init;
  249. {
  250. MutexLock lock(languages_mutex);
  251. for (int i = 0; i < _language_count; i++) {
  252. if (_languages[i]) {
  253. langs_to_init.insert(_languages[i]);
  254. }
  255. }
  256. }
  257. for (ScriptLanguage *E : langs_to_init) {
  258. E->init();
  259. }
  260. {
  261. MutexLock lock(languages_mutex);
  262. languages_ready = true;
  263. }
  264. }
  265. void ScriptServer::finish_languages() {
  266. HashSet<ScriptLanguage *> langs_to_finish;
  267. {
  268. MutexLock lock(languages_mutex);
  269. for (int i = 0; i < _language_count; i++) {
  270. if (_languages[i]) {
  271. langs_to_finish.insert(_languages[i]);
  272. }
  273. }
  274. }
  275. for (ScriptLanguage *E : langs_to_finish) {
  276. E->finish();
  277. }
  278. {
  279. MutexLock lock(languages_mutex);
  280. languages_ready = false;
  281. }
  282. global_classes_clear();
  283. }
  284. bool ScriptServer::are_languages_initialized() {
  285. MutexLock lock(languages_mutex);
  286. return languages_ready;
  287. }
  288. bool ScriptServer::thread_is_entered() {
  289. return thread_entered;
  290. }
  291. void ScriptServer::set_reload_scripts_on_save(bool p_enable) {
  292. reload_scripts_on_save = p_enable;
  293. }
  294. bool ScriptServer::is_reload_scripts_on_save_enabled() {
  295. return reload_scripts_on_save;
  296. }
  297. void ScriptServer::thread_enter() {
  298. if (thread_entered) {
  299. return;
  300. }
  301. MutexLock lock(languages_mutex);
  302. if (!languages_ready) {
  303. return;
  304. }
  305. for (int i = 0; i < _language_count; i++) {
  306. _languages[i]->thread_enter();
  307. }
  308. thread_entered = true;
  309. }
  310. void ScriptServer::thread_exit() {
  311. if (!thread_entered) {
  312. return;
  313. }
  314. MutexLock lock(languages_mutex);
  315. if (!languages_ready) {
  316. return;
  317. }
  318. for (int i = 0; i < _language_count; i++) {
  319. _languages[i]->thread_exit();
  320. }
  321. thread_entered = false;
  322. }
  323. HashMap<StringName, ScriptServer::GlobalScriptClass> ScriptServer::global_classes;
  324. HashMap<StringName, Vector<StringName>> ScriptServer::inheriters_cache;
  325. bool ScriptServer::inheriters_cache_dirty = true;
  326. void ScriptServer::global_classes_clear() {
  327. global_classes.clear();
  328. inheriters_cache.clear();
  329. }
  330. void ScriptServer::add_global_class(const StringName &p_class, const StringName &p_base, const StringName &p_language, const String &p_path) {
  331. 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.");
  332. GlobalScriptClass *existing = global_classes.getptr(p_class);
  333. if (existing) {
  334. // Update an existing class (only set dirty if something changed).
  335. if (existing->base != p_base || existing->path != p_path || existing->language != p_language) {
  336. existing->base = p_base;
  337. existing->path = p_path;
  338. existing->language = p_language;
  339. inheriters_cache_dirty = true;
  340. }
  341. } else {
  342. // Add new class.
  343. GlobalScriptClass g;
  344. g.language = p_language;
  345. g.path = p_path;
  346. g.base = p_base;
  347. global_classes[p_class] = g;
  348. inheriters_cache_dirty = true;
  349. }
  350. }
  351. void ScriptServer::remove_global_class(const StringName &p_class) {
  352. global_classes.erase(p_class);
  353. inheriters_cache_dirty = true;
  354. }
  355. void ScriptServer::get_inheriters_list(const StringName &p_base_type, List<StringName> *r_classes) {
  356. if (inheriters_cache_dirty) {
  357. inheriters_cache.clear();
  358. for (const KeyValue<StringName, GlobalScriptClass> &K : global_classes) {
  359. if (!inheriters_cache.has(K.value.base)) {
  360. inheriters_cache[K.value.base] = Vector<StringName>();
  361. }
  362. inheriters_cache[K.value.base].push_back(K.key);
  363. }
  364. for (KeyValue<StringName, Vector<StringName>> &K : inheriters_cache) {
  365. K.value.sort_custom<StringName::AlphCompare>();
  366. }
  367. inheriters_cache_dirty = false;
  368. }
  369. if (!inheriters_cache.has(p_base_type)) {
  370. return;
  371. }
  372. const Vector<StringName> &v = inheriters_cache[p_base_type];
  373. for (int i = 0; i < v.size(); i++) {
  374. r_classes->push_back(v[i]);
  375. }
  376. }
  377. void ScriptServer::remove_global_class_by_path(const String &p_path) {
  378. for (const KeyValue<StringName, GlobalScriptClass> &kv : global_classes) {
  379. if (kv.value.path == p_path) {
  380. global_classes.erase(kv.key);
  381. inheriters_cache_dirty = true;
  382. return;
  383. }
  384. }
  385. }
  386. bool ScriptServer::is_global_class(const StringName &p_class) {
  387. return global_classes.has(p_class);
  388. }
  389. StringName ScriptServer::get_global_class_language(const StringName &p_class) {
  390. ERR_FAIL_COND_V(!global_classes.has(p_class), StringName());
  391. return global_classes[p_class].language;
  392. }
  393. String ScriptServer::get_global_class_path(const String &p_class) {
  394. ERR_FAIL_COND_V(!global_classes.has(p_class), String());
  395. return global_classes[p_class].path;
  396. }
  397. StringName ScriptServer::get_global_class_base(const String &p_class) {
  398. ERR_FAIL_COND_V(!global_classes.has(p_class), String());
  399. return global_classes[p_class].base;
  400. }
  401. StringName ScriptServer::get_global_class_native_base(const String &p_class) {
  402. ERR_FAIL_COND_V(!global_classes.has(p_class), String());
  403. String base = global_classes[p_class].base;
  404. while (global_classes.has(base)) {
  405. base = global_classes[base].base;
  406. }
  407. return base;
  408. }
  409. void ScriptServer::get_global_class_list(List<StringName> *r_global_classes) {
  410. List<StringName> classes;
  411. for (const KeyValue<StringName, GlobalScriptClass> &E : global_classes) {
  412. classes.push_back(E.key);
  413. }
  414. classes.sort_custom<StringName::AlphCompare>();
  415. for (const StringName &E : classes) {
  416. r_global_classes->push_back(E);
  417. }
  418. }
  419. void ScriptServer::save_global_classes() {
  420. Dictionary class_icons;
  421. Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
  422. for (const Variant &script_class : script_classes) {
  423. Dictionary d = script_class;
  424. if (!d.has("name") || !d.has("icon")) {
  425. continue;
  426. }
  427. class_icons[d["name"]] = d["icon"];
  428. }
  429. List<StringName> gc;
  430. get_global_class_list(&gc);
  431. Array gcarr;
  432. for (const StringName &E : gc) {
  433. Dictionary d;
  434. d["class"] = E;
  435. d["language"] = global_classes[E].language;
  436. d["path"] = global_classes[E].path;
  437. d["base"] = global_classes[E].base;
  438. d["icon"] = class_icons.get(E, "");
  439. gcarr.push_back(d);
  440. }
  441. ProjectSettings::get_singleton()->store_global_class_list(gcarr);
  442. }
  443. ////////////////////
  444. ScriptCodeCompletionCache *ScriptCodeCompletionCache::singleton = nullptr;
  445. ScriptCodeCompletionCache::ScriptCodeCompletionCache() {
  446. singleton = this;
  447. }
  448. void ScriptLanguage::get_core_type_words(List<String> *p_core_type_words) const {
  449. p_core_type_words->push_back("String");
  450. p_core_type_words->push_back("Vector2");
  451. p_core_type_words->push_back("Vector2i");
  452. p_core_type_words->push_back("Rect2");
  453. p_core_type_words->push_back("Rect2i");
  454. p_core_type_words->push_back("Vector3");
  455. p_core_type_words->push_back("Vector3i");
  456. p_core_type_words->push_back("Transform2D");
  457. p_core_type_words->push_back("Vector4");
  458. p_core_type_words->push_back("Vector4i");
  459. p_core_type_words->push_back("Plane");
  460. p_core_type_words->push_back("Quaternion");
  461. p_core_type_words->push_back("AABB");
  462. p_core_type_words->push_back("Basis");
  463. p_core_type_words->push_back("Transform3D");
  464. p_core_type_words->push_back("Projection");
  465. p_core_type_words->push_back("Color");
  466. p_core_type_words->push_back("StringName");
  467. p_core_type_words->push_back("NodePath");
  468. p_core_type_words->push_back("RID");
  469. p_core_type_words->push_back("Callable");
  470. p_core_type_words->push_back("Signal");
  471. p_core_type_words->push_back("Dictionary");
  472. p_core_type_words->push_back("Array");
  473. p_core_type_words->push_back("PackedByteArray");
  474. p_core_type_words->push_back("PackedInt32Array");
  475. p_core_type_words->push_back("PackedInt64Array");
  476. p_core_type_words->push_back("PackedFloat32Array");
  477. p_core_type_words->push_back("PackedFloat64Array");
  478. p_core_type_words->push_back("PackedStringArray");
  479. p_core_type_words->push_back("PackedVector2Array");
  480. p_core_type_words->push_back("PackedVector3Array");
  481. p_core_type_words->push_back("PackedColorArray");
  482. p_core_type_words->push_back("PackedVector4Array");
  483. }
  484. void ScriptLanguage::frame() {
  485. }
  486. TypedArray<int> ScriptLanguage::CodeCompletionOption::get_option_characteristics(const String &p_base) {
  487. // Return characteristics of the match found by order of importance.
  488. // Matches will be ranked by a lexicographical order on the vector returned by this function.
  489. // The lower values indicate better matches and that they should go before in the order of appearance.
  490. if (last_matches == matches) {
  491. return charac;
  492. }
  493. charac.clear();
  494. // Ensure base is not empty and at the same time that matches is not empty too.
  495. if (p_base.length() == 0) {
  496. last_matches = matches;
  497. charac.push_back(location);
  498. return charac;
  499. }
  500. charac.push_back(matches.size());
  501. charac.push_back((matches[0].first == 0) ? 0 : 1);
  502. const char32_t *target_char = &p_base[0];
  503. int bad_case = 0;
  504. for (const Pair<int, int> &match_segment : matches) {
  505. const char32_t *string_to_complete_char = &display[match_segment.first];
  506. for (int j = 0; j < match_segment.second; j++, string_to_complete_char++, target_char++) {
  507. if (*string_to_complete_char != *target_char) {
  508. bad_case++;
  509. }
  510. }
  511. }
  512. charac.push_back(bad_case);
  513. charac.push_back(location);
  514. charac.push_back(matches[0].first);
  515. last_matches = matches;
  516. return charac;
  517. }
  518. void ScriptLanguage::CodeCompletionOption::clear_characteristics() {
  519. charac = TypedArray<int>();
  520. }
  521. TypedArray<int> ScriptLanguage::CodeCompletionOption::get_option_cached_characteristics() const {
  522. // Only returns the cached value and warns if it was not updated since the last change of matches.
  523. if (last_matches != matches) {
  524. WARN_PRINT("Characteristics are not up to date.");
  525. }
  526. return charac;
  527. }
  528. void ScriptLanguage::_bind_methods() {
  529. BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_AUTO);
  530. BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_PASCAL_CASE);
  531. BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_SNAKE_CASE);
  532. BIND_ENUM_CONSTANT(SCRIPT_NAME_CASING_KEBAB_CASE);
  533. }
  534. bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  535. if (script->is_placeholder_fallback_enabled()) {
  536. return false;
  537. }
  538. if (values.has(p_name)) {
  539. Variant defval;
  540. if (script->get_property_default_value(p_name, defval)) {
  541. // The evaluate function ensures that a NIL variant is equal to e.g. an empty Resource.
  542. // Simply doing defval == p_value does not do this.
  543. if (Variant::evaluate(Variant::OP_EQUAL, defval, p_value)) {
  544. values.erase(p_name);
  545. return true;
  546. }
  547. }
  548. values[p_name] = p_value;
  549. return true;
  550. } else {
  551. Variant defval;
  552. if (script->get_property_default_value(p_name, defval)) {
  553. if (Variant::evaluate(Variant::OP_NOT_EQUAL, defval, p_value)) {
  554. values[p_name] = p_value;
  555. }
  556. return true;
  557. }
  558. }
  559. return false;
  560. }
  561. bool PlaceHolderScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  562. if (values.has(p_name)) {
  563. r_ret = values[p_name];
  564. return true;
  565. }
  566. if (constants.has(p_name)) {
  567. r_ret = constants[p_name];
  568. return true;
  569. }
  570. if (!script->is_placeholder_fallback_enabled()) {
  571. Variant defval;
  572. if (script->get_property_default_value(p_name, defval)) {
  573. r_ret = defval;
  574. return true;
  575. }
  576. }
  577. return false;
  578. }
  579. void PlaceHolderScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  580. if (script->is_placeholder_fallback_enabled()) {
  581. for (const PropertyInfo &E : properties) {
  582. p_properties->push_back(E);
  583. }
  584. } else {
  585. for (const PropertyInfo &E : properties) {
  586. PropertyInfo pinfo = E;
  587. p_properties->push_back(E);
  588. }
  589. }
  590. }
  591. Variant::Type PlaceHolderScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  592. if (values.has(p_name)) {
  593. if (r_is_valid) {
  594. *r_is_valid = true;
  595. }
  596. return values[p_name].get_type();
  597. }
  598. if (constants.has(p_name)) {
  599. if (r_is_valid) {
  600. *r_is_valid = true;
  601. }
  602. return constants[p_name].get_type();
  603. }
  604. if (r_is_valid) {
  605. *r_is_valid = false;
  606. }
  607. return Variant::NIL;
  608. }
  609. void PlaceHolderScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  610. if (script->is_placeholder_fallback_enabled()) {
  611. return;
  612. }
  613. if (script.is_valid()) {
  614. script->get_script_method_list(p_list);
  615. }
  616. }
  617. bool PlaceHolderScriptInstance::has_method(const StringName &p_method) const {
  618. if (script->is_placeholder_fallback_enabled()) {
  619. return false;
  620. }
  621. if (script.is_valid()) {
  622. Ref<Script> scr = script;
  623. while (scr.is_valid()) {
  624. if (scr->has_method(p_method)) {
  625. return true;
  626. }
  627. scr = scr->get_base_script();
  628. }
  629. }
  630. return false;
  631. }
  632. Variant PlaceHolderScriptInstance::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  633. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  634. #if TOOLS_ENABLED
  635. if (Engine::get_singleton()->is_editor_hint()) {
  636. return String("Attempt to call a method on a placeholder instance. Check if the script is in tool mode.");
  637. } else {
  638. return String("Attempt to call a method on a placeholder instance. Probably a bug, please report.");
  639. }
  640. #else
  641. return Variant();
  642. #endif // TOOLS_ENABLED
  643. }
  644. void PlaceHolderScriptInstance::update(const List<PropertyInfo> &p_properties, const HashMap<StringName, Variant> &p_values) {
  645. HashSet<StringName> new_values;
  646. for (const PropertyInfo &E : p_properties) {
  647. if (E.usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_CATEGORY)) {
  648. continue;
  649. }
  650. StringName n = E.name;
  651. new_values.insert(n);
  652. if (!values.has(n) || values[n].get_type() != E.type) {
  653. if (p_values.has(n)) {
  654. values[n] = p_values[n];
  655. }
  656. }
  657. }
  658. properties = p_properties;
  659. List<StringName> to_remove;
  660. for (KeyValue<StringName, Variant> &E : values) {
  661. if (!new_values.has(E.key)) {
  662. to_remove.push_back(E.key);
  663. }
  664. Variant defval;
  665. if (script->get_property_default_value(E.key, defval)) {
  666. //remove because it's the same as the default value
  667. if (defval == E.value) {
  668. to_remove.push_back(E.key);
  669. }
  670. }
  671. }
  672. while (to_remove.size()) {
  673. values.erase(to_remove.front()->get());
  674. to_remove.pop_front();
  675. }
  676. if (owner && owner->get_script_instance() == this) {
  677. owner->notify_property_list_changed();
  678. }
  679. //change notify
  680. constants.clear();
  681. script->get_constants(&constants);
  682. }
  683. void PlaceHolderScriptInstance::property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid) {
  684. if (script->is_placeholder_fallback_enabled()) {
  685. HashMap<StringName, Variant>::Iterator E = values.find(p_name);
  686. if (E) {
  687. E->value = p_value;
  688. } else {
  689. values.insert(p_name, p_value);
  690. }
  691. bool found = false;
  692. for (const PropertyInfo &F : properties) {
  693. if (F.name == p_name) {
  694. found = true;
  695. break;
  696. }
  697. }
  698. if (!found) {
  699. PropertyHint hint = PROPERTY_HINT_NONE;
  700. const Object *obj = p_value.get_validated_object();
  701. if (obj && obj->is_class("Node")) {
  702. hint = PROPERTY_HINT_NODE_TYPE;
  703. }
  704. properties.push_back(PropertyInfo(p_value.get_type(), p_name, hint, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE));
  705. }
  706. }
  707. if (r_valid) {
  708. *r_valid = false; // Cannot change the value in either case
  709. }
  710. }
  711. Variant PlaceHolderScriptInstance::property_get_fallback(const StringName &p_name, bool *r_valid) {
  712. if (script->is_placeholder_fallback_enabled()) {
  713. HashMap<StringName, Variant>::ConstIterator E = values.find(p_name);
  714. if (E) {
  715. if (r_valid) {
  716. *r_valid = true;
  717. }
  718. return E->value;
  719. }
  720. E = constants.find(p_name);
  721. if (E) {
  722. if (r_valid) {
  723. *r_valid = true;
  724. }
  725. return E->value;
  726. }
  727. }
  728. if (r_valid) {
  729. *r_valid = false;
  730. }
  731. return Variant();
  732. }
  733. PlaceHolderScriptInstance::PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script, Object *p_owner) :
  734. owner(p_owner),
  735. language(p_language),
  736. script(p_script) {
  737. }
  738. PlaceHolderScriptInstance::~PlaceHolderScriptInstance() {
  739. if (script.is_valid()) {
  740. script->_placeholder_erased(this);
  741. }
  742. }