script_language.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*************************************************************************/
  2. /* script_language.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "script_language.h"
  30. ScriptLanguage *ScriptServer::_languages[MAX_LANGUAGES];
  31. int ScriptServer::_language_count=0;
  32. bool ScriptServer::scripting_enabled=true;
  33. void Script::_notification( int p_what) {
  34. if (p_what==NOTIFICATION_POSTINITIALIZE) {
  35. if (ScriptDebugger::get_singleton())
  36. ScriptDebugger::get_singleton()->set_break_language(get_language());
  37. }
  38. }
  39. void Script::_bind_methods() {
  40. ObjectTypeDB::bind_method(_MD("can_instance"),&Script::can_instance);
  41. //ObjectTypeDB::bind_method(_MD("instance_create","base_object"),&Script::instance_create);
  42. ObjectTypeDB::bind_method(_MD("instance_has","base_object"),&Script::instance_has);
  43. ObjectTypeDB::bind_method(_MD("has_source_code"),&Script::has_source_code);
  44. ObjectTypeDB::bind_method(_MD("get_source_code"),&Script::get_source_code);
  45. ObjectTypeDB::bind_method(_MD("set_source_code","source"),&Script::set_source_code);
  46. ObjectTypeDB::bind_method(_MD("reload"),&Script::reload);
  47. }
  48. void ScriptServer::set_scripting_enabled(bool p_enabled) {
  49. scripting_enabled=p_enabled;
  50. }
  51. bool ScriptServer::is_scripting_enabled() {
  52. return scripting_enabled;
  53. }
  54. int ScriptServer::get_language_count() {
  55. return _language_count;
  56. }
  57. ScriptLanguage* ScriptServer::get_language(int p_idx) {
  58. ERR_FAIL_INDEX_V(p_idx,_language_count,NULL);
  59. return _languages[p_idx];
  60. }
  61. void ScriptServer::register_language(ScriptLanguage *p_language) {
  62. ERR_FAIL_COND( _language_count >= MAX_LANGUAGES );
  63. _languages[_language_count++]=p_language;
  64. }
  65. void ScriptServer::init_languages() {
  66. for(int i=0;i<_language_count;i++) {
  67. _languages[i]->init();
  68. }
  69. }
  70. Variant ScriptInstance::call(const StringName& p_method,VARIANT_ARG_DECLARE) {
  71. VARIANT_ARGPTRS;
  72. int argc=0;
  73. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  74. if (argptr[i]->get_type()==Variant::NIL)
  75. break;
  76. argc++;
  77. }
  78. Variant::CallError error;
  79. return call(p_method,argptr,argc,error);
  80. }
  81. void ScriptInstance::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount) {
  82. Variant::CallError ce;
  83. call(p_method,p_args,p_argcount,ce); // script may not support multilevel calls
  84. }
  85. void ScriptInstance::call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount) {
  86. Variant::CallError ce;
  87. call(p_method,p_args,p_argcount,ce); // script may not support multilevel calls
  88. }
  89. void ScriptInstance::call_multilevel(const StringName& p_method,VARIANT_ARG_DECLARE) {
  90. VARIANT_ARGPTRS;
  91. int argc=0;
  92. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  93. if (argptr[i]->get_type()==Variant::NIL)
  94. break;
  95. argc++;
  96. }
  97. Variant::CallError error;
  98. call_multilevel(p_method,argptr,argc);
  99. }
  100. ScriptInstance::~ScriptInstance() {
  101. }
  102. ScriptCodeCompletionCache *ScriptCodeCompletionCache::singleton=NULL;
  103. ScriptCodeCompletionCache::ScriptCodeCompletionCache() {
  104. singleton=this;
  105. }
  106. void ScriptLanguage::frame() {
  107. }
  108. ScriptDebugger * ScriptDebugger::singleton=NULL;
  109. void ScriptDebugger::set_lines_left(int p_left) {
  110. lines_left=p_left;
  111. }
  112. int ScriptDebugger::get_lines_left() const {
  113. return lines_left;
  114. }
  115. void ScriptDebugger::set_depth(int p_depth) {
  116. depth=p_depth;
  117. }
  118. int ScriptDebugger::get_depth() const {
  119. return depth;
  120. }
  121. void ScriptDebugger::insert_breakpoint(int p_line, const StringName& p_source) {
  122. if (!breakpoints.has(p_line))
  123. breakpoints[p_line]=Set<StringName>();
  124. breakpoints[p_line].insert(p_source);
  125. }
  126. void ScriptDebugger::remove_breakpoint(int p_line, const StringName& p_source) {
  127. if (!breakpoints.has(p_line))
  128. return;
  129. breakpoints[p_line].erase(p_source);
  130. if (breakpoints[p_line].size()==0)
  131. breakpoints.erase(p_line);
  132. }
  133. bool ScriptDebugger::is_breakpoint(int p_line,const StringName& p_source) const {
  134. if (!breakpoints.has(p_line))
  135. return false;
  136. return breakpoints[p_line].has(p_source);
  137. }
  138. bool ScriptDebugger::is_breakpoint_line(int p_line) const {
  139. return breakpoints.has(p_line);
  140. }
  141. String ScriptDebugger::breakpoint_find_source(const String& p_source) const {
  142. return p_source;
  143. }
  144. void ScriptDebugger::clear_breakpoints() {
  145. breakpoints.clear();
  146. }
  147. void ScriptDebugger::idle_poll() {
  148. }
  149. void ScriptDebugger::line_poll() {
  150. }
  151. void ScriptDebugger::set_break_language(ScriptLanguage *p_lang) {
  152. break_lang=p_lang;
  153. }
  154. ScriptLanguage* ScriptDebugger::get_break_language() const{
  155. return break_lang;
  156. }
  157. ScriptDebugger::ScriptDebugger() {
  158. singleton=this;
  159. lines_left=-1;
  160. depth=-1;
  161. break_lang=NULL;
  162. }
  163. bool PlaceHolderScriptInstance::set(const StringName& p_name, const Variant& p_value) {
  164. if (values.has(p_name)) {
  165. values[p_name]=p_value;
  166. return true;
  167. }
  168. return false;
  169. }
  170. bool PlaceHolderScriptInstance::get(const StringName& p_name, Variant &r_ret) const {
  171. if (values.has(p_name)) {
  172. r_ret=values[p_name];
  173. return true;
  174. } return false;
  175. }
  176. void PlaceHolderScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  177. for(const List<PropertyInfo>::Element *E=properties.front();E;E=E->next()) {
  178. p_properties->push_back(E->get());
  179. }
  180. }
  181. Variant::Type PlaceHolderScriptInstance::get_property_type(const StringName& p_name,bool *r_is_valid) const {
  182. if (values.has(p_name)) {
  183. if (r_is_valid)
  184. *r_is_valid=true;
  185. return values[p_name].get_type();
  186. }
  187. if (r_is_valid)
  188. *r_is_valid=false;
  189. return Variant::NIL;
  190. }
  191. void PlaceHolderScriptInstance::update(const List<PropertyInfo> &p_properties,const Map<StringName,Variant>& p_values) {
  192. Set<StringName> new_values;
  193. for(const List<PropertyInfo>::Element *E=p_properties.front();E;E=E->next()) {
  194. StringName n = E->get().name;
  195. new_values.insert(n);
  196. if (!values.has(n) || values[n].get_type()!=E->get().type) {
  197. if (p_values.has(n))
  198. values[n]=p_values[n];
  199. }
  200. }
  201. properties=p_properties;
  202. List<StringName> to_remove;
  203. for(Map<StringName,Variant>::Element *E=values.front();E;E=E->next()) {
  204. if (!new_values.has(E->key()))
  205. to_remove.push_back(E->key());
  206. }
  207. while(to_remove.size()) {
  208. values.erase(to_remove.front()->get());
  209. to_remove.pop_front();
  210. }
  211. if (owner && owner->get_script_instance()==this) {
  212. owner->_change_notify();
  213. }
  214. //change notify
  215. }
  216. PlaceHolderScriptInstance::PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script,Object *p_owner) {
  217. language=p_language;
  218. script=p_script;
  219. owner=p_owner;
  220. }
  221. PlaceHolderScriptInstance::~PlaceHolderScriptInstance() {
  222. if (script.is_valid()) {
  223. script->_placeholder_erased(this);
  224. }
  225. }