From bee627ac6a59c0c8b2ebde87a823c33cac050f3d Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Thu, 24 Feb 2022 11:00:30 +1100 Subject: [PATCH] Turn SandboxManager into a system (#6867) --- .../UI/Tabs/AdminbusTab/AdminbusTab.xaml.cs | 38 +--- Content.Client/Entry/EntryPoint.cs | 1 - Content.Client/IoC/ClientContentIoC.cs | 6 +- Content.Client/Sandbox/ISandboxManager.cs | 11 -- .../{SandboxManager.cs => SandboxSystem.cs} | 181 +++++++++++------- Content.Server/Entry/EntryPoint.cs | 1 - .../GameTicking/Rules/SandboxRuleSystem.cs | 3 +- Content.Server/IoC/ServerContentIoC.cs | 3 - .../Sandbox/Commands/ColorNetworkCommand.cs | 2 +- Content.Server/Sandbox/ISandboxManager.cs | 8 - .../{SandboxManager.cs => SandboxSystem.cs} | 98 +++++----- .../Sandbox/SharedSandboxManager.cs | 80 -------- Content.Shared/Sandbox/SharedSandboxSystem.cs | 28 +++ 13 files changed, 193 insertions(+), 267 deletions(-) delete mode 100644 Content.Client/Sandbox/ISandboxManager.cs rename Content.Client/Sandbox/{SandboxManager.cs => SandboxSystem.cs} (70%) delete mode 100644 Content.Server/Sandbox/ISandboxManager.cs rename Content.Server/Sandbox/{SandboxManager.cs => SandboxSystem.cs} (57%) delete mode 100644 Content.Shared/Sandbox/SharedSandboxManager.cs create mode 100644 Content.Shared/Sandbox/SharedSandboxSystem.cs diff --git a/Content.Client/Administration/UI/Tabs/AdminbusTab/AdminbusTab.xaml.cs b/Content.Client/Administration/UI/Tabs/AdminbusTab/AdminbusTab.xaml.cs index 6bf45c0c1b..d4e267aa96 100644 --- a/Content.Client/Administration/UI/Tabs/AdminbusTab/AdminbusTab.xaml.cs +++ b/Content.Client/Administration/UI/Tabs/AdminbusTab/AdminbusTab.xaml.cs @@ -1,17 +1,12 @@ using System.IO; using Content.Client.Administration.Managers; using Content.Client.Decals.UI; +using Content.Client.Sandbox; using Content.Shared.Administration; using Robust.Client.AutoGenerated; -using Robust.Client.Placement; -using Robust.Client.ResourceManagement; using Robust.Client.UserInterface; using Robust.Client.UserInterface.Controls; -using Robust.Client.UserInterface.CustomControls; using Robust.Client.UserInterface.XAML; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Map; using Robust.Shared.Prototypes; namespace Content.Client.Administration.UI.Tabs.AdminbusTab @@ -19,10 +14,6 @@ namespace Content.Client.Administration.UI.Tabs.AdminbusTab [GenerateTypedNameReferences] public sealed partial class AdminbusTab : Control { - private EntitySpawnWindow? _entitySpawnWindow; - private TileSpawnWindow? _tileSpawnWindow; - private DecalPlacerWindow? _decalPlacerWindow; - public AdminbusTab() { RobustXamlLoader.Load(this); @@ -53,38 +44,17 @@ namespace Content.Client.Administration.UI.Tabs.AdminbusTab private void SpawnEntitiesButtonOnOnPressed(BaseButton.ButtonEventArgs obj) { - //FIXME: WE SHOULDN'T NEED TO CHECK FOR DISPOSED - if (_entitySpawnWindow == null || _entitySpawnWindow.Disposed) - { - _entitySpawnWindow = new EntitySpawnWindow(IoCManager.Resolve(), - IoCManager.Resolve(), - IoCManager.Resolve()); - } - - EntitySystem.Get().OpenCommand(_entitySpawnWindow); + EntitySystem.Get().ToggleEntitySpawnWindow(); } private void SpawnTilesButtonOnOnPressed(BaseButton.ButtonEventArgs obj) { - //FIXME: WE SHOULDN'T NEED TO CHECK FOR DISPOSED - if (_tileSpawnWindow == null || _tileSpawnWindow.Disposed) - { - _tileSpawnWindow = new TileSpawnWindow(IoCManager.Resolve(), - IoCManager.Resolve(), - IoCManager.Resolve()); - } - - EntitySystem.Get().OpenCommand(_tileSpawnWindow); + EntitySystem.Get().ToggleTilesWindow(); } private void SpawnDecalsButtonOnPressed(BaseButton.ButtonEventArgs obj) { - if (_decalPlacerWindow == null || _decalPlacerWindow.Disposed) - { - _decalPlacerWindow = new DecalPlacerWindow(IoCManager.Resolve()); - } - - EntitySystem.Get().OpenCommand(_decalPlacerWindow); + EntitySystem.Get().ToggleDecalsWindow(); } } } diff --git a/Content.Client/Entry/EntryPoint.cs b/Content.Client/Entry/EntryPoint.cs index 32de36df8d..941d319df0 100644 --- a/Content.Client/Entry/EntryPoint.cs +++ b/Content.Client/Entry/EntryPoint.cs @@ -186,7 +186,6 @@ namespace Content.Client.Entry overlayMgr.AddOverlay(new RadiationPulseOverlay()); IoCManager.Resolve().Initialize(); - IoCManager.Resolve().Initialize(); IoCManager.Resolve().Initialize(); IoCManager.Resolve().Initialize(); IoCManager.Resolve().Initialize(); diff --git a/Content.Client/IoC/ClientContentIoC.cs b/Content.Client/IoC/ClientContentIoC.cs index d0d0e0da9a..dbb02a7974 100644 --- a/Content.Client/IoC/ClientContentIoC.cs +++ b/Content.Client/IoC/ClientContentIoC.cs @@ -1,5 +1,4 @@ -using Content.Client.Administration; -using Content.Client.Administration.Managers; +using Content.Client.Administration.Managers; using Content.Client.Changelog; using Content.Client.Chat.Managers; using Content.Client.Clickable; @@ -11,7 +10,6 @@ using Content.Client.Items.Managers; using Content.Client.Module; using Content.Client.Parallax.Managers; using Content.Client.Preferences; -using Content.Client.Sandbox; using Content.Client.Screenshot; using Content.Client.StationEvents.Managers; using Content.Client.Stylesheets; @@ -20,7 +18,6 @@ using Content.Client.Voting; using Content.Shared.Actions; using Content.Shared.Administration; using Content.Shared.Module; -using Robust.Shared.IoC; namespace Content.Client.IoC { @@ -32,7 +29,6 @@ namespace Content.Client.IoC IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); - IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); diff --git a/Content.Client/Sandbox/ISandboxManager.cs b/Content.Client/Sandbox/ISandboxManager.cs deleted file mode 100644 index 90be917fa5..0000000000 --- a/Content.Client/Sandbox/ISandboxManager.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace Content.Client.Sandbox -{ - public interface ISandboxManager - { - void Initialize(); - bool SandboxAllowed { get; } - event Action AllowedChanged; - } -} diff --git a/Content.Client/Sandbox/SandboxManager.cs b/Content.Client/Sandbox/SandboxSystem.cs similarity index 70% rename from Content.Client/Sandbox/SandboxManager.cs rename to Content.Client/Sandbox/SandboxSystem.cs index 3da0bed2e3..c1daa0e6e4 100644 --- a/Content.Client/Sandbox/SandboxManager.cs +++ b/Content.Client/Sandbox/SandboxSystem.cs @@ -1,11 +1,12 @@ -using System; +using Content.Client.Administration.Managers; using Content.Client.Decals.UI; using Content.Client.HUD; using Content.Client.Markers; using Content.Client.SubFloor; +using Content.Shared.Administration; +using Content.Shared.GameTicking; using Content.Shared.Input; using Content.Shared.Sandbox; -using Content.Shared.SubFloor; using Robust.Client.Console; using Robust.Client.Debugging; using Robust.Client.Graphics; @@ -14,13 +15,9 @@ using Robust.Client.Placement; using Robust.Client.ResourceManagement; using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.CustomControls; -using Robust.Shared.GameObjects; using Robust.Shared.Input.Binding; -using Robust.Shared.IoC; -using Robust.Shared.Localization; using Robust.Shared.Map; using Robust.Shared.Network; -using Robust.Shared.Prototypes; using static Robust.Client.UserInterface.Controls.BoxContainer; namespace Content.Client.Sandbox @@ -116,41 +113,33 @@ namespace Content.Client.Sandbox } - internal sealed class SandboxManager : SharedSandboxManager, ISandboxManager + public sealed class SandboxSystem : SharedSandboxSystem { [Dependency] private readonly IClientConsoleHost _consoleHost = default!; [Dependency] private readonly IGameHud _gameHud = default!; - [Dependency] private readonly IClientNetManager _netManager = default!; [Dependency] private readonly IPlacementManager _placementManager = default!; - [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IResourceCache _resourceCache = default!; [Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!; [Dependency] private readonly IInputManager _inputManager = default!; + [Dependency] private readonly IClientAdminManager _adminManager = default!; public bool SandboxAllowed { get; private set; } - public event Action? AllowedChanged; - - private SandboxWindow? _window; + private SandboxWindow? _sandboxWindow; private EntitySpawnWindow? _spawnWindow; private TileSpawnWindow? _tilesSpawnWindow; private DecalPlacerWindow? _decalSpawnWindow; - private bool _sandboxWindowToggled; - public void Initialize() + public override void Initialize() { - _netManager.RegisterNetMessage(message => SetAllowed(message.SandboxAllowed)); - - _netManager.RegisterNetMessage(); - - _netManager.RegisterNetMessage(); - - _netManager.RegisterNetMessage(); - - _netManager.RegisterNetMessage(); + base.Initialize(); + SubscribeNetworkEvent(OnSandboxStatus); + SubscribeNetworkEvent(OnRoundRestart); + _adminManager.AdminStatusUpdated += OnAdminStatus; _gameHud.SandboxButtonToggled += SandboxButtonPressed; + // Do these need cleanup? _inputManager.SetInputCommand(ContentKeyFunctions.OpenEntitySpawnWindow, InputCmdHandler.FromDelegate(session => ToggleEntitySpawnWindow())); _inputManager.SetInputCommand(ContentKeyFunctions.OpenSandboxWindow, @@ -161,83 +150,129 @@ namespace Content.Client.Sandbox InputCmdHandler.FromDelegate(session => ToggleDecalsWindow())); } + private void OnAdminStatus() + { + if (CanSandbox()) + Enable(); + else + Disable(); + } + + private bool CanSandbox() + { + return SandboxAllowed || _adminManager.IsActive(); + } + + /// + /// Run when sandbox is disabled + /// + private void Disable() + { + _gameHud.SandboxButtonVisible = false; + _sandboxWindow?.Close(); + _sandboxWindow = null; + _spawnWindow?.Close(); + _tilesSpawnWindow?.Close(); + _decalSpawnWindow?.Close(); + } + + private void Enable() + { + _gameHud.SandboxButtonVisible = true; + } + + private void OnRoundRestart(RoundRestartCleanupEvent ev) + { + // Go through and cleanup windows (even if they remain adminned better to just shut them). + Disable(); + + if (CanSandbox()) + Enable(); + } + + public override void Shutdown() + { + base.Shutdown(); + // TODO: Gamehud moment + _gameHud.SandboxButtonToggled -= SandboxButtonPressed; + _adminManager.AdminStatusUpdated -= OnAdminStatus; + } + + private void OnSandboxStatus(MsgSandboxStatus ev) + { + SetAllowed(ev.SandboxAllowed); + } + private void SandboxButtonPressed(bool newValue) { - _sandboxWindowToggled = newValue; UpdateSandboxWindowVisibility(); } private void ToggleSandboxWindow() { - _sandboxWindowToggled = !_sandboxWindowToggled; UpdateSandboxWindowVisibility(); } private void UpdateSandboxWindowVisibility() { - if (_sandboxWindowToggled && SandboxAllowed) - OpenWindow(); + if (CanSandbox() && _sandboxWindow?.IsOpen != true) + OpenSandboxWindow(); else - _window?.Close(); + _sandboxWindow?.Close(); } private void SetAllowed(bool newAllowed) { if (newAllowed == SandboxAllowed) - { return; - } SandboxAllowed = newAllowed; - _gameHud.SandboxButtonVisible = newAllowed; + _gameHud.SandboxButtonVisible = CanSandbox(); - if (!newAllowed) - { - // Sandbox permission revoked, close window. - _window?.Close(); - } - - AllowedChanged?.Invoke(newAllowed); + if (!CanSandbox()) + Disable(); } - private void OpenWindow() + private void OpenSandboxWindow() { - if (_window != null) + if (_sandboxWindow != null) { + if (!_sandboxWindow.IsOpen) + _sandboxWindow.Open(); + return; } - _window = new SandboxWindow(); + _sandboxWindow = new SandboxWindow(); - _window.OnClose += WindowOnOnClose; + _sandboxWindow.OnClose += SandboxWindowOnClose; - _window.RespawnButton.OnPressed += OnRespawnButtonOnOnPressed; - _window.SpawnTilesButton.OnPressed += OnSpawnTilesButtonClicked; - _window.SpawnEntitiesButton.OnPressed += OnSpawnEntitiesButtonClicked; - _window.SpawnDecalsButton.OnPressed += OnSpawnDecalsButtonClicked; - _window.GiveFullAccessButton.OnPressed += OnGiveAdminAccessButtonClicked; - _window.GiveAghostButton.OnPressed += OnGiveAghostButtonClicked; - _window.ToggleLightButton.OnToggled += OnToggleLightButtonClicked; - _window.ToggleFovButton.OnToggled += OnToggleFovButtonClicked; - _window.ToggleShadowsButton.OnToggled += OnToggleShadowsButtonClicked; - _window.SuicideButton.OnPressed += OnSuicideButtonClicked; - _window.ToggleSubfloorButton.OnPressed += OnToggleSubfloorButtonClicked; - _window.ShowMarkersButton.OnPressed += OnShowMarkersButtonClicked; - _window.ShowBbButton.OnPressed += OnShowBbButtonClicked; - _window.MachineLinkingButton.OnPressed += OnMachineLinkingButtonClicked; + _sandboxWindow.RespawnButton.OnPressed += OnRespawnButtonOnOnPressed; + _sandboxWindow.SpawnTilesButton.OnPressed += OnSpawnTilesButtonClicked; + _sandboxWindow.SpawnEntitiesButton.OnPressed += OnSpawnEntitiesButtonClicked; + _sandboxWindow.SpawnDecalsButton.OnPressed += OnSpawnDecalsButtonClicked; + _sandboxWindow.GiveFullAccessButton.OnPressed += OnGiveAdminAccessButtonClicked; + _sandboxWindow.GiveAghostButton.OnPressed += OnGiveAghostButtonClicked; + _sandboxWindow.ToggleLightButton.OnToggled += OnToggleLightButtonClicked; + _sandboxWindow.ToggleFovButton.OnToggled += OnToggleFovButtonClicked; + _sandboxWindow.ToggleShadowsButton.OnToggled += OnToggleShadowsButtonClicked; + _sandboxWindow.SuicideButton.OnPressed += OnSuicideButtonClicked; + _sandboxWindow.ToggleSubfloorButton.OnPressed += OnToggleSubfloorButtonClicked; + _sandboxWindow.ShowMarkersButton.OnPressed += OnShowMarkersButtonClicked; + _sandboxWindow.ShowBbButton.OnPressed += OnShowBbButtonClicked; + _sandboxWindow.MachineLinkingButton.OnPressed += OnMachineLinkingButtonClicked; - _window.OpenCentered(); + _sandboxWindow.OpenCentered(); } - private void WindowOnOnClose() + private void SandboxWindowOnClose() { - _window = null; - _sandboxWindowToggled = false; + _sandboxWindow = null; } private void OnRespawnButtonOnOnPressed(BaseButton.ButtonEventArgs args) { - _netManager.ClientSendMessage(_netManager.CreateNetMessage()); + RaiseNetworkEvent(new MsgSandboxRespawn()); } private void OnSpawnEntitiesButtonClicked(BaseButton.ButtonEventArgs args) @@ -291,24 +326,27 @@ namespace Content.Client.Sandbox private void OnGiveAdminAccessButtonClicked(BaseButton.ButtonEventArgs args) { - _netManager.ClientSendMessage(_netManager.CreateNetMessage()); + RaiseNetworkEvent(new MsgSandboxGiveAccess()); } private void OnGiveAghostButtonClicked(BaseButton.ButtonEventArgs args) { - _netManager.ClientSendMessage(_netManager.CreateNetMessage()); + RaiseNetworkEvent(new MsgSandboxGiveAghost()); } private void OnSuicideButtonClicked(BaseButton.ButtonEventArgs args) { - _netManager.ClientSendMessage(_netManager.CreateNetMessage()); + RaiseNetworkEvent(new MsgSandboxSuicide()); } - private void ToggleEntitySpawnWindow() + // TODO: These should check for command perms + be reset if the round is over. + public void ToggleEntitySpawnWindow() { if (_spawnWindow == null) { - _spawnWindow = new EntitySpawnWindow(_placementManager, _prototypeManager, _resourceCache); + if (!CanSandbox()) return; + + _spawnWindow = new EntitySpawnWindow(_placementManager, PrototypeManager, _resourceCache); _spawnWindow.OpenToLeft(); return; } @@ -323,10 +361,12 @@ namespace Content.Client.Sandbox } } - private void ToggleTilesWindow() + public void ToggleTilesWindow() { if (_tilesSpawnWindow == null) { + if (!CanSandbox()) return; + _tilesSpawnWindow = new TileSpawnWindow(_tileDefinitionManager, _placementManager, _resourceCache); _tilesSpawnWindow.OpenToLeft(); return; @@ -342,11 +382,13 @@ namespace Content.Client.Sandbox } } - private void ToggleDecalsWindow() + public void ToggleDecalsWindow() { if (_decalSpawnWindow == null) { - _decalSpawnWindow = new DecalPlacerWindow(_prototypeManager); + if (!CanSandbox()) return; + + _decalSpawnWindow = new DecalPlacerWindow(PrototypeManager); _decalSpawnWindow.OpenToLeft(); return; } @@ -361,6 +403,7 @@ namespace Content.Client.Sandbox } } + // TODO: need to cleanup these private void ToggleLight() { _consoleHost.ExecuteCommand("togglelight"); diff --git a/Content.Server/Entry/EntryPoint.cs b/Content.Server/Entry/EntryPoint.cs index a77228dde6..13d9e6d616 100644 --- a/Content.Server/Entry/EntryPoint.cs +++ b/Content.Server/Entry/EntryPoint.cs @@ -112,7 +112,6 @@ namespace Content.Server.Entry } else { - IoCManager.Resolve().Initialize(); IoCManager.Resolve().Initialize(); IoCManager.Resolve().Initialize(); IoCManager.Resolve().Initialize(); diff --git a/Content.Server/GameTicking/Rules/SandboxRuleSystem.cs b/Content.Server/GameTicking/Rules/SandboxRuleSystem.cs index 64aa8a9773..46e541fa56 100644 --- a/Content.Server/GameTicking/Rules/SandboxRuleSystem.cs +++ b/Content.Server/GameTicking/Rules/SandboxRuleSystem.cs @@ -1,11 +1,10 @@ using Content.Server.Sandbox; -using Robust.Shared.IoC; namespace Content.Server.GameTicking.Rules; public sealed class SandboxRuleSystem : GameRuleSystem { - [Dependency] private readonly ISandboxManager _sandbox = default!; + [Dependency] private readonly SandboxSystem _sandbox = default!; public override string Prototype => "Sandbox"; diff --git a/Content.Server/IoC/ServerContentIoC.cs b/Content.Server/IoC/ServerContentIoC.cs index ccc36ef576..e9d57e70e6 100644 --- a/Content.Server/IoC/ServerContentIoC.cs +++ b/Content.Server/IoC/ServerContentIoC.cs @@ -16,13 +16,11 @@ using Content.Server.NodeContainer.NodeGroups; using Content.Server.Objectives; using Content.Server.Objectives.Interfaces; using Content.Server.Preferences.Managers; -using Content.Server.Sandbox; using Content.Server.Voting.Managers; using Content.Shared.Actions; using Content.Shared.Administration; using Content.Shared.Kitchen; using Content.Shared.Module; -using Robust.Shared.IoC; namespace Content.Server.IoC { @@ -33,7 +31,6 @@ namespace Content.Server.IoC IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); - IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); diff --git a/Content.Server/Sandbox/Commands/ColorNetworkCommand.cs b/Content.Server/Sandbox/Commands/ColorNetworkCommand.cs index d2ed7290fe..16aadd8cce 100644 --- a/Content.Server/Sandbox/Commands/ColorNetworkCommand.cs +++ b/Content.Server/Sandbox/Commands/ColorNetworkCommand.cs @@ -24,7 +24,7 @@ namespace Content.Server.Sandbox.Commands public void Execute(IConsoleShell shell, string argStr, string[] args) { - var sandboxManager = IoCManager.Resolve(); + var sandboxManager = EntitySystem.Get(); var adminManager = IoCManager.Resolve(); if (shell.IsClient && (!sandboxManager.IsSandboxEnabled && !adminManager.HasAdminFlag((IPlayerSession)shell.Player!, AdminFlags.Mapping))) { diff --git a/Content.Server/Sandbox/ISandboxManager.cs b/Content.Server/Sandbox/ISandboxManager.cs deleted file mode 100644 index ba55ed0a8f..0000000000 --- a/Content.Server/Sandbox/ISandboxManager.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Content.Server.Sandbox -{ - public interface ISandboxManager - { - bool IsSandboxEnabled { get; set; } - void Initialize(); - } -} diff --git a/Content.Server/Sandbox/SandboxManager.cs b/Content.Server/Sandbox/SandboxSystem.cs similarity index 57% rename from Content.Server/Sandbox/SandboxManager.cs rename to Content.Server/Sandbox/SandboxSystem.cs index cee8cbbfc8..1b4e7b1a8b 100644 --- a/Content.Server/Sandbox/SandboxManager.cs +++ b/Content.Server/Sandbox/SandboxSystem.cs @@ -22,14 +22,16 @@ using Robust.Shared.ViewVariables; namespace Content.Server.Sandbox { - internal sealed class SandboxManager : SharedSandboxManager, ISandboxManager, IEntityEventSubscriber + public sealed class SandboxSystem : SharedSandboxSystem { [Dependency] private readonly IPlayerManager _playerManager = default!; - [Dependency] private readonly IServerNetManager _netManager = default!; [Dependency] private readonly IPlacementManager _placementManager = default!; [Dependency] private readonly IConGroupController _conGroupController = default!; - [Dependency] private readonly IEntityManager _entityManager = default!; [Dependency] private readonly IServerConsoleHost _host = default!; + [Dependency] private readonly AccessSystem _access = default!; + [Dependency] private readonly InventorySystem _inventory = default!; + [Dependency] private readonly ItemSlotsSystem _slots = default!; + [Dependency] private readonly GameTicker _ticker = default!; private bool _isSandboxEnabled; @@ -44,16 +46,17 @@ namespace Content.Server.Sandbox } } - public void Initialize() + public override void Initialize() { - _netManager.RegisterNetMessage(); - _netManager.RegisterNetMessage(SandboxRespawnReceived); - _netManager.RegisterNetMessage(SandboxGiveAccessReceived); - _netManager.RegisterNetMessage(SandboxGiveAghostReceived); - _netManager.RegisterNetMessage(SandboxSuicideReceived); + base.Initialize(); + SubscribeNetworkEvent(SandboxRespawnReceived); + SubscribeNetworkEvent(SandboxGiveAccessReceived); + SubscribeNetworkEvent(SandboxGiveAghostReceived); + SubscribeNetworkEvent(SandboxSuicideReceived); + + SubscribeLocalEvent(GameTickerOnOnRunLevelChanged); _playerManager.PlayerStatusChanged += OnPlayerStatusChanged; - _entityManager.EventBus.SubscribeEvent(EventSource.Local, this, GameTickerOnOnRunLevelChanged); _placementManager.AllowPlacementFunc = placement => { @@ -74,6 +77,13 @@ namespace Content.Server.Sandbox }; } + public override void Shutdown() + { + base.Shutdown(); + _placementManager.AllowPlacementFunc = null; + _playerManager.PlayerStatusChanged -= OnPlayerStatusChanged; + } + private void GameTickerOnOnRunLevelChanged(GameRunLevelChangedEvent obj) { // Automatically clear sandbox state when round resets. @@ -86,60 +96,51 @@ namespace Content.Server.Sandbox private void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e) { if (e.NewStatus != SessionStatus.Connected || e.OldStatus != SessionStatus.Connecting) - { return; - } - var msg = _netManager.CreateNetMessage(); - msg.SandboxAllowed = IsSandboxEnabled; - _netManager.ServerSendMessage(msg, e.Session.ConnectedClient); + RaiseNetworkEvent(new MsgSandboxStatus {SandboxAllowed = IsSandboxEnabled}, e.Session.ConnectedClient); } - private void SandboxRespawnReceived(MsgSandboxRespawn message) + private void SandboxRespawnReceived(MsgSandboxRespawn message, EntitySessionEventArgs args) { if (!IsSandboxEnabled) - { return; - } - var player = _playerManager.GetSessionByChannel(message.MsgChannel); - EntitySystem.Get().Respawn(player); + var player = _playerManager.GetSessionByChannel(args.SenderSession.ConnectedClient); + if (player.AttachedEntity == null) return; + + _ticker.Respawn(player); } - private void SandboxGiveAccessReceived(MsgSandboxGiveAccess message) + private void SandboxGiveAccessReceived(MsgSandboxGiveAccess message, EntitySessionEventArgs args) { if (!IsSandboxEnabled) - { return; - } - var player = _playerManager.GetSessionByChannel(message.MsgChannel); + var player = _playerManager.GetSessionByChannel(args.SenderSession.ConnectedClient); if (player.AttachedEntity is not {} attached) { return; } - var allAccess = IoCManager.Resolve() + var allAccess = PrototypeManager .EnumeratePrototypes() .Select(p => p.ID).ToArray(); - var invSystem = EntitySystem.Get(); - - if (invSystem.TryGetSlotEntity(attached, "id", out var slotEntity)) + if (_inventory.TryGetSlotEntity(attached, "id", out var slotEntity)) { - if (_entityManager.HasComponent(slotEntity)) + if (HasComp(slotEntity)) { UpgradeId(slotEntity.Value); } - else if (_entityManager.TryGetComponent(slotEntity, out PDAComponent? pda)) + else if (TryComp(slotEntity, out var pda)) { if (pda.ContainedID == null) { var newID = CreateFreshId(); - if (_entityManager.TryGetComponent(pda.Owner, out ItemSlotsComponent? itemSlots)) + if (TryComp(pda.Owner, out var itemSlots)) { - _entityManager.EntitySysManager.GetEntitySystem(). - TryInsert(slotEntity.Value, pda.IdSlot, newID, null); + _slots.TryInsert(slotEntity.Value, pda.IdSlot, newID, null); } } else @@ -148,21 +149,20 @@ namespace Content.Server.Sandbox } } } - else if (_entityManager.TryGetComponent(attached, out var hands)) + else if (TryComp(attached, out var hands)) { var card = CreateFreshId(); - if (!invSystem.TryEquip(attached, card, "id", true, true)) + if (!_inventory.TryEquip(attached, card, "id", true, true)) { - hands.PutInHandOrDrop(_entityManager.GetComponent(card)); + hands.PutInHandOrDrop(Comp(card)); } } void UpgradeId(EntityUid id) { - var accessSystem = EntitySystem.Get(); - accessSystem.TrySetTags(id, allAccess); + _access.TrySetTags(id, allAccess); - if (_entityManager.TryGetComponent(id, out SpriteComponent? sprite)) + if (TryComp(id, out var sprite)) { sprite.LayerSetState(0, "gold"); } @@ -170,42 +170,36 @@ namespace Content.Server.Sandbox EntityUid CreateFreshId() { - var card = _entityManager.SpawnEntity("CaptainIDCard", _entityManager.GetComponent(attached).Coordinates); + var card = Spawn("CaptainIDCard", Transform(attached).Coordinates); UpgradeId(card); - _entityManager.GetComponent(card).FullName = _entityManager.GetComponent(attached).EntityName; + Comp(card).FullName = MetaData(attached).EntityName; return card; } } - private void SandboxGiveAghostReceived(MsgSandboxGiveAghost message) + private void SandboxGiveAghostReceived(MsgSandboxGiveAghost message, EntitySessionEventArgs args) { if (!IsSandboxEnabled) - { return; - } - var player = _playerManager.GetSessionByChannel(message.MsgChannel); + var player = _playerManager.GetSessionByChannel(args.SenderSession.ConnectedClient); _host.ExecuteCommand(player, _conGroupController.CanCommand(player, "aghost") ? "aghost" : "ghost"); } - private void SandboxSuicideReceived(MsgSandboxSuicide message) + private void SandboxSuicideReceived(MsgSandboxSuicide message, EntitySessionEventArgs args) { if (!IsSandboxEnabled) - { return; - } - var player = _playerManager.GetSessionByChannel(message.MsgChannel); + var player = _playerManager.GetSessionByChannel(args.SenderSession.ConnectedClient); _host.ExecuteCommand(player, "suicide"); } private void UpdateSandboxStatusForAll() { - var msg = _netManager.CreateNetMessage(); - msg.SandboxAllowed = IsSandboxEnabled; - _netManager.ServerSendToAll(msg); + RaiseNetworkEvent(new MsgSandboxStatus {SandboxAllowed = IsSandboxEnabled}); } } } diff --git a/Content.Shared/Sandbox/SharedSandboxManager.cs b/Content.Shared/Sandbox/SharedSandboxManager.cs deleted file mode 100644 index 96adbd2b07..0000000000 --- a/Content.Shared/Sandbox/SharedSandboxManager.cs +++ /dev/null @@ -1,80 +0,0 @@ -using Lidgren.Network; -using Robust.Shared.Network; - -namespace Content.Shared.Sandbox -{ - public abstract class SharedSandboxManager - { - protected sealed class MsgSandboxStatus : NetMessage - { - public override MsgGroups MsgGroup => MsgGroups.Command; - - public bool SandboxAllowed { get; set; } - - public override void ReadFromBuffer(NetIncomingMessage buffer) - { - SandboxAllowed = buffer.ReadBoolean(); - } - - public override void WriteToBuffer(NetOutgoingMessage buffer) - { - buffer.Write(SandboxAllowed); - } - } - - protected sealed class MsgSandboxRespawn : NetMessage - { - public override MsgGroups MsgGroup => MsgGroups.Command; - - public override void ReadFromBuffer(NetIncomingMessage buffer) - { - } - - public override void WriteToBuffer(NetOutgoingMessage buffer) - { - } - } - - protected sealed class MsgSandboxGiveAccess : NetMessage - { - public override MsgGroups MsgGroup => MsgGroups.Command; - - public override void ReadFromBuffer(NetIncomingMessage buffer) - { - } - - public override void WriteToBuffer(NetOutgoingMessage buffer) - { - } - - } - - protected sealed class MsgSandboxGiveAghost : NetMessage - { - public override MsgGroups MsgGroup => MsgGroups.Command; - - public override void ReadFromBuffer(NetIncomingMessage buffer) - { - } - - public override void WriteToBuffer(NetOutgoingMessage buffer) - { - } - - } - - protected sealed class MsgSandboxSuicide : NetMessage - { - public override MsgGroups MsgGroup => MsgGroups.Command; - - public override void ReadFromBuffer(NetIncomingMessage buffer) - { - } - - public override void WriteToBuffer(NetOutgoingMessage buffer) - { - } - - } - } -} diff --git a/Content.Shared/Sandbox/SharedSandboxSystem.cs b/Content.Shared/Sandbox/SharedSandboxSystem.cs new file mode 100644 index 0000000000..525106234e --- /dev/null +++ b/Content.Shared/Sandbox/SharedSandboxSystem.cs @@ -0,0 +1,28 @@ +using Robust.Shared.Prototypes; +using Robust.Shared.Serialization; + +namespace Content.Shared.Sandbox +{ + public abstract class SharedSandboxSystem : EntitySystem + { + [Dependency] protected readonly IPrototypeManager PrototypeManager = default!; + + [Serializable, NetSerializable] + protected sealed class MsgSandboxStatus : EntityEventArgs + { + public bool SandboxAllowed { get; set; } + } + + [Serializable, NetSerializable] + protected sealed class MsgSandboxRespawn : EntityEventArgs {} + + [Serializable, NetSerializable] + protected sealed class MsgSandboxGiveAccess : EntityEventArgs {} + + [Serializable, NetSerializable] + protected sealed class MsgSandboxGiveAghost : EntityEventArgs {} + + [Serializable, NetSerializable] + protected sealed class MsgSandboxSuicide : EntityEventArgs {} + } +}