From b7a2d2477dbe7a0b6d5dcc868ba124b8e398bca1 Mon Sep 17 00:00:00 2001 From: LuoZhihao Date: Thu, 19 Jun 2025 04:16:28 +0800 Subject: [PATCH] Don't use deprecated methods and add `no-deprecated` feature --- godot-codegen/Cargo.toml | 1 + godot-codegen/src/generator/extension_interface.rs | 13 ++++++++++++- godot-core/Cargo.toml | 1 + godot-core/src/builtin/string/gstring.rs | 4 ++++ godot-core/src/classes/class_runtime.rs | 7 ++++++- godot-core/src/obj/bounds.rs | 8 ++++++-- godot-core/src/registry/callbacks.rs | 7 ++++++- godot-ffi/Cargo.toml | 1 + godot-ffi/src/interface_init.rs | 7 ++++++- godot/Cargo.toml | 1 + itest/rust/Cargo.toml | 1 + 11 files changed, 45 insertions(+), 6 deletions(-) diff --git a/godot-codegen/Cargo.toml b/godot-codegen/Cargo.toml index 55b391c27..9bd483ce8 100644 --- a/godot-codegen/Cargo.toml +++ b/godot-codegen/Cargo.toml @@ -20,6 +20,7 @@ api-custom = ["godot-bindings/api-custom"] api-custom-json = ["godot-bindings/api-custom-json"] experimental-godot-api = [] experimental-threads = [] +no-deprecated = [] [dependencies] godot-bindings = { path = "../godot-bindings", version = "=0.3.1" } diff --git a/godot-codegen/src/generator/extension_interface.rs b/godot-codegen/src/generator/extension_interface.rs index 0b3303d6e..91d60e13a 100644 --- a/godot-codegen/src/generator/extension_interface.rs +++ b/godot-codegen/src/generator/extension_interface.rs @@ -38,6 +38,7 @@ struct GodotFuncPtr { name: Ident, func_ptr_ty: Ident, doc: String, + is_deprecated: bool, } fn generate_proc_address_funcs(h_path: &Path) -> TokenStream { @@ -52,8 +53,14 @@ fn generate_proc_address_funcs(h_path: &Path) -> TokenStream { name, func_ptr_ty, doc, + is_deprecated, } = fptr; + if is_deprecated { + #[cfg(feature = "no-deprecated")] + continue; + } + let name_str = Literal::byte_string(format!("{name}\0").as_bytes()); let decl = quote! { @@ -149,6 +156,7 @@ fn parse_function_pointers(header_code: &str) -> Vec { name: ident(name.as_str()), func_ptr_ty: ident(funcptr_ty.as_str()), doc: doc.as_str().replace("\n *", "\n").trim().to_string(), + is_deprecated: doc.as_str().contains("@deprecated"), }); } @@ -170,7 +178,7 @@ fn test_parse_function_pointers() { /** * @name classdb_register_extension_class - * + * @deprecated This method is deprecated. * Registers an extension class in the ClassDB. * * Provided struct can be safely freed once the function returns. @@ -200,6 +208,7 @@ typedef void (*GDExtensionInterfaceClassdbRegisterExtensionClass)(GDExtensionCla assert_eq!( func_ptr.doc, r#" + @deprecated This method is deprecated. Registers an extension class in the ClassDB. Provided struct can be safely freed once the function returns. @@ -211,4 +220,6 @@ typedef void (*GDExtensionInterfaceClassdbRegisterExtensionClass)(GDExtensionCla "# .trim() ); + + assert!(func_ptr.is_deprecated); } diff --git a/godot-core/Cargo.toml b/godot-core/Cargo.toml index 33f46b5b6..8188ab681 100644 --- a/godot-core/Cargo.toml +++ b/godot-core/Cargo.toml @@ -23,6 +23,7 @@ double-precision = ["godot-codegen/double-precision"] experimental-godot-api = ["godot-codegen/experimental-godot-api"] experimental-threads = ["godot-ffi/experimental-threads", "godot-codegen/experimental-threads"] experimental-wasm-nothreads = ["godot-ffi/experimental-wasm-nothreads"] +no-deprecated = ["godot-ffi/no-deprecated"] debug-log = ["godot-ffi/debug-log"] trace = [] diff --git a/godot-core/src/builtin/string/gstring.rs b/godot-core/src/builtin/string/gstring.rs index 5d71a9f88..3ccc287c1 100644 --- a/godot-core/src/builtin/string/gstring.rs +++ b/godot-core/src/builtin/string/gstring.rs @@ -325,7 +325,11 @@ impl From<&str> for GString { unsafe { Self::new_with_string_uninit(|string_ptr| { + #[cfg(before_api = "4.3")] let ctor = interface_fn!(string_new_with_utf8_chars_and_len); + #[cfg(since_api = "4.3")] + let ctor = interface_fn!(string_new_with_utf8_chars_and_len2); + ctor( string_ptr, bytes.as_ptr() as *const std::ffi::c_char, diff --git a/godot-core/src/classes/class_runtime.rs b/godot-core/src/classes/class_runtime.rs index 0f77323f5..f276fbf31 100644 --- a/godot-core/src/classes/class_runtime.rs +++ b/godot-core/src/classes/class_runtime.rs @@ -60,9 +60,14 @@ pub(crate) fn construct_engine_object() -> Gd where T: GodotClass + Bounds, { + #[cfg(before_api = "4.4")] + let construct_fn = sys::interface_fn!(classdb_construct_object); + #[cfg(since_api = "4.4")] + let construct_fn = sys::interface_fn!(classdb_construct_object2); + // SAFETY: adhere to Godot API; valid class name and returned pointer is an object. unsafe { - let object_ptr = sys::interface_fn!(classdb_construct_object)(T::class_name().string_sys()); + let object_ptr = construct_fn(T::class_name().string_sys()); Gd::from_obj_sys(object_ptr) } } diff --git a/godot-core/src/obj/bounds.rs b/godot-core/src/obj/bounds.rs index 06bbabc51..8b5f54a76 100644 --- a/godot-core/src/obj/bounds.rs +++ b/godot-core/src/obj/bounds.rs @@ -389,9 +389,13 @@ impl Declarer for DeclEngine { where T: GodotDefault + Bounds, { + #[cfg(before_api = "4.4")] + let construct_fn = sys::interface_fn!(classdb_construct_object); + #[cfg(since_api = "4.4")] + let construct_fn = sys::interface_fn!(classdb_construct_object2); + unsafe { - let object_ptr = - sys::interface_fn!(classdb_construct_object)(T::class_name().string_sys()); + let object_ptr = construct_fn(T::class_name().string_sys()); Gd::from_obj_sys(object_ptr) } } diff --git a/godot-core/src/registry/callbacks.rs b/godot-core/src/registry/callbacks.rs index 708ffeaba..8621ad4d8 100644 --- a/godot-core/src/registry/callbacks.rs +++ b/godot-core/src/registry/callbacks.rs @@ -91,8 +91,13 @@ where T: GodotClass, F: FnOnce(Base) -> T, { + #[cfg(before_api = "4.4")] + let construct_fn = sys::interface_fn!(classdb_construct_object); + #[cfg(since_api = "4.4")] + let construct_fn = sys::interface_fn!(classdb_construct_object2); + let base_class_name = T::Base::class_name(); - let base_ptr = unsafe { interface_fn!(classdb_construct_object)(base_class_name.string_sys()) }; + let base_ptr = unsafe { construct_fn(base_class_name.string_sys()) }; match create_rust_part_for_existing_godot_part(make_user_instance, base_ptr) { Ok(_extension_ptr) => Ok(base_ptr), diff --git a/godot-ffi/Cargo.toml b/godot-ffi/Cargo.toml index 868ccd14d..c1d066356 100644 --- a/godot-ffi/Cargo.toml +++ b/godot-ffi/Cargo.toml @@ -16,6 +16,7 @@ codegen-lazy-fptrs = ["godot-codegen/codegen-lazy-fptrs"] experimental-godot-api = ["godot-codegen/experimental-godot-api"] experimental-threads = ["godot-codegen/experimental-threads"] experimental-wasm-nothreads = ["godot-bindings/experimental-wasm-nothreads"] +no-deprecated = ["godot-codegen/no-deprecated"] debug-log = [] api-custom = ["godot-bindings/api-custom"] diff --git a/godot-ffi/src/interface_init.rs b/godot-ffi/src/interface_init.rs index 6c2de4d81..72a3302cf 100644 --- a/godot-ffi/src/interface_init.rs +++ b/godot-ffi/src/interface_init.rs @@ -127,8 +127,13 @@ unsafe fn runtime_version_inner( *const std::ffi::c_char, ) -> sys::GDExtensionInterfaceFunctionPtr, ) -> sys::GDExtensionGodotVersion { + #[cfg(before_api = "4.5")] + let godot_version_addr = b"get_godot_version\0"; + #[cfg(since_api = "4.5")] + let godot_version_addr = b"get_godot_version2\0"; + // SAFETY: `self.0` is a valid `get_proc_address` pointer. - let get_godot_version = unsafe { get_proc_address(sys::c_str(b"get_godot_version\0")) }; //.expect("get_godot_version unexpectedly null"); + let get_godot_version = unsafe { get_proc_address(sys::c_str(godot_version_addr)) }; //.expect("get_godot_version unexpectedly null"); // SAFETY: `sys::GDExtensionInterfaceGetGodotVersion` is an `Option` of an `unsafe extern "C"` function pointer. let get_godot_version = diff --git a/godot/Cargo.toml b/godot/Cargo.toml index 7b2a61744..f1e435ef7 100644 --- a/godot/Cargo.toml +++ b/godot/Cargo.toml @@ -24,6 +24,7 @@ experimental-wasm-nothreads = ["godot-core/experimental-wasm-nothreads"] codegen-rustfmt = ["godot-core/codegen-rustfmt"] lazy-function-tables = ["godot-core/codegen-lazy-fptrs"] serde = ["godot-core/serde"] +no-deprecated = ["godot-core/no-deprecated"] register-docs = ["godot-macros/register-docs", "godot-core/register-docs"] diff --git a/itest/rust/Cargo.toml b/itest/rust/Cargo.toml index 0694779fb..cbc60f0b1 100644 --- a/itest/rust/Cargo.toml +++ b/itest/rust/Cargo.toml @@ -16,6 +16,7 @@ codegen-full = ["godot/__codegen-full"] codegen-full-experimental = ["codegen-full", "godot/experimental-godot-api"] experimental-threads = ["godot/experimental-threads"] register-docs = ["godot/register-docs"] +no-deprecated = ["godot/no-deprecated"] serde = ["dep:serde", "dep:serde_json", "godot/serde"] # Do not add features here that are 1:1 forwarded to the `godot` crate, unless they are needed by itest itself.