Skip to content

Commit

Permalink
Fix base classes registered under Rust instead of Godot class name
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Feb 5, 2023
1 parent 84a8c42 commit 5dc2c12
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions godot-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ const SELECTED_CLASSES: &[&str] = &[
"CollisionShape2D",
"Control",
"FileAccess",
"HTTPRequest",
"Input",
"Label",
"MainLoop",
Expand Down
3 changes: 3 additions & 0 deletions godot-core/src/obj/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions godot-macros/src/derive_godot_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ pub fn transform(input: TokenStream) -> ParseResult<TokenStream> {
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);
Expand Down Expand Up @@ -57,7 +56,7 @@ pub fn transform(input: TokenStream) -> ParseResult<TokenStream> {
::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>,
},
Expand Down
51 changes: 51 additions & 0 deletions itest/rust/src/codegen_test.rs
Original file line number Diff line number Diff line change
@@ -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<HttpRequest>,
}

#[godot_api]
impl GodotExt for TestBaseRenamed {
fn init(base: Base<HttpRequest>) -> Self {
TestBaseRenamed { base }
}
}
2 changes: 2 additions & 0 deletions itest/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit 5dc2c12

Please sign in to comment.