/**************************************************************************/ /* csharp_script.h */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* 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 "mono_gc_handle.h" #include "mono_gd/gd_mono.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" #ifdef TOOLS_ENABLED #include "editor/plugins/editor_plugin.h" #endif class CSharpScript; class CSharpInstance; class CSharpLanguage; template TScriptInstance *cast_script_instance(ScriptInstance *p_inst) { return dynamic_cast(p_inst); } #define CAST_CSHARP_INSTANCE(m_inst) (cast_script_instance(m_inst)) class CSharpScript : public Script { GDCLASS(CSharpScript, Script); friend class CSharpInstance; friend class CSharpLanguage; public: struct TypeInfo { /** * Name of the C# class. */ String class_name; /** * Path to the icon that will be used for this class by the editor. */ String icon_path; /** * Script is marked as tool and runs in the editor. */ bool is_tool = false; /** * Script is marked as global class and will be registered in the editor. * Registered classes can be created using certain editor dialogs and * can be referenced by name from other languages that support the feature. */ bool is_global_class = false; /** * Script is declared abstract. */ bool is_abstract = false; /** * The C# type that corresponds to this script is a constructed generic type. * E.g.: `Dictionary` */ bool is_constructed_generic_type = false; /** * The C# type that corresponds to this script is a generic type definition. * E.g.: `Dictionary<,>` */ bool is_generic_type_definition = false; /** * The C# type that corresponds to this script contains generic type parameters, * regardless of whether the type parameters are bound or not. */ bool is_generic() const { return is_constructed_generic_type || is_generic_type_definition; } /** * Check if the script can be instantiated. * C# types can't be instantiated if they are abstract or contain generic * type parameters, but a CSharpScript is still created for them. */ bool can_instantiate() const { return !is_abstract && !is_generic_type_definition; } }; private: /** * Contains the C# type information for this script. */ TypeInfo type_info; /** * Scripts are valid when the corresponding C# class is found and used * to extract the script info using the [update_script_class_info] method. */ bool valid = false; /** * Scripts extract info from the C# class in the reload methods but, * if the reload is not invalidated, then the current extracted info * is still valid and there's no need to reload again. */ bool reload_invalidated = false; /** * Base script that this script derives from, or null if it derives from a * native Godot class. */ Ref base_script; HashSet 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; Dictionary event_signals; }; HashSet pending_reload_instances; RBMap pending_reload_state; bool was_tool_before_reload = false; HashSet pending_replace_placeholders; #endif /** * Script source code. */ String source; SelfList script_list = this; Dictionary rpc_config; struct EventSignalInfo { StringName name; // MethodInfo stores a string... MethodInfo method_info; }; struct CSharpMethodInfo { StringName name; // MethodInfo stores a string... MethodInfo method_info; }; Vector event_signals; Vector methods; #ifdef TOOLS_ENABLED List exported_members_cache; // members_cache HashMap exported_members_defval_cache; // member_default_values_cache HashSet placeholders; bool source_changed_cache = false; bool placeholder_fallback_enabled = false; bool exports_invalidated = true; void _update_exports_values(HashMap &values, List &propnames); void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder) override; #endif #if defined(TOOLS_ENABLED) || defined(DEBUG_ENABLED) HashSet exported_members_names; #endif HashMap member_info; void _clear(); static void GD_CLR_STDCALL _add_property_info_list_callback(CSharpScript *p_script, const String *p_current_class_name, void *p_props, int32_t p_count); #ifdef TOOLS_ENABLED static void GD_CLR_STDCALL _add_property_default_values_callback(CSharpScript *p_script, void *p_def_vals, int32_t p_count); #endif bool _update_exports(PlaceHolderScriptInstance *p_instance_to_update = nullptr); 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 static void update_script_class_info(Ref p_script); void _get_script_signal_list(List *r_signals, bool p_include_base) const; protected: static void _bind_methods(); 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: static void reload_registered_script(Ref p_script); 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 Vector get_documentation() const override { // TODO Vector docs; return docs; } virtual String get_class_icon_path() const override { return type_info.icon_path; } #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(HashSet *p_members) override; bool is_tool() const override { return type_info.is_tool; } bool is_valid() const override { return valid; } bool is_abstract() const override { return type_info.is_abstract; } bool inherits_script(const Ref