Files
tbd-station-14/Content.Client/Store/Ui/StoreWithdrawWindow.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

95 lines
2.9 KiB
C#

using System.Linq;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Content.Shared.FixedPoint;
using Content.Shared.Store;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Prototypes;
namespace Content.Client.Store.Ui;
/// <summary>
/// Window to select amount TC to withdraw from Uplink account
/// Used as sub-window in Uplink UI
/// </summary>
[GenerateTypedNameReferences]
public sealed partial class StoreWithdrawWindow : DefaultWindow
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private Dictionary<CurrencyPrototype, FixedPoint2> _validCurrencies = new();
private HashSet<CurrencyWithdrawButton> _buttons = new();
public event Action<BaseButton.ButtonEventArgs, string, int>? OnWithdrawAttempt;
public StoreWithdrawWindow()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
}
public void CreateCurrencyButtons(Dictionary<ProtoId<CurrencyPrototype>, FixedPoint2> balance)
{
_validCurrencies.Clear();
foreach (var currency in balance)
{
if (!_prototypeManager.Resolve(currency.Key, out var proto))
continue;
_validCurrencies.Add(proto, currency.Value);
}
//this shouldn't ever happen but w/e
if (_validCurrencies.Count < 1)
return;
ButtonContainer.Children.Clear();
_buttons.Clear();
foreach (var currency in _validCurrencies)
{
if (!currency.Key.CanWithdraw)
continue;
var button = new CurrencyWithdrawButton()
{
Id = currency.Key.ID,
Amount = currency.Value,
MinHeight = 20,
Text = Loc.GetString("store-withdraw-button-ui", ("currency",Loc.GetString(currency.Key.DisplayName, ("amount", currency.Value)))),
Disabled = false,
};
button.OnPressed += args =>
{
OnWithdrawAttempt?.Invoke(args, button.Id, WithdrawSlider.Value);
Close();
};
_buttons.Add(button);
ButtonContainer.AddChild(button);
}
var maxWithdrawAmount = _validCurrencies.Values.Max().Int();
// setup withdraw slider
WithdrawSlider.MinValue = 1;
WithdrawSlider.MaxValue = maxWithdrawAmount;
WithdrawSlider.OnValueChanged += OnValueChanged;
OnValueChanged(WithdrawSlider.Value);
}
public void OnValueChanged(int i)
{
foreach (var button in _buttons)
{
button.Disabled = button.Amount < WithdrawSlider.Value;
}
}
private sealed class CurrencyWithdrawButton : Button
{
public string? Id;
public FixedPoint2 Amount = FixedPoint2.Zero;
}
}