ProtodataTag.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Globalization;
  2. using Robust.Client.UserInterface.RichText;
  3. using Robust.Shared.Utility;
  4. namespace Content.Client.Guidebook.RichText;
  5. /// <summary>
  6. /// RichText tag that can display values extracted from entity prototypes.
  7. /// In order to be accessed by this tag, the desired field/property must
  8. /// be tagged with <see cref="Shared.Guidebook.GuidebookDataAttribute"/>.
  9. /// </summary>
  10. public sealed class ProtodataTag : IMarkupTag
  11. {
  12. [Dependency] private readonly ILogManager _logMan = default!;
  13. [Dependency] private readonly IEntityManager _entMan = default!;
  14. public string Name => "protodata";
  15. private ISawmill Log => _log ??= _logMan.GetSawmill("protodata_tag");
  16. private ISawmill? _log;
  17. public string TextBefore(MarkupNode node)
  18. {
  19. // Do nothing with an empty tag
  20. if (!node.Value.TryGetString(out var prototype))
  21. return string.Empty;
  22. if (!node.Attributes.TryGetValue("comp", out var component))
  23. return string.Empty;
  24. if (!node.Attributes.TryGetValue("member", out var member))
  25. return string.Empty;
  26. node.Attributes.TryGetValue("format", out var format);
  27. var guidebookData = _entMan.System<GuidebookDataSystem>();
  28. // Try to get the value
  29. if (!guidebookData.TryGetValue(prototype, component.StringValue!, member.StringValue!, out var value))
  30. {
  31. Log.Error($"Failed to find protodata for {component}.{member} in {prototype}");
  32. return "???";
  33. }
  34. // If we have a format string and a formattable value, format it as requested
  35. if (!string.IsNullOrEmpty(format.StringValue) && value is IFormattable formattable)
  36. return formattable.ToString(format.StringValue, CultureInfo.CurrentCulture);
  37. // No format string given, so just use default ToString
  38. return value?.ToString() ?? "NULL";
  39. }
  40. }