From 360dea5348652d8806d33598e111651afb3d193a Mon Sep 17 00:00:00 2001 From: reduz Date: Sat, 26 Mar 2022 16:48:43 +0100 Subject: [PATCH] Add GDExtension support to Script * Ability to create script languages from GDExtension * Some additions to gdnative_extension.h to make this happen * Moved the GDExtension binder to core This now allows creating scripting languages from GDExtension, with the same ease as if it was a module. It replaces the old PluginScript from Godot 3.x. Warning: GodotCPP will need to be updated to support this (it may be a bit of work as ScriptInstance needs to be created over there again). --- core/core_bind.cpp | 16 + core/core_bind.h | 5 + core/extension/SCsub | 6 + core/extension/gdnative_interface.cpp | 12 + core/extension/gdnative_interface.h | 88 ++ .../extension}/make_wrappers.py | 0 core/object/class_db.cpp | 9 +- core/object/script_language.h | 97 +- core/object/script_language_extension.cpp | 175 ++++ core/object/script_language_extension.h | 832 ++++++++++++++++++ core/register_core_types.cpp | 6 + doc/classes/Engine.xml | 17 + doc/classes/Range.xml | 2 +- doc/classes/ScriptExtension.xml | 173 ++++ doc/classes/ScriptLanguage.xml | 9 + doc/classes/ScriptLanguageExtension.xml | 404 +++++++++ editor/code_editor.cpp | 26 +- editor/code_editor.h | 6 +- editor/editor_file_system.cpp | 2 +- editor/plugins/script_editor_plugin.cpp | 12 +- editor/plugins/script_text_editor.cpp | 20 +- editor/plugins/script_text_editor.h | 4 +- editor/plugins/shader_editor_plugin.cpp | 2 +- editor/plugins/shader_editor_plugin.h | 2 +- modules/gdscript/gdscript.h | 4 +- modules/gdscript/gdscript_editor.cpp | 204 ++--- .../gdscript_text_document.cpp | 26 +- .../language_server/gdscript_workspace.cpp | 4 +- .../language_server/gdscript_workspace.h | 2 +- modules/mono/csharp_script.h | 4 +- modules/visual_script/visual_script.h | 4 +- scene/gui/code_edit.cpp | 30 +- scene/gui/code_edit.h | 8 +- scene/gui/range.cpp | 2 +- servers/extensions/SCsub | 5 - .../extensions/physics_server_3d_extension.h | 2 +- servers/rendering/shader_language.cpp | 64 +- servers/rendering/shader_language.h | 2 +- 38 files changed, 2019 insertions(+), 267 deletions(-) rename {servers/extensions => core/extension}/make_wrappers.py (100%) create mode 100644 core/object/script_language_extension.cpp create mode 100644 core/object/script_language_extension.h create mode 100644 doc/classes/ScriptExtension.xml create mode 100644 doc/classes/ScriptLanguage.xml create mode 100644 doc/classes/ScriptLanguageExtension.xml diff --git a/core/core_bind.cpp b/core/core_bind.cpp index 7150459d844..8b060b09f97 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -2296,6 +2296,18 @@ Vector Engine::get_singleton_list() const { return ret; } +void Engine::register_script_language(ScriptLanguage *p_language) { + ScriptServer::register_language(p_language); +} + +int Engine::get_script_language_count() { + return ScriptServer::get_language_count(); +} + +ScriptLanguage *Engine::get_script_language(int p_index) const { + return ScriptServer::get_language(p_index); +} + void Engine::set_editor_hint(bool p_enabled) { ::Engine::get_singleton()->set_editor_hint(p_enabled); } @@ -2347,6 +2359,10 @@ void Engine::_bind_methods() { ClassDB::bind_method(D_METHOD("unregister_singleton", "name"), &Engine::unregister_singleton); ClassDB::bind_method(D_METHOD("get_singleton_list"), &Engine::get_singleton_list); + ClassDB::bind_method(D_METHOD("register_script_language", "language"), &Engine::register_script_language); + ClassDB::bind_method(D_METHOD("get_script_language_count"), &Engine::get_script_language_count); + ClassDB::bind_method(D_METHOD("get_script_language", "index"), &Engine::get_script_language); + ClassDB::bind_method(D_METHOD("is_editor_hint"), &Engine::is_editor_hint); ClassDB::bind_method(D_METHOD("set_print_error_messages", "enabled"), &Engine::set_print_error_messages); diff --git a/core/core_bind.h b/core/core_bind.h index a0fdf26dff8..2942f54a15d 100644 --- a/core/core_bind.h +++ b/core/core_bind.h @@ -38,6 +38,7 @@ #include "core/io/image.h" #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" +#include "core/object/script_language.h" #include "core/os/os.h" #include "core/os/semaphore.h" #include "core/os/thread.h" @@ -664,6 +665,10 @@ public: void unregister_singleton(const StringName &p_name); Vector get_singleton_list() const; + void register_script_language(ScriptLanguage *p_language); + int get_script_language_count(); + ScriptLanguage *get_script_language(int p_index) const; + void set_editor_hint(bool p_enabled); bool is_editor_hint() const; diff --git a/core/extension/SCsub b/core/extension/SCsub index a3a54250c12..23727c1b769 100644 --- a/core/extension/SCsub +++ b/core/extension/SCsub @@ -2,6 +2,12 @@ Import("env") +import make_wrappers +from platform_methods import run_in_subprocess + +env.CommandNoCache(["ext_wrappers.gen.inc"], "make_wrappers.py", run_in_subprocess(make_wrappers.run)) + + env_extension = env.Clone() env_extension.add_source_files(env.core_sources, "*.cpp") diff --git a/core/extension/gdnative_interface.cpp b/core/extension/gdnative_interface.cpp index d0461611ec5..b5b340731d7 100644 --- a/core/extension/gdnative_interface.cpp +++ b/core/extension/gdnative_interface.cpp @@ -32,6 +32,7 @@ #include "core/config/engine.h" #include "core/object/class_db.h" +#include "core/object/script_language_extension.h" #include "core/os/memory.h" #include "core/variant/variant.h" #include "core/version.h" @@ -864,6 +865,13 @@ static GDObjectInstanceID gdnative_object_get_instance_id(const GDNativeObjectPt return (GDObjectInstanceID)o->get_instance_id(); } +static GDNativeScriptInstancePtr gdnative_script_instance_create(const GDNativeExtensionScriptInstanceInfo *p_info, GDNativeExtensionScriptInstanceDataPtr p_instance_data) { + ScriptInstanceExtension *script_instance_extension = memnew(ScriptInstanceExtension); + script_instance_extension->instance = p_instance_data; + script_instance_extension->native_info = p_info; + return reinterpret_cast(script_instance_extension); +} + static GDNativeMethodBindPtr gdnative_classdb_get_method_bind(const char *p_classname, const char *p_methodname, GDNativeInt p_hash) { MethodBind *mb = ClassDB::get_method(StringName(p_classname), StringName(p_methodname)); ERR_FAIL_COND_V(!mb, nullptr); @@ -1032,6 +1040,10 @@ void gdnative_setup_interface(GDNativeInterface *p_interface) { gdni.object_get_instance_from_id = gdnative_object_get_instance_from_id; gdni.object_get_instance_id = gdnative_object_get_instance_id; + /* SCRIPT INSTANCE */ + + gdni.script_instance_create = gdnative_script_instance_create; + /* CLASSDB */ gdni.classdb_construct_object = gdnative_classdb_construct_object; diff --git a/core/extension/gdnative_interface.h b/core/extension/gdnative_interface.h index cc2957ec56b..50c747a7b4d 100644 --- a/core/extension/gdnative_interface.h +++ b/core/extension/gdnative_interface.h @@ -205,6 +205,17 @@ typedef struct { uint32_t usage; } GDNativePropertyInfo; +typedef struct { + const char *name; + GDNativePropertyInfo return_value; + uint32_t flags; // From GDNativeExtensionClassMethodFlags + int32_t id; + GDNativePropertyInfo *arguments; + uint32_t argument_count; + GDNativeVariantPtr default_arguments; + uint32_t default_argument_count; +} GDNativeMethodInfo; + typedef const GDNativePropertyInfo *(*GDNativeExtensionClassGetPropertyList)(GDExtensionClassInstancePtr p_instance, uint32_t *r_count); typedef void (*GDNativeExtensionClassFreePropertyList)(GDExtensionClassInstancePtr p_instance, const GDNativePropertyInfo *p_list); typedef void (*GDNativeExtensionClassNotification)(GDExtensionClassInstancePtr p_instance, int32_t p_what); @@ -289,6 +300,79 @@ typedef struct { GDNativeVariantPtr *default_arguments; } GDNativeExtensionClassMethodInfo; +/* SCRIPT INSTANCE EXTENSION */ + +typedef void *GDNativeExtensionScriptInstanceDataPtr; // Pointer to custom ScriptInstance native implementation + +typedef GDNativeBool (*GDNativeExtensionScriptInstanceSet)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name, const GDNativeVariantPtr p_value); +typedef GDNativeBool (*GDNativeExtensionScriptInstanceGet)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name, GDNativeVariantPtr r_ret); +typedef const GDNativePropertyInfo *(*GDNativeExtensionScriptInstanceGetPropertyList)(GDNativeExtensionScriptInstanceDataPtr p_instance, uint32_t *r_count); +typedef void (*GDNativeExtensionScriptInstanceFreePropertyList)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativePropertyInfo *p_list); +typedef GDNativeVariantType (*GDNativeExtensionScriptInstanceGetPropertyType)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name, GDNativeBool *r_is_valid); + +typedef GDNativeObjectPtr (*GDNativeExtensionScriptInstanceGetOwner)(GDNativeExtensionScriptInstanceDataPtr p_instance); +typedef void (*GDNativeExtensionScriptInstancePropertyStateAdd)(const GDNativeStringNamePtr p_name, const GDNativeVariantPtr p_value, void *p_userdata); +typedef void (*GDNativeExtensionScriptInstanceGetPropertyState)(GDNativeExtensionScriptInstanceDataPtr p_instance, GDNativeExtensionScriptInstancePropertyStateAdd p_add_func, void *p_userdata); + +typedef const GDNativeMethodInfo *(*GDNativeExtensionScriptInstanceGetMethodList)(GDNativeExtensionScriptInstanceDataPtr p_instance, uint32_t *r_count); +typedef void (*GDNativeExtensionScriptInstanceFreeMethodList)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeMethodInfo *p_list); + +typedef GDNativeBool (*GDNativeExtensionScriptInstanceHasMethod)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name); + +typedef void (*GDNativeExtensionScriptInstanceCall)(GDNativeExtensionScriptInstanceDataPtr p_self, const GDNativeStringNamePtr p_method, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeVariantPtr r_return, GDNativeCallError *r_error); +typedef void (*GDNativeExtensionScriptInstanceNotification)(GDNativeExtensionScriptInstanceDataPtr p_instance, int32_t p_what); +typedef const char *(*GDNativeExtensionScriptInstanceToString)(GDNativeExtensionScriptInstanceDataPtr p_instance, GDNativeBool *r_is_valid); + +typedef void (*GDNativeExtensionScriptInstanceRefCountIncremented)(GDNativeExtensionScriptInstanceDataPtr p_instance); +typedef GDNativeBool (*GDNativeExtensionScriptInstanceRefCountDecremented)(GDNativeExtensionScriptInstanceDataPtr p_instance); + +typedef GDNativeObjectPtr (*GDNativeExtensionScriptInstanceGetScript)(GDNativeExtensionScriptInstanceDataPtr p_instance); +typedef GDNativeBool (*GDNativeExtensionScriptInstanceIsPlaceholder)(GDNativeExtensionScriptInstanceDataPtr p_instance); + +typedef void *GDNativeExtensionScriptLanguagePtr; + +typedef GDNativeExtensionScriptLanguagePtr (*GDNativeExtensionScriptInstanceGetLanguage)(GDNativeExtensionScriptInstanceDataPtr p_instance); + +typedef void (*GDNativeExtensionScriptInstanceFree)(GDNativeExtensionScriptInstanceDataPtr p_instance); + +typedef void *GDNativeScriptInstancePtr; // Pointer to ScriptInstance. + +typedef struct { + GDNativeExtensionScriptInstanceSet set_func; + GDNativeExtensionScriptInstanceGet get_func; + GDNativeExtensionScriptInstanceGetPropertyList get_property_list_func; + GDNativeExtensionScriptInstanceFreePropertyList free_property_list_func; + GDNativeExtensionScriptInstanceGetPropertyType get_property_type_func; + + GDNativeExtensionScriptInstanceGetOwner get_owner_func; + GDNativeExtensionScriptInstanceGetPropertyState get_property_state_func; + + GDNativeExtensionScriptInstanceGetMethodList get_method_list_func; + GDNativeExtensionScriptInstanceFreeMethodList free_method_list_func; + + GDNativeExtensionScriptInstanceHasMethod has_method_func; + + GDNativeExtensionScriptInstanceCall call_func; + GDNativeExtensionScriptInstanceNotification notification_func; + + GDNativeExtensionScriptInstanceToString to_string_func; + + GDNativeExtensionScriptInstanceRefCountIncremented refcount_incremented_func; + GDNativeExtensionScriptInstanceRefCountDecremented refcount_decremented_func; + + GDNativeExtensionScriptInstanceGetScript get_script_func; + + GDNativeExtensionScriptInstanceIsPlaceholder is_placeholder_func; + + GDNativeExtensionScriptInstanceSet set_fallback_func; + GDNativeExtensionScriptInstanceGet get_fallback_func; + + GDNativeExtensionScriptInstanceGetLanguage get_language_func; + + GDNativeExtensionScriptInstanceFree free_func; + +} GDNativeExtensionScriptInstanceInfo; + /* INTERFACE */ typedef struct { @@ -441,6 +525,10 @@ typedef struct { GDNativeObjectPtr (*object_get_instance_from_id)(GDObjectInstanceID p_instance_id); GDObjectInstanceID (*object_get_instance_id)(const GDNativeObjectPtr p_object); + /* SCRIPT INSTANCE */ + + GDNativeScriptInstancePtr (*script_instance_create)(const GDNativeExtensionScriptInstanceInfo *p_info, GDNativeExtensionScriptInstanceDataPtr p_instance_data); + /* CLASSDB */ GDNativeObjectPtr (*classdb_construct_object)(const char *p_classname); /* The passed class must be a built-in godot class, or an already-registered extension class. In both case, object_set_instance should be called to fully initialize the object. */ GDNativeMethodBindPtr (*classdb_get_method_bind)(const char *p_classname, const char *p_methodname, GDNativeInt p_hash); diff --git a/servers/extensions/make_wrappers.py b/core/extension/make_wrappers.py similarity index 100% rename from servers/extensions/make_wrappers.py rename to core/extension/make_wrappers.py diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index e09c6cb97ca..51ca06d9d26 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -1485,9 +1485,10 @@ void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_ if (p_object_core) { mi.flags |= METHOD_FLAG_OBJECT_CORE; } - if (p_arg_names.size()) { + + if (!p_object_core) { if (p_arg_names.size() != mi.arguments.size()) { - WARN_PRINT("Mismatch argument name count for virtual function: " + String(p_class) + "::" + p_method.name); + WARN_PRINT("Mismatch argument name count for virtual method: " + String(p_class) + "::" + p_method.name); } else { for (int i = 0; i < p_arg_names.size(); i++) { mi.arguments[i].name = p_arg_names[i]; @@ -1495,6 +1496,10 @@ void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_ } } + if (classes[p_class].virtual_methods_map.has(p_method.name)) { + // overloading not supported + ERR_FAIL_MSG("Virtual method already bound '" + String(p_class) + "::" + p_method.name + "'."); + } classes[p_class].virtual_methods.push_back(mi); classes[p_class].virtual_methods_map[p_method.name] = mi; diff --git a/core/object/script_language.h b/core/object/script_language.h index 2122f785b6d..6161a0fc0f6 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -132,7 +132,7 @@ public: virtual Error reload(bool p_keep_state = false) = 0; #ifdef TOOLS_ENABLED - virtual const Vector &get_documentation() const = 0; + virtual Vector get_documentation() const = 0; #endif // TOOLS_ENABLED virtual bool has_method(const StringName &p_method) const = 0; @@ -212,44 +212,12 @@ public: virtual void property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid); virtual Variant property_get_fallback(const StringName &p_name, bool *r_valid); - virtual const Vector get_rpc_methods() const = 0; + virtual const Vector get_rpc_methods() const { return get_script()->get_rpc_methods(); } virtual ScriptLanguage *get_language() = 0; virtual ~ScriptInstance(); }; -struct ScriptCodeCompletionOption { - /* Keep enum in Sync with: */ - /* /scene/gui/code_edit.h - CodeEdit::CodeCompletionKind */ - enum Kind { - KIND_CLASS, - KIND_FUNCTION, - KIND_SIGNAL, - KIND_VARIABLE, - KIND_MEMBER, - KIND_ENUM, - KIND_CONSTANT, - KIND_NODE_PATH, - KIND_FILE_PATH, - KIND_PLAIN_TEXT, - }; - Kind kind = KIND_PLAIN_TEXT; - String display; - String insert_text; - Color font_color; - RES icon; - Variant default_value; - Vector> matches; - - ScriptCodeCompletionOption() {} - - ScriptCodeCompletionOption(const String &p_text, Kind p_kind) { - display = p_text; - insert_text = p_text; - kind = p_kind; - } -}; - class ScriptCodeCompletionCache { static ScriptCodeCompletionCache *singleton; @@ -261,7 +229,8 @@ public: virtual ~ScriptCodeCompletionCache() {} }; -class ScriptLanguage { +class ScriptLanguage : public Object { + GDCLASS(ScriptLanguage, Object) public: virtual String get_name() const = 0; @@ -326,19 +295,55 @@ public: virtual Error open_in_external_editor(const Ref