csharp_script.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*************************************************************************/
  2. /* csharp_script.h */
  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. #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. bool _get_member_export(GDMonoClass *p_class, GDMonoClassMember *p_member, PropertyInfo &r_prop_info, bool &r_exported);
  87. CSharpInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Variant::CallError &r_error);
  88. Variant _new(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  89. // Do not use unless you know what you are doing
  90. friend void GDMonoInternals::tie_managed_to_unmanaged(MonoObject *, Object *);
  91. static Ref<CSharpScript> create_for_managed_type(GDMonoClass *p_class);
  92. protected:
  93. static void _bind_methods();
  94. Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  95. virtual void _resource_path_changed();
  96. bool _get(const StringName &p_name, Variant &r_ret) const;
  97. bool _set(const StringName &p_name, const Variant &p_value);
  98. void _get_property_list(List<PropertyInfo> *p_properties) const;
  99. public:
  100. virtual bool can_instance() const;
  101. virtual StringName get_instance_base_type() const;
  102. virtual ScriptInstance *instance_create(Object *p_this);
  103. virtual bool instance_has(const Object *p_this) const;
  104. virtual bool has_source_code() const;
  105. virtual String get_source_code() const;
  106. virtual void set_source_code(const String &p_code);
  107. virtual Error reload(bool p_keep_state = false);
  108. /* TODO */ virtual bool has_script_signal(const StringName &p_signal) const { return false; }
  109. /* TODO */ virtual void get_script_signal_list(List<MethodInfo> *r_signals) const {}
  110. /* TODO */ virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const;
  111. virtual void get_script_property_list(List<PropertyInfo> *p_list) const;
  112. virtual void update_exports();
  113. virtual bool is_tool() const { return tool; }
  114. virtual Ref<Script> get_base_script() const;
  115. virtual ScriptLanguage *get_language() const;
  116. /* TODO */ virtual void get_script_method_list(List<MethodInfo> *p_list) const {}
  117. bool has_method(const StringName &p_method) const;
  118. /* TODO */ MethodInfo get_method_info(const StringName &p_method) const { return MethodInfo(); }
  119. virtual int get_member_line(const StringName &p_member) const;
  120. Error load_source_code(const String &p_path);
  121. StringName get_script_name() const;
  122. CSharpScript();
  123. ~CSharpScript();
  124. };
  125. class CSharpInstance : public ScriptInstance {
  126. friend class CSharpScript;
  127. friend class CSharpLanguage;
  128. Object *owner;
  129. Ref<CSharpScript> script;
  130. Ref<MonoGCHandle> gchandle;
  131. bool base_ref;
  132. bool ref_dying;
  133. void _ml_call_reversed(MonoObject *p_mono_object, GDMonoClass *klass, const StringName &p_method, const Variant **p_args, int p_argcount);
  134. void _reference_owner_unsafe();
  135. void _unreference_owner_unsafe();
  136. // Do not use unless you know what you are doing
  137. friend void GDMonoInternals::tie_managed_to_unmanaged(MonoObject *, Object *);
  138. static CSharpInstance *create_for_managed_type(Object *p_owner, CSharpScript *p_script, const Ref<MonoGCHandle> &p_gchandle);
  139. void _call_multilevel(MonoObject *p_mono_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  140. RPCMode _member_get_rpc_mode(GDMonoClassMember *p_member) const;
  141. public:
  142. MonoObject *get_mono_object() const;
  143. virtual bool set(const StringName &p_name, const Variant &p_value);
  144. virtual bool get(const StringName &p_name, Variant &r_ret) const;
  145. virtual void get_property_list(List<PropertyInfo> *p_properties) const;
  146. virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid) const;
  147. /* TODO */ virtual void get_method_list(List<MethodInfo> *p_list) const {}
  148. virtual bool has_method(const StringName &p_method) const;
  149. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  150. virtual void call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount);
  151. virtual void call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount);
  152. void mono_object_disposed();
  153. virtual void refcount_incremented();
  154. virtual bool refcount_decremented();
  155. virtual RPCMode get_rpc_mode(const StringName &p_method) const;
  156. virtual RPCMode get_rset_mode(const StringName &p_variable) const;
  157. virtual void notification(int p_notification);
  158. void call_notification_no_check(MonoObject *p_mono_object, int p_notification);
  159. virtual Ref<Script> get_script() const;
  160. virtual ScriptLanguage *get_language();
  161. CSharpInstance();
  162. ~CSharpInstance();
  163. };
  164. class CSharpLanguage : public ScriptLanguage {
  165. friend class CSharpScript;
  166. friend class CSharpInstance;
  167. static CSharpLanguage *singleton;
  168. bool finalizing;
  169. GDMono *gdmono;
  170. SelfList<CSharpScript>::List script_list;
  171. Mutex *lock;
  172. Mutex *script_bind_lock;
  173. Map<Ref<CSharpScript>, Map<ObjectID, List<Pair<StringName, Variant> > > > to_reload;
  174. Map<Object *, Ref<MonoGCHandle> > gchandle_bindings;
  175. struct StringNameCache {
  176. StringName _signal_callback;
  177. StringName _set;
  178. StringName _get;
  179. StringName _notification;
  180. StringName _script_source;
  181. StringName dotctor; // .ctor
  182. StringNameCache();
  183. };
  184. int lang_idx;
  185. public:
  186. StringNameCache string_names;
  187. _FORCE_INLINE_ int get_language_index() { return lang_idx; }
  188. void set_language_index(int p_idx);
  189. _FORCE_INLINE_ const StringNameCache &get_string_names() { return string_names; }
  190. _FORCE_INLINE_ static CSharpLanguage *get_singleton() { return singleton; }
  191. bool debug_break(const String &p_error, bool p_allow_continue = true);
  192. bool debug_break_parse(const String &p_file, int p_line, const String &p_error);
  193. #ifdef TOOLS_ENABLED
  194. void reload_assemblies_if_needed(bool p_soft_reload);
  195. #endif
  196. virtual String get_name() const;
  197. /* LANGUAGE FUNCTIONS */
  198. virtual String get_type() const;
  199. virtual String get_extension() const;
  200. virtual Error execute_file(const String &p_path);
  201. virtual void init();
  202. virtual void finish();
  203. /* EDITOR FUNCTIONS */
  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 is_using_templates();
  209. virtual void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script);
  210. /* 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; }
  211. virtual Script *create_script() const;
  212. virtual bool has_named_classes() const;
  213. virtual bool supports_builtin_mode() const;
  214. /* TODO? */ virtual int find_function(const String &p_function, const String &p_code) const { return -1; }
  215. virtual String make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const;
  216. /* 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; }
  217. virtual String _get_indentation() const;
  218. /* TODO? */ virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {}
  219. /* TODO */ virtual void add_global_constant(const StringName &p_variable, const Variant &p_value) {}
  220. /* DEBUGGER FUNCTIONS */
  221. /* TODO */ virtual String debug_get_error() const { return ""; }
  222. /* TODO */ virtual int debug_get_stack_level_count() const { return 1; }
  223. /* TODO */ virtual int debug_get_stack_level_line(int p_level) const { return 1; }
  224. /* TODO */ virtual String debug_get_stack_level_function(int p_level) const { return ""; }
  225. /* TODO */ virtual String debug_get_stack_level_source(int p_level) const { return ""; }
  226. /* 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) {}
  227. /* 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) {}
  228. /* TODO */ virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {}
  229. /* TODO */ virtual String debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) { return ""; }
  230. /* TODO */ virtual Vector<StackInfo> debug_get_current_stack_info() { return Vector<StackInfo>(); }
  231. /* PROFILING FUNCTIONS */
  232. /* TODO */ virtual void profiling_start() {}
  233. /* TODO */ virtual void profiling_stop() {}
  234. /* TODO */ virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) { return 0; }
  235. /* TODO */ virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) { return 0; }
  236. virtual void frame();
  237. /* TODO? */ virtual void get_public_functions(List<MethodInfo> *p_functions) const {}
  238. /* TODO? */ virtual void get_public_constants(List<Pair<String, Variant> > *p_constants) const {}
  239. virtual void reload_all_scripts();
  240. virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload);
  241. /* LOADER FUNCTIONS */
  242. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  243. #ifdef TOOLS_ENABLED
  244. virtual Error open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col);
  245. virtual bool overrides_external_editor();
  246. #endif
  247. /* THREAD ATTACHING */
  248. virtual void thread_enter();
  249. virtual void thread_exit();
  250. // Don't use these. I'm watching you
  251. virtual void *alloc_instance_binding_data(Object *p_object);
  252. virtual void free_instance_binding_data(void *p_data);
  253. CSharpLanguage();
  254. ~CSharpLanguage();
  255. };
  256. class ResourceFormatLoaderCSharpScript : public ResourceFormatLoader {
  257. public:
  258. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  259. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  260. virtual bool handles_type(const String &p_type) const;
  261. virtual String get_resource_type(const String &p_path) const;
  262. };
  263. class ResourceFormatSaverCSharpScript : public ResourceFormatSaver {
  264. public:
  265. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  266. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  267. virtual bool recognize(const RES &p_resource) const;
  268. };
  269. #endif // CSHARP_SCRIPT_H