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>
This commit is contained in:
Pieter-Jan Briers
2025-09-09 18:17:56 +02:00
committed by GitHub
parent fca45851cc
commit 0c97520276
136 changed files with 229 additions and 228 deletions

View File

@@ -146,8 +146,8 @@ public sealed class SuicideCommandTests
mobThresholdsComp = entManager.GetComponent<MobThresholdsComponent>(player);
damageableComp = entManager.GetComponent<DamageableComponent>(player);
if (protoMan.TryIndex(DamageType, out var slashProto))
damageableSystem.TryChangeDamage(player, new DamageSpecifier(slashProto, FixedPoint2.New(46.5)));
var slashProto = protoMan.Index(DamageType);
damageableSystem.TryChangeDamage(player, new DamageSpecifier(slashProto, FixedPoint2.New(46.5)));
});
// Check that running the suicide command kills the player

View File

@@ -27,8 +27,11 @@ public sealed class ContrabandTest
if (!proto.TryGetComponent<ContrabandComponent>(out var contraband, componentFactory))
continue;
Assert.That(protoMan.TryIndex(contraband.Severity, out var severity, false),
@$"{proto.ID} has a ContrabandComponent with a unknown severity.");
if (!protoMan.TryIndex(contraband.Severity, out var severity))
{
Assert.Fail($"{proto.ID} has a ContrabandComponent with a unknown severity.");
continue;
}
if (!severity.ShowDepartmentsAndJobs)
continue;

View File

@@ -88,14 +88,18 @@ public sealed class LatheTest
// Check each recipe assigned to this lathe
foreach (var recipeId in recipes)
{
Assert.That(protoMan.TryIndex(recipeId, out var recipeProto));
if (!protoMan.TryIndex(recipeId, out var recipeProto))
{
Assert.Fail($"Lathe recipe '{recipeId}' does not exist");
continue;
}
// Track the total material volume of the recipe
var totalQuantity = 0;
// Check each material called for by the recipe
foreach (var (materialId, quantity) in recipeProto.Materials)
{
Assert.That(protoMan.TryIndex(materialId, out var materialProto));
Assert.That(protoMan.HasIndex(materialId), $"Material '{materialId}' does not exist");
// Make sure the material is accepted by the lathe
Assert.That(acceptedMaterials, Does.Contain(materialId), $"Lathe {latheProto.ID} has recipe {recipeId} but does not accept any materials containing {materialId}");
totalQuantity += quantity;

View File

@@ -145,10 +145,7 @@ public sealed partial class MindTests
await server.WaitAssertion(() =>
{
var damageable = entMan.GetComponent<DamageableComponent>(entity);
if (!protoMan.TryIndex(BluntDamageType, out var prototype))
{
return;
}
var prototype = protoMan.Index(BluntDamageType);
damageableSystem.SetDamage(entity, damageable, new DamageSpecifier(prototype, FixedPoint2.New(401)));
Assert.That(mindSystem.GetMind(entity, mindContainerComp), Is.EqualTo(mindId));

View File

@@ -256,8 +256,7 @@ namespace Content.IntegrationTests.Tests
return;
var yamlEntities = node["entities"];
if (!protoManager.TryIndex(DoNotMapCategory, out var dnmCategory))
return;
var dnmCategory = protoManager.Index(DoNotMapCategory);
Assert.Multiple(() =>
{
@@ -266,7 +265,7 @@ namespace Content.IntegrationTests.Tests
var protoId = yamlEntity["proto"].AsString();
// This doesn't properly handle prototype migrations, but thats not a significant issue.
if (!protoManager.TryIndex(protoId, out var proto, false))
if (!protoManager.TryIndex(protoId, out var proto))
continue;
Assert.That(!proto.Categories.Contains(dnmCategory),