Skip to content

Commit f045ab4

Browse files
committed
handle 3D Transforms, reorder some conditions, add another TODO
1 parent c13ba1d commit f045ab4

File tree

1 file changed

+33
-25
lines changed
  • gdnative-core/src/core_types/variant

1 file changed

+33
-25
lines changed

gdnative-core/src/core_types/variant/serde.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<'de> Visitor<'de> for VariantVisitor {
416416
}
417417
}
418418
} else if len == 2 {
419-
if let Some(v) = vec2_plane_rect2_or_aabb(&dict) {
419+
if let Some(v) = vec2_plane_xform_rect2_or_aabb(&dict) {
420420
return Ok(v);
421421
}
422422
} else if len == 3 {
@@ -456,19 +456,17 @@ fn basis_seq<Access: ThreadAccess>(arr: &VariantArray<Access>, first: Vector3) -
456456

457457
fn string_tagged(key: &str, value: Variant) -> Option<Variant> {
458458
let s = key;
459-
if s == value.get_type().name() {
459+
let value_type = value.get_type();
460+
if (s == value_type.name())
461+
|| ((value_type == Option::<()>::None.to_variant().get_type()) && (s == "Object"))
462+
|| ((value_type == ().to_variant().get_type()) && (s == "Rid"))
463+
//maybe a Basis represented as a Map, in which case visit_seq will have assumed [Vector3; 3] was a Basis
464+
|| ((value_type == VariantType::Basis) && (s == "elements"))
465+
{
460466
return Some(value);
461-
} else if s == "elements" {
462-
//maybe a Basis represented as a Map
463-
if let VariantType::Basis = value.get_type() {
464-
//visit_seq will have assumed [Vector3; 3] was a Basis
465-
return Some(value);
466-
}
467-
} else if value.get_type() == ().to_variant().get_type() {
468-
match &*s {
469-
"Object" => return Some(Variant::new()),
470-
"Rid" => return Some(Rid::new().to_variant()),
471-
_ => {}
467+
} else if s == VariantType::NodePath.name() {
468+
if let Some(path) = value.try_to_string() {
469+
return Some(NodePath::from_str(&*path).to_variant());
472470
}
473471
} else if let Some(arr) = value.try_to_array() {
474472
if let Some(s) = s.strip_suffix("Array") {
@@ -488,15 +486,22 @@ fn string_tagged(key: &str, value: Variant) -> Option<Variant> {
488486
}
489487

490488
fn int_tagged(key: i64, value: Variant) -> Option<Variant> {
489+
//TODO: The field enums serde generates for structs could get stored as ints.
490+
// We could either hand-write all the impls so we know what the int will be,
491+
// or assume serde will keep the indices the same as the declaration order,
492+
// or just not support deserializing Variants from formats that store the field
493+
// identifier as an int (VariantDispatch should still work).
491494
let i = key;
492495
if i == value.get_type() as i64 {
493496
return Some(value);
494-
} else if value.get_type() == ().to_variant().get_type() {
495-
if i == VariantType::Object as i64 {
496-
return Some(Variant::new());
497-
} else if i == VariantType::Rid as i64 {
498-
return Some(Rid::new().to_variant());
499-
}
497+
} else if (i == VariantType::Object as i64)
498+
&& (value.get_type() == ().to_variant().get_type())
499+
{
500+
return Some(Variant::new());
501+
} else if (i == VariantType::Rid as i64)
502+
&& (value.get_type() == Option::<()>::None.to_variant().get_type())
503+
{
504+
return Some(Rid::new().to_variant());
500505
} else if let Some(arr) = value.try_to_array() {
501506
if i == VariantType::ByteArray as i64 {
502507
return Some(ByteArray::from_variant_array(&arr).to_variant());
@@ -515,17 +520,20 @@ fn int_tagged(key: i64, value: Variant) -> Option<Variant> {
515520
None
516521
}
517522

518-
fn vec2_plane_rect2_or_aabb(dict: &Dictionary<Unique>) -> Option<Variant> {
523+
fn vec2_plane_xform_rect2_or_aabb(dict: &Dictionary<Unique>) -> Option<Variant> {
519524
if let Some(x) = get_f32(&dict, "x") {
520525
if let Some(y) = get_f32(&dict, "y") {
521526
return Some(Vector2 { x, y }.to_variant());
522527
}
523-
} else {
524-
if let Some(normal) = dict.get("normal").try_to_vector3() {
525-
if let Some(d) = get_f32(&dict, "d") {
526-
return Some(Plane { normal, d }.to_variant());
527-
}
528+
} else if let Some(normal) = dict.get("normal").try_to_vector3() {
529+
if let Some(d) = get_f32(&dict, "d") {
530+
return Some(Plane { normal, d }.to_variant());
528531
}
532+
} else if let Some(basis) = dict.get("basis").try_to_basis() {
533+
if let Some(origin) = dict.get("origin").try_to_vector3() {
534+
return Some(Transform { basis, origin }.to_variant());
535+
}
536+
} else {
529537
match dict.get("position").dispatch() {
530538
VariantDispatch::Vector2(position) => {
531539
if let Some(size) = dict.get("size").try_to_vector2() {

0 commit comments

Comments
 (0)