Files
tbd-station-14/Content.Client/Clothing/UI/ChameleonMenu.xaml.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

88 lines
2.6 KiB
C#

using System.Linq;
using System.Numerics;
using Content.Client.Clothing.Systems;
using Content.Client.Stylesheets;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
namespace Content.Client.Clothing.UI;
[GenerateTypedNameReferences]
public sealed partial class ChameleonMenu : DefaultWindow
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
private readonly SpriteSystem _sprite;
public event Action<string>? OnIdSelected;
private IEnumerable<EntProtoId> _possibleIds = [];
private EntProtoId? _selectedId;
private string _searchFilter = "";
public ChameleonMenu()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_sprite = _entityManager.System<SpriteSystem>();
Search.OnTextChanged += OnSearchEntered;
}
public void UpdateState(IEnumerable<EntProtoId> possibleIds, string? selectedId)
{
_possibleIds = possibleIds;
_selectedId = selectedId;
UpdateGrid();
}
private void OnSearchEntered(LineEdit.LineEditEventArgs obj)
{
_searchFilter = obj.Text;
UpdateGrid();
}
private void UpdateGrid()
{
ClearGrid();
var group = new ButtonGroup();
var searchFilterLow = _searchFilter.ToLowerInvariant();
foreach (var id in _possibleIds)
{
if (!_prototypeManager.Resolve(id, out EntityPrototype? proto))
continue;
var lowId = id.Id.ToLowerInvariant();
var lowName = proto.Name.ToLowerInvariant();
if (!lowId.Contains(searchFilterLow) && !lowName.Contains(_searchFilter))
continue;
var button = new Button
{
MinSize = new Vector2(48, 48),
HorizontalExpand = true,
Group = group,
StyleClasses = {StyleBase.ButtonSquare},
ToggleMode = true,
Pressed = _selectedId == id,
ToolTip = proto.Name
};
button.OnPressed += _ => OnIdSelected?.Invoke(id);
Grid.AddChild(button);
var entityPrototypeView = new EntityPrototypeView();
button.AddChild(entityPrototypeView);
entityPrototypeView.SetPrototype(proto);
}
}
private void ClearGrid()
{
Grid.RemoveAllChildren();
}
}