|
20 | 20 | ---@field public GetAddress fun(self: T): uint64_t |
21 | 21 | ---@field __safecall fun(self: T, default: any, func: fun(...?): R1, R2?, R3?, R4?, R5?, ...?): R1, R2?, R3?, R4?, R5?, ...? |
22 | 22 |
|
23 | | --- Creates a basic GTA class definition with default methods. |
24 | | ----@generic T |
25 | | ----@param name string |
26 | | ----@param size? integer Optional sugar, plays nice with `SizeOf` |
27 | | ----@return CStructBase<T> |
28 | | -local function CStructView(name, size) |
29 | | - local cls = { |
30 | | - m_size = size or GenericClass.m_size, |
31 | | - __type = name, |
32 | | - __ptr_ctor = true |
33 | | - } |
34 | | - cls.__index = cls |
| 23 | +---@diagnostic disable: invisible |
35 | 24 |
|
36 | | - setmetatable(cls, { |
37 | | - __call = function(t, ...) |
38 | | - return t.new(...) |
39 | | - end |
40 | | - }) |
| 25 | +---@param self CStructBase |
| 26 | +local function is_valid(self) |
| 27 | + return self.m_ptr and self.m_ptr:is_valid() or false |
| 28 | +end |
41 | 29 |
|
42 | | - ---@return boolean |
43 | | - function cls:IsValid() |
44 | | - return self.m_ptr and self.m_ptr:is_valid() or false |
45 | | - end |
| 30 | +---@param self CStructBase |
| 31 | +local function get_pointer(self) |
| 32 | + return self.m_ptr or nullptr |
| 33 | +end |
46 | 34 |
|
47 | | - ---@return pointer |
48 | | - function cls:GetPointer() |
49 | | - return self.m_ptr or nullptr |
50 | | - end |
| 35 | +---@param self CStructBase |
| 36 | +local function get_address(self) |
| 37 | + return self.m_ptr and self.m_ptr:get_address() or 0x0 |
| 38 | +end |
51 | 39 |
|
52 | | - ---@return uint64_t |
53 | | - function cls:GetAddress() |
54 | | - return self.m_ptr and self.m_ptr:get_address() or 0x0 |
| 40 | +---@generic R1, R2, R3, R4, R5 |
| 41 | +---@param self CStructBase |
| 42 | +---@param default any Defaul value to return on failure |
| 43 | +---@param func fun(...): R1, R2?, R3?, R4?, R5?, ... ? |
| 44 | +---@param ... any |
| 45 | +---@return R1, R2?, R3?, R4?, R5?, ... ? |
| 46 | +local function safe_call(self, default, func, ...) |
| 47 | + if (not self:IsValid()) then |
| 48 | + return default |
55 | 49 | end |
56 | 50 |
|
57 | | - ---@private |
58 | | - ---@generic R1, R2, R3, R4, R5 |
59 | | - ---@param default any Defaul value to return on failure |
60 | | - ---@param func fun(...): R1, R2?, R3?, R4?, R5?, ... ? |
61 | | - ---@param ... any |
62 | | - ---@return R1, R2?, R3?, R4?, R5?, ... ? |
63 | | - function cls:__safecall(default, func, ...) |
64 | | - if (not self:IsValid()) then |
65 | | - return default |
66 | | - end |
67 | | - |
68 | | - local results = { pcall(func, ...) } |
69 | | - local ok = results[1] |
70 | | - local err = results[2] |
71 | | - |
72 | | - if (not ok) then |
73 | | - log.fwarning("Safecall failed in %s: %s", self.__type, err) |
74 | | - return default |
75 | | - end |
| 51 | + local results = { pcall(func, ...) } |
| 52 | + local ok = results[1] |
| 53 | + local err = results[2] |
76 | 54 |
|
77 | | - table.remove(results, 1) |
78 | | - return table.unpack(results) |
| 55 | + if (not ok) then |
| 56 | + log.fwarning("Safecall failed in %s: %s", self.__type, err) |
| 57 | + return default |
79 | 58 | end |
80 | 59 |
|
81 | | - return cls |
| 60 | + table.remove(results, 1) |
| 61 | + return table.unpack(results) |
82 | 62 | end |
83 | 63 |
|
84 | | -return CStructView |
| 64 | +---@diagnostic enable: invisible |
| 65 | + |
| 66 | +-- Creates a basic GTA class definition with default methods. |
| 67 | +---@generic T |
| 68 | +---@param name string |
| 69 | +---@param size? integer Optional sugar, plays nice with `SizeOf` |
| 70 | +---@return CStructBase<T> |
| 71 | +return function(name, size) |
| 72 | + return Callable(name, { |
| 73 | + ctor = function(t, ptr) return t.new(ptr) end, |
| 74 | + ptr_ctor = true, |
| 75 | + t_data = { |
| 76 | + m_size = size or GenericClass.m_size, |
| 77 | + IsValid = is_valid, |
| 78 | + GetPointer = get_pointer, |
| 79 | + GetAddress = get_address, |
| 80 | + __safecall = safe_call |
| 81 | + }, |
| 82 | + }) |
| 83 | +end |
0 commit comments