Crate oc_wasm_opencomputers

source ·
Expand description

This crate provides high-level APIs for accessing components provided by OpenComputers in a vanilla Minecraft environment (e.g. redstone blocks, GPUs, screens, etc.).

As a general rule, APIs in this crate accept an Invoker and a Buffer scratch buffer, the latter being used for encoding parameters and decoding return values. This buffer can be reused between API calls to reduce heap allocations. In some cases the return value of an API may borrow from the scratch buffer.

§Important

You must depend on oc-wasm-futures with the proper-waker feature in your own application if your chosen executor requires the proper-waker feature.

§Example

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)
}

Modules§

  • Provides high-level access to the EEPROM APIs.
  • Provides an error type used for higher level calls.
  • Provides high-level access to the filesystem APIs.
  • Provides high-level access to the GPU APIs.
  • Provides high-level access to the inventory controller, fluid tank controller, and transposer APIs.
  • Provides decoders for the keyboard-related signals.
  • Provides high-level access to the modem API, as implemented on network cards and wireless network cards.
  • Provides symbols that virtually all consumers will want to import all of.
  • Provides high-level access to the redstone APIs.
  • Provides high-level access to the robot component APIs.
  • Provides high-level access to the screen APIs.