Files
tbd-station-14/Content.Client/Storage/Visualizers/EntityStorageVisualizerSystem.cs
Pieter-Jan Briers 0c97520276 Fix usages of TryIndex() (#39124)
* 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>
2025-09-09 18:17:56 +02:00

112 lines
4.6 KiB
C#

using Content.Shared.SprayPainter.Prototypes;
using Content.Shared.Storage;
using Robust.Client.GameObjects;
using Robust.Shared.Prototypes;
namespace Content.Client.Storage.Visualizers;
public sealed class EntityStorageVisualizerSystem : VisualizerSystem<EntityStorageVisualsComponent>
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IComponentFactory _componentFactory = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EntityStorageVisualsComponent, ComponentInit>(OnComponentInit);
}
/// <summary>
/// Sets the base sprite to this layer. Exists to make the inheritance tree less boilerplate-y.
/// </summary>
private void OnComponentInit(EntityUid uid, EntityStorageVisualsComponent comp, ComponentInit args)
{
if (comp.StateBaseClosed == null)
return;
comp.StateBaseOpen ??= comp.StateBaseClosed;
if (!TryComp<SpriteComponent>(uid, out var sprite))
return;
SpriteSystem.LayerSetRsiState((uid, sprite), StorageVisualLayers.Base, comp.StateBaseClosed);
}
protected override void OnAppearanceChange(EntityUid uid,
EntityStorageVisualsComponent comp,
ref AppearanceChangeEvent args)
{
if (args.Sprite == null
|| !AppearanceSystem.TryGetData<bool>(uid, StorageVisuals.Open, out var open, args.Component))
return;
var forceRedrawBase = false;
if (AppearanceSystem.TryGetData<string>(uid, PaintableVisuals.Prototype, out var prototype, args.Component))
{
if (_prototypeManager.Resolve(prototype, out var proto))
{
if (proto.TryGetComponent(out SpriteComponent? sprite, _componentFactory))
{
SpriteSystem.SetBaseRsi((uid, args.Sprite), sprite.BaseRSI);
}
if (proto.TryGetComponent(out EntityStorageVisualsComponent? visuals, _componentFactory))
{
comp.StateBaseOpen = visuals.StateBaseOpen;
comp.StateBaseClosed = visuals.StateBaseClosed;
comp.StateDoorOpen = visuals.StateDoorOpen;
comp.StateDoorClosed = visuals.StateDoorClosed;
forceRedrawBase = true;
}
}
}
// Open/Closed state for the storage entity.
if (SpriteSystem.LayerMapTryGet((uid, args.Sprite), StorageVisualLayers.Door, out _, false))
{
if (open)
{
if (comp.OpenDrawDepth != null)
SpriteSystem.SetDrawDepth((uid, args.Sprite), comp.OpenDrawDepth.Value);
if (comp.StateDoorOpen != null)
{
SpriteSystem.LayerSetRsiState((uid, args.Sprite), StorageVisualLayers.Door, comp.StateDoorOpen);
SpriteSystem.LayerSetVisible((uid, args.Sprite), StorageVisualLayers.Door, true);
}
else
{
SpriteSystem.LayerSetVisible((uid, args.Sprite), StorageVisualLayers.Door, false);
}
if (comp.StateBaseOpen != null)
SpriteSystem.LayerSetRsiState((uid, args.Sprite), StorageVisualLayers.Base, comp.StateBaseOpen);
else if (forceRedrawBase && comp.StateBaseClosed != null)
SpriteSystem.LayerSetRsiState((uid, args.Sprite), StorageVisualLayers.Base, comp.StateBaseClosed);
}
else
{
if (comp.ClosedDrawDepth != null)
SpriteSystem.SetDrawDepth((uid, args.Sprite), comp.ClosedDrawDepth.Value);
if (comp.StateDoorClosed != null)
{
SpriteSystem.LayerSetRsiState((uid, args.Sprite), StorageVisualLayers.Door, comp.StateDoorClosed);
SpriteSystem.LayerSetVisible((uid, args.Sprite), StorageVisualLayers.Door, true);
}
else
SpriteSystem.LayerSetVisible((uid, args.Sprite), StorageVisualLayers.Door, false);
if (comp.StateBaseClosed != null)
SpriteSystem.LayerSetRsiState((uid, args.Sprite), StorageVisualLayers.Base, comp.StateBaseClosed);
else if (forceRedrawBase && comp.StateBaseOpen != null)
SpriteSystem.LayerSetRsiState((uid, args.Sprite), StorageVisualLayers.Base, comp.StateBaseOpen);
}
}
}
}
public enum StorageVisualLayers : byte
{
Base,
Door
}