script_language.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*************************************************************************/
  2. /* script_language.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. ScriptLanguage *ScriptServer::_languages[MAX_LANGUAGES];
  32. int ScriptServer::_language_count = 0;
  33. bool ScriptServer::scripting_enabled = true;
  34. bool ScriptServer::reload_scripts_on_save = false;
  35. ScriptEditRequestFunction ScriptServer::edit_request_func = NULL;
  36. void Script::_notification(int p_what) {
  37. if (p_what == NOTIFICATION_POSTINITIALIZE) {
  38. if (ScriptDebugger::get_singleton())
  39. ScriptDebugger::get_singleton()->set_break_language(get_language());
  40. }
  41. }
  42. void Script::_bind_methods() {
  43. ClassDB::bind_method(D_METHOD("can_instance"), &Script::can_instance);
  44. //ClassDB::bind_method(D_METHOD("instance_create","base_object"),&Script::instance_create);
  45. ClassDB::bind_method(D_METHOD("instance_has", "base_object"), &Script::instance_has);
  46. ClassDB::bind_method(D_METHOD("has_source_code"), &Script::has_source_code);
  47. ClassDB::bind_method(D_METHOD("get_source_code"), &Script::get_source_code);
  48. ClassDB::bind_method(D_METHOD("set_source_code", "source"), &Script::set_source_code);
  49. ClassDB::bind_method(D_METHOD("reload", "keep_state"), &Script::reload, DEFVAL(false));
  50. ClassDB::bind_method(D_METHOD("get_base_script"), &Script::get_base_script);
  51. ClassDB::bind_method(D_METHOD("get_instance_base_type"), &Script::get_instance_base_type);
  52. ClassDB::bind_method(D_METHOD("has_script_signal", "signal_name"), &Script::has_script_signal);
  53. ClassDB::bind_method(D_METHOD("is_tool"), &Script::is_tool);
  54. ADD_PROPERTY(PropertyInfo(Variant::STRING, "source_code", PROPERTY_HINT_NONE, "", 0), "set_source_code", "get_source_code");
  55. }
  56. void ScriptServer::set_scripting_enabled(bool p_enabled) {
  57. scripting_enabled = p_enabled;
  58. }
  59. bool ScriptServer::is_scripting_enabled() {
  60. return scripting_enabled;
  61. }
  62. ScriptLanguage *ScriptServer::get_language(int p_idx) {
  63. ERR_FAIL_INDEX_V(p_idx, _language_count, NULL);
  64. return _languages[p_idx];
  65. }
  66. void ScriptServer::register_language(ScriptLanguage *p_language) {
  67. ERR_FAIL_COND(_language_count >= MAX_LANGUAGES);
  68. _languages[_language_count++] = p_language;
  69. }
  70. void ScriptServer::unregister_language(ScriptLanguage *p_language) {
  71. for (int i = 0; i < _language_count; i++) {
  72. if (_languages[i] == p_language) {
  73. _language_count--;
  74. if (i < _language_count) {
  75. SWAP(_languages[i], _languages[_language_count]);
  76. }
  77. return;
  78. }
  79. }
  80. }
  81. void ScriptServer::init_languages() {
  82. for (int i = 0; i < _language_count; i++) {
  83. _languages[i]->init();
  84. }
  85. }
  86. void ScriptServer::finish_languages() {
  87. for (int i = 0; i < _language_count; i++) {
  88. _languages[i]->finish();
  89. }
  90. }
  91. void ScriptServer::set_reload_scripts_on_save(bool p_enable) {
  92. reload_scripts_on_save = p_enable;
  93. }
  94. bool ScriptServer::is_reload_scripts_on_save_enabled() {
  95. return reload_scripts_on_save;
  96. }
  97. void ScriptServer::thread_enter() {
  98. for (int i = 0; i < _language_count; i++) {
  99. _languages[i]->thread_enter();
  100. }
  101. }
  102. void ScriptServer::thread_exit() {
  103. for (int i = 0; i < _language_count; i++) {
  104. _languages[i]->thread_exit();
  105. }
  106. }
  107. void ScriptInstance::get_property_state(List<Pair<StringName, Variant> > &state) {
  108. List<PropertyInfo> pinfo;
  109. get_property_list(&pinfo);
  110. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  111. if (E->get().usage & PROPERTY_USAGE_STORAGE) {
  112. Pair<StringName, Variant> p;
  113. p.first = E->get().name;
  114. if (get(p.first, p.second))
  115. state.push_back(p);
  116. }
  117. }
  118. }
  119. Variant ScriptInstance::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
  120. VARIANT_ARGPTRS;
  121. int argc = 0;
  122. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  123. if (argptr[i]->get_type() == Variant::NIL)
  124. break;
  125. argc++;
  126. }
  127. Variant::CallError error;
  128. return call(p_method, argptr, argc, error);
  129. }
  130. void ScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  131. Variant::CallError ce;
  132. call(p_method, p_args, p_argcount, ce); // script may not support multilevel calls
  133. }
  134. void ScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  135. Variant::CallError ce;
  136. call(p_method, p_args, p_argcount, ce); // script may not support multilevel calls
  137. }
  138. void ScriptInstance::call_multilevel(const StringName &p_method, VARIANT_ARG_DECLARE) {
  139. VARIANT_ARGPTRS;
  140. int argc = 0;
  141. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  142. if (argptr[i]->get_type() == Variant::NIL)
  143. break;
  144. argc++;
  145. }
  146. call_multilevel(p_method, argptr, argc);
  147. }
  148. ScriptInstance::~ScriptInstance() {
  149. }
  150. ScriptCodeCompletionCache *ScriptCodeCompletionCache::singleton = NULL;
  151. ScriptCodeCompletionCache::ScriptCodeCompletionCache() {
  152. singleton = this;
  153. }
  154. void ScriptLanguage::frame() {
  155. }
  156. ScriptDebugger *ScriptDebugger::singleton = NULL;
  157. void ScriptDebugger::set_lines_left(int p_left) {
  158. lines_left = p_left;
  159. }
  160. int ScriptDebugger::get_lines_left() const {
  161. return lines_left;
  162. }
  163. void ScriptDebugger::set_depth(int p_depth) {
  164. depth = p_depth;
  165. }
  166. int ScriptDebugger::get_depth() const {
  167. return depth;
  168. }
  169. void ScriptDebugger::insert_breakpoint(int p_line, const StringName &p_source) {
  170. if (!breakpoints.has(p_line))
  171. breakpoints[p_line] = Set<StringName>();
  172. breakpoints[p_line].insert(p_source);
  173. }
  174. void ScriptDebugger::remove_breakpoint(int p_line, const StringName &p_source) {
  175. if (!breakpoints.has(p_line))
  176. return;
  177. breakpoints[p_line].erase(p_source);
  178. if (breakpoints[p_line].size() == 0)
  179. breakpoints.erase(p_line);
  180. }
  181. bool ScriptDebugger::is_breakpoint(int p_line, const StringName &p_source) const {
  182. if (!breakpoints.has(p_line))
  183. return false;
  184. return breakpoints[p_line].has(p_source);
  185. }
  186. bool ScriptDebugger::is_breakpoint_line(int p_line) const {
  187. return breakpoints.has(p_line);
  188. }
  189. String ScriptDebugger::breakpoint_find_source(const String &p_source) const {
  190. return p_source;
  191. }
  192. void ScriptDebugger::clear_breakpoints() {
  193. breakpoints.clear();
  194. }
  195. void ScriptDebugger::idle_poll() {
  196. }
  197. void ScriptDebugger::line_poll() {
  198. }
  199. void ScriptDebugger::set_break_language(ScriptLanguage *p_lang) {
  200. break_lang = p_lang;
  201. }
  202. ScriptLanguage *ScriptDebugger::get_break_language() const {
  203. return break_lang;
  204. }
  205. ScriptDebugger::ScriptDebugger() {
  206. singleton = this;
  207. lines_left = -1;
  208. depth = -1;
  209. break_lang = NULL;
  210. }
  211. bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  212. if (values.has(p_name)) {
  213. Variant defval;
  214. if (script->get_property_default_value(p_name, defval)) {
  215. if (defval == p_value) {
  216. values.erase(p_name);
  217. return true;
  218. }
  219. }
  220. values[p_name] = p_value;
  221. return true;
  222. } else {
  223. Variant defval;
  224. if (script->get_property_default_value(p_name, defval)) {
  225. if (defval != p_value) {
  226. values[p_name] = p_value;
  227. }
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. bool PlaceHolderScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  234. if (values.has(p_name)) {
  235. r_ret = values[p_name];
  236. return true;
  237. }
  238. Variant defval;
  239. if (script->get_property_default_value(p_name, defval)) {
  240. r_ret = defval;
  241. return true;
  242. }
  243. return false;
  244. }
  245. void PlaceHolderScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  246. for (const List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  247. PropertyInfo pinfo = E->get();
  248. if (!values.has(pinfo.name)) {
  249. pinfo.usage |= PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE;
  250. }
  251. p_properties->push_back(E->get());
  252. }
  253. }
  254. Variant::Type PlaceHolderScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  255. if (values.has(p_name)) {
  256. if (r_is_valid)
  257. *r_is_valid = true;
  258. return values[p_name].get_type();
  259. }
  260. if (r_is_valid)
  261. *r_is_valid = false;
  262. return Variant::NIL;
  263. }
  264. void PlaceHolderScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  265. if (script.is_valid()) {
  266. script->get_script_method_list(p_list);
  267. }
  268. }
  269. bool PlaceHolderScriptInstance::has_method(const StringName &p_method) const {
  270. if (script.is_valid()) {
  271. return script->has_method(p_method);
  272. }
  273. return false;
  274. }
  275. void PlaceHolderScriptInstance::update(const List<PropertyInfo> &p_properties, const Map<StringName, Variant> &p_values) {
  276. Set<StringName> new_values;
  277. for (const List<PropertyInfo>::Element *E = p_properties.front(); E; E = E->next()) {
  278. StringName n = E->get().name;
  279. new_values.insert(n);
  280. if (!values.has(n) || values[n].get_type() != E->get().type) {
  281. if (p_values.has(n))
  282. values[n] = p_values[n];
  283. }
  284. }
  285. properties = p_properties;
  286. List<StringName> to_remove;
  287. for (Map<StringName, Variant>::Element *E = values.front(); E; E = E->next()) {
  288. if (!new_values.has(E->key()))
  289. to_remove.push_back(E->key());
  290. Variant defval;
  291. if (script->get_property_default_value(E->key(), defval)) {
  292. //remove because it's the same as the default value
  293. if (defval == E->get()) {
  294. to_remove.push_back(E->key());
  295. }
  296. }
  297. }
  298. while (to_remove.size()) {
  299. values.erase(to_remove.front()->get());
  300. to_remove.pop_front();
  301. }
  302. if (owner && owner->get_script_instance() == this) {
  303. owner->_change_notify();
  304. }
  305. //change notify
  306. }
  307. PlaceHolderScriptInstance::PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script, Object *p_owner) :
  308. owner(p_owner),
  309. language(p_language),
  310. script(p_script) {
  311. }
  312. PlaceHolderScriptInstance::~PlaceHolderScriptInstance() {
  313. if (script.is_valid()) {
  314. script->_placeholder_erased(this);
  315. }
  316. }