Files
tbd-station-14/Content.Shared/Access/Components/IdCardConsoleComponent.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

121 lines
4.0 KiB
C#

using Content.Shared.Access.Systems;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Roles;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
namespace Content.Shared.Access.Components;
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(SharedIdCardConsoleSystem))]
public sealed partial class IdCardConsoleComponent : Component
{
public static string PrivilegedIdCardSlotId = "IdCardConsole-privilegedId";
public static string TargetIdCardSlotId = "IdCardConsole-targetId";
[DataField]
public ItemSlot PrivilegedIdSlot = new();
[DataField]
public ItemSlot TargetIdSlot = new();
[Serializable, NetSerializable]
public sealed class WriteToTargetIdMessage : BoundUserInterfaceMessage
{
public readonly string FullName;
public readonly string JobTitle;
public readonly List<ProtoId<AccessLevelPrototype>> AccessList;
public readonly ProtoId<JobPrototype> JobPrototype;
public WriteToTargetIdMessage(string fullName, string jobTitle, List<ProtoId<AccessLevelPrototype>> accessList, ProtoId<JobPrototype> jobPrototype)
{
FullName = fullName;
JobTitle = jobTitle;
AccessList = accessList;
JobPrototype = jobPrototype;
}
}
// Put this on shared so we just send the state once in PVS range rather than every time the UI updates.
[DataField, AutoNetworkedField]
public List<ProtoId<AccessLevelPrototype>> AccessLevels = new()
{
"Armory",
"Atmospherics",
"Bar",
"Brig",
"Detective",
"Captain",
"Cargo",
"Chapel",
"Chemistry",
"ChiefEngineer",
"ChiefMedicalOfficer",
"Command",
"Cryogenics",
"Engineering",
"External",
"HeadOfPersonnel",
"HeadOfSecurity",
"Hydroponics",
"Janitor",
"Kitchen",
"Lawyer",
"Maintenance",
"Medical",
"Quartermaster",
"Research",
"ResearchDirector",
"Salvage",
"Security",
"Service",
"Theatre",
};
[Serializable, NetSerializable]
public sealed class IdCardConsoleBoundUserInterfaceState : BoundUserInterfaceState
{
public readonly string PrivilegedIdName;
public readonly bool IsPrivilegedIdPresent;
public readonly bool IsPrivilegedIdAuthorized;
public readonly bool IsTargetIdPresent;
public readonly string TargetIdName;
public readonly string? TargetIdFullName;
public readonly string? TargetIdJobTitle;
public readonly List<ProtoId<AccessLevelPrototype>>? TargetIdAccessList;
public readonly List<ProtoId<AccessLevelPrototype>>? AllowedModifyAccessList;
public readonly ProtoId<JobPrototype> TargetIdJobPrototype;
public IdCardConsoleBoundUserInterfaceState(bool isPrivilegedIdPresent,
bool isPrivilegedIdAuthorized,
bool isTargetIdPresent,
string? targetIdFullName,
string? targetIdJobTitle,
List<ProtoId<AccessLevelPrototype>>? targetIdAccessList,
List<ProtoId<AccessLevelPrototype>>? allowedModifyAccessList,
ProtoId<JobPrototype> targetIdJobPrototype,
string privilegedIdName,
string targetIdName)
{
IsPrivilegedIdPresent = isPrivilegedIdPresent;
IsPrivilegedIdAuthorized = isPrivilegedIdAuthorized;
IsTargetIdPresent = isTargetIdPresent;
TargetIdFullName = targetIdFullName;
TargetIdJobTitle = targetIdJobTitle;
TargetIdAccessList = targetIdAccessList;
AllowedModifyAccessList = allowedModifyAccessList;
TargetIdJobPrototype = targetIdJobPrototype;
PrivilegedIdName = privilegedIdName;
TargetIdName = targetIdName;
}
}
[Serializable, NetSerializable]
public enum IdCardConsoleUiKey : byte
{
Key,
}
}