Skip to content

Commit 35e77d0

Browse files
authored
Merge pull request #139 from YimMenu-Lua/misc_fixes
fix: several fixes
2 parents 6592e66 + 255a720 commit 35e77d0

111 files changed

Lines changed: 1440 additions & 1082 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

SSV2/includes/backend.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-- * Provide a copy of or a link to the original license (GPL-3.0 or later); see LICENSE.md or <https://www.gnu.org/licenses/>.
88

99

10+
local Decorator = require("includes.modules.Decorator")
1011
local PreviewService = require("includes.services.PreviewService")
1112
local ClearPreview = function() PreviewService:Clear() end
1213

@@ -122,7 +123,7 @@ function Backend:GetGameBranch()
122123
---@type (fun(): integer)?
123124
local get_game_branch = _G["get_game_branch"]
124125
if (type(get_game_branch) ~= "function") then
125-
self.m_game_branch = Enums.eGameBranch.LAGECY
126+
self.m_game_branch = Enums.eGameBranch.LEGACY
126127
return self.m_game_branch
127128
end
128129

SSV2/includes/classes/Mutex.lua

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,14 @@
1111
-- Class: Mutex
1212
--------------------------------------
1313
-- Simple mutual exclusivity.
14-
---@class Mutex
15-
---@field protected m_locked boolean
14+
---@class Mutex : Callable<Mutex>
15+
---@field private m_locked boolean
1616
---@overload fun(): Mutex
17-
local Mutex = {}
18-
Mutex.__index = Mutex
19-
---@diagnostic disable-next-line
20-
setmetatable(Mutex, {
21-
__call = function(_)
22-
return Mutex.new()
23-
end
24-
})
17+
local Mutex = Callable("Mutex", { ctor = function(t) return t:new() end })
2518

2619
---@return Mutex
27-
function Mutex.new()
28-
---@diagnostic disable-next-line
29-
return setmetatable({ m_locked = false }, Mutex)
20+
function Mutex:new()
21+
return setmetatable({ m_locked = false }, self)
3022
end
3123

3224
---@return boolean

SSV2/includes/classes/Pair.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
-- Class: Pair
1212
--------------------------------------
1313
---@class Pair<K, V> : { first: K, second: V }
14-
Pair = {}
15-
Pair.__index = Pair
14+
---@overload fun(k: K, v: V): Pair<K, V>
15+
local Pair = Callable("Pair", { ctor = function(t, ...) return t:new(...) end })
1616

17-
local _mt = {}
18-
_mt.__index = function(self, k)
17+
18+
local _mt <const> = {}
19+
_mt.__index = function(self, k)
1920
if (k == "first") then
2021
return self._raw[1]
2122
end
@@ -27,7 +28,7 @@ _mt.__index = function(self, k)
2728
return _mt[k]
2829
end
2930

30-
_mt.__newindex = function(self, k, v)
31+
_mt.__newindex = function(self, k, v)
3132
if (type(k) == "number") then -- table.insert won't work without this
3233
if (k == 1) then
3334
self._raw[1] = v
@@ -69,7 +70,9 @@ end
6970
---@param a K
7071
---@param b V
7172
---@return Pair<K, V>
72-
function Pair.new(a, b)
73+
function Pair:new(a, b)
7374
local obj = { _raw = { a, b } }
7475
return setmetatable(obj, _mt)
7576
end
77+
78+
return Pair

SSV2/includes/classes/Range.lua

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,27 @@
1212
--------------------------------------
1313
-- A simple `Range` utiliy.
1414
---@ignore -- manual docs
15-
---@class Range
15+
---@class Range : Callable<Range>
1616
---@field private m_min number
1717
---@field private m_max number
1818
---@field private m_step number
1919
---@overload fun(from: number, to: number, step?: number): Range
20-
Range = {}
21-
Range.__index = Range
22-
---@diagnostic disable-next-line
23-
setmetatable(Range, {
24-
__call = function(_, from, to, step)
25-
return Range.new(from, to, step)
26-
end
27-
})
20+
local Range = Callable("Range", { ctor = function(t, ...) return t:new(...) end })
2821

2922
---@param from number
3023
---@param to number
3124
---@param step? number
3225
---@return Range
33-
function Range.new(from, to, step)
26+
function Range:new(from, to, step)
3427
step = step or 1
3528
assert(type(from) == "number" and type(to) == "number", "Range requires numeric from/to")
3629
assert(step ~= 0, "Step cannot be 0")
3730

3831
return setmetatable({
39-
m_min = from,
40-
m_max = to,
32+
m_min = from,
33+
m_max = to,
4134
m_step = step,
42-
---@diagnostic disable-next-line
43-
}, Range)
35+
}, self)
4436
end
4537

4638
---@param value number

SSV2/includes/classes/Rect.lua

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,21 @@
1010
--------------------------------------
1111
-- Class: Rect
1212
--------------------------------------
13-
---@class Rect
14-
---@field min vec2
15-
---@field max vec2
13+
---@class Rect : Callable<Rect>
14+
---@field public min vec2
15+
---@field public max vec2
1616
---@overload fun(min: vec2, max: vec2) : Rect
17-
Rect = {}
18-
Rect.__index = Rect
19-
---@diagnostic disable-next-line
20-
setmetatable(Rect, {
21-
__call = function(_, ...)
22-
return Rect.new(...)
17+
local Rect = Callable("Rect", {
18+
ctor = function(t, ...)
19+
return t:new(...)
2320
end
2421
})
2522

2623
---@param min vec2
2724
---@param max vec2
2825
---@return Rect
29-
function Rect.new(min, max)
30-
---@diagnostic disable-next-line
31-
return setmetatable({ min = min, max = max }, Rect)
26+
function Rect:new(min, max)
27+
return setmetatable({ min = min, max = max }, self)
3228
end
3329

3430
---@return float
@@ -93,3 +89,5 @@ end
9389
function Rect:__add(other)
9490
return self:Add(other)
9591
end
92+
93+
return Rect

SSV2/includes/classes/Set.lua

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@
1515
---@field protected m_data_type string
1616
---@field new fun(...: T): Set<T>
1717
---@overload fun(...: T): Set<T>
18-
Set = { __type = "Set" }
19-
Set.__index = Set
20-
21-
---@diagnostic disable-next-line: param-type-mismatch
22-
setmetatable(Set, {
23-
__call = function(_, ...)
24-
return Set.new(...)
18+
local Set = Callable("Set", {
19+
ctor = function(t, ...)
20+
return t:new(...)
2521
end
2622
})
2723

28-
function Set.new(...)
29-
---@diagnostic disable-next-line: param-type-mismatch
30-
local instance = setmetatable({ m_data = {} }, Set)
24+
function Set:new(...)
25+
local instance = setmetatable({ m_data = {} }, self)
3126
local args = { ... }
3227

3328
if (#args > 0) then
@@ -101,7 +96,4 @@ function Set:__pairs()
10196
return pairs(self.m_data)
10297
end
10398

104-
-- This is probably bad. Set:Contains() should be the only source of truth.
105-
-- function Set:__index(key)
106-
-- return Set[key] or (self.m_data[key] == true)
107-
-- end
99+
return Set

SSV2/includes/classes/gta/CBoatHandlingData.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ local CBaseSubHandlingData = require("includes.classes.gta.CBaseSubHandlingData"
1717
---@overload fun(ptr: pointer): CBoatHandlingData
1818
local CBoatHandlingData = Class("CBoatHandlingData", { parent = CBaseSubHandlingData, pointer_ctor = true })
1919

20+
---@param ptr pointer
21+
function CBoatHandlingData.new(ptr)
22+
---@diagnostic disable-next-line: param-type-mismatch
23+
return setmetatable({ m_ptr = ptr }, CBoatHandlingData)
24+
end
25+
2026
return CBoatHandlingData

SSV2/includes/classes/gta/CPed.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ function CPed:init(ped)
5959
m_armor = ptr:add(0x150C),
6060
m_cash = ptr:add(0x1614),
6161
m_player_info = CPlayerInfo(ptr:add(0x10A8):deref()),
62-
---@diagnostic disable-next-line: param-type-mismatch
63-
}, CPed)
62+
}, self)
6463
end
6564

6665
---@return boolean

SSV2/includes/classes/gta/CStructView.lua

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,65 +20,64 @@
2020
---@field public GetAddress fun(self: T): uint64_t
2121
---@field __safecall fun(self: T, default: any, func: fun(...?): R1, R2?, R3?, R4?, R5?, ...?): R1, R2?, R3?, R4?, R5?, ...?
2222

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
3524

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
4129

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
4634

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
5139

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
5549
end
5650

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]
7654

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
7958
end
8059

81-
return cls
60+
table.remove(results, 1)
61+
return table.unpack(results)
8262
end
8363

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

SSV2/includes/classes/gta/CVehicle.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ local phFragInst = require("includes.classes.gta.phFragInst")
3838
---@field public m_velocity pointer<vec3>
3939
---@field public m_transmission CTransmission
4040
---@field public m_deform_god pointer<uint8_t>
41-
---@field public m_frag_inst phFragInst //0x09C0 `fragInstGTA`
41+
---@field public m_frag_inst phFragInst 0x09C0 `fragInstGTA`
4242
---@field public m_turbo pointer<float>
4343
---@field public m_water_damage pointer<uint32_t>
4444
---@field public m_engine_health pointer<float>
45-
---@field public m_steering_input pointer<float> // 0x00D4 name might not correctly reflect what this actually is but this seems to store controller input (value is between 1.0 (left) .. -1.0 (right))
46-
---@field public m_steering_angle pointer<float> 0x00DC // steering angle?. We'll use it to rewrite last known steering value after exiting a vehicle in IV-Style Exit so we'll no longer have to teleport outside or patch CTaskVehicleExit
45+
---@field public m_steering_input pointer<float> 0x00D4 name might not correctly reflect what this actually is but this seems to store controller input (value is between 1.0 (left) .. -1.0 (right))
46+
---@field public m_steering_angle pointer<float> 0x00DC // steering angle? // the same max value that this member can reach is the same value that YimMenu shows in the handling editor tab so yes, this is indeed the current steering angle.
4747
---@field public m_throttle_power pointer<float> m_steering_angle + 0x8
4848
---@field public m_brake_power pointer<float>
4949
---@field public m_is_targetable pointer<byte> `bool`
5050
---@field public m_door_lock_status pointer<uint32_t>
51-
---@field public m_wheels atArray<CWheel> -- 0x0C30
52-
---@field public m_num_wheels integer -- 0x0C38
51+
---@field public m_wheels atArray<CWheel> 0x0C30
52+
---@field public m_num_wheels integer array start + 8 so atArray.m_size
5353
---@field public m_ride_height pointer<float>
5454
---@field private DumpFlags fun(self: CVehicle, enum_flags: Enum, get_func: fun(self: CVehicle, flag: integer): boolean): nil
5555
---@overload fun(vehicle: integer): CVehicle|nil
@@ -62,9 +62,7 @@ function CVehicle:init(vehicle)
6262
error("Invalid entity!")
6363
end
6464

65-
---@type CVehicle
66-
---@diagnostic disable-next-line: param-type-mismatch
67-
local instance = setmetatable({}, CVehicle)
65+
local instance = MakeInstance({}, self) ---@cast instance CVehicle
6866
instance:super().init(instance, vehicle)
6967

7068
local ptr = memory.handle_to_ptr(vehicle)

0 commit comments

Comments
 (0)