From f474b5ed0e1baf8295a5fd5931e19926f9dacd51 Mon Sep 17 00:00:00 2001 From: niekschoemaker <32094562+niekschoemaker@users.noreply.github.com> Date: Sat, 6 Nov 2021 16:39:23 +0100 Subject: [PATCH] Bugfixes and optimizations --- server/ELS-Server/ELS-Server.csproj | 3 +- server/ELS-Server/ELSServer.cs | 55 +- server/ELS-Server/Timer.cs | 58 ++ server/ELS-Server/VcfSync.cs | 2 +- server/ELS-Server/packages.config | 2 +- src/Board/ArrowBoard.cs | 2 +- src/ELS.cs | 61 ++- src/ELS.csproj | 4 +- src/Extra/Extra.FullSync.cs | 24 +- src/Extra/Extra.cs | 6 +- src/Gadgets/Ladder.cs | 2 +- src/Light/Indicator.cs | 19 +- src/Light/Light.Loop.cs | 227 +++++--- src/Light/Lights.FullSync.cs | 233 ++++++-- src/Light/Lights.Patterns.cs | 135 +++-- src/Light/Lights.cs | 134 +++-- src/Light/Lights.logic.cs | 444 +++++++++------- src/Light/Scene.cs | 13 +- src/Light/SpotLight.cs | 65 ++- src/Light/Stage.cs | 2 + src/Manager/VehicleList.cs | 159 ++---- src/Manager/VehicleManager.cs | 62 +-- src/NUI/ElsUiPanel.cs | 1 + src/RemoteEventManager.cs | 15 +- src/Siren/MainSiren/MainSiren.FullSync.cs | 34 +- src/Siren/MainSiren/MainSiren.cs | 28 +- src/Siren/Siren/Controls/Siren.Controls.KB.cs | 50 +- src/Siren/Siren/Siren.FullSync.cs | 24 + src/Siren/Siren/Siren.Logic.cs | 62 ++- src/Siren/Siren/Siren.Loop.cs | 9 +- src/Siren/Siren/Siren.cs | 7 +- src/Siren/Tone/Tones.cs | 57 +- src/Utils.cs | 23 +- src/Vehicle/ELSVehicle.cs | 73 +-- src/configuration/CustomPatterns.cs | 58 ++ src/configuration/ElsConfiguration.cs | 231 ++++---- src/configuration/VCF.cs | 503 +++++------------- src/configuration/VcfSync.cs | 68 +++ src/packages.config | 2 +- 39 files changed, 1694 insertions(+), 1263 deletions(-) create mode 100644 server/ELS-Server/Timer.cs create mode 100644 src/configuration/CustomPatterns.cs create mode 100644 src/configuration/VcfSync.cs diff --git a/server/ELS-Server/ELS-Server.csproj b/server/ELS-Server/ELS-Server.csproj index 346fe7a..98e7709 100644 --- a/server/ELS-Server/ELS-Server.csproj +++ b/server/ELS-Server/ELS-Server.csproj @@ -32,7 +32,7 @@ - ..\..\src\packages\CitizenFX.Core.Server.1.0.3489\lib\net45\CitizenFX.Core.Server.dll + ..\..\src\packages\CitizenFX.Core.Server.1.0.4394\lib\net45\CitizenFX.Core.Server.dll False @@ -54,6 +54,7 @@ + diff --git a/server/ELS-Server/ELSServer.cs b/server/ELS-Server/ELSServer.cs index bc411be..6406c5c 100644 --- a/server/ELS-Server/ELSServer.cs +++ b/server/ELS-Server/ELSServer.cs @@ -123,29 +123,50 @@ public void UpdateState(int networkID, IDictionary data = null) } } - [EventHandler(EventNames.VcfSyncServer)] - public void VcfSyncServer([FromSource] Player player) + private readonly Dictionary rateLimiters = new Dictionary(); + + private bool IsRateLimited(Player player) { - TriggerClientEvent(player, EventNames.VcfSyncClient, VcfSync.VcfData); - TriggerClientEvent(player, "ELS:PatternSync:Client", CustomPatterns.Patterns); + if (!rateLimiters.TryGetValue(player.Handle, out var timer)) + { + timer = new Timer(500); + rateLimiters.Add(player.Handle, timer); + } + else + { + if (!timer.Expired) + { + return true; + } + } + + timer.Reset(); + return false; } + [EventHandler(EventNames.FullSyncBroadcast)] - public void FullSyncBroadcast([FromSource] Player player, ExpandoObject expandoData, int networkId) + public async void FullSyncBroadcast([FromSource] Player player, ExpandoObject expandoData, int networkId) { + if (IsRateLimited(player)) + { + CitizenFX.Core.Debug.WriteLine($"{player.Name} ({player.Identifiers["steam"]}) exceeded FullSyncBroadcast ratelimiter!"); + } + var dataDict = (IDictionary)expandoData; _cachedData[networkId] = dataDict; CheckModel(networkId); _lastPlayerData[networkId] = player.Handle; UpdateState(networkId, dataDict); + await Delay(0); TriggerClientEvent(EventNames.NewFullSyncData, expandoData, networkId, int.Parse(player.Handle)); } [EventHandler(EventNames.FullSycnRequestAll)] public void FullSyncRequestAll([FromSource] Player player) { - TriggerClientEvent(player, EventNames.NewSpawnWithData, _cachedData); + TriggerLatentClientEvent(player, EventNames.NewSpawnWithData, 12500, _cachedData); } [EventHandler(EventNames.FullSyncRequestOne)] @@ -160,10 +181,15 @@ public void FullSyncRequestOne([FromSource] Player player, int networkID) } [EventHandler(EventNames.LightSyncBroadcast)] - public void LightSyncBroadcast([FromSource] Player player, IDictionary newData, int networkID) + public async void LightSyncBroadcast([FromSource] Player player, IDictionary newData, int networkID) { try { + if (IsRateLimited(player)) + { + CitizenFX.Core.Debug.WriteLine($"{player.Name} ({player.Identifiers["steam"]}) exceeded LightSyncBroadcast ratelimiter!"); + } + if (!_cachedData.TryGetValue(networkID, out var cachedData)) { cachedData = new Dictionary(); @@ -188,6 +214,7 @@ public void LightSyncBroadcast([FromSource] Player player, IDictionary newData, int networkID) + public async void SirenSyncBroadCast([FromSource] Player player, IDictionary newData, int networkID) { try { + if (IsRateLimited(player)) + { + CitizenFX.Core.Debug.WriteLine($"{player.Name} ({player.Identifiers["steam"]}) exceeded SirenSyncBroadcast ratelimiter!"); + } // Make sure the cachedData exists and otherwise create it if (!_cachedData.TryGetValue(networkID, out var cachedData)) { @@ -248,6 +284,7 @@ public void SirenSyncBroadCast([FromSource] Player player, IDictionary _limit) + { + return true; + } + else + { + return false; + } + + } + } + + public Timer() { } + + public Timer(int limit) + { + Limit = limit; + } + + public void Reset() + { + Limit = _timeLimit; + } + } +} diff --git a/server/ELS-Server/VcfSync.cs b/server/ELS-Server/VcfSync.cs index f602ea6..942a733 100644 --- a/server/ELS-Server/VcfSync.cs +++ b/server/ELS-Server/VcfSync.cs @@ -13,7 +13,7 @@ public class VcfSync public static List ElsResources = new List(); public static List> VcfData = new List>(); - public async Task CheckVCF(Player player) + public void CheckVCF(Player player) { foreach (string name in ElsResources) { diff --git a/server/ELS-Server/packages.config b/server/ELS-Server/packages.config index 88aa9ef..30433b5 100644 --- a/server/ELS-Server/packages.config +++ b/server/ELS-Server/packages.config @@ -1,6 +1,6 @@  - + \ No newline at end of file diff --git a/src/Board/ArrowBoard.cs b/src/Board/ArrowBoard.cs index 741c9b9..d5e766a 100644 --- a/src/Board/ArrowBoard.cs +++ b/src/Board/ArrowBoard.cs @@ -6,7 +6,7 @@ namespace ELS.Board { - internal class ArrowBoard : IFullSyncComponent + internal class ArrowBoard { ILight lights; configuration.MISC _misc; diff --git a/src/ELS.cs b/src/ELS.cs index f127a8b..e01f466 100644 --- a/src/ELS.cs +++ b/src/ELS.cs @@ -25,6 +25,7 @@ You should have received a copy of the GNU Lesser General Public License using ELS.NUI; using ELSShared; using Newtonsoft.Json; +using Shared; using System; using System.Collections.Generic; using System.Dynamic; @@ -37,7 +38,7 @@ namespace ELS { public class ELS : BaseScript { - public static int GameTime { get; private set; } + public static int GameTime { get; private set; } = Game.GameTime; internal static int? CurrentSeat { get; private set; } internal static Vehicle CurrentVehicle { get; private set; } private readonly FileLoader _FileLoader; @@ -50,6 +51,11 @@ public class ELS : BaseScript public ELS() { + if (API.GetConvar("dev_server", "false") == "true" || API.GetConvar("test_server", "false") == "true") + { + Utils.IsDeveloper = true; + Utils.ReleaseWriteLine("Debug logging enabled!"); + } bool Loaded = false; _controlConfiguration = new ElsConfiguration(); _FileLoader = new FileLoader(this); @@ -73,7 +79,10 @@ public ELS() await Delay(250); SetupConnections(); await Delay(250); - TriggerServerEvent(EventNames.VcfSyncServer, Game.Player.ServerId); + VcfSync.CheckResources(); + VCF.ParseVcfs(VcfSync.VcfData); + CustomPatterns.CheckCustomPatterns(); + VCF.ParsePatterns(CustomPatterns.Patterns); await Delay(250); Tick -= Class1_Tick; Tick += Class1_Tick; @@ -108,10 +117,6 @@ public ELS() } } }); - if (API.GetConvar("dev_server", "false") == "true") - { - Utils.IsDeveloper = true; - } } int lastVehicle = -1; @@ -129,15 +134,6 @@ private void SetupConnections() } }); - EventHandlers[EventNames.VcfSyncClient] += new Action>((vcfs) => - { - VCF.ParseVcfs(vcfs); - }); - - EventHandlers.Add("ELS:PatternSync:Client", new Action>((patterns) => - { - VCF.ParsePatterns(patterns); - })); EventHandlers["ELS:FullSync:NewSpawnWithData"] += new Action>((a) => { VehicleManager.Instance.SyncAllVehiclesOnFirstSpawn(a); @@ -397,7 +393,7 @@ public void GetState(int source, List args, string raw) #endif [Command("vcfsync")] - public void VcfSync() + public void VcfSyncCommand() { if (GameTime - lastCall < 60000) { @@ -500,6 +496,33 @@ internal static Vector3 Position { } set => position = value; } + + private static InputMode? _currentInputMode = null; + public static InputMode CurrentInputMode + { + get + { + if (_currentInputMode == null) + { + _currentInputMode = API.IsInputDisabled(2) ? InputMode.MouseAndKeyboard : InputMode.GamePad; + } + return _currentInputMode.Value; + } + } + + private void ClearGetterCaches() + { + GameTime = Game.GameTime; + Player = null; + Ped = null; + position = null; + _currentInputMode = null; + } + + public delegate void OnTickEventHandler(object sender); + + public static event OnTickEventHandler OnTick; + private async Task Class1_Tick() { try @@ -515,10 +538,8 @@ private async Task Class1_Tick() { return; } - GameTime = Game.GameTime; - Player = null; - Ped = null; - position = null; + OnTick?.Invoke(this); + ClearGetterCaches(); VehicleManager.Instance.RunTick(); if (CurrentVehicle?.IsEls() ?? false) { diff --git a/src/ELS.csproj b/src/ELS.csproj index f9a411a..4284da5 100644 --- a/src/ELS.csproj +++ b/src/ELS.csproj @@ -81,7 +81,7 @@ - packages\CitizenFX.Core.Client.1.0.3489\lib\net45\CitizenFX.Core.Client.dll + packages\CitizenFX.Core.Client.1.0.4394\lib\net45\CitizenFX.Core.Client.dll @@ -99,11 +99,13 @@ + + diff --git a/src/Extra/Extra.FullSync.cs b/src/Extra/Extra.FullSync.cs index 3179ee7..214f59f 100644 --- a/src/Extra/Extra.FullSync.cs +++ b/src/Extra/Extra.FullSync.cs @@ -4,7 +4,7 @@ namespace ELS.Extra { - internal partial class Extra : IFullSyncComponent + internal partial class Extra { public Dictionary GetData() @@ -12,15 +12,29 @@ public Dictionary GetData() Dictionary dic = new Dictionary(); - dic.Add(DataNames.PatternRunning, IsPatternRunning); - dic.Add(DataNames.TurnedOn, TurnedOn); + if (IsPatternRunning == true) + { + dic.Add(DataNames.PatternRunning, IsPatternRunning); + } + if (TurnedOn == true) + { + dic.Add(DataNames.TurnedOn, TurnedOn); + } return dic; } public void SetData(IDictionary data) { - IsPatternRunning = (bool)data[DataNames.PatternRunning]; - TurnedOn = (bool)data[DataNames.TurnedOn]; + if (data == null || !data.TryGetValue(DataNames.PatternRunning, out var patternRunning)) + { + patternRunning = false; + } + IsPatternRunning = (bool)patternRunning; + if (data == null || !data.TryGetValue(DataNames.TurnedOn, out var turnedOn)) + { + turnedOn = false; + } + TurnedOn = (bool)turnedOn; } } diff --git a/src/Extra/Extra.cs b/src/Extra/Extra.cs index fc47458..7424553 100644 --- a/src/Extra/Extra.cs +++ b/src/Extra/Extra.cs @@ -70,15 +70,15 @@ internal int Id } } - bool _on; - internal bool TurnedOn + bool? _on; + internal bool? TurnedOn { get { return _on; } set { if (_on == value) return; _on = value; - if (TurnedOn) + if (TurnedOn == true) { if (Id == 11) { diff --git a/src/Gadgets/Ladder.cs b/src/Gadgets/Ladder.cs index 2ee49fa..b19c30d 100644 --- a/src/Gadgets/Ladder.cs +++ b/src/Gadgets/Ladder.cs @@ -8,7 +8,7 @@ namespace ELS.Gadgets { - internal class Ladder : IFullSyncComponent + internal class Ladder { ILight lights; string _boardType; diff --git a/src/Light/Indicator.cs b/src/Light/Indicator.cs index 86c86d3..da80f30 100644 --- a/src/Light/Indicator.cs +++ b/src/Light/Indicator.cs @@ -3,6 +3,7 @@ using ELS.configuration; using ELS.NUI; using System.Collections.Generic; +using static ELS.configuration.ElsConfiguration; namespace ELS.Light { @@ -66,7 +67,7 @@ public static void ToggleRightBlinker() { ElsUiPanel.PlayUiSound("indoff"); } - RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); + //RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); return; } ToggleInicatorState(veh, IndicatorState.Right); @@ -74,13 +75,13 @@ public static void ToggleRightBlinker() { ElsUiPanel.PlayUiSound("indon"); } - RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); + //RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); } [Command("togglerightblinker")] public static void ToggleRightBlinkerCommand() { - if (ELS.CurrentVehicle != null && Vehicle.Exists(ELS.CurrentVehicle) && Game.IsControlEnabled(0, ElsConfiguration.KBBindings.ToggleRIND)) + if (ELS.CurrentVehicle != null && Vehicle.Exists(ELS.CurrentVehicle) && Controls.ToggleRIND.IsEnabled()) { ToggleRightBlinker(); } @@ -96,7 +97,7 @@ public static void ToggleLeftBlinker() { ElsUiPanel.PlayUiSound("indoff"); } - RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); + //RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); return; } @@ -105,13 +106,13 @@ public static void ToggleLeftBlinker() { ElsUiPanel.PlayUiSound("indon"); } - RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); + //RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); } [Command("toggleleftblinker")] public static void ToggleLeftBlinkerCommand() { - if (ELS.CurrentVehicle != null && Vehicle.Exists(ELS.CurrentVehicle) && Game.IsControlEnabled(0, ElsConfiguration.KBBindings.ToggleLIND)) + if (ELS.CurrentVehicle != null && Vehicle.Exists(ELS.CurrentVehicle) && Controls.ToggleLIND.IsEnabled()) { ToggleLeftBlinker(); } @@ -128,7 +129,7 @@ public static void SetHazards(bool turnOn) { ElsUiPanel.PlayUiSound("indoff"); } - RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); + //RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); return; } @@ -138,7 +139,7 @@ public static void SetHazards(bool turnOn) { ElsUiPanel.PlayUiSound("indon"); } - RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); + //RemoteEventManager.SendEvent(RemoteEventManager.Commands.ToggleInd, veh, true); } public static void ToggleHazards() @@ -157,7 +158,7 @@ public static void ToggleHazards() [Command("togglehazard")] public static void ToggleHazardsCommand() { - if (ELS.CurrentVehicle != null && Vehicle.Exists(ELS.CurrentVehicle) && !Game.IsPaused && Game.IsControlEnabled(0, ElsConfiguration.KBBindings.ToggleHAZ)) + if (ELS.CurrentVehicle != null && Vehicle.Exists(ELS.CurrentVehicle) && !Game.IsPaused && Controls.ToggleHAZ.IsEnabled()) { ToggleHazards(); } diff --git a/src/Light/Light.Loop.cs b/src/Light/Light.Loop.cs index 11e9887..9df7ca2 100644 --- a/src/Light/Light.Loop.cs +++ b/src/Light/Light.Loop.cs @@ -1,16 +1,17 @@ using CitizenFX.Core; using CitizenFX.Core.Native; using ELS.configuration; +using ELS.NUI; using Shared; using System; using System.Collections.Generic; using System.Linq; +using static ELS.configuration.ElsConfiguration; namespace ELS.Light { partial class Lights { - public async void ControlTicker() { if (!Vehicle.Exists(ElsVehicle.Vehicle)) @@ -21,17 +22,21 @@ public async void ControlTicker() return; } } + if (!ElsVehicle.ControlTimer.Expired) + { + return; + } //KB Controls - ToggleSecLKB(); - ToggleWrnLKB(); - ToggleBrdKB(); - ToggleCrsKB(); - ChgPrmPattKB(); - ChgWrnPattKB(); - ChgSecPattKB(); - ToggleTdlKB(); - ToggleSclKB(); - ToggleLightStageKB(); + ToggleSecondaryLightControlTick(); + ToggleWarningLightControlTick(); + ToggleBoardControlTick(); + ToggleCruiseLightControlTick(); + ChangePrimaryPatternControlTick(); + ChangeWarningPatternControlTick(); + ChangeSecondaryPatternControlTick(); + ToggleTakedownLightControlTick(); + ToggleSceneLightControlTick(); + ToggleLightStageControlTick(); } @@ -67,92 +72,111 @@ public async void Ticker() { Scene.RunTick(); } - //foreach (Extra.Extra prim in _extras.PRML.Values) - for (int i = 0; i < _extras.PrimaryLights.Count; i++) + + var shouldRunScanTicker = false; + + if (_extras.PrimaryLights != null) { - _extras.PrimaryLights.ElementAt(i).Value.ExtraTicker(); - if (_stage != null) + if (_stage != null && _scan) { switch (_stage.CurrentStage) { case 1: - if (!string.IsNullOrEmpty(_stage.PRML.PresetPatterns.Lstg1.Pattern) && _stage.PRML.PresetPatterns.Lstg1.Pattern.ToLower().Equals("scan") && _scan) + if (_stage.PRML.PresetPatterns.Lstg1.IsScanPattern) { - ScanPatternTicker(); + shouldRunScanTicker = true; } break; case 2: - if (!string.IsNullOrEmpty(_stage.PRML.PresetPatterns.Lstg1.Pattern) && _stage.PRML.PresetPatterns.Lstg2.Pattern.ToLower().Equals("scan") && _scan) + if (_stage.PRML.PresetPatterns.Lstg2.IsScanPattern) { - ScanPatternTicker(); + shouldRunScanTicker = true; } break; case 3: - if (!string.IsNullOrEmpty(_stage.PRML.PresetPatterns.Lstg1.Pattern) && _stage.PRML.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan") && _scan) + if (_stage.PRML.PresetPatterns.Lstg3.IsScanPattern) { - ScanPatternTicker(); + shouldRunScanTicker = true; } break; } } + + for (int i = 0; i < _extras.PrimaryLights.Count; i++) + { + _extras.PrimaryLights.ElementAt(i).Value.ExtraTicker(); + } } - //foreach (Extra.Extra sec in _extras.SECL.Values) - for (int i = 0; i < _extras.SecondaryLights.Count; i++) + + if (_extras.SecondaryLights != null) { - _extras.SecondaryLights.ElementAt(i).Value.ExtraTicker(); - if (_stage != null) + if (!shouldRunScanTicker && _stage != null && _scan) { switch (_stage.CurrentStage) { case 1: - if (!string.IsNullOrEmpty(_stage.SECL.PresetPatterns.Lstg1.Pattern) && _stage.SECL.PresetPatterns.Lstg1.Pattern.ToLower().Equals("scan") && _scan) + if (_stage.SECL.PresetPatterns.Lstg1.IsScanPattern) { - ScanPatternTicker(); + shouldRunScanTicker = true; } break; case 2: - if (!string.IsNullOrEmpty(_stage.SECL.PresetPatterns.Lstg1.Pattern) && _stage.SECL.PresetPatterns.Lstg2.Pattern.ToLower().Equals("scan") && _scan) + if (_stage.SECL.PresetPatterns.Lstg2.IsScanPattern) { - ScanPatternTicker(); + shouldRunScanTicker = true; } break; case 3: - if (!string.IsNullOrEmpty(_stage.SECL.PresetPatterns.Lstg1.Pattern) && _stage.SECL.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan") && _scan) + if (_stage.SECL.PresetPatterns.Lstg3.IsScanPattern) { - ScanPatternTicker(); + shouldRunScanTicker = true; } break; } } + + for (int i = 0; i < _extras.SecondaryLights.Count; i++) + { + _extras.SecondaryLights.ElementAt(i).Value.ExtraTicker(); + } } - //foreach (Extra.Extra wrn in _extras.WRNL.Values) - for (int i = 0; i < _extras.WarningLights.Count; i++) + + if (_extras.WarningLights != null) { - _extras.WarningLights.ElementAt(i).Value.ExtraTicker(); - if (_stage != null) + if (!shouldRunScanTicker && _stage != null && _scan) { switch (_stage.CurrentStage) { case 1: - if (!string.IsNullOrEmpty(_stage.WRNL.PresetPatterns.Lstg1.Pattern) && _stage.WRNL.PresetPatterns.Lstg1.Pattern.ToLower().Equals("scan") && _scan) + if (_stage.WRNL.PresetPatterns.Lstg1.IsScanPattern) { - ScanPatternTicker(); + shouldRunScanTicker = true; } break; case 2: - if (!string.IsNullOrEmpty(_stage.WRNL.PresetPatterns.Lstg1.Pattern) && _stage.WRNL.PresetPatterns.Lstg2.Pattern.ToLower().Equals("scan") && _scan) + if (_stage.WRNL.PresetPatterns.Lstg2.IsScanPattern) { - ScanPatternTicker(); + shouldRunScanTicker = true; } break; case 3: - if (!string.IsNullOrEmpty(_stage.WRNL.PresetPatterns.Lstg1.Pattern) && _stage.WRNL.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan") && _scan) + if (_stage.WRNL.PresetPatterns.Lstg3.IsScanPattern) { - ScanPatternTicker(); + shouldRunScanTicker = true; } break; } } + + for (int i = 0; i < _extras.WarningLights.Count; i++) + { + _extras.WarningLights.ElementAt(i).Value.ExtraTicker(); + } + } + + if (shouldRunScanTicker) + { + ScanPatternTicker(); } } @@ -161,39 +185,36 @@ internal void ScanPatternTicker() { if (ELS.GameTime - _patternStart > 15000) { + Utils.DeveloperWriteLine("Scan pattern ticker running"); _patternStart = ELS.GameTime; ToggleScanPattern(); } - else if (_patternStart == 0) - { - _patternStart = ELS.GameTime; - } } - internal void ToggleSecLKB() + internal void ToggleSecondaryLightControlTick() { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.ToggleSecL); - if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ToggleSecL) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + Controls.ToggleSecL.Disable(); + if (Controls.ToggleSecL.IsDisabledJustPressed()) { ToggleSecLights(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.SECL, GetSecondaryLightsData()); } } - internal void ToggleWrnLKB() + internal void ToggleWarningLightControlTick() { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.ToggleWrnL); - if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ToggleWrnL) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + Controls.ToggleWrnL.Disable(); + if (Controls.ToggleWrnL.IsDisabledJustPressed()) { ToggleWrnLights(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.WRNL, GetWarningLightsData()); } } - internal void ToggleBrdKB() + internal void ToggleBoardControlTick() { //Game.DisableControlThisFrame(0, Control.CharacterWheel); - if (Game.IsControlJustPressed(0, ElsConfiguration.KBBindings.ToggleBoard) && Game.IsControlPressed(0, Control.CharacterWheel) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + if (Controls.ToggleBoard.IsJustPressed() && Game.IsControlPressed(0, Control.CharacterWheel)) { if (_extras.Board.BoardRaised) { @@ -207,20 +228,28 @@ internal void ToggleBrdKB() } } - internal void ToggleCrsKB() + internal void ToggleCruiseLightControlTick() { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.ToggleCrsL); - if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ToggleCrsL) && !Game.IsControlPressed(0, Control.CharacterWheel) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + if (_extras.PrimaryLights == null) + { + return; + } + Controls.ToggleCrsL.Disable(); + if (Controls.ToggleCrsL.IsDisabledJustReleased() && !Game.IsControlPressed(0, Control.CharacterWheel) && ELS.CurrentInputMode == InputMode.MouseAndKeyboard) { ToggleCrs(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.PRML, GetPrimaryLightsData()); } } - internal void ToggleTdlKB() + internal void ToggleTakedownLightControlTick() { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.ToggleTdl); - if ((Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ToggleTdl) && !Game.IsControlPressed(0, Control.CharacterWheel) && !API.IsPauseMenuActive()) && Game.CurrentInputMode == InputMode.MouseAndKeyboard || (Global.AllowController && Game.IsControlJustPressed(2, ElsConfiguration.GPBindings.ToggleTdl) && Game.CurrentInputMode == InputMode.GamePad)) + Controls.ToggleTdl.Disable(); + if (ELS.CurrentInputMode == InputMode.MouseAndKeyboard && !Game.IsControlPressed(0, Control.CharacterWheel)) + { + return; + } + if (Controls.ToggleTdl.IsDisabledJustPressed()) { ToggleTdl(); var dic = new Dictionary(); @@ -237,70 +266,100 @@ internal void ToggleTdlKB() } } - internal void ToggleSclKB() + internal void ToggleSceneLightControlTick() { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.ToggleTdl); - if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ToggleTdl) && Game.IsControlPressed(0, Control.CharacterWheel) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + if (Controls.ToggleTdl.IsDisabledJustPressed() && Game.IsControlPressed(0, Control.CharacterWheel)) { ToggleScl(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.SceneLights, _extras.SceneLights.GetData()); } } - internal void ChgPrmPattKB() + internal void ChangePrimaryPatternControlTick() { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.ChgPattPrmL); - if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ChgPattPrmL) && !Game.IsControlPressed(0, Control.CharacterWheel) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + if (_extras.PrimaryLights == null) + { + return; + } + Controls.ChgPattPrmL.Disable(); + if (!Controls.ChgPattPrmL.IsDisabledJustPressed()) + { + return; + } + if (Game.IsControlPressed(0, Control.CharacterWheel)) { - ChgPrmPatt(false); + DecrementPrimaryPattern(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.PrimaryPattern, CurrentPrmPattern); } - else if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ChgPattPrmL) && Game.IsControlPressed(0, Control.CharacterWheel) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + else { - ChgPrmPatt(true); + IncrementPrimaryPattern(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.PrimaryPattern, CurrentPrmPattern); } + ElsUiPanel.PlayUiSound("sirenclickoff"); } - internal void ChgSecPattKB() + internal void ChangeSecondaryPatternControlTick() { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.ChgPattSecL); - if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ChgPattSecL) && !Game.IsControlPressed(0, Control.CharacterWheel) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + if (_extras.SecondaryLights == null) { - ChgSecPatt(false); + return; + } + Controls.ChgPattSecL.Disable(); + if (!Controls.ChgPattSecL.IsDisabledJustPressed()) + { + return; + } + if (!Game.IsControlPressed(0, Control.CharacterWheel)) + { + IncrementSecondaryPattern(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.SecondaryPattern, CurrentSecPattern); } - else if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ChgPattSecL) && Game.IsControlPressed(0, Control.CharacterWheel) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + else { - ChgSecPatt(true); + DecrementSeconaryPattern(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.SecondaryPattern, CurrentSecPattern); } + ElsUiPanel.PlayUiSound("sirenclickoff"); } - internal void ChgWrnPattKB() + internal void ChangeWarningPatternControlTick() { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.ChgPattWrnL); - if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ChgPattWrnL) && !Game.IsControlPressed(0, Control.CharacterWheel) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + if (_extras.WarningLights == null) + { + return; + } + Controls.ChgPattWrnL.Disable(); + if (!Controls.ChgPattWrnL.IsDisabledJustPressed()) + { + return; + } + if (!Game.IsControlPressed(0, Control.CharacterWheel)) { - ChgWrnPatt(false); + IncrementWarningPattern(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.WarningPattern, CurrentWrnPattern); } - else if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ChgPattWrnL) && Game.IsControlPressed(0, Control.CharacterWheel) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + else { - ChgWrnPatt(true); + DecrementWarningPattern(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.WarningPattern, CurrentWrnPattern); } + ElsUiPanel.PlayUiSound("sirenclickoff"); } - internal void ToggleLightStageKB() + internal void ToggleLightStageControlTick() { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.ToggleLstg); - if ((Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ToggleLstg) && !Game.IsControlPressed(0, Control.CharacterWheel)) && Game.CurrentInputMode == InputMode.MouseAndKeyboard || (Global.AllowController && Game.IsControlJustPressed(2, ElsConfiguration.GPBindings.ToggleLstg) && Game.CurrentInputMode == InputMode.GamePad)) + Controls.ToggleLstg.Disable(); + if (!Controls.ToggleLstg.IsDisabledJustPressed()) + { + return; + } + if (ELS.CurrentInputMode == InputMode.MouseAndKeyboard && !Game.IsControlPressed(0, Control.CharacterWheel) || (ELS.CurrentInputMode == InputMode.GamePad && Global.AllowController)) { ToggleLightStage(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.Stage, CurrentStage); } - else if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.ToggleLstg) && Game.IsControlPressed(0, Control.CharacterWheel) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) + else { ToggleLightStageInverse(); RemoteEventManager.SendLightEvent(ElsVehicle, DataNames.Stage, CurrentStage); diff --git a/src/Light/Lights.FullSync.cs b/src/Light/Lights.FullSync.cs index 90ed55a..9b11968 100644 --- a/src/Light/Lights.FullSync.cs +++ b/src/Light/Lights.FullSync.cs @@ -6,8 +6,52 @@ namespace ELS.Light { - partial class Lights : IFullSyncComponent + partial class Lights { + public Dictionary GetPrimaryLightsData() + { + if (_extras.PrimaryLights == null) + { + return null; + } + var prm = new Dictionary(); + foreach (Extra.Extra e in _extras.PrimaryLights.Values) + { + prm.Add(e.Id.ToString(), e.GetData()); + } + + return prm; + } + + public Dictionary GetSecondaryLightsData() + { + if (_extras.SecondaryLights == null) + { + return null; + } + var sec = new Dictionary(); + foreach (Extra.Extra e in _extras.SecondaryLights.Values) + { + sec.Add(e.Id.ToString(), e.GetData()); + } + + return sec; + } + + public Dictionary GetWarningLightsData() + { + if (_extras.WarningLights == null) + { + return null; + } + var wrn = new Dictionary(); + foreach (Extra.Extra e in _extras.WarningLights.Values) + { + wrn.Add(e.Id.ToString(), e.GetData()); + } + return wrn; + } + public Dictionary GetData() { @@ -41,14 +85,14 @@ public Dictionary GetData() dic.Add(DataNames.TakedownLights, _extras.TakedownLights.GetData()); } - if (_extras.Board.HasBoard) + if (_extras.Board?.HasBoard ?? false) { dic.Add(DataNames.Board, _extras.Board.GetData()); } - dic.Add(DataNames.PrimaryPattern, CurrentPrmPattern); - dic.Add(DataNames.SecondaryPattern, CurrentSecPattern); - dic.Add(DataNames.WarningPattern, CurrentWrnPattern); - dic.Add(DataNames.Stage, _stage.CurrentStage); + if (CurrentPrmPattern != 0) { dic.Add(DataNames.PrimaryPattern, CurrentPrmPattern); } + if (CurrentSecPattern != 0) { dic.Add(DataNames.SecondaryPattern, CurrentSecPattern); } + if (CurrentWrnPattern != 0) { dic.Add(DataNames.WarningPattern, CurrentWrnPattern); } + if (_stage.CurrentStage != 0) { dic.Add(DataNames.Stage, _stage.CurrentStage); } if (SpotLight != null) { dic.Add(DataNames.Spotlight, SpotLight.GetData()); @@ -60,62 +104,145 @@ public Dictionary GetData() return dic; } - public Dictionary GetPrimaryLightsData() + public void SetDataPartial(IDictionary data) { - var prm = new Dictionary(); - foreach (Extra.Extra e in _extras.PrimaryLights.Values) + if (data.TryGetValue(DataNames.PRML, out var prml) && _extras.PrimaryLights != null) { - prm.Add(e.Id, e.GetData()); + IDictionary prm = (IDictionary)prml; + foreach (Extra.Extra extra in _extras.PrimaryLights.Values) + { + extra.SetData((IDictionary)prm[$"{extra.Id}"]); + } } - - return prm; - } - - public Dictionary GetSecondaryLightsData() - { - var sec = new Dictionary(); - foreach (Extra.Extra e in _extras.SecondaryLights.Values) + if (data.TryGetValue(DataNames.SECL, out var secl) && _extras.SecondaryLights != null) { - sec.Add(e.Id, e.GetData()); + IDictionary sec = (IDictionary)secl; + foreach (Extra.Extra extra in _extras.SecondaryLights.Values) + { + extra.SetData((IDictionary)sec[$"{extra.Id}"]); + } } - - return sec; - } - - public Dictionary GetWarningLightsData() - { - var wrn = new Dictionary(); - foreach (Extra.Extra e in _extras.WarningLights.Values) + if (data.TryGetValue(DataNames.WRNL, out var wrnl) && _extras.WarningLights != null) { - wrn.Add(e.Id, e.GetData()); + IDictionary wrn = (IDictionary)wrnl; + foreach (Extra.Extra extra in _extras.WarningLights.Values) + { + extra.SetData((IDictionary)wrn[$"{extra.Id}"]); + } + } + if (data.TryGetValue(DataNames.SteadyBurn, out var sbrn)) + { + try + { + if (_extras.SteadyBurn == null) + { + CreateMissingExtra(10); + } + _extras.SteadyBurn.SetData((IDictionary)sbrn); + } + catch (Exception e) + { + Utils.DebugWriteLine($"SBRN error: {e}"); + } + } + if (data.TryGetValue(DataNames.SceneLights, out var scl)) + { + try + { + if (_extras.SceneLights == null) + { + CreateMissingExtra(12); + } + _extras.SceneLights.SetData((IDictionary)scl); + } + catch (Exception e) + { + Utils.DebugWriteLine($"SCL error: {e}"); + } + } + if (data.TryGetValue(DataNames.TakedownLights, out var tdl)) + { + try + { + if (_extras.TakedownLights == null) + { + CreateMissingExtra(11); + } + _extras.TakedownLights.SetData((IDictionary)tdl); + } + catch (Exception e) + { + Utils.DebugWriteLine($"TDL error: {e}"); + } + } + if (data.TryGetValue(DataNames.Board, out var brd)) + { + try + { + if (_extras.Board == null) + { + CreateBoard(); + } + _extras.Board.SetData((IDictionary)brd); + } + catch (Exception e) + { + Utils.DebugWriteLine($"BRD error: {e}"); + } + } + if (data.TryGetValue(DataNames.PrimaryPattern, out var prmPatt) && prmPatt != null) + { + CurrentPrmPattern = (int)prmPatt; + } + if (data.TryGetValue(DataNames.SecondaryPattern, out var secPatt) && secPatt != null) + { + CurrentSecPattern = (int)secPatt; + } + if (data.TryGetValue(DataNames.WarningPattern, out var wrnPatt) && wrnPatt != null) + { + CurrentWrnPattern = (int)wrnPatt; + } + if (data.TryGetValue(DataNames.Stage, out var stage) && stage != null) + { + _stage.SetStage((int)stage); + } + if (SpotLight != null && data.TryGetValue(DataNames.Spotlight, out var spotlight)) + { + SpotLight.SetData((IDictionary)spotlight); + } + if (Scene != null && data.TryGetValue(DataNames.Scene, out var sceneLight)) + { + Scene.SetData((IDictionary)sceneLight); } - return wrn; } public void SetData(IDictionary data) { - if (data.TryGetValue(DataNames.PRML, out var prml)) + if (data.TryGetValue(DataNames.PRML, out var prml) && _extras.PrimaryLights != null) { IDictionary prm = (IDictionary)prml; - foreach (Extra.Extra e in _extras.PrimaryLights.Values) + foreach (Extra.Extra extra in _extras.PrimaryLights.Values) { - e.SetData((IDictionary)prm[$"{e.Id}"]); + prm.TryGetValue(extra.Id.ToString(), out var extraData); + extra.SetData((IDictionary)extraData); } } - if (data.TryGetValue(DataNames.SECL, out var secl)) + if (data.TryGetValue(DataNames.SECL, out var secl) && _extras.SecondaryLights != null) { IDictionary sec = (IDictionary)secl; - foreach (Extra.Extra e in _extras.SecondaryLights.Values) + foreach (Extra.Extra extra in _extras.SecondaryLights.Values) { - e.SetData((IDictionary)sec[$"{e.Id}"]); + sec.TryGetValue(extra.Id.ToString(), out var extraData); + extra.SetData((IDictionary)extraData); } } - if (data.TryGetValue(DataNames.WRNL, out var wrnl)) + if (data.TryGetValue(DataNames.WRNL, out var wrnl) && _extras.WarningLights != null) { IDictionary wrn = (IDictionary)wrnl; - foreach (Extra.Extra e in _extras.WarningLights.Values) + foreach (Extra.Extra extra in _extras.WarningLights.Values) { - e.SetData((IDictionary)wrn[$"{e.Id}"]); + wrn.TryGetValue(extra.Id.ToString(), out var extraData); + extra.SetData((IDictionary)extraData); } } if (data.TryGetValue(DataNames.SteadyBurn, out var sbrn)) @@ -178,21 +305,37 @@ public void SetData(IDictionary data) Utils.DebugWriteLine($"BRD error: {e}"); } } - if (data.TryGetValue(DataNames.PrimaryPattern, out var prmPatt)) + if (data.TryGetValue(DataNames.PrimaryPattern, out var prmPatt) && prmPatt != null) { CurrentPrmPattern = (int)prmPatt; } - if (data.TryGetValue(DataNames.SecondaryPattern, out var secPatt)) + else + { + CurrentPrmPattern = 0; + } + if (data.TryGetValue(DataNames.SecondaryPattern, out var secPatt) && secPatt != null) + { + CurrentSecPattern = (int)secPatt; + } + else + { + CurrentSecPattern = 0; + } + if (data.TryGetValue(DataNames.WarningPattern, out var wrnPatt) && wrnPatt != null) + { + CurrentWrnPattern = (int)wrnPatt; + } + else { - CurrentSecPattern = int.Parse(secPatt.ToString()); + CurrentWrnPattern = 0; } - if (data.TryGetValue(DataNames.WarningPattern, out var wrnPatt)) + if (data.TryGetValue(DataNames.Stage, out var stage) && stage != null) { - CurrentWrnPattern = int.Parse(wrnPatt.ToString()); + _stage.SetStage((int)stage); } - if (data.TryGetValue(DataNames.Stage, out var stage)) + else { - _stage.SetStage(int.Parse(stage.ToString())); + _stage.SetStage(0); } if (SpotLight != null && data.TryGetValue(DataNames.Spotlight, out var spotlight)) { diff --git a/src/Light/Lights.Patterns.cs b/src/Light/Lights.Patterns.cs index 1a23d61..39f00db 100644 --- a/src/Light/Lights.Patterns.cs +++ b/src/Light/Lights.Patterns.cs @@ -10,9 +10,9 @@ namespace ELS.Light { partial class Lights : IPatterns { - private int _prmPatt = 0; - private int _secPatt = 0; - private int _wrnPatt = 0; + private int _prmPatt = -1; + private int _secPatt = -1; + private int _wrnPatt = -1; internal int _oldprm = 0; internal int _oldsec = 0; internal int _oldwrn = 0; @@ -32,53 +32,60 @@ public int CurrentPrmPattern } set { + if (_prmPatt == value) return; _oldprm = _prmPatt; _prmPatt = value; - foreach (Extra.Extra e in _extras.PrimaryLights.Values) + if (_extras.PrimaryLights == null) + { + Utils.DeveloperWriteLine($"Ignoring primary pattern update because no extras found"); + return; + } + Utils.DeveloperWriteLine($"Primary Pattern set to {_prmPatt}"); + foreach (var extra in _extras.PrimaryLights.Values) { try { switch (Vcfroot.PRML.LightingFormat.ToLower()) { case "leds": - e.Pattern = Leds.PrimaryPatterns[CurrentPrmPattern][e.Id]; + extra.Pattern = Leds.PrimaryPatterns[CurrentPrmPattern][extra.Id]; _prmPatterns = Leds.PrimaryPatterns.Count; _prefix = "L"; break; case "strb": - e.Pattern = Strobe.PrimaryPatterns[CurrentPrmPattern][e.Id]; + extra.Pattern = Strobe.PrimaryPatterns[CurrentPrmPattern][extra.Id]; _prmPatterns = Strobe.PrimaryPatterns.Count; _prefix = "S"; break; case "rota": - e.Pattern = Rotary.PrimaryPatterns[CurrentPrmPattern][e.Id]; + extra.Pattern = Rotary.PrimaryPatterns[CurrentPrmPattern][extra.Id]; _prmPatterns = Rotary.PrimaryPatterns.Count; _prefix = "R"; break; case "dro1": - if (e.Id == 1) + if (extra.Id == 1) { - e.SetState(true); + extra.SetState(true); } - e.Pattern = DRO.PrimaryPatternsDro1[CurrentPrmPattern][e.Id]; + extra.Pattern = DRO.PrimaryPatternsDro1[CurrentPrmPattern][extra.Id]; _prmPatterns = DRO.PrimaryPatternsDro1.Count; _prefix = "D"; break; case "dro2": - if (e.Id == 1) + if (extra.Id == 1) { - e.SetState(false); + extra.SetState(false); } - e.Pattern = DRO.PrimaryPatternsDro2[CurrentPrmPattern][e.Id]; + extra.Pattern = DRO.PrimaryPatternsDro2[CurrentPrmPattern][extra.Id]; _prmPatterns = DRO.PrimaryPatternsDro2.Count; _prefix = "D"; break; case "dro3": - if (e.Id == 1 || e.Id == 2) + if (extra.Id == 1 || extra.Id == 2) { - e.SetState(true); + extra.SetState(true); } - e.Pattern = DRO.PrimaryPatternsDro3[CurrentPrmPattern][e.Id]; + extra.Pattern = DRO.PrimaryPatternsDro3[CurrentPrmPattern][extra.Id]; _prmPatterns = DRO.PrimaryPatternsDro3.Count; _prefix = "D"; break; @@ -86,23 +93,23 @@ public int CurrentPrmPattern switch (_stage.CurrentStage) { case 1: - e.Pattern = CHP.LightStage1[e.Id]; + extra.Pattern = CHP.LightStage1[extra.Id]; _prmPatterns = 1; break; case 2: - e.Pattern = CHP.LightStage2[CurrentSecPattern][e.Id]; + extra.Pattern = CHP.LightStage2[CurrentSecPattern][extra.Id]; _prmPatterns = 1; _wrnPatterns = 1; _secPatterns = CHP.LightStage2.Count; break; case 3: - e.Pattern = CHP.LightStage3[CurrentWrnPattern][e.Id]; + extra.Pattern = CHP.LightStage3[CurrentWrnPattern][extra.Id]; _prmPatterns = 1; _secPatterns = 1; _wrnPatterns = CHP.LightStage3.Count; break; default: - e.Pattern = CHP.LightStage1[e.Id]; + extra.Pattern = CHP.LightStage1[extra.Id]; _prmPatterns = 1; _wrnPatterns = 1; _secPatterns = 1; @@ -111,24 +118,24 @@ public int CurrentPrmPattern _prefix = "C"; break; case "custom": - e.Pattern = Vcfroot.CustomPatterns.Values.ElementAt(CurrentPrmPattern).PatternData[e.Id]; - e.Delay = Vcfroot.CustomPatterns.Values.ElementAt(CurrentPrmPattern).PrmDelay; + extra.Pattern = Vcfroot.CustomPatterns.Values.ElementAt(CurrentPrmPattern).PatternData[extra.Id]; + extra.Delay = Vcfroot.CustomPatterns.Values.ElementAt(CurrentPrmPattern).PrmDelay; _prmPatterns = Vcfroot.CustomPatterns.Count; break; default: - e.Pattern = Leds.PrimaryPatterns[CurrentPrmPattern][e.Id]; + extra.Pattern = Leds.PrimaryPatterns[CurrentPrmPattern][extra.Id]; _prefix = "L"; break; } - e.PatternNum = CurrentPrmPattern; + extra.PatternNum = CurrentPrmPattern; } catch (Exception ex) { - Debug.WriteLine($"{e.Id} error {ex.Message}"); + Debug.WriteLine($"{extra.Id} error {ex.Message}"); } } - if (Game.PlayerPed.IsInPoliceVehicle && Game.PlayerPed.CurrentVehicle.NetworkId == ElsVehicle.Vehicle?.NetworkId) + if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) { if (Vcfroot.PRML.LightingFormat.Equals("custom")) { @@ -151,8 +158,15 @@ public int CurrentSecPattern } set { - _oldsec = _oldprm; + if (_secPatt == value) return; + _oldsec = _secPatt; _secPatt = value; + if (_extras.SecondaryLights == null) + { + Utils.DeveloperWriteLine($"Ignoring seconary pattern update because no extras found"); + return; + } + Utils.DeveloperWriteLine($"Seconary Pattern set to {_secPatt}"); foreach (Extra.Extra e in _extras.SecondaryLights.Values) { try @@ -219,7 +233,7 @@ public int CurrentSecPattern Debug.WriteLine($"{e.Id} error {ex.Message}"); } } - if (Game.PlayerPed.IsInPoliceVehicle && Game.PlayerPed.CurrentVehicle.NetworkId == ElsVehicle.Vehicle?.NetworkId) + if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) { if (Vcfroot.SECL.LightingFormat.Equals("custom")) { @@ -242,8 +256,15 @@ public int CurrentWrnPattern } set { + if (_wrnPatt == value) return; _oldwrn = _wrnPatt; _wrnPatt = value; + if (_extras.WarningLights == null) + { + Utils.DeveloperWriteLine($"Ignoring warning pattern update because no extras found"); + return; + } + Utils.DeveloperWriteLine($"Warning Pattern set to {_wrnPatt}"); foreach (Extra.Extra e in _extras.WarningLights.Values) { try @@ -295,7 +316,7 @@ public int CurrentWrnPattern Debug.WriteLine($"{e.Id} error {ex.Message}"); } } - if (Game.PlayerPed.IsInPoliceVehicle && Game.PlayerPed.CurrentVehicle.NetworkId == ElsVehicle.Vehicle?.NetworkId) + if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) { if (Vcfroot.SECL.LightingFormat.Equals("custom")) { @@ -310,14 +331,45 @@ public int CurrentWrnPattern } } + public void RefreshExtras() + { + if (_prmPatt < 0) + { + CurrentPrmPattern = 0; + } else + { + CurrentPrmPattern = _prmPatt; + } + if (_secPatt < 0) + { + CurrentSecPattern = 0; + } else + { + CurrentSecPattern = _secPatt; + } + if (_wrnPatt < 0) + { + CurrentWrnPattern = 0; + } else + { + CurrentWrnPattern = _wrnPatt; + } + } + public int CurrentStage { get { return _stage.CurrentStage; } } - private void SetupPatternsPrm() + private void SetupPatterns() + { + SetupPrimaryPatterns(); + SetupSecondaryPatterns(); + SetupWarningPatterns(); + } + + private void SetupPrimaryPatterns() { - CurrentPrmPattern = 0; - PrmScanPatts = new List(); if (_stage.PRML.ScanPatternCustomPool.Enabled) { + PrmScanPatts = new List(); foreach (int p in _stage.PRML.ScanPatternCustomPool.Pattern) { PrmScanPatts.Add(p); @@ -326,26 +378,27 @@ private void SetupPatternsPrm() } } - private void SetupSecPatterns() + private void SetupSecondaryPatterns() { - CurrentSecPattern = 0; - SecScanPatts = new List(); if (_stage.SECL.ScanPatternCustomPool.Enabled) { - foreach (int p in _stage.SECL.ScanPatternCustomPool.Pattern) + SecScanPatts = new List(); + if (_stage.SECL.ScanPatternCustomPool.Enabled) { - SecScanPatts.Add(p); + foreach (int p in _stage.SECL.ScanPatternCustomPool.Pattern) + { + SecScanPatts.Add(p); + } + CurrentSecPattern = SecScanPatts[0]; } - CurrentSecPattern = SecScanPatts[0]; } } - private void SetupWrnPatterns() + private void SetupWarningPatterns() { - CurrentWrnPattern = 0; - WrnScanPatts = new List(); if (_stage.WRNL.ScanPatternCustomPool.Enabled) { + WrnScanPatts = new List(); foreach (int p in _stage.WRNL.ScanPatternCustomPool.Pattern) { WrnScanPatts.Add(p); diff --git a/src/Light/Lights.cs b/src/Light/Lights.cs index 6e7d789..ff8b237 100644 --- a/src/Light/Lights.cs +++ b/src/Light/Lights.cs @@ -31,8 +31,9 @@ enum ExtraEnum WRNL, SECL } - internal struct Extras + internal class Extras { + internal ELSVehicle _elsVehicle = null; public Dictionary PrimaryLights; internal Dictionary WarningLights; internal Dictionary SecondaryLights; @@ -47,9 +48,6 @@ partial class Lights : IManagerEntry, ILight { private Extras _extras = new Extras { - PrimaryLights = new Dictionary(), - WarningLights = new Dictionary(), - SecondaryLights = new Dictionary(), }; public Vcfroot Vcfroot { get; set; } @@ -96,9 +94,8 @@ private void Init() return; } AddAllValidLightExtras(); - SetupPatternsPrm(); - SetupSecPatterns(); - SetupWrnPatterns(); + SetupPatterns(); + RefreshExtras(); _isInitialized = true; } @@ -113,13 +110,40 @@ internal void SyncUi() { ElsUiPanel.SetEuro(false); } - ElsUiPanel.SetUiDesc(_prefix + CurrentPrmPattern.ToString().PadLeft(3, '0'), ExtraEnum.PRML.ToString()); - ElsUiPanel.SetUiDesc(_prefix + CurrentSecPattern.ToString().PadLeft(3, '0'), ExtraEnum.SECL.ToString()); - ElsUiPanel.SetUiDesc(_prefix + CurrentWrnPattern.ToString().PadLeft(3, '0'), ExtraEnum.WRNL.ToString()); - ElsUiPanel.ToggleUiBtnState(prmLights, "PRML"); - ElsUiPanel.ToggleUiBtnState(secLights, "SECL"); - ElsUiPanel.ToggleUiBtnState(wrnLights, "WRNL"); - ElsUiPanel.ToggleUiBtnState(crsLights, "CRS"); + if (_extras.PrimaryLights == null) + { + ElsUiPanel.SetUiDesc("--", ExtraEnum.PRML.ToString()); + ElsUiPanel.ToggleUiBtnState(false, "PRML"); + ElsUiPanel.ToggleUiBtnState(false, "CRS"); + } + else + { + ElsUiPanel.SetUiDesc(_prefix + CurrentPrmPattern.ToString().PadLeft(3, '0'), ExtraEnum.PRML.ToString()); + ElsUiPanel.ToggleUiBtnState(prmLights, "PRML"); + } + + if (_extras.SecondaryLights == null) + { + ElsUiPanel.SetUiDesc("--", ExtraEnum.SECL.ToString()); + ElsUiPanel.ToggleUiBtnState(false, "SECL"); + } + else + { + ElsUiPanel.SetUiDesc(_prefix + CurrentSecPattern.ToString().PadLeft(3, '0'), ExtraEnum.SECL.ToString()); + ElsUiPanel.ToggleUiBtnState(secLights, "SECL"); + } + + if (_extras.WarningLights == null) + { + ElsUiPanel.SetUiDesc("--", ExtraEnum.WRNL.ToString()); + ElsUiPanel.ToggleUiBtnState(false, "WRNL"); + } + else + { + ElsUiPanel.SetUiDesc(_prefix + CurrentSecPattern.ToString().PadLeft(3, '0'), ExtraEnum.WRNL.ToString()); + ElsUiPanel.ToggleUiBtnState(wrnLights, "WRNL"); + } + if (Scene != null) { ElsUiPanel.ToggleUiBtnState(Scene.TurnedOn, "SCL"); @@ -130,7 +154,6 @@ internal void SyncUi() } } - [Conditional("SIREN")] internal void SetGTASirens(bool state) { ElsVehicle.IsSirenActive = state; var vehicle = ElsVehicle.Vehicle; @@ -141,15 +164,11 @@ internal void SetGTASirens(bool state) { var distance = Game.Player.Character.Position.DistanceToSquared(ElsVehicle.Vehicle.Position); if (distance < (ELSVehicle.deactivateDistance * ELSVehicle.deactivateDistance) || state == false) { -#if SIREN - _vehicle.IsSirenActive = state; -#endif + vehicle.IsSirenActive = state; } else { -#if SIREN - _vehicle.IsSirenActive = false; -#endif + vehicle.IsSirenActive = false; } } @@ -161,6 +180,10 @@ private void CreateMissingExtra(int x) { if (Vcfroot.EOVERRIDE.Extra01.IsElsControlled) { + if (_extras.PrimaryLights == null) + { + _extras.PrimaryLights = new Dictionary(); + } _extras.PrimaryLights.Add(1, new Extra.Extra(this, 1, Vcfroot.EOVERRIDE.Extra01, Vcfroot.PRML.LightingFormat)); } } @@ -169,6 +192,10 @@ private void CreateMissingExtra(int x) { if (Vcfroot.EOVERRIDE.Extra02.IsElsControlled) { + if (_extras.PrimaryLights == null) + { + _extras.PrimaryLights = new Dictionary(); + } _extras.PrimaryLights.Add(2, new Extra.Extra(this, 2, Vcfroot.EOVERRIDE.Extra02, Vcfroot.PRML.LightingFormat)); } } @@ -177,6 +204,10 @@ private void CreateMissingExtra(int x) { if (Vcfroot.EOVERRIDE.Extra03.IsElsControlled) { + if (_extras.PrimaryLights == null) + { + _extras.PrimaryLights = new Dictionary(); + } _extras.PrimaryLights.Add(3, new Extra.Extra(this, 3, Vcfroot.EOVERRIDE.Extra03, Vcfroot.PRML.LightingFormat)); } } @@ -185,6 +216,10 @@ private void CreateMissingExtra(int x) { if (Vcfroot.EOVERRIDE.Extra04.IsElsControlled) { + if (_extras.PrimaryLights == null) + { + _extras.PrimaryLights = new Dictionary(); + } _extras.PrimaryLights.Add(4, new Extra.Extra(this, 4, Vcfroot.EOVERRIDE.Extra04, Vcfroot.PRML.LightingFormat)); } } @@ -193,6 +228,10 @@ private void CreateMissingExtra(int x) { if (Vcfroot.EOVERRIDE.Extra05.IsElsControlled) { + if (_extras.WarningLights == null) + { + _extras.WarningLights = new Dictionary(); + } _extras.WarningLights.Add(5, new Extra.Extra(this, 5, Vcfroot.EOVERRIDE.Extra05, Vcfroot.WRNL.LightingFormat)); } } @@ -201,6 +240,10 @@ private void CreateMissingExtra(int x) { if (Vcfroot.EOVERRIDE.Extra06.IsElsControlled) { + if (_extras.WarningLights == null) + { + _extras.WarningLights = new Dictionary(); + } _extras.WarningLights.Add(6, new Extra.Extra(this, 6, Vcfroot.EOVERRIDE.Extra06, Vcfroot.WRNL.LightingFormat)); } } @@ -209,6 +252,10 @@ private void CreateMissingExtra(int x) { if (Vcfroot.EOVERRIDE.Extra07.IsElsControlled) { + if (_extras.SecondaryLights == null) + { + _extras.SecondaryLights = new Dictionary(); + } _extras.SecondaryLights.Add(7, new Extra.Extra(this, 7, Vcfroot.EOVERRIDE.Extra07, Vcfroot.SECL.LightingFormat)); } } @@ -217,6 +264,10 @@ private void CreateMissingExtra(int x) { if (Vcfroot.EOVERRIDE.Extra08.IsElsControlled) { + if (_extras.SecondaryLights == null) + { + _extras.SecondaryLights = new Dictionary(); + } _extras.SecondaryLights.Add(8, new Extra.Extra(this, 8, Vcfroot.EOVERRIDE.Extra08, Vcfroot.SECL.LightingFormat)); } } @@ -225,6 +276,10 @@ private void CreateMissingExtra(int x) { if (Vcfroot.EOVERRIDE.Extra09.IsElsControlled) { + if (_extras.SecondaryLights == null) + { + _extras.SecondaryLights = new Dictionary(); + } _extras.SecondaryLights.Add(9, new Extra.Extra(this, 9, Vcfroot.EOVERRIDE.Extra09, Vcfroot.SECL.LightingFormat)); } } @@ -293,10 +348,10 @@ private void AddAllValidLightExtras() { for (int x = 1; x < 13; x++) { -// if (!API.DoesExtraExist(Vehicle.Handle, 1)) -// { -// continue; -// } + if (!API.DoesExtraExist(ElsVehicle.Vehicle.Handle, x)) + { + continue; + } CreateMissingExtra(x); } if (!string.IsNullOrEmpty(Vcfroot.MISC.ArrowboardType)) @@ -307,17 +362,17 @@ private void AddAllValidLightExtras() { _extras.Ladder = new Gadgets.Ladder(this, Vcfroot.MISC); } - if (_extras.PrimaryLights.Count == 0) + if (_extras.PrimaryLights == null || _extras.PrimaryLights.Count == 0) { _prmPatterns = 0; ElsUiPanel.SetUiDesc("--", ExtraEnum.PRML.ToString()); } - if (_extras.SecondaryLights.Count == 0) + if (_extras.SecondaryLights == null || _extras.SecondaryLights.Count == 0) { _secPatterns = 0; ElsUiPanel.SetUiDesc("--", ExtraEnum.SECL.ToString()); } - if (_extras.WarningLights.Count == 0) + if (_extras.WarningLights == null || _extras.WarningLights.Count == 0) { _wrnPatterns = 0; ElsUiPanel.SetUiDesc("--", ExtraEnum.WRNL.ToString()); @@ -329,17 +384,26 @@ private void AddAllValidLightExtras() public void CleanUP(bool tooFarAwayCleanup = false) { - foreach(Extra.Extra e in _extras.PrimaryLights.Values) + if (_extras.PrimaryLights != null) { - e.CleanUp(); + foreach (Extra.Extra e in _extras.PrimaryLights.Values) + { + e.CleanUp(); + } } - foreach (Extra.Extra e in _extras.SecondaryLights.Values) - { - e.CleanUp(); + if (_extras.SecondaryLights != null) + { + foreach (Extra.Extra e in _extras.SecondaryLights.Values) + { + e.CleanUp(); + } } - foreach (Extra.Extra e in _extras.WarningLights.Values) + if (_extras.WarningLights != null) { - e.CleanUp(); + foreach (Extra.Extra e in _extras.WarningLights.Values) + { + e.CleanUp(); + } } if (_extras.SteadyBurn != null) { diff --git a/src/Light/Lights.logic.cs b/src/Light/Lights.logic.cs index 55a6e5c..c5c7126 100644 --- a/src/Light/Lights.logic.cs +++ b/src/Light/Lights.logic.cs @@ -55,6 +55,10 @@ internal void ToggleWrnLights() bool crsLights = false; internal async void ToggleCrs() { + if (_extras.PrimaryLights == null) + { + return; + } if (Vcfroot.CRUISE.DisableAtLstg3 && _stage.CurrentStage == 3) { foreach (Extra.Extra e in _extras.PrimaryLights.Values) @@ -141,87 +145,76 @@ internal void ToggleScl() ElsUiPanel.PlayUiSound("sirenclickoff"); } - internal async void ChgPrmPatt(bool decrement) + internal void DecrementPrimaryPattern() { - if (decrement) + if (CurrentPrmPattern == 0) { - if (CurrentPrmPattern == 0) - { - CurrentPrmPattern = _prmPatterns - 1; - } - else - { - CurrentPrmPattern--; - } - ElsUiPanel.PlayUiSound("sirenclickoff"); + CurrentPrmPattern = _prmPatterns - 1; } else { - if (CurrentPrmPattern == _prmPatterns - 1) - { - CurrentPrmPattern = 0; - } - else - { - CurrentPrmPattern++; - } - ElsUiPanel.PlayUiSound("sirenclickoff"); + CurrentPrmPattern--; } + ElsUiPanel.PlayUiSound("sirenclickoff"); } - internal async void ChgSecPatt(bool decrement) + internal void IncrementPrimaryPattern() { - if (decrement) + if (CurrentPrmPattern == _prmPatterns - 1) { - if (CurrentSecPattern == 0) - { - CurrentSecPattern = _secPatterns - 1; - } - else - { - CurrentSecPattern--; - } - ElsUiPanel.PlayUiSound("sirenclickoff"); + CurrentPrmPattern = 0; } else { - if (CurrentSecPattern == _secPatterns - 1) - { - CurrentSecPattern = 0; - } - else - { - CurrentSecPattern++; - } - ElsUiPanel.PlayUiSound("sirenclickoff"); + CurrentPrmPattern++; } } - internal async void ChgWrnPatt(bool decrement) + internal void DecrementSeconaryPattern() { - if (decrement) + if (CurrentSecPattern == 0) { - if (CurrentWrnPattern == 0) - { - CurrentWrnPattern = _wrnPatterns - 1; - } - else - { - CurrentWrnPattern--; - } - ElsUiPanel.PlayUiSound("sirenclickoff"); + CurrentSecPattern = _secPatterns - 1; } else { - if (CurrentWrnPattern == _wrnPatterns - 1) - { - CurrentWrnPattern = 0; - } - else - { - CurrentWrnPattern++; - } - ElsUiPanel.PlayUiSound("sirenclickoff"); + CurrentSecPattern--; + } + } + + internal void IncrementSecondaryPattern() + { + if (CurrentSecPattern == _secPatterns - 1) + { + CurrentSecPattern = 0; + } + else + { + CurrentSecPattern++; + } + } + + internal void DecrementWarningPattern() + { + if (CurrentWrnPattern == 0) + { + CurrentWrnPattern = _wrnPatterns - 1; + } + else + { + CurrentWrnPattern--; + } + } + + internal void IncrementWarningPattern() + { + if (CurrentWrnPattern == _wrnPatterns - 1) + { + CurrentWrnPattern = 0; + } + else + { + CurrentWrnPattern++; } } @@ -255,7 +248,7 @@ internal async Task ToggleScanPattern() } else { - if (!String.IsNullOrEmpty(_stage.SECL.PresetPatterns.Lstg1.Pattern) && _stage.SECL.PresetPatterns.Lstg1.Pattern.ToLower().Equals("scan")) + if (_stage.SECL.PresetPatterns.Lstg1.IsScanPattern) { if (CurrentSecPattern == _secPatterns - 1) { @@ -290,7 +283,7 @@ internal async Task ToggleScanPattern() } else { - if (!String.IsNullOrEmpty(_stage.SECL.PresetPatterns.Lstg1.Pattern) && _stage.SECL.PresetPatterns.Lstg1.Pattern.ToLower().Equals("scan")) + if (_stage.SECL.PresetPatterns.Lstg1.IsScanPattern) { if (CurrentSecPattern == _secPatterns - 1) { @@ -324,7 +317,7 @@ internal async Task ToggleScanPattern() } else { - if (!String.IsNullOrEmpty(_stage.PRML.PresetPatterns.Lstg2.Pattern) && _stage.PRML.PresetPatterns.Lstg2.Pattern.ToLower().Equals("scan")) + if (_stage.PRML.PresetPatterns.Lstg2.IsScanPattern) { if (CurrentPrmPattern == _prmPatterns - 1) { @@ -360,7 +353,7 @@ internal async Task ToggleScanPattern() } else { - if (!String.IsNullOrEmpty(_stage.WRNL.PresetPatterns.Lstg3.Pattern) && _stage.WRNL.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan")) + if (_stage.WRNL.PresetPatterns.Lstg3.IsScanPattern) { if (CurrentWrnPattern == _wrnPatterns - 1) { @@ -394,7 +387,7 @@ internal async Task ToggleScanPattern() } else { - if (!String.IsNullOrEmpty(_stage.SECL.PresetPatterns.Lstg3.Pattern) && _stage.SECL.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan")) + if (_stage.SECL.PresetPatterns.Lstg3.IsScanPattern) { if (CurrentSecPattern == _secPatterns - 1) { @@ -428,7 +421,7 @@ internal async Task ToggleScanPattern() } else { - if (!String.IsNullOrEmpty(_stage.PRML.PresetPatterns.Lstg3.Pattern) && _stage.PRML.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan")) + if (_stage.PRML.PresetPatterns.Lstg3.IsScanPattern) { if (CurrentPrmPattern == _prmPatterns - 1) { @@ -467,20 +460,29 @@ internal void SetStage() { ElsUiPanel.ToggleUiBtnState(false, "WW"); } - //foreach (Extra.Extra e in _extras.PRML.Values) - for (int i = 0; i < _extras.PrimaryLights.Count; i++) + + if (_extras.PrimaryLights != null) { - _extras.PrimaryLights.ElementAt(i).Value.IsPatternRunning = false; + foreach (var extra in _extras.PrimaryLights) + { + extra.Value.IsPatternRunning = false; + } } - //foreach (Extra.Extra e in _extras.SECL.Values) - for (int i = 0; i < _extras.SecondaryLights.Count; i++) + + if (_extras.SecondaryLights != null) { - _extras.SecondaryLights.ElementAt(i).Value.IsPatternRunning = false; + foreach (var extra in _extras.SecondaryLights) + { + extra.Value.IsPatternRunning = false; + } } - //foreach (Extra.Extra e in _extras.WRNL.Values) - for (int i = 0; i < _extras.WarningLights.Count; i++) + + if (_extras.WarningLights != null) { - _extras.WarningLights.ElementAt(i).Value.IsPatternRunning = false; + foreach(var extra in _extras.WarningLights) + { + extra.Value.IsPatternRunning = false; + } } if (Vcfroot.SECL.LightingFormat.ToLower().Equals("chp")) { @@ -511,24 +513,23 @@ internal void SetStage() } } //foreach (Extra.Extra e in _extras.SECL.Values) - for (int i = 0; i < _extras.SecondaryLights.Count; i++) + if (_stage.SECL.PresetPatterns.Lstg1.Enabled) { - if (_stage.SECL.PresetPatterns.Lstg1.Enabled) + if (_stage.SECL.PresetPatterns.Lstg1.IsScanPattern) { - if (_stage.SECL.PresetPatterns.Lstg1.Pattern.ToLower().Equals("scan")) - { - _scan = true; - } - else - { - CurrentSecPattern = _stage.SECL.PresetPatterns.Lstg1.IntPattern; - } + _scan = true; } else { - + CurrentSecPattern = _stage.SECL.PresetPatterns.Lstg1.IntPattern; + } + } + if (_extras.SecondaryLights != null) + { + foreach(var extra in _extras.SecondaryLights) + { + extra.Value.IsPatternRunning = true; } - _extras.SecondaryLights.ElementAt(i).Value.IsPatternRunning = true; } if (Vcfroot.SECL.LightingFormat.ToLower().Equals("chp")) { @@ -537,16 +538,31 @@ internal void SetStage() //_extras.SBRN.IsPatternRunning = true; _extras.SteadyBurn.SetState(true); } - //foreach (Extra.Extra e in _extras.PRML.Values) - for (int i = 0; i < _extras.PrimaryLights.Count; i++) + if (_extras.PrimaryLights != null) { - _extras.PrimaryLights.ElementAt(i).Value.IsPatternRunning = false; + foreach (var extra in _extras.PrimaryLights) + { + extra.Value.IsPatternRunning = false; + } } - //foreach (Extra.Extra e in _extras.WRNL.Values) - for (int i = 0; i < _extras.WarningLights.Count; i++) + + if (_extras.WarningLights != null) { - _extras.WarningLights.ElementAt(i).Value.IsPatternRunning = false; + foreach (var extra in _extras.WarningLights) + { + extra.Value.IsPatternRunning = false; + } } + + if (_extras.WarningLights != null) + { + foreach(var extra in _extras.WarningLights) + { + extra.Value.IsPatternRunning = false; + } + } + + if (Vcfroot.MISC.UseSteadyBurnLights && _extras.SteadyBurn != null) { _extras.SteadyBurn.SetState(true); @@ -571,63 +587,64 @@ internal void SetStage() ElsUiPanel.ToggleUiBtnState(true, "WW"); } } - //foreach (Extra.Extra e in _extras.PRML.Values) - for (int i = 0; i < _extras.PrimaryLights.Count; i++) + if (_extras.PrimaryLights != null) { - _extras.PrimaryLights.ElementAt(i).Value.IsPatternRunning = false; + foreach(var extra in _extras.PrimaryLights) + { + extra.Value.IsPatternRunning = false; + } } - //foreach (Extra.Extra e in _extras.WRNL.Values) - for (int i = 0; i < _extras.WarningLights.Count; i++) + + if (_extras.WarningLights != null) { - _extras.WarningLights.ElementAt(i).Value.IsPatternRunning = false; + foreach(var extra in _extras.WarningLights) + { + extra.Value.IsPatternRunning = false; + } } - //foreach (Extra.Extra e in _extras.SECL.Values) - for (int i = 0; i < _extras.SecondaryLights.Count; i++) + + if (_stage.SECL.PresetPatterns.Lstg2.Enabled) { - if (_stage.SECL.PresetPatterns.Lstg2.Enabled) + if (_stage.SECL.PresetPatterns.Lstg2.IsScanPattern) { - if (_stage.SECL.PresetPatterns.Lstg2.Pattern.ToLower().Equals("scan")) - { - _scan = true; - } - else - { - CurrentSecPattern = _stage.SECL.PresetPatterns.Lstg2.IntPattern; - } + _scan = true; } else { + CurrentSecPattern = _stage.SECL.PresetPatterns.Lstg2.IntPattern; } - _extras.SecondaryLights.ElementAt(i).Value.IsPatternRunning = false; - _extras.SecondaryLights.ElementAt(i).Value.IsPatternRunning = true; } - secLights = true; - //foreach (int i in extras) - for(int i = 0; i < extras.Length; i++) + + if (_extras.SecondaryLights != null) { - if (_extras.PrimaryLights.ContainsKey(extras[i])) + foreach(var extra in _extras.SecondaryLights) { - if (_stage.PRML.PresetPatterns.Lstg2.Enabled) - { - if (_stage.PRML.PresetPatterns.Lstg2.Pattern.ToLower().Equals("scan")) - { - _scan = true; - } - else - { - CurrentPrmPattern = _stage.PRML.PresetPatterns.Lstg2.IntPattern; - } - } - else - { + extra.Value.IsPatternRunning = true; + } + secLights = true; + } + - } - //_extras.PRML.ElementAt(i).Value.IsPatternRunning = false; - //_extras.PRML.ElementAt(i).Value.IsPatternRunning = true; - _extras.PrimaryLights[extras[i]].IsPatternRunning = false; - _extras.PrimaryLights[extras[i]].IsPatternRunning = true; + if (_stage.PRML.PresetPatterns.Lstg2.Enabled) + { + if (_stage.PRML.PresetPatterns.Lstg2.IsScanPattern) + { + _scan = true; + } + else + { + CurrentPrmPattern = _stage.PRML.PresetPatterns.Lstg2.IntPattern; } } + + if (_extras.PrimaryLights != null && _extras.PrimaryLights.Count > 0) + { + foreach(var extra in _extras.PrimaryLights) + { + extra.Value.IsPatternRunning = true; + } + } + if (Vcfroot.PRML.LightingFormat.ToLower().Equals("chp")) { SetCHP(); @@ -659,109 +676,140 @@ internal void SetStage() ElsUiPanel.ToggleUiBtnState(true, "WW"); } } - //foreach (Extra.Extra e in _extras.SECL.Values) - for (int i = 0; i < _extras.SecondaryLights.Count; i++) + + if (_stage.SECL.PresetPatterns.Lstg3.Enabled) { + if (_stage.SECL.PresetPatterns.Lstg3.IsScanPattern) + { + _scan = true; + } + else + { + CurrentSecPattern = _stage.SECL.PresetPatterns.Lstg3.IntPattern; + } + } - if (_stage.SECL.PresetPatterns.Lstg3.Enabled) + if (Vcfroot.SECL.DisableAtLstg3) + { + if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) { - if (_stage.SECL.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan")) + ElsUiPanel.ToggleUiBtnState(secLights, "SECL"); + } + secLights = false; + } + else + { + if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) + { + ElsUiPanel.ToggleUiBtnState(secLights, "SECL"); + } + secLights = true; + } + + if (_extras.SecondaryLights != null) + { + foreach(var extra in _extras.SecondaryLights) + { + if (Vcfroot.SECL.DisableAtLstg3) { - _scan = true; + extra.Value.IsPatternRunning = false; } else { - CurrentSecPattern = _stage.SECL.PresetPatterns.Lstg3.IntPattern; + extra.Value.IsPatternRunning = true; } } - if (Vcfroot.SECL.DisableAtLstg3) + } + + if (_stage.PRML.PresetPatterns.Lstg3.Enabled) + { + if (_stage.PRML.PresetPatterns.Lstg3.IsScanPattern) { - _extras.SecondaryLights.ElementAt(i).Value.IsPatternRunning = false; - secLights = false; - if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) - { - ElsUiPanel.ToggleUiBtnState(secLights, "SECL"); - } + _scan = true; } else { - _extras.SecondaryLights.ElementAt(i).Value.IsPatternRunning = false; - _extras.SecondaryLights.ElementAt(i).Value.IsPatternRunning = true; - secLights = true; - if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) - { - ElsUiPanel.ToggleUiBtnState(secLights, "SECL"); - } + CurrentPrmPattern = _stage.PRML.PresetPatterns.Lstg3.IntPattern; + } + } + + if (Vcfroot.PRML.DisableAtLstg3) + { + prmLights = false; + if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) + { + ElsUiPanel.ToggleUiBtnState(prmLights, "PRML"); } } - //foreach (Extra.Extra e in _extras.PRML.Values) - for (int i = 0; i < _extras.PrimaryLights.Count; i++) + else { - if (_stage.PRML.PresetPatterns.Lstg3.Enabled) + prmLights = true; + if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) { - if (_stage.PRML.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan")) + ElsUiPanel.ToggleUiBtnState(prmLights, "PRML"); + } + } + + if (_extras.PrimaryLights != null) + { + foreach(var extra in _extras.PrimaryLights) + { + if (Vcfroot.PRML.DisableAtLstg3) { - _scan = true; + extra.Value.IsPatternRunning = false; } else { - CurrentPrmPattern = _stage.PRML.PresetPatterns.Lstg3.IntPattern; + extra.Value.IsPatternRunning = true; } } - if (Vcfroot.PRML.DisableAtLstg3) + } + + + if (_stage.WRNL.PresetPatterns.Lstg3.Enabled) + { + if (_stage.WRNL.PresetPatterns.Lstg3.IsScanPattern) { - _extras.PrimaryLights.ElementAt(i).Value.IsPatternRunning = false; - prmLights = false; - if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) - { - ElsUiPanel.ToggleUiBtnState(prmLights, "PRML"); - } + _scan = true; } else { - _extras.PrimaryLights.ElementAt(i).Value.IsPatternRunning = false; - _extras.PrimaryLights.ElementAt(i).Value.IsPatternRunning = true; - prmLights = true; - if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) - { - ElsUiPanel.ToggleUiBtnState(prmLights, "PRML"); - } + CurrentWrnPattern = _stage.WRNL.PresetPatterns.Lstg3.IntPattern; } } - //foreach (Extra.Extra e in _extras.WRNL.Values) - for (int i = 0; i < _extras.WarningLights.Count; i++) + + if (Vcfroot.WRNL.DisableAtLstg3) { - if (_stage.WRNL.PresetPatterns.Lstg3.Enabled) + wrnLights = false; + if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) { - if (_stage.WRNL.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan")) - { - _scan = true; - } - else - { - CurrentWrnPattern = _stage.WRNL.PresetPatterns.Lstg3.IntPattern; - } + ElsUiPanel.ToggleUiBtnState(wrnLights, "WRNL"); } - if (Vcfroot.WRNL.DisableAtLstg3) + } + else + { + wrnLights = true; + if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) { - _extras.WarningLights.ElementAt(i).Value.IsPatternRunning = false; - wrnLights = false; - if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) - { - ElsUiPanel.ToggleUiBtnState(wrnLights, "WRNL"); - } + ElsUiPanel.ToggleUiBtnState(wrnLights, "WRNL"); } - else + } + + if (_extras.WarningLights != null) + { + foreach(var extra in _extras.WarningLights) { - _extras.WarningLights.ElementAt(i).Value.IsPatternRunning = false; - _extras.WarningLights.ElementAt(i).Value.IsPatternRunning = true; - wrnLights = true; - if (ELS.CurrentVehicle != null && ELS.CurrentVehicle == ElsVehicle.Vehicle) + if (Vcfroot.WRNL.DisableAtLstg3) { - ElsUiPanel.ToggleUiBtnState(wrnLights, "WRNL"); + extra.Value.IsPatternRunning = false; + } + else + { + extra.Value.IsPatternRunning = true; } } } + if (Vcfroot.PRML.LightingFormat.ToLower().Equals("chp")) { SetCHP(); diff --git a/src/Light/Scene.cs b/src/Light/Scene.cs index 95f3ba9..2cad3a7 100644 --- a/src/Light/Scene.cs +++ b/src/Light/Scene.cs @@ -38,15 +38,20 @@ public Dictionary GetData() Dictionary dic = new Dictionary(); - - dic.Add(DataNames.TurnedOn, TurnedOn); + if (TurnedOn == true) + { + dic.Add(DataNames.TurnedOn, TurnedOn); + } return dic; } public void SetData(IDictionary data) { - - TurnedOn = (bool)data[DataNames.TurnedOn]; + if (data == null || !data.TryGetValue(DataNames.TurnedOn, out var turnedOn)) + { + turnedOn = false; + } + TurnedOn = (bool)turnedOn; } public void RunTick() diff --git a/src/Light/SpotLight.cs b/src/Light/SpotLight.cs index c1924c5..b71ea57 100644 --- a/src/Light/SpotLight.cs +++ b/src/Light/SpotLight.cs @@ -65,15 +65,28 @@ public Dictionary GetData() dic.Add(DataNames.Horizontal, anglehorizontal); dic.Add(DataNames.Vertical, angleVertical); - dic.Add(DataNames.TurnedOn, TurnedOn); + if (TurnedOn == true) + { + dic.Add(DataNames.TurnedOn, TurnedOn); + } return dic; } public void SetData(IDictionary data) { - anglehorizontal = float.Parse(data[DataNames.Horizontal].ToString()); - angleVertical = float.Parse(data[DataNames.Vertical].ToString()); - TurnedOn = (bool)data[DataNames.TurnedOn]; + if (data.TryGetValue(DataNames.Horizontal, out var horizontal)) + { + anglehorizontal = float.Parse(horizontal.ToString()); + } + if (data.TryGetValue(DataNames.Vertical, out var vertical)) + { + angleVertical = float.Parse(vertical.ToString()); + } + if (data == null || !data.TryGetValue(DataNames.TurnedOn, out var turnedOn)) + { + turnedOn = false; + } + TurnedOn = (bool)turnedOn; } public void RunTick() @@ -83,29 +96,29 @@ public void RunTick() { return; } - if (Game.IsControlPressed(0, Control.PhoneLeft) && Game.PlayerPed.IsSittingInELSVehicle() && Game.PlayerPed.CurrentVehicle.NetworkId == lights.ElsVehicle.NetworkId) - { - RemoteEventManager.SendEvent(RemoteEventManager.Commands.MoveSpotlightLeft, lights.ElsVehicle, true); - anglehorizontal++; - - } - if (Game.IsControlPressed(0, Control.PhoneRight) && Game.PlayerPed.IsSittingInELSVehicle() && Game.PlayerPed.CurrentVehicle.NetworkId == lights.ElsVehicle.NetworkId) - { - - RemoteEventManager.SendEvent(RemoteEventManager.Commands.MoveSpotlightRight, lights.ElsVehicle, true); - anglehorizontal--; - } - if (Game.IsControlPressed(0, Control.PhoneUp) && Game.PlayerPed.IsSittingInELSVehicle() && Game.PlayerPed.CurrentVehicle.NetworkId == lights.ElsVehicle.NetworkId) - { - - RemoteEventManager.SendEvent(RemoteEventManager.Commands.MoveSpotlightUp, lights.ElsVehicle, true); - angleVertical++; - } - if (Game.IsControlPressed(0, Control.PhoneDown) && Game.PlayerPed.IsSittingInELSVehicle() && Game.PlayerPed.CurrentVehicle.NetworkId == lights.ElsVehicle.NetworkId) + + if (lights.ElsVehicle.ControlTimer.Expired && Game.PlayerPed.IsSittingInELSVehicle() && Game.PlayerPed.CurrentVehicle.NetworkId == lights.ElsVehicle.NetworkId) { - - RemoteEventManager.SendEvent(RemoteEventManager.Commands.MoveSpotlightDown, lights.ElsVehicle, true); - angleVertical--; + if (Game.IsControlPressed(0, Control.PhoneLeft)) + { + RemoteEventManager.SendEvent(RemoteEventManager.Commands.MoveSpotlightLeft, lights.ElsVehicle, true); + anglehorizontal++; + } + if (Game.IsControlPressed(0, Control.PhoneRight)) + { + RemoteEventManager.SendEvent(RemoteEventManager.Commands.MoveSpotlightRight, lights.ElsVehicle, true); + anglehorizontal--; + } + if (Game.IsControlPressed(0, Control.PhoneUp)) + { + RemoteEventManager.SendEvent(RemoteEventManager.Commands.MoveSpotlightUp, lights.ElsVehicle, true); + angleVertical++; + } + if (Game.IsControlPressed(0, Control.PhoneDown)) + { + RemoteEventManager.SendEvent(RemoteEventManager.Commands.MoveSpotlightDown, lights.ElsVehicle, true); + angleVertical--; + } } //var spotoffset = Game.Player.Character.CurrentVehicle.GetOffsetPosition(new Vector3(-0.9f, 1.15f, 0.5f)); diff --git a/src/Light/Stage.cs b/src/Light/Stage.cs index ee4a4ef..bb2558b 100644 --- a/src/Light/Stage.cs +++ b/src/Light/Stage.cs @@ -30,7 +30,9 @@ internal int CurrentStage get { return stage; } private set { + if (stage == value) return; stage = value; + Utils.DeveloperWriteLine($"Current stage set to {stage}"); if (ELS.Ped.IsSittingInELSVehicle() && vehicleId == ELS.CurrentVehicle.NetworkId) { ElsUiPanel.ToggleStages(CurrentStage); diff --git a/src/Manager/VehicleList.cs b/src/Manager/VehicleList.cs index bb92d42..6608905 100644 --- a/src/Manager/VehicleList.cs +++ b/src/Manager/VehicleList.cs @@ -15,11 +15,6 @@ class VehicleList : Dictionary internal VehicleList() {} - public void Add(int NetworkID) - { - var veh = new ELSVehicle(API.NetToVeh(NetworkID), NetworkID); - Add(NetworkID,veh); - } public bool IsReadOnly => throw new NotImplementedException(); public void RunTick(bool inVehicle = false) @@ -30,27 +25,22 @@ public void RunTick(bool inVehicle = false) } } - public void RunExternalTick([Optional] ELSVehicle vehicle) + public bool RecentlyRegistered(int netId) { - try + if (!VehRegAttempts.ContainsKey(netId)) { - for (int i = 0; i < Count; i++) - //foreach (var t in Values) - { - if (vehicle == null || this.ElementAt(i).Value.Handle != vehicle.Handle) - { - //t.RunExternalTick(); - this.ElementAt(i).Value.RunTick(); - } - } + return false; } - catch (Exception e) + + if (ELS.GameTime - VehRegAttempts[netId].Item2 >= 15000 && VehRegAttempts[netId].Item1 < 5) { - Utils.DebugWriteLine($"VehicleList Error: {e.Message}"); + return false; } + + return true; } - public bool MakeSureItExists(int NetworkID, [Optional]out ELSVehicle vehicle) + public bool TryGetElsVehicle(int NetworkID, out ELSVehicle vehicle, IDictionary data = null, int playerId = -1) { if (NetworkID == 0) { @@ -59,131 +49,48 @@ public bool MakeSureItExists(int NetworkID, [Optional]out ELSVehicle vehicle) return false; } - else if (!ContainsKey(NetworkID)) - { - if (VehRegAttempts.ContainsKey(NetworkID)) - { - VehRegAttempts[NetworkID] = new Tuple(VehRegAttempts[NetworkID].Item1 + 1, ELS.GameTime); - } - else - { - VehRegAttempts.Add(NetworkID, new Tuple(1, ELS.GameTime)); - } - try - { - if (API.NetworkDoesNetworkIdExist(NetworkID)) - { - ELSVehicle veh = null; - int handle = API.NetToVeh(NetworkID); - if (handle == 0) - { - veh = new ELSVehicle(ELS.CurrentVehicle.Handle, ELS.CurrentVehicle.NetworkId); - } - else - { - veh = new ELSVehicle(handle, NetworkID); - } - Add(NetworkID, veh); - vehicle = veh; - return true; - } - else - { - Utils.DebugWriteLine($"Vehicle with netId {NetworkID} doesn't exist."); - vehicle = null; - return false; - } - } - catch (Exception ex) - { - Utils.DebugWriteLine($"Exsits Error: {ex.Message} due to {ex.InnerException} {ex.StackTrace} {ex}"); - vehicle = null; - return false; - throw ex; - } - - } - else + if (ContainsKey(NetworkID)) { - vehicle = this[NetworkID];//Find(poolObject => ((ELSVehicle)poolObject).GetNetworkId() == NetworkID); + vehicle = this[NetworkID]; return true; } - } - public bool MakeSureItExists(int NetworkID, IDictionary data, [Optional]out ELSVehicle vehicle, int PlayerId = -1) - { - if (NetworkID == 0) + if (VehRegAttempts.ContainsKey(NetworkID)) { - Debug.WriteLine("ERROR NetwordID equals 0\n"); - vehicle = null; - return false; + VehRegAttempts[NetworkID] = new Tuple(VehRegAttempts[NetworkID].Item1 + 1, ELS.GameTime); } - - else if (!ContainsKey(NetworkID)) + else + { + VehRegAttempts.Add(NetworkID, new Tuple(1, ELS.GameTime)); + } + try { - if (VehRegAttempts.ContainsKey(NetworkID)) + if (API.NetworkDoesNetworkIdExist(NetworkID)) { - VehRegAttempts[NetworkID] = new Tuple(VehRegAttempts[NetworkID].Item1 + 1, ELS.GameTime); + vehicle = new ELSVehicle(NetworkID, data); + Add(NetworkID, vehicle); + return true; } else { - VehRegAttempts.Add(NetworkID, new Tuple(1, ELS.GameTime)); - } - try - { - ELSVehicle veh = null; - // Vehicle is out of scope so create it with just network id - if (PlayerId != -1 && !API.NetworkDoesNetworkIdExist(NetworkID)) + if (data == null) { - Utils.DebugWriteLine($"Registering vehicle with netid of {NetworkID} to list from {PlayerId}"); - veh = new ELSVehicle(0, NetworkID, data); - if (veh == null) - { - vehicle = null; - veh = null; - return false; - } - } - else - { - if (API.NetworkDoesNetworkIdExist(NetworkID)) - { - int handle = API.NetworkGetEntityFromNetworkId(NetworkID); - veh = new ELSVehicle(handle, NetworkID, data); - } - } - if (veh != null) - { - //CurrentlyRegisteringVehicle.Remove(NetworkID); - Add(NetworkID, veh); - Utils.DebugWriteLine($"Added {NetworkID} to vehicle list"); - vehicle = veh; - return true; - } - else - { - //CurrentlyRegisteringVehicle.Remove(NetworkID); - Utils.DebugWriteLine("Failed to add vehicle to list please try again"); + Utils.DebugWriteLine($"Vehicle with netId {NetworkID} doesn't exist."); vehicle = null; return false; } - } - catch (Exception ex) - { - //CurrentlyRegisteringVehicle.Remove(NetworkID); - Utils.DebugWriteLine($"Exsits Error With Data: {ex.Message}\n" + - $"{ex}"); - vehicle = null; - return false; - throw ex; - } + vehicle = new ELSVehicle(NetworkID, data); + Add(NetworkID, vehicle); + return true; + } } - else + catch (Exception ex) { - Utils.DebugWriteLine($"Returning vehicle {NetworkID} from list"); - vehicle = this[NetworkID]; - return true; + Utils.DebugWriteLine($"Exsits Error: {ex.Message} due to {ex.InnerException} {ex.StackTrace} {ex}"); + vehicle = null; + return false; + throw ex; } } diff --git a/src/Manager/VehicleManager.cs b/src/Manager/VehicleManager.cs index 67d7b09..194aae9 100644 --- a/src/Manager/VehicleManager.cs +++ b/src/Manager/VehicleManager.cs @@ -38,9 +38,9 @@ internal void RunTick() } else { - if (!vehicleList.VehRegAttempts.ContainsKey(netId) || ELS.GameTime - vehicleList.VehRegAttempts[netId].Item2 >= 15000 && vehicleList.VehRegAttempts[netId].Item1 < 5) + if (!vehicleList.RecentlyRegistered(netId)) { - if (ELS.CurrentVehicle.IsNetworked() && vehicleList.MakeSureItExists(netId, vehicle: out ELSVehicle _currentVehicle)) + if (ELS.CurrentVehicle.IsNetworked() && vehicleList.TryGetElsVehicle(netId, vehicle: out ELSVehicle _currentVehicle)) { _currentVehicle?.RunControlTick(); } @@ -48,23 +48,23 @@ internal void RunTick() } } } - if (ELS.CurrentVehicle != null) - { - var isPaused = Game.IsPaused; - if (isPaused) - { - if (!Indicator.IsHazardLightActive) - { - WasPaused = true; - Indicator.SetHazards(true); - } - } - else if (WasPaused && Indicator.IsHazardLightActive) - { - WasPaused = false; - Indicator.SetHazards(false); - } - } + //if (ELS.CurrentVehicle != null) + //{ + // var isPaused = Game.IsPaused; + // if (isPaused) + // { + // if (!Indicator.IsHazardLightActive) + // { + // WasPaused = true; + // Indicator.SetHazards(true); + // } + // } + // else if (WasPaused && Indicator.IsHazardLightActive) + // { + // WasPaused = false; + // Indicator.SetHazards(false); + // } + //} vehicleList.RunTick(); } catch (Exception e) @@ -113,7 +113,7 @@ public void SetVehicleSirenData(IDictionary dataDic, int Network /// Proxies the sync data to a certain vehicle /// /// data - public void SetVehicleSyncData(IDictionary dataDic, int networkId, int PlayerId) + public void SetVehicleSyncData(IDictionary dataDic, int netId, int PlayerId) { if (Game.Player.ServerId == PlayerId) { @@ -124,29 +124,25 @@ public void SetVehicleSyncData(IDictionary dataDic, int networkI Utils.DebugWriteLine($"{PlayerId} has sent us data parsing"); var hasSirenOrLightData = dataDic.ContainsKey(DataNames.Siren) || dataDic.ContainsKey(DataNames.Light); - if (vehicleList.ContainsKey(networkId) && hasSirenOrLightData) + if (vehicleList.ContainsKey(netId) && hasSirenOrLightData) { - vehicleList[networkId].SetData(dataDic); + vehicleList[netId].SetData(dataDic); return; } - if (API.GetPlayerFromServerId(PlayerId) == -1) - { - - } - if (hasSirenOrLightData && (!vehicleList.VehRegAttempts.ContainsKey(networkId) || (ELS.GameTime - vehicleList.VehRegAttempts[networkId].Item2 >= 15000 && vehicleList.VehRegAttempts[networkId].Item1 < 5))) + if (hasSirenOrLightData && !vehicleList.RecentlyRegistered(netId)) { - if (!vehicleList.MakeSureItExists(networkId, dataDic, out ELSVehicle veh1, PlayerId)) + if (!vehicleList.TryGetElsVehicle(netId, out ELSVehicle _, dataDic, PlayerId)) { - Utils.ReleaseWriteLine($"Failed to register other clients vehicle with id {networkId}"); + Utils.ReleaseWriteLine($"Failed to register other clients vehicle with id {netId}"); return; } } - if (dataDic.ContainsKey(DataNames.IndicatorState) && !hasSirenOrLightData && API.NetworkDoesNetworkIdExist(networkId)) + if (dataDic.ContainsKey(DataNames.IndicatorState) && !hasSirenOrLightData && API.NetworkDoesNetworkIdExist(netId)) { - if(API.NetworkDoesNetworkIdExist(networkId)) + if(API.NetworkDoesNetworkIdExist(netId)) { - Vehicle veh = (Vehicle)Entity.FromHandle(API.NetworkGetEntityFromNetworkId(networkId)); + Vehicle veh = (Vehicle)Entity.FromHandle(API.NetworkGetEntityFromNetworkId(netId)); if (veh != null) { Indicator.ToggleInicatorState(veh, Indicator.IndStateLib[dataDic[DataNames.IndicatorState].ToString()]); @@ -201,7 +197,7 @@ internal void SyncAllVehiclesOnFirstSpawn(IDictionary data) { int netID = int.Parse(struct1.Key); var vehData = (IDictionary)struct1.Value; - vehicleList.MakeSureItExists(netID, vehData, out ELSVehicle _); + vehicleList.TryGetElsVehicle(netID, out ELSVehicle _, vehData); } } diff --git a/src/NUI/ElsUiPanel.cs b/src/NUI/ElsUiPanel.cs index db394ff..ab72d3f 100644 --- a/src/NUI/ElsUiPanel.cs +++ b/src/NUI/ElsUiPanel.cs @@ -106,6 +106,7 @@ internal static void SendLightData(bool state, string light, string color) internal static void SetUiDesc(string desc, string uielement) { + Utils.DeveloperWriteLine($"SetUiDesc ${uielement}: ${desc}"); API.SendNuiMessage($"{{\"type\":\"setuidesc\", \"uielement\":\"{uielement}\", \"desc\":\"{desc}\" }}"); } diff --git a/src/RemoteEventManager.cs b/src/RemoteEventManager.cs index 8626f51..295f625 100644 --- a/src/RemoteEventManager.cs +++ b/src/RemoteEventManager.cs @@ -71,24 +71,13 @@ internal enum MessageTypes internal delegate void RemoteMessageRecievedHandler(); internal static event RemoteMessageRecievedHandler RemoteMessageRecieved; - internal static void SendEvent(Commands type, Vehicle vehicle, bool state) - { - Debug.WriteLine($"sendding data for netID {vehicle.NetworkId} : {state}"); - Manager.VehicleManager.SyncRequestReply(type, vehicle.NetworkId); - } - internal static void SendEvent(Commands type, ELSVehicle vehicle, bool state) { + vehicle.ControlTimer.Reset(); Debug.WriteLine($"sendding data for netID {vehicle.NetworkId} of type {type} : {state}"); Manager.VehicleManager.SyncRequestReply(type, vehicle.NetworkId); } - internal static void SendEvent(ELSVehicle vehicle, Dictionary data) - { - Debug.WriteLine($"sendding data for netID {vehicle.NetworkId}"); - FullSync.FullSyncManager.SendDataBroadcast(data, vehicle.NetworkId); - } - internal static void SendLightEvent(ELSVehicle vehicle, string key, object value) { Debug.WriteLine($"sendding LightSync data for netID {vehicle.NetworkId}; {key} : {value}"); @@ -102,6 +91,7 @@ internal static void SendLightEvent(ELSVehicle vehicle, string key, object value internal static void SendLightEvent(ELSVehicle vehicle, Dictionary data) { Debug.WriteLine($"sendding LightSync data for netID {vehicle.NetworkId}"); + vehicle.ControlTimer.Reset(); FullSync.FullSyncManager.SendLightBroadcast(data, vehicle.NetworkId); } @@ -119,6 +109,7 @@ internal static void SendSirenEvent(ELSVehicle vehicle, string key, object value internal static void SendSirenEvent(ELSVehicle vehicle, Dictionary data) { Debug.WriteLine($"sendding SirenSync data for netID {vehicle.NetworkId}"); + vehicle.ControlTimer.Reset(); FullSync.FullSyncManager.SendSirenBroadcast(data, vehicle.NetworkId); } } diff --git a/src/Siren/MainSiren/MainSiren.FullSync.cs b/src/Siren/MainSiren/MainSiren.FullSync.cs index 54513bd..7466cf8 100644 --- a/src/Siren/MainSiren/MainSiren.FullSync.cs +++ b/src/Siren/MainSiren/MainSiren.FullSync.cs @@ -7,23 +7,43 @@ partial class Siren { internal partial class MainSiren { - + IDictionary oldData = null; public Dictionary GetData() { Dictionary dic = new Dictionary(); dic.Add(DataNames.Interrupted, interupted); - dic.Add(DataNames.CurrentTone, currentTone.ToString()); - dic.Add(DataNames._enable, _enable); + dic.Add(DataNames.CurrentTone, currentTone); + dic.Add(DataNames._enable, Enable); return dic; } public void SetData(IDictionary data) { - if (data.TryGetValue(DataNames.CurrentTone, out var tone)) + oldData = data; + if (!data.TryGetValue(DataNames.CurrentTone, out var tone) || tone == null) + { + tone = 0; + } + SetMainTone((int)tone); + currentTone = (int)tone; + + if (data.TryGetValue(DataNames.Interrupted, out var interrupted)) + { + interupted = (bool)interrupted; + } + if (data.TryGetValue(DataNames._enable, out var enable)) + { + SetEnable((bool)enable); + } + } + + public void SetDataPartial(IDictionary data) + { + if (data.TryGetValue(DataNames.CurrentTone, out var tone) && tone != null) { - setMainTone(int.Parse(tone.ToString())); - currentTone = int.Parse(tone.ToString()); + SetMainTone((int)tone); + currentTone = (int)tone; } if (data.TryGetValue(DataNames.Interrupted, out var interrupted)) { @@ -33,8 +53,6 @@ public void SetData(IDictionary data) { SetEnable((bool)enable); } - - } } } diff --git a/src/Siren/MainSiren/MainSiren.cs b/src/Siren/MainSiren/MainSiren.cs index efc5558..efb2b1a 100644 --- a/src/Siren/MainSiren/MainSiren.cs +++ b/src/Siren/MainSiren/MainSiren.cs @@ -7,9 +7,9 @@ namespace ELS.Siren { partial class Siren : IManagerEntry { - internal partial class MainSiren : IFullSyncComponent + internal partial class MainSiren { - internal bool _enable { get; private set; } + internal bool Enable { get; private set; } internal bool interupted = false; internal int currentTone; @@ -26,14 +26,14 @@ internal MainSiren(ref Tones tonesl) /// internal void SetEnable(bool enable) { - if (_enable == enable) return; - _enable = enable; - if (_enable) //turning the main siren on + if (Enable == enable) return; + Enable = enable; + if (Enable) //turning the main siren on { MainTones[currentTone].SetState(true); if (Game.PlayerPed.IsSittingInELSVehicle()) { - ElsUiPanel.ToggleUiBtnState(_enable, "SRN"); + ElsUiPanel.ToggleUiBtnState(Enable, "SRN"); } } else @@ -41,7 +41,7 @@ internal void SetEnable(bool enable) MainTones[currentTone].SetState(false); if (Game.PlayerPed.IsSittingInELSVehicle()) { - ElsUiPanel.ToggleUiBtnState(_enable, "SRN"); + ElsUiPanel.ToggleUiBtnState(Enable, "SRN"); } } } @@ -51,12 +51,7 @@ internal void SetEnable(bool enable) /// else it will disable the current tone and enable the /// /// - internal void setMainTone(int tone) - { - _setMainTone(tone); - } - - private void _setMainTone(int tone) + internal void SetMainTone(int tone) { if (currentTone == tone) SetEnable(false); else @@ -64,11 +59,12 @@ private void _setMainTone(int tone) MainTones[currentTone].SetState(false); currentTone = tone; } - if(_enable) + if (Enable) { MainTones[currentTone].SetState(true); } } + internal void nextTone() { @@ -81,7 +77,7 @@ internal void nextTone() { currentTone += 1; } - if (_enable) { MainTones[currentTone].SetState(true); } + if (Enable) { MainTones[currentTone].SetState(true); } } internal void previousTone() @@ -93,7 +89,7 @@ internal void previousTone() { currentTone -= 1; } - if (_enable) { MainTones[currentTone].SetState(true); } + if (Enable) { MainTones[currentTone].SetState(true); } } diff --git a/src/Siren/Siren/Controls/Siren.Controls.KB.cs b/src/Siren/Siren/Controls/Siren.Controls.KB.cs index f1e1d3a..2775ccc 100644 --- a/src/Siren/Siren/Controls/Siren.Controls.KB.cs +++ b/src/Siren/Siren/Controls/Siren.Controls.KB.cs @@ -1,6 +1,7 @@ using CitizenFX.Core; using ELS.configuration; using System; +using static ELS.configuration.ElsConfiguration; namespace ELS.Siren { @@ -8,25 +9,31 @@ partial class Siren : IManagerEntry { void AirHornControlsKB() { - if ((Game.IsControlJustPressed(0, ElsConfiguration.KBBindings.Sound_Ahorn) && - Game.CurrentInputMode == InputMode.MouseAndKeyboard) || - (Game.IsControlJustPressed(2, ElsConfiguration.GPBindings.Sound_Ahorn) && Game.CurrentInputMode == InputMode.GamePad && Global.AllowController)) + if (!_tones.horn.State) { - AirHornLogic(true, true); - RemoteEventManager.SendEvent(RemoteEventManager.Commands.AirHorn, _elsVehicle, true); + if (Controls.SoundAirHorn.IsJustPressed()) + { + if (!_elsVehicle.ControlTimer.Expired) + { + return; + } + AirHornLogic(true, true); + RemoteEventManager.SendEvent(RemoteEventManager.Commands.AirHorn, _elsVehicle, true); + } } - if ((Game.IsControlJustReleased(0, ElsConfiguration.KBBindings.Sound_Ahorn) && - Game.CurrentInputMode == InputMode.MouseAndKeyboard) - || (Game.IsControlJustReleased(2, Control.SpecialAbility) && Game.CurrentInputMode == InputMode.GamePad && Global.AllowController)) + else { - AirHornLogic(false, true); - RemoteEventManager.SendEvent(RemoteEventManager.Commands.AirHorn, _elsVehicle, false); + if (Controls.SoundAirHorn.IsJustReleased()) + { + AirHornLogic(false, true); + RemoteEventManager.SendEvent(RemoteEventManager.Commands.AirHorn, _elsVehicle, false); + } } } void ManualTone0ControlsKB() { - if ((Game.IsControlJustReleased(0, ElsConfiguration.KBBindings.Snd_SrnTon1) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) || (Global.AllowController && Game.IsControlJustPressed(2, ElsConfiguration.GPBindings.Snd_SrnTon1) && Game.CurrentInputMode == InputMode.GamePad)) + if (Controls.SoundSirenTone0.IsJustReleased()) { if (SirenTone0Logic(true, true)) { RemoteEventManager.SendSirenEvent(_elsVehicle, "ms", new { c = 0 }); @@ -35,7 +42,7 @@ void ManualTone0ControlsKB() } void ManualTone1ControlsKB() { - if ((Game.IsControlJustReleased(0, ElsConfiguration.KBBindings.Snd_SrnTon2) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) || (Global.AllowController && Game.IsControlPressed(2, ElsConfiguration.GPBindings.Snd_SrnTon1) && Game.IsControlJustPressed(2, ElsConfiguration.GPBindings.Snd_SrnTon3) && Game.CurrentInputMode == InputMode.GamePad)) + if (Controls.SoundSirenTone1.IsJustReleased()) { if (SirenTone1Logic(true, true)) { @@ -45,7 +52,7 @@ void ManualTone1ControlsKB() } void ManualTone2ControlsKB() { - if ((Game.IsControlJustReleased(0, ElsConfiguration.KBBindings.Snd_SrnTon3) && Game.CurrentInputMode == InputMode.MouseAndKeyboard) || (Global.AllowController && Game.IsControlJustPressed(2, ElsConfiguration.GPBindings.Snd_SrnTon3) && Game.CurrentInputMode == InputMode.GamePad)) + if (Controls.SoundSirenTone2.IsJustReleased()) { if (SirenTone2Logic(true, true)) { @@ -55,18 +62,15 @@ void ManualTone2ControlsKB() } void ManualTone3ControlsKB() { - if (Game.IsControlJustReleased(0, ElsConfiguration.KBBindings.Snd_SrnTon4) && SirenTone3Logic(true, true)) + if (Controls.SoundSirenTone3.IsJustReleased() && SirenTone3Logic(true, true)) { RemoteEventManager.SendSirenEvent(_elsVehicle, "ms", new { c = 3 }); } } - + void MainSirenToggleControlsKB() { - if ((Game.IsControlJustReleased(0, ElsConfiguration.KBBindings.Toggle_SIRN) - && Game.CurrentInputMode == InputMode.MouseAndKeyboard) || (Global.AllowController - && Game.IsControlJustReleased(2, ElsConfiguration.GPBindings.Toggle_SIRN) - && Game.CurrentInputMode == InputMode.GamePad)) + if (Controls.ToggleSiren.IsJustReleased()) { MainSirenToggleLogic(true, true); RemoteEventManager.SendEvent(RemoteEventManager.Commands.MainSiren, _elsVehicle, true); @@ -75,13 +79,13 @@ void MainSirenToggleControlsKB() void ManualSoundControlsKB() { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.Sound_Manul); - if (Game.IsDisabledControlJustPressed(0, ElsConfiguration.KBBindings.Sound_Manul)) + Controls.SoundManual.Disable(); + if (Controls.SoundManual.IsDisabledJustPressed()) { ManualSoundLogic(true, true); RemoteEventManager.SendEvent(RemoteEventManager.Commands.ManualSound, _elsVehicle, true); } - if (Game.IsDisabledControlJustReleased(0, ElsConfiguration.KBBindings.Sound_Manul)) + if (Controls.SoundManual.IsDisabledJustReleased()) { ManualSoundLogic(false, true); RemoteEventManager.SendEvent(RemoteEventManager.Commands.ManualSound, _elsVehicle, false); @@ -90,7 +94,7 @@ void ManualSoundControlsKB() void DualSirenControlsKB() { - if (Game.IsControlJustReleased(0, ElsConfiguration.KBBindings.Toggle_DSRN)) + if (Controls.ToggleDualSiren.IsJustReleased()) { DualSirenLogic(true, true); RemoteEventManager.SendEvent(RemoteEventManager.Commands.DualSiren, _elsVehicle, true); diff --git a/src/Siren/Siren/Siren.FullSync.cs b/src/Siren/Siren/Siren.FullSync.cs index ae9de1d..a9e717f 100644 --- a/src/Siren/Siren/Siren.FullSync.cs +++ b/src/Siren/Siren/Siren.FullSync.cs @@ -6,6 +6,10 @@ namespace ELS.Siren { partial class Siren : IManagerEntry, IFullSyncComponent { + /// + /// Set Data of siren, assumes null values are defaults (ie. int = 0, bool = false) + /// + /// public void SetData(IDictionary data) { if (data.TryGetValue(DataNames.dual_siren, out var dualSiren)) @@ -21,6 +25,26 @@ public void SetData(IDictionary data) } } + /// + /// Set Data of siren, assumes null values are not to be changed + /// + /// + public void SetDataPartial(IDictionary data) + { + if (data.TryGetValue(DataNames.dual_siren, out var dualSiren)) + { + dual_siren = (bool)dualSiren; + } + if (data.TryGetValue(DataNames._mainSiren, out var mainSiren)) + { + _mainSiren.SetDataPartial((IDictionary)mainSiren); + } + if (data.TryGetValue(DataNames._tones, out var tones)) + { + _tones.SetData((IDictionary)tones); + } + } + public Dictionary GetData() { var dic = new Dictionary diff --git a/src/Siren/Siren/Siren.Logic.cs b/src/Siren/Siren/Siren.Logic.cs index c3a4084..20067ac 100644 --- a/src/Siren/Siren/Siren.Logic.cs +++ b/src/Siren/Siren/Siren.Logic.cs @@ -1,6 +1,7 @@ using CitizenFX.Core; using ELS.configuration; using ELS.NUI; +using static ELS.configuration.ElsConfiguration; using Control = CitizenFX.Core.Control; namespace ELS.Siren @@ -12,8 +13,7 @@ void AirHornLogic(bool pressed, bool disableControls = false) { if (disableControls) { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.Sound_Ahorn); - Game.DisableControlThisFrame(2, Control.ScriptPadDown); + Controls.SoundAirHorn.Disable(); } if (pressed) { @@ -40,7 +40,7 @@ void AirHornLogic(bool pressed, bool disableControls = false) } if (_vcf.SOUNDS.MainHorn.InterruptsSiren) { - if (_mainSiren._enable) + if (_mainSiren.Enable) { _mainSiren.interupted = true; _mainSiren.SetEnable(false); @@ -80,15 +80,18 @@ bool SirenTone0Logic(bool pressed, bool disableControls = false) { return false; } - if (disableControls) Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.Snd_SrnTon1); - if (!_mainSiren._enable || _mainSiren.currentTone == 0) + if (disableControls) + { + Controls.SoundSirenTone0.Disable(); + } + if (!_mainSiren.Enable || _mainSiren.currentTone == 0) { return false; } if (pressed) { - _mainSiren.setMainTone(0); + _mainSiren.SetMainTone(0); if (_vcf.PRML.ForcedPatterns.SrnTone1.Enabled) { _patternController.CurrentPrmPattern = _vcf.PRML.ForcedPatterns.SrnTone1.IntPattern; @@ -109,7 +112,7 @@ bool SirenTone0Logic(bool pressed, bool disableControls = false) _tones.tone3.SetState(false); } ElsUiPanel.SetUiDesc(_mainSiren.MainTones[_mainSiren.currentTone].Type, "SRN"); - ElsUiPanel.ToggleUiBtnState(_mainSiren._enable, "SRN"); + ElsUiPanel.ToggleUiBtnState(_mainSiren.Enable, "SRN"); if (Global.BtnClicksBtwnSrnTones) { ElsUiPanel.PlayUiSound("sirenclickoff"); @@ -128,16 +131,16 @@ bool SirenTone1Logic(bool pressed, bool disableControls = false) } if (disableControls) { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.Snd_SrnTon2); + Controls.SoundSirenTone1.Disable(); } - if (!_mainSiren._enable || _mainSiren.currentTone == 1) + if (!_mainSiren.Enable || _mainSiren.currentTone == 1) { return false; } if (pressed) { - _mainSiren.setMainTone(1); + _mainSiren.SetMainTone(1); if (_vcf.PRML.ForcedPatterns.SrnTone2.Enabled) { _patternController.CurrentPrmPattern = _vcf.PRML.ForcedPatterns.SrnTone2.IntPattern; @@ -158,7 +161,7 @@ bool SirenTone1Logic(bool pressed, bool disableControls = false) _tones.tone3.SetState(true); } ElsUiPanel.SetUiDesc(_mainSiren.MainTones[_mainSiren.currentTone].Type, "SRN"); - ElsUiPanel.ToggleUiBtnState(_mainSiren._enable, "SRN"); + ElsUiPanel.ToggleUiBtnState(_mainSiren.Enable, "SRN"); if (Global.BtnClicksBtwnSrnTones) { ElsUiPanel.PlayUiSound("sirenclickoff"); @@ -172,19 +175,20 @@ bool SirenTone2Logic(bool pressed, bool disableControls = false) { if (!_tones.tone3.AllowUse) { + Utils.DeveloperWriteLine("tone3 not allowed!"); return false; } if (disableControls) { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.Snd_SrnTon3); + Controls.SoundSirenTone2.Disable(); } - if (!_mainSiren._enable || _mainSiren.currentTone == 2) + if (!_mainSiren.Enable || _mainSiren.currentTone == 2) { return false; } if (pressed) { - _mainSiren.setMainTone(2); + _mainSiren.SetMainTone(2); if (_vcf.PRML.ForcedPatterns.SrnTone3.Enabled) { _patternController.CurrentPrmPattern = _vcf.PRML.ForcedPatterns.SrnTone3.IntPattern; @@ -205,7 +209,7 @@ bool SirenTone2Logic(bool pressed, bool disableControls = false) _tones.tone4.SetState(true); } ElsUiPanel.SetUiDesc(_mainSiren.MainTones[_mainSiren.currentTone].Type, "SRN"); - ElsUiPanel.ToggleUiBtnState(_mainSiren._enable, "SRN"); + ElsUiPanel.ToggleUiBtnState(_mainSiren.Enable, "SRN"); if (Global.BtnClicksBtwnSrnTones) { ElsUiPanel.PlayUiSound("sirenclickoff"); @@ -228,15 +232,15 @@ bool SirenTone3Logic(bool pressed, bool disableControls = false) } if (disableControls) { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.Snd_SrnTon4); + Controls.SoundSirenTone3.Disable(); } - if (!_mainSiren._enable || _mainSiren.currentTone == 3) + if (!_mainSiren.Enable || _mainSiren.currentTone == 3) { return false; } if (pressed) { - _mainSiren.setMainTone(3); + _mainSiren.SetMainTone(3); if (_vcf.PRML.ForcedPatterns.SrnTone4.Enabled) { _patternController.CurrentPrmPattern = _vcf.PRML.ForcedPatterns.SrnTone4.IntPattern; @@ -258,7 +262,7 @@ bool SirenTone3Logic(bool pressed, bool disableControls = false) _tones.tone3.SetState(false); } ElsUiPanel.SetUiDesc(_mainSiren.MainTones[_mainSiren.currentTone].Type, "SRN"); - ElsUiPanel.ToggleUiBtnState(_mainSiren._enable, "SRN"); + ElsUiPanel.ToggleUiBtnState(_mainSiren.Enable, "SRN"); if (Global.BtnClicksBtwnSrnTones) { ElsUiPanel.PlayUiSound("sirenclickoff"); @@ -273,15 +277,15 @@ private void MainSirenToggleLogic(bool toggle, bool disableControls = false) { if (disableControls) { - Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.Toggle_SIRN); + Controls.ToggleSiren.Disable(); } if (toggle) { - _mainSiren.SetEnable(!_mainSiren._enable); + _mainSiren.SetEnable(!_mainSiren.Enable); if (dual_siren) { - dual_siren = _mainSiren._enable; + dual_siren = _mainSiren.Enable; } ElsUiPanel.SetUiDesc(_mainSiren.MainTones[_mainSiren.currentTone].Type, "SRN"); ElsUiPanel.PlayUiSound("sirenclickoff"); @@ -291,10 +295,13 @@ private void MainSirenToggleLogic(bool toggle, bool disableControls = false) void ManualSoundLogic(bool pressed, bool disableControls = false) { - if (disableControls) Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.Sound_Manul); + if (disableControls) + { + Controls.SoundManual.Disable(); + } if (pressed) { - if (!_mainSiren._enable || (!_mainSiren._enable && _vcf.SOUNDS.MainHorn.InterruptsSiren && + if (!_mainSiren.Enable || (!_mainSiren.Enable && _vcf.SOUNDS.MainHorn.InterruptsSiren && _tones.horn.State)) { _tones.tone1.SetState(true); @@ -311,7 +318,7 @@ void ManualSoundLogic(bool pressed, bool disableControls = false) } else { - if (!_mainSiren._enable || (!_mainSiren._enable && _vcf.SOUNDS.MainHorn.InterruptsSiren && + if (!_mainSiren.Enable || (!_mainSiren.Enable && _vcf.SOUNDS.MainHorn.InterruptsSiren && _tones.horn.State)) { _tones.tone1.SetState(false); @@ -326,7 +333,10 @@ void ManualSoundLogic(bool pressed, bool disableControls = false) void DualSirenLogic(bool toggle, bool disableControls = false) { - if (disableControls) Game.DisableControlThisFrame(0, ElsConfiguration.KBBindings.Toggle_DSRN); + if (disableControls) + { + Controls.ToggleDualSiren.Disable(); + } if (toggle) { dual_siren = !dual_siren; diff --git a/src/Siren/Siren/Siren.Loop.cs b/src/Siren/Siren/Siren.Loop.cs index 0a285b9..8f44055 100644 --- a/src/Siren/Siren/Siren.Loop.cs +++ b/src/Siren/Siren/Siren.Loop.cs @@ -1,5 +1,6 @@ using CitizenFX.Core; using ELS.Light; +using static ELS.configuration.ElsConfiguration; namespace ELS.Siren { @@ -12,13 +13,17 @@ public void Ticker() public void ControlTicker(Lights lights) { - Game.DisableControlThisFrame(0, configuration.ElsConfiguration.KBBindings.Sound_Ahorn); + Controls.SoundAirHorn.Disable(); AirHornControlsKB(); + if (!_elsVehicle.ControlTimer.Expired) + { + return; + } ManualTone0ControlsKB(); ManualTone1ControlsKB(); ManualTone2ControlsKB(); ManualTone3ControlsKB(); - ManualSoundControlsKB(); + //ManualSoundControlsKB(); if (lights._stage.CurrentStage == 3) { MainSirenToggleControlsKB(); diff --git a/src/Siren/Siren/Siren.cs b/src/Siren/Siren/Siren.cs index 50a5908..3e43e23 100644 --- a/src/Siren/Siren/Siren.cs +++ b/src/Siren/Siren/Siren.cs @@ -19,10 +19,9 @@ partial class Siren : IManagerEntry public ELSVehicle _elsVehicle { get; set; } public Vehicle _vehicle { get; set; } public MainSiren _mainSiren; - private bool _isInitialized = false; IPatterns _patternController; internal Tones _tones; - public Siren(ELSVehicle vehicle,Vcfroot vcfroot, [Optional]IDictionary data, IPatterns patt) + public Siren(ELSVehicle vehicle, Vcfroot vcfroot, IPatterns patt) { _vcf = vcfroot; if (_vcf is null) @@ -50,10 +49,6 @@ public Siren(ELSVehicle vehicle,Vcfroot vcfroot, [Optional]IDictionary GetData() { - var dic = - new Dictionary - { - {DataNames.Horn, horn.State}, - {DataNames.tone1, tone1.State}, - {DataNames.tone2, tone2.State}, - {DataNames.tone3, tone3.State}, - {DataNames.tone4, tone4.State} - }; + var dic = new Dictionary(); + if (horn.State) + { + dic[DataNames.Horn] = horn.State; + } + if (tone1.State) + { + dic[DataNames.tone1] = tone1.State; + } + if (tone2.State) + { + dic[DataNames.tone2] = tone2.State; + } + if (tone3.State) + { + dic[DataNames.tone3] = tone3.State; + } + if (tone4.State) + { + dic[DataNames.tone4] = tone4.State; + } return dic; } public void SetData(IDictionary data) { - if (data.TryGetValue(DataNames.Horn, out var _horn)) + if (!data.TryGetValue(DataNames.Horn, out var _horn)) { - horn.SetState((bool)_horn); + _horn = false; } - if (data.TryGetValue(DataNames.tone1, out var tone1State)) + horn.SetState((bool)_horn); + if (!data.TryGetValue(DataNames.tone1, out var tone1State)) { - tone1.SetState((bool)tone1State); + tone1State = false; } - if (data.TryGetValue(DataNames.tone2, out var tone2State)) + tone1.SetState((bool)tone1State); + if (!data.TryGetValue(DataNames.tone2, out var tone2State)) { - tone2.SetState((bool)tone2State); + tone2State = false; } - if (data.TryGetValue(DataNames.tone3, out var tone3State)) + tone2.SetState((bool)tone2State); + if (!data.TryGetValue(DataNames.tone3, out var tone3State)) { - tone3.SetState((bool)tone3State); + tone3State = false; } - if (data.TryGetValue(DataNames.tone4, out var tone4State)) + tone3.SetState((bool)tone3State); + if (!data.TryGetValue(DataNames.tone4, out var tone4State)) { - tone4.SetState((bool)tone4State); + tone4State = false; } + tone4.SetState((bool)tone4State); } public void RunTick() diff --git a/src/Utils.cs b/src/Utils.cs index 366565a..4a347cf 100644 --- a/src/Utils.cs +++ b/src/Utils.cs @@ -93,11 +93,22 @@ static internal void ThrowException(Exception ex) } - class Timer + public class Timer { int _limit = 0; - public int Limit { set { _limit = ELS.GameTime + value; } } + int _timeLimit = 0; + + public int Limit { + set { + _limit = ELS.GameTime + value; + _timeLimit = value; + } + get + { + return _timeLimit; + } + } public bool Expired { get @@ -115,14 +126,16 @@ public bool Expired } } - public Timer() - { + public Timer() {} + public Timer(int limit) + { + Limit = limit; } public void Reset() { - _limit = 0; + Limit = _timeLimit; } } } diff --git a/src/Vehicle/ELSVehicle.cs b/src/Vehicle/ELSVehicle.cs index 4eebd91..5f0bb11 100644 --- a/src/Vehicle/ELSVehicle.cs +++ b/src/Vehicle/ELSVehicle.cs @@ -9,8 +9,9 @@ namespace ELS { - public class ELSVehicle : PoolObject, FullSync.IFullSyncComponent + public class ELSVehicle { + public Timer ControlTimer = new Timer(1000); public bool Changed { get; set; } = true; internal const float maxDistance = 200.0f; internal const float deactivateDistance = maxDistance + 10.0f; @@ -36,23 +37,14 @@ public class ELSVehicle : PoolObject, FullSync.IFullSyncComponent private int _lastTry = -1000; public bool IsSirenActive = false; internal int cachedNetId; - IDictionary cachedData = null; - public ELSVehicle(int handle, int netId, [Optional] IDictionary data) : base(handle) + public ELSVehicle(int netId, IDictionary data = null) { - cachedData = data; cachedNetId = netId; - if (API.DoesEntityExist(handle)) - { - Init(handle); - } - else if (netId != 0 && API.NetworkDoesNetworkIdExist(netId)) + + if (API.NetworkDoesNetworkIdExist(netId)) { - handle = API.NetToVeh(netId); - if (handle == 0) - { - handle = API.NetworkGetEntityFromNetworkId(netId); - } + int handle = API.NetworkGetEntityFromNetworkId(netId); Init(handle); } @@ -61,19 +53,32 @@ public ELSVehicle(int handle, int netId, [Optional] IDictionary _vcf = vcf.root; } - _light = new Light.Lights(this, _vcf); - IDictionary sirenData = null; - if (data != null && data.TryGetValue(DataNames.Siren, out var rawData)) - { - sirenData = (IDictionary)rawData; - } - _siren = new Siren.Siren(this, _vcf, sirenData, _light); + CreateLightsAndSirens(data); if (cachedNetId == 0) { cachedNetId = _vehicle?.NetworkId ?? 0; } } + + private void CreateLightsAndSirens(IDictionary data = null) + { + _light = new Light.Lights(this, _vcf); + _siren = new Siren.Siren(this, _vcf, _light); + + if (data != null) + { + if (data.TryGetValue(DataNames.Siren, out var sirenData)) + { + _siren.SetData((IDictionary)sirenData); + } + if (data.TryGetValue(DataNames.Light, out var lightData)) + { + _light.SetData((IDictionary)lightData); + } + } + } + private async void ModelLoaded() { if (!_vehicle.Model.IsInCdImage) @@ -204,10 +209,19 @@ internal void RunControlTick() { return; } + if (Game.IsPaused) + { + return; + } + var keyboardState = API.UpdateOnscreenKeyboard(); + if (keyboardState == 0) + { + return; + } _siren.ControlTicker(_light); _light.ControlTicker(); - if (_siren._mainSiren._enable && _light._stage.CurrentStage != 3 && ELS.GameTime - lastAutoDisable > 1000) + if (_siren._mainSiren.Enable && _light._stage.CurrentStage != 3 && ELS.GameTime - lastAutoDisable > 1000) { lastAutoDisable = ELS.GameTime; Utils.DeveloperWriteLine("Disabling siren because stage not 3"); @@ -280,7 +294,7 @@ internal void SyncUi() bool lastExistsValue = false; private Vehicle vehicle; - public override bool Exists() + public bool Exists() { if (lastExists == ELS.GameTime) { @@ -292,7 +306,7 @@ public override bool Exists() public void DisableSiren() { - if (_siren._mainSiren._enable) + if (_siren._mainSiren.Enable) { _siren._mainSiren.SetEnable(false); RemoteEventManager.SendEvent(RemoteEventManager.Commands.MainSiren, this, true); @@ -307,7 +321,7 @@ public void DisableSiren() } } - public override void Delete() + public void Delete() { try { @@ -344,10 +358,6 @@ public int NetworkId } } - internal int GetNetworkId() - { - return NetworkId; - } /// /// Proxies sync data to the lighting and siren sub components /// @@ -377,12 +387,12 @@ public void SetData(IDictionary data) public void SetLightData(IDictionary data) { - _light.SetData(data); + _light.SetDataPartial(data); } public void SetSirenData(IDictionary data) { - _siren.SetData(data); + _siren.SetDataPartial(data); } public Dictionary GetData() @@ -392,6 +402,7 @@ public Dictionary GetData() {DataNames.Siren, _siren.GetData() }, {DataNames.Light, _light.GetData() } }; + return vehDic; } diff --git a/src/configuration/CustomPatterns.cs b/src/configuration/CustomPatterns.cs new file mode 100644 index 0000000..01b62a3 --- /dev/null +++ b/src/configuration/CustomPatterns.cs @@ -0,0 +1,58 @@ +using CitizenFX.Core.Native; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ELS.configuration +{ + public class CustomPatterns + { + internal static List> Patterns = new List>(); + internal static bool IsValidPatternData(string data) + { + if (string.IsNullOrEmpty(data)) return false; + NanoXMLDocument doc = new NanoXMLDocument(data); + + //TODO change how below is detected to account for xml meta tag being before it. + return doc.RootNode.Name == "pattern"; + } + + public static void CheckCustomPatterns() + { + var numResources = API.GetNumResources(); + foreach (string name in VcfSync.ElsResources) + { + ParsePatterns(name); + } + + } + + internal static void ParsePatterns(string name) + { + int num = Function.Call(Hash.GET_NUM_RESOURCE_METADATA, name, "file"); + string isElsResource = API.GetResourceMetadata(name, "is_els", 0); + for (int i = 0; i < num; i++) + { + var filename = Function.Call(Hash.GET_RESOURCE_METADATA, name, "file", i); + var data = Function.Call(Hash.LOAD_RESOURCE_FILE, name, filename); + if (Path.GetExtension(filename).ToLower() == ".xml") + { + try + { + if (CustomPatterns.IsValidPatternData(data)) + { + Patterns.Add(new Tuple(name, filename, data)); + } + } + catch (Exception e) + { + Utils.ReleaseWriteLine($"There was a parsing error in {filename} please validate this XML and try again."); + } + } + } + } + } +} diff --git a/src/configuration/ElsConfiguration.cs b/src/configuration/ElsConfiguration.cs index f3c7e54..1828dc9 100644 --- a/src/configuration/ElsConfiguration.cs +++ b/src/configuration/ElsConfiguration.cs @@ -16,17 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ using CitizenFX.Core; +using Newtonsoft.Json; namespace ELS.configuration { - internal delegate void ControlsUpdatedhandler(ElsConfiguration.ELSKBControls kbControls, ElsConfiguration.ELSGPControls gpControls); internal class ElsConfiguration { - - public static event ControlsUpdatedhandler ControlsUpdated; - public static ELSKBControls KBBindings = new ELSKBControls(); - public static ELSGPControls GPBindings = new ELSGPControls(); - internal ElsConfiguration() { FileLoader.OnSettingsLoaded += FileLoader_OnSettingsLoaded; @@ -37,91 +32,58 @@ private void FileLoader_OnSettingsLoaded(SettingsType.Type type, string Data) if (type == SettingsType.Type.GLOBAL) { var u = SharpConfig.Configuration.LoadFromString(Data); - var t = u["KEYBOARD"]["Sound_Ahorn"].IntValue; - KBBindings.Sound_Ahorn = (Control)t; + Controls.SoundAirHorn.Keyboard = (Control)u["KEYBOARD"]["Sound_Ahorn"].IntValue; - t = u["KEYBOARD"]["Snd_SrnTon1"].IntValue; - KBBindings.Snd_SrnTon1 = (Control)t; + Controls.SoundSirenTone0.Keyboard = (Control)u["KEYBOARD"]["Snd_SrnTon1"].IntValue; - t = u["KEYBOARD"]["Snd_SrnTon2"].IntValue; - KBBindings.Snd_SrnTon2 = (Control)t; + Controls.SoundSirenTone1.Keyboard = (Control)u["KEYBOARD"]["Snd_SrnTon2"].IntValue; - t = u["KEYBOARD"]["Snd_SrnTon3"].IntValue; - KBBindings.Snd_SrnTon3 = (Control)t; + Controls.SoundSirenTone2.Keyboard = (Control)u["KEYBOARD"]["Snd_SrnTon3"].IntValue; - t = u["KEYBOARD"]["Snd_SrnTon4"].IntValue; - KBBindings.Snd_SrnTon4 = (Control)t; + Controls.SoundSirenTone3.Keyboard = (Control)u["KEYBOARD"]["Snd_SrnTon4"].IntValue; - t = u["KEYBOARD"]["Sound_Manul"].IntValue; - KBBindings.Sound_Manul = (Control)t; + Controls.SoundManual.Keyboard = (Control)u["KEYBOARD"]["Sound_Manul"].IntValue; - t = u["KEYBOARD"]["Toggle_SIRN"].IntValue; - KBBindings.Toggle_SIRN = (Control)t; + Controls.ToggleSiren.Keyboard = (Control)u["KEYBOARD"]["Toggle_SIRN"].IntValue; - t = u["KEYBOARD"]["Toggle_DSRN"].IntValue; - KBBindings.Toggle_DSRN = (Control)t; + Controls.ToggleDualSiren.Keyboard = (Control)u["KEYBOARD"]["Toggle_DSRN"].IntValue; - t = u["KEYBOARD"]["TogInfoPanl"].IntValue; - KBBindings.TogInfoPanl = (Control)t; + Controls.ToggleInfoPanel.Keyboard = (Control)u["KEYBOARD"]["TogInfoPanl"].IntValue; - t = u["KEYBOARD"]["Snd_SrnPnic"].IntValue; - KBBindings.Snd_SrnPnic = (Control)t; + Controls.SoundSirenPanic.Keyboard = (Control)u["KEYBOARD"]["Snd_SrnPnic"].IntValue; - t = u["KEYBOARD"]["Toggle_SECL"].IntValue; - KBBindings.ToggleSecL = (Control)t; + Controls.ToggleSecL.Keyboard = (Control)u["KEYBOARD"]["Toggle_SECL"].IntValue; - t = u["KEYBOARD"]["Toggle_WRNL"].IntValue; - KBBindings.ToggleWrnL = (Control)t; + Controls.ToggleWrnL.Keyboard = (Control)u["KEYBOARD"]["Toggle_WRNL"].IntValue; - t = u["KEYBOARD"]["Toggle_CRSL"].IntValue; - KBBindings.ToggleCrsL = (Control)t; + Controls.ToggleCrsL.Keyboard = (Control)u["KEYBOARD"]["Toggle_CRSL"].IntValue; - t = u["KEYBOARD"]["ChgPat_PRML"].IntValue; - KBBindings.ChgPattPrmL = (Control)t; + Controls.ChgPattPrmL.Keyboard = (Control)u["KEYBOARD"]["ChgPat_PRML"].IntValue; - t = u["KEYBOARD"]["ChgPat_SECL"].IntValue; - KBBindings.ChgPattSecL = (Control)t; + Controls.ChgPattSecL.Keyboard = (Control)u["KEYBOARD"]["ChgPat_SECL"].IntValue; - t = u["KEYBOARD"]["ChgPat_WRNL"].IntValue; - KBBindings.ChgPattWrnL = (Control)t; + Controls.ChgPattWrnL.Keyboard = (Control)u["KEYBOARD"]["ChgPat_WRNL"].IntValue; - t = u["KEYBOARD"]["Toggle_LSTG"].IntValue; - KBBindings.ToggleLstg = (Control)t; + Controls.ToggleLstg.Keyboard = (Control)u["KEYBOARD"]["Toggle_LSTG"].IntValue; - t = u["KEYBOARD"]["Toggle_TKDL"].IntValue; - KBBindings.ToggleTdl = (Control)t; + Controls.ToggleTdl.Keyboard = (Control)u["KEYBOARD"]["Toggle_TKDL"].IntValue; - t = u["KEYBOARD"]["Toggle_BRD"].IntValue; - KBBindings.ToggleBoard = (Control)t; + Controls.ToggleBoard.Keyboard = (Control)u["KEYBOARD"]["Toggle_BRD"].IntValue; - t = u["KEYBOARD"]["Toggle_LIND"].IntValue; - KBBindings.ToggleLIND = (Control)t; + Controls.ToggleLIND.Keyboard = (Control)u["KEYBOARD"]["Toggle_LIND"].IntValue; - t = u["KEYBOARD"]["Toggle_RIND"].IntValue; - KBBindings.ToggleRIND = (Control)t; + Controls.ToggleRIND.Keyboard = (Control)u["KEYBOARD"]["Toggle_RIND"].IntValue; - t = u["KEYBOARD"]["Toggle_HAZ"].IntValue; - KBBindings.ToggleHAZ = (Control)t; + Controls.ToggleHAZ.Keyboard = (Control)u["KEYBOARD"]["Toggle_HAZ"].IntValue; //Gamepad - t = u["GAMEPAD"]["Toggle_TKDL"].IntValue; - GPBindings.ToggleTdl = (Control)t; - - t = u["GAMEPAD"]["Sound_Ahorn"].IntValue; - GPBindings.Sound_Ahorn = (Control)t; - - t = u["GAMEPAD"]["Snd_SrnTon1"].IntValue; - GPBindings.Snd_SrnTon1 = (Control)t; - - t = u["GAMEPAD"]["Snd_SrnTon3"].IntValue; - GPBindings.Snd_SrnTon3 = (Control)t; - - t = u["GAMEPAD"]["Toggle_LSTG"].IntValue; - GPBindings.ToggleLstg = (Control)t; - - t = u["GAMEPAD"]["Toggle_SIRN"].IntValue; - GPBindings.Toggle_SIRN = (Control)t; + Controls.ToggleTdl.Gamepad = (Control)u["GAMEPAD"]["Toggle_TKDL"].IntValue; + Controls.SoundAirHorn.Gamepad = (Control)u["GAMEPAD"]["Sound_Ahorn"].IntValue; + Controls.SoundSirenTone0.Gamepad = (Control)u["GAMEPAD"]["Snd_SrnTon1"].IntValue; + Controls.SoundSirenTone1.Gamepad = (Control)u["GAMEPAD"]["Snd_SrnTon3"].IntValue; + Controls.ToggleLstg.Gamepad = (Control)u["GAMEPAD"]["Toggle_LSTG"].IntValue; + Controls.ToggleSiren.Gamepad = (Control)u["GAMEPAD"]["Toggle_SIRN"].IntValue; //ControlsUpdated?.Invoke(KeyBindings); @@ -146,40 +108,115 @@ private void FileLoader_OnSettingsLoaded(SettingsType.Type type, string Data) #endif } } - internal class ELSKBControls + + internal class ELSControl { - internal Control ToggleTdl { get; set; } - internal Control Toggle_SIRN { get; set; } - internal Control Sound_Ahorn { get; set; } - internal Control Snd_SrnTon1 { get; set; } - internal Control Snd_SrnTon2 { get; set; } - internal Control Snd_SrnTon3 { get; set; } - internal Control Snd_SrnTon4 { get; set; } - internal Control Snd_SrnPnic { get; set; } - internal Control Sound_Manul { get; set; } - internal Control Toggle_DSRN { get; set; } - internal Control TogInfoPanl { get; set; } - internal Control ToggleBoard { get; set; } - internal Control ToggleSecL { get; set; } - internal Control ToggleWrnL { get; set; } - internal Control ToggleCrsL { get; set; } - internal Control ChgPattPrmL { get; set; } - internal Control ChgPattSecL { get; set; } - internal Control ChgPattWrnL { get; set; } - internal Control ToggleLstg { get; set; } - internal Control ToggleLIND { get; set; } - internal Control ToggleRIND { get; set; } - internal Control ToggleHAZ { get; set; } + internal Control? Keyboard { get; set; } + internal Control? Gamepad { get; set; } + + public Control? GetRelevantControl() + { + if (Global.AllowController && Gamepad != null && ELS.CurrentInputMode == InputMode.GamePad) + { + return Gamepad.Value; + } + + if (Keyboard != null && ELS.CurrentInputMode == InputMode.MouseAndKeyboard) + { + return Keyboard.Value; + } + + return null; + } + + public bool IsPressed() + { + var control = GetRelevantControl(); + if (control == null) { return false; } + + return Game.IsControlPressed(0, control.Value); + } + + public bool IsDisabledPressed() + { + var control = GetRelevantControl(); + if (control == null) { return false; } + + return Game.IsDisabledControlPressed(0, control.Value); + } + + public bool IsJustReleased() + { + var control = GetRelevantControl(); + if (control == null) { return false; } + + return Game.IsControlJustReleased(0, control.Value); + } + + public bool IsDisabledJustReleased() + { + var control = GetRelevantControl(); + if (control == null) { return false; } + + return Game.IsDisabledControlJustReleased(0, control.Value); + } + + public bool IsJustPressed() + { + var control = GetRelevantControl(); + if (control == null) { return false; } + + return Game.IsControlJustPressed(0, control.Value); + } + + public bool IsDisabledJustPressed() + { + var control = GetRelevantControl(); + if (control == null) { return false; } + + return Game.IsDisabledControlJustPressed(0, control.Value); + } + + public void Disable() + { + var control = GetRelevantControl(); + if (control == null) { return; } + Game.DisableControlThisFrame(0, control.Value); + } + + public bool IsEnabled() + { + var control = GetRelevantControl(); + if (control == null) { return false; } + + return Game.IsControlEnabled(0, control.Value); + } } - internal class ELSGPControls + internal static class Controls { - internal Control ToggleTdl { get; set; } - internal Control Toggle_SIRN { get; set; } - internal Control Sound_Ahorn { get; set; } - internal Control Snd_SrnTon1 { get; set; } - internal Control Snd_SrnTon3 { get; set; } - internal Control ToggleLstg { get; set; } + internal static ELSControl ToggleTdl { get; set; } = new ELSControl(); + internal static ELSControl ToggleSiren { get; set; } = new ELSControl(); + internal static ELSControl SoundSirenTone0 { get; set; } = new ELSControl(); + internal static ELSControl SoundAirHorn { get; set; } = new ELSControl(); + internal static ELSControl SoundSirenTone1 { get; set; } = new ELSControl(); + internal static ELSControl SoundSirenTone2 { get; set; } = new ELSControl(); + internal static ELSControl SoundSirenTone3 { get; set; } = new ELSControl(); + internal static ELSControl SoundSirenPanic { get; set; } = new ELSControl(); + internal static ELSControl SoundManual { get; set; } = new ELSControl(); + internal static ELSControl ToggleDualSiren { get; set; } = new ELSControl(); + internal static ELSControl ToggleInfoPanel { get; set; } = new ELSControl(); + internal static ELSControl ToggleBoard { get; set; } = new ELSControl(); + internal static ELSControl ToggleSecL { get; set; } = new ELSControl(); + internal static ELSControl ToggleWrnL { get; set; } = new ELSControl(); + internal static ELSControl ToggleCrsL { get; set; } = new ELSControl(); + internal static ELSControl ChgPattPrmL { get; set; } = new ELSControl(); + internal static ELSControl ChgPattSecL { get; set; } = new ELSControl(); + internal static ELSControl ChgPattWrnL { get; set; } = new ELSControl(); + internal static ELSControl ToggleLstg { get; set; } = new ELSControl(); + internal static ELSControl ToggleLIND { get; set; } = new ELSControl(); + internal static ELSControl ToggleRIND { get; set; } = new ELSControl(); + internal static ELSControl ToggleHAZ { get; set; } = new ELSControl(); } internal static bool isValidData(string data) diff --git a/src/configuration/VCF.cs b/src/configuration/VCF.cs index db7ce71..d7de368 100644 --- a/src/configuration/VCF.cs +++ b/src/configuration/VCF.cs @@ -48,13 +48,13 @@ public VCF() { } - internal static async void ParseVcfs(List VcfData) + internal static async void ParseVcfs(List> VcfData) { - foreach (object obj in VcfData) + foreach (var obj in VcfData) { - var vcf = obj as IDictionary; + var vcf = obj; await BaseScript.Delay(50); - load(SettingsType.Type.VCF, vcf["Item2"] as string, vcf["Item3"] as string, vcf["Item1"] as string); + load(SettingsType.Type.VCF, vcf.Item2, vcf.Item3, vcf.Item1); } ELS.Loaded = true; } @@ -140,76 +140,31 @@ static void load(SettingsType.Type type, string name, string Data, string Resour switch (n.Name) { case "Extra01": - data.root.EOVERRIDE.Extra01.IsElsControlled = bool.Parse(n.GetAttribute("IsElsControlled").Value); - data.root.EOVERRIDE.Extra01.AllowEnvLight = bool.Parse(n.GetAttribute("AllowEnvLight").Value); - data.root.EOVERRIDE.Extra01.Color = n.GetAttribute("Color").Value; - data.root.EOVERRIDE.Extra01.OffsetX = float.Parse(n.GetAttribute("OffsetX").Value); - data.root.EOVERRIDE.Extra01.OffsetY = float.Parse(n.GetAttribute("OffsetY").Value); - data.root.EOVERRIDE.Extra01.OffsetZ = float.Parse(n.GetAttribute("OffsetZ").Value); + data.root.EOVERRIDE.Extra01.SetData(n); break; case "Extra02": - data.root.EOVERRIDE.Extra02.IsElsControlled = bool.Parse(n.GetAttribute("IsElsControlled").Value); - data.root.EOVERRIDE.Extra02.AllowEnvLight = bool.Parse(n.GetAttribute("AllowEnvLight").Value); - data.root.EOVERRIDE.Extra02.Color = n.GetAttribute("Color").Value; - data.root.EOVERRIDE.Extra02.OffsetX = float.Parse(n.GetAttribute("OffsetX").Value); - data.root.EOVERRIDE.Extra02.OffsetY = float.Parse(n.GetAttribute("OffsetY").Value); - data.root.EOVERRIDE.Extra02.OffsetZ = float.Parse(n.GetAttribute("OffsetZ").Value); + data.root.EOVERRIDE.Extra02.SetData(n); break; case "Extra03": - data.root.EOVERRIDE.Extra03.IsElsControlled = bool.Parse(n.GetAttribute("IsElsControlled").Value); - data.root.EOVERRIDE.Extra03.AllowEnvLight = bool.Parse(n.GetAttribute("AllowEnvLight").Value); - data.root.EOVERRIDE.Extra03.Color = n.GetAttribute("Color").Value; - data.root.EOVERRIDE.Extra03.OffsetX = float.Parse(n.GetAttribute("OffsetX").Value); - data.root.EOVERRIDE.Extra03.OffsetY = float.Parse(n.GetAttribute("OffsetY").Value); - data.root.EOVERRIDE.Extra03.OffsetZ = float.Parse(n.GetAttribute("OffsetZ").Value); + data.root.EOVERRIDE.Extra03.SetData(n); break; case "Extra04": - data.root.EOVERRIDE.Extra04.IsElsControlled = bool.Parse(n.GetAttribute("IsElsControlled").Value); - data.root.EOVERRIDE.Extra04.AllowEnvLight = bool.Parse(n.GetAttribute("AllowEnvLight").Value); - data.root.EOVERRIDE.Extra04.Color = n.GetAttribute("Color").Value; - data.root.EOVERRIDE.Extra04.OffsetX = float.Parse(n.GetAttribute("OffsetX").Value); - data.root.EOVERRIDE.Extra04.OffsetY = float.Parse(n.GetAttribute("OffsetY").Value); - data.root.EOVERRIDE.Extra04.OffsetZ = float.Parse(n.GetAttribute("OffsetZ").Value); + data.root.EOVERRIDE.Extra04.SetData(n); break; case "Extra05": - data.root.EOVERRIDE.Extra05.IsElsControlled = bool.Parse(n.GetAttribute("IsElsControlled").Value); - data.root.EOVERRIDE.Extra05.AllowEnvLight = bool.Parse(n.GetAttribute("AllowEnvLight").Value); - data.root.EOVERRIDE.Extra05.Color = n.GetAttribute("Color").Value; - data.root.EOVERRIDE.Extra05.OffsetX = float.Parse(n.GetAttribute("OffsetX").Value); - data.root.EOVERRIDE.Extra05.OffsetY = float.Parse(n.GetAttribute("OffsetY").Value); - data.root.EOVERRIDE.Extra05.OffsetZ = float.Parse(n.GetAttribute("OffsetZ").Value); + data.root.EOVERRIDE.Extra05.SetData(n); break; case "Extra06": - data.root.EOVERRIDE.Extra06.IsElsControlled = bool.Parse(n.GetAttribute("IsElsControlled").Value); - data.root.EOVERRIDE.Extra06.AllowEnvLight = bool.Parse(n.GetAttribute("AllowEnvLight").Value); - data.root.EOVERRIDE.Extra06.Color = n.GetAttribute("Color").Value; - data.root.EOVERRIDE.Extra06.OffsetX = float.Parse(n.GetAttribute("OffsetX").Value); - data.root.EOVERRIDE.Extra06.OffsetY = float.Parse(n.GetAttribute("OffsetY").Value); - data.root.EOVERRIDE.Extra06.OffsetZ = float.Parse(n.GetAttribute("OffsetZ").Value); + data.root.EOVERRIDE.Extra06.SetData(n); break; case "Extra07": - data.root.EOVERRIDE.Extra07.IsElsControlled = bool.Parse(n.GetAttribute("IsElsControlled").Value); - data.root.EOVERRIDE.Extra07.AllowEnvLight = bool.Parse(n.GetAttribute("AllowEnvLight").Value); - data.root.EOVERRIDE.Extra07.Color = n.GetAttribute("Color").Value; - data.root.EOVERRIDE.Extra07.OffsetX = float.Parse(n.GetAttribute("OffsetX").Value); - data.root.EOVERRIDE.Extra07.OffsetY = float.Parse(n.GetAttribute("OffsetY").Value); - data.root.EOVERRIDE.Extra07.OffsetZ = float.Parse(n.GetAttribute("OffsetZ").Value); + data.root.EOVERRIDE.Extra07.SetData(n); break; case "Extra08": - data.root.EOVERRIDE.Extra08.IsElsControlled = bool.Parse(n.GetAttribute("IsElsControlled").Value); - data.root.EOVERRIDE.Extra08.AllowEnvLight = bool.Parse(n.GetAttribute("AllowEnvLight").Value); - data.root.EOVERRIDE.Extra08.Color = n.GetAttribute("Color").Value; - data.root.EOVERRIDE.Extra08.OffsetX = float.Parse(n.GetAttribute("OffsetX").Value); - data.root.EOVERRIDE.Extra08.OffsetY = float.Parse(n.GetAttribute("OffsetY").Value); - data.root.EOVERRIDE.Extra08.OffsetZ = float.Parse(n.GetAttribute("OffsetZ").Value); + data.root.EOVERRIDE.Extra08.SetData(n); break; case "Extra09": - data.root.EOVERRIDE.Extra09.IsElsControlled = bool.Parse(n.GetAttribute("IsElsControlled").Value); - data.root.EOVERRIDE.Extra09.AllowEnvLight = bool.Parse(n.GetAttribute("AllowEnvLight").Value); - data.root.EOVERRIDE.Extra09.Color = n.GetAttribute("Color").Value; - data.root.EOVERRIDE.Extra09.OffsetX = float.Parse(n.GetAttribute("OffsetX").Value); - data.root.EOVERRIDE.Extra09.OffsetY = float.Parse(n.GetAttribute("OffsetY").Value); - data.root.EOVERRIDE.Extra09.OffsetZ = float.Parse(n.GetAttribute("OffsetZ").Value); + data.root.EOVERRIDE.Extra08.SetData(n); break; case "Extra10": data.root.EOVERRIDE.Extra10.IsElsControlled = bool.Parse(n.GetAttribute("IsElsControlled").Value); @@ -332,50 +287,6 @@ static void load(SettingsType.Type type, string name, string Data, string Resour } #endregion -#region Aux Coronas -#if DEBUG - Utils.DebugWriteLine("Parsing Aux Coronas"); -#endif - try - { - foreach (NanoXMLNode n in subNodes["ACORONAS"].SubNodes) - { - switch (n.Name) - { - case "Headlights": - data.root.ACORONAS.Headlights.DfltPattern = int.Parse(n.GetAttribute("DfltPattern").Value); - data.root.ACORONAS.Headlights.ColorL = n.GetAttribute("ColorL").Value; - data.root.ACORONAS.Headlights.ColorR = n.GetAttribute("ColorR").Value; - break; - case "TailLights": - data.root.ACORONAS.TailLights.DfltPattern = int.Parse(n.GetAttribute("DfltPattern").Value); - data.root.ACORONAS.TailLights.ColorL = n.GetAttribute("ColorL").Value; - data.root.ACORONAS.TailLights.ColorR = n.GetAttribute("ColorR").Value; - break; - case "IndicatorsF": - data.root.ACORONAS.IndicatorsF.DfltPattern = int.Parse(n.GetAttribute("DfltPattern").Value); - data.root.ACORONAS.IndicatorsF.ColorL = n.GetAttribute("ColorL").Value; - data.root.ACORONAS.IndicatorsF.ColorR = n.GetAttribute("ColorR").Value; - break; - case "IndicatorsB": - data.root.ACORONAS.IndicatorsB.DfltPattern = int.Parse(n.GetAttribute("DfltPattern").Value); - data.root.ACORONAS.IndicatorsB.ColorL = n.GetAttribute("ColorL").Value; - data.root.ACORONAS.IndicatorsB.ColorR = n.GetAttribute("ColorR").Value; - break; - case "ReverseLights": - data.root.ACORONAS.ReverseLights.DfltPattern = int.Parse(n.GetAttribute("DfltPattern").Value); - data.root.ACORONAS.ReverseLights.ColorL = n.GetAttribute("ColorL").Value; - data.root.ACORONAS.ReverseLights.ColorR = n.GetAttribute("ColorR").Value; - break; - } - } - } - catch (Exception e) - { - Utils.ReleaseWriteLine($"ACoronas for {name} failed to parse due to {e.Message} with inner of {e.InnerException}"); - } -#endregion - #region Sounds #if DEBUG Utils.DebugWriteLine("Parsing Sounds"); @@ -387,49 +298,31 @@ static void load(SettingsType.Type type, string name, string Data, string Resour switch (n.Name) { case "ManTone1": - data.root.SOUNDS.ManTone1.AudioString = n.GetAttribute("AudioString")?.Value; - data.root.SOUNDS.ManTone1.AllowUse = bool.Parse(n.GetAttribute("AllowUse")?.Value ?? "false"); - data.root.SOUNDS.ManTone1.SoundSet = n.GetAttribute("SoundSet")?.Value; + data.root.SOUNDS.ManTone1.SetData(n); break; case "ManTone2": - data.root.SOUNDS.ManTone2.AudioString = n.GetAttribute("AudioString")?.Value; - data.root.SOUNDS.ManTone2.AllowUse = bool.Parse(n.GetAttribute("AllowUse")?.Value); - data.root.SOUNDS.ManTone2.SoundSet = n.GetAttribute("SoundSet")?.Value; + data.root.SOUNDS.ManTone2.SetData(n); break; case "MainHorn": - data.root.SOUNDS.MainHorn.AudioString = n.GetAttribute("AudioString")?.Value; - data.root.SOUNDS.MainHorn.InterruptsSiren = bool.Parse(n.GetAttribute("InterruptsSiren")?.Value ?? "false"); - data.root.SOUNDS.MainHorn.SoundSet = n.GetAttribute("SoundSet")?.Value; + data.root.SOUNDS.MainHorn.SetData(n); break; case "SrnTone1": - data.root.SOUNDS.SrnTone1.AudioString = n.GetAttribute("AudioString")?.Value; - data.root.SOUNDS.SrnTone1.AllowUse = bool.Parse(n.GetAttribute("AllowUse")?.Value ?? "false"); - data.root.SOUNDS.SrnTone1.SoundSet = n.GetAttribute("SoundSet")?.Value; + data.root.SOUNDS.SrnTone1.SetData(n); break; case "SrnTone2": - data.root.SOUNDS.SrnTone2.AudioString = n.GetAttribute("AudioString")?.Value; - data.root.SOUNDS.SrnTone2.AllowUse = bool.Parse(n.GetAttribute("AllowUse")?.Value ?? "false"); - data.root.SOUNDS.SrnTone2.SoundSet = n.GetAttribute("SoundSet")?.Value; + data.root.SOUNDS.SrnTone2.SetData(n); break; case "SrnTone3": - data.root.SOUNDS.SrnTone3.AudioString = n.GetAttribute("AudioString")?.Value; - data.root.SOUNDS.SrnTone3.AllowUse = bool.Parse(n.GetAttribute("AllowUse")?.Value ?? "false"); - data.root.SOUNDS.SrnTone3.SoundSet = n.GetAttribute("SoundSet")?.Value; + data.root.SOUNDS.SrnTone3.SetData(n); break; case "SrnTone4": - data.root.SOUNDS.SrnTone4.AudioString = n.GetAttribute("AudioString")?.Value; - data.root.SOUNDS.SrnTone4.AllowUse = bool.Parse(n.GetAttribute("AllowUse")?.Value ?? "false"); - data.root.SOUNDS.SrnTone4.SoundSet = n.GetAttribute("SoundSet")?.Value; + data.root.SOUNDS.SrnTone4.SetData(n); break; case "AuxSiren": - data.root.SOUNDS.AuxSiren.AllowUse = bool.Parse(n.GetAttribute("AllowUse")?.Value ?? "false"); - data.root.SOUNDS.AuxSiren.AudioString = n.GetAttribute("AudioString")?.Value; - data.root.SOUNDS.AuxSiren.SoundSet = n.GetAttribute("SoundSet")?.Value; + data.root.SOUNDS.AuxSiren.SetData(n); break; case "PanicMde": - data.root.SOUNDS.PanicMde.AllowUse = bool.Parse(n.GetAttribute("AllowUse")?.Value ?? "false"); - data.root.SOUNDS.PanicMde.AudioString = n.GetAttribute("AudioString")?.Value; - data.root.SOUNDS.PanicMde.SoundSet = n.GetAttribute("SoundSet")?.Value; + data.root.SOUNDS.PanicMde.SetData(n); break; } @@ -469,28 +362,13 @@ static void load(SettingsType.Type type, string name, string Data, string Resour switch (sn.Name) { case "Lstg1": - data.root.WRNL.PresetPatterns.Lstg1.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.WRNL.PresetPatterns.Lstg1.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.WRNL.PresetPatterns.Lstg1.Pattern.ToLower().Equals("scan")) - { - data.root.WRNL.PresetPatterns.Lstg1.IntPattern = int.Parse(data.root.WRNL.PresetPatterns.Lstg1.Pattern); - } + data.root.WRNL.PresetPatterns.Lstg1.SetData(sn); break; case "Lstg2": - data.root.WRNL.PresetPatterns.Lstg2.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.WRNL.PresetPatterns.Lstg2.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.WRNL.PresetPatterns.Lstg2.Pattern.ToLower().Equals("scan")) - { - data.root.WRNL.PresetPatterns.Lstg2.IntPattern = int.Parse(data.root.WRNL.PresetPatterns.Lstg2.Pattern); - } + data.root.WRNL.PresetPatterns.Lstg2.SetData(sn); break; case "Lstg3": - data.root.WRNL.PresetPatterns.Lstg3.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.WRNL.PresetPatterns.Lstg3.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.WRNL.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan")) - { - data.root.WRNL.PresetPatterns.Lstg3.IntPattern = int.Parse(data.root.WRNL.PresetPatterns.Lstg3.Pattern); - } + data.root.WRNL.PresetPatterns.Lstg3.SetData(sn); break; } } @@ -501,68 +379,28 @@ static void load(SettingsType.Type type, string name, string Data, string Resour switch (sn.Name) { case "MainHorn": - data.root.WRNL.ForcedPatterns.MainHorn.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.WRNL.ForcedPatterns.MainHorn.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.WRNL.ForcedPatterns.MainHorn.Pattern.ToLower().Equals("scan")) - { - data.root.WRNL.ForcedPatterns.MainHorn.IntPattern = int.Parse(data.root.WRNL.ForcedPatterns.MainHorn.Pattern); - } + data.root.WRNL.ForcedPatterns.MainHorn.SetData(sn); break; case "SrnTone1": - data.root.WRNL.ForcedPatterns.SrnTone1.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.WRNL.ForcedPatterns.SrnTone1.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.WRNL.ForcedPatterns.SrnTone1.Pattern.ToLower().Equals("scan")) - { - data.root.WRNL.ForcedPatterns.SrnTone1.IntPattern = int.Parse(data.root.WRNL.ForcedPatterns.SrnTone1.Pattern); - } + data.root.WRNL.ForcedPatterns.SrnTone1.SetData(sn); break; case "SrnTone2": - data.root.WRNL.ForcedPatterns.SrnTone2.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.WRNL.ForcedPatterns.SrnTone2.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.WRNL.ForcedPatterns.SrnTone2.Pattern.ToLower().Equals("scan")) - { - data.root.WRNL.ForcedPatterns.SrnTone2.IntPattern = int.Parse(data.root.WRNL.ForcedPatterns.SrnTone2.Pattern); - } + data.root.WRNL.ForcedPatterns.SrnTone2.SetData(sn); break; case "SrnTone3": - data.root.WRNL.ForcedPatterns.SrnTone3.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.WRNL.ForcedPatterns.SrnTone3.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.WRNL.ForcedPatterns.SrnTone3.Pattern.ToLower().Equals("scan")) - { - data.root.WRNL.ForcedPatterns.SrnTone3.IntPattern = int.Parse(data.root.WRNL.ForcedPatterns.SrnTone3.Pattern); - } + data.root.WRNL.ForcedPatterns.SrnTone3.SetData(sn); break; case "SrnTone4": - data.root.WRNL.ForcedPatterns.SrnTone4.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.WRNL.ForcedPatterns.SrnTone4.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.WRNL.ForcedPatterns.SrnTone4.Pattern.ToLower().Equals("scan")) - { - data.root.WRNL.ForcedPatterns.SrnTone4.IntPattern = int.Parse(data.root.WRNL.ForcedPatterns.SrnTone4.Pattern); - } + data.root.WRNL.ForcedPatterns.SrnTone4.SetData(sn); break; case "AuxSiren": - data.root.WRNL.ForcedPatterns.AuxSiren.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.WRNL.ForcedPatterns.AuxSiren.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.WRNL.ForcedPatterns.AuxSiren.Pattern.ToLower().Equals("scan")) - { - data.root.WRNL.ForcedPatterns.AuxSiren.IntPattern = int.Parse(data.root.WRNL.ForcedPatterns.AuxSiren.Pattern); - } + data.root.WRNL.ForcedPatterns.AuxSiren.SetData(sn); break; case "PanicMde": - data.root.WRNL.ForcedPatterns.PanicMde.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.WRNL.ForcedPatterns.PanicMde.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.WRNL.ForcedPatterns.PanicMde.Pattern.ToLower().Equals("scan")) - { - data.root.WRNL.ForcedPatterns.PanicMde.IntPattern = int.Parse(data.root.WRNL.ForcedPatterns.PanicMde.Pattern); - } + data.root.WRNL.ForcedPatterns.PanicMde.SetData(sn); break; case "OutOfVeh": - data.root.WRNL.ForcedPatterns.OutOfVeh.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.WRNL.ForcedPatterns.OutOfVeh.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.WRNL.ForcedPatterns.OutOfVeh.Pattern.ToLower().Equals("scan")) - { - data.root.WRNL.ForcedPatterns.OutOfVeh.IntPattern = int.Parse(data.root.WRNL.ForcedPatterns.OutOfVeh.Pattern); - } + data.root.WRNL.ForcedPatterns.OutOfVeh.SetData(sn); break; } } @@ -610,28 +448,13 @@ static void load(SettingsType.Type type, string name, string Data, string Resour switch (sn.Name) { case "Lstg1": - data.root.PRML.PresetPatterns.Lstg1.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.PRML.PresetPatterns.Lstg1.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.PRML.PresetPatterns.Lstg1.Pattern.ToLower().Equals("scan")) - { - data.root.PRML.PresetPatterns.Lstg1.IntPattern = int.Parse(data.root.PRML.PresetPatterns.Lstg1.Pattern); - } + data.root.PRML.PresetPatterns.Lstg1.SetData(sn); break; case "Lstg2": - data.root.PRML.PresetPatterns.Lstg2.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.PRML.PresetPatterns.Lstg2.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.PRML.PresetPatterns.Lstg2.Pattern.ToLower().Equals("scan")) - { - data.root.PRML.PresetPatterns.Lstg2.IntPattern = int.Parse(data.root.PRML.PresetPatterns.Lstg2.Pattern); - } + data.root.PRML.PresetPatterns.Lstg2.SetData(sn); break; case "Lstg3": - data.root.PRML.PresetPatterns.Lstg3.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.PRML.PresetPatterns.Lstg3.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.PRML.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan")) - { - data.root.PRML.PresetPatterns.Lstg3.IntPattern = int.Parse(data.root.PRML.PresetPatterns.Lstg3.Pattern); - } + data.root.PRML.PresetPatterns.Lstg3.SetData(sn); break; } } @@ -642,68 +465,28 @@ static void load(SettingsType.Type type, string name, string Data, string Resour switch (sn.Name) { case "MainHorn": - data.root.PRML.ForcedPatterns.MainHorn.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.PRML.ForcedPatterns.MainHorn.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.PRML.ForcedPatterns.MainHorn.Pattern.ToLower().Equals("scan")) - { - data.root.PRML.ForcedPatterns.MainHorn.IntPattern = int.Parse(data.root.WRNL.ForcedPatterns.MainHorn.Pattern); - } + data.root.PRML.ForcedPatterns.MainHorn.SetData(sn); break; case "SrnTone1": - data.root.PRML.ForcedPatterns.SrnTone1.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.PRML.ForcedPatterns.SrnTone1.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.PRML.ForcedPatterns.SrnTone1.Pattern.ToLower().Equals("scan")) - { - data.root.PRML.ForcedPatterns.SrnTone1.IntPattern = int.Parse(data.root.PRML.ForcedPatterns.SrnTone1.Pattern); - } + data.root.PRML.ForcedPatterns.SrnTone1.SetData(sn); break; case "SrnTone2": - data.root.PRML.ForcedPatterns.SrnTone2.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.PRML.ForcedPatterns.SrnTone2.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.PRML.ForcedPatterns.SrnTone2.Pattern.ToLower().Equals("scan")) - { - data.root.PRML.ForcedPatterns.SrnTone2.IntPattern = int.Parse(data.root.PRML.ForcedPatterns.SrnTone2.Pattern); - } + data.root.PRML.ForcedPatterns.SrnTone2.SetData(sn); break; case "SrnTone3": - data.root.PRML.ForcedPatterns.SrnTone3.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.PRML.ForcedPatterns.SrnTone3.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.PRML.ForcedPatterns.SrnTone3.Pattern.ToLower().Equals("scan")) - { - data.root.PRML.ForcedPatterns.SrnTone3.IntPattern = int.Parse(data.root.PRML.ForcedPatterns.SrnTone3.Pattern); - } + data.root.PRML.ForcedPatterns.SrnTone3.SetData(sn); break; case "SrnTone4": - data.root.PRML.ForcedPatterns.SrnTone4.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.PRML.ForcedPatterns.SrnTone4.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.PRML.ForcedPatterns.SrnTone4.Pattern.ToLower().Equals("scan")) - { - data.root.PRML.ForcedPatterns.SrnTone4.IntPattern = int.Parse(data.root.PRML.ForcedPatterns.SrnTone4.Pattern); - } + data.root.PRML.ForcedPatterns.SrnTone4.SetData(sn); break; case "AuxSiren": - data.root.PRML.ForcedPatterns.AuxSiren.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.PRML.ForcedPatterns.AuxSiren.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.PRML.ForcedPatterns.AuxSiren.Pattern.ToLower().Equals("scan")) - { - data.root.PRML.ForcedPatterns.AuxSiren.IntPattern = int.Parse(data.root.PRML.ForcedPatterns.MainHorn.Pattern); - } + data.root.PRML.ForcedPatterns.AuxSiren.SetData(sn); break; case "PanicMde": - data.root.PRML.ForcedPatterns.PanicMde.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.PRML.ForcedPatterns.PanicMde.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.PRML.ForcedPatterns.PanicMde.Pattern.ToLower().Equals("scan")) - { - data.root.PRML.ForcedPatterns.PanicMde.IntPattern = int.Parse(data.root.PRML.ForcedPatterns.PanicMde.Pattern); - } + data.root.PRML.ForcedPatterns.PanicMde.SetData(sn); break; case "OutOfVeh": - data.root.PRML.ForcedPatterns.OutOfVeh.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.PRML.ForcedPatterns.OutOfVeh.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.PRML.ForcedPatterns.OutOfVeh.Pattern.ToLower().Equals("scan")) - { - data.root.PRML.ForcedPatterns.OutOfVeh.IntPattern = int.Parse(data.root.PRML.ForcedPatterns.OutOfVeh.Pattern); - } + data.root.PRML.ForcedPatterns.OutOfVeh.SetData(sn); break; } } @@ -750,28 +533,13 @@ static void load(SettingsType.Type type, string name, string Data, string Resour switch (sn.Name) { case "Lstg1": - data.root.SECL.PresetPatterns.Lstg1.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.SECL.PresetPatterns.Lstg1.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.SECL.PresetPatterns.Lstg1.Pattern.ToLower().Equals("scan")) - { - data.root.SECL.PresetPatterns.Lstg1.IntPattern = int.Parse(data.root.SECL.PresetPatterns.Lstg1.Pattern); - } + data.root.SECL.PresetPatterns.Lstg1.SetData(sn); break; case "Lstg2": - data.root.SECL.PresetPatterns.Lstg2.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.SECL.PresetPatterns.Lstg2.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.SECL.PresetPatterns.Lstg2.Pattern.ToLower().Equals("scan")) - { - data.root.SECL.PresetPatterns.Lstg2.IntPattern = int.Parse(data.root.SECL.PresetPatterns.Lstg2.Pattern); - } + data.root.SECL.PresetPatterns.Lstg2.SetData(sn); break; case "Lstg3": - data.root.SECL.PresetPatterns.Lstg3.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.SECL.PresetPatterns.Lstg3.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.SECL.PresetPatterns.Lstg3.Pattern.ToLower().Equals("scan")) - { - data.root.SECL.PresetPatterns.Lstg3.IntPattern = int.Parse(data.root.SECL.PresetPatterns.Lstg3.Pattern); - } + data.root.SECL.PresetPatterns.Lstg3.SetData(sn); break; } } @@ -782,68 +550,28 @@ static void load(SettingsType.Type type, string name, string Data, string Resour switch (sn.Name) { case "MainHorn": - data.root.SECL.ForcedPatterns.MainHorn.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.SECL.ForcedPatterns.MainHorn.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.SECL.ForcedPatterns.MainHorn.Pattern.ToLower().Equals("scan")) - { - data.root.SECL.ForcedPatterns.MainHorn.IntPattern = int.Parse(data.root.SECL.ForcedPatterns.MainHorn.Pattern); - } + data.root.SECL.ForcedPatterns.MainHorn.SetData(sn); break; case "SrnTone1": - data.root.SECL.ForcedPatterns.SrnTone1.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.SECL.ForcedPatterns.SrnTone1.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.SECL.ForcedPatterns.SrnTone1.Pattern.ToLower().Equals("scan")) - { - data.root.SECL.ForcedPatterns.SrnTone1.IntPattern = int.Parse(data.root.SECL.ForcedPatterns.SrnTone1.Pattern); - } + data.root.SECL.ForcedPatterns.SrnTone1.SetData(sn); break; case "SrnTone2": - data.root.SECL.ForcedPatterns.SrnTone2.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.SECL.ForcedPatterns.SrnTone2.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.SECL.ForcedPatterns.SrnTone2.Pattern.ToLower().Equals("scan")) - { - data.root.SECL.ForcedPatterns.SrnTone2.IntPattern = int.Parse(data.root.SECL.ForcedPatterns.SrnTone2.Pattern); - } + data.root.SECL.ForcedPatterns.SrnTone2.SetData(sn); break; case "SrnTone3": - data.root.SECL.ForcedPatterns.SrnTone3.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.SECL.ForcedPatterns.SrnTone3.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.SECL.ForcedPatterns.SrnTone3.Pattern.ToLower().Equals("scan")) - { - data.root.SECL.ForcedPatterns.SrnTone3.IntPattern = int.Parse(data.root.SECL.ForcedPatterns.SrnTone3.Pattern); - } + data.root.SECL.ForcedPatterns.SrnTone3.SetData(sn); break; case "SrnTone4": - data.root.SECL.ForcedPatterns.SrnTone4.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.SECL.ForcedPatterns.SrnTone4.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.SECL.ForcedPatterns.SrnTone4.Pattern.ToLower().Equals("scan")) - { - data.root.SECL.ForcedPatterns.SrnTone4.IntPattern = int.Parse(data.root.SECL.ForcedPatterns.SrnTone4.Pattern); - } + data.root.SECL.ForcedPatterns.SrnTone4.SetData(sn); break; case "AuxSiren": - data.root.SECL.ForcedPatterns.AuxSiren.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.SECL.ForcedPatterns.AuxSiren.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.SECL.ForcedPatterns.AuxSiren.Pattern.ToLower().Equals("scan")) - { - data.root.SECL.ForcedPatterns.AuxSiren.IntPattern = int.Parse(data.root.SECL.ForcedPatterns.AuxSiren.Pattern); - } + data.root.SECL.ForcedPatterns.AuxSiren.SetData(sn); break; case "PanicMde": - data.root.SECL.ForcedPatterns.PanicMde.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.SECL.ForcedPatterns.PanicMde.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.SECL.ForcedPatterns.PanicMde.Pattern.ToLower().Equals("scan")) - { - data.root.SECL.ForcedPatterns.PanicMde.IntPattern = int.Parse(data.root.SECL.ForcedPatterns.PanicMde.Pattern); - } + data.root.SECL.ForcedPatterns.PanicMde.SetData(sn); break; case "OutOfVeh": - data.root.SECL.ForcedPatterns.OutOfVeh.Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); - data.root.SECL.ForcedPatterns.OutOfVeh.Pattern = sn.GetAttribute("Pattern").Value; - if (!data.root.SECL.ForcedPatterns.OutOfVeh.Pattern.ToLower().Equals("scan")) - { - data.root.SECL.ForcedPatterns.OutOfVeh.IntPattern = int.Parse(data.root.SECL.ForcedPatterns.OutOfVeh.Pattern); - } + data.root.SECL.ForcedPatterns.OutOfVeh.SetData(sn); break; } } @@ -887,16 +615,16 @@ internal static void unload(string hash) #endif } - internal async static void ParsePatterns(List patterns) + internal async static void ParsePatterns(List> patterns) { - foreach (dynamic patt in patterns) + foreach (var patt in patterns) { await BaseScript.Delay(0); LoadCustomPattern(patt); } } - static void LoadCustomPattern(dynamic pattData) + static void LoadCustomPattern(Tuple pattData) { Light.Patterns.CustomPattern pattern = new Light.Patterns.CustomPattern(150, 200, 200, new Dictionary()); NanoXMLDocument doc = new NanoXMLDocument(pattData.Item3); @@ -1026,18 +754,29 @@ public class INTERFACE public class Extra { - public bool IsElsControlled { get; set; } public bool AllowEnvLight { get; set; } public string Color { get; set; } public float OffsetX { get; set; } public float OffsetY { get; set; } public float OffsetZ { get; set; } + public int ExtraId { get; set; } - public Extra() + public Extra(int id) { IsElsControlled = false; AllowEnvLight = false; + ExtraId = id; + } + + internal void SetData(NanoXMLNode data) + { + IsElsControlled = bool.Parse(data.GetAttribute("IsElsControlled").Value); + AllowEnvLight = bool.Parse(data.GetAttribute("AllowEnvLight").Value); + Color = data.GetAttribute("Color").Value; + OffsetX = float.Parse(data.GetAttribute("OffsetX").Value); + OffsetY = float.Parse(data.GetAttribute("OffsetY").Value); + OffsetZ = float.Parse(data.GetAttribute("OffsetZ").Value); } } public class EOVERRIDE @@ -1060,18 +799,18 @@ public class EOVERRIDE public EOVERRIDE() { - Extra01 = new Extra(); - Extra02 = new Extra(); - Extra03 = new Extra(); - Extra04 = new Extra(); - Extra05 = new Extra(); - Extra06 = new Extra(); - Extra07 = new Extra(); - Extra08 = new Extra(); - Extra09 = new Extra(); - Extra10 = new Extra(); - Extra11 = new Extra(); - Extra12 = new Extra(); + Extra01 = new Extra(1); + Extra02 = new Extra(2); + Extra03 = new Extra(3); + Extra04 = new Extra(4); + Extra05 = new Extra(5); + Extra06 = new Extra(6); + Extra07 = new Extra(7); + Extra08 = new Extra(8); + Extra09 = new Extra(9); + Extra10 = new Extra(10); + Extra11 = new Extra(11); + Extra12 = new Extra(12); } } @@ -1160,30 +899,12 @@ public class AcoronaLights public string ColorL { get; set; } public string ColorR { get; set; } - } - - - public class ACORONAS - { - - public AcoronaLights Headlights { get; set; } - - public AcoronaLights TailLights { get; set; } - - public AcoronaLights IndicatorsF { get; set; } - - public AcoronaLights IndicatorsB { get; set; } - - public AcoronaLights ReverseLights { get; set; } - - public ACORONAS() + internal void SetData(NanoXMLNode data) { - Headlights = new AcoronaLights(); - TailLights = new AcoronaLights(); - IndicatorsF = new AcoronaLights(); - IndicatorsB = new AcoronaLights(); - ReverseLights = new AcoronaLights(); + DfltPattern = int.Parse(data.GetAttribute("DfltPattern").Value); + ColorL = data.GetAttribute("ColorL").Value; + ColorR = data.GetAttribute("ColorR").Value; } } @@ -1196,6 +917,13 @@ public class MainHorn public string AudioString { get; set; } public string SoundSet { get; set; } + + internal void SetData(NanoXMLNode data) + { + AudioString = data.GetAttribute("AudioString")?.Value; + InterruptsSiren = bool.Parse(data.GetAttribute("InterruptsSiren")?.Value ?? "false"); + SoundSet = data.GetAttribute("SoundSet")?.Value; + } } public class SrnTone @@ -1206,12 +934,19 @@ public class SrnTone public string AudioString { get; set; } public string SoundSet { get; set; } + + internal void SetData(NanoXMLNode data) + { + AudioString = data.GetAttribute("AudioString")?.Value; + AllowUse = bool.Parse(data.GetAttribute("AllowUse")?.Value ?? "false"); + SoundSet = data.GetAttribute("SoundSet")?.Value; + } } public class TonePattern { public bool Enabled { get; set; } - public string Pattern { get; set; } + public bool IsScanPattern { get; set; } = false; public int IntPattern { get; set; } public TonePattern() @@ -1219,6 +954,17 @@ public TonePattern() Enabled = false; IntPattern = -1; } + + internal void SetData(NanoXMLNode sn) + { + Enabled = bool.Parse(sn.GetAttribute("Enabled").Value); + var pattern = sn.GetAttribute("Pattern").Value; + IsScanPattern = pattern.ToLower().Equals("scan"); + if (!IsScanPattern) + { + IntPattern = int.Parse(pattern); + } + } } @@ -1327,11 +1073,27 @@ public ScanPatternCustomPool() public class Lstg { - public bool Enabled { get; set; } public int IntPattern { get; set; } - public string Pattern { get; set; } + public bool IsScanPattern { get; set; } = false; + + private bool dataSet = false; + internal void SetData(NanoXMLNode data) + { + if (dataSet) + { + throw new InvalidOperationException("Data cannot be set twice!"); + } + Enabled = bool.Parse(data.GetAttribute("Enabled").Value); + var pattern = data.GetAttribute("Pattern").Value; + IsScanPattern = pattern?.ToLower().Equals("scan") ?? false; + if (!IsScanPattern) + { + IntPattern = int.Parse(pattern); + } + dataSet = true; + } } public class Lights @@ -1371,8 +1133,6 @@ public class Vcfroot public CRUISE CRUISE { get; set; } - public ACORONAS ACORONAS { get; set; } - public SOUNDS SOUNDS { get; set; } public Lights WRNL { get; set; } @@ -1393,7 +1153,6 @@ public Vcfroot() EOVERRIDE = new EOVERRIDE(); MISC = new MISC(); CRUISE = new CRUISE(); - ACORONAS = new ACORONAS(); SOUNDS = new SOUNDS(); WRNL = new Lights(); PRML = new Lights(); diff --git a/src/configuration/VcfSync.cs b/src/configuration/VcfSync.cs new file mode 100644 index 0000000..b338a00 --- /dev/null +++ b/src/configuration/VcfSync.cs @@ -0,0 +1,68 @@ +using CitizenFX.Core.Native; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ELS.configuration +{ + public class VcfSync + { + + public static List ElsResources = new List(); + public static List> VcfData = new List>(); + + public async Task CheckVCF() + { + foreach (string name in ElsResources) + { + LoadFilesPromScript(name); + } + } + + public static void CheckResources() + { + var numResources = Function.Call(Hash.GET_NUM_RESOURCES); + for (int x = 0; x < numResources; x++) + { + var name = Function.Call(Hash.GET_RESOURCE_BY_FIND_INDEX, x); + string isElsResource = API.GetResourceMetadata(name, "is_els", 0); + if (!String.IsNullOrEmpty(isElsResource) && isElsResource.Equals("true")) + { + ElsResources.Add(name); + LoadFilesPromScript(name); + } + + } + Utils.ReleaseWriteLine($"Total ELS Resources: {ElsResources.Count}"); + } + + internal static async void LoadFilesPromScript(string name) + { + int num = Function.Call(Hash.GET_NUM_RESOURCE_METADATA, name, "file"); + + for (int i = 0; i < num; i++) + { + var filename = Function.Call(Hash.GET_RESOURCE_METADATA, name, "file", i); + + var data = Function.Call(Hash.LOAD_RESOURCE_FILE, name, filename); + if (Path.GetExtension(filename).ToLower() == ".xml") + { + try + { + if (VCF.isValidData(data)) + { + VcfData.Add(new Tuple(name, filename, data)); + } + } + catch (XMLParsingException e) + { + Utils.ReleaseWriteLine($"There was a parsing error in {filename} please validate this XML and try again."); + } + } + } + } + } +} diff --git a/src/packages.config b/src/packages.config index 5ca4de4..e49b47e 100644 --- a/src/packages.config +++ b/src/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file