script_language.h
1 /*************************************************************************/
2 /* script_language.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 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 #ifndef SCRIPT_LANGUAGE_H
30 #define SCRIPT_LANGUAGE_H
31 
32 #include "resource.h"
33 #include "map.h"
34 #include "pair.h"
39 class ScriptLanguage;
40 
41 class ScriptServer {
42  enum {
43 
44  MAX_LANGUAGES=4
45  };
46 
47  static ScriptLanguage *_languages[MAX_LANGUAGES];
48  static int _language_count;
49  static bool scripting_enabled;
50 public:
51 
52  static void set_scripting_enabled(bool p_enabled);
53  static bool is_scripting_enabled();
54  static int get_language_count();
55  static ScriptLanguage *get_language(int p_idx);
56  static void register_language(ScriptLanguage *p_language);
57 
58  static void init_languages();
59 };
60 
61 
62 
63 class ScriptInstance;
65 
66 class Script : public Resource {
67 
68  OBJ_TYPE( Script, Resource );
69  OBJ_SAVE_TYPE( Script );
70 
71 protected:
72 
73  void _notification( int p_what);
74  static void _bind_methods();
75 
76 friend class PlaceHolderScriptInstance;
77  virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {}
78 public:
79 
80  virtual bool can_instance() const=0;
81 
82  virtual StringName get_instance_base_type() const=0; // this may not work in all scripts, will return empty if so
83  virtual ScriptInstance* instance_create(Object *p_this)=0;
84  virtual bool instance_has(const Object *p_this) const=0;
85 
86 
87  virtual bool has_source_code() const=0;
88  virtual String get_source_code() const=0;
89  virtual void set_source_code(const String& p_code)=0;
90  virtual Error reload()=0;
91 
92  virtual bool is_tool() const=0;
93 
94  virtual String get_node_type() const=0;
95 
96  virtual ScriptLanguage *get_language() const=0;
97 
98  virtual bool has_script_signal(const StringName& p_signal) const=0;
99  virtual void get_script_signal_list(List<MethodInfo> *r_signals) const=0;
100 
101  virtual bool get_property_default_value(const StringName& p_property,Variant& r_value) const=0;
102 
103  virtual void update_exports() {} //editor tool
104 
105 
106  Script() {}
107 };
108 
110 public:
111  virtual bool set(const StringName& p_name, const Variant& p_value)=0;
112  virtual bool get(const StringName& p_name, Variant &r_ret) const=0;
113  virtual void get_property_list(List<PropertyInfo> *p_properties) const=0;
114  virtual Variant::Type get_property_type(const StringName& p_name,bool *r_is_valid=NULL) const=0;
115 
116  virtual void get_property_state(List<Pair<StringName,Variant> > &state);
117 
118  virtual void get_method_list(List<MethodInfo> *p_list) const=0;
119  virtual bool has_method(const StringName& p_method) const=0;
120  virtual Variant call(const StringName& p_method,VARIANT_ARG_LIST);
121  virtual Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error)=0;
122  virtual void call_multilevel(const StringName& p_method,VARIANT_ARG_LIST);
123  virtual void call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount);
124  virtual void call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount);
125  virtual void notification(int p_notification)=0;
126 
127 
128  virtual Ref<Script> get_script() const=0;
129 
130  virtual ScriptLanguage *get_language()=0;
131  virtual ~ScriptInstance();
132 };
133 
135 
136  static ScriptCodeCompletionCache *singleton;
137 public:
138 
139  virtual RES get_cached_resource(const String& p_path)=0;
140 
141  static ScriptCodeCompletionCache* get_sigleton() { return singleton; }
142 
144 
145 };
146 
148 public:
149 
150  virtual String get_name() const=0;
151 
152  /* LANGUAGE FUNCTIONS */
153  virtual void init()=0;
154  virtual String get_type() const=0;
155  virtual String get_extension() const=0;
156  virtual Error execute_file(const String& p_path) =0;
157  virtual void finish()=0;
158 
159  /* EDITOR FUNCTIONS */
160  virtual void get_reserved_words(List<String> *p_words) const=0;
161  virtual void get_comment_delimiters(List<String> *p_delimiters) const=0;
162  virtual void get_string_delimiters(List<String> *p_delimiters) const=0;
163  virtual String get_template(const String& p_class_name, const String& p_base_class_name) const=0;
164  virtual bool validate(const String& p_script, int &r_line_error,int &r_col_error,String& r_test_error, const String& p_path="",List<String> *r_functions=NULL) const=0;
165  virtual Script *create_script() const=0;
166  virtual bool has_named_classes() const=0;
167  virtual int find_function(const String& p_function,const String& p_code) const=0;
168  virtual String make_function(const String& p_class,const String& p_name,const StringArray& p_args) const=0;
169  virtual Error complete_code(const String& p_code, const String& p_base_path, Object*p_owner,List<String>* r_options,String& r_call_hint) { return ERR_UNAVAILABLE; }
170  virtual void auto_indent_code(String& p_code,int p_from_line,int p_to_line) const=0;
171  virtual void add_global_constant(const StringName& p_variable,const Variant& p_value)=0;
172 
173  /* DEBUGGER FUNCTIONS */
174 
175  virtual String debug_get_error() const=0;
176  virtual int debug_get_stack_level_count() const=0;
177  virtual int debug_get_stack_level_line(int p_level) const=0;
178  virtual String debug_get_stack_level_function(int p_level) const=0;
179  virtual String debug_get_stack_level_source(int p_level) const=0;
180  virtual void debug_get_stack_level_locals(int p_level,List<String> *p_locals, List<Variant> *p_values, int p_max_subitems=-1,int p_max_depth=-1)=0;
181  virtual void debug_get_stack_level_members(int p_level,List<String> *p_members, List<Variant> *p_values, int p_max_subitems=-1,int p_max_depth=-1)=0;
182  virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems=-1,int p_max_depth=-1)=0;
183  virtual String debug_parse_stack_level_expression(int p_level,const String& p_expression,int p_max_subitems=-1,int p_max_depth=-1)=0;
184 
185  struct StackInfo {
186  Ref<Script> script;
187  int line;
188  };
189 
190  virtual Vector<StackInfo> debug_get_current_stack_info() { return Vector<StackInfo>(); }
191 
192  /* LOADER FUNCTIONS */
193 
194  virtual void get_recognized_extensions(List<String> *p_extensions) const=0;
195  virtual void get_public_functions(List<MethodInfo> *p_functions) const=0;
196  virtual void get_public_constants(List<Pair<String,Variant> > *p_constants) const=0;
197 
198  virtual void frame();
199 
200  virtual ~ScriptLanguage() {};
201 };
202 
203 extern uint8_t script_encryption_key[32];
204 
206 
207  Object *owner;
208  List<PropertyInfo> properties;
210  ScriptLanguage *language;
211  Ref<Script> script;
212 
213 public:
214  virtual bool set(const StringName& p_name, const Variant& p_value);
215  virtual bool get(const StringName& p_name, Variant &r_ret) const;
216  virtual void get_property_list(List<PropertyInfo> *p_properties) const;
217  virtual Variant::Type get_property_type(const StringName& p_name,bool *r_is_valid=NULL) const;
218 
219  virtual void get_method_list(List<MethodInfo> *p_list) const {}
220  virtual bool has_method(const StringName& p_method) const { return false; }
221  virtual Variant call(const StringName& p_method,VARIANT_ARG_LIST) { return Variant();}
222  virtual Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) { r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; return Variant(); }
223  //virtual void call_multilevel(const StringName& p_method,VARIANT_ARG_LIST) { return Variant(); }
224  //virtual void call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) { return Variant(); }
225  virtual void notification(int p_notification) {}
226 
227 
228  virtual Ref<Script> get_script() const { return script; }
229 
230  virtual ScriptLanguage *get_language() { return language; }
231 
232  Object *get_owner() { return owner; }
233 
234  void update(const List<PropertyInfo> &p_properties,const Map<StringName,Variant>& p_values); //likely changed in editor
235 
236  PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script,Object *p_owner);
238 
239 
240 };
241 
243 
244  int lines_left;
245  int depth;
246 
247  static ScriptDebugger * singleton;
248  Map<int,Set<StringName> > breakpoints;
249 
250  ScriptLanguage *break_lang;
251 public:
252 
253  typedef void (*RequestSceneTreeMessageFunc)(void *);
254 
255  struct LiveEditFuncs {
256 
257  void *udata;
258  void (*node_path_func)(void *,const NodePath &p_path,int p_id);
259  void (*res_path_func)(void *,const String &p_path,int p_id);
260 
261  void (*node_set_func)(void *,int p_id,const StringName& p_prop,const Variant& p_value);
262  void (*node_set_res_func)(void *,int p_id,const StringName& p_prop,const String& p_value);
263  void (*node_call_func)(void *,int p_id,const StringName& p_method,VARIANT_ARG_DECLARE);
264  void (*res_set_func)(void *,int p_id,const StringName& p_prop,const Variant& p_value);
265  void (*res_set_res_func)(void *,int p_id,const StringName& p_prop,const String& p_value);
266  void (*res_call_func)(void *,int p_id,const StringName& p_method,VARIANT_ARG_DECLARE);
267  void (*root_func)(void*, const NodePath& p_scene_path,const String& p_scene_from);
268 
269  void (*tree_create_node_func)(void*,const NodePath& p_parent,const String& p_type,const String& p_name);
270  void (*tree_instance_node_func)(void*,const NodePath& p_parent,const String& p_path,const String& p_name);
271  void (*tree_remove_node_func)(void*,const NodePath& p_at);
272  void (*tree_remove_and_keep_node_func)(void*,const NodePath& p_at,ObjectID p_keep_id);
273  void (*tree_restore_node_func)(void*,ObjectID p_id,const NodePath& p_at,int p_at_pos);
274  void (*tree_duplicate_node_func)(void*,const NodePath& p_at,const String& p_new_name);
275  void (*tree_reparent_node_func)(void*,const NodePath& p_at,const NodePath& p_new_place,const String& p_new_name,int p_at_pos);
276 
277  };
278 
279 
280 
281  _FORCE_INLINE_ static ScriptDebugger * get_singleton() { return singleton; }
282  void set_lines_left(int p_left);
283  int get_lines_left() const;
284 
285  void set_depth(int p_depth);
286  int get_depth() const;
287 
288  String breakpoint_find_source(const String& p_source) const;
289  void insert_breakpoint(int p_line, const StringName& p_source);
290  void remove_breakpoint(int p_line, const StringName& p_source);
291  bool is_breakpoint(int p_line,const StringName& p_source) const;
292  bool is_breakpoint_line(int p_line) const;
293  void clear_breakpoints();
294 
295 
296  virtual void debug(ScriptLanguage *p_script,bool p_can_continue=true)=0;
297  virtual void idle_poll();
298  virtual void line_poll();
299 
300 
301  void set_break_language(ScriptLanguage *p_lang);
302  ScriptLanguage* get_break_language() const;
303 
304  virtual void send_message(const String& p_message, const Array& p_args)=0;
305 
306  virtual bool is_remote() const { return false; }
307  virtual void request_quit() {}
308 
309  virtual void set_request_scene_tree_message_func(RequestSceneTreeMessageFunc p_func, void *p_udata) {}
310  virtual void set_live_edit_funcs(LiveEditFuncs *p_funcs) {}
311 
312  ScriptDebugger();
313  virtual ~ScriptDebugger() {singleton=NULL;}
314 
315 };
316 
317 
318 
319 #endif
Definition: array.h:38
Definition: variant.h:74
Definition: path_db.h:41
Definition: script_language.h:147
Definition: string_db.h:48
Definition: script_language.h:242
Definition: list.h:44
Definition: script_language.h:134
Definition: script_language.h:205
Definition: resource.h:89
Definition: script_language.h:109
Definition: script_language.h:255
Definition: dvector.h:43
Definition: ustring.h:64
Definition: vector.h:43
Definition: object.h:317
Definition: script_language.h:185
Definition: script_language.h:41
Definition: variant.h:379
Definition: pair.h:33
Definition: script_language.h:66