Skip to content

Commit d60f628

Browse files
committed
fix: fix memory stuff
1 parent 3cc1029 commit d60f628

6 files changed

Lines changed: 27 additions & 108 deletions

File tree

SSV2/includes/classes/gta/atArray.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function atArray.new(ptr, data_type)
5353
if (array_size == 0) then return instance end
5454

5555
instance.m_size = array_size
56-
instance.m_capacity = ptr:add(0x10):get_word()
56+
instance.m_capacity = ptr:add(0xA):get_word()
5757
instance.m_data_type = data_type
5858

5959
for i = 0, array_size - 1 do
@@ -96,7 +96,7 @@ end
9696

9797
---@return string
9898
function atArray:GetDataType()
99-
local _t = "unknonwn"
99+
local _t = "unknown"
100100
if (type(self.m_data_type) == "table" and self.m_data_type.__type) then
101101
_t = self.m_data_type.__type
102102
end
@@ -135,7 +135,6 @@ end
135135

136136
---@return fun(t: array<T>, i?: integer): integer, pointer<T> Iterator
137137
function atArray:__pairs()
138-
log.warning("[atArray]: Use of pairs! Please use atArray:Iter() instead.")
139138
return self:Iter()
140139
end
141140

SSV2/includes/features/vehicle/car_crashes.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ function CarCrash:Init()
6868
name = self.m_entity.MemoryPatches.DeformMult,
6969
onEnable = function(patch)
7070
local cvehicle = self.m_entity:Resolve()
71-
if (not cvehicle) then
72-
error("Handling data is null!")
73-
end
71+
if (not cvehicle) then return end
7472

75-
local fDeformMult = cvehicle.m_deform_mult
73+
local fDeformMult = cvehicle.m_handling_data.m_deform_mult
7674
patch.m_state = {
7775
ptr = fDeformMult,
7876
default_value = fDeformMult:get_float()
@@ -89,7 +87,7 @@ function CarCrash:Init()
8987

9088
local ptr = patch.m_state.ptr
9189
if (not ptr or ptr:is_null()) then
92-
error("pointer is null")
90+
return
9391
end
9492

9593
ptr:set_float(patch.m_state.default_value)

SSV2/includes/features/vehicle/misc_vehicle.lua

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,61 +29,46 @@ function MiscVehicle:Init()
2929
self.m_entity:AddMemoryPatch({
3030
name = self.m_entity.MemoryPatches.Turbulence,
3131
onEnable = function(patch)
32-
if (not self.m_entity:IsPlane()) then
33-
error("Invalid vehicle type!")
34-
return
35-
end
32+
if (not self.m_entity:IsPlane()) then return end
3633

3734
---@type CFlyingHandlingData
3835
local handingdata = self.m_entity:GetHandlingData()
39-
if (not handingdata) then
40-
error("Handling data is null!")
41-
end
36+
if (not handingdata) then return end
4237

43-
local fturbulence = handingdata.m_turbulence_force_mult
44-
if (fturbulence:is_null()) then
45-
error("Pointer is null!")
46-
end
38+
local fTurbulence = handingdata.m_turbulence_force_mult
39+
if (fTurbulence:is_null()) then return end
4740

4841
patch.m_state = {
49-
ptr = fturbulence,
50-
default_value = fturbulence:get_float()
42+
ptr = fTurbulence,
43+
default_value = fTurbulence:get_float()
5144
}
5245

53-
fturbulence:set_float(0.0)
46+
fTurbulence:set_float(0.0)
5447
end,
5548
onDisable = function(patch)
5649
if (not patch.m_state or patch.m_state.default_value == nil) then
5750
return
5851
end
5952

6053
local ptr = patch.m_state.ptr
61-
if (not ptr or ptr:is_null()) then
62-
error("Pointer is null")
63-
end
64-
54+
if (not ptr or ptr:is_null()) then return end
6555
ptr:set_float(patch.m_state.default_value)
6656
end
6757
})
6858

6959
self.m_entity:AddMemoryPatch({
7060
name = self.m_entity.MemoryPatches.WindMult,
7161
onEnable = function(patch)
72-
if (not self.m_entity:IsPlane()) then
73-
error("Invalid vehicle type!")
74-
return
75-
end
62+
if (not self.m_entity:IsPlane()) then return end
7663

77-
---@type CFlyingHandlingData
64+
---@type CFlyingHandlingData?
7865
local handingdata = self.m_entity:GetHandlingData()
79-
if (not handingdata) then
80-
error("Handling data is null!")
66+
if (not handingdata or not handingdata:IsValid()) then
67+
return
8168
end
8269

8370
local fwindForce = handingdata.m_wind_force_mult
84-
if (fwindForce:is_null()) then
85-
error("Pointer is null!")
86-
end
71+
if (fwindForce:is_null()) then return end
8772

8873
patch.m_state = {
8974
ptr = fwindForce,
@@ -98,10 +83,7 @@ function MiscVehicle:Init()
9883
end
9984

10085
local ptr = patch.m_state.ptr
101-
if (not ptr or ptr:is_null()) then
102-
error("Pointer is null")
103-
end
104-
86+
if (not ptr or ptr:is_null()) then return end
10587
ptr:set_float(patch.m_state.default_value)
10688
end
10789
})

SSV2/includes/frontend/vehicle/vehicle_ui.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,11 +602,9 @@ vehicleTab:RegisterSubtab("VEH_ENGINE_SWAP", function()
602602
name = PV.MemoryPatches.Acceleration,
603603
onEnable = function(patch)
604604
local cvehicle = PV:Resolve()
605-
if (not cvehicle) then
606-
error("Handling data is null!")
607-
end
605+
if (not cvehicle) then return end
608606

609-
local fAccelMult = cvehicle.m_acceleration
607+
local fAccelMult = cvehicle.m_handling_data.m_acceleration
610608
patch.m_state = {
611609
ptr = fAccelMult,
612610
default_value = fAccelMult:get_float()
@@ -622,10 +620,7 @@ vehicleTab:RegisterSubtab("VEH_ENGINE_SWAP", function()
622620
end
623621

624622
local ptr = patch.m_state.ptr
625-
if (not ptr or ptr:is_null()) then
626-
error("pointer is null")
627-
end
628-
623+
if (not ptr or ptr:is_null()) then return end
629624
ptr:set_float(patch.m_state.default_value)
630625
end
631626
}, true)

SSV2/includes/modules/Memory.lua

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
local CPed = require("includes.classes.gta.CPed")
1313
local CVehicle = require("includes.classes.gta.CVehicle")
1414
local MemoryPatch = require("includes.structs.MemoryPatch")
15+
local CEntity = require("includes.classes.gta.CEntity")
1516

1617

1718
---@class PatchData
@@ -233,40 +234,14 @@ function Memory:GlobalIndexFromAddress(addr)
233234
return 0
234235
end
235236

236-
---@param vehicle integer vehicle handle
237-
---@return CVehicle|nil
238-
function Memory:GetVehicleInternal(vehicle)
239-
return CVehicle(vehicle)
240-
end
241-
242-
---@param ped handle A Ped ID, not a Player ID.
243-
---@return CPed|nil
244-
function Memory:GetPedInternal(ped)
245-
return CPed(ped)
246-
end
247-
248237
-- Checks if a vehicle's handling flag is set. It is recommended to use the `Vehicle` module instead since it caches the CVehicle instance.
249238
--
250239
-- This is only useful if you want to quickly get/set a flag once and don't need a `Vehicle` instance.
251240
---@param vehicle handle
252241
---@param flag eVehicleHandlingFlags
253242
---@return boolean
254243
function Memory:GetVehicleHandlingFlag(vehicle, flag)
255-
if not (ENTITY.DOES_ENTITY_EXIST(vehicle) or ENTITY.IS_ENTITY_A_VEHICLE(vehicle)) then
256-
return false
257-
end
258-
259-
local cvehicle = self:GetVehicleInternal(vehicle)
260-
if (not cvehicle) then
261-
return false
262-
end
263-
264-
local m_handling_flags = cvehicle.m_handling_flags
265-
if m_handling_flags:is_null() then
266-
return false
267-
end
268-
269-
return Bit.IsBitSet(m_handling_flags:get_dword(), flag)
244+
return CVehicle(vehicle):GetHandlingFlag(flag)
270245
end
271246

272247
-- Checks if a vehicle's model info flag is set. It is recommended to use the `Vehicle` module instead since it caches the CVehicle instance.
@@ -276,22 +251,7 @@ end
276251
---@param flag eVehicleModelFlags
277252
---@return boolean
278253
function Memory:GetVehicleModelInfoFlag(vehicle, flag)
279-
local cvehicle = self:GetVehicleInternal(vehicle)
280-
if (not cvehicle) then
281-
return false
282-
end
283-
284-
local base_ptr = cvehicle.m_model_info_flags
285-
if base_ptr:is_null() then
286-
return false
287-
end
288-
289-
local index = math.floor(flag / 32)
290-
local bitPos = flag % 32
291-
local flag_ptr = base_ptr:add(index * 4)
292-
local dword = flag_ptr:get_dword()
293-
294-
return Bit.IsBitSet(dword, bitPos)
254+
return CVehicle(vehicle):GetModelInfoFlag(flag)
295255
end
296256

297257
-- Unsafe for non-scripted entities.
@@ -300,22 +260,7 @@ end
300260
---@param entity handle
301261
---@return eModelType
302262
function Memory:GetEntityModelType(entity)
303-
if (not ENTITY.DOES_ENTITY_EXIST(entity)) then
304-
return Enums.eModelType.Invalid
305-
end
306-
307-
local isMemSafe, entityType = pcall(function()
308-
local pEntity = memory.handle_to_ptr(entity)
309-
if (pEntity:is_null()) then
310-
return Enums.eModelType.Invalid
311-
end
312-
313-
local m_model_info = pEntity:add(0x0020):deref()
314-
local m_model_type = m_model_info:add(0x009D)
315-
return m_model_type:get_word() & 0x1F
316-
end)
317-
318-
return isMemSafe and entityType or Enums.eModelType.Invalid
263+
return CEntity(entity):GetModelType()
319264
end
320265

321266
--[[

SSV2/includes/modules/Vehicle.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ function Vehicle:SetBoneMatrix(bone_index, matrix)
13261326
self:Resolve():SetBoneMatrix(bone_index, matrix)
13271327
end
13281328

1329-
---@return (CCarHandlingData|CBikeHandlingData|CFlyingHandlingData)?
1329+
---@return (CCarHandlingData|CBikeHandlingData|CFlyingHandlingData|any)?
13301330
function Vehicle:GetHandlingData()
13311331
if (not self:IsValid()) then return end
13321332

0 commit comments

Comments
 (0)