csharp_script.h 18 KB

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