Skip to content

Cargo feature to exclude deprecated GDExtension APIs #1208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions godot-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
13 changes: 12 additions & 1 deletion godot-codegen/src/generator/extension_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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! {
Expand Down Expand Up @@ -149,6 +156,7 @@ fn parse_function_pointers(header_code: &str) -> Vec<GodotFuncPtr> {
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"),
});
}

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -211,4 +220,6 @@ typedef void (*GDExtensionInterfaceClassdbRegisterExtensionClass)(GDExtensionCla
"#
.trim()
);

assert!(func_ptr.is_deprecated);
}
1 change: 1 addition & 0 deletions godot-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
4 changes: 4 additions & 0 deletions godot-core/src/builtin/string/gstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion godot-core/src/classes/class_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ pub(crate) fn construct_engine_object<T>() -> Gd<T>
where
T: GodotClass + Bounds<Declarer = bounds::DeclEngine>,
{
#[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)
}
}
Expand Down
8 changes: 6 additions & 2 deletions godot-core/src/obj/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,13 @@ impl Declarer for DeclEngine {
where
T: GodotDefault + Bounds<Declarer = Self>,
{
#[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)
}
}
Expand Down
7 changes: 6 additions & 1 deletion godot-core/src/registry/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ where
T: GodotClass,
F: FnOnce(Base<T::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),
Expand Down
1 change: 1 addition & 0 deletions godot-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
7 changes: 6 additions & 1 deletion godot-ffi/src/interface_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
1 change: 1 addition & 0 deletions godot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
1 change: 1 addition & 0 deletions itest/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading