Skip to content

Commit c369617

Browse files
committed
fix(misc): miscellaneous fixes
1 parent f8b2cd3 commit c369617

23 files changed

Lines changed: 654 additions & 258 deletions

SSV2/includes/backend.lua

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,38 @@ Enums.eEntityType = {
4242
---@class Backend
4343
---@field private api_version eAPIVersion
4444
local Backend = {
45-
__version = "",
46-
target_build = "",
47-
target_version = "",
48-
disable_input = false, -- Never serialize this runtime variable!
45+
__version = "",
46+
target_build = "",
47+
target_version = "",
48+
disable_input = false, -- Never serialize this runtime variable!
49+
is_in_session_transition = false,
50+
is_in_player_transition = false,
4951

5052
---@type table<integer, integer>
51-
ControlsToDisable = {},
53+
ControlsToDisable = {},
5254

5355
---@type table<integer, BlipData>
54-
CreatedBlips = {},
56+
CreatedBlips = {},
5557

5658
---@type array<handle>
57-
AttachedEntities = {},
59+
AttachedEntities = {},
5860

5961
---@type table<eBackendEvent, array<function>>
60-
EventCallbacks = {
62+
EventCallbacks = {
6163
[Enums.eBackendEvent.RELOAD_UNLOAD] = { ClearPreview },
6264
[Enums.eBackendEvent.SESSION_SWITCH] = { ClearPreview },
6365
[Enums.eBackendEvent.PLAYER_SWITCH] = { ClearPreview }
6466
},
6567

6668
---@type table<eEntityType, table<handle, handle>>
67-
SpawnedEntities = {
69+
SpawnedEntities = {
6870
[Enums.eEntityType.Ped] = {},
6971
[Enums.eEntityType.Vehicle] = {},
7072
[Enums.eEntityType.Object] = {},
7173
},
7274

7375
---@type table<eEntityType, integer>
74-
MaxAllowedEntities = {
76+
MaxAllowedEntities = {
7577
[Enums.eEntityType.Ped] = 50,
7678
[Enums.eEntityType.Vehicle] = 25,
7779
[Enums.eEntityType.Object] = 75,
@@ -418,29 +420,47 @@ function Backend:Cleanup()
418420
end
419421

420422
function Backend:OnSessionSwitch()
423+
if (self.is_in_session_transition) then
424+
return
425+
end
426+
421427
if (not script.is_active("maintransition")) then
422428
return
423429
end
424430

425-
self:TriggerEventCallbacks(Enums.eBackendEvent.SESSION_SWITCH)
431+
self.is_in_session_transition = true
432+
ThreadManager:Run(function()
433+
self:TriggerEventCallbacks(Enums.eBackendEvent.SESSION_SWITCH)
426434

427-
repeat
428-
sleep(100)
429-
until not script.is_active("maintransition")
430-
sleep(1000)
435+
while (script.is_active("maintransition")) do
436+
yield()
437+
end
438+
439+
sleep(1000)
440+
self.is_in_session_transition = false
441+
end)
431442
end
432443

433444
function Backend:OnPlayerSwitch()
445+
if (self.is_in_player_transition) then
446+
return
447+
end
448+
434449
if (not self:IsPlayerSwitchInProgress()) then
435450
return
436451
end
437452

438-
self:TriggerEventCallbacks(Enums.eBackendEvent.PLAYER_SWITCH)
453+
self.is_in_player_transition = true
454+
455+
ThreadManager:Run(function()
456+
self:TriggerEventCallbacks(Enums.eBackendEvent.PLAYER_SWITCH)
457+
458+
while (self:IsPlayerSwitchInProgress()) do
459+
yield()
460+
end
439461

440-
repeat
441-
sleep(100)
442-
until not self:IsPlayerSwitchInProgress()
443-
sleep(1000)
462+
self.is_in_player_transition = false
463+
end)
444464
end
445465

446466
function Backend:RegisterHandlers()

SSV2/includes/data/yrv3_data.lua

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -566,26 +566,26 @@ local RawBusinessData <const> = {
566566
{ size = 2, max = 111, coords = vec3:new(93.278641, -2216.144775, 6.033320) },
567567
},
568568
BikerBusinesses = {
569-
{ gxt = "MP_BWH_METH_1", coords = vec3:new(52.903, 6338.585, 31.35), },
570-
{ gxt = "MP_BWH_WEED_1", coords = vec3:new(416.7524, 6520.753, 27.7121), },
571-
{ gxt = "MP_BWH_CRACK_1", coords = vec3:new(51.7653, 6486.163, 31.428), },
572-
{ gxt = "MP_BWH_CASH_1", coords = vec3:new(-413.6606, 6171.938, 31.4782), },
573-
{ gxt = "MP_BWH_FAKEID_1", coords = vec3:new(-163.6828, 6334.845, 31.5808), },
574-
{ gxt = "MP_BWH_METH_2", coords = vec3:new(1454.671, -1651.986, 67), },
575-
{ gxt = "MP_BWH_WEED_2", coords = vec3:new(102.14, 175.26, 104.56), },
576-
{ gxt = "MP_BWH_CRACK_2", coords = vec3:new(-1462.622, -381.826, 38.802), },
577-
{ gxt = "MP_BWH_CASH_2", coords = vec3:new(-1171.005, -1380.922, 4.937), },
578-
{ gxt = "MP_BWH_FAKEID_2", coords = vec3:new(299.071, -759.072, 29.333), },
579-
{ gxt = "MP_BWH_METH_3", coords = vec3:new(201.8909, 2461.782, 55.6885), },
580-
{ gxt = "MP_BWH_WEED_3", coords = vec3:new(2848.369, 4450.147, 48.5139), },
581-
{ gxt = "MP_BWH_CRACK_3", coords = vec3:new(387.5332, 3585.042, 33.2922), },
582-
{ gxt = "MP_BWH_CASH_3", coords = vec3:new(636.6344, 2785.126, 42.0111), },
583-
{ gxt = "MP_BWH_FAKEID_3", coords = vec3:new(1657.066, 4851.732, 41.9882), },
584-
{ gxt = "MP_BWH_METH_4", coords = vec3:new(1181.44, -3113.82, 6.03), },
585-
{ gxt = "MP_BWH_WEED_4", coords = vec3:new(136.973, -2472.795, 5.98), },
586-
{ gxt = "MP_BWH_CRACK_4", coords = vec3:new(-253.31, -2591.15, 5.97), },
587-
{ gxt = "MP_BWH_CASH_4", coords = vec3:new(671.451, -2667.502, 6.0812), },
588-
{ gxt = "MP_BWH_FAKEID_4", coords = vec3:new(-331.52, -2778.97, 5.12), },
569+
{ gxt = "MP_BWH_METH_1", id = 3, coords = vec3:new(52.903, 6338.585, 31.35), },
570+
{ gxt = "MP_BWH_WEED_1", id = 1, coords = vec3:new(416.7524, 6520.753, 27.7121), },
571+
{ gxt = "MP_BWH_CRACK_1", id = 4, coords = vec3:new(51.7653, 6486.163, 31.428), },
572+
{ gxt = "MP_BWH_CASH_1", id = 2, coords = vec3:new(-413.6606, 6171.938, 31.4782), },
573+
{ gxt = "MP_BWH_FAKEID_1", id = 0, coords = vec3:new(-163.6828, 6334.845, 31.5808), },
574+
{ gxt = "MP_BWH_METH_2", id = 3, coords = vec3:new(1454.671, -1651.986, 67), },
575+
{ gxt = "MP_BWH_WEED_2", id = 1, coords = vec3:new(102.14, 175.26, 104.56), },
576+
{ gxt = "MP_BWH_CRACK_2", id = 4, coords = vec3:new(-1462.622, -381.826, 38.802), },
577+
{ gxt = "MP_BWH_CASH_2", id = 2, coords = vec3:new(-1171.005, -1380.922, 4.937), },
578+
{ gxt = "MP_BWH_FAKEID_2", id = 0, coords = vec3:new(299.071, -759.072, 29.333), },
579+
{ gxt = "MP_BWH_METH_3", id = 3, coords = vec3:new(201.8909, 2461.782, 55.6885), },
580+
{ gxt = "MP_BWH_WEED_3", id = 1, coords = vec3:new(2848.369, 4450.147, 48.5139), },
581+
{ gxt = "MP_BWH_CRACK_3", id = 4, coords = vec3:new(387.5332, 3585.042, 33.2922), },
582+
{ gxt = "MP_BWH_CASH_3", id = 2, coords = vec3:new(636.6344, 2785.126, 42.0111), },
583+
{ gxt = "MP_BWH_FAKEID_3", id = 0, coords = vec3:new(1657.066, 4851.732, 41.9882), },
584+
{ gxt = "MP_BWH_METH_4", id = 3, coords = vec3:new(1181.44, -3113.82, 6.03), },
585+
{ gxt = "MP_BWH_WEED_4", id = 1, coords = vec3:new(136.973, -2472.795, 5.98), },
586+
{ gxt = "MP_BWH_CRACK_4", id = 4, coords = vec3:new(-253.31, -2591.15, 5.97), },
587+
{ gxt = "MP_BWH_CASH_4", id = 2, coords = vec3:new(671.451, -2667.502, 6.0812), },
588+
{ gxt = "MP_BWH_FAKEID_4", id = 0, coords = vec3:new(-331.52, -2778.97, 5.12), },
589589
},
590590
BikerTunables = {
591591
[0] = { max_units = 60, vpu = "BIKER_FAKEIDS_PRODUCT_VALUE", mult_1 = "BIKER_FAKEIDS_PRODUCT_VALUE_EQUIPMENT_UPGRADE", mult_2 = "BIKER_FAKEIDS_PRODUCT_VALUE_STAFF_UPGRADE" },

SSV2/includes/features/YimResupplierV3.lua

Lines changed: 94 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ local ScriptsToTerminate <const> = {
4141
"appbusinesshub"
4242
}
4343

44+
---@enum eYRState
45+
Enums.eYRState = {
46+
OFFLINE = 0x0,
47+
WAITING = 0x1,
48+
LOADING = 0x2,
49+
RUNNING = 0x3,
50+
ERROR = 0x4
51+
}
52+
4453
---@class YRV3Businesses
4554
---@field warehouses Warehouse[]
4655
---@field biker_businesses BikerBusiness[]
@@ -66,6 +75,7 @@ local ScriptsToTerminate <const> = {
6675
---@field private m_cooldown_state_dirty boolean
6776
---@field private m_initial_data_done boolean
6877
---@field private m_data_initialized boolean
78+
---@field protected m_state eYRState
6979
---@field protected m_thread Thread?
7080
---@field protected m_initialized boolean
7181
local YRV3 = { m_raw_data = require("includes.data.yrv3_data") }
@@ -81,13 +91,15 @@ function YRV3:init()
8191
self.m_bhub_script_handle = 0
8292
self.m_last_as_check_time = 0
8393
self.m_last_income_check_time = 0
94+
self.m_state = Enums.eYRState.OFFLINE
8495
self.m_has_triggered_autosell = false
8596
self.m_sell_script_running = false
8697
self.m_initial_data_done = false
8798
self.m_data_initialized = false
8899
self.m_cooldown_state_dirty = true
89100
self.m_sell_script_name = nil
90101
self.m_sell_script_disp_name = "None"
102+
self.m_last_error = ""
91103
self.m_businesses = {
92104
warehouses = {},
93105
biker_businesses = {},
@@ -101,30 +113,61 @@ function YRV3:init()
101113
end)
102114

103115
Backend:RegisterEventCallback(Enums.eBackendEvent.RELOAD_UNLOAD, function()
104-
self:Reset()
116+
if (self.m_data_initialized) then
117+
self:Reset()
118+
end
105119
end)
106120

107121
Backend:RegisterEventCallback(Enums.eBackendEvent.SESSION_SWITCH, function()
108-
self:Reset()
122+
if (self.m_data_initialized) then
123+
self:Reset()
124+
end
109125
end)
110126

111127
return self
112128
end
113129

114130
function YRV3:Reset()
115-
self.m_cooldown_state_dirty = true
131+
self.m_total_sum = 0
132+
self.m_last_as_check_time = 0
133+
self.m_last_income_check_time = 0
134+
self.m_businesses = {
135+
warehouses = {},
136+
biker_businesses = {},
137+
safes = self.m_raw_data.BS_Default,
138+
money_fronts = self.m_raw_data.MF_Default,
139+
}
140+
self.m_has_triggered_autosell = false
141+
self.m_sell_script_running = false
142+
self.m_initial_data_done = false
143+
self.m_data_initialized = false
144+
self.m_cooldown_state_dirty = true
145+
end
146+
147+
function YRV3:Reload()
148+
if (self.m_thread and self.m_thread:IsRunning()) then
149+
self.m_thread:Suspend()
150+
end
116151

117-
-- we no longer reset business data here and end up with dangling pointers in ImGui.
118-
-- if the player changes something (swaps properties, buys a new one, etc.)
119-
-- they can simply reload the script. It's not rocket science
152+
self:Reset()
153+
154+
if (self.m_thread and self.m_thread:IsSuspended()) then
155+
self.m_thread:Resume()
156+
end
120157
end
121158

159+
---@return boolean
122160
function YRV3:CanAccess()
123161
return Backend:IsUpToDate()
124162
and Game.IsOnline()
125163
and not Backend:IsMockEnv()
126-
and not script.is_active("maintransition")
127164
and not NETWORK.NETWORK_IS_ACTIVITY_SESSION()
165+
and self.m_state ~= Enums.eYRState.ERROR
166+
end
167+
168+
---@return boolean
169+
function YRV3:IsDataInitialized()
170+
return self.m_data_initialized
128171
end
129172

130173
---@param where integer|vec3
@@ -138,8 +181,9 @@ function YRV3:Teleport(where, keepVehicle)
138181
Self:Teleport(where, keepVehicle)
139182
end
140183

141-
function YRV3:IsDataInitialized()
142-
return self.m_data_initialized
184+
---@return eYRState
185+
function YRV3:GetState()
186+
return self.m_state
143187
end
144188

145189
---@return string
@@ -151,6 +195,15 @@ function YRV3:GetLastError()
151195
return self.m_last_error
152196
end
153197

198+
---@param msg string
199+
function YRV3:SetLastError(msg)
200+
if (msg == self.m_last_error) then
201+
return
202+
end
203+
204+
self.m_last_error = msg
205+
end
206+
154207
---@return array<Warehouse>
155208
function YRV3:GetSCWarehouses()
156209
return self.m_businesses.warehouses
@@ -243,14 +296,18 @@ end
243296
function YRV3:PopulateBikerBusinesses()
244297
for i = 0, 4 do
245298
local property_index = (stats.get_int(_F("MPX_FACTORYSLOT%d", i)))
246-
local ref = self.m_raw_data.BikerBusinesses[property_index]
247-
local ref2 = self.m_raw_data.BikerTunables[ref.id]
248-
if (not ref or not ref2) then
299+
local ref = self.m_raw_data.BikerBusinesses[property_index]
300+
if (not ref) then
301+
goto continue
302+
end
303+
304+
local ref2 = self.m_raw_data.BikerTunables[ref.id]
305+
if (not ref2) then
249306
goto continue
250307
end
251308

252-
local has_eq_upgrade = stats.get_int(_F("MPX_FACTORYUPGRADES%d", ref.id)) == 1
253-
local has_staff_upgrade = stats.get_int(_F("MPX_FACTORYUPGRADES%d_1", ref.id)) == 1
309+
local has_eq_upgrade = stats.get_int(_F("MPX_FACTORYUPGRADES%d", i)) == 1
310+
local has_staff_upgrade = stats.get_int(_F("MPX_FACTORYUPGRADES%d_1", i)) == 1
254311
local eq_upg_mult = tunables.get_int(ref2.mult_1)
255312
local stf_upg_mult = tunables.get_int(ref2.mult_2)
256313
table.insert(self.m_businesses.biker_businesses, BikerBusiness.new({
@@ -301,6 +358,7 @@ function YRV3:PopulateBikerBusinesses()
301358
vpu_mult_2 = 0,
302359
vpu = tunables.get_int("BIKER_ACID_PRODUCT_VALUE"),
303360
max_units = 160,
361+
blip = 847,
304362
})
305363
end
306364
end
@@ -416,10 +474,12 @@ function YRV3:PreInit()
416474
end
417475

418476
function YRV3:InitializeData()
419-
if (self.m_data_initialized or not Game.IsOnline()) then
477+
if (self.m_data_initialized or not Game.IsOnline() or self.m_state == Enums.eYRState.LOADING) then
420478
return
421479
end
422480

481+
self.m_state = Enums.eYRState.LOADING
482+
self.m_last_error = "GENERIC_WAIT_LABEL"
423483
self:PreInit()
424484
self:PopulateNightclub()
425485
end
@@ -891,11 +951,27 @@ end
891951

892952
function YRV3:OnTick()
893953
if (not Backend:IsUpToDate() and (self.m_thread and self.m_thread:IsRunning())) then
954+
self.m_state = Enums.eYRState.ERROR
955+
self:SetLastError("GENERIC_OUTDATED")
894956
self.m_thread:Stop()
895957
return
896958
end
897959

898960
if (not self:CanAccess()) then
961+
self.m_state = Enums.eYRState.OFFLINE
962+
963+
if (not network.is_session_started()) then
964+
self:SetLastError("GENERIC_UNAVAILABLE_SP")
965+
else
966+
if (script.is_active("maintransition")) then
967+
self.m_state = Enums.eYRState.WAITING
968+
self:SetLastError("YRV3_STATE_WAIT_TRANSITION")
969+
elseif (NETWORK.NETWORK_IS_ACTIVITY_SESSION()) then
970+
self.m_state = Enums.eYRState.ERROR
971+
self:SetLastError("YRV3_STATE_ERR_WRONG_SESSION")
972+
end
973+
end
974+
899975
yield()
900976
return
901977
end
@@ -907,6 +983,9 @@ function YRV3:OnTick()
907983
return
908984
end
909985

986+
self.m_state = Enums.eYRState.RUNNING
987+
self:SetLastError("")
988+
910989
self:AutoSellHandler()
911990
self:AutoFillHandler()
912991
self:AutoProduceHandler()

0 commit comments

Comments
 (0)