18 个版本 (11 个重大更新)
0.12.1 | 2024年6月8日 |
---|---|
0.11.0 | 2024年1月8日 |
0.10.2 | 2023年1月31日 |
0.9.0 | 2022年9月5日 |
0.1.0 | 2021年7月26日 |
#267 在 游戏
每月下载量 1,016
380KB
6K SLoC
OC-Wasm-OpenComputers 为在 OC-Wasm 架构中运行的应用程序提供了访问 OpenComputers 本身提供的组件的高级 API,在纯 Minecraft 环境中(例如红石方块、GPU、屏幕等)。
lib.rs
:
此 crate 提供了在纯 Minecraft 环境中访问 OpenComputers 提供的组件的高级 API(例如红石方块、GPU、屏幕等)。
一般来说,此 crate 中的 API 接受一个 Invoker
和一个 Buffer
抛弃缓冲区,后者用于编码参数和解码返回值。此缓冲区可以在 API 调用之间重用,以减少堆分配。在某些情况下,API 的返回值可能会借用自抛弃缓冲区。
重要
如果你的选择执行器需要 proper-waker
功能,你必须在自己的应用程序中依赖于 oc-wasm-futures
并启用 proper-waker
功能。
示例
extern crate alloc;
use alloc::vec::Vec;
use oc_wasm_futures::sleep;
use oc_wasm_opencomputers::prelude::*;
use oc_wasm_opencomputers::{gpu, screen};
use oc_wasm_opencomputers::common::{Dimension, Point};
use oc_wasm_safe::{component, computer};
use core::panic::PanicInfo;
#[global_allocator]
static ALLOC: lol_alloc::AssumeSingleThreaded<lol_alloc::LeakingAllocator> =
unsafe { lol_alloc::AssumeSingleThreaded::new(lol_alloc::LeakingAllocator::new()) };
#[panic_handler]
fn panic_hook(_: &PanicInfo<'_>) -> ! {
computer::error("panic occurred");
}
async fn main_impl() -> Result<(), oc_wasm_opencomputers::error::Error> {
// Grab the one-and-only resources.
let mut invoker = component::Invoker::take().unwrap();
let mut lister = component::Lister::take().unwrap();
// Find the GPU.
let mut listing = lister.start(Some("gpu"));
let gpu = *listing.next().expect("no GPU").address();
let gpu = gpu::Gpu::new(gpu);
// Find the screen.
listing = lister.start(Some("screen"));
let screen = *listing.next().expect("no screen").address();
let screen = screen::Screen::new(screen);
// Allocate a scratch buffer to use for method calls.
let mut buffer = Vec::<u8>::new();
// Lock the GPU so method calls can be made on it. For gpu_locked’s lifetime, methods can only
// be called on the GPU, not on anything else. To make method calls on another component, drop
// this value and recreate it later.
let mut gpu_locked = gpu.lock(&mut invoker, &mut buffer);
// Bind the GPU to the screen.
gpu_locked.bind(*screen.address(), true).await?;
// Clear the screen.
gpu_locked.set_foreground(gpu::Colour::Rgb(gpu::Rgb(0x00_FF_FF_FF))).await?;
gpu_locked.set_background(gpu::Colour::Rgb(gpu::Rgb(0))).await?;
gpu_locked.fill(Point{x: 1, y: 1}, Dimension{width: 160, height: 80}, ' ').await?;
// Say hello.
gpu_locked.set(Point{x: 1, y: 1}, "Hello World!", gpu::TextDirection::Horizontal).await?;
// Stop running forever.
loop {
sleep::for_uptime(core::time::Duration::from_secs(3600)).await;
}
}
async fn main() -> core::convert::Infallible {
match main_impl().await {
Ok(()) => computer::error("main task terminated"),
Err(e) => computer::error(e.as_str()),
}
}
#[no_mangle]
pub extern "C" fn run(arg: i32) -> i32 {
oc_wasm_cassette::run(arg, async_main)
}
依赖项
~2MB
~49K SLoC