Skip to content

Commit 4b1d208

Browse files
committed
Fix Variant::evaluate to work with both 4.0 and 4.1
Fix impl_variant_traits macro's FromVariant impl to work with both 4.0 and 4.1
1 parent 5dc6915 commit 4b1d208

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
lines changed

godot-bindings/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ mod prebuilt {
103103
full_string: godot4_prebuilt::GODOT_VERSION.into(),
104104
major: version[0].parse().unwrap(),
105105
minor: version[1].parse().unwrap(),
106-
patch: version[2].parse().unwrap(),
106+
patch: version
107+
.get(2)
108+
.and_then(|patch| patch.parse().ok())
109+
.unwrap_or(0),
107110
status: "stable".into(),
108111
custom_rev: None,
109112
}
@@ -132,12 +135,12 @@ pub fn emit_godot_version_cfg() {
132135
..
133136
} = get_godot_version();
134137

135-
println!(r#"cargo:rustc-cfg=gdextension_api_version="{major}.{minor}""#);
138+
println!(r#"cargo:rustc-cfg=gdextension_api="{major}.{minor}""#);
136139

137140
// Godot drops the patch version if it is 0.
138141
if patch != 0 {
139-
println!(r#"cargo:rustc-cfg=gdextension_api_version_full="{major}.{minor}.{patch}""#);
142+
println!(r#"cargo:rustc-cfg=gdextension_exact_api="{major}.{minor}.{patch}""#);
140143
} else {
141-
println!(r#"cargo:rustc-cfg=gdextension_api_version_full="{major}.{minor}""#);
144+
println!(r#"cargo:rustc-cfg=gdextension_exact_api="{major}.{minor}""#);
142145
}
143146
}

godot-core/src/builtin/basis.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ impl Basis {
143143
/// orthonormalized. The `target` and `up` vectors cannot be zero, and
144144
/// cannot be parallel to each other.
145145
///
146-
#[cfg(gdextension_api_version = "4.0")]
146+
#[cfg(gdextension_api = "4.0")]
147147
/// _Godot equivalent: `Basis.looking_at()`_
148148
pub fn new_looking_at(target: Vector3, up: Vector3) -> Self {
149149
super::inner::InnerBasis::looking_at(target, up)
150150
}
151-
#[cfg(not(gdextension_api_version = "4.0"))]
151+
#[cfg(not(gdextension_api = "4.0"))]
152152
/// If `use_model_front` is true, the +Z axis (asset front) is treated as forward (implies +X is left)
153153
/// and points toward the target position. By default, the -Z axis (camera forward) is treated as forward
154154
/// (implies +X is right).

godot-core/src/builtin/transform3d.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ impl Transform3D {
145145
/// See [`Basis::new_looking_at()`] for more information.
146146
///
147147
/// _Godot equivalent: Transform3D.looking_at()_
148-
#[cfg(gdextension_api_version = "4.0")]
148+
#[cfg(gdextension_api = "4.0")]
149149
#[must_use]
150150
pub fn looking_at(self, target: Vector3, up: Vector3) -> Self {
151151
Self {
152152
basis: Basis::new_looking_at(target - self.origin, up),
153153
origin: self.origin,
154154
}
155155
}
156-
#[cfg(not(gdextension_api_version = "4.0"))]
156+
#[cfg(not(gdextension_api = "4.0"))]
157157
#[must_use]
158158
pub fn looking_at(self, target: Vector3, up: Vector3, use_model_front: bool) -> Self {
159159
Self {

godot-core/src/builtin/variant/impls.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,21 @@ macro_rules! impl_variant_traits {
6161
return Err(VariantConversionError::BadType)
6262
}
6363

64+
// For 4.0:
6465
// In contrast to T -> Variant, the conversion Variant -> T assumes
6566
// that the destination is initialized (at least for some T). For example:
6667
// void String::operator=(const String &p_str) { _cowdata._ref(p_str._cowdata); }
6768
// does a copy-on-write and explodes if this->_cowdata is not initialized.
6869
// We can thus NOT use Self::from_sys_init().
70+
//
71+
// This was changed in 4.1.
6972
let result = unsafe {
70-
Self::from_sys_init(|self_ptr| {
73+
#[cfg(gdextension_api = "4.0")]
74+
let from_sys_init = Self::from_sys_init_default;
75+
#[cfg(not(gdextension_api = "4.0"))]
76+
let from_sys_init = Self::from_sys_init;
77+
78+
from_sys_init(|self_ptr| {
7179
let converter = sys::builtin_fn!($to_fn);
7280
converter(self_ptr, variant.var_sys());
7381
})

godot-core/src/builtin/variant/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ impl Variant {
132132
let op_sys = op.sys();
133133
let mut is_valid = false as u8;
134134

135+
#[cfg(gdextension_api = "4.0")]
136+
let result = {
137+
#[allow(unused_mut)]
138+
let mut result = Variant::nil();
139+
unsafe {
140+
interface_fn!(variant_evaluate)(
141+
op_sys,
142+
self.var_sys(),
143+
rhs.var_sys(),
144+
result.var_sys(),
145+
ptr::addr_of_mut!(is_valid),
146+
)
147+
};
148+
result
149+
};
150+
#[cfg(not(gdextension_api = "4.0"))]
135151
let result = unsafe {
136152
Variant::from_var_sys_init(|variant_ptr| {
137153
interface_fn!(variant_evaluate)(

godot-core/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ pub mod private {
152152
macro_rules! generate_gdextension_api_version {
153153
(
154154
$(
155-
$name:ident => {
155+
($name:ident, $gdextension_api:ident) => {
156156
$($version:literal, )*
157157
}
158158
),* $(,)?
159159
) => {
160160
$(
161161
$(
162-
#[cfg(gdextension_api_version = $version)]
162+
#[cfg($gdextension_api = $version)]
163163
#[allow(dead_code)]
164164
const $name: &str = $version;
165165
)*
@@ -172,14 +172,14 @@ macro_rules! generate_gdextension_api_version {
172172
//
173173
// This includes all versions we're developing for, including unreleased future versions.
174174
generate_gdextension_api_version!(
175-
GDEXTENSION_API_VERSION_FULL => {
175+
(GDEXTENSION_API_VERSION_FULL, gdextension_exact_api) => {
176176
"4.0",
177177
"4.0.1",
178178
"4.0.2",
179179
"4.0.3",
180180
"4.1",
181181
},
182-
GDEXTENSION_API_VERSION => {
182+
(GDEXTENSION_API_VERSION, gdextension_api) => {
183183
"4.0",
184184
"4.1",
185185
},

0 commit comments

Comments
 (0)