1个不稳定版本
0.1.0 | 2024年5月28日 |
---|
#159 在 游戏
44KB
545 行
uxi
uxi 是一个用于轻松构建符合UXI协议游戏引擎的包。游戏引擎的主要表示为 Client
,有关更多信息请参阅其文档。从GUI或其他地方发送到引擎的命令表示为 Command
,有关更多详情请参阅其文档。
use uxi::*;
fn main() {
// Setting up and starting the Client.
Client::new()
// Set engine protocol (Universal Ataxx Interface here).
.protocol("uai")
// Set engine details.
.engine("Engine v0.0.0")
.author("Rak Laptudirm")
// Register engine options.
.option("Hash", Parameter::Spin(16, 1, 8192))
.option("Threads", Parameter::Spin(1, 1, 99))
// Register the custom commands.
.command("i", i())
.command("d", d())
// Start the Client so it can start running Commands.
.start(Context { number: 0 });
}
// A custom user specified context stores information which persists across
// different Commands. In this case, we are storing a number which is used
// differently inside different Commands.
pub struct Context {
pub number: i32,
}
// The command i increases the current value of the number by delta. Commands
// take a type parameter which specifies the type of the user defined Context.
pub fn i() -> Command<Context> {
// The main part of a Command is its run function. The Bundle provided to the
// run function provides access to the user specified context, flags, and other
// things. Look at the documentation for more details.
Command::new(|bundle: Bundle<Context>| {
// Lock the mutex guarding the context bundle.
let mut ctx = bundle.lock();
// Get the value of the single flag named 'delta'.
match bundle.get_single_flag("delta") {
Some(delta) => {
// Mutate the contents of the context.
ctx.number += delta.parse::<i32>()?;
}
// Return an error from the Command which is handled by the Client.
None => return error!("the flag 'delta' is not set"),
}
// No error.
Ok(())
})
// Commands can also take any number of flags as input during their invocation.
// This line define a flag delta which specifies the amount to increase number by.
.flag("delta", Flag::Single)
// You can specify that a Command should run in parallel with this function.
.parallelize(true)
}
// The command d displays the current value of the number.
pub fn d() -> Command<Context> {
Command::new(|bundle| {
let ctx = bundle.lock();
println!("current number: {}", ctx.number);
println!(
"hash size: {}\nnumber of threads: {}",
// Get the values of options from the context.
ctx.get_spin_option("Hash").unwrap(),
ctx.get_spin_option("Threads").unwrap()
);
Ok(())
})
}
特性
- 轻松构建符合所有UXI协议的引擎,UGI默认支持。
- 无畏并发™,通过一个精心构建的系统并行运行命令。
- 只需一行代码即可为您的引擎添加新选项。
- 客户端内置了许多常见的命令,包括
setoption
、ugi
、isready
和quit
。 - 通过共享状态进行通信;自定义上下文允许您存储所需的持久数据,所有线程安全。
- 您的命令中的错误处理与使用
anyhow
一样简单,这归功于强大的RunError
类型。
有关功能函数的完整深入列表,请参阅 文档。