]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Remove instruments compref (#19560)
authormetalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Tue, 12 Sep 2023 04:43:06 +0000 (14:43 +1000)
committerGitHub <noreply@github.com>
Tue, 12 Sep 2023 04:43:06 +0000 (14:43 +1000)
Content.Client/Instruments/InstrumentComponent.cs
Content.Client/Instruments/InstrumentSystem.cs
Content.Server/Instruments/InstrumentComponent.cs
Content.Server/Instruments/InstrumentSystem.cs
Content.Server/Instruments/SwappableInstrumentSystem.cs
Content.Shared/Instruments/SharedInstrumentComponent.cs
Content.Shared/Instruments/SharedInstrumentSystem.cs

index 74218f802f642dd9873416014d7236da53ed662b..068dc26ac5f81d82052a23f6fb309be86e15cd46 100644 (file)
@@ -4,7 +4,7 @@ using Robust.Shared.Audio.Midi;
 
 namespace Content.Client.Instruments;
 
-[RegisterComponent, ComponentReference(typeof(SharedInstrumentComponent))]
+[RegisterComponent]
 public sealed partial class InstrumentComponent : SharedInstrumentComponent
 {
     public event Action? OnMidiPlaybackEnded;
index 0b969b6efde30ba3dbf16e982997d9db236102da..e89df99fd6f84bb1b4153eaf08879b95311cf7e9 100644 (file)
@@ -6,12 +6,12 @@ using JetBrains.Annotations;
 using Robust.Client.Audio.Midi;
 using Robust.Shared.Audio.Midi;
 using Robust.Shared.Configuration;
+using Robust.Shared.GameStates;
 using Robust.Shared.Network;
 using Robust.Shared.Timing;
 
 namespace Content.Client.Instruments;
 
-[UsedImplicitly]
 public sealed class InstrumentSystem : SharedInstrumentSystem
 {
     [Dependency] private readonly IClientNetManager _netManager = default!;
@@ -37,6 +37,27 @@ public sealed class InstrumentSystem : SharedInstrumentSystem
         SubscribeNetworkEvent<InstrumentStopMidiEvent>(OnMidiStop);
 
         SubscribeLocalEvent<InstrumentComponent, ComponentShutdown>(OnShutdown);
+        SubscribeLocalEvent<InstrumentComponent, ComponentHandleState>(OnHandleState);
+    }
+
+    private void OnHandleState(EntityUid uid, SharedInstrumentComponent component, ref ComponentHandleState args)
+    {
+        if (args.Current is not InstrumentComponentState state)
+            return;
+
+        component.Playing = state.Playing;
+        component.InstrumentProgram = state.InstrumentProgram;
+        component.InstrumentBank = state.InstrumentBank;
+        component.AllowPercussion = state.AllowPercussion;
+        component.AllowProgramChange = state.AllowProgramChange;
+        component.RespectMidiLimits = state.RespectMidiLimits;
+        component.Master = EnsureEntity<InstrumentComponent>(state.Master, uid);
+        component.FilteredChannels = state.FilteredChannels;
+
+        if (component.Playing)
+            SetupRenderer(uid, true, component);
+        else
+            EndRenderer(uid, true, component);
     }
 
     public override void Shutdown()
@@ -71,9 +92,19 @@ public sealed class InstrumentSystem : SharedInstrumentSystem
         RaiseNetworkEvent(new InstrumentSetFilteredChannelEvent(GetNetEntity(uid), channel, value));
     }
 
+    public override bool ResolveInstrument(EntityUid uid, ref SharedInstrumentComponent? component)
+    {
+        if (component is not null)
+            return true;
+
+        TryComp<InstrumentComponent>(uid, out var localComp);
+        component = localComp;
+        return component != null;
+    }
+
     public override void SetupRenderer(EntityUid uid, bool fromStateChange, SharedInstrumentComponent? component = null)
     {
-        if (!Resolve(uid, ref component))
+        if (!ResolveInstrument(uid, ref component))
             return;
 
         if (component is not InstrumentComponent instrument)
@@ -156,7 +187,7 @@ public sealed class InstrumentSystem : SharedInstrumentSystem
 
     public override void EndRenderer(EntityUid uid, bool fromStateChange, SharedInstrumentComponent? component = null)
     {
-        if (!Resolve(uid, ref component, false))
+        if (!ResolveInstrument(uid, ref component))
             return;
 
         if (component is not InstrumentComponent instrument)
index 51de0ed35bb1c4d4857970a38ef85a11ea5f5e9f..5a6b5828da2ed4c40926221d1da31815c431bfe4 100644 (file)
@@ -5,7 +5,7 @@ using Robust.Server.Player;
 
 namespace Content.Server.Instruments;
 
-[RegisterComponent, ComponentReference(typeof(SharedInstrumentComponent))]
+[RegisterComponent]
 public sealed partial class InstrumentComponent : SharedInstrumentComponent
 {
     [Dependency] private readonly IEntityManager _entMan = default!;
index 70947069508525f36aa31c66c86c2bd839f72e12..4e002eb677c1f6b08504e345dd774d2eedbf5686 100644 (file)
@@ -15,6 +15,7 @@ using Robust.Shared.Audio.Midi;
 using Robust.Shared.Collections;
 using Robust.Shared.Configuration;
 using Robust.Shared.Console;
+using Robust.Shared.GameStates;
 using Robust.Shared.Timing;
 using Robust.Shared.Utility;
 
@@ -55,9 +56,26 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
         SubscribeLocalEvent<InstrumentComponent, BoundUIOpenedEvent>(OnBoundUIOpened);
         SubscribeLocalEvent<InstrumentComponent, InstrumentBandRequestBuiMessage>(OnBoundUIRequestBands);
 
+        SubscribeLocalEvent<InstrumentComponent, ComponentGetState>(OnStrumentGetState);
+
         _conHost.RegisterCommand("addtoband", AddToBandCommand);
     }
 
+    private void OnStrumentGetState(EntityUid uid, InstrumentComponent component, ref ComponentGetState args)
+    {
+        args.State = new InstrumentComponentState()
+        {
+            Playing = component.Playing,
+            InstrumentProgram = component.InstrumentProgram,
+            InstrumentBank = component.InstrumentBank,
+            AllowPercussion = component.AllowPercussion,
+            AllowProgramChange = component.AllowProgramChange,
+            RespectMidiLimits = component.RespectMidiLimits,
+            Master = GetNetEntity(component.Master),
+            FilteredChannels = component.FilteredChannels
+        };
+    }
+
     [AdminCommand(AdminFlags.Fun)]
     private void AddToBandCommand(IConsoleShell shell, string _, string[] args)
     {
@@ -441,4 +459,14 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
         if (_bui.TryGetUi(uid, InstrumentUiKey.Key, out var bui))
             _bui.ToggleUi(bui, session);
     }
+
+    public override bool ResolveInstrument(EntityUid uid, ref SharedInstrumentComponent? component)
+    {
+        if (component is not null)
+            return true;
+
+        TryComp<InstrumentComponent>(uid, out var localComp);
+        component = localComp;
+        return component != null;
+    }
 }
index 465cdb7fdf30250077a6226d64e95b83822628ac..3f3cfb9e6dba0dbf58e216ae2465e3d75a7c4752 100644 (file)
@@ -22,7 +22,7 @@ public sealed class SwappableInstrumentSystem : EntitySystem
         if (!args.CanInteract || !args.CanAccess || component.InstrumentList.Count <= 1)
             return;
 
-        if (!TryComp<SharedInstrumentComponent>(uid, out var instrument))
+        if (!TryComp<InstrumentComponent>(uid, out var instrument))
             return;
 
         var priority = 0;
index cdd85b90ac2cc98336b068fc72a5b1b0d6117423..73500f3869d27c3dbd1e8bb6a89fc2546b170978 100644 (file)
@@ -6,35 +6,54 @@ using Robust.Shared.Serialization;
 namespace Content.Shared.Instruments;
 
 [NetworkedComponent]
-[AutoGenerateComponentState(true)]
 [Access(typeof(SharedInstrumentSystem))]
 public abstract partial class SharedInstrumentComponent : Component
 {
-    [ViewVariables, AutoNetworkedField]
+    [ViewVariables]
     public bool Playing { get; set; }
 
-    [DataField("program"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
+    [DataField("program"), ViewVariables(VVAccess.ReadWrite)]
     public byte InstrumentProgram { get; set; }
 
-    [DataField("bank"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
+    [DataField("bank"), ViewVariables(VVAccess.ReadWrite)]
     public byte InstrumentBank { get; set; }
 
-    [DataField("allowPercussion"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
+    [DataField("allowPercussion"), ViewVariables(VVAccess.ReadWrite)]
     public bool AllowPercussion { get; set; }
 
-    [DataField("allowProgramChange"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
+    [DataField("allowProgramChange"), ViewVariables(VVAccess.ReadWrite)]
     public bool AllowProgramChange { get ; set; }
 
-    [DataField("respectMidiLimits"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
+    [DataField("respectMidiLimits"), ViewVariables(VVAccess.ReadWrite)]
     public bool RespectMidiLimits { get; set; } = true;
 
-    [ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
+    [ViewVariables(VVAccess.ReadWrite)]
     public EntityUid? Master { get; set; } = null;
 
-    [ViewVariables, AutoNetworkedField]
+    [ViewVariables]
     public BitArray FilteredChannels { get; set; } = new(RobustMidiEvent.MaxChannels, true);
 }
 
+[Serializable, NetSerializable]
+public sealed class InstrumentComponentState : ComponentState
+{
+    public bool Playing;
+
+    public byte InstrumentProgram;
+
+    public byte InstrumentBank;
+
+    public bool AllowPercussion;
+
+    public bool AllowProgramChange;
+
+    public bool RespectMidiLimits;
+
+    public NetEntity? Master;
+
+    public BitArray FilteredChannels = default!;
+}
+
 
 /// <summary>
 ///     This message is sent to the client to completely stop midi input and midi playback.
index a2a7425a5bc7e57642bd223a64b05b4db1172298..87e3a69489c468cf18a02c10d78ded7713ade9ff 100644 (file)
@@ -2,10 +2,7 @@ namespace Content.Shared.Instruments;
 
 public abstract class SharedInstrumentSystem : EntitySystem
 {
-    public override void Initialize()
-    {
-        SubscribeLocalEvent<SharedInstrumentComponent, AfterAutoHandleStateEvent>(AfterHandleInstrumentState);
-    }
+    public abstract bool ResolveInstrument(EntityUid uid, ref SharedInstrumentComponent? component);
 
     public virtual void SetupRenderer(EntityUid uid, bool fromStateChange, SharedInstrumentComponent? instrument = null)
     {
@@ -21,12 +18,4 @@ public abstract class SharedInstrumentSystem : EntitySystem
         component.InstrumentProgram = program;
         Dirty(component);
     }
-
-    private void AfterHandleInstrumentState(EntityUid uid, SharedInstrumentComponent instrument, ref AfterAutoHandleStateEvent args)
-    {
-        if(instrument.Playing)
-            SetupRenderer(uid, true, instrument);
-        else
-            EndRenderer(uid, true, instrument);
-    }
 }