|
1 | | ----@diagnostic disable: lowercase-global |
| 1 | +-- Copyright (C) 2026 SAMURAI (xesdoog) & Contributors. |
| 2 | +-- This file is part of Samurai's Scripts. |
| 3 | +-- |
| 4 | +-- Permission is hereby granted to copy, modify, and redistribute |
| 5 | +-- this code as long as you respect these conditions: |
| 6 | +-- * Credit the owner and contributors. |
| 7 | +-- * 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/>. |
| 8 | + |
2 | 9 |
|
3 | 10 | local PreviewService = require("includes.services.PreviewService") |
4 | 11 | local function ClearPreview() |
@@ -35,36 +42,38 @@ Enums.eEntityType = { |
35 | 42 | ---@class Backend |
36 | 43 | ---@field private api_version eAPIVersion |
37 | 44 | local Backend = { |
38 | | - __version = "", |
39 | | - target_build = "", |
40 | | - target_version = "", |
41 | | - 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, |
42 | 51 |
|
43 | 52 | ---@type table<integer, integer> |
44 | | - ControlsToDisable = {}, |
| 53 | + ControlsToDisable = {}, |
45 | 54 |
|
46 | 55 | ---@type table<integer, BlipData> |
47 | | - CreatedBlips = {}, |
| 56 | + CreatedBlips = {}, |
48 | 57 |
|
49 | 58 | ---@type array<handle> |
50 | | - AttachedEntities = {}, |
| 59 | + AttachedEntities = {}, |
51 | 60 |
|
52 | 61 | ---@type table<eBackendEvent, array<function>> |
53 | | - EventCallbacks = { |
| 62 | + EventCallbacks = { |
54 | 63 | [Enums.eBackendEvent.RELOAD_UNLOAD] = { ClearPreview }, |
55 | 64 | [Enums.eBackendEvent.SESSION_SWITCH] = { ClearPreview }, |
56 | 65 | [Enums.eBackendEvent.PLAYER_SWITCH] = { ClearPreview } |
57 | 66 | }, |
58 | 67 |
|
59 | 68 | ---@type table<eEntityType, table<handle, handle>> |
60 | | - SpawnedEntities = { |
| 69 | + SpawnedEntities = { |
61 | 70 | [Enums.eEntityType.Ped] = {}, |
62 | 71 | [Enums.eEntityType.Vehicle] = {}, |
63 | 72 | [Enums.eEntityType.Object] = {}, |
64 | 73 | }, |
65 | 74 |
|
66 | 75 | ---@type table<eEntityType, integer> |
67 | | - MaxAllowedEntities = { |
| 76 | + MaxAllowedEntities = { |
68 | 77 | [Enums.eEntityType.Ped] = 50, |
69 | 78 | [Enums.eEntityType.Vehicle] = 25, |
70 | 79 | [Enums.eEntityType.Object] = 75, |
@@ -411,29 +420,47 @@ function Backend:Cleanup() |
411 | 420 | end |
412 | 421 |
|
413 | 422 | function Backend:OnSessionSwitch() |
| 423 | + if (self.is_in_session_transition) then |
| 424 | + return |
| 425 | + end |
| 426 | + |
414 | 427 | if (not script.is_active("maintransition")) then |
415 | 428 | return |
416 | 429 | end |
417 | 430 |
|
418 | | - self:TriggerEventCallbacks(Enums.eBackendEvent.SESSION_SWITCH) |
| 431 | + self.is_in_session_transition = true |
| 432 | + ThreadManager:Run(function() |
| 433 | + self:TriggerEventCallbacks(Enums.eBackendEvent.SESSION_SWITCH) |
419 | 434 |
|
420 | | - repeat |
421 | | - sleep(100) |
422 | | - until not script.is_active("maintransition") |
423 | | - 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) |
424 | 442 | end |
425 | 443 |
|
426 | 444 | function Backend:OnPlayerSwitch() |
| 445 | + if (self.is_in_player_transition) then |
| 446 | + return |
| 447 | + end |
| 448 | + |
427 | 449 | if (not self:IsPlayerSwitchInProgress()) then |
428 | 450 | return |
429 | 451 | end |
430 | 452 |
|
431 | | - 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) |
432 | 457 |
|
433 | | - repeat |
434 | | - sleep(100) |
435 | | - until not self:IsPlayerSwitchInProgress() |
436 | | - sleep(1000) |
| 458 | + while (self:IsPlayerSwitchInProgress()) do |
| 459 | + yield() |
| 460 | + end |
| 461 | + |
| 462 | + self.is_in_player_transition = false |
| 463 | + end) |
437 | 464 | end |
438 | 465 |
|
439 | 466 | function Backend:RegisterHandlers() |
|
0 commit comments