Skip to content

Commit

Permalink
Add GodotDictionary to Rust API
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Jan 3, 2025
1 parent 67b0c37 commit c54f15d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
4 changes: 4 additions & 0 deletions program/rust/docker/src/godot/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ impl GodotArray {
self.call("pop_back", &[]);
}

pub fn clear(&self) {
self.call("clear", &[]);
}

pub fn get(&self, idx: i32) -> Variant {
const ECALL_ARRAY_AT: i32 = 522;
let mut var = Variant::new_nil();
Expand Down
86 changes: 86 additions & 0 deletions program/rust/docker/src/godot/dictionary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#![allow(dead_code)]
use core::arch::asm;
use crate::Variant;
use crate::VariantType;

#[repr(C)]
pub struct GodotDictionary {
pub reference: i32
}

impl GodotDictionary {
pub fn new() -> GodotDictionary {
let mut var = Variant::new_nil();
unsafe {
asm!("ecall",
in("a0") &mut var,
in("a1") VariantType::Dictionary as i32,
in("a2") 0, // method
in("a7") 517, // ECALL_VCREATE
);
}
GodotDictionary {
reference: unsafe { var.u.i } as i32
}
}

pub fn from_ref(var_ref: i32) -> GodotDictionary {
GodotDictionary {
reference: var_ref
}
}

pub fn to_variant(&self) -> Variant {
let mut v = Variant::new_nil();
v.t = VariantType::Array;
v.u.i = self.reference as i64;
v
}

/* Godot Dictionary API */
pub fn size(&self) -> i64 {
return self.call("size", &[]).to_integer();
}

pub fn empty(&self) -> bool {
return self.size() == 0;
}

pub fn clear(&self) {
self.call("clear", &[]);
}

pub fn get(&self, key: &Variant) -> Variant {
const ECALL_DICTIONARY_OPS: i32 = 524;
let mut var = Variant::new_nil();
unsafe {
asm!("ecall",
in("a0") 0, // OP_GET
in("a1") self.reference,
in("a2") key,
in("a3") &mut var,
in("a7") ECALL_DICTIONARY_OPS,
);
}
var
}
pub fn set(&self, key: &Variant, value: &Variant) {
const ECALL_DICTIONARY_OPS: i32 = 524;
unsafe {
asm!("ecall",
in("a0") 1, // OP_SET
in("a1") self.reference,
in("a2") key,
in("a3") value,
in("a7") ECALL_DICTIONARY_OPS,
);
}
}

/* Make a method call on the string (as Variant) */
pub fn call(&self, method: &str, args: &[Variant]) -> Variant {
// Call the method using Variant::callp
let var = Variant::from_ref(VariantType::Array, self.reference);
var.call(method, &args)
}
}
1 change: 1 addition & 0 deletions program/rust/docker/src/godot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod api;
pub mod variant;
pub mod node;
pub mod array;
pub mod dictionary;
pub mod string;
pub mod sysalloc;
pub mod syscalls;

0 comments on commit c54f15d

Please sign in to comment.