csharp_script.h 15 KB

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