From 5f98714fa176be7f8730a4da80dd94a95e5e4080 Mon Sep 17 00:00:00 2001 From: Jovan Gerodetti Date: Fri, 17 Jan 2025 00:46:29 +0100 Subject: [PATCH] Provide safe way for implementing IScriptExtension::instance_has --- .../special_cases/codegen_special_cases.rs | 2 + godot-core/src/obj/script.rs | 41 ++++- itest/godot/ScriptInstanceTests.gd | 56 +++++-- .../script/script_instance_tests.rs | 147 +++++++++++++++++- 4 files changed, 225 insertions(+), 21 deletions(-) diff --git a/godot-codegen/src/special_cases/codegen_special_cases.rs b/godot-codegen/src/special_cases/codegen_special_cases.rs index 7e0eadfa5..a05352382 100644 --- a/godot-codegen/src/special_cases/codegen_special_cases.rs +++ b/godot-codegen/src/special_cases/codegen_special_cases.rs @@ -173,7 +173,9 @@ const SELECTED_CLASSES: &[&str] = &[ "SceneTreeTimer", "Script", "ScriptExtension", + "ScriptNameCasing", "ScriptLanguage", + "ScriptLanguageExtension", "Sprite2D", "SpriteFrames", "TextServer", diff --git a/godot-core/src/obj/script.rs b/godot-core/src/obj/script.rs index bbbfe3527..9b3d954cd 100644 --- a/godot-core/src/obj/script.rs +++ b/godot-core/src/obj/script.rs @@ -25,7 +25,7 @@ use godot_cell::panicking::{GdCell, MutGuard, RefGuard}; use godot_cell::blocking::{GdCell, MutGuard, RefGuard}; use crate::builtin::{GString, StringName, Variant, VariantType}; -use crate::classes::{Script, ScriptLanguage}; +use crate::classes::{Object, Script, ScriptLanguage}; use crate::meta::{MethodInfo, PropertyInfo}; use crate::obj::{Base, Gd, GodotClass}; use crate::sys; @@ -33,6 +33,11 @@ use crate::sys; #[cfg(before_api = "4.3")] use self::bounded_ptr_list::BoundedPtrList; +#[cfg(since_api = "4.2")] +use crate::classes::IScriptExtension; +#[cfg(since_api = "4.2")] +use crate::obj::Inherits; + /// Implement custom scripts that can be attached to objects in Godot. /// /// To use script instances, implement this trait for your own type. @@ -337,6 +342,40 @@ pub unsafe fn create_script_instance( } } +/// Checks if an instance of the script exists for a given object. +/// +/// This function both checks if the passed script matches the one currently assigned to the passed object, as well as verifies that +/// there is an instance for the script. +/// +/// Use this function to implement [`IScriptExtension::instance_has`](crate::classes::IScriptExtension::instance_has). +#[cfg(since_api = "4.2")] +pub fn script_instance_exists(object: &Gd, script: &Gd) -> bool +where + O: Inherits, + S: Inherits