Skip to content

Commit

Permalink
Random spontaneous cleanup PR (#25131)
Browse files Browse the repository at this point in the history
* Use new Subs.CVar helper

Removes manual config OnValueChanged calls, removes need to remember to manually unsubscribe.

This both reduces boilerplate and fixes many issues where subscriptions weren't removed on entity system shutdown.

* Fix a bunch of warnings

* More warning fixes

* Use new DateTime serializer to get rid of ISerializationHooks in changelog code.

* Get rid of some more ISerializationHooks for enums

* And a little more

* Apply suggestions from code review

Co-authored-by: 0x6273 <[email protected]>

---------

Co-authored-by: 0x6273 <[email protected]>
(cherry picked from commit 68ce53a)
  • Loading branch information
PJB3005 authored and DebugOk committed Feb 18, 2024
1 parent 7b1eee2 commit ccea851
Show file tree
Hide file tree
Showing 210 changed files with 481 additions and 930 deletions.
8 changes: 4 additions & 4 deletions Content.Benchmarks/ColorInterpolateBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public static Color InterpolateSysVector4(Color a, Color b,
public static Color InterpolateSysVector4In(in Color endPoint1, in Color endPoint2,
float lambda)
{
ref var sva = ref Unsafe.As<Color, SysVector4>(ref Unsafe.AsRef(endPoint1));
ref var svb = ref Unsafe.As<Color, SysVector4>(ref Unsafe.AsRef(endPoint2));
ref var sva = ref Unsafe.As<Color, SysVector4>(ref Unsafe.AsRef(in endPoint1));
ref var svb = ref Unsafe.As<Color, SysVector4>(ref Unsafe.AsRef(in endPoint2));

var res = SysVector4.Lerp(svb, sva, lambda);

Expand All @@ -156,8 +156,8 @@ public static Color InterpolateSimd(Color a, Color b,
public static Color InterpolateSimdIn(in Color a, in Color b,
float lambda)
{
var vecA = Unsafe.As<Color, Vector128<float>>(ref Unsafe.AsRef(a));
var vecB = Unsafe.As<Color, Vector128<float>>(ref Unsafe.AsRef(b));
var vecA = Unsafe.As<Color, Vector128<float>>(ref Unsafe.AsRef(in a));
var vecB = Unsafe.As<Color, Vector128<float>>(ref Unsafe.AsRef(in b));

vecB = Fma.MultiplyAdd(Sse.Subtract(vecB, vecA), Vector128.Create(lambda), vecA);

Expand Down
5 changes: 0 additions & 5 deletions Content.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ internal static class Program

public static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
}

public static async Task MainAsync(string[] args)
{
#if DEBUG
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nWARNING: YOU ARE RUNNING A DEBUG BUILD, USE A RELEASE BUILD FOR AN ACCURATE BENCHMARK");
Expand Down
18 changes: 9 additions & 9 deletions Content.Client/Actions/ActionsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void BaseHandleState<T>(EntityUid uid, BaseActionComponent component, Ba
component.ItemIconStyle = state.ItemIconStyle;
component.Sound = state.Sound;

if (_playerManager.LocalPlayer?.ControlledEntity == component.AttachedEntity)
if (_playerManager.LocalEntity == component.AttachedEntity)
ActionsUpdated?.Invoke();
}

Expand All @@ -111,7 +111,7 @@ protected override void UpdateAction(EntityUid? actionId, BaseActionComponent? a
return;

base.UpdateAction(actionId, action);
if (_playerManager.LocalPlayer?.ControlledEntity != action.AttachedEntity)
if (_playerManager.LocalEntity != action.AttachedEntity)
return;

ActionsUpdated?.Invoke();
Expand Down Expand Up @@ -144,7 +144,7 @@ private void HandleComponentState(EntityUid uid, ActionsComponent component, ref
_added.Add((actionId, action));
}

if (_playerManager.LocalPlayer?.ControlledEntity != uid)
if (_playerManager.LocalEntity != uid)
return;

foreach (var action in _removed)
Expand Down Expand Up @@ -177,23 +177,23 @@ public static int ActionComparer((EntityUid, BaseActionComponent?) a, (EntityUid
protected override void ActionAdded(EntityUid performer, EntityUid actionId, ActionsComponent comp,
BaseActionComponent action)
{
if (_playerManager.LocalPlayer?.ControlledEntity != performer)
if (_playerManager.LocalEntity != performer)
return;

OnActionAdded?.Invoke(actionId);
}

protected override void ActionRemoved(EntityUid performer, EntityUid actionId, ActionsComponent comp, BaseActionComponent action)
{
if (_playerManager.LocalPlayer?.ControlledEntity != performer)
if (_playerManager.LocalEntity != performer)
return;

OnActionRemoved?.Invoke(actionId);
}

public IEnumerable<(EntityUid Id, BaseActionComponent Comp)> GetClientActions()
{
if (_playerManager.LocalPlayer?.ControlledEntity is not { } user)
if (_playerManager.LocalEntity is not { } user)
return Enumerable.Empty<(EntityUid, BaseActionComponent)>();

return GetActions(user);
Expand All @@ -216,7 +216,7 @@ public void UnlinkAllActions()

public void LinkAllActions(ActionsComponent? actions = null)
{
if (_playerManager.LocalPlayer?.ControlledEntity is not { } user ||
if (_playerManager.LocalEntity is not { } user ||
!Resolve(user, ref actions, false))
{
return;
Expand All @@ -233,7 +233,7 @@ public override void Shutdown()

public void TriggerAction(EntityUid actionId, BaseActionComponent action)
{
if (_playerManager.LocalPlayer?.ControlledEntity is not { } user ||
if (_playerManager.LocalEntity is not { } user ||
!TryComp(user, out ActionsComponent? actions))
{
return;
Expand Down Expand Up @@ -261,7 +261,7 @@ public void TriggerAction(EntityUid actionId, BaseActionComponent action)
/// </summary>
public void LoadActionAssignments(string path, bool userData)
{
if (_playerManager.LocalPlayer?.ControlledEntity is not { } user)
if (_playerManager.LocalEntity is not { } user)
return;

var file = new ResPath(path).ToRootedPath();
Expand Down
17 changes: 9 additions & 8 deletions Content.Client/Administration/Managers/ClientAdminManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ public sealed class ClientAdminManager : IClientAdminManager, IClientConGroupImp
[Dependency] private readonly IClientNetManager _netMgr = default!;
[Dependency] private readonly IClientConGroupController _conGroup = default!;
[Dependency] private readonly IResourceManager _res = default!;
[Dependency] private readonly ILogManager _logManager = default!;

private AdminData? _adminData;
private readonly HashSet<string> _availableCommands = new();

private readonly AdminCommandPermissions _localCommandPermissions = new();
private ISawmill _sawmill = default!;

public event Action? AdminStatusUpdated;

Expand Down Expand Up @@ -92,17 +94,17 @@ private void UpdateMessageRx(MsgUpdateAdminStatus message)
}

_availableCommands.UnionWith(message.AvailableCommands);
Logger.DebugS("admin", $"Have {message.AvailableCommands.Length} commands available");
_sawmill.Debug($"Have {message.AvailableCommands.Length} commands available");

_adminData = message.Admin;
if (_adminData != null)
{
var flagsText = string.Join("|", AdminFlagsHelper.FlagsToNames(_adminData.Flags));
Logger.InfoS("admin", $"Updated admin status: {_adminData.Active}/{_adminData.Title}/{flagsText}");
_sawmill.Info($"Updated admin status: {_adminData.Active}/{_adminData.Title}/{flagsText}");
}
else
{
Logger.InfoS("admin", "Updated admin status: Not admin");
_sawmill.Info("Updated admin status: Not admin");
}

AdminStatusUpdated?.Invoke();
Expand All @@ -114,26 +116,25 @@ private void UpdateMessageRx(MsgUpdateAdminStatus message)
void IPostInjectInit.PostInject()
{
_conGroup.Implementation = this;
_sawmill = _logManager.GetSawmill("admin");
}

public AdminData? GetAdminData(EntityUid uid, bool includeDeAdmin = false)
{
return uid == _player.LocalPlayer?.ControlledEntity
? _adminData
: null;
return uid == _player.LocalEntity ? _adminData : null;
}

public AdminData? GetAdminData(ICommonSession session, bool includeDeAdmin = false)
{
if (_player.LocalPlayer?.UserId == session.UserId)
if (_player.LocalUser == session.UserId)
return _adminData;

return null;
}

public AdminData? GetAdminData(bool includeDeAdmin = false)
{
if (_player.LocalPlayer is { Session: { } session })
if (_player.LocalSession is { } session)
return GetAdminData(session, includeDeAdmin);

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void SetLocation()
{
UpdateMapOptions();

if (!_entMan.TryGetComponent(_playerManager.LocalPlayer?.ControlledEntity, out TransformComponent? transform))
if (!_entMan.TryGetComponent(_playerManager.LocalEntity, out TransformComponent? transform))
return;

_pausePreview = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void Reset()
var entManager = IoCManager.Resolve<IEntityManager>();
var xformSystem = entManager.System<SharedTransformSystem>();
var playerManager = IoCManager.Resolve<IPlayerManager>();
var player = playerManager.LocalPlayer?.ControlledEntity;
var player = playerManager.LocalEntity;

var currentMap = MapId.Nullspace;
var position = Vector2.Zero;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override void EnteredTree()
{
_data.Clear();

var player = _players.LocalPlayer?.ControlledEntity;
var player = _players.LocalEntity;
var playerGrid = _entities.GetComponentOrNull<TransformComponent>(player)?.GridUid;
var query = IoCManager.Resolve<IEntityManager>().AllEntityQueryEnumerator<MapGridComponent>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected override void EnteredTree()
while (gridQuery.MoveNext(out var uid, out _))
{
_gridData.Add(entManager.GetNetEntity(uid));
var player = playerManager.LocalPlayer?.ControlledEntity;
var player = playerManager.LocalEntity;
var playerGrid = entManager.GetComponentOrNull<TransformComponent>(player)?.GridUid;
GridOptions.AddItem($"{uid} {(playerGrid == uid ? " (Current)" : "")}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override void EnteredTree()

while (gridQuery.MoveNext(out var uid, out _))
{
var player = playerManager.LocalPlayer?.ControlledEntity;
var player = playerManager.LocalEntity;
var playerGrid = entManager.GetComponentOrNull<TransformComponent>(player)?.GridUid;
GridOptions.AddItem($"{uid} {(playerGrid == uid ? " (Current)" : "")}");
_gridData.Add(entManager.GetNetEntity(uid));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected override void EnteredTree()

while (gridQuery.MoveNext(out var uid, out _))
{
var player = playerManager.LocalPlayer?.ControlledEntity;
var player = playerManager.LocalEntity;
var playerGrid = entManager.GetComponentOrNull<TransformComponent>(player)?.GridUid;
GridOptions.AddItem($"{uid} {(playerGrid == uid ? " (Current)" : "")}");
_data.Add(entManager.GetNetEntity(uid));
Expand Down
12 changes: 6 additions & 6 deletions Content.Client/Alerts/ClientAlertsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public IReadOnlyDictionary<AlertKey, AlertState>? ActiveAlerts
{
get
{
var ent = _playerManager.LocalPlayer?.ControlledEntity;
var ent = _playerManager.LocalEntity;
return ent is not null
? GetActiveAlerts(ent.Value)
: null;
Expand All @@ -49,29 +49,29 @@ public IReadOnlyDictionary<AlertKey, AlertState>? ActiveAlerts

protected override void AfterShowAlert(Entity<AlertsComponent> alerts)
{
if (_playerManager.LocalPlayer?.ControlledEntity != alerts.Owner)
if (_playerManager.LocalEntity != alerts.Owner)
return;

SyncAlerts?.Invoke(this, alerts.Comp.Alerts);
}

protected override void AfterClearAlert(Entity<AlertsComponent> alertsComponent)
{
if (_playerManager.LocalPlayer?.ControlledEntity != alertsComponent.Owner)
if (_playerManager.LocalEntity != alertsComponent.Owner)
return;

SyncAlerts?.Invoke(this, alertsComponent.Comp.Alerts);
}

private void ClientAlertsHandleState(EntityUid uid, AlertsComponent component, ref AfterAutoHandleStateEvent args)
{
if (_playerManager.LocalPlayer?.ControlledEntity == uid)
if (_playerManager.LocalEntity == uid)
SyncAlerts?.Invoke(this, component.Alerts);
}

private void OnPlayerAttached(EntityUid uid, AlertsComponent component, LocalPlayerAttachedEvent args)
{
if (_playerManager.LocalPlayer?.ControlledEntity != uid)
if (_playerManager.LocalEntity != uid)
return;

SyncAlerts?.Invoke(this, component.Alerts);
Expand All @@ -81,7 +81,7 @@ protected override void HandleComponentShutdown(EntityUid uid, AlertsComponent c
{
base.HandleComponentShutdown(uid, component, args);

if (_playerManager.LocalPlayer?.ControlledEntity != uid)
if (_playerManager.LocalEntity != uid)
return;

ClearAlerts?.Invoke(this, EventArgs.Empty);
Expand Down
2 changes: 1 addition & 1 deletion Content.Client/Animations/EntityPickupAnimationSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void AnimateEntityPickup(EntityUid uid, EntityCoordinates initial, Vector
despawn.Lifetime = 0.25f;
_transform.SetLocalRotationNoLerp(animatableClone, initialAngle);

_animations.Play(animatableClone, animations, new Animation
_animations.Play(new Entity<AnimationPlayerComponent>(animatableClone, animations), new Animation
{
Length = TimeSpan.FromMilliseconds(125),
AnimationTracks =
Expand Down
1 change: 0 additions & 1 deletion Content.Client/Anomaly/Ui/AnomalyGeneratorWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace Content.Client.Anomaly.Ui;
[GenerateTypedNameReferences]
public sealed partial class AnomalyGeneratorWindow : FancyWindow
{
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IGameTiming _timing = default!;

private TimeSpan _cooldownEnd = TimeSpan.Zero;
Expand Down
1 change: 0 additions & 1 deletion Content.Client/Atmos/Monitor/UI/AirAlarmWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public sealed partial class AirAlarmWindow : FancyWindow
public event Action<string, AtmosMonitorThresholdType, AtmosAlarmThreshold, Gas?>? AtmosAlarmThresholdChanged;
public event Action<AirAlarmMode>? AirAlarmModeChanged;
public event Action<bool>? AutoModeChanged;
public event Action<string>? ResyncDeviceRequested;
public event Action? ResyncAllRequested;
public event Action<AirAlarmTab>? AirAlarmTabChange;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
HorizontalExpand="True" Orientation="Vertical"
Margin = "20 0 0 0" MinSize="160 0" >
<Label Name="CBoundLabel" HorizontalAlignment="Center" />
<CheckBox Name="CBoundEnabled" HorizontalAlignment="Center" Text="{Loc 'Enable'}"/>
<CheckBox Name="CBoundEnabled" HorizontalAlignment="Center" Text="{Loc 'Enable'}" Pressed="True" />
<FloatSpinBox Name="CSpinner" />
</BoxContainer>
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public ThresholdBoundControl(string controlLabel, float value, float uiValueScal
CBoundLabel.Text = controlLabel;

CSpinner.Value = ScaledValue;
CBoundEnabled.Pressed = _value != null;

CSpinner.OnValueChanged += SpinnerValueChanged;
CBoundEnabled.OnToggled += CheckboxToggled;
Expand Down
13 changes: 4 additions & 9 deletions Content.Client/Audio/AmbientSoundSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ public override void Initialize()
UpdatesOutsidePrediction = true;
UpdatesAfter.Add(typeof(AmbientSoundTreeSystem));

_cfg.OnValueChanged(CCVars.AmbientCooldown, SetCooldown, true);
_cfg.OnValueChanged(CCVars.MaxAmbientSources, SetAmbientCount, true);
_cfg.OnValueChanged(CCVars.AmbientRange, SetAmbientRange, true);
_cfg.OnValueChanged(CCVars.AmbienceVolume, SetAmbienceGain, true);
Subs.CVar(_cfg, CCVars.AmbientCooldown, SetCooldown, true);
Subs.CVar(_cfg, CCVars.MaxAmbientSources, SetAmbientCount, true);
Subs.CVar(_cfg, CCVars.AmbientRange, SetAmbientRange, true);
Subs.CVar(_cfg, CCVars.AmbienceVolume, SetAmbienceGain, true);
SubscribeLocalEvent<AmbientSoundComponent, ComponentShutdown>(OnShutdown);
}

Expand Down Expand Up @@ -138,11 +138,6 @@ public override void Shutdown()
{
base.Shutdown();
ClearSounds();

_cfg.UnsubValueChanged(CCVars.AmbientCooldown, SetCooldown);
_cfg.UnsubValueChanged(CCVars.MaxAmbientSources, SetAmbientCount);
_cfg.UnsubValueChanged(CCVars.AmbientRange, SetAmbientRange);
_cfg.UnsubValueChanged(CCVars.AmbienceVolume, SetAmbienceGain);
}

private int PlayingCount(string countSound)
Expand Down
7 changes: 2 additions & 5 deletions Content.Client/Audio/BackgroundAudioSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public override void Initialize()
{
base.Initialize();

_configManager.OnValueChanged(CCVars.LobbyMusicEnabled, LobbyMusicCVarChanged);
_configManager.OnValueChanged(CCVars.LobbyMusicVolume, LobbyMusicVolumeCVarChanged);
Subs.CVar(_configManager, CCVars.LobbyMusicEnabled, LobbyMusicCVarChanged);
Subs.CVar(_configManager, CCVars.LobbyMusicVolume, LobbyMusicVolumeCVarChanged);

_stateManager.OnStateChanged += StateManagerOnStateChanged;

Expand All @@ -50,9 +50,6 @@ public override void Shutdown()
{
base.Shutdown();

_configManager.UnsubValueChanged(CCVars.LobbyMusicEnabled, LobbyMusicCVarChanged);
_configManager.UnsubValueChanged(CCVars.LobbyMusicVolume, LobbyMusicVolumeCVarChanged);

_stateManager.OnStateChanged -= StateManagerOnStateChanged;

_client.PlayerLeaveServer -= OnLeave;
Expand Down
4 changes: 2 additions & 2 deletions Content.Client/Audio/ClientGlobalSoundSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public override void Initialize()
base.Initialize();
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestart);
SubscribeNetworkEvent<AdminSoundEvent>(PlayAdminSound);
_cfg.OnValueChanged(CCVars.AdminSoundsEnabled, ToggleAdminSound, true);
Subs.CVar(_cfg, CCVars.AdminSoundsEnabled, ToggleAdminSound, true);

SubscribeNetworkEvent<StationEventMusicEvent>(PlayStationEventMusic);
SubscribeNetworkEvent<StopStationEventMusic>(StopStationEventMusic);
_cfg.OnValueChanged(CCVars.EventMusicEnabled, ToggleStationEventMusic, true);
Subs.CVar(_cfg, CCVars.EventMusicEnabled, ToggleStationEventMusic, true);

SubscribeNetworkEvent<GameGlobalSoundEvent>(PlayGameSound);
}
Expand Down
Loading

0 comments on commit ccea851

Please sign in to comment.