Remove instruments compref (#19560)

This commit is contained in:
metalgearsloth
2023-09-12 14:43:06 +10:00
committed by GitHub
parent f05f94fdc0
commit fd60d39bc7
7 changed files with 94 additions and 27 deletions

View File

@@ -4,7 +4,7 @@ using Robust.Shared.Audio.Midi;
namespace Content.Client.Instruments; namespace Content.Client.Instruments;
[RegisterComponent, ComponentReference(typeof(SharedInstrumentComponent))] [RegisterComponent]
public sealed partial class InstrumentComponent : SharedInstrumentComponent public sealed partial class InstrumentComponent : SharedInstrumentComponent
{ {
public event Action? OnMidiPlaybackEnded; public event Action? OnMidiPlaybackEnded;

View File

@@ -6,12 +6,12 @@ using JetBrains.Annotations;
using Robust.Client.Audio.Midi; using Robust.Client.Audio.Midi;
using Robust.Shared.Audio.Midi; using Robust.Shared.Audio.Midi;
using Robust.Shared.Configuration; using Robust.Shared.Configuration;
using Robust.Shared.GameStates;
using Robust.Shared.Network; using Robust.Shared.Network;
using Robust.Shared.Timing; using Robust.Shared.Timing;
namespace Content.Client.Instruments; namespace Content.Client.Instruments;
[UsedImplicitly]
public sealed class InstrumentSystem : SharedInstrumentSystem public sealed class InstrumentSystem : SharedInstrumentSystem
{ {
[Dependency] private readonly IClientNetManager _netManager = default!; [Dependency] private readonly IClientNetManager _netManager = default!;
@@ -37,6 +37,27 @@ public sealed class InstrumentSystem : SharedInstrumentSystem
SubscribeNetworkEvent<InstrumentStopMidiEvent>(OnMidiStop); SubscribeNetworkEvent<InstrumentStopMidiEvent>(OnMidiStop);
SubscribeLocalEvent<InstrumentComponent, ComponentShutdown>(OnShutdown); 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() public override void Shutdown()
@@ -71,9 +92,19 @@ public sealed class InstrumentSystem : SharedInstrumentSystem
RaiseNetworkEvent(new InstrumentSetFilteredChannelEvent(GetNetEntity(uid), channel, value)); 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) public override void SetupRenderer(EntityUid uid, bool fromStateChange, SharedInstrumentComponent? component = null)
{ {
if (!Resolve(uid, ref component)) if (!ResolveInstrument(uid, ref component))
return; return;
if (component is not InstrumentComponent instrument) 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) public override void EndRenderer(EntityUid uid, bool fromStateChange, SharedInstrumentComponent? component = null)
{ {
if (!Resolve(uid, ref component, false)) if (!ResolveInstrument(uid, ref component))
return; return;
if (component is not InstrumentComponent instrument) if (component is not InstrumentComponent instrument)

View File

@@ -5,7 +5,7 @@ using Robust.Server.Player;
namespace Content.Server.Instruments; namespace Content.Server.Instruments;
[RegisterComponent, ComponentReference(typeof(SharedInstrumentComponent))] [RegisterComponent]
public sealed partial class InstrumentComponent : SharedInstrumentComponent public sealed partial class InstrumentComponent : SharedInstrumentComponent
{ {
[Dependency] private readonly IEntityManager _entMan = default!; [Dependency] private readonly IEntityManager _entMan = default!;

View File

@@ -15,6 +15,7 @@ using Robust.Shared.Audio.Midi;
using Robust.Shared.Collections; using Robust.Shared.Collections;
using Robust.Shared.Configuration; using Robust.Shared.Configuration;
using Robust.Shared.Console; using Robust.Shared.Console;
using Robust.Shared.GameStates;
using Robust.Shared.Timing; using Robust.Shared.Timing;
using Robust.Shared.Utility; using Robust.Shared.Utility;
@@ -55,9 +56,26 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
SubscribeLocalEvent<InstrumentComponent, BoundUIOpenedEvent>(OnBoundUIOpened); SubscribeLocalEvent<InstrumentComponent, BoundUIOpenedEvent>(OnBoundUIOpened);
SubscribeLocalEvent<InstrumentComponent, InstrumentBandRequestBuiMessage>(OnBoundUIRequestBands); SubscribeLocalEvent<InstrumentComponent, InstrumentBandRequestBuiMessage>(OnBoundUIRequestBands);
SubscribeLocalEvent<InstrumentComponent, ComponentGetState>(OnStrumentGetState);
_conHost.RegisterCommand("addtoband", AddToBandCommand); _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)] [AdminCommand(AdminFlags.Fun)]
private void AddToBandCommand(IConsoleShell shell, string _, string[] args) 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)) if (_bui.TryGetUi(uid, InstrumentUiKey.Key, out var bui))
_bui.ToggleUi(bui, session); _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;
}
} }

View File

@@ -22,7 +22,7 @@ public sealed class SwappableInstrumentSystem : EntitySystem
if (!args.CanInteract || !args.CanAccess || component.InstrumentList.Count <= 1) if (!args.CanInteract || !args.CanAccess || component.InstrumentList.Count <= 1)
return; return;
if (!TryComp<SharedInstrumentComponent>(uid, out var instrument)) if (!TryComp<InstrumentComponent>(uid, out var instrument))
return; return;
var priority = 0; var priority = 0;

View File

@@ -6,35 +6,54 @@ using Robust.Shared.Serialization;
namespace Content.Shared.Instruments; namespace Content.Shared.Instruments;
[NetworkedComponent] [NetworkedComponent]
[AutoGenerateComponentState(true)]
[Access(typeof(SharedInstrumentSystem))] [Access(typeof(SharedInstrumentSystem))]
public abstract partial class SharedInstrumentComponent : Component public abstract partial class SharedInstrumentComponent : Component
{ {
[ViewVariables, AutoNetworkedField] [ViewVariables]
public bool Playing { get; set; } public bool Playing { get; set; }
[DataField("program"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] [DataField("program"), ViewVariables(VVAccess.ReadWrite)]
public byte InstrumentProgram { get; set; } public byte InstrumentProgram { get; set; }
[DataField("bank"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] [DataField("bank"), ViewVariables(VVAccess.ReadWrite)]
public byte InstrumentBank { get; set; } public byte InstrumentBank { get; set; }
[DataField("allowPercussion"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] [DataField("allowPercussion"), ViewVariables(VVAccess.ReadWrite)]
public bool AllowPercussion { get; set; } public bool AllowPercussion { get; set; }
[DataField("allowProgramChange"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] [DataField("allowProgramChange"), ViewVariables(VVAccess.ReadWrite)]
public bool AllowProgramChange { get ; set; } public bool AllowProgramChange { get ; set; }
[DataField("respectMidiLimits"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] [DataField("respectMidiLimits"), ViewVariables(VVAccess.ReadWrite)]
public bool RespectMidiLimits { get; set; } = true; public bool RespectMidiLimits { get; set; } = true;
[ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] [ViewVariables(VVAccess.ReadWrite)]
public EntityUid? Master { get; set; } = null; public EntityUid? Master { get; set; } = null;
[ViewVariables, AutoNetworkedField] [ViewVariables]
public BitArray FilteredChannels { get; set; } = new(RobustMidiEvent.MaxChannels, true); 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> /// <summary>
/// This message is sent to the client to completely stop midi input and midi playback. /// This message is sent to the client to completely stop midi input and midi playback.

View File

@@ -2,10 +2,7 @@ namespace Content.Shared.Instruments;
public abstract class SharedInstrumentSystem : EntitySystem public abstract class SharedInstrumentSystem : EntitySystem
{ {
public override void Initialize() public abstract bool ResolveInstrument(EntityUid uid, ref SharedInstrumentComponent? component);
{
SubscribeLocalEvent<SharedInstrumentComponent, AfterAutoHandleStateEvent>(AfterHandleInstrumentState);
}
public virtual void SetupRenderer(EntityUid uid, bool fromStateChange, SharedInstrumentComponent? instrument = null) public virtual void SetupRenderer(EntityUid uid, bool fromStateChange, SharedInstrumentComponent? instrument = null)
{ {
@@ -21,12 +18,4 @@ public abstract class SharedInstrumentSystem : EntitySystem
component.InstrumentProgram = program; component.InstrumentProgram = program;
Dirty(component); Dirty(component);
} }
private void AfterHandleInstrumentState(EntityUid uid, SharedInstrumentComponent instrument, ref AfterAutoHandleStateEvent args)
{
if(instrument.Playing)
SetupRenderer(uid, true, instrument);
else
EndRenderer(uid, true, instrument);
}
} }