script_language.cpp 10 KB

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