From 5dc2c122138251176bf2b9696b60abf480feaacf Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Sun, 5 Feb 2023 12:48:31 +0100 Subject: [PATCH] Fix base classes registered under Rust instead of Godot class name --- godot-codegen/src/lib.rs | 1 + godot-core/src/obj/traits.rs | 3 ++ godot-macros/src/derive_godot_class.rs | 5 +-- itest/rust/src/codegen_test.rs | 51 ++++++++++++++++++++++++++ itest/rust/src/lib.rs | 2 + 5 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 itest/rust/src/codegen_test.rs diff --git a/godot-codegen/src/lib.rs b/godot-codegen/src/lib.rs index e11b035f6..52bebc1a7 100644 --- a/godot-codegen/src/lib.rs +++ b/godot-codegen/src/lib.rs @@ -269,6 +269,7 @@ const SELECTED_CLASSES: &[&str] = &[ "CollisionShape2D", "Control", "FileAccess", + "HTTPRequest", "Input", "Label", "MainLoop", diff --git a/godot-core/src/obj/traits.rs b/godot-core/src/obj/traits.rs index 1ec24739f..5d3297e19 100644 --- a/godot-core/src/obj/traits.rs +++ b/godot-core/src/obj/traits.rs @@ -25,6 +25,9 @@ where /// Defines the memory strategy. type Mem: mem::Memory; + /// The name of the class, under which it is registered in Godot. + /// + /// This may deviate from the Rust struct name: `HttpRequest::CLASS_NAME == "HTTPRequest"`. const CLASS_NAME: &'static str; } diff --git a/godot-macros/src/derive_godot_class.rs b/godot-macros/src/derive_godot_class.rs index 5bbc89f63..e85a999a8 100644 --- a/godot-macros/src/derive_godot_class.rs +++ b/godot-macros/src/derive_godot_class.rs @@ -22,10 +22,9 @@ pub fn transform(input: TokenStream) -> ParseResult { let fields = parse_fields(class)?; let base_ty = &struct_cfg.base_ty; - let base_ty_str = struct_cfg.base_ty.to_string(); let class_name = &class.name; let class_name_str = class.name.to_string(); - let inherits_macro = format_ident!("inherits_transitive_{}", &base_ty_str); + let inherits_macro = format_ident!("inherits_transitive_{}", base_ty); let prv = quote! { ::godot::private }; let deref_impl = make_deref_impl(class_name, &fields); @@ -57,7 +56,7 @@ pub fn transform(input: TokenStream) -> ParseResult { ::godot::sys::plugin_add!(__GODOT_PLUGIN_REGISTRY in #prv; #prv::ClassPlugin { class_name: #class_name_str, component: #prv::PluginComponent::ClassDef { - base_class_name: #base_ty_str, + base_class_name: <::godot::engine::#base_ty as ::godot::obj::GodotClass>::CLASS_NAME, generated_create_fn: #create_fn, free_fn: #prv::callbacks::free::<#class_name>, }, diff --git a/itest/rust/src/codegen_test.rs b/itest/rust/src/codegen_test.rs new file mode 100644 index 000000000..82f0761c5 --- /dev/null +++ b/itest/rust/src/codegen_test.rs @@ -0,0 +1,51 @@ +/* + * 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/. + */ + +// This file tests the presence and naming of generated symbols, not their functionality. + +use crate::itest; + +use godot::engine::HttpRequest; +use godot::prelude::*; + +pub fn run() -> bool { + let mut ok = true; + ok &= codegen_class_renamed(); + ok &= codegen_base_renamed(); + ok +} + +#[itest] +fn codegen_class_renamed() { + // Known as `HTTPRequest` in Godot + let obj = HttpRequest::new_alloc(); + obj.free(); +} + +#[itest] +fn codegen_base_renamed() { + // The registration is done at startup time, so it may already fail during GDExtension init. + // Nevertheless, try to instantiate an object with base HttpRequest here. + + let obj = Gd::with_base(|base| TestBaseRenamed { base }); + let _id = obj.instance_id(); + + obj.free(); +} + +#[derive(GodotClass)] +#[class(base=HttpRequest)] +pub struct TestBaseRenamed { + #[base] + base: Base, +} + +#[godot_api] +impl GodotExt for TestBaseRenamed { + fn init(base: Base) -> Self { + TestBaseRenamed { base } + } +} diff --git a/itest/rust/src/lib.rs b/itest/rust/src/lib.rs index 94e65f266..7aedc7c58 100644 --- a/itest/rust/src/lib.rs +++ b/itest/rust/src/lib.rs @@ -12,6 +12,7 @@ use std::panic::UnwindSafe; mod array_test; mod base_test; mod builtin_test; +mod codegen_test; mod dictionary_test; mod enum_test; mod export_test; @@ -29,6 +30,7 @@ fn run_tests() -> bool { let mut ok = true; ok &= base_test::run(); ok &= builtin_test::run(); + ok &= codegen_test::run(); ok &= enum_test::run(); ok &= export_test::run(); ok &= gdscript_ffi_test::run();