跳转至

levilamina::client · 客户端

只在客户端可用的能力。

在服务器宿主上,这一整组都是空槽位

在服务器宿主上调用,得到的是运行时的 Err,编译不会报错。契约 §2.1:所有目标上的布局都一样,缺少的能力就是 NULL 槽位,所以同一份模组源码能为两个目标编译;加载到错误的目标上时,宿主会在握手时根据 mod_flags 明确拒绝。

用 is_available 判断,不要用 cfg。

回调在客户端线程上运行

不在服务器线程上。热键回调不能碰服务器的状态。

函数

client::is_available

Rust
pub fn is_available() -> bool

这个宿主是不是为客户端目标构建的,也就是 client_* 槽位有没有填上。

client::is_in_level

Rust
pub fn is_in_level() -> Result<bool>

客户端当前是否在一个世界里。

client::local_player_name

Rust
pub fn local_player_name() -> Result<String>

本地玩家的名字。不在世界里时返回 Err。

client::screen_name

Rust
pub fn screen_name() -> Result<String>

当前界面的名字,比如 "hud_screen" 或 "pause_screen"。

client::register_key

Rust
1
2
3
4
5
6
pub fn register_key(
    name: &str,
    key_codes: &[i32],
    allow_remap: bool,
    handler: impl FnMut(KeyAction, i32) + Send + 'static,
) -> Result<KeyBinding>

注册一个热键。

key_codes 是默认的绑定,allow_remap 决定玩家能不能在设置里改它。回调收到(动作,焦点影响),两者都在客户端线程上运行。

  • 参数:
    • name : &str
    • key_codes : &[i32]
    • allow_remap : bool
    • handler : impl FnMut(KeyAction, i32) + Send + 'static
  • 返回值类型:Result<KeyBinding>
  • 对应槽位:client_register_key

KeyBinding

Rust
1
2
3
pub struct KeyBinding {
    // private fields
}

一个已注册的热键。丢弃它就会注销。

  • 实现的 trait:Drop

KeyBinding::name

Rust
pub fn name(&self) -> &str
  • 返回值类型:&str

KeyBinding::key_codes

Rust
pub fn key_codes(&self) -> Result<Vec<i32>>

当前绑定的按键码。玩家重新绑定以后,就和默认值不同了。

KeyBinding::forget

Rust
pub fn forget(mut self)

让它一直存活到模组卸载,那时由宿主清掉。

KeyAction

Rust
1
2
3
4
5
6
pub enum KeyAction {
        Up,
        Down,
        /// The host reported an action code this side does not recognize.
        Unknown(i32),
}

一个按键动作。

  • 实现的 trait:Debug、Clone、Copy、PartialEq、Eq

KeyAction::from_i32

Rust
pub fn from_i32(v: i32) -> KeyAction
  • 参数:
    • v : i32
  • 返回值类型:KeyAction

KeyAction::is_down

Rust
pub fn is_down(self) -> bool
  • 返回值类型:bool