From 8ca5b52f6528c94eaac4e23a087863454745922a Mon Sep 17 00:00:00 2001 From: Jovan Gerodetti Date: Wed, 25 Oct 2023 01:36:22 +0200 Subject: [PATCH] Implementation of godots GDExtensionScriptInstance --- godot-codegen/src/codegen_special_cases.rs | 2 + godot-core/src/builtin/meta/mod.rs | 48 +++ godot-core/src/builtin/mod.rs | 2 + godot-core/src/builtin/script.rs | 480 +++++++++++++++++++++ 4 files changed, 532 insertions(+) create mode 100644 godot-core/src/builtin/script.rs diff --git a/godot-codegen/src/codegen_special_cases.rs b/godot-codegen/src/codegen_special_cases.rs index 6bdb2141c..297c17c0f 100644 --- a/godot-codegen/src/codegen_special_cases.rs +++ b/godot-codegen/src/codegen_special_cases.rs @@ -157,6 +157,8 @@ const SELECTED_CLASSES: &[&str] = &[ "ResourceLoader", "RigidBody2D", "SceneTree", + "Script", + "ScriptLanguage", "Sprite2D", "SpriteFrames", "TextServer", diff --git a/godot-core/src/builtin/meta/mod.rs b/godot-core/src/builtin/meta/mod.rs index d668a2bcc..6fcba40a0 100644 --- a/godot-core/src/builtin/meta/mod.rs +++ b/godot-core/src/builtin/meta/mod.rs @@ -270,3 +270,51 @@ impl PropertyInfo { } } } + +#[derive(Debug)] +pub struct MethodInfo { + pub id: i32, + pub method_name: StringName, + pub class_name: ClassName, + pub return_type: PropertyInfo, + pub arguments: Vec, + pub default_arguments: Vec, + pub flags: global::MethodFlags, +} + +impl MethodInfo { + pub fn method_sys(&self) -> sys::GDExtensionMethodInfo { + use crate::obj::EngineEnum as _; + + let argument_count = self.arguments.len() as u32; + let argument_vec = self + .arguments + .iter() + .map(|arg| arg.property_sys()) + .collect::>() + .into_boxed_slice(); + + let arguments = unsafe { (*Box::into_raw(argument_vec)).as_mut_ptr() }; + + let default_argument_count = self.default_arguments.len() as u32; + let default_argument_vec = self + .default_arguments + .iter() + .map(|arg| arg.var_sys()) + .collect::>() + .into_boxed_slice(); + + let default_arguments = unsafe { (*Box::into_raw(default_argument_vec)).as_mut_ptr() }; + + sys::GDExtensionMethodInfo { + id: self.id, + name: self.method_name.string_sys(), + return_value: self.return_type.property_sys(), + argument_count, + arguments, + default_argument_count, + default_arguments, + flags: u32::try_from(self.flags.ord()).expect("flags should be valid"), + } + } +} diff --git a/godot-core/src/builtin/mod.rs b/godot-core/src/builtin/mod.rs index 438b35fca..80592df7c 100644 --- a/godot-core/src/builtin/mod.rs +++ b/godot-core/src/builtin/mod.rs @@ -50,6 +50,7 @@ pub use real_inner::*; pub use rect2::*; pub use rect2i::*; pub use rid::*; +pub use script::*; pub use string::*; pub use transform2d::*; pub use transform3d::*; @@ -91,6 +92,7 @@ mod quaternion; mod rect2; mod rect2i; mod rid; +mod script; mod string; mod transform2d; mod transform3d; diff --git a/godot-core/src/builtin/script.rs b/godot-core/src/builtin/script.rs new file mode 100644 index 000000000..853852488 --- /dev/null +++ b/godot-core/src/builtin/script.rs @@ -0,0 +1,480 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +use std::{cell::RefCell, rc::Rc}; + +use godot_ffi::{ + get_interface, GDExtensionCallErrorType, GDExtensionScriptInstanceDataPtr, + GDExtensionScriptInstancePtr, VariantType, +}; + +use crate::{ + engine::{Object, Script, ScriptLanguage}, + obj::Gd, +}; + +use super::{ + meta::{MethodInfo, PropertyInfo}, + GString, StringName, Variant, +}; + +pub trait ScriptInstance { + fn class_name(&self) -> GString; + fn set(&mut self, name: StringName, value: &Variant) -> bool; + fn get(&self, name: StringName) -> Option; + fn property_list(&self) -> Rc>; + fn method_list(&self) -> Rc>; + fn call( + &mut self, + method: StringName, + args: &[&Variant], + ) -> Result; + + fn is_placeholder(&self) -> bool; + fn has_method(&self, method: StringName) -> bool; + fn get_script(&self) -> Gd