script_language.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #include "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. for (int i = 0; i < _language_count; i++) {
  84. _languages[i]->init();
  85. }
  86. }
  87. void ScriptServer::finish_languages() {
  88. for (int i = 0; i < _language_count; i++) {
  89. _languages[i]->finish();
  90. }
  91. }
  92. void ScriptServer::set_reload_scripts_on_save(bool p_enable) {
  93. reload_scripts_on_save = p_enable;
  94. }
  95. bool ScriptServer::is_reload_scripts_on_save_enabled() {
  96. return reload_scripts_on_save;
  97. }
  98. void ScriptServer::thread_enter() {
  99. for (int i = 0; i < _language_count; i++) {
  100. _languages[i]->thread_enter();
  101. }
  102. }
  103. void ScriptServer::thread_exit() {
  104. for (int i = 0; i < _language_count; i++) {
  105. _languages[i]->thread_exit();
  106. }
  107. }
  108. void ScriptInstance::get_property_state(List<Pair<StringName, Variant> > &state) {
  109. List<PropertyInfo> pinfo;
  110. get_property_list(&pinfo);
  111. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  112. if (E->get().usage & PROPERTY_USAGE_STORAGE) {
  113. Pair<StringName, Variant> p;
  114. p.first = E->get().name;
  115. if (get(p.first, p.second))
  116. state.push_back(p);
  117. }
  118. }
  119. }
  120. Variant ScriptInstance::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
  121. VARIANT_ARGPTRS;
  122. int argc = 0;
  123. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  124. if (argptr[i]->get_type() == Variant::NIL)
  125. break;
  126. argc++;
  127. }
  128. Variant::CallError error;
  129. return call(p_method, argptr, argc, error);
  130. }
  131. void ScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  132. Variant::CallError ce;
  133. call(p_method, p_args, p_argcount, ce); // script may not support multilevel calls
  134. }
  135. void ScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  136. Variant::CallError ce;
  137. call(p_method, p_args, p_argcount, ce); // script may not support multilevel calls
  138. }
  139. void ScriptInstance::call_multilevel(const StringName &p_method, VARIANT_ARG_DECLARE) {
  140. VARIANT_ARGPTRS;
  141. int argc = 0;
  142. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  143. if (argptr[i]->get_type() == Variant::NIL)
  144. break;
  145. argc++;
  146. }
  147. call_multilevel(p_method, argptr, argc);
  148. }
  149. ScriptInstance::~ScriptInstance() {
  150. }
  151. ScriptCodeCompletionCache *ScriptCodeCompletionCache::singleton = NULL;
  152. ScriptCodeCompletionCache::ScriptCodeCompletionCache() {
  153. singleton = this;
  154. }
  155. void ScriptLanguage::frame() {
  156. }
  157. ScriptDebugger *ScriptDebugger::singleton = NULL;
  158. void ScriptDebugger::set_lines_left(int p_left) {
  159. lines_left = p_left;
  160. }
  161. int ScriptDebugger::get_lines_left() const {
  162. return lines_left;
  163. }
  164. void ScriptDebugger::set_depth(int p_depth) {
  165. depth = p_depth;
  166. }
  167. int ScriptDebugger::get_depth() const {
  168. return depth;
  169. }
  170. void ScriptDebugger::insert_breakpoint(int p_line, const StringName &p_source) {
  171. if (!breakpoints.has(p_line))
  172. breakpoints[p_line] = Set<StringName>();
  173. breakpoints[p_line].insert(p_source);
  174. }
  175. void ScriptDebugger::remove_breakpoint(int p_line, const StringName &p_source) {
  176. if (!breakpoints.has(p_line))
  177. return;
  178. breakpoints[p_line].erase(p_source);
  179. if (breakpoints[p_line].size() == 0)
  180. breakpoints.erase(p_line);
  181. }
  182. bool ScriptDebugger::is_breakpoint(int p_line, const StringName &p_source) const {
  183. if (!breakpoints.has(p_line))
  184. return false;
  185. return breakpoints[p_line].has(p_source);
  186. }
  187. bool ScriptDebugger::is_breakpoint_line(int p_line) const {
  188. return breakpoints.has(p_line);
  189. }
  190. String ScriptDebugger::breakpoint_find_source(const String &p_source) const {
  191. return p_source;
  192. }
  193. void ScriptDebugger::clear_breakpoints() {
  194. breakpoints.clear();
  195. }
  196. void ScriptDebugger::idle_poll() {
  197. }
  198. void ScriptDebugger::line_poll() {
  199. }
  200. void ScriptDebugger::set_break_language(ScriptLanguage *p_lang) {
  201. break_lang = p_lang;
  202. }
  203. ScriptLanguage *ScriptDebugger::get_break_language() const {
  204. return break_lang;
  205. }
  206. ScriptDebugger::ScriptDebugger() {
  207. singleton = this;
  208. lines_left = -1;
  209. depth = -1;
  210. break_lang = NULL;
  211. update_frequency = GLOBAL_GET("debug/settings/performance/update_frequency_msec");
  212. }
  213. bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  214. if (values.has(p_name)) {
  215. Variant defval;
  216. if (script->get_property_default_value(p_name, defval)) {
  217. if (defval == p_value) {
  218. values.erase(p_name);
  219. return true;
  220. }
  221. }
  222. values[p_name] = p_value;
  223. return true;
  224. } else {
  225. Variant defval;
  226. if (script->get_property_default_value(p_name, defval)) {
  227. if (defval != p_value) {
  228. values[p_name] = p_value;
  229. }
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. bool PlaceHolderScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  236. if (values.has(p_name)) {
  237. r_ret = values[p_name];
  238. return true;
  239. }
  240. Variant defval;
  241. if (script->get_property_default_value(p_name, defval)) {
  242. r_ret = defval;
  243. return true;
  244. }
  245. return false;
  246. }
  247. void PlaceHolderScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  248. for (const List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  249. PropertyInfo pinfo = E->get();
  250. if (!values.has(pinfo.name)) {
  251. pinfo.usage |= PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE;
  252. }
  253. p_properties->push_back(E->get());
  254. }
  255. }
  256. Variant::Type PlaceHolderScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  257. if (values.has(p_name)) {
  258. if (r_is_valid)
  259. *r_is_valid = true;
  260. return values[p_name].get_type();
  261. }
  262. if (r_is_valid)
  263. *r_is_valid = false;
  264. return Variant::NIL;
  265. }
  266. void PlaceHolderScriptInstance::update(const List<PropertyInfo> &p_properties, const Map<StringName, Variant> &p_values) {
  267. Set<StringName> new_values;
  268. for (const List<PropertyInfo>::Element *E = p_properties.front(); E; E = E->next()) {
  269. StringName n = E->get().name;
  270. new_values.insert(n);
  271. if (!values.has(n) || values[n].get_type() != E->get().type) {
  272. if (p_values.has(n))
  273. values[n] = p_values[n];
  274. }
  275. }
  276. properties = p_properties;
  277. List<StringName> to_remove;
  278. for (Map<StringName, Variant>::Element *E = values.front(); E; E = E->next()) {
  279. if (!new_values.has(E->key()))
  280. to_remove.push_back(E->key());
  281. Variant defval;
  282. if (script->get_property_default_value(E->key(), defval)) {
  283. //remove because it's the same as the default value
  284. if (defval == E->get()) {
  285. to_remove.push_back(E->key());
  286. }
  287. }
  288. }
  289. while (to_remove.size()) {
  290. values.erase(to_remove.front()->get());
  291. to_remove.pop_front();
  292. }
  293. if (owner && owner->get_script_instance() == this) {
  294. owner->_change_notify();
  295. }
  296. //change notify
  297. }
  298. PlaceHolderScriptInstance::PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script, Object *p_owner) :
  299. owner(p_owner),
  300. language(p_language),
  301. script(p_script) {
  302. }
  303. PlaceHolderScriptInstance::~PlaceHolderScriptInstance() {
  304. if (script.is_valid()) {
  305. script->_placeholder_erased(this);
  306. }
  307. }