script_language.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*************************************************************************/
  2. /* script_language.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/core_string_names.h"
  32. #include "core/project_settings.h"
  33. ScriptLanguage *ScriptServer::_languages[MAX_LANGUAGES];
  34. int ScriptServer::_language_count = 0;
  35. bool ScriptServer::scripting_enabled = true;
  36. bool ScriptServer::reload_scripts_on_save = false;
  37. bool ScriptServer::languages_finished = false;
  38. ScriptEditRequestFunction ScriptServer::edit_request_func = NULL;
  39. void Script::_notification(int p_what) {
  40. if (p_what == NOTIFICATION_POSTINITIALIZE) {
  41. if (ScriptDebugger::get_singleton())
  42. ScriptDebugger::get_singleton()->set_break_language(get_language());
  43. }
  44. }
  45. Variant Script::_get_property_default_value(const StringName &p_property) {
  46. Variant ret;
  47. get_property_default_value(p_property, ret);
  48. return ret;
  49. }
  50. Array Script::_get_script_property_list() {
  51. Array ret;
  52. List<PropertyInfo> list;
  53. get_script_property_list(&list);
  54. for (List<PropertyInfo>::Element *E = list.front(); E; E = E->next()) {
  55. ret.append(E->get().operator Dictionary());
  56. }
  57. return ret;
  58. }
  59. Array Script::_get_script_method_list() {
  60. Array ret;
  61. List<MethodInfo> list;
  62. get_script_method_list(&list);
  63. for (List<MethodInfo>::Element *E = list.front(); E; E = E->next()) {
  64. ret.append(E->get().operator Dictionary());
  65. }
  66. return ret;
  67. }
  68. Array Script::_get_script_signal_list() {
  69. Array ret;
  70. List<MethodInfo> list;
  71. get_script_signal_list(&list);
  72. for (List<MethodInfo>::Element *E = list.front(); E; E = E->next()) {
  73. ret.append(E->get().operator Dictionary());
  74. }
  75. return ret;
  76. }
  77. Dictionary Script::_get_script_constant_map() {
  78. Dictionary ret;
  79. Map<StringName, Variant> map;
  80. get_constants(&map);
  81. for (Map<StringName, Variant>::Element *E = map.front(); E; E = E->next()) {
  82. ret[E->key()] = E->value();
  83. }
  84. return ret;
  85. }
  86. void Script::_bind_methods() {
  87. ClassDB::bind_method(D_METHOD("can_instance"), &Script::can_instance);
  88. //ClassDB::bind_method(D_METHOD("instance_create","base_object"),&Script::instance_create);
  89. ClassDB::bind_method(D_METHOD("instance_has", "base_object"), &Script::instance_has);
  90. ClassDB::bind_method(D_METHOD("has_source_code"), &Script::has_source_code);
  91. ClassDB::bind_method(D_METHOD("get_source_code"), &Script::get_source_code);
  92. ClassDB::bind_method(D_METHOD("set_source_code", "source"), &Script::set_source_code);
  93. ClassDB::bind_method(D_METHOD("reload", "keep_state"), &Script::reload, DEFVAL(false));
  94. ClassDB::bind_method(D_METHOD("get_base_script"), &Script::get_base_script);
  95. ClassDB::bind_method(D_METHOD("get_instance_base_type"), &Script::get_instance_base_type);
  96. ClassDB::bind_method(D_METHOD("has_script_signal", "signal_name"), &Script::has_script_signal);
  97. ClassDB::bind_method(D_METHOD("get_script_property_list"), &Script::_get_script_property_list);
  98. ClassDB::bind_method(D_METHOD("get_script_method_list"), &Script::_get_script_method_list);
  99. ClassDB::bind_method(D_METHOD("get_script_signal_list"), &Script::_get_script_signal_list);
  100. ClassDB::bind_method(D_METHOD("get_script_constant_map"), &Script::_get_script_constant_map);
  101. ClassDB::bind_method(D_METHOD("get_property_default_value", "property"), &Script::_get_property_default_value);
  102. ClassDB::bind_method(D_METHOD("is_tool"), &Script::is_tool);
  103. ADD_PROPERTY(PropertyInfo(Variant::STRING, "source_code", PROPERTY_HINT_NONE, "", 0), "set_source_code", "get_source_code");
  104. }
  105. void ScriptServer::set_scripting_enabled(bool p_enabled) {
  106. scripting_enabled = p_enabled;
  107. }
  108. bool ScriptServer::is_scripting_enabled() {
  109. return scripting_enabled;
  110. }
  111. ScriptLanguage *ScriptServer::get_language(int p_idx) {
  112. ERR_FAIL_INDEX_V(p_idx, _language_count, NULL);
  113. return _languages[p_idx];
  114. }
  115. void ScriptServer::register_language(ScriptLanguage *p_language) {
  116. ERR_FAIL_COND(_language_count >= MAX_LANGUAGES);
  117. _languages[_language_count++] = p_language;
  118. }
  119. void ScriptServer::unregister_language(ScriptLanguage *p_language) {
  120. for (int i = 0; i < _language_count; i++) {
  121. if (_languages[i] == p_language) {
  122. _language_count--;
  123. if (i < _language_count) {
  124. SWAP(_languages[i], _languages[_language_count]);
  125. }
  126. return;
  127. }
  128. }
  129. }
  130. void ScriptServer::init_languages() {
  131. { //load global classes
  132. global_classes_clear();
  133. if (ProjectSettings::get_singleton()->has_setting("_global_script_classes")) {
  134. Array script_classes = ProjectSettings::get_singleton()->get("_global_script_classes");
  135. for (int i = 0; i < script_classes.size(); i++) {
  136. Dictionary c = script_classes[i];
  137. if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base"))
  138. continue;
  139. add_global_class(c["class"], c["base"], c["language"], c["path"]);
  140. }
  141. }
  142. }
  143. for (int i = 0; i < _language_count; i++) {
  144. _languages[i]->init();
  145. }
  146. }
  147. void ScriptServer::finish_languages() {
  148. for (int i = 0; i < _language_count; i++) {
  149. _languages[i]->finish();
  150. }
  151. global_classes_clear();
  152. languages_finished = true;
  153. }
  154. void ScriptServer::set_reload_scripts_on_save(bool p_enable) {
  155. reload_scripts_on_save = p_enable;
  156. }
  157. bool ScriptServer::is_reload_scripts_on_save_enabled() {
  158. return reload_scripts_on_save;
  159. }
  160. void ScriptServer::thread_enter() {
  161. for (int i = 0; i < _language_count; i++) {
  162. _languages[i]->thread_enter();
  163. }
  164. }
  165. void ScriptServer::thread_exit() {
  166. for (int i = 0; i < _language_count; i++) {
  167. _languages[i]->thread_exit();
  168. }
  169. }
  170. HashMap<StringName, ScriptServer::GlobalScriptClass> ScriptServer::global_classes;
  171. void ScriptServer::global_classes_clear() {
  172. global_classes.clear();
  173. }
  174. void ScriptServer::add_global_class(const StringName &p_class, const StringName &p_base, const StringName &p_language, const String &p_path) {
  175. 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.");
  176. GlobalScriptClass g;
  177. g.language = p_language;
  178. g.path = p_path;
  179. g.base = p_base;
  180. global_classes[p_class] = g;
  181. }
  182. void ScriptServer::remove_global_class(const StringName &p_class) {
  183. global_classes.erase(p_class);
  184. }
  185. bool ScriptServer::is_global_class(const StringName &p_class) {
  186. return global_classes.has(p_class);
  187. }
  188. StringName ScriptServer::get_global_class_language(const StringName &p_class) {
  189. ERR_FAIL_COND_V(!global_classes.has(p_class), StringName());
  190. return global_classes[p_class].language;
  191. }
  192. String ScriptServer::get_global_class_path(const String &p_class) {
  193. ERR_FAIL_COND_V(!global_classes.has(p_class), String());
  194. return global_classes[p_class].path;
  195. }
  196. StringName ScriptServer::get_global_class_base(const String &p_class) {
  197. ERR_FAIL_COND_V(!global_classes.has(p_class), String());
  198. return global_classes[p_class].base;
  199. }
  200. StringName ScriptServer::get_global_class_native_base(const String &p_class) {
  201. ERR_FAIL_COND_V(!global_classes.has(p_class), String());
  202. String base = global_classes[p_class].base;
  203. while (global_classes.has(base)) {
  204. base = global_classes[base].base;
  205. }
  206. return base;
  207. }
  208. void ScriptServer::get_global_class_list(List<StringName> *r_global_classes) {
  209. const StringName *K = NULL;
  210. List<StringName> classes;
  211. while ((K = global_classes.next(K))) {
  212. classes.push_back(*K);
  213. }
  214. classes.sort_custom<StringName::AlphCompare>();
  215. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  216. r_global_classes->push_back(E->get());
  217. }
  218. }
  219. void ScriptServer::save_global_classes() {
  220. List<StringName> gc;
  221. get_global_class_list(&gc);
  222. Array gcarr;
  223. for (List<StringName>::Element *E = gc.front(); E; E = E->next()) {
  224. Dictionary d;
  225. d["class"] = E->get();
  226. d["language"] = global_classes[E->get()].language;
  227. d["path"] = global_classes[E->get()].path;
  228. d["base"] = global_classes[E->get()].base;
  229. gcarr.push_back(d);
  230. }
  231. ProjectSettings::get_singleton()->set("_global_script_classes", gcarr);
  232. ProjectSettings::get_singleton()->save();
  233. }
  234. ////////////////////
  235. void ScriptInstance::get_property_state(List<Pair<StringName, Variant> > &state) {
  236. List<PropertyInfo> pinfo;
  237. get_property_list(&pinfo);
  238. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  239. if (E->get().usage & PROPERTY_USAGE_STORAGE) {
  240. Pair<StringName, Variant> p;
  241. p.first = E->get().name;
  242. if (get(p.first, p.second))
  243. state.push_back(p);
  244. }
  245. }
  246. }
  247. Variant ScriptInstance::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
  248. VARIANT_ARGPTRS;
  249. int argc = 0;
  250. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  251. if (argptr[i]->get_type() == Variant::NIL)
  252. break;
  253. argc++;
  254. }
  255. Variant::CallError error;
  256. return call(p_method, argptr, argc, error);
  257. }
  258. void ScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  259. Variant::CallError ce;
  260. call(p_method, p_args, p_argcount, ce); // script may not support multilevel calls
  261. }
  262. void ScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  263. Variant::CallError ce;
  264. call(p_method, p_args, p_argcount, ce); // script may not support multilevel calls
  265. }
  266. void ScriptInstance::property_set_fallback(const StringName &, const Variant &, bool *r_valid) {
  267. if (r_valid)
  268. *r_valid = false;
  269. }
  270. Variant ScriptInstance::property_get_fallback(const StringName &, bool *r_valid) {
  271. if (r_valid)
  272. *r_valid = false;
  273. return Variant();
  274. }
  275. void ScriptInstance::call_multilevel(const StringName &p_method, VARIANT_ARG_DECLARE) {
  276. VARIANT_ARGPTRS;
  277. int argc = 0;
  278. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  279. if (argptr[i]->get_type() == Variant::NIL)
  280. break;
  281. argc++;
  282. }
  283. call_multilevel(p_method, argptr, argc);
  284. }
  285. ScriptInstance::~ScriptInstance() {
  286. }
  287. ScriptCodeCompletionCache *ScriptCodeCompletionCache::singleton = NULL;
  288. ScriptCodeCompletionCache::ScriptCodeCompletionCache() {
  289. singleton = this;
  290. }
  291. void ScriptLanguage::frame() {
  292. }
  293. ScriptDebugger *ScriptDebugger::singleton = NULL;
  294. void ScriptDebugger::set_lines_left(int p_left) {
  295. lines_left = p_left;
  296. }
  297. int ScriptDebugger::get_lines_left() const {
  298. return lines_left;
  299. }
  300. void ScriptDebugger::set_depth(int p_depth) {
  301. depth = p_depth;
  302. }
  303. int ScriptDebugger::get_depth() const {
  304. return depth;
  305. }
  306. void ScriptDebugger::insert_breakpoint(int p_line, const StringName &p_source) {
  307. if (!breakpoints.has(p_line))
  308. breakpoints[p_line] = Set<StringName>();
  309. breakpoints[p_line].insert(p_source);
  310. }
  311. void ScriptDebugger::remove_breakpoint(int p_line, const StringName &p_source) {
  312. if (!breakpoints.has(p_line))
  313. return;
  314. breakpoints[p_line].erase(p_source);
  315. if (breakpoints[p_line].size() == 0)
  316. breakpoints.erase(p_line);
  317. }
  318. bool ScriptDebugger::is_breakpoint(int p_line, const StringName &p_source) const {
  319. if (!breakpoints.has(p_line))
  320. return false;
  321. return breakpoints[p_line].has(p_source);
  322. }
  323. bool ScriptDebugger::is_breakpoint_line(int p_line) const {
  324. return breakpoints.has(p_line);
  325. }
  326. String ScriptDebugger::breakpoint_find_source(const String &p_source) const {
  327. return p_source;
  328. }
  329. void ScriptDebugger::clear_breakpoints() {
  330. breakpoints.clear();
  331. }
  332. void ScriptDebugger::idle_poll() {
  333. }
  334. void ScriptDebugger::line_poll() {
  335. }
  336. void ScriptDebugger::set_break_language(ScriptLanguage *p_lang) {
  337. break_lang = p_lang;
  338. }
  339. ScriptLanguage *ScriptDebugger::get_break_language() const {
  340. return break_lang;
  341. }
  342. ScriptDebugger::ScriptDebugger() {
  343. singleton = this;
  344. lines_left = -1;
  345. depth = -1;
  346. break_lang = NULL;
  347. }
  348. bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  349. if (script->is_placeholder_fallback_enabled())
  350. return false;
  351. if (values.has(p_name)) {
  352. Variant defval;
  353. if (script->get_property_default_value(p_name, defval)) {
  354. if (defval == p_value) {
  355. values.erase(p_name);
  356. return true;
  357. }
  358. }
  359. values[p_name] = p_value;
  360. return true;
  361. } else {
  362. Variant defval;
  363. if (script->get_property_default_value(p_name, defval)) {
  364. if (defval != p_value) {
  365. values[p_name] = p_value;
  366. }
  367. return true;
  368. }
  369. }
  370. return false;
  371. }
  372. bool PlaceHolderScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  373. if (values.has(p_name)) {
  374. r_ret = values[p_name];
  375. return true;
  376. }
  377. if (constants.has(p_name)) {
  378. r_ret = constants[p_name];
  379. return true;
  380. }
  381. if (!script->is_placeholder_fallback_enabled()) {
  382. Variant defval;
  383. if (script->get_property_default_value(p_name, defval)) {
  384. r_ret = defval;
  385. return true;
  386. }
  387. }
  388. return false;
  389. }
  390. void PlaceHolderScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  391. if (script->is_placeholder_fallback_enabled()) {
  392. for (const List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  393. p_properties->push_back(E->get());
  394. }
  395. } else {
  396. for (const List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  397. PropertyInfo pinfo = E->get();
  398. if (!values.has(pinfo.name)) {
  399. pinfo.usage |= PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE;
  400. }
  401. p_properties->push_back(E->get());
  402. }
  403. }
  404. }
  405. Variant::Type PlaceHolderScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  406. if (values.has(p_name)) {
  407. if (r_is_valid)
  408. *r_is_valid = true;
  409. return values[p_name].get_type();
  410. }
  411. if (constants.has(p_name)) {
  412. if (r_is_valid)
  413. *r_is_valid = true;
  414. return constants[p_name].get_type();
  415. }
  416. if (r_is_valid)
  417. *r_is_valid = false;
  418. return Variant::NIL;
  419. }
  420. void PlaceHolderScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  421. if (script->is_placeholder_fallback_enabled())
  422. return;
  423. if (script.is_valid()) {
  424. script->get_script_method_list(p_list);
  425. }
  426. }
  427. bool PlaceHolderScriptInstance::has_method(const StringName &p_method) const {
  428. if (script->is_placeholder_fallback_enabled())
  429. return false;
  430. if (script.is_valid()) {
  431. return script->has_method(p_method);
  432. }
  433. return false;
  434. }
  435. void PlaceHolderScriptInstance::update(const List<PropertyInfo> &p_properties, const Map<StringName, Variant> &p_values) {
  436. Set<StringName> new_values;
  437. for (const List<PropertyInfo>::Element *E = p_properties.front(); E; E = E->next()) {
  438. StringName n = E->get().name;
  439. new_values.insert(n);
  440. if (!values.has(n) || values[n].get_type() != E->get().type) {
  441. if (p_values.has(n))
  442. values[n] = p_values[n];
  443. }
  444. }
  445. properties = p_properties;
  446. List<StringName> to_remove;
  447. for (Map<StringName, Variant>::Element *E = values.front(); E; E = E->next()) {
  448. if (!new_values.has(E->key()))
  449. to_remove.push_back(E->key());
  450. Variant defval;
  451. if (script->get_property_default_value(E->key(), defval)) {
  452. //remove because it's the same as the default value
  453. if (defval == E->get()) {
  454. to_remove.push_back(E->key());
  455. }
  456. }
  457. }
  458. while (to_remove.size()) {
  459. values.erase(to_remove.front()->get());
  460. to_remove.pop_front();
  461. }
  462. if (owner && owner->get_script_instance() == this) {
  463. owner->_change_notify();
  464. }
  465. //change notify
  466. constants.clear();
  467. script->get_constants(&constants);
  468. }
  469. void PlaceHolderScriptInstance::property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid) {
  470. if (script->is_placeholder_fallback_enabled()) {
  471. Map<StringName, Variant>::Element *E = values.find(p_name);
  472. if (E) {
  473. E->value() = p_value;
  474. } else {
  475. values.insert(p_name, p_value);
  476. }
  477. bool found = false;
  478. for (const List<PropertyInfo>::Element *F = properties.front(); F; F = F->next()) {
  479. if (F->get().name == p_name) {
  480. found = true;
  481. break;
  482. }
  483. }
  484. if (!found) {
  485. properties.push_back(PropertyInfo(p_value.get_type(), p_name, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE));
  486. }
  487. }
  488. if (r_valid)
  489. *r_valid = false; // Cannot change the value in either case
  490. }
  491. Variant PlaceHolderScriptInstance::property_get_fallback(const StringName &p_name, bool *r_valid) {
  492. if (script->is_placeholder_fallback_enabled()) {
  493. const Map<StringName, Variant>::Element *E = values.find(p_name);
  494. if (E) {
  495. if (r_valid)
  496. *r_valid = true;
  497. return E->value();
  498. }
  499. E = constants.find(p_name);
  500. if (E) {
  501. if (r_valid)
  502. *r_valid = true;
  503. return E->value();
  504. }
  505. }
  506. if (r_valid)
  507. *r_valid = false;
  508. return Variant();
  509. }
  510. PlaceHolderScriptInstance::PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script, Object *p_owner) :
  511. owner(p_owner),
  512. language(p_language),
  513. script(p_script) {
  514. }
  515. PlaceHolderScriptInstance::~PlaceHolderScriptInstance() {
  516. if (script.is_valid()) {
  517. script->_placeholder_erased(this);
  518. }
  519. }