script_language.cpp 15 KB

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