classicube_helpers\tab_list/
entry.rs

1#![allow(clippy::used_underscore_binding)]
2
3use std::os::raw::c_int;
4
5use classicube_sys::{StringsBuffer_UNSAFE_Get, TabList};
6
7#[derive(Debug)]
8pub struct TabListEntry {
9    id: u8,
10    name_offset: &'static u16,
11    group_rank: &'static u8,
12}
13
14impl TabListEntry {
15    /// # Safety
16    ///
17    /// `id` must exist.
18    ///
19    /// `Entity` cannot outlive the entity in-game.
20    ///
21    /// `Entities` will use `Weak` to make sure this dies when the entity is removed.
22    #[must_use]
23    pub unsafe fn from_id(id: u8) -> Option<Self> {
24        unsafe {
25            let name_offset = &TabList.NameOffsets[id as usize];
26            let group_rank = &TabList.GroupRanks[id as usize];
27
28            if *name_offset == 0 || *group_rank == 0 || TabList._buffer.count == 0 {
29                return None;
30            }
31
32            Some(Self {
33                id,
34                name_offset,
35                group_rank,
36            })
37        }
38    }
39
40    #[must_use]
41    pub fn get_id(&self) -> u8 {
42        self.id
43    }
44
45    /// or "Player"
46    pub fn get_real_name(&self) -> String {
47        unsafe {
48            StringsBuffer_UNSAFE_Get(&raw mut TabList._buffer, c_int::from(*self.name_offset - 3))
49        }
50        .to_string()
51    }
52
53    /// or "Text" or "list"
54    pub fn get_nick_name(&self) -> String {
55        unsafe {
56            StringsBuffer_UNSAFE_Get(&raw mut TabList._buffer, c_int::from(*self.name_offset - 2))
57        }
58        .to_string()
59    }
60
61    pub fn get_group(&self) -> String {
62        unsafe {
63            StringsBuffer_UNSAFE_Get(&raw mut TabList._buffer, c_int::from(*self.name_offset - 1))
64        }
65        .to_string()
66    }
67
68    #[must_use]
69    pub fn get_rank(&self) -> u8 {
70        *self.group_rank
71    }
72}