* 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>
55 lines
2.3 KiB
C#
55 lines
2.3 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using Robust.Shared.Collections;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Random;
|
|
|
|
namespace Content.Shared.StoryGen;
|
|
|
|
/// <summary>
|
|
/// Provides functionality to generate a story from a <see cref="StoryTemplatePrototype"/>.
|
|
/// </summary>
|
|
public sealed partial class StoryGeneratorSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly IPrototypeManager _protoMan = default!;
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
|
|
|
/// <summary>
|
|
/// Tries to generate a random story using the given template, picking a random word from the referenced
|
|
/// datasets for each variable and passing them into the localization system with template.
|
|
/// If <paramref name="seed"/> is specified, the randomizer will be seeded with it for consistent story generation;
|
|
/// otherwise the variables will be randomized.
|
|
/// Fails if the template prototype cannot be loaded.
|
|
/// </summary>
|
|
/// <returns>true if the template was loaded, otherwise false.</returns>
|
|
public bool TryGenerateStoryFromTemplate(ProtoId<StoryTemplatePrototype> template, [NotNullWhen(true)] out string? story, int? seed = null)
|
|
{
|
|
// Get the story template prototype from the ID
|
|
if (!_protoMan.Resolve(template, out var templateProto))
|
|
{
|
|
story = null;
|
|
return false;
|
|
}
|
|
|
|
// If given a seed, use it
|
|
if (seed != null)
|
|
_random.SetSeed(seed.Value);
|
|
|
|
// Pick values for all of the variables in the template
|
|
var variables = new ValueList<(string, object)>(templateProto.Variables.Count);
|
|
foreach (var (name, list) in templateProto.Variables)
|
|
{
|
|
// Get the prototype for the world list dataset
|
|
if (!_protoMan.Resolve(list, out var listProto))
|
|
continue; // Missed one, but keep going with the rest of the story
|
|
|
|
// Pick a random word from the dataset and localize it
|
|
var chosenWord = Loc.GetString(_random.Pick(listProto.Values));
|
|
variables.Add((name, chosenWord));
|
|
}
|
|
|
|
// Pass the variables to the localization system and build the story
|
|
story = Loc.GetString(templateProto.LocId, variables.ToArray());
|
|
return true;
|
|
}
|
|
}
|