From 8e8b8eb8aa3063c01bd3da61774a50675561d5de Mon Sep 17 00:00:00 2001 From: Evil Factory <36804725+evilfactory@users.noreply.github.com> Date: Sat, 28 Feb 2026 16:05:20 -0300 Subject: [PATCH] GameMain.LuaCs is no more --- .../ClientSource/DebugConsole.cs | 8 +++---- .../ClientSource/GUI/ChatBox.cs | 2 +- .../BarotraumaClient/ClientSource/GameMain.cs | 9 +++----- .../ClientSource/LuaCs/LuaCsSettingsMenu.cs | 12 +++++----- .../ClientSource/Networking/GameClient.cs | 2 +- .../ServerSource/Characters/Character.cs | 2 +- .../Characters/CharacterNetworking.cs | 2 +- .../ServerSource/DebugConsole.cs | 6 ++--- .../BarotraumaServer/ServerSource/GameMain.cs | 14 +++++------- .../ServerSource/Networking/ChatMessage.cs | 2 +- .../ServerSource/Networking/GameServer.cs | 10 ++++----- .../ServerSource/Networking/RespawnManager.cs | 8 +++---- .../Networking/Voip/VoipServer.cs | 4 ++-- .../Animation/HumanoidAnimController.cs | 4 ++-- .../Characters/Animation/Ragdoll.cs | 2 +- .../SharedSource/Characters/Character.cs | 4 ++-- .../Health/Afflictions/AfflictionHusk.cs | 4 ++-- .../Characters/Health/CharacterHealth.cs | 4 ++-- .../Items/Components/Holdable/MeleeWeapon.cs | 2 +- .../Components/Machines/Deconstructor.cs | 2 +- .../Items/Components/Signal/WifiComponent.cs | 2 +- .../SharedSource/Items/Item.cs | 2 +- .../SharedSource/LuaCs/LuaCsSetup.cs | 12 +++++++++- .../SharedSource/LuaCs/ModUtils.cs | 12 +++++----- .../SharedSource/LuaCs/_Plugins/ACsMod.cs | 2 +- .../LuaCs/_Services/LoggerService.cs | 2 +- .../LuaCs/_Services/LuaCsInfoProvider.cs | 14 ++++++------ .../_Services/LuaScriptManagementService.cs | 2 +- .../_Lua/LuaClasses/LuaBarotraumaAdditions.cs | 2 +- .../_Services/_Lua/LuaClasses/LuaCsLogger.cs | 22 +++++++++---------- .../_Services/_Lua/LuaClasses/LuaCsUtility.cs | 2 +- .../LuaCs/_Services/_Lua/LuaPatcherService.cs | 6 ++--- .../BarotraumaShared/SharedSource/Map/Gap.cs | 2 +- .../SharedSource/Map/MapEntity.cs | 4 ++-- .../StatusEffects/StatusEffect.cs | 6 ++--- 35 files changed, 99 insertions(+), 96 deletions(-) diff --git a/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs b/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs index 46fa01e72..ecf7d8dd5 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs @@ -223,7 +223,7 @@ namespace Barotrauma private static bool IsCommandPermitted(Identifier command, GameClient client) { - if (GameMain.LuaCs.Game.IsCustomCommandPermitted(command)) { return true; } + if (LuaCsSetup.Instance.Game.IsCustomCommandPermitted(command)) { return true; } switch (command.Value.ToLowerInvariant()) { @@ -4231,14 +4231,14 @@ namespace Barotrauma return; } - if (GameMain.LuaCs.CurrentRunState != RunState.Running) + if (LuaCsSetup.Instance.CurrentRunState != RunState.Running) { ThrowError("LuaCs not initialized, use the console command cl_reloadluacs to force initialization."); return; } - var result = GameMain.LuaCs.LuaScriptManagementService.DoString(string.Join(" ", args)); - GameMain.LuaCs.Logger.LogResults(result.ToResult()); + var result = LuaCsSetup.Instance.LuaScriptManagementService.DoString(string.Join(" ", args)); + LuaCsSetup.Instance.Logger.LogResults(result.ToResult()); })); } diff --git a/Barotrauma/BarotraumaClient/ClientSource/GUI/ChatBox.cs b/Barotrauma/BarotraumaClient/ClientSource/GUI/ChatBox.cs index a8e5e33bf..5514fa1a0 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GUI/ChatBox.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GUI/ChatBox.cs @@ -415,7 +415,7 @@ namespace Barotrauma if (GameMain.IsSingleplayer) { bool? should = null; - GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnChatMessage(message.Text, message.SenderClient, message.Type, message) ?? should); + LuaCsSetup.Instance.EventService.PublishEvent(x => should = x.OnChatMessage(message.Text, message.SenderClient, message.Type, message) ?? should); if (should != null && should.Value) { return; } } diff --git a/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs b/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs index 10086bdb0..b8ddf8f72 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs @@ -24,8 +24,6 @@ namespace Barotrauma { class GameMain : Game { - private static LuaCsSetup _luaCs; - public static LuaCsSetup LuaCs => _luaCs ??= new LuaCsSetup(); public static bool ShowFPS; public static bool ShowPerf; public static bool DebugDraw; @@ -298,7 +296,7 @@ namespace Barotrauma Window.FileDropped += OnFileDropped; - LuaCs.GetType(); + LuaCsSetup.Instance.GetType(); } public static void ExecuteAfterContentFinishedLoading(Action action) @@ -1296,10 +1294,9 @@ namespace Barotrauma CreatureMetrics.Save(); try { - if (_luaCs is not null) + if (LuaCsSetup.Instance is not null) { - _luaCs.Dispose(); - _luaCs = null; + LuaCsSetup.Instance.Dispose(); } } catch (Exception e) diff --git a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/LuaCsSettingsMenu.cs b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/LuaCsSettingsMenu.cs index 5f1e6d90e..8b1004165 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/LuaCsSettingsMenu.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/LuaCsSettingsMenu.cs @@ -19,33 +19,33 @@ namespace Barotrauma new GUITickBox(new RectTransform(new Vector2(0.8f, 0.1f), list.Content.RectTransform), "Enable CSharp Scripting") { - Selected = GameMain.LuaCs.IsCsEnabled, + Selected = LuaCsSetup.Instance.IsCsEnabled, ToolTip = "This enables CSharp Scripting for mods to use, WARNING: CSharp is NOT sandboxed, be careful with what mods you download.", OnSelected = (GUITickBox tick) => { - GameMain.LuaCs.IsCsEnabled = tick.Selected; + LuaCsSetup.Instance.IsCsEnabled = tick.Selected; return true; } }; new GUITickBox(new RectTransform(new Vector2(0.8f, 0.1f), list.Content.RectTransform), "Disable Error GUI Overlay") { - Selected = GameMain.LuaCs.DisableErrorGUIOverlay, + Selected = LuaCsSetup.Instance.DisableErrorGUIOverlay, ToolTip = "", OnSelected = (GUITickBox tick) => { - GameMain.LuaCs.DisableErrorGUIOverlay = tick.Selected; + LuaCsSetup.Instance.DisableErrorGUIOverlay = tick.Selected; return true; } }; new GUITickBox(new RectTransform(new Vector2(0.8f, 0.1f), list.Content.RectTransform), "Hide usernames In Error Logs") { - Selected = GameMain.LuaCs.HideUserNamesInLogs, + Selected = LuaCsSetup.Instance.HideUserNamesInLogs, ToolTip = "Hides the operating system username when displaying error logs (eg your username on windows).", OnSelected = (GUITickBox tick) => { - GameMain.LuaCs.HideUserNamesInLogs = tick.Selected; + LuaCsSetup.Instance.HideUserNamesInLogs = tick.Selected; return true; } }; diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs index 3111861d4..5e64723bf 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs @@ -3004,7 +3004,7 @@ namespace Barotrauma.Networking public override void AddChatMessage(ChatMessage message) { bool? should = null; - GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnChatMessage(message.Text, message.SenderClient, message.Type, message) ?? should); + LuaCsSetup.Instance.EventService.PublishEvent(x => should = x.OnChatMessage(message.Text, message.SenderClient, message.Type, message) ?? should); if (should != null && should.Value) { return; } if (string.IsNullOrEmpty(message.Text)) { return; } diff --git a/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs b/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs index e173c7a45..5ab01aeee 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs @@ -65,7 +65,7 @@ namespace Barotrauma var owner = GameMain.Server.ConnectedClients.Find(c => c.Character == this); if (owner != null) { - if (!GameMain.LuaCs.Game.overrideTraitors) + if (!LuaCsSetup.Instance.Game.overrideTraitors) { GameMain.Server.SendDirectChatMessage(TextManager.FormatServerMessage("KilledByTraitorNotification"), owner, ChatMessageType.ServerMessageBoxInGame); } diff --git a/Barotrauma/BarotraumaServer/ServerSource/Characters/CharacterNetworking.cs b/Barotrauma/BarotraumaServer/ServerSource/Characters/CharacterNetworking.cs index 8aa2c1809..65d87e934 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Characters/CharacterNetworking.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Characters/CharacterNetworking.cs @@ -811,7 +811,7 @@ namespace Barotrauma var tempBuffer = new ReadWriteMessage(); WriteStatus(tempBuffer, forceAfflictionData: true); - if (msgLengthBeforeStatus + tempBuffer.LengthBytes >= 255 && restrictMessageSize && (GameMain.LuaCs.RestrictMessageSize)) + if (msgLengthBeforeStatus + tempBuffer.LengthBytes >= 255 && restrictMessageSize && (LuaCsSetup.Instance.RestrictMessageSize)) { msg.WriteBoolean(false); if (msgLengthBeforeStatus < 255) diff --git a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs index 507bacc83..6a04212ff 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs @@ -1291,14 +1291,14 @@ namespace Barotrauma commands.Add(new Command("lua", "lua: Runs a string.", (string[] args) => { - var result = GameMain.LuaCs.LuaScriptManagementService.DoString(string.Join(" ", args)); - GameMain.LuaCs.Logger.LogResults(result.ToResult()); + var result = LuaCsSetup.Instance.LuaScriptManagementService.DoString(string.Join(" ", args)); + LuaCsSetup.Instance.Logger.LogResults(result.ToResult()); })); commands.Add(new Command("reloadlua|reloadcs|reloadluacs", "Re-initializes the LuaCs environment.", (string[] args) => { //GameMain.LuaCs.Initialize(); - GameMain.LuaCs.EventService.PublishEvent(sub => sub.OnReloadAllPackages()); + LuaCsSetup.Instance.EventService.PublishEvent(sub => sub.OnReloadAllPackages()); })); commands.Add(new Command("toggleluadebug", "Toggles the MoonSharp Debug Server.", (string[] args) => diff --git a/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs b/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs index c875eed2f..2fc1138e4 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs @@ -35,9 +35,6 @@ namespace Barotrauma set { world = value; } } - private static LuaCsSetup _luaCs; - public static LuaCsSetup LuaCs => _luaCs ??= new LuaCsSetup(); - public static GameServer Server; public static NetworkMember NetworkMember { @@ -115,7 +112,7 @@ namespace Barotrauma MainThread = Thread.CurrentThread; - LuaCs.GetType(); + LuaCsSetup.Instance.GetType(); } public void Init() @@ -367,9 +364,9 @@ namespace Barotrauma CoroutineManager.Update(paused: false, (float)Timing.Step); performanceCounterTimer.Stop(); - if (GameMain.LuaCs.PerformanceCounter.EnablePerformanceCounter) + if (LuaCsSetup.Instance.PerformanceCounter.EnablePerformanceCounter) { - GameMain.LuaCs.PerformanceCounter.AddElapsedTicks(new SimplePerformanceData("Update", performanceCounterTimer.ElapsedTicks)); + LuaCsSetup.Instance.PerformanceCounter.AddElapsedTicks(new SimplePerformanceData("Update", performanceCounterTimer.ElapsedTicks)); } performanceCounterTimer.Reset(); @@ -455,10 +452,9 @@ namespace Barotrauma ShouldRun = false; try { - if (_luaCs is not null) + if (LuaCsSetup.Instance is not null) { - _luaCs.Dispose(); - _luaCs = null; + LuaCsSetup.Instance.Dispose(); } } catch (Exception e) diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs index 83898e882..1d15c4b60 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs @@ -89,7 +89,7 @@ namespace Barotrauma.Networking if (flaggedAsSpam) { return; } bool? should = null; - GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnChatMessage(txt, c, type, ChatMessage.Create(c.Name, txt, type, null, c)) ?? should); + LuaCsSetup.Instance.EventService.PublishEvent(x => should = x.OnChatMessage(txt, c, type, ChatMessage.Create(c.Name, txt, type, null, c)) ?? should); if (should != null && should.Value) { return; } if (type == ChatMessageType.Order) diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs index 89a92c077..ac6d9374a 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs @@ -440,7 +440,7 @@ namespace Barotrauma.Networking (permadeathMode && (!character.IsDead || character.CauseOfDeath?.Type == CauseOfDeathType.Disconnected))); if (!character.IsDead) { - if (!GameMain.LuaCs.Game.disableDisconnectCharacter) + if (!LuaCsSetup.Instance.Game.disableDisconnectCharacter) { character.KillDisconnectedTimer += deltaTime; character.SetStun(1.0f); @@ -3178,7 +3178,7 @@ namespace Barotrauma.Networking } TraitorManager.Initialize(GameMain.GameSession.EventManager, Level.Loaded); - if (GameMain.LuaCs.Game.overrideTraitors) + if (LuaCsSetup.Instance.Game.overrideTraitors) { TraitorManager.Enabled = false; } @@ -3512,7 +3512,7 @@ namespace Barotrauma.Networking } bool? result = null; - GameMain.LuaCs.EventService.PublishEvent(x => result = x.OnTryClienChangeName(c, newName, newJob, newTeam) ?? result); + LuaCsSetup.Instance.EventService.PublishEvent(x => result = x.OnTryClienChangeName(c, newName, newJob, newTeam) ?? result); if (result != null) { @@ -3964,7 +3964,7 @@ namespace Barotrauma.Networking senderName = null; senderCharacter = null; } - else if (type == ChatMessageType.Radio && !GameMain.LuaCs.Game.overrideSignalRadio) + else if (type == ChatMessageType.Radio && !LuaCsSetup.Instance.Game.overrideSignalRadio) { //send to chat-linked wifi components Signal s = new Signal(message, sender: senderCharacter, source: senderRadio.Item); @@ -4771,7 +4771,7 @@ namespace Barotrauma.Networking { if (GameMain.Server == null || !GameMain.Server.ServerSettings.SaveServerLogs) { return; } - GameMain.LuaCs?.EventService.PublishEvent(x => x.OnServerLog(line, messageType)); + LuaCsSetup.Instance?.EventService.PublishEvent(x => x.OnServerLog(line, messageType)); GameMain.Server.ServerSettings.ServerLog.WriteLine(line, messageType); diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/RespawnManager.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/RespawnManager.cs index b83376d66..77f213cf3 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/RespawnManager.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/RespawnManager.cs @@ -18,7 +18,7 @@ namespace Barotrauma.Networking MultiPlayerCampaign campaign = GameMain.GameSession.GameMode as MultiPlayerCampaign; foreach (Client c in networkMember.ConnectedClients) { - if (GameMain.LuaCs.Game.overrideRespawnSub) + if (LuaCsSetup.Instance.Game.overrideRespawnSub) continue; if (!c.InGame) { continue; } @@ -169,7 +169,7 @@ namespace Barotrauma.Networking private bool ShouldStartRespawnCountdown(int characterToRespawnCount) { - if (GameMain.LuaCs.Game.overrideRespawnSub) + if (LuaCsSetup.Instance.Game.overrideRespawnSub) { characterToRespawnCount = 0; } @@ -187,7 +187,7 @@ namespace Barotrauma.Networking var teamId = teamSpecificState.TeamID; var respawnShuttle = GetShuttle(teamId); - if (respawnShuttle != null && !GameMain.LuaCs.Game.overrideRespawnSub) + if (respawnShuttle != null && !LuaCsSetup.Instance.Game.overrideRespawnSub) { respawnShuttle.Velocity = Vector2.Zero; } @@ -240,7 +240,7 @@ namespace Barotrauma.Networking if (RespawnShuttles.Any()) { ResetShuttle(teamSpecificState); - if (GameMain.LuaCs.Game.overrideRespawnSub) + if (LuaCsSetup.Instance.Game.overrideRespawnSub) { teamSpecificState.CurrentState = State.Waiting; } diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs index a28f2701b..5970af2bf 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs @@ -100,7 +100,7 @@ namespace Barotrauma.Networking (recipientSpectating || ChatMessage.CanUseRadio(recipient.Character, out recipientRadio))) { bool? canUse = null; - GameMain.LuaCs.EventService.PublishEvent(x => canUse = x.OnCanUseVoiceRadio(sender, recipient) ?? canUse); + LuaCsSetup.Instance.EventService.PublishEvent(x => canUse = x.OnCanUseVoiceRadio(sender, recipient) ?? canUse); if (canUse != null) { @@ -121,7 +121,7 @@ namespace Barotrauma.Networking } float range = 1.0f; - GameMain.LuaCs.EventService.PublishEvent(x => range = x.OnChangeLocalVoiceRange(sender, recipient) ?? range); + LuaCsSetup.Instance.EventService.PublishEvent(x => range = x.OnChangeLocalVoiceRange(sender, recipient) ?? range); if (recipientSpectating) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs index 3c64fb44c..3c3bd5c48 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs @@ -1306,11 +1306,11 @@ namespace Barotrauma //increase oxygen and clamp it above zero // -> the character should be revived if there are no major afflictions in addition to lack of oxygen target.Oxygen = Math.Max(target.Oxygen + 10.0f, 10.0f); - GameMain.LuaCs.EventService.PublishEvent(x => x.OnCharacterCPRSuccess(this)); + LuaCsSetup.Instance.EventService.PublishEvent(x => x.OnCharacterCPRSuccess(this)); } else { - GameMain.LuaCs.EventService.PublishEvent(x => x.OnCharacterCPRFailed(this)); + LuaCsSetup.Instance.EventService.PublishEvent(x => x.OnCharacterCPRFailed(this)); } } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs index 7517e00f4..f5e3a47f3 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs @@ -859,7 +859,7 @@ namespace Barotrauma float impactDamage = GetImpactDamage(impact, impactTolerance); float? should = null; - GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnChangeFallDamage(impactDamage, character, impactPos, velocity) ?? should); + LuaCsSetup.Instance.EventService.PublishEvent(x => should = x.OnChangeFallDamage(impactDamage, character, impactPos, velocity) ?? should); if (should != null) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs index 198ff8d93..22a0e4382 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs @@ -3388,13 +3388,13 @@ namespace Barotrauma { for (int i = 0; i < CharacterList.Count; i++) { - if (GameMain.LuaCs.Game.UpdatePriorityCharacters.Contains(CharacterList[i])) continue; + if (LuaCsSetup.Instance.Game.UpdatePriorityCharacters.Contains(CharacterList[i])) continue; CharacterList[i].Update(deltaTime * CharacterUpdateInterval, cam); } } - foreach (Character character in GameMain.LuaCs.Game.UpdatePriorityCharacters) + foreach (Character character in LuaCsSetup.Instance.Game.UpdatePriorityCharacters) { if (character.Removed) { continue; } Debug.Assert(character is { Removed: false }); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs index 7974edd57..37b2f35f5 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs @@ -338,13 +338,13 @@ namespace Barotrauma if (Prefab is AfflictionPrefabHusk huskPrefab) { - if (huskPrefab.ControlHusk || GameMain.LuaCs.Game.enableControlHusk) + if (huskPrefab.ControlHusk || LuaCsSetup.Instance.Game.enableControlHusk) { #if SERVER if (client != null) { GameMain.Server.SetClientCharacter(client, husk); - GameMain.LuaCs.EventService.PublishEvent(x => x.OnClientControlHusk(client, husk)); + LuaCsSetup.Instance.EventService.PublishEvent(x => x.OnClientControlHusk(client, husk)); } #else if (!character.IsRemotelyControlled && character == Character.Controlled) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs index 2ddc7d8f9..61cff37a5 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs @@ -658,7 +658,7 @@ namespace Barotrauma } bool? should = null; - GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnCharacterApplyDamage(this, attackResult, hitLimb, allowStacking) ?? should); + LuaCsSetup.Instance.EventService.PublishEvent(x => should = x.OnCharacterApplyDamage(this, attackResult, hitLimb, allowStacking) ?? should); if (should != null && should.Value) { return; } foreach (Affliction newAffliction in attackResult.Afflictions) @@ -830,7 +830,7 @@ namespace Barotrauma if (Character.Params.Health.ImmunityIdentifiers.Contains(newAffliction.Identifier)) { return; } bool? should = null; - GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnCharacterApplyAffliction(this, limbHealth, newAffliction, allowStacking) ?? should); + LuaCsSetup.Instance.EventService.PublishEvent(x => should = x.OnCharacterApplyAffliction(this, limbHealth, newAffliction, allowStacking) ?? should); if (should != null && should.Value) { return; } Affliction existingAffliction = null; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs index fe27ceb15..7212ba5de 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs @@ -436,7 +436,7 @@ namespace Barotrauma.Items.Components Structure targetStructure = target.UserData as Structure ?? targetFixture.UserData as Structure; Item targetItem = target.UserData is Holdable h ? h.Item : target.UserData as Item ?? targetFixture.UserData as Item; Entity targetEntity = targetCharacter ?? targetStructure ?? targetItem ?? target.UserData as Entity; - GameMain.LuaCs.EventService.PublishEvent(x => x.OnMeleeWeaponHandleImpact(this, target)); + LuaCsSetup.Instance.EventService.PublishEvent(x => x.OnMeleeWeaponHandleImpact(this, target)); if (Attack != null) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Deconstructor.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Deconstructor.cs index be847f047..8b04bf85d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Deconstructor.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Deconstructor.cs @@ -335,7 +335,7 @@ namespace Barotrauma.Items.Components } bool? should = null; - GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnItemDeconstructed(targetItem, this, user, allowRemove) ?? should); + LuaCsSetup.Instance.EventService.PublishEvent(x => should = x.OnItemDeconstructed(targetItem, this, user, allowRemove) ?? should); if (should == true) { return; } if (targetItem.AllowDeconstruct && allowRemove) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs index 7f8b4c75b..9d2104d73 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs @@ -232,7 +232,7 @@ namespace Barotrauma.Items.Components public void TransmitSignal(Signal signal, bool sentFromChat) { bool? should = null; - GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnWifiSignalTransmitted(this, signal, sentFromChat) ?? should); + LuaCsSetup.Instance.EventService.PublishEvent(x => should = x.OnWifiSignalTransmitted(this, signal, sentFromChat) ?? should); if (should != null && should.Value) { return; } bool chatMsgSent = false; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs index 8c700265a..fbe4d9ae9 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs @@ -3895,7 +3895,7 @@ namespace Barotrauma } bool? should = null; - GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnItemReadPropertyChange(this, property, parentObject, allowEditing, sender) ?? should); + LuaCsSetup.Instance.EventService.PublishEvent(x => should = x.OnItemReadPropertyChange(this, property, parentObject, allowEditing, sender) ?? should); if (should != null && should.Value) { return; } Type type = property.PropertyType; diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs index f81efda0d..9a92f8e49 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs @@ -22,8 +22,16 @@ namespace Barotrauma partial class LuaCsSetup : IDisposable, IEventScreenSelected, IEventEnabledPackageListChanged, IEventReloadAllPackages { - public LuaCsSetup() + private static LuaCsSetup _luaCsSetup; + public static LuaCsSetup Instance => _luaCsSetup ??= new LuaCsSetup(); + + private LuaCsSetup() { + if (_luaCsSetup != null) + { + throw new Exception("Tried to create another LuaCsSetup instance"); + } + // == startup _servicesProvider = SetupServicesProvider(); _runStateMachine = SetupStateMachine(); @@ -432,6 +440,8 @@ namespace Barotrauma Console.WriteLine(e); throw; } + + _luaCsSetup = null; GC.SuppressFinalize(this); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/ModUtils.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/ModUtils.cs index a6588444e..e2a08d806 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/ModUtils.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/ModUtils.cs @@ -74,27 +74,27 @@ namespace Barotrauma.LuaCs public static void PrintMessage(string s) { #if SERVER - GameMain.LuaCs.Logger.LogMessage($"{s}"); + LuaCsSetup.Instance.Logger.LogMessage($"{s}"); #else - GameMain.LuaCs.Logger.LogMessage($"{s}"); + LuaCsSetup.Instance.Logger.LogMessage($"{s}"); #endif } public static void PrintWarning(string s) { #if SERVER - GameMain.LuaCs.Logger.Log($"{s}", Color.Yellow); + LuaCsSetup.Instance.Logger.Log($"{s}", Color.Yellow); #else - GameMain.LuaCs.Logger.Log($"{s}", Color.Yellow); + LuaCsSetup.Instance.Logger.Log($"{s}", Color.Yellow); #endif } public static void PrintError(string s) { #if SERVER - GameMain.LuaCs.Logger.LogError($"{s}"); + LuaCsSetup.Instance.Logger.LogError($"{s}"); #else - GameMain.LuaCs.Logger.LogError($"{s}"); + LuaCsSetup.Instance.Logger.LogError($"{s}"); #endif } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Plugins/ACsMod.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Plugins/ACsMod.cs index 8065d4dcc..1a189e31b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Plugins/ACsMod.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Plugins/ACsMod.cs @@ -48,7 +48,7 @@ namespace Barotrauma } catch (Exception e) { - GameMain.LuaCs.Logger.HandleException(e); + LuaCsSetup.Instance.Logger.HandleException(e); } IsDisposed = true; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LoggerService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LoggerService.cs index 305950efe..aeccae840 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LoggerService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LoggerService.cs @@ -60,7 +60,7 @@ public partial class LoggerService : ILoggerService if (!_isInsideLogCall) { _isInsideLogCall = true; - GameMain.LuaCs?.EventService.PublishEvent(x => x.OnServerLog(logMessage, log.MessageType)); + LuaCsSetup.Instance?.EventService.PublishEvent(x => x.OnServerLog(logMessage, log.MessageType)); _isInsideLogCall = false; } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaCsInfoProvider.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaCsInfoProvider.cs index 81ff151b0..edf63fc32 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaCsInfoProvider.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaCsInfoProvider.cs @@ -12,13 +12,13 @@ public sealed class LuaCsInfoProvider : ILuaCsInfoProvider } public bool IsDisposed => false; - public bool IsCsEnabled => GameMain.LuaCs.IsCsEnabled; - public bool DisableErrorGUIOverlay => GameMain.LuaCs.DisableErrorGUIOverlay; - public bool HideUserNamesInLogs => GameMain.LuaCs.HideUserNamesInLogs; - public ulong LuaForBarotraumaSteamId => GameMain.LuaCs.LuaForBarotraumaSteamId; - public bool RestrictMessageSize => GameMain.LuaCs.RestrictMessageSize; - public string LocalDataSavePath => GameMain.LuaCs.LocalDataSavePath; - public RunState CurrentRunState => GameMain.LuaCs.CurrentRunState; + public bool IsCsEnabled => LuaCsSetup.Instance.IsCsEnabled; + public bool DisableErrorGUIOverlay => LuaCsSetup.Instance.DisableErrorGUIOverlay; + public bool HideUserNamesInLogs => LuaCsSetup.Instance.HideUserNamesInLogs; + public ulong LuaForBarotraumaSteamId => LuaCsSetup.Instance.LuaForBarotraumaSteamId; + public bool RestrictMessageSize => LuaCsSetup.Instance.RestrictMessageSize; + public string LocalDataSavePath => LuaCsSetup.Instance.LocalDataSavePath; + public RunState CurrentRunState => LuaCsSetup.Instance.CurrentRunState; public ContentPackage LuaCsForBarotraumaPackage { get diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs index 43a140e1d..5bb2d4bc8 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs @@ -90,7 +90,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService { commands.RegisterCommand("cl_reloadlua|cl_reloadcs|cl_reloadluacs", "Re-initializes the LuaCs environment.", (string[] args) => { - GameMain.LuaCs.EventService.PublishEvent(sub => sub.OnReloadAllPackages()); + LuaCsSetup.Instance.EventService.PublishEvent(sub => sub.OnReloadAllPackages()); }); commands.RegisterCommand("cl_toggleluadebug", "Toggles the MoonSharp Debug Server.", (string[] args) => diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaBarotraumaAdditions.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaBarotraumaAdditions.cs index cacdf5bf9..a80aa9e99 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaBarotraumaAdditions.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaBarotraumaAdditions.cs @@ -59,7 +59,7 @@ namespace Barotrauma { public object GetComponentString(string component) { - Type type = GameMain.LuaCs.PluginManagementService.GetType("Barotrauma.Items.Components." + component); + Type type = LuaCsSetup.Instance.PluginManagementService.GetType("Barotrauma.Items.Components." + component); if (type == null) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaCsLogger.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaCsLogger.cs index 7179ece13..cf9dbbe8c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaCsLogger.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaCsLogger.cs @@ -17,41 +17,41 @@ namespace Barotrauma { public static void HandleException(Exception ex, LuaCsMessageOrigin origin) { - GameMain.LuaCs.Logger.HandleException(ex); + LuaCsSetup.Instance.Logger.HandleException(ex); } public static void LogError(string message, LuaCsMessageOrigin origin) { - GameMain.LuaCs.Logger.LogError(message); + LuaCsSetup.Instance.Logger.LogError(message); } public static void LogError(string message) { - GameMain.LuaCs.Logger.LogError(message); + LuaCsSetup.Instance.Logger.LogError(message); } public static void LogMessage(string message, Color? serverColor = null, Color? clientColor = null) { - GameMain.LuaCs.Logger.LogMessage(message, serverColor, clientColor); + LuaCsSetup.Instance.Logger.LogMessage(message, serverColor, clientColor); } public static void Log(string message, Color? color = null, ServerLog.MessageType messageType = ServerLog.MessageType.ServerMessage) { - GameMain.LuaCs.Logger.Log(message, color, messageType); + LuaCsSetup.Instance.Logger.Log(message, color, messageType); } } partial class LuaCsSetup { // Compatibility with cs mods that use this method. - public static void PrintLuaError(object message) => GameMain.LuaCs.Logger.LogError($"{message}"); - public static void PrintCsError(object message) => GameMain.LuaCs.Logger.LogError($"{message}"); - public static void PrintGenericError(object message) => GameMain.LuaCs.Logger.LogError($"{message}"); + public static void PrintLuaError(object message) => LuaCsSetup.Instance.Logger.LogError($"{message}"); + public static void PrintCsError(object message) => LuaCsSetup.Instance.Logger.LogError($"{message}"); + public static void PrintGenericError(object message) => LuaCsSetup.Instance.Logger.LogError($"{message}"); - internal void PrintMessage(object message) => GameMain.LuaCs.Logger.LogMessage($"{message}"); + internal void PrintMessage(object message) => LuaCsSetup.Instance.Logger.LogMessage($"{message}"); - public static void PrintCsMessage(object message) => GameMain.LuaCs.Logger.LogMessage($"{message}"); + public static void PrintCsMessage(object message) => LuaCsSetup.Instance.Logger.LogMessage($"{message}"); - internal void HandleException(Exception ex, LuaCsMessageOrigin origin) => GameMain.LuaCs.Logger.HandleException(ex); + internal void HandleException(Exception ex, LuaCsMessageOrigin origin) => LuaCsSetup.Instance.Logger.HandleException(ex); } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaCsUtility.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaCsUtility.cs index 596f8de3b..b4aca75b9 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaCsUtility.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaClasses/LuaCsUtility.cs @@ -60,7 +60,7 @@ namespace Barotrauma foreach (var package in ContentPackageManager.AllPackages) { - if (package.UgcId.ValueEquals(new SteamWorkshopId(GameMain.LuaCs.LuaForBarotraumaSteamId)) + if (package.UgcId.ValueEquals(new SteamWorkshopId(LuaCsSetup.Instance.LuaForBarotraumaSteamId)) && pathStartsWith(getFullPath(package.Path))) { return false; diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaPatcherService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaPatcherService.cs index 181e04df3..0bee7c96c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaPatcherService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/LuaPatcherService.cs @@ -294,7 +294,7 @@ namespace Barotrauma.LuaCs private static MethodBase ResolveMethod(string className, string methodName, string[] parameters) { - var classType = GameMain.LuaCs.PluginManagementService.GetType(className); + var classType = LuaCsSetup.Instance.PluginManagementService.GetType(className); if (classType == null) throw new ScriptRuntimeException($"invalid class name '{className}'"); const BindingFlags BINDING_FLAGS = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; @@ -309,7 +309,7 @@ namespace Barotrauma.LuaCs for (int i = 0; i < parameters.Length; i++) { - Type type = GameMain.LuaCs.PluginManagementService.GetType(parameters[i]); + Type type = LuaCsSetup.Instance.PluginManagementService.GetType(parameters[i]); if (type == null) { throw new ScriptRuntimeException($"invalid parameter type '{parameters[i]}'"); @@ -670,7 +670,7 @@ namespace Barotrauma.LuaCs } var type = typeBuilder.CreateType(); - type.GetField(FIELD_LUACS, BindingFlags.Public | BindingFlags.Static).SetValue(null, GameMain.LuaCs); + type.GetField(FIELD_LUACS, BindingFlags.Public | BindingFlags.Static).SetValue(null, LuaCsSetup.Instance); return type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs index cdf46bbda..9457b77df 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs @@ -886,7 +886,7 @@ namespace Barotrauma } bool? should = null; - GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnGapOxygenUpdate(this, hull1, hull2) ?? should); + LuaCsSetup.Instance.EventService.PublishEvent(x => should = x.OnGapOxygenUpdate(this, hull1, hull2) ?? should); if (should != null && should.Value) return; float totalOxygen = hull1.Oxygen + hull2.Oxygen; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs index 64f01bb58..eb906563d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs @@ -697,7 +697,7 @@ namespace Barotrauma { foreach (Item item in Item.ItemList) { - if (GameMain.LuaCs.Game.UpdatePriorityItems.Contains(item)) { continue; } + if (LuaCsSetup.Instance.Game.UpdatePriorityItems.Contains(item)) { continue; } lastUpdatedItem = item; item.Update(deltaTime * MapEntityUpdateInterval, cam); } @@ -712,7 +712,7 @@ namespace Barotrauma } } - foreach (var item in GameMain.LuaCs.Game.UpdatePriorityItems) + foreach (var item in LuaCsSetup.Instance.Game.UpdatePriorityItems) { if (item.Removed) continue; diff --git a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs index a0cd1be2a..649e59e6e 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs @@ -1787,14 +1787,14 @@ namespace Barotrauma { if (entity is Item item) { - var result = GameMain.LuaCs.Hook.Call("statusEffect.apply." + item.Prefab.Identifier, this, deltaTime, entity, targets, worldPosition); + var result = LuaCsSetup.Instance.Hook.Call("statusEffect.apply." + item.Prefab.Identifier, this, deltaTime, entity, targets, worldPosition); if (result != null && result.Value) { return; } } if (entity is Character character) { - var result = GameMain.LuaCs.Hook.Call("statusEffect.apply." + character.SpeciesName, this, deltaTime, entity, targets, worldPosition); + var result = LuaCsSetup.Instance.Hook.Call("statusEffect.apply." + character.SpeciesName, this, deltaTime, entity, targets, worldPosition); if (result != null && result.Value) { return; } } @@ -1804,7 +1804,7 @@ namespace Barotrauma { foreach ((string hookName, ContentXElement element) in luaHook) { - var result = GameMain.LuaCs.Hook.Call(hookName, this, deltaTime, entity, targets, worldPosition, element); + var result = LuaCsSetup.Instance.Hook.Call(hookName, this, deltaTime, entity, targets, worldPosition, element); if (result != null && result.Value) { return; } }