From 11f8c4cbd36f76ae82b0f92706cde5402d3fa7b8 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Wed, 7 Aug 2024 14:41:02 -0500 Subject: [PATCH] Add `ScriptInstanceExtension` class which mimicks the API of Godot's internal `ScriptInstance` class --- binding_generator.py | 17 + .../classes/script_instance_extension.hpp | 109 ++++++ src/classes/script_instance_extension.cpp | 316 ++++++++++++++++++ 3 files changed, 442 insertions(+) create mode 100644 include/godot_cpp/classes/script_instance_extension.hpp create mode 100644 src/classes/script_instance_extension.cpp diff --git a/binding_generator.py b/binding_generator.py index e7609b2c..0ad18e36 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -736,6 +736,23 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl result.append("") result.append("public:") + if class_name == "Callable": + result.append("\tstruct CallError {") + result.append("\t\tenum Error {") + result.append("\t\t\tCALL_OK,") + result.append("\t\t\tCALL_ERROR_INVALID_METHOD,") + result.append("\t\t\tCALL_ERROR_INVALID_ARGUMENT,") + result.append("\t\t\tCALL_ERROR_TOO_MANY_ARGUMENTS,") + result.append("\t\t\tCALL_ERROR_TOO_FEW_ARGUMENTS,") + result.append("\t\t\tCALL_ERROR_INSTANCE_IS_NULL,") + result.append("\t\t\tCALL_ERROR_METHOD_NOT_CONST,") + result.append("\t\t};") + result.append("\t\tError error = Error::CALL_OK;") + result.append("\t\tint argument = 0;") + result.append("\t\tint expected = 0;") + result.append("\t};") + result.append("") + result.append( f"\t_FORCE_INLINE_ GDExtensionTypePtr _native_ptr() const {{ return const_cast(&opaque); }}" ) diff --git a/include/godot_cpp/classes/script_instance_extension.hpp b/include/godot_cpp/classes/script_instance_extension.hpp new file mode 100644 index 00000000..cca99db6 --- /dev/null +++ b/include/godot_cpp/classes/script_instance_extension.hpp @@ -0,0 +1,109 @@ +/**************************************************************************/ +/* script_instance_extension.hpp */ +/**************************************************************************/ +/* 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 GODOT_SCRIPT_INSTANCE_EXTENSION_HPP +#define GODOT_SCRIPT_INSTANCE_EXTENSION_HPP + +#include +#include +#include + +namespace godot { + +class Script; +class ScriptLanguage; + +class ScriptInstanceExtension { + static GDExtensionScriptInstanceInfo3 script_instance_info; + + // Used to keep the refcounted properties alive after passing pointers to the data back to Godot. + static thread_local List property_list; + static thread_local List method_list; + + static GDExtensionBool gdextension_script_instance_set(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value); + static GDExtensionBool gdextension_script_instance_get(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret); + static const GDExtensionPropertyInfo *gdextension_script_instance_get_property_list(GDExtensionScriptInstanceDataPtr p_instance, uint32_t *r_count); + static void gdextension_script_instance_free_property_list(GDExtensionScriptInstanceDataPtr p_instance, const GDExtensionPropertyInfo *p_list, uint32_t p_count); + static GDExtensionVariantType gdextension_script_instance_get_property_type(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionBool *r_is_valid); + static GDExtensionBool gdextension_script_instance_validate_property(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionPropertyInfo *p_property); + static GDExtensionBool gdextension_script_instance_property_can_revert(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name); + static GDExtensionBool gdextension_script_instance_property_get_revert(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret); + static GDExtensionObjectPtr gdextension_script_instance_get_owner(GDExtensionScriptInstanceDataPtr p_instance); + static void gdextension_script_instance_get_property_state(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionScriptInstancePropertyStateAdd p_add_func, void *p_userdata); + static const GDExtensionMethodInfo *gdextension_script_instance_get_method_list(GDExtensionScriptInstanceDataPtr p_instance, uint32_t *r_count); + static void gdextension_script_instance_free_method_list(GDExtensionScriptInstanceDataPtr p_instance, const GDExtensionMethodInfo *p_list, uint32_t p_count); + static GDExtensionBool gdextension_script_instance_has_method(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name); + static GDExtensionInt gdextension_script_instance_get_method_argument_count(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionBool *r_is_valid); + static void gdextension_script_instance_call(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_method, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionVariantPtr r_return, GDExtensionCallError *r_error); + static void gdextension_script_instance_notification(GDExtensionScriptInstanceDataPtr p_instance, int32_t p_what, GDExtensionBool p_reversed); + static void gdextension_script_instance_to_string(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionBool *r_is_valid, GDExtensionStringPtr r_out); + static void gdextension_script_instance_refcount_incremented(GDExtensionScriptInstanceDataPtr p_instance); + static GDExtensionBool gdextension_script_instance_refcount_decremented(GDExtensionScriptInstanceDataPtr p_instance); + static GDExtensionObjectPtr gdextension_script_instance_get_script(GDExtensionScriptInstanceDataPtr p_instance); + static GDExtensionBool gdextension_script_instance_is_placeholder(GDExtensionScriptInstanceDataPtr p_instance); + static GDExtensionBool gdextension_script_instance_set_fallback(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value); + static GDExtensionBool gdextension_script_instance_get_fallback(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret); + static GDExtensionScriptLanguagePtr gdextension_script_instance_get_language(GDExtensionScriptInstanceDataPtr p_instance); + static void gdextension_script_instance_free(GDExtensionScriptInstanceDataPtr p_instance); + +public: + static GDExtensionScriptInstancePtr create_native_instance(ScriptInstanceExtension *p_instance) { + return internal::gdextension_interface_script_instance_create3(&script_instance_info, p_instance); + } + + virtual bool set(const StringName &p_name, const Variant &p_value) = 0; + virtual bool get(const StringName &p_name, Variant &r_ret) const = 0; + virtual void get_property_list(List *p_properties) const = 0; + virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid) const = 0; + virtual void validate_property(PropertyInfo &p_property) const = 0; + virtual bool property_can_revert(const StringName &p_name) const = 0; + virtual bool property_get_revert(const StringName &p_name, Variant &r_ret) const = 0; + virtual Object *get_owner() = 0; + virtual void get_property_state(List> &state); + virtual void get_method_list(List *p_info) const = 0; + virtual bool has_method(const StringName &p_method) const = 0; + virtual int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const = 0; + virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) = 0; + virtual void notification(int p_notification, bool p_reversed) = 0; + virtual String to_string(bool *r_valid) = 0; + virtual void refcount_incremented() = 0; + virtual bool refcount_decremented() = 0; + virtual Ref