Skip to content

Commit f1feec9

Browse files
committed
fix: several fixes
- Fix miscellaneous typos. - Cleanup the global namespace by limiting global exposure. - Add a callable object generator function to be used instead of `Class` for lightweight objects. - Rename `Game.GetGXTLabel` to `Game.GetLabelText`. - Rename `HandlingPreset` to `VehicleFlagPreset`. - Manual Gearbox: Add an `Automatic` mode that simply does nothing other than allow the user to still use the engine start/stop keybind with default engine and gearbox behavior. Fix RGB lights color switch timing. It should now only switch colors when the lights are dimmed.
1 parent 6592e66 commit f1feec9

97 files changed

Lines changed: 1250 additions & 852 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: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,11 @@
1414
---@class Mutex
1515
---@field protected 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")
2518

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

3224
---@return boolean

SSV2/includes/classes/Pair.lua

Lines changed: 6 additions & 3 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 = {}
14+
local Pair = {}
1515
Pair.__index = Pair
1616

17-
local _mt = {}
18-
_mt.__index = function(self, k)
17+
18+
local _mt = {}
19+
_mt.__index = function(self, k)
1920
if (k == "first") then
2021
return self._raw[1]
2122
end
@@ -73,3 +74,5 @@ 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: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@
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")
2821

2922
---@param from number
3023
---@param to number

SSV2/includes/classes/Rect.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@
1414
---@field min vec2
1515
---@field 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(...)
23-
end
24-
})
17+
local Rect = Callable("Rect")
2518

2619
---@param min vec2
2720
---@param max vec2
@@ -93,3 +86,5 @@ end
9386
function Rect:__add(other)
9487
return self:Add(other)
9588
end
89+
90+
return Rect

SSV2/includes/classes/Set.lua

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@
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(...)
25-
end
26-
})
18+
local Set = Callable("Set")
2719

2820
function Set.new(...)
2921
---@diagnostic disable-next-line: param-type-mismatch
@@ -101,7 +93,4 @@ function Set:__pairs()
10193
return pairs(self.m_data)
10294
end
10395

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
96+
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)