* Fix usages of TryIndex()
Most usages of TryIndex() were using it incorrectly. Checking whether prototype IDs specified in prototypes actually existed before using them. This is not appropriate as it's just hiding bugs that should be getting caught by the YAML linter and other tools. (#39115)
This then resulted in TryIndex() getting modified to log errors (94f98073b0), which is incorrect as it causes false-positive errors in proper uses of the API: external data validation. (#39098)
This commit goes through and checks every call site of TryIndex() to see whether they were correct. Most call sites were replaced with the new Resolve(), which is suitable for these "defensive programming" use cases.
Fixes #39115
Breaking change: while doing this I noticed IdCardComponent and related systems were erroneously using ProtoId<AccessLevelPrototype> for job prototypes. This has been corrected.
* fix tests
---------
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
158 lines
5.2 KiB
C#
158 lines
5.2 KiB
C#
using Content.Server.Power.Components;
|
|
using Content.Server.Power.EntitySystems;
|
|
using Content.Shared.Audio.Jukebox;
|
|
using Content.Shared.Power;
|
|
using Robust.Server.GameObjects;
|
|
using Robust.Shared.Audio;
|
|
using Robust.Shared.Audio.Components;
|
|
using Robust.Shared.Audio.Systems;
|
|
using Robust.Shared.Player;
|
|
using Robust.Shared.Prototypes;
|
|
using JukeboxComponent = Content.Shared.Audio.Jukebox.JukeboxComponent;
|
|
|
|
namespace Content.Server.Audio.Jukebox;
|
|
|
|
|
|
public sealed class JukeboxSystem : SharedJukeboxSystem
|
|
{
|
|
[Dependency] private readonly IPrototypeManager _protoManager = default!;
|
|
[Dependency] private readonly AppearanceSystem _appearanceSystem = default!;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
SubscribeLocalEvent<JukeboxComponent, JukeboxSelectedMessage>(OnJukeboxSelected);
|
|
SubscribeLocalEvent<JukeboxComponent, JukeboxPlayingMessage>(OnJukeboxPlay);
|
|
SubscribeLocalEvent<JukeboxComponent, JukeboxPauseMessage>(OnJukeboxPause);
|
|
SubscribeLocalEvent<JukeboxComponent, JukeboxStopMessage>(OnJukeboxStop);
|
|
SubscribeLocalEvent<JukeboxComponent, JukeboxSetTimeMessage>(OnJukeboxSetTime);
|
|
SubscribeLocalEvent<JukeboxComponent, ComponentInit>(OnComponentInit);
|
|
SubscribeLocalEvent<JukeboxComponent, ComponentShutdown>(OnComponentShutdown);
|
|
|
|
SubscribeLocalEvent<JukeboxComponent, PowerChangedEvent>(OnPowerChanged);
|
|
}
|
|
|
|
private void OnComponentInit(EntityUid uid, JukeboxComponent component, ComponentInit args)
|
|
{
|
|
if (HasComp<ApcPowerReceiverComponent>(uid))
|
|
{
|
|
TryUpdateVisualState(uid, component);
|
|
}
|
|
}
|
|
|
|
private void OnJukeboxPlay(EntityUid uid, JukeboxComponent component, ref JukeboxPlayingMessage args)
|
|
{
|
|
if (Exists(component.AudioStream))
|
|
{
|
|
Audio.SetState(component.AudioStream, AudioState.Playing);
|
|
}
|
|
else
|
|
{
|
|
component.AudioStream = Audio.Stop(component.AudioStream);
|
|
|
|
if (string.IsNullOrEmpty(component.SelectedSongId) ||
|
|
!_protoManager.Resolve(component.SelectedSongId, out var jukeboxProto))
|
|
{
|
|
return;
|
|
}
|
|
|
|
component.AudioStream = Audio.PlayPvs(jukeboxProto.Path, uid, AudioParams.Default.WithMaxDistance(10f))?.Entity;
|
|
Dirty(uid, component);
|
|
}
|
|
}
|
|
|
|
private void OnJukeboxPause(Entity<JukeboxComponent> ent, ref JukeboxPauseMessage args)
|
|
{
|
|
Audio.SetState(ent.Comp.AudioStream, AudioState.Paused);
|
|
}
|
|
|
|
private void OnJukeboxSetTime(EntityUid uid, JukeboxComponent component, JukeboxSetTimeMessage args)
|
|
{
|
|
if (TryComp(args.Actor, out ActorComponent? actorComp))
|
|
{
|
|
var offset = actorComp.PlayerSession.Channel.Ping * 1.5f / 1000f;
|
|
Audio.SetPlaybackPosition(component.AudioStream, args.SongTime + offset);
|
|
}
|
|
}
|
|
|
|
private void OnPowerChanged(Entity<JukeboxComponent> entity, ref PowerChangedEvent args)
|
|
{
|
|
TryUpdateVisualState(entity);
|
|
|
|
if (!this.IsPowered(entity.Owner, EntityManager))
|
|
{
|
|
Stop(entity);
|
|
}
|
|
}
|
|
|
|
private void OnJukeboxStop(Entity<JukeboxComponent> entity, ref JukeboxStopMessage args)
|
|
{
|
|
Stop(entity);
|
|
}
|
|
|
|
private void Stop(Entity<JukeboxComponent> entity)
|
|
{
|
|
Audio.SetState(entity.Comp.AudioStream, AudioState.Stopped);
|
|
Dirty(entity);
|
|
}
|
|
|
|
private void OnJukeboxSelected(EntityUid uid, JukeboxComponent component, JukeboxSelectedMessage args)
|
|
{
|
|
if (!Audio.IsPlaying(component.AudioStream))
|
|
{
|
|
component.SelectedSongId = args.SongId;
|
|
DirectSetVisualState(uid, JukeboxVisualState.Select);
|
|
component.Selecting = true;
|
|
component.AudioStream = Audio.Stop(component.AudioStream);
|
|
}
|
|
|
|
Dirty(uid, component);
|
|
}
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
base.Update(frameTime);
|
|
|
|
var query = EntityQueryEnumerator<JukeboxComponent>();
|
|
while (query.MoveNext(out var uid, out var comp))
|
|
{
|
|
if (comp.Selecting)
|
|
{
|
|
comp.SelectAccumulator += frameTime;
|
|
if (comp.SelectAccumulator >= 0.5f)
|
|
{
|
|
comp.SelectAccumulator = 0f;
|
|
comp.Selecting = false;
|
|
|
|
TryUpdateVisualState(uid, comp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnComponentShutdown(EntityUid uid, JukeboxComponent component, ComponentShutdown args)
|
|
{
|
|
component.AudioStream = Audio.Stop(component.AudioStream);
|
|
}
|
|
|
|
private void DirectSetVisualState(EntityUid uid, JukeboxVisualState state)
|
|
{
|
|
_appearanceSystem.SetData(uid, JukeboxVisuals.VisualState, state);
|
|
}
|
|
|
|
private void TryUpdateVisualState(EntityUid uid, JukeboxComponent? jukeboxComponent = null)
|
|
{
|
|
if (!Resolve(uid, ref jukeboxComponent))
|
|
return;
|
|
|
|
var finalState = JukeboxVisualState.On;
|
|
|
|
if (!this.IsPowered(uid, EntityManager))
|
|
{
|
|
finalState = JukeboxVisualState.Off;
|
|
}
|
|
|
|
_appearanceSystem.SetData(uid, JukeboxVisuals.VisualState, finalState);
|
|
}
|
|
}
|