From 116694344f9830083c7640856839babf49541f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alf-Andr=C3=A9=20Walla?= Date: Fri, 3 Jan 2025 17:10:57 +0100 Subject: [PATCH] Add GodotDictionary to Rust API --- .github/workflows/cmake_example.yml | 6 +- .github/workflows/unittests.yml | 6 +- program/rust/docker/src/godot/array.rs | 4 + program/rust/docker/src/godot/dictionary.rs | 86 +++++++++++++++++++++ program/rust/docker/src/godot/mod.rs | 1 + 5 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 program/rust/docker/src/godot/dictionary.rs diff --git a/.github/workflows/cmake_example.yml b/.github/workflows/cmake_example.yml index 112f7c4..ec920c3 100644 --- a/.github/workflows/cmake_example.yml +++ b/.github/workflows/cmake_example.yml @@ -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 diff --git a/.github/workflows/unittests.yml b/.github/workflows/unittests.yml index 5b9d8e5..e40cfdc 100644 --- a/.github/workflows/unittests.yml +++ b/.github/workflows/unittests.yml @@ -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 diff --git a/program/rust/docker/src/godot/array.rs b/program/rust/docker/src/godot/array.rs index 4761ba9..1b44919 100644 --- a/program/rust/docker/src/godot/array.rs +++ b/program/rust/docker/src/godot/array.rs @@ -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(); diff --git a/program/rust/docker/src/godot/dictionary.rs b/program/rust/docker/src/godot/dictionary.rs new file mode 100644 index 0000000..cc636c7 --- /dev/null +++ b/program/rust/docker/src/godot/dictionary.rs @@ -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) + } +} diff --git a/program/rust/docker/src/godot/mod.rs b/program/rust/docker/src/godot/mod.rs index 8389e0f..9f1b7e4 100644 --- a/program/rust/docker/src/godot/mod.rs +++ b/program/rust/docker/src/godot/mod.rs @@ -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;