FIXED: Chemistry JSON dump tool and companion GitHub Action (#6222)

Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
Co-authored-by: moonheart08 <moonheart08@users.noreply.github.com>
This commit is contained in:
Sam Weaver
2022-01-18 16:44:22 -05:00
committed by GitHub
parent 9d73ff1e26
commit adddd2fac6
27 changed files with 602 additions and 43 deletions

View File

@@ -0,0 +1,70 @@
using System;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Content.Shared.Chemistry.Reaction;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Damage;
using Content.Shared.FixedPoint;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
namespace Content.Server.GuideGenerator;
public class ChemistryJsonGenerator
{
public static void PublishJson(StreamWriter file)
{
var prototype = IoCManager.Resolve<IPrototypeManager>();
var prototypes =
prototype
.EnumeratePrototypes<ReagentPrototype>()
.Where(x => !x.Abstract)
.Select(x => new ReagentEntry(x))
.ToDictionary(x => x.Id, x => x);
var reactions =
prototype
.EnumeratePrototypes<ReactionPrototype>()
.Where(x => x.Products.Count != 0);
foreach (var reaction in reactions)
{
foreach (var product in reaction.Products.Keys)
{
prototypes[product].Recipes.Add(reaction.ID);
}
}
var serializeOptions = new JsonSerializerOptions
{
WriteIndented = true,
Converters =
{
new UniversalJsonConverter<ReagentEffect>(),
new UniversalJsonConverter<ReagentEffectCondition>(),
new UniversalJsonConverter<ReagentEffectsEntry>(),
new UniversalJsonConverter<DamageSpecifier>(),
new FixedPointJsonConverter()
}
};
file.Write(JsonSerializer.Serialize(prototypes, serializeOptions));
}
public class FixedPointJsonConverter : JsonConverter<FixedPoint2>
{
public override void Write(Utf8JsonWriter writer, FixedPoint2 value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Float());
}
public override FixedPoint2 Read(ref Utf8JsonReader reader, Type objectType, JsonSerializerOptions options)
{
// Throwing a NotSupportedException here allows the error
// message to provide path information.
throw new NotSupportedException();
}
}
}

View File

@@ -0,0 +1,36 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using Content.Shared.Chemistry.Reaction;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
namespace Content.Server.GuideGenerator;
public class ReactionJsonGenerator
{
public static void PublishJson(StreamWriter file)
{
var prototype = IoCManager.Resolve<IPrototypeManager>();
var reactions =
prototype
.EnumeratePrototypes<ReactionPrototype>()
.Select(x => new ReactionEntry(x))
.ToDictionary(x => x.Id, x => x);
var serializeOptions = new JsonSerializerOptions
{
WriteIndented = true,
Converters =
{
new UniversalJsonConverter<ReagentEffect>(),
}
};
file.Write(JsonSerializer.Serialize(reactions, serializeOptions));
}
}

View File

@@ -0,0 +1,96 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using Content.Server.Body.Components;
using Content.Shared.Chemistry.Reaction;
using Content.Shared.Chemistry.Reagent;
using Robust.Shared.Maths;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.GuideGenerator;
public class ReagentEntry
{
[JsonPropertyName("id")]
public string Id { get; }
[JsonPropertyName("name")]
public string Name { get; }
[JsonPropertyName("group")]
public string Group { get; }
[JsonPropertyName("desc")]
public string Description { get; }
[JsonPropertyName("physicalDesc")]
public string PhysicalDescription { get; }
[JsonPropertyName("color")]
public string SubstanceColor { get; }
[JsonPropertyName("recipes")]
public List<string> Recipes { get; } = new();
[JsonPropertyName("metabolisms")]
public Dictionary<string, ReagentEffectsEntry>? Metabolisms { get; }
public ReagentEntry(ReagentPrototype proto)
{
Id = proto.ID;
Name = proto.Name;
Group = proto.Group;
Description = proto.Description;
PhysicalDescription = proto.PhysicalDescription;
SubstanceColor = proto.SubstanceColor.ToHex();
Metabolisms = proto.Metabolisms;
}
}
public class ReactionEntry
{
[JsonPropertyName("id")]
public string Id { get; }
[JsonPropertyName("name")]
public string Name { get; }
[JsonPropertyName("reactants")]
public Dictionary<string, ReactantEntry> Reactants { get; }
[JsonPropertyName("products")]
public Dictionary<string, float> Products { get; }
[JsonPropertyName("effects")]
public List<ReagentEffect> Effects { get; }
public ReactionEntry(ReactionPrototype proto)
{
Id = proto.ID;
Name = proto.Name;
Reactants =
proto.Reactants
.Select(x => KeyValuePair.Create(x.Key, new ReactantEntry(x.Value.Amount.Float(), x.Value.Catalyst)))
.ToDictionary(x => x.Key, x => x.Value);
Products =
proto.Products
.Select(x => KeyValuePair.Create(x.Key, x.Value.Float()))
.ToDictionary(x => x.Key, x => x.Value);
Effects = proto.Effects;
}
}
public class ReactantEntry
{
[JsonPropertyName("amount")]
public float Amount { get; }
[JsonPropertyName("catalyst")]
public bool Catalyst { get; }
public ReactantEntry(float amnt, bool cata)
{
Amount = amnt;
Catalyst = cata;
}
}

View File

@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Content.Server.GuideGenerator
{
// This class is used as a shim to help do polymorphic serialization of objects into JSON
// (serializing objects that inherit abstract base classes or interfaces) since
// System.Text.Json (our new JSON solution) doesn't support that while Newtonsoft.Json (our old
// solution) does.
public class UniversalJsonConverter<T> : JsonConverter<T>
{
// This converter can only convert types that are T or descend from T.
public override bool CanConvert(Type typeToConvert)
{
return typeof(T).IsAssignableFrom(typeToConvert);
}
// We don't support deserialization right now. In order to do so, we'd need to bundle a
// field like "$type" with our objects so they'd be reserialized into the correct base class
// but that presents a security hazard.
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// Throwing a NotImplementedException here allows the Utf8JsonReader to provide
// an error message that provides the specific JSON path of the problematic object
// rather than a generic error message. At least in theory. Haven't tested that.
throw new NotImplementedException();
}
// The bread and butter. Deserialize an object of parameter type T.
// This method is automatically called when the JSON writer finds an object of a type
// where we've registered this class as its converter using the [JsonConverter(...)] attribute
public override void Write(Utf8JsonWriter writer, T obj, JsonSerializerOptions options)
{
// If the object is null, don't include it.
if (obj is null)
{
writer.WriteNullValue();
return;
}
// Use reflection to get a list of fields and properties on the object we're serializing.
// Using obj.GetType() here instead of typeof(T) allows us to get the true base class rather
// than the abstract ancestor, even if we're parameterized with that abstract class.
FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
// Since the JSON writer will have already written the field name, we need to write the object itself.
// Since we only use this class to serialize complex objects, we know we'll be writing a JSON object, so open one.
writer.WriteStartObject();
// For each field, try to write it into the object.
foreach (FieldInfo field in fields)
{
// If the field has a [JsonIgnore] attribute, skip it
if (Attribute.GetCustomAttribute(field, typeof(JsonIgnoreAttribute), true) != null) continue;
// exclude fields that are compiler autogenerated like "__BackingField" fields
if (Attribute.GetCustomAttribute(field, typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true) != null) continue;
// If the field has a [JsonPropertyName] attribute, get the property name. Otherwise, use the field name.
JsonPropertyNameAttribute? attr = (JsonPropertyNameAttribute?) Attribute.GetCustomAttribute(field, typeof(JsonPropertyNameAttribute), true);
string name = attr == null ? field.Name : attr.Name;
// Write a new key/value pair into the JSON object itself.
WriteKV(writer, name, field.GetValue(obj), options);
}
// Repeat the same process for each property.
foreach (PropertyInfo prop in properties)
{
// If the field has a [JsonIgnore] attribute, skip it
if (Attribute.GetCustomAttribute(prop, typeof(JsonIgnoreAttribute), true) != null) continue;
// If the property has a [JsonPropertyName] attribute, get the property name. Otherwise, use the property name.
JsonPropertyNameAttribute? attr = (JsonPropertyNameAttribute?) Attribute.GetCustomAttribute(prop, typeof(JsonPropertyNameAttribute), true);
string name = attr == null ? prop.Name : attr.Name;
// Write a new key/value pair into the JSON object itself.
WriteKV(writer, name, prop.GetValue(obj), options);
}
// Close the object, we're done!
writer.WriteEndObject();
}
// This is a little utility method to write a key/value pair inside a JSON object.
// It's used for all the actual writing.
public void WriteKV(Utf8JsonWriter writer, string key, object? obj, JsonSerializerOptions options)
{
// First, write the property name
writer.WritePropertyName(key);
// Then, recurse. This ensures that primitive values will be written directly, while
// more complex values can use any custom converters we've registered (like this one.)
JsonSerializer.Serialize(writer, obj, options);
}
}
}