ConstructionGraphStepTypeSerializer.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Robust.Shared.Serialization;
  2. using Robust.Shared.Serialization.Manager;
  3. using Robust.Shared.Serialization.Markdown.Mapping;
  4. using Robust.Shared.Serialization.Markdown.Validation;
  5. using Robust.Shared.Serialization.TypeSerializers.Interfaces;
  6. namespace Content.Shared.Construction.Steps
  7. {
  8. [TypeSerializer]
  9. public sealed class ConstructionGraphStepTypeSerializer : ITypeReader<ConstructionGraphStep, MappingDataNode>
  10. {
  11. private Type? GetType(MappingDataNode node)
  12. {
  13. if (node.Has("material"))
  14. {
  15. return typeof(MaterialConstructionGraphStep);
  16. }
  17. if (node.Has("tool"))
  18. {
  19. return typeof(ToolConstructionGraphStep);
  20. }
  21. if (node.Has("component"))
  22. {
  23. return typeof(ComponentConstructionGraphStep);
  24. }
  25. if (node.Has("tag"))
  26. {
  27. return typeof(TagConstructionGraphStep);
  28. }
  29. if (node.Has("allTags") || node.Has("anyTags"))
  30. {
  31. return typeof(MultipleTagsConstructionGraphStep);
  32. }
  33. if (node.Has("minTemperature") || node.Has("maxTemperature"))
  34. {
  35. return typeof(TemperatureConstructionGraphStep);
  36. }
  37. if (node.Has("assemblyId") || node.Has("guideString"))
  38. {
  39. return typeof(PartAssemblyConstructionGraphStep);
  40. }
  41. return null;
  42. }
  43. public ConstructionGraphStep Read(ISerializationManager serializationManager,
  44. MappingDataNode node,
  45. IDependencyCollection dependencies,
  46. SerializationHookContext hookCtx,
  47. ISerializationContext? context = null,
  48. ISerializationManager.InstantiationDelegate<ConstructionGraphStep>? instanceProvider = null)
  49. {
  50. var type = GetType(node) ??
  51. throw new ArgumentException(
  52. "Tried to convert invalid YAML node mapping to ConstructionGraphStep!");
  53. return (ConstructionGraphStep)serializationManager.Read(type, node, hookCtx, context)!;
  54. }
  55. public ValidationNode Validate(ISerializationManager serializationManager, MappingDataNode node,
  56. IDependencyCollection dependencies,
  57. ISerializationContext? context = null)
  58. {
  59. var type = GetType(node);
  60. if (type == null)
  61. return new ErrorNode(node, "No construction graph step type found.");
  62. return serializationManager.ValidateNode(type, node, context);
  63. }
  64. }
  65. }