1
0

ChemistryJsonGenerator.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.IO;
  2. using System.Linq;
  3. using System.Text.Json;
  4. using System.Text.Json.Serialization;
  5. using Content.Shared.Chemistry.Reaction;
  6. using Content.Shared.Chemistry.Reagent;
  7. using Content.Shared.Damage;
  8. using Content.Shared.EntityEffects;
  9. using Content.Shared.FixedPoint;
  10. using Robust.Shared.Prototypes;
  11. namespace Content.Server.GuideGenerator;
  12. public sealed class ChemistryJsonGenerator
  13. {
  14. public static void PublishJson(StreamWriter file)
  15. {
  16. var prototype = IoCManager.Resolve<IPrototypeManager>();
  17. var prototypes =
  18. prototype
  19. .EnumeratePrototypes<ReagentPrototype>()
  20. .Where(x => !x.Abstract)
  21. .Select(x => new ReagentEntry(x))
  22. .ToDictionary(x => x.Id, x => x);
  23. var reactions =
  24. prototype
  25. .EnumeratePrototypes<ReactionPrototype>()
  26. .Where(x => x.Products.Count != 0);
  27. foreach (var reaction in reactions)
  28. {
  29. foreach (var product in reaction.Products.Keys)
  30. {
  31. prototypes[product].Recipes.Add(reaction.ID);
  32. }
  33. }
  34. var serializeOptions = new JsonSerializerOptions
  35. {
  36. WriteIndented = true,
  37. Converters =
  38. {
  39. new UniversalJsonConverter<EntityEffect>(),
  40. new UniversalJsonConverter<EntityEffectCondition>(),
  41. new UniversalJsonConverter<ReagentEffectsEntry>(),
  42. new UniversalJsonConverter<DamageSpecifier>(),
  43. new FixedPointJsonConverter()
  44. }
  45. };
  46. file.Write(JsonSerializer.Serialize(prototypes, serializeOptions));
  47. }
  48. public sealed class FixedPointJsonConverter : JsonConverter<FixedPoint2>
  49. {
  50. public override void Write(Utf8JsonWriter writer, FixedPoint2 value, JsonSerializerOptions options)
  51. {
  52. writer.WriteNumberValue(value.Float());
  53. }
  54. public override FixedPoint2 Read(ref Utf8JsonReader reader, Type objectType, JsonSerializerOptions options)
  55. {
  56. // Throwing a NotSupportedException here allows the error
  57. // message to provide path information.
  58. throw new NotSupportedException();
  59. }
  60. }
  61. }