Files
crystall-punk-14/Content.Shared/Silicons/StationAi/SharedStationAiSystem.Customization.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

83 lines
3.2 KiB
C#

using Content.Shared.Holopad;
using Robust.Shared.Prototypes;
namespace Content.Shared.Silicons.StationAi;
public abstract partial class SharedStationAiSystem
{
private ProtoId<StationAiCustomizationGroupPrototype> _stationAiCoreCustomGroupProtoId = "StationAiCoreIconography";
private ProtoId<StationAiCustomizationGroupPrototype> _stationAiHologramCustomGroupProtoId = "StationAiHolograms";
private void InitializeCustomization()
{
SubscribeLocalEvent<StationAiCoreComponent, StationAiCustomizationMessage>(OnStationAiCustomization);
}
private void OnStationAiCustomization(Entity<StationAiCoreComponent> entity, ref StationAiCustomizationMessage args)
{
if (!_protoManager.Resolve(args.GroupProtoId, out var groupPrototype) || !_protoManager.Resolve(args.CustomizationProtoId, out var customizationProto))
return;
if (!TryGetHeld((entity, entity.Comp), out var held))
return;
if (!TryComp<StationAiCustomizationComponent>(held, out var stationAiCustomization))
return;
if (stationAiCustomization.ProtoIds.TryGetValue(args.GroupProtoId, out var protoId) && protoId == args.CustomizationProtoId)
return;
stationAiCustomization.ProtoIds[args.GroupProtoId] = args.CustomizationProtoId;
Dirty(held, stationAiCustomization);
// Update hologram
if (groupPrototype.Category == StationAiCustomizationType.Hologram)
UpdateHolographicAvatar((held, stationAiCustomization));
// Update core iconography
if (groupPrototype.Category == StationAiCustomizationType.CoreIconography && TryComp<StationAiHolderComponent>(entity, out var stationAiHolder))
UpdateAppearance((entity, stationAiHolder));
}
private void UpdateHolographicAvatar(Entity<StationAiCustomizationComponent> entity)
{
if (!TryComp<HolographicAvatarComponent>(entity, out var avatar))
return;
if (!entity.Comp.ProtoIds.TryGetValue(_stationAiHologramCustomGroupProtoId, out var protoId))
return;
if (!_protoManager.Resolve(protoId, out var prototype))
return;
if (!prototype.LayerData.TryGetValue(StationAiState.Hologram.ToString(), out var layerData))
return;
avatar.LayerData = [layerData];
Dirty(entity, avatar);
}
private void CustomizeAppearance(Entity<StationAiCoreComponent> entity, StationAiState state)
{
var stationAi = GetInsertedAI(entity);
if (stationAi == null)
{
_appearance.RemoveData(entity.Owner, StationAiVisualState.Key);
return;
}
if (!TryComp<StationAiCustomizationComponent>(stationAi, out var stationAiCustomization) ||
!stationAiCustomization.ProtoIds.TryGetValue(_stationAiCoreCustomGroupProtoId, out var protoId) ||
!_protoManager.Resolve(protoId, out var prototype) ||
!prototype.LayerData.TryGetValue(state.ToString(), out var layerData))
{
return;
}
// This data is handled manually in the client StationAiSystem
_appearance.SetData(entity.Owner, StationAiVisualState.Key, layerData);
}
}