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 27, 2025
1 parent 67b0c37 commit 1166943
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/cmake_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ jobs:
sudo apt install -y git cmake zip wget xz-utils
git submodule update --init --recursive --depth=1
# Install Zig
wget https://ziglang.org/builds/zig-linux-x86_64-0.14.0-dev.2414+ba37a4369.tar.xz
tar -xf zig-linux-x86_64-0.14.0-dev.2414+ba37a4369.tar.xz
wget https://ziglang.org/builds/zig-linux-x86_64-0.14.0-dev.2851+b074fb7dd.tar.xz
tar -xf zig-linux-x86_64-0.14.0-dev.2851+b074fb7dd.tar.xz
- name: Build the example program
run: |
export PATH=$PWD/zig-linux-x86_64-0.14.0-dev.2414+ba37a4369:$PATH
export PATH=$PWD/zig-linux-x86_64-0.14.0-dev.2851+b074fb7dd:$PATH
cmake -B build -DCMAKE_BUILD_TYPE=Release -DSTRIPPED=ON
cmake --build build --parallel 8
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ jobs:
working-directory: ${{github.workspace}}/tests
run: |
# Download and install Zig
wget https://ziglang.org/builds/zig-linux-x86_64-0.14.0-dev.2414+ba37a4369.tar.xz
tar -xf zig-linux-x86_64-0.14.0-dev.2414+ba37a4369.tar.xz
export PATH=$PWD/zig-linux-x86_64-0.14.0-dev.2414+ba37a4369:$PATH
wget https://ziglang.org/builds/zig-linux-x86_64-0.14.0-dev.2851+b074fb7dd.tar.xz
tar -xf zig-linux-x86_64-0.14.0-dev.2851+b074fb7dd.tar.xz
export PATH=$PWD/zig-linux-x86_64-0.14.0-dev.2851+b074fb7dd:$PATH
# Run the tests with Zig
CI=1 GODOT=./Godot_v4.3-stable_linux.x86_64 ./zig_unittests.sh
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 1166943

Please sign in to comment.