csharp_script.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*************************************************************************/
  2. /* csharp_script.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 CSHARP_SCRIPT_H
  31. #define CSHARP_SCRIPT_H
  32. #include "io/resource_loader.h"
  33. #include "io/resource_saver.h"
  34. #include "script_language.h"
  35. #include "self_list.h"
  36. #include "mono_gc_handle.h"
  37. #include "mono_gd/gd_mono.h"
  38. #include "mono_gd/gd_mono_header.h"
  39. #include "mono_gd/gd_mono_internals.h"
  40. class CSharpScript;
  41. class CSharpInstance;
  42. class CSharpLanguage;
  43. #ifdef NO_SAFE_CAST
  44. template <typename TScriptInstance, typename TScriptLanguage>
  45. TScriptInstance *cast_script_instance(ScriptInstance *p_inst) {
  46. return p_inst->get_language() == TScriptLanguage::get_singleton() ? static_cast<TScriptInstance *>(p_inst) : NULL;
  47. }
  48. #else
  49. template <typename TScriptInstance, typename TScriptLanguage>
  50. TScriptInstance *cast_script_instance(ScriptInstance *p_inst) {
  51. return dynamic_cast<TScriptInstance *>(p_inst);
  52. }
  53. #endif
  54. #define CAST_CSHARP_INSTANCE(m_inst) (cast_script_instance<CSharpInstance, CSharpLanguage>(m_inst))
  55. class CSharpScript : public Script {
  56. GDCLASS(CSharpScript, Script)
  57. friend class CSharpInstance;
  58. friend class CSharpLanguage;
  59. friend class CSharpScriptDepSort;
  60. bool tool;
  61. bool valid;
  62. bool builtin;
  63. GDMonoClass *base;
  64. GDMonoClass *native;
  65. GDMonoClass *script_class;
  66. Ref<CSharpScript> base_cache; // TODO what's this for?
  67. Set<Object *> instances;
  68. String source;
  69. StringName name;
  70. SelfList<CSharpScript> script_list;
  71. #ifdef TOOLS_ENABLED
  72. List<PropertyInfo> exported_members_cache; // members_cache
  73. Map<StringName, Variant> exported_members_defval_cache; // member_default_values_cache
  74. Set<PlaceHolderScriptInstance *> placeholders;
  75. bool source_changed_cache;
  76. bool exports_invalidated;
  77. void _update_exports_values(Map<StringName, Variant> &values, List<PropertyInfo> &propnames);
  78. virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder);
  79. #endif
  80. #ifdef DEBUG_ENABLED
  81. Map<ObjectID, List<Pair<StringName, Variant> > > pending_reload_state;
  82. #endif
  83. Map<StringName, PropertyInfo> member_info;
  84. void _clear();
  85. bool _update_exports();
  86. CSharpInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Variant::CallError &r_error);
  87. Variant _new(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  88. // Do not use unless you know what you are doing
  89. friend void GDMonoInternals::tie_managed_to_unmanaged(MonoObject *, Object *);
  90. static Ref<CSharpScript> create_for_managed_type(GDMonoClass *p_class);
  91. protected:
  92. static void _bind_methods();
  93. Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  94. virtual void _resource_path_changed();
  95. public:
  96. virtual bool can_instance() const;
  97. virtual StringName get_instance_base_type() const;
  98. virtual ScriptInstance *instance_create(Object *p_this);
  99. virtual bool instance_has(const Object *p_this) const;
  100. virtual bool has_source_code() const;
  101. virtual String get_source_code() const;
  102. virtual void set_source_code(const String &p_code);
  103. virtual Error reload(bool p_keep_state = false);
  104. /* TODO */ virtual bool has_script_signal(const StringName &p_signal) const { return false; }
  105. /* TODO */ virtual void get_script_signal_list(List<MethodInfo> *r_signals) const {}
  106. /* TODO */ virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const;
  107. virtual void get_script_property_list(List<PropertyInfo> *p_list) const;
  108. virtual void update_exports();
  109. virtual bool is_tool() const { return tool; }
  110. virtual Ref<Script> get_base_script() const;
  111. virtual String get_node_type() const;
  112. virtual ScriptLanguage *get_language() const;
  113. /* TODO */ virtual void get_script_method_list(List<MethodInfo> *p_list) const {}
  114. bool has_method(const StringName &p_method) const;
  115. /* TODO */ MethodInfo get_method_info(const StringName &p_method) const { return MethodInfo(); }
  116. virtual int get_member_line(const StringName &p_member) const;
  117. Error load_source_code(const String &p_path);
  118. StringName get_script_name() const;
  119. CSharpScript();
  120. ~CSharpScript();
  121. };
  122. class CSharpInstance : public ScriptInstance {
  123. friend class CSharpScript;
  124. friend class CSharpLanguage;
  125. Object *owner;
  126. Ref<CSharpScript> script;
  127. Ref<MonoGCHandle> gchandle;
  128. bool base_ref;
  129. bool ref_dying;
  130. void _ml_call_reversed(GDMonoClass *klass, const StringName &p_method, const Variant **p_args, int p_argcount);
  131. void _reference_owner_unsafe();
  132. void _unreference_owner_unsafe();
  133. // Do not use unless you know what you are doing
  134. friend void GDMonoInternals::tie_managed_to_unmanaged(MonoObject *, Object *);
  135. static CSharpInstance *create_for_managed_type(Object *p_owner, CSharpScript *p_script, const Ref<MonoGCHandle> &p_gchandle);
  136. public:
  137. MonoObject *get_mono_object() const;
  138. virtual bool set(const StringName &p_name, const Variant &p_value);
  139. virtual bool get(const StringName &p_name, Variant &r_ret) const;
  140. virtual void get_property_list(List<PropertyInfo> *p_properties) const;
  141. virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid) const;
  142. /* TODO */ virtual void get_method_list(List<MethodInfo> *p_list) const {}
  143. virtual bool has_method(const StringName &p_method) const;
  144. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  145. virtual void call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount);
  146. virtual void call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount);
  147. void mono_object_disposed();
  148. void refcount_incremented();
  149. bool refcount_decremented();
  150. RPCMode get_rpc_mode(const StringName &p_method) const;
  151. RPCMode get_rset_mode(const StringName &p_variable) const;
  152. virtual void notification(int p_notification);
  153. virtual Ref<Script> get_script() const;
  154. virtual ScriptLanguage *get_language();
  155. CSharpInstance();
  156. ~CSharpInstance();
  157. };
  158. class CSharpLanguage : public ScriptLanguage {
  159. friend class CSharpScript;
  160. friend class CSharpInstance;
  161. static CSharpLanguage *singleton;
  162. GDMono *gdmono;
  163. SelfList<CSharpScript>::List script_list;
  164. Mutex *lock;
  165. Mutex *script_bind_lock;
  166. Map<Ref<CSharpScript>, Map<ObjectID, List<Pair<StringName, Variant> > > > to_reload;
  167. Map<Object *, Ref<MonoGCHandle> > gchandle_bindings;
  168. struct StringNameCache {
  169. StringName _signal_callback;
  170. StringName _set;
  171. StringName _get;
  172. StringName _notification;
  173. StringName dotctor; // .ctor
  174. StringNameCache();
  175. };
  176. StringNameCache string_names;
  177. int lang_idx;
  178. public:
  179. _FORCE_INLINE_ int get_language_index() { return lang_idx; }
  180. void set_language_index(int p_idx);
  181. _FORCE_INLINE_ const StringNameCache &get_string_names() { return string_names; }
  182. _FORCE_INLINE_ static CSharpLanguage *get_singleton() { return singleton; }
  183. bool debug_break(const String &p_error, bool p_allow_continue = true);
  184. bool debug_break_parse(const String &p_file, int p_line, const String &p_error);
  185. #ifdef TOOLS_ENABLED
  186. void reload_assemblies_if_needed(bool p_soft_reload);
  187. #endif
  188. virtual String get_name() const;
  189. /* LANGUAGE FUNCTIONS */
  190. virtual String get_type() const;
  191. virtual String get_extension() const;
  192. virtual Error execute_file(const String &p_path);
  193. virtual void init();
  194. virtual void finish();
  195. /* EDITOR FUNCTIONS */
  196. virtual void get_reserved_words(List<String> *p_words) const;
  197. virtual void get_comment_delimiters(List<String> *p_delimiters) const;
  198. virtual void get_string_delimiters(List<String> *p_delimiters) const;
  199. virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const;
  200. /* TODO */ 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 { return true; }
  201. virtual Script *create_script() const;
  202. virtual bool has_named_classes() const;
  203. virtual bool supports_builtin_mode() const;
  204. /* TODO? */ virtual int find_function(const String &p_function, const String &p_code) const { return -1; }
  205. virtual String make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const;
  206. /* TODO? */ 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; }
  207. /* TODO? */ virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {}
  208. /* TODO */ virtual void add_global_constant(const StringName &p_variable, const Variant &p_value) {}
  209. /* DEBUGGER FUNCTIONS */
  210. /* TODO */ virtual String debug_get_error() const { return ""; }
  211. /* TODO */ virtual int debug_get_stack_level_count() const { return 1; }
  212. /* TODO */ virtual int debug_get_stack_level_line(int p_level) const { return 1; }
  213. /* TODO */ virtual String debug_get_stack_level_function(int p_level) const { return ""; }
  214. /* TODO */ virtual String debug_get_stack_level_source(int p_level) const { return ""; }
  215. /* TODO */ 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) {}
  216. /* TODO */ 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) {}
  217. /* TODO */ virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {}
  218. /* TODO */ virtual String debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) { return ""; }
  219. /* TODO */ virtual Vector<StackInfo> debug_get_current_stack_info() { return Vector<StackInfo>(); }
  220. /* PROFILING FUNCTIONS */
  221. /* TODO */ virtual void profiling_start() {}
  222. /* TODO */ virtual void profiling_stop() {}
  223. /* TODO */ virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) { return 0; }
  224. /* TODO */ virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) { return 0; }
  225. virtual void frame();
  226. /* TODO? */ virtual void get_public_functions(List<MethodInfo> *p_functions) const {}
  227. /* TODO? */ virtual void get_public_constants(List<Pair<String, Variant> > *p_constants) const {}
  228. virtual void reload_all_scripts();
  229. virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload);
  230. /* LOADER FUNCTIONS */
  231. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  232. #ifdef TOOLS_ENABLED
  233. virtual Error open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col);
  234. virtual bool overrides_external_editor();
  235. #endif
  236. /* THREAD ATTACHING */
  237. virtual void thread_enter();
  238. virtual void thread_exit();
  239. // Don't use these. I'm watching you
  240. virtual void *alloc_instance_binding_data(Object *p_object);
  241. virtual void free_instance_binding_data(void *p_data);
  242. CSharpLanguage();
  243. ~CSharpLanguage();
  244. };
  245. class ResourceFormatLoaderCSharpScript : public ResourceFormatLoader {
  246. public:
  247. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  248. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  249. virtual bool handles_type(const String &p_type) const;
  250. virtual String get_resource_type(const String &p_path) const;
  251. };
  252. class ResourceFormatSaverCSharpScript : public ResourceFormatSaver {
  253. public:
  254. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  255. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  256. virtual bool recognize(const RES &p_resource) const;
  257. };
  258. #endif // CSHARP_SCRIPT_H