Skip to content

Commit 97060b0

Browse files
authored
Script ClassNames can be allocated as ASCII only or unicode #74 (#75)
gdext wants `ClassName`s to be either allocated as ASCII only if they are 100% ASCII and otherwise as unicode. Resolves #74
1 parent a914177 commit 97060b0

File tree

1 file changed

+28
-59
lines changed

1 file changed

+28
-59
lines changed

rust-script/src/static_script_registry.rs

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use crate::runtime::GodotScriptObject;
2222
godot::sys::plugin_registry!(pub SCRIPT_REGISTRY: RegistryItem);
2323

2424
#[macro_export]
25-
#[cfg(before_api = "4.4")]
2625
macro_rules! register_script_class {
2726
($class_name:ty, $base_name:ty, $desc:expr, $props:expr, $signals:expr) => {
2827
$crate::private_export::plugin_add! {
@@ -44,28 +43,6 @@ macro_rules! register_script_class {
4443
};
4544
}
4645

47-
#[macro_export]
48-
#[cfg(since_api = "4.4")]
49-
macro_rules! register_script_class {
50-
($class_name:ty, $base_name:ty, $desc:expr, $props:expr, $signals:expr) => {
51-
$crate::private_export::plugin_add! {
52-
$crate::private_export::SCRIPT_REGISTRY ;
53-
$crate::private_export::RegistryItem::Entry($crate::private_export::RustScriptEntry {
54-
class_name: stringify!($class_name),
55-
base_type_name: <$base_name as $crate::godot::prelude::GodotClass>::class_name().to_cow_str(),
56-
properties: || {
57-
$props
58-
},
59-
signals: || {
60-
$signals
61-
},
62-
create_data: $crate::private_export::create_default_data_struct::<$class_name>,
63-
description: $desc,
64-
})
65-
}
66-
};
67-
}
68-
6946
#[macro_export]
7047
macro_rules! register_script_methods {
7148
($class_name:ty, $methods:expr) => {
@@ -83,7 +60,6 @@ macro_rules! register_script_methods {
8360

8461
pub struct RustScriptEntry {
8562
pub class_name: &'static str,
86-
#[cfg(before_api = "4.4")]
8763
pub class_name_cstr: &'static std::ffi::CStr,
8864
pub base_type_name: Cow<'static, str>,
8965
pub properties: fn() -> Vec<RustScriptPropDesc>,
@@ -145,13 +121,12 @@ impl RustScriptMethodDesc {
145121
self,
146122
id: i32,
147123
class_name: &'static str,
148-
#[cfg(before_api = "4.4")] class_name_cstr: &'static std::ffi::CStr,
124+
class_name_cstr: &'static std::ffi::CStr,
149125
) -> RustScriptMethodInfo {
150126
RustScriptMethodInfo {
151127
id,
152128
method_name: self.name,
153129
class_name,
154-
#[cfg(before_api = "4.4")]
155130
class_name_cstr,
156131
return_type: self.return_type.to_property_info(),
157132
flags: self.flags.ord(),
@@ -221,7 +196,6 @@ pub fn assemble_metadata<'a>(
221196
method.into_method_info(
222197
(index + 1) as i32,
223198
class.class_name,
224-
#[cfg(before_api = "4.4")]
225199
class.class_name_cstr,
226200
)
227201
})
@@ -234,7 +208,6 @@ pub fn assemble_metadata<'a>(
234208

235209
RustScriptMetaData::new(
236210
class.class_name,
237-
#[cfg(before_api = "4.4")]
238211
class.class_name_cstr,
239212
class.base_type_name.as_ref().into(),
240213
props,
@@ -281,7 +254,6 @@ pub struct RustScriptMethodInfo {
281254
pub id: i32,
282255
pub method_name: &'static str,
283256
pub class_name: &'static str,
284-
#[cfg(before_api = "4.4")]
285257
pub class_name_cstr: &'static std::ffi::CStr,
286258
pub return_type: RustScriptPropertyInfo,
287259
pub arguments: Box<[RustScriptPropertyInfo]>,
@@ -294,11 +266,7 @@ impl From<&RustScriptMethodInfo> for MethodInfo {
294266
Self {
295267
id: value.id,
296268
method_name: value.method_name.into(),
297-
class_name: ClassName::new_script(
298-
value.class_name,
299-
#[cfg(before_api = "4.4")]
300-
value.class_name_cstr,
301-
),
269+
class_name: ClassName::new_script(value.class_name, value.class_name_cstr),
302270
return_type: (&value.return_type).into(),
303271
arguments: value.arguments.iter().map(|arg| arg.into()).collect(),
304272
default_arguments: vec![],
@@ -349,10 +317,10 @@ pub struct RustScriptMetaData {
349317
}
350318

351319
impl RustScriptMetaData {
352-
#[cfg_attr(before_api = "4.4", expect(clippy::too_many_arguments))]
320+
#[expect(clippy::too_many_arguments)]
353321
pub fn new(
354322
class_name: &'static str,
355-
#[cfg(before_api = "4.4")] class_name_cstr: &'static std::ffi::CStr,
323+
class_name_cstr: &'static std::ffi::CStr,
356324
base_type_name: StringName,
357325
properties: Box<[RustScriptPropertyInfo]>,
358326
methods: Box<[RustScriptMethodInfo]>,
@@ -361,11 +329,8 @@ impl RustScriptMetaData {
361329
description: &'static str,
362330
) -> Self {
363331
Self {
364-
#[cfg(before_api = "4.4")]
365332
class_name: ClassName::new_script(class_name, class_name_cstr),
366333

367-
#[cfg(since_api = "4.4")]
368-
class_name: ClassName::new_script(class_name),
369334
base_type_name,
370335
properties,
371336
methods,
@@ -423,15 +388,10 @@ static DYNAMIC_INDEX_BY_CLASS_NAME: LazyLock<RwLock<HashMap<&'static str, ClassN
423388
LazyLock::new(RwLock::default);
424389

425390
trait ClassNameExtension {
426-
#[cfg(before_api = "4.4")]
427391
fn new_script(str: &'static str, cstr: &'static std::ffi::CStr) -> Self;
428-
429-
#[cfg(since_api = "4.4")]
430-
fn new_script(str: &'static str) -> Self;
431392
}
432393

433394
impl ClassNameExtension for ClassName {
434-
#[cfg(before_api = "4.4")]
435395
fn new_script(str: &'static str, cstr: &'static std::ffi::CStr) -> Self {
436396
// Check if class name exists.
437397
if let Some(name) = DYNAMIC_INDEX_BY_CLASS_NAME.read().unwrap().get(str) {
@@ -440,27 +400,36 @@ impl ClassNameExtension for ClassName {
440400

441401
let mut map = DYNAMIC_INDEX_BY_CLASS_NAME.write().unwrap();
442402

443-
let class_name = map
444-
.entry(str)
445-
.or_insert_with(|| ClassName::alloc_next_ascii(cstr));
403+
let class_name = *map.entry(str).or_insert_with(|| {
404+
if str.is_ascii() {
405+
ClassName::alloc_next_ascii(cstr)
406+
} else {
407+
ClassName::alloc_next_unicode(str)
408+
}
409+
});
446410

447-
*class_name
411+
class_name
448412
}
413+
}
449414

450-
#[cfg(since_api = "4.4")]
451-
fn new_script(str: &'static str) -> Self {
452-
// Check if class name exists.
415+
#[cfg(test)]
416+
mod tests {
417+
use godot::meta::ClassName;
453418

454-
if let Some(name) = DYNAMIC_INDEX_BY_CLASS_NAME.read().unwrap().get(str) {
455-
return *name;
456-
}
419+
use crate::static_script_registry::ClassNameExtension;
457420

458-
let mut map = DYNAMIC_INDEX_BY_CLASS_NAME.write().unwrap();
421+
#[test]
422+
fn new_class_name() {
423+
let script_name = ClassName::new_script("TestScript", c"TestScript");
459424

460-
let class_name = *map
461-
.entry(str)
462-
.or_insert_with(|| ClassName::alloc_next_unicode(str));
425+
assert_eq!(script_name.to_cow_str(), "TestScript");
426+
}
463427

464-
class_name
428+
#[cfg(since_api = "4.4")]
429+
#[test]
430+
fn new_unicode_class_name() {
431+
let script_name = ClassName::new_script("ÜbertragungsScript", c"ÜbertragungsScript");
432+
433+
assert_eq!(script_name.to_cow_str(), "ÜbertragungsScript");
465434
}
466435
}

0 commit comments

Comments
 (0)