/*************************************************************************/ /* csharp_script.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #ifndef CSHARP_SCRIPT_H #define CSHARP_SCRIPT_H #include "core/doc_data.h" #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/object/script_language.h" #include "core/templates/self_list.h" #include "mono_gc_handle.h" #include "mono_gd/gd_mono.h" #include "mono_gd/gd_mono_header.h" #include "mono_gd/gd_mono_internals.h" #ifdef TOOLS_ENABLED #include "editor/editor_plugin.h" #endif class CSharpScript; class CSharpInstance; class CSharpLanguage; #ifdef NO_SAFE_CAST template TScriptInstance *cast_script_instance(ScriptInstance *p_inst) { if (!p_inst) return nullptr; return p_inst->get_language() == TScriptLanguage::get_singleton() ? static_cast(p_inst) : nullptr; } #else template TScriptInstance *cast_script_instance(ScriptInstance *p_inst) { return dynamic_cast(p_inst); } #endif #define CAST_CSHARP_INSTANCE(m_inst) (cast_script_instance(m_inst)) struct DotNetScriptLookupInfo { String class_namespace; String class_name; GDMonoClass *script_class = nullptr; DotNetScriptLookupInfo() {} // Required by HashMap... DotNetScriptLookupInfo(const String &p_class_namespace, const String &p_class_name, GDMonoClass *p_script_class) : class_namespace(p_class_namespace), class_name(p_class_name), script_class(p_script_class) { } }; class CSharpScript : public Script { GDCLASS(CSharpScript, Script); public: struct SignalParameter { String name; Variant::Type type; bool nil_is_variant = false; }; struct EventSignal { GDMonoField *field = nullptr; GDMonoMethod *invoke_method = nullptr; Vector parameters; }; private: friend class CSharpInstance; friend class CSharpLanguage; friend struct CSharpScriptDepSort; bool tool = false; bool valid = false; bool builtin; GDMonoClass *base = nullptr; GDMonoClass *native = nullptr; GDMonoClass *script_class = nullptr; Ref base_cache; // TODO what's this for? Set instances; #ifdef GD_MONO_HOT_RELOAD struct StateBackup { // TODO // Replace with buffer containing the serialized state of managed scripts. // Keep variant state backup to use only with script instance placeholders. List> properties; List> event_signals; }; Set pending_reload_instances; Map pending_reload_state; StringName tied_class_name_for_reload; StringName tied_class_namespace_for_reload; #endif String source; StringName name; SelfList script_list = this; Map> _signals; Map event_signals; bool signals_invalidated = true; Vector rpc_functions; #ifdef TOOLS_ENABLED List exported_members_cache; // members_cache Map exported_members_defval_cache; // member_default_values_cache Set placeholders; bool source_changed_cache = false; bool placeholder_fallback_enabled = false; bool exports_invalidated = true; void _update_exports_values(Map &values, List &propnames); void _update_member_info_no_exports(); void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder) override; #endif #if defined(TOOLS_ENABLED) || defined(DEBUG_ENABLED) Set exported_members_names; #endif OrderedHashMap member_info; void _clear(); void _update_name(); void load_script_signals(GDMonoClass *p_class, GDMonoClass *p_native_class); bool _get_signal(GDMonoClass *p_class, GDMonoMethod *p_delegate_invoke, Vector ¶ms); bool _update_exports(PlaceHolderScriptInstance *p_instance_to_update = nullptr); bool _get_member_export(IMonoClassMember *p_member, bool p_inspect_export, PropertyInfo &r_prop_info, bool &r_exported); #ifdef TOOLS_ENABLED 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); #endif CSharpInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_is_ref_counted, Callable::CallError &r_error); Variant _new(const Variant **p_args, int p_argcount, Callable::CallError &r_error); // Do not use unless you know what you are doing friend void GDMonoInternals::tie_managed_to_unmanaged(MonoObject *, Object *); static Ref create_for_managed_type(GDMonoClass *p_class, GDMonoClass *p_native); static void update_script_class_info(Ref p_script); static void initialize_for_managed_type(Ref p_script, GDMonoClass *p_class, GDMonoClass *p_native); Multiplayer::RPCMode _member_get_rpc_mode(IMonoClassMember *p_member) const; protected: static void _bind_methods(); Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override; void _resource_path_changed() override; bool _get(const StringName &p_name, Variant &r_ret) const; bool _set(const StringName &p_name, const Variant &p_value); void _get_property_list(List *p_properties) const; public: bool can_instantiate() const override; StringName get_instance_base_type() const override; ScriptInstance *instance_create(Object *p_this) override; PlaceHolderScriptInstance *placeholder_instance_create(Object *p_this) override; bool instance_has(const Object *p_this) const override; bool has_source_code() const override; String get_source_code() const override; void set_source_code(const String &p_code) override; #ifdef TOOLS_ENABLED virtual const Vector &get_documentation() const override { // TODO static Vector docs; return docs; } #endif // TOOLS_ENABLED Error reload(bool p_keep_state = false) override; bool has_script_signal(const StringName &p_signal) const override; void get_script_signal_list(List *r_signals) const override; bool get_property_default_value(const StringName &p_property, Variant &r_value) const override; void get_script_property_list(List *r_list) const override; void update_exports() override; void get_members(Set *p_members) override; bool is_tool() const override { return tool; } bool is_valid() const override { return valid; } bool inherits_script(const Ref