From 14dac914ce773a9da085dd57c69aaa21eb228fd2 Mon Sep 17 00:00:00 2001 From: Kara Date: Wed, 11 Oct 2023 02:17:59 -0700 Subject: [PATCH] Kill `UserInterfaceHelpers` (#20912) --- .../CommunicationsConsoleComponent.cs | 2 - .../CommunicationsConsoleSystem.cs | 47 ++++++----- Content.Server/Crayon/CrayonComponent.cs | 2 - Content.Server/Crayon/CrayonSystem.cs | 28 +++---- .../Instruments/InstrumentComponent.cs | 2 - .../Instruments/InstrumentSystem.cs | 6 +- .../Components/HealthAnalyzerComponent.cs | 2 - .../Medical/HealthAnalyzerSystem.cs | 12 +-- .../UserInterface/ActivatableUIComponent.cs | 2 - .../UserInterface/ActivatableUISystem.cs | 77 ++++++++++++------- .../UserInterface/IntrinsicUISystem.cs | 9 +-- .../UserInterface/UserInterfaceHelpers.cs | 13 ---- 12 files changed, 104 insertions(+), 98 deletions(-) delete mode 100644 Content.Server/UserInterface/UserInterfaceHelpers.cs diff --git a/Content.Server/Communications/CommunicationsConsoleComponent.cs b/Content.Server/Communications/CommunicationsConsoleComponent.cs index 82a4a94539..e2aa0d7620 100644 --- a/Content.Server/Communications/CommunicationsConsoleComponent.cs +++ b/Content.Server/Communications/CommunicationsConsoleComponent.cs @@ -55,7 +55,5 @@ namespace Content.Server.Communications /// [DataField] public SoundSpecifier Sound = new SoundPathSpecifier("/Audio/Announcements/announce.ogg"); - - public PlayerBoundUserInterface? UserInterface => Owner.GetUIOrNull(CommunicationsConsoleUiKey.Key); } } diff --git a/Content.Server/Communications/CommunicationsConsoleSystem.cs b/Content.Server/Communications/CommunicationsConsoleSystem.cs index e2a96335d0..b5c9385492 100644 --- a/Content.Server/Communications/CommunicationsConsoleSystem.cs +++ b/Content.Server/Communications/CommunicationsConsoleSystem.cs @@ -72,8 +72,8 @@ namespace Content.Server.Communications comp.UIUpdateAccumulator -= UIUpdateInterval; - if (comp.UserInterface is { } ui && ui.SubscribedSessions.Count > 0) - UpdateCommsConsoleInterface(uid, comp); + if (_uiSystem.TryGetUi(uid, CommunicationsConsoleUiKey.Key, out var ui) && ui.SubscribedSessions.Count > 0) + UpdateCommsConsoleInterface(uid, comp, ui); } base.Update(frameTime); @@ -121,9 +121,11 @@ namespace Content.Server.Communications /// /// Updates the UI for a particular comms console. /// - /// - public void UpdateCommsConsoleInterface(EntityUid uid, CommunicationsConsoleComponent comp) + public void UpdateCommsConsoleInterface(EntityUid uid, CommunicationsConsoleComponent comp, PlayerBoundUserInterface? ui = null) { + if (ui == null && !_uiSystem.TryGetUi(uid, CommunicationsConsoleUiKey.Key, out ui)) + return; + var stationUid = _stationSystem.GetOwningStation(uid); List? levels = null; string currentLevel = default!; @@ -151,15 +153,14 @@ namespace Content.Server.Communications } } - if (comp.UserInterface is not null) - _uiSystem.SetUiState(comp.UserInterface, new CommunicationsConsoleInterfaceState( - CanAnnounce(comp), - CanCallOrRecall(comp), - levels, - currentLevel, - currentDelay, - _roundEndSystem.ExpectedCountdownEnd - )); + _uiSystem.SetUiState(ui, new CommunicationsConsoleInterfaceState( + CanAnnounce(comp), + CanCallOrRecall(comp), + levels, + currentLevel, + currentDelay, + _roundEndSystem.ExpectedCountdownEnd + )); } private static bool CanAnnounce(CommunicationsConsoleComponent comp) @@ -203,7 +204,9 @@ namespace Content.Server.Communications private void OnSelectAlertLevelMessage(EntityUid uid, CommunicationsConsoleComponent comp, CommunicationsConsoleSelectAlertLevelMessage message) { - if (message.Session.AttachedEntity is not { Valid: true } mob) return; + if (message.Session.AttachedEntity is not { Valid: true } mob) + return; + if (!CanUse(mob, uid)) { _popupSystem.PopupCursor(Loc.GetString("comms-console-permission-denied"), message.Session, PopupType.Medium); @@ -284,8 +287,12 @@ namespace Content.Server.Communications private void OnCallShuttleMessage(EntityUid uid, CommunicationsConsoleComponent comp, CommunicationsConsoleCallEmergencyShuttleMessage message) { - if (!CanCallOrRecall(comp)) return; - if (message.Session.AttachedEntity is not { Valid: true } mob) return; + if (!CanCallOrRecall(comp)) + return; + + if (message.Session.AttachedEntity is not { Valid: true } mob) + return; + if (!CanUse(mob, uid)) { _popupSystem.PopupEntity(Loc.GetString("comms-console-permission-denied"), uid, message.Session); @@ -306,8 +313,12 @@ namespace Content.Server.Communications private void OnRecallShuttleMessage(EntityUid uid, CommunicationsConsoleComponent comp, CommunicationsConsoleRecallEmergencyShuttleMessage message) { - if (!CanCallOrRecall(comp)) return; - if (message.Session.AttachedEntity is not { Valid: true } mob) return; + if (!CanCallOrRecall(comp)) + return; + + if (message.Session.AttachedEntity is not { Valid: true } mob) + return; + if (!CanUse(mob, uid)) { _popupSystem.PopupEntity(Loc.GetString("comms-console-permission-denied"), uid, message.Session); diff --git a/Content.Server/Crayon/CrayonComponent.cs b/Content.Server/Crayon/CrayonComponent.cs index 6f2cd6d397..df20681938 100644 --- a/Content.Server/Crayon/CrayonComponent.cs +++ b/Content.Server/Crayon/CrayonComponent.cs @@ -24,7 +24,5 @@ namespace Content.Server.Crayon [ViewVariables(VVAccess.ReadWrite)] [DataField("deleteEmpty")] public bool DeleteEmpty = true; - - [ViewVariables] public PlayerBoundUserInterface? UserInterface => Owner.GetUIOrNull(CrayonUiKey.Key); } } diff --git a/Content.Server/Crayon/CrayonSystem.cs b/Content.Server/Crayon/CrayonSystem.cs index 7cc2c20d89..d225df2dae 100644 --- a/Content.Server/Crayon/CrayonSystem.cs +++ b/Content.Server/Crayon/CrayonSystem.cs @@ -74,7 +74,8 @@ public sealed class CrayonSystem : SharedCrayonSystem // Decrease "Ammo" component.Charges--; - Dirty(component); + Dirty(uid, component); + _adminLogger.Add(LogType.CrayonDraw, LogImpact.Low, $"{EntityManager.ToPrettyString(args.User):user} drew a {component.Color:color} {component.SelectedState}"); args.Handled = true; @@ -89,17 +90,16 @@ public sealed class CrayonSystem : SharedCrayonSystem return; if (!TryComp(args.User, out var actor) || - component.UserInterface == null) + !_uiSystem.TryGetUi(uid, SharedCrayonComponent.CrayonUiKey.Key, out var ui)) { return; } - _uiSystem.ToggleUi(component.UserInterface, actor.PlayerSession); - - if (component.UserInterface?.SubscribedSessions.Contains(actor.PlayerSession) == true) + _uiSystem.ToggleUi(ui, actor.PlayerSession); + if (ui.SubscribedSessions.Contains(actor.PlayerSession)) { // Tell the user interface the selected stuff - _uiSystem.SetUiState(component.UserInterface, new CrayonBoundUserInterfaceState(component.SelectedState, component.SelectableColor, component.Color)); + _uiSystem.SetUiState(ui, new CrayonBoundUserInterfaceState(component.SelectedState, component.SelectableColor, component.Color)); } args.Handled = true; @@ -108,22 +108,22 @@ public sealed class CrayonSystem : SharedCrayonSystem private void OnCrayonBoundUI(EntityUid uid, CrayonComponent component, CrayonSelectMessage args) { // Check if the selected state is valid - if (!_prototypeManager.TryIndex(args.State, out var prototype) || !prototype.Tags.Contains("crayon")) return; + if (!_prototypeManager.TryIndex(args.State, out var prototype) || !prototype.Tags.Contains("crayon")) + return; component.SelectedState = args.State; - Dirty(component); + Dirty(uid, component); } private void OnCrayonBoundUIColor(EntityUid uid, CrayonComponent component, CrayonColorMessage args) { // you still need to ensure that the given color is a valid color - if (component.SelectableColor && args.Color != component.Color) - { - component.Color = args.Color; + if (!component.SelectableColor || args.Color == component.Color) + return; - Dirty(component); - } + component.Color = args.Color; + Dirty(uid, component); } @@ -134,7 +134,7 @@ public sealed class CrayonSystem : SharedCrayonSystem // Get the first one from the catalog and set it as default var decal = _prototypeManager.EnumeratePrototypes().FirstOrDefault(x => x.Tags.Contains("crayon")); component.SelectedState = decal?.ID ?? string.Empty; - Dirty(component); + Dirty(uid, component); } private void OnCrayonDropped(EntityUid uid, CrayonComponent component, DroppedEvent args) diff --git a/Content.Server/Instruments/InstrumentComponent.cs b/Content.Server/Instruments/InstrumentComponent.cs index 5a6b5828da..810d265314 100644 --- a/Content.Server/Instruments/InstrumentComponent.cs +++ b/Content.Server/Instruments/InstrumentComponent.cs @@ -20,8 +20,6 @@ public sealed partial class InstrumentComponent : SharedInstrumentComponent public IPlayerSession? InstrumentPlayer => _entMan.GetComponentOrNull(Owner)?.CurrentSingleUser ?? _entMan.GetComponentOrNull(Owner)?.PlayerSession; - - [ViewVariables] public PlayerBoundUserInterface? UserInterface => Owner.GetUIOrNull(InstrumentUiKey.Key); } [RegisterComponent] diff --git a/Content.Server/Instruments/InstrumentSystem.cs b/Content.Server/Instruments/InstrumentSystem.cs index 4e002eb677..ec23382105 100644 --- a/Content.Server/Instruments/InstrumentSystem.cs +++ b/Content.Server/Instruments/InstrumentSystem.cs @@ -5,7 +5,6 @@ using Content.Server.Stunnable; using Content.Shared.Administration; using Content.Shared.Instruments; using Content.Shared.Instruments.UI; -using Content.Shared.Interaction; using Content.Shared.Physics; using Content.Shared.Popups; using JetBrains.Annotations; @@ -17,7 +16,6 @@ using Robust.Shared.Configuration; using Robust.Shared.Console; using Robust.Shared.GameStates; using Robust.Shared.Timing; -using Robust.Shared.Utility; namespace Content.Server.Instruments; @@ -435,9 +433,7 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem // Just in case Clean(uid); - - if (instrument.UserInterface is not null) - _bui.CloseAll(instrument.UserInterface); + _bui.TryCloseAll(uid, InstrumentUiKey.Key); } instrument.Timer += frameTime; diff --git a/Content.Server/Medical/Components/HealthAnalyzerComponent.cs b/Content.Server/Medical/Components/HealthAnalyzerComponent.cs index 4b0bb5ff1f..39b1df573f 100644 --- a/Content.Server/Medical/Components/HealthAnalyzerComponent.cs +++ b/Content.Server/Medical/Components/HealthAnalyzerComponent.cs @@ -17,8 +17,6 @@ namespace Content.Server.Medical.Components [DataField("scanDelay")] public float ScanDelay = 0.8f; - public PlayerBoundUserInterface? UserInterface => Owner.GetUIOrNull(HealthAnalyzerUiKey.Key); - /// /// Sound played on scanning begin /// diff --git a/Content.Server/Medical/HealthAnalyzerSystem.cs b/Content.Server/Medical/HealthAnalyzerSystem.cs index 79d55e8068..6e2f7fdf36 100644 --- a/Content.Server/Medical/HealthAnalyzerSystem.cs +++ b/Content.Server/Medical/HealthAnalyzerSystem.cs @@ -51,12 +51,12 @@ namespace Content.Server.Medical args.Handled = true; } - private void OpenUserInterface(EntityUid user, HealthAnalyzerComponent healthAnalyzer) + private void OpenUserInterface(EntityUid user, EntityUid analyzer) { - if (!TryComp(user, out var actor) || healthAnalyzer.UserInterface == null) + if (!TryComp(user, out var actor) || !_uiSystem.TryGetUi(analyzer, HealthAnalyzerUiKey.Key, out var ui)) return; - _uiSystem.OpenUi(healthAnalyzer.UserInterface ,actor.PlayerSession); + _uiSystem.OpenUi(ui ,actor.PlayerSession); } public void UpdateScannedUser(EntityUid uid, EntityUid user, EntityUid? target, HealthAnalyzerComponent? healthAnalyzer) @@ -64,7 +64,7 @@ namespace Content.Server.Medical if (!Resolve(uid, ref healthAnalyzer)) return; - if (target == null || healthAnalyzer.UserInterface == null) + if (target == null || !_uiSystem.TryGetUi(uid, HealthAnalyzerUiKey.Key, out var ui)) return; if (!HasComp(target)) @@ -73,9 +73,9 @@ namespace Content.Server.Medical TryComp(target, out var temp); TryComp(target, out var bloodstream); - OpenUserInterface(user, healthAnalyzer); + OpenUserInterface(user, uid); - _uiSystem.SendUiMessage(healthAnalyzer.UserInterface, new HealthAnalyzerScannedUserMessage(GetNetEntity(target), temp != null ? temp.CurrentTemperature : float.NaN, + _uiSystem.SendUiMessage(ui, new HealthAnalyzerScannedUserMessage(GetNetEntity(target), temp != null ? temp.CurrentTemperature : float.NaN, bloodstream != null ? bloodstream.BloodSolution.FillFraction : float.NaN)); } } diff --git a/Content.Server/UserInterface/ActivatableUIComponent.cs b/Content.Server/UserInterface/ActivatableUIComponent.cs index ff605c8119..bb020608e2 100644 --- a/Content.Server/UserInterface/ActivatableUIComponent.cs +++ b/Content.Server/UserInterface/ActivatableUIComponent.cs @@ -11,8 +11,6 @@ namespace Content.Server.UserInterface [ViewVariables] public Enum? Key { get; set; } - [ViewVariables] public PlayerBoundUserInterface? UserInterface => (Key != null) ? Owner.GetUIOrNull(Key) : null; - [ViewVariables(VVAccess.ReadWrite)] [DataField] public bool InHandsOnly { get; set; } = false; diff --git a/Content.Server/UserInterface/ActivatableUISystem.cs b/Content.Server/UserInterface/ActivatableUISystem.cs index c200d7a3f0..59086415b4 100644 --- a/Content.Server/UserInterface/ActivatableUISystem.cs +++ b/Content.Server/UserInterface/ActivatableUISystem.cs @@ -76,7 +76,7 @@ public sealed partial class ActivatableUISystem : EntitySystem return; ActivationVerb verb = new(); - verb.Act = () => InteractUI(args.User, component); + verb.Act = () => InteractUI(args.User, uid, component); verb.Text = Loc.GetString(component.VerbText); // TODO VERBS add "open UI" icon? args.Verbs.Add(verb); @@ -84,16 +84,24 @@ public sealed partial class ActivatableUISystem : EntitySystem private void OnActivate(EntityUid uid, ActivatableUIComponent component, ActivateInWorldEvent args) { - if (args.Handled) return; - if (component.InHandsOnly) return; - args.Handled = InteractUI(args.User, component); + if (args.Handled) + return; + + if (component.InHandsOnly) + return; + + args.Handled = InteractUI(args.User, uid, component); } private void OnUseInHand(EntityUid uid, ActivatableUIComponent component, UseInHandEvent args) { - if (args.Handled) return; - if (component.rightClickOnly) return; - args.Handled = InteractUI(args.User, component); + if (args.Handled) + return; + + if (component.rightClickOnly) + return; + + args.Handled = InteractUI(args.User, uid, component); } private void OnParentChanged(EntityUid uid, ActivatableUIComponent aui, ref EntParentChangedMessage args) @@ -103,53 +111,64 @@ public sealed partial class ActivatableUISystem : EntitySystem private void OnUIClose(EntityUid uid, ActivatableUIComponent component, BoundUIClosedEvent args) { - if (args.Session != component.CurrentSingleUser) return; - if (args.UiKey != component.Key) return; + if (args.Session != component.CurrentSingleUser) + return; + + if (!Equals(args.UiKey, component.Key)) + return; + SetCurrentSingleUser(uid, null, component); } - private bool InteractUI(EntityUid user, ActivatableUIComponent aui) + private bool InteractUI(EntityUid user, EntityUid uiEntity, ActivatableUIComponent aui) { - if (!_blockerSystem.CanInteract(user, aui.Owner) && (!aui.AllowSpectator || !HasComp(user))) + if (!_blockerSystem.CanInteract(user, uiEntity) && (!aui.AllowSpectator || !HasComp(user))) return false; if (aui.RequireHands && !HasComp(user)) return false; - if (!EntityManager.TryGetComponent(user, out ActorComponent? actor)) return false; + if (!EntityManager.TryGetComponent(user, out ActorComponent? actor)) + return false; + + if (aui.AdminOnly && !_adminManager.IsAdmin(actor.PlayerSession)) + return false; - if (aui.AdminOnly && !_adminManager.IsAdmin(actor.PlayerSession)) return false; + if (aui.Key == null) + return false; - var ui = aui.UserInterface; - if (ui == null) return false; + if (!_uiSystem.TryGetUi(uiEntity, aui.Key, out var ui)) + return false; if (aui.SingleUser && (aui.CurrentSingleUser != null) && (actor.PlayerSession != aui.CurrentSingleUser)) { // If we get here, supposedly, the object is in use. // Check with BUI that it's ACTUALLY in use just in case. // Since this could brick the object if it goes wrong. - if (ui.SubscribedSessions.Count != 0) return false; + if (ui.SubscribedSessions.Count != 0) + return false; } // If we've gotten this far, fire a cancellable event that indicates someone is about to activate this. // This is so that stuff can require further conditions (like power). var oae = new ActivatableUIOpenAttemptEvent(user); - var uae = new UserOpenActivatableUIAttemptEvent(user, aui.Owner); - RaiseLocalEvent(user, uae, false); - RaiseLocalEvent((aui).Owner, oae, false); - if (oae.Cancelled || uae.Cancelled) return false; + var uae = new UserOpenActivatableUIAttemptEvent(user, uiEntity); + RaiseLocalEvent(user, uae); + RaiseLocalEvent(uiEntity, oae); + if (oae.Cancelled || uae.Cancelled) + return false; // Give the UI an opportunity to prepare itself if it needs to do anything // before opening var bae = new BeforeActivatableUIOpenEvent(user); - RaiseLocalEvent((aui).Owner, bae, false); + RaiseLocalEvent(uiEntity, bae); - SetCurrentSingleUser((aui).Owner, actor.PlayerSession, aui); + SetCurrentSingleUser(uiEntity, actor.PlayerSession, aui); _uiSystem.ToggleUi(ui, actor.PlayerSession); //Let the component know a user opened it so it can do whatever it needs to do var aae = new AfterActivatableUIOpenEvent(user, actor.PlayerSession); - RaiseLocalEvent((aui).Owner, aae, false); + RaiseLocalEvent(uiEntity, aae); return true; } @@ -163,24 +182,28 @@ public sealed partial class ActivatableUISystem : EntitySystem aui.CurrentSingleUser = v; - RaiseLocalEvent(uid, new ActivatableUIPlayerChangedEvent(), false); + RaiseLocalEvent(uid, new ActivatableUIPlayerChangedEvent()); } public void CloseAll(EntityUid uid, ActivatableUIComponent? aui = null) { if (!Resolve(uid, ref aui, false)) return; - if (aui.UserInterface is null) + + if (aui.Key == null || !_uiSystem.TryGetUi(uid, aui.Key, out var ui)) return; - _uiSystem.CloseAll(aui.UserInterface); + _uiSystem.CloseAll(ui); } private void OnHandDeselected(EntityUid uid, ActivatableUIComponent? aui, HandDeselectedEvent args) { - if (!Resolve(uid, ref aui, false)) return; + if (!Resolve(uid, ref aui, false)) + return; + if (!aui.CloseOnHandDeselect) return; + CloseAll(uid, aui); } } diff --git a/Content.Server/UserInterface/IntrinsicUISystem.cs b/Content.Server/UserInterface/IntrinsicUISystem.cs index bd449df5f5..27b682cd41 100644 --- a/Content.Server/UserInterface/IntrinsicUISystem.cs +++ b/Content.Server/UserInterface/IntrinsicUISystem.cs @@ -1,5 +1,4 @@ using Content.Server.Actions; -using Content.Shared.Actions; using Content.Shared.UserInterface; using Robust.Server.GameObjects; @@ -36,19 +35,19 @@ public sealed class IntrinsicUISystem : EntitySystem if (key is null) { - Logger.ErrorS("bui", $"Entity {ToPrettyString(uid)} has an invalid intrinsic UI."); + Log.Error($"Entity {ToPrettyString(uid)} has an invalid intrinsic UI."); } var ui = GetUIOrNull(uid, key, iui); if (ui is null) { - Logger.ErrorS("bui", $"Couldn't get UI {key} on {ToPrettyString(uid)}"); + Log.Error($"Couldn't get UI {key} on {ToPrettyString(uid)}"); return false; } var attempt = new IntrinsicUIOpenAttemptEvent(uid, key); - RaiseLocalEvent(uid, attempt, false); + RaiseLocalEvent(uid, attempt); if (attempt.Cancelled) return false; @@ -61,7 +60,7 @@ public sealed class IntrinsicUISystem : EntitySystem if (!Resolve(uid, ref component)) return null; - return key is null ? null : uid.GetUIOrNull(key); + return key is null ? null : _uiSystem.GetUiOrNull(uid, key); } } diff --git a/Content.Server/UserInterface/UserInterfaceHelpers.cs b/Content.Server/UserInterface/UserInterfaceHelpers.cs deleted file mode 100644 index 865772c772..0000000000 --- a/Content.Server/UserInterface/UserInterfaceHelpers.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Robust.Server.GameObjects; - -namespace Content.Server.UserInterface -{ - public static class UserInterfaceHelpers - { - [Obsolete("Use UserInterfaceSystem")] - public static PlayerBoundUserInterface? GetUIOrNull(this EntityUid entity, Enum uiKey) - { - return IoCManager.Resolve().GetEntitySystem().GetUiOrNull(entity, uiKey); - } - } -} -- 2.51.2