nativescript.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*************************************************************************/
  2. /* nativescript.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #ifndef NATIVE_SCRIPT_H
  31. #define NATIVE_SCRIPT_H
  32. #include "core/resource.h"
  33. #include "core/script_language.h"
  34. #include "core/self_list.h"
  35. #include "io/resource_loader.h"
  36. #include "io/resource_saver.h"
  37. #include "ordered_hash_map.h"
  38. #include "os/thread_safe.h"
  39. #include "scene/main/node.h"
  40. #include "modules/gdnative/gdnative.h"
  41. #include <nativescript/godot_nativescript.h>
  42. #ifndef NO_THREADS
  43. #include "os/mutex.h"
  44. #endif
  45. struct NativeScriptDesc {
  46. struct Method {
  47. godot_instance_method method;
  48. MethodInfo info;
  49. int rpc_mode;
  50. };
  51. struct Property {
  52. godot_property_set_func setter;
  53. godot_property_get_func getter;
  54. PropertyInfo info;
  55. Variant default_value;
  56. int rset_mode;
  57. };
  58. struct Signal {
  59. MethodInfo signal;
  60. };
  61. Map<StringName, Method> methods;
  62. OrderedHashMap<StringName, Property> properties;
  63. Map<StringName, Signal> signals_; // QtCreator doesn't like the name signals
  64. StringName base;
  65. StringName base_native_type;
  66. NativeScriptDesc *base_data;
  67. godot_instance_create_func create_func;
  68. godot_instance_destroy_func destroy_func;
  69. bool is_tool;
  70. inline NativeScriptDesc()
  71. : methods(),
  72. properties(),
  73. signals_(),
  74. base(),
  75. base_native_type() {
  76. zeromem(&create_func, sizeof(godot_instance_create_func));
  77. zeromem(&destroy_func, sizeof(godot_instance_destroy_func));
  78. }
  79. };
  80. class NativeScript : public Script {
  81. GDCLASS(NativeScript, Script)
  82. #ifdef TOOLS_ENABLED
  83. Set<PlaceHolderScriptInstance *> placeholders;
  84. void _update_placeholder(PlaceHolderScriptInstance *p_placeholder);
  85. virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder);
  86. #endif
  87. friend class NativeScriptInstance;
  88. friend class NativeScriptLanguage;
  89. friend class NativeReloadNode;
  90. friend class GDNativeLibrary;
  91. Ref<GDNativeLibrary> library;
  92. String lib_path;
  93. String class_name;
  94. #ifndef NO_THREADS
  95. Mutex *owners_lock;
  96. #endif
  97. Set<Object *> instance_owners;
  98. protected:
  99. static void _bind_methods();
  100. public:
  101. inline NativeScriptDesc *get_script_desc() const;
  102. void set_class_name(String p_class_name);
  103. String get_class_name() const;
  104. void set_library(Ref<GDNativeLibrary> p_library);
  105. Ref<GDNativeLibrary> get_library() const;
  106. virtual bool can_instance() const;
  107. virtual Ref<Script> get_base_script() const; //for script inheritance
  108. virtual StringName get_instance_base_type() const; // this may not work in all scripts, will return empty if so
  109. virtual ScriptInstance *instance_create(Object *p_this);
  110. virtual bool instance_has(const Object *p_this) const;
  111. virtual bool has_source_code() const;
  112. virtual String get_source_code() const;
  113. virtual void set_source_code(const String &p_code);
  114. virtual Error reload(bool p_keep_state = false);
  115. virtual bool has_method(const StringName &p_method) const;
  116. virtual MethodInfo get_method_info(const StringName &p_method) const;
  117. virtual bool is_tool() const;
  118. virtual String get_node_type() const;
  119. virtual ScriptLanguage *get_language() const;
  120. virtual bool has_script_signal(const StringName &p_signal) const;
  121. virtual void get_script_signal_list(List<MethodInfo> *r_signals) const;
  122. virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const;
  123. virtual void update_exports(); //editor tool
  124. virtual void get_script_method_list(List<MethodInfo> *p_list) const;
  125. virtual void get_script_property_list(List<PropertyInfo> *p_list) const;
  126. Variant _new(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  127. NativeScript();
  128. ~NativeScript();
  129. };
  130. class NativeScriptInstance : public ScriptInstance {
  131. friend class NativeScript;
  132. Object *owner;
  133. Ref<NativeScript> script;
  134. void _ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount);
  135. public:
  136. void *userdata;
  137. virtual bool set(const StringName &p_name, const Variant &p_value);
  138. virtual bool get(const StringName &p_name, Variant &r_ret) const;
  139. virtual void get_property_list(List<PropertyInfo> *p_properties) const;
  140. virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid) const;
  141. virtual void get_method_list(List<MethodInfo> *p_list) const;
  142. virtual bool has_method(const StringName &p_method) const;
  143. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  144. virtual void notification(int p_notification);
  145. virtual Ref<Script> get_script() const;
  146. virtual RPCMode get_rpc_mode(const StringName &p_method) const;
  147. virtual RPCMode get_rset_mode(const StringName &p_variable) const;
  148. virtual ScriptLanguage *get_language();
  149. virtual void call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount);
  150. virtual void call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount);
  151. virtual void refcount_incremented();
  152. virtual bool refcount_decremented();
  153. ~NativeScriptInstance();
  154. };
  155. class NativeReloadNode;
  156. class NativeScriptLanguage : public ScriptLanguage {
  157. friend class NativeScript;
  158. friend class NativeScriptInstance;
  159. friend class NativeReloadNode;
  160. private:
  161. static NativeScriptLanguage *singleton;
  162. void _unload_stuff();
  163. #ifndef NO_THREADS
  164. Mutex *mutex;
  165. Set<Ref<GDNativeLibrary> > libs_to_init;
  166. Set<NativeScript *> scripts_to_register;
  167. volatile bool has_objects_to_register; // so that we don't lock mutex every frame - it's rarely needed
  168. void defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script);
  169. #endif
  170. void init_library(const Ref<GDNativeLibrary> &lib);
  171. void register_script(NativeScript *script);
  172. void unregister_script(NativeScript *script);
  173. void call_libraries_cb(const StringName &name);
  174. public:
  175. // These two maps must only be touched on the main thread
  176. Map<String, Map<StringName, NativeScriptDesc> > library_classes;
  177. Map<String, Ref<GDNative> > library_gdnatives;
  178. Map<String, Set<NativeScript *> > library_script_users;
  179. const StringName _init_call_type = "nativescript_init";
  180. const StringName _init_call_name = "godot_nativescript_init";
  181. const StringName _noarg_call_type = "nativescript_no_arg";
  182. const StringName _frame_call_name = "godot_nativescript_frame";
  183. #ifndef NO_THREADS
  184. const StringName _thread_enter_call_name = "godot_nativescript_thread_enter";
  185. const StringName _thread_exit_call_name = "godot_nativescript_thread_exit";
  186. #endif
  187. NativeScriptLanguage();
  188. ~NativeScriptLanguage();
  189. inline static NativeScriptLanguage *get_singleton() {
  190. return singleton;
  191. }
  192. void _hacky_api_anchor();
  193. #ifndef NO_THREADS
  194. virtual void thread_enter();
  195. virtual void thread_exit();
  196. #endif
  197. virtual void frame();
  198. virtual String get_name() const;
  199. virtual void init();
  200. virtual String get_type() const;
  201. virtual String get_extension() const;
  202. virtual Error execute_file(const String &p_path);
  203. virtual void finish();
  204. virtual void get_reserved_words(List<String> *p_words) const;
  205. virtual void get_comment_delimiters(List<String> *p_delimiters) const;
  206. virtual void get_string_delimiters(List<String> *p_delimiters) const;
  207. virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const;
  208. 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) const;
  209. virtual Script *create_script() const;
  210. virtual bool has_named_classes() const;
  211. virtual bool supports_builtin_mode() const;
  212. virtual int find_function(const String &p_function, const String &p_code) const;
  213. virtual String make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const;
  214. virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const;
  215. virtual void add_global_constant(const StringName &p_variable, const Variant &p_value);
  216. virtual String debug_get_error() const;
  217. virtual int debug_get_stack_level_count() const;
  218. virtual int debug_get_stack_level_line(int p_level) const;
  219. virtual String debug_get_stack_level_function(int p_level) const;
  220. virtual String debug_get_stack_level_source(int p_level) const;
  221. virtual void debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth);
  222. virtual void debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth);
  223. virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth);
  224. virtual String debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth);
  225. virtual void reload_all_scripts();
  226. virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload);
  227. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  228. virtual void get_public_functions(List<MethodInfo> *p_functions) const;
  229. virtual void get_public_constants(List<Pair<String, Variant> > *p_constants) const;
  230. virtual void profiling_start();
  231. virtual void profiling_stop();
  232. virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max);
  233. virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max);
  234. };
  235. inline NativeScriptDesc *NativeScript::get_script_desc() const {
  236. Map<StringName, NativeScriptDesc>::Element *E = NativeScriptLanguage::singleton->library_classes[lib_path].find(class_name);
  237. return E ? &E->get() : NULL;
  238. }
  239. class NativeReloadNode : public Node {
  240. GDCLASS(NativeReloadNode, Node)
  241. bool unloaded = false;
  242. public:
  243. static void _bind_methods();
  244. void _notification(int p_what);
  245. };
  246. class ResourceFormatLoaderNativeScript : public ResourceFormatLoader {
  247. public:
  248. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  249. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  250. virtual bool handles_type(const String &p_type) const;
  251. virtual String get_resource_type(const String &p_path) const;
  252. };
  253. class ResourceFormatSaverNativeScript : public ResourceFormatSaver {
  254. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  255. virtual bool recognize(const RES &p_resource) const;
  256. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  257. };
  258. #endif // GDNATIVE_H