csharp_script.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 "core/io/resource_loader.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/script_language.h"
  35. #include "core/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. if (!p_inst)
  47. return NULL;
  48. return p_inst->get_language() == TScriptLanguage::get_singleton() ? static_cast<TScriptInstance *>(p_inst) : NULL;
  49. }
  50. #else
  51. template <typename TScriptInstance, typename TScriptLanguage>
  52. TScriptInstance *cast_script_instance(ScriptInstance *p_inst) {
  53. return dynamic_cast<TScriptInstance *>(p_inst);
  54. }
  55. #endif
  56. #define CAST_CSHARP_INSTANCE(m_inst) (cast_script_instance<CSharpInstance, CSharpLanguage>(m_inst))
  57. class CSharpScript : public Script {
  58. GDCLASS(CSharpScript, Script)
  59. friend class CSharpInstance;
  60. friend class CSharpLanguage;
  61. friend struct CSharpScriptDepSort;
  62. bool tool;
  63. bool valid;
  64. bool builtin;
  65. GDMonoClass *base;
  66. GDMonoClass *native;
  67. GDMonoClass *script_class;
  68. Ref<CSharpScript> base_cache; // TODO what's this for?
  69. Set<Object *> instances;
  70. #ifdef DEBUG_ENABLED
  71. Set<ObjectID> pending_reload_instances;
  72. #endif
  73. struct StateBackup {
  74. // TODO
  75. // Replace with buffer containing the serialized state of managed scripts.
  76. // Keep variant state backup to use only with script instance placeholders.
  77. List<Pair<StringName, Variant> > properties;
  78. };
  79. #ifdef TOOLS_ENABLED
  80. Map<ObjectID, CSharpScript::StateBackup> pending_reload_state;
  81. #endif
  82. String source;
  83. StringName name;
  84. SelfList<CSharpScript> script_list;
  85. struct Argument {
  86. String name;
  87. Variant::Type type;
  88. };
  89. Map<StringName, Vector<Argument> > _signals;
  90. bool signals_invalidated;
  91. #ifdef TOOLS_ENABLED
  92. List<PropertyInfo> exported_members_cache; // members_cache
  93. Map<StringName, Variant> exported_members_defval_cache; // member_default_values_cache
  94. Set<PlaceHolderScriptInstance *> placeholders;
  95. bool source_changed_cache;
  96. bool exports_invalidated;
  97. void _update_exports_values(Map<StringName, Variant> &values, List<PropertyInfo> &propnames);
  98. virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder);
  99. #endif
  100. Map<StringName, PropertyInfo> member_info;
  101. void _clear();
  102. void load_script_signals(GDMonoClass *p_class, GDMonoClass *p_native_class);
  103. bool _get_signal(GDMonoClass *p_class, GDMonoClass *p_delegate, Vector<Argument> &params);
  104. bool _update_exports();
  105. #ifdef TOOLS_ENABLED
  106. bool _get_member_export(GDMonoClass *p_class, GDMonoClassMember *p_member, PropertyInfo &r_prop_info, bool &r_exported);
  107. #endif
  108. CSharpInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Variant::CallError &r_error);
  109. Variant _new(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  110. // Do not use unless you know what you are doing
  111. friend void GDMonoInternals::tie_managed_to_unmanaged(MonoObject *, Object *);
  112. static Ref<CSharpScript> create_for_managed_type(GDMonoClass *p_class);
  113. protected:
  114. static void _bind_methods();
  115. Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  116. virtual void _resource_path_changed();
  117. bool _get(const StringName &p_name, Variant &r_ret) const;
  118. bool _set(const StringName &p_name, const Variant &p_value);
  119. void _get_property_list(List<PropertyInfo> *p_properties) const;
  120. public:
  121. virtual bool can_instance() const;
  122. virtual StringName get_instance_base_type() const;
  123. virtual ScriptInstance *instance_create(Object *p_this);
  124. virtual PlaceHolderScriptInstance *placeholder_instance_create(Object *p_this);
  125. virtual bool instance_has(const Object *p_this) const;
  126. virtual bool has_source_code() const;
  127. virtual String get_source_code() const;
  128. virtual void set_source_code(const String &p_code);
  129. virtual Error reload(bool p_keep_state = false);
  130. virtual bool has_script_signal(const StringName &p_signal) const;
  131. virtual void get_script_signal_list(List<MethodInfo> *r_signals) const;
  132. /* TODO */ virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const;
  133. virtual void get_script_property_list(List<PropertyInfo> *p_list) const;
  134. virtual void update_exports();
  135. virtual bool is_tool() const { return tool; }
  136. virtual bool is_valid() const { return valid; }
  137. virtual Ref<Script> get_base_script() const;
  138. virtual ScriptLanguage *get_language() const;
  139. virtual void get_script_method_list(List<MethodInfo> *p_list) const;
  140. bool has_method(const StringName &p_method) const;
  141. MethodInfo get_method_info(const StringName &p_method) const;
  142. virtual int get_member_line(const StringName &p_member) const;
  143. Error load_source_code(const String &p_path);
  144. StringName get_script_name() const;
  145. CSharpScript();
  146. ~CSharpScript();
  147. };
  148. class CSharpInstance : public ScriptInstance {
  149. friend class CSharpScript;
  150. friend class CSharpLanguage;
  151. Object *owner;
  152. bool base_ref;
  153. bool ref_dying;
  154. bool unsafe_referenced;
  155. bool predelete_notified;
  156. bool destructing_script_instance;
  157. Ref<CSharpScript> script;
  158. Ref<MonoGCHandle> gchandle;
  159. bool _reference_owner_unsafe();
  160. bool _unreference_owner_unsafe();
  161. MonoObject *_internal_new_managed();
  162. // Do not use unless you know what you are doing
  163. friend void GDMonoInternals::tie_managed_to_unmanaged(MonoObject *, Object *);
  164. static CSharpInstance *create_for_managed_type(Object *p_owner, CSharpScript *p_script, const Ref<MonoGCHandle> &p_gchandle);
  165. void _call_multilevel(MonoObject *p_mono_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  166. MultiplayerAPI::RPCMode _member_get_rpc_mode(GDMonoClassMember *p_member) const;
  167. public:
  168. MonoObject *get_mono_object() const;
  169. _FORCE_INLINE_ bool is_destructing_script_instance() { return destructing_script_instance; }
  170. virtual bool set(const StringName &p_name, const Variant &p_value);
  171. virtual bool get(const StringName &p_name, Variant &r_ret) const;
  172. virtual void get_property_list(List<PropertyInfo> *p_properties) const;
  173. virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid) const;
  174. /* TODO */ virtual void get_method_list(List<MethodInfo> *p_list) const {}
  175. virtual bool has_method(const StringName &p_method) const;
  176. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  177. virtual void call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount);
  178. virtual void call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount);
  179. void mono_object_disposed(MonoObject *p_obj);
  180. void mono_object_disposed_baseref(MonoObject *p_obj, bool p_is_finalizer, bool &r_owner_deleted);
  181. virtual void refcount_incremented();
  182. virtual bool refcount_decremented();
  183. virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const;
  184. virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const;
  185. virtual void notification(int p_notification);
  186. void _call_notification(int p_notification);
  187. virtual Ref<Script> get_script() const;
  188. virtual ScriptLanguage *get_language();
  189. CSharpInstance();
  190. ~CSharpInstance();
  191. };
  192. struct CSharpScriptBinding {
  193. StringName type_name;
  194. GDMonoClass *wrapper_class;
  195. Ref<MonoGCHandle> gchandle;
  196. };
  197. class CSharpLanguage : public ScriptLanguage {
  198. friend class CSharpScript;
  199. friend class CSharpInstance;
  200. static CSharpLanguage *singleton;
  201. bool finalizing;
  202. GDMono *gdmono;
  203. SelfList<CSharpScript>::List script_list;
  204. Mutex *script_instances_mutex;
  205. Mutex *script_gchandle_release_mutex;
  206. Mutex *language_bind_mutex;
  207. Map<Object *, CSharpScriptBinding> script_bindings;
  208. struct StringNameCache {
  209. StringName _signal_callback;
  210. StringName _set;
  211. StringName _get;
  212. StringName _notification;
  213. StringName _script_source;
  214. StringName dotctor; // .ctor
  215. StringNameCache();
  216. };
  217. int lang_idx;
  218. Dictionary scripts_metadata;
  219. public:
  220. StringNameCache string_names;
  221. _FORCE_INLINE_ int get_language_index() { return lang_idx; }
  222. void set_language_index(int p_idx);
  223. _FORCE_INLINE_ const StringNameCache &get_string_names() { return string_names; }
  224. _FORCE_INLINE_ static CSharpLanguage *get_singleton() { return singleton; }
  225. static void release_script_gchandle(Ref<MonoGCHandle> &p_gchandle);
  226. static void release_script_gchandle(MonoObject *p_pinned_expected_obj, Ref<MonoGCHandle> &p_gchandle);
  227. bool debug_break(const String &p_error, bool p_allow_continue = true);
  228. bool debug_break_parse(const String &p_file, int p_line, const String &p_error);
  229. #ifdef TOOLS_ENABLED
  230. bool is_assembly_reloading_needed();
  231. void reload_assemblies(bool p_soft_reload);
  232. #endif
  233. void project_assembly_loaded();
  234. _FORCE_INLINE_ const Dictionary &get_scripts_metadata() { return scripts_metadata; }
  235. virtual String get_name() const;
  236. /* LANGUAGE FUNCTIONS */
  237. virtual String get_type() const;
  238. virtual String get_extension() const;
  239. virtual Error execute_file(const String &p_path);
  240. virtual void init();
  241. virtual void finish();
  242. /* EDITOR FUNCTIONS */
  243. virtual void get_reserved_words(List<String> *p_words) const;
  244. virtual void get_comment_delimiters(List<String> *p_delimiters) const;
  245. virtual void get_string_delimiters(List<String> *p_delimiters) const;
  246. virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const;
  247. virtual bool is_using_templates();
  248. virtual void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script);
  249. /* 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, List<ScriptLanguage::Warning> *r_warnings = NULL, Set<int> *r_safe_lines = NULL) const { return true; }
  250. virtual String validate_path(const String &p_path) const;
  251. virtual Script *create_script() const;
  252. virtual bool has_named_classes() const;
  253. virtual bool supports_builtin_mode() const;
  254. /* TODO? */ virtual int find_function(const String &p_function, const String &p_code) const { return -1; }
  255. virtual String make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const;
  256. /* 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; }
  257. virtual String _get_indentation() const;
  258. /* TODO? */ virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {}
  259. /* TODO */ virtual void add_global_constant(const StringName &p_variable, const Variant &p_value) {}
  260. /* DEBUGGER FUNCTIONS */
  261. /* TODO */ virtual String debug_get_error() const { return ""; }
  262. /* TODO */ virtual int debug_get_stack_level_count() const { return 1; }
  263. /* TODO */ virtual int debug_get_stack_level_line(int p_level) const { return 1; }
  264. /* TODO */ virtual String debug_get_stack_level_function(int p_level) const { return ""; }
  265. /* TODO */ virtual String debug_get_stack_level_source(int p_level) const { return ""; }
  266. /* 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) {}
  267. /* 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) {}
  268. /* TODO */ virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {}
  269. /* TODO */ virtual String debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) { return ""; }
  270. virtual Vector<StackInfo> debug_get_current_stack_info();
  271. /* PROFILING FUNCTIONS */
  272. /* TODO */ virtual void profiling_start() {}
  273. /* TODO */ virtual void profiling_stop() {}
  274. /* TODO */ virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) { return 0; }
  275. /* TODO */ virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) { return 0; }
  276. virtual void frame();
  277. /* TODO? */ virtual void get_public_functions(List<MethodInfo> *p_functions) const {}
  278. /* TODO? */ virtual void get_public_constants(List<Pair<String, Variant> > *p_constants) const {}
  279. virtual void reload_all_scripts();
  280. virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload);
  281. /* LOADER FUNCTIONS */
  282. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  283. #ifdef TOOLS_ENABLED
  284. virtual Error open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col);
  285. virtual bool overrides_external_editor();
  286. #endif
  287. /* THREAD ATTACHING */
  288. virtual void thread_enter();
  289. virtual void thread_exit();
  290. // Don't use these. I'm watching you
  291. virtual void *alloc_instance_binding_data(Object *p_object);
  292. virtual void free_instance_binding_data(void *p_data);
  293. virtual void refcount_incremented_instance_binding(Object *p_object);
  294. virtual bool refcount_decremented_instance_binding(Object *p_object);
  295. #ifdef DEBUG_ENABLED
  296. Vector<StackInfo> stack_trace_get_info(MonoObject *p_stack_trace);
  297. #endif
  298. CSharpLanguage();
  299. ~CSharpLanguage();
  300. };
  301. class ResourceFormatLoaderCSharpScript : public ResourceFormatLoader {
  302. GDCLASS(ResourceFormatLoaderCSharpScript, ResourceFormatLoader)
  303. public:
  304. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  305. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  306. virtual bool handles_type(const String &p_type) const;
  307. virtual String get_resource_type(const String &p_path) const;
  308. };
  309. class ResourceFormatSaverCSharpScript : public ResourceFormatSaver {
  310. GDCLASS(ResourceFormatSaverCSharpScript, ResourceFormatSaver)
  311. public:
  312. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  313. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  314. virtual bool recognize(const RES &p_resource) const;
  315. };
  316. #endif // CSHARP_SCRIPT_H