Skip to content

Support Unicode class names (Godot 4.4+) #891

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

Merged
merged 1 commit into from
Sep 15, 2024
Merged
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
2 changes: 1 addition & 1 deletion godot-codegen/src/generator/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fn make_class(class: &Class, ctx: &mut Context, view: &ApiView) -> GeneratedClas
// Optimization note: instead of lazy init, could use separate static which is manually initialized during registration.
static CLASS_NAME: std::sync::OnceLock<ClassName> = std::sync::OnceLock::new();

let name: &'static ClassName = CLASS_NAME.get_or_init(|| ClassName::alloc_next(#class_name_cstr));
let name: &'static ClassName = CLASS_NAME.get_or_init(|| ClassName::alloc_next_ascii(#class_name_cstr));
*name
}

Expand Down
31 changes: 30 additions & 1 deletion godot-core/src/meta/class_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,41 @@ impl ClassName {
}

#[doc(hidden)]
pub fn alloc_next(class_name_cstr: &'static CStr) -> Self {
pub fn alloc_next_ascii(class_name_cstr: &'static CStr) -> Self {
let utf8 = class_name_cstr
.to_str()
.expect("class name is invalid UTF-8");

assert!(
utf8.is_ascii(),
"ClassName::alloc_next_ascii() with non-ASCII Unicode string '{}'",
utf8
);

let global_index = insert_class(ClassNameSource::Borrowed(class_name_cstr));

Self { global_index }
}

#[doc(hidden)]
pub fn alloc_next_unicode(class_name_str: &'static str) -> Self {
assert!(
cfg!(since_api = "4.4"),
"Before Godot 4.4, class names must be ASCII, but '{class_name_str}' is not.\nSee https://github.com/godotengine/godot/pull/96501."
);

assert!(
!class_name_str.is_ascii(),
"ClassName::alloc_next_unicode() with ASCII string '{}'",
class_name_str
);

// StringNames use optimized 1-byte-per-char layout for Latin-1/ASCII, so Unicode can as well use the regular constructor.
let global_index = insert_class(ClassNameSource::Owned(class_name_str.to_owned()));

Self { global_index }
}

#[doc(hidden)]
pub fn is_none(&self) -> bool {
self.global_index == 0
Expand Down
2 changes: 1 addition & 1 deletion godot-core/src/registry/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ fn register_class_raw(mut info: ClassRegistrationInfo) {
// This can happen during hot reload if a class changes base type in an incompatible way (e.g. RefCounted -> Node).
if registration_failed {
godot_error!(
"Failed to register class `{class_name}`; check preceding Godot stderr messages"
"Failed to register class `{class_name}`; check preceding Godot stderr messages."
);
}

Expand Down
11 changes: 9 additions & 2 deletions godot-macros/src/class/derive_godot_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ pub fn derive_godot_class(item: venial::Item) -> ParseResult<TokenStream> {
.map_or_else(|| class.name.clone(), |rename| rename)
.to_string();

let class_name_cstr = util::c_str(&class_name_str);
// Determine if we can use ASCII for the class name (in most cases).
let class_name_allocation = if class_name_str.is_ascii() {
let c_str = util::c_str(&class_name_str);
quote! { ClassName::alloc_next_ascii(#c_str) }
} else {
quote! { ClassName::alloc_next_unicode(#class_name_str) }
};

let class_name_obj = util::class_name_obj(class_name);

let is_internal = struct_cfg.is_internal;
Expand Down Expand Up @@ -123,7 +130,7 @@ pub fn derive_godot_class(item: venial::Item) -> ParseResult<TokenStream> {
// Optimization note: instead of lazy init, could use separate static which is manually initialized during registration.
static CLASS_NAME: std::sync::OnceLock<ClassName> = std::sync::OnceLock::new();

let name: &'static ClassName = CLASS_NAME.get_or_init(|| ClassName::alloc_next(#class_name_cstr));
let name: &'static ClassName = CLASS_NAME.get_or_init(|| #class_name_allocation);
*name
}
}
Expand Down
2 changes: 1 addition & 1 deletion itest/rust/src/register_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod conversion_test;
mod derive_godotconvert_test;
mod func_test;
mod gdscript_ffi_test;
mod keyword_parameters_test;
mod naming_tests;
mod option_ffi_test;
mod var_test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ impl IEditorExportPlugin for KeywordParameterEditorExportPlugin {
fn get_customization_configuration_hash(&self) -> u64 { unreachable!() }
fn get_name(&self) -> GString { unreachable!() }
}

#[cfg(since_api = "4.4")]
#[derive(GodotClass)]
#[class(no_init)]
struct 统一码 {}
Loading