Revert "Chemistry JSON dump tool and companion GitHub Action (#6134)" (#6217)

This reverts commit 40e2e78e0f.
This commit is contained in:
Moony
2022-01-17 16:06:19 -06:00
committed by GitHub
parent 71ef2f4938
commit 5fd45fc82a
27 changed files with 44 additions and 591 deletions

View File

@@ -626,12 +626,5 @@ namespace Content.Shared.CCVar
/// </summary>
public static readonly CVarDef<float> RulesWaitTime =
CVarDef.Create("rules.time", 45f, CVar.SERVER | CVar.REPLICATED);
/*
* Autogeneration
*/
public static readonly CVarDef<string> DestinationFile =
CVarDef.Create("autogen.destination_file", "", CVar.SERVER | CVar.SERVERONLY);
}
}

View File

@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Collections.Generic;
using Content.Shared.Administration.Logs;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Converters;
using Content.Shared.Database;
using Content.Shared.FixedPoint;
using JetBrains.Annotations;
@@ -21,32 +18,26 @@ namespace Content.Shared.Chemistry.Reagent
/// </summary>
[ImplicitDataDefinitionForInheritors]
[MeansImplicitUse]
[JsonConverter(typeof(UniversalJsonConverter<ReagentEffect>))]
public abstract class ReagentEffect
{
[JsonPropertyName("id")] private protected string _id => this.GetType().Name;
/// <summary>
/// The list of conditions required for the effect to activate. Not required.
/// </summary>
[JsonPropertyName("conditions")]
[DataField("conditions")]
public ReagentEffectCondition[]? Conditions;
/// <summary>
/// What's the chance, from 0 to 1, that this effect will occur?
/// </summary>
[JsonPropertyName("probability")]
[DataField("probability")]
public float Probability = 1.0f;
[JsonIgnore]
[DataField("logImpact")]
public virtual LogImpact LogImpact { get; } = LogImpact.Low;
/// <summary>
/// Should this reagent effect log at all?
/// </summary>
[JsonIgnore]
[DataField("shouldLog")]
public virtual bool ShouldLog { get; } = false;

View File

@@ -1,5 +1,4 @@
using System.Text.Json.Serialization;
using Content.Shared.Converters;
using Content.Shared.Chemistry.Components;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
@@ -8,11 +7,8 @@ namespace Content.Shared.Chemistry.Reagent
{
[ImplicitDataDefinitionForInheritors]
[MeansImplicitUse]
[JsonConverter(typeof(UniversalJsonConverter<ReagentEffectCondition>))]
public abstract class ReagentEffectCondition
{
[JsonPropertyName("id")] private protected string _id => this.GetType().Name;
public abstract bool Condition(ReagentEffectArgs args);
}
}

View File

@@ -1,11 +1,9 @@
using System;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Content.Shared.Administration.Logs;
using Content.Shared.Body.Prototypes;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reaction;
using Content.Shared.Converters;
using Content.Shared.Database;
using Content.Shared.FixedPoint;
using Robust.Shared.GameObjects;
@@ -32,9 +30,6 @@ namespace Content.Shared.Chemistry.Reagent
[DataField("name")]
public string Name { get; } = string.Empty;
[DataField("group")]
public string Group { get; } = "Unknown";
[DataField("parent", customTypeSerializer:typeof(PrototypeIdSerializer<ReagentPrototype>))]
public string? Parent { get; private set; }
@@ -144,20 +139,17 @@ namespace Content.Shared.Chemistry.Reagent
}
[DataDefinition]
[JsonConverter(typeof(UniversalJsonConverter<ReagentEffectsEntry>))]
public class ReagentEffectsEntry
{
/// <summary>
/// Amount of reagent to metabolize, per metabolism cycle.
/// </summary>
[JsonPropertyName("rate")]
[DataField("metabolismRate")]
public FixedPoint2 MetabolismRate = FixedPoint2.New(0.5f);
/// <summary>
/// A list of effects to apply when these reagents are metabolized.
/// </summary>
[JsonPropertyName("effects")]
[DataField("effects", required: true)]
public ReagentEffect[] Effects = default!;
}

View File

@@ -1,102 +0,0 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Content.Shared.Converters
{
// 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>
{
// We can convert anything! Probably.
public override bool CanConvert(Type typeToConvert)
{
return true;
}
// 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);
}
}
}

View File

@@ -9,9 +9,7 @@ using Robust.Shared.ViewVariables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using Content.Shared.FixedPoint;
using Content.Shared.Converters;
namespace Content.Shared.Damage
{
@@ -23,21 +21,17 @@ namespace Content.Shared.Damage
/// functions to apply resistance sets and supports basic math operations to modify this dictionary.
/// </remarks>
[DataDefinition]
[JsonConverter(typeof(UniversalJsonConverter<DamageSpecifier>))]
public class DamageSpecifier
{
[JsonPropertyName("types")]
[DataField("types", customTypeSerializer: typeof(PrototypeIdDictionarySerializer<FixedPoint2, DamageTypePrototype>))]
private readonly Dictionary<string,FixedPoint2>? _damageTypeDictionary;
[JsonPropertyName("groups")]
[DataField("groups", customTypeSerializer: typeof(PrototypeIdDictionarySerializer<FixedPoint2, DamageGroupPrototype>))]
private readonly Dictionary<string, FixedPoint2>? _damageGroupDictionary;
/// <summary>
/// Main DamageSpecifier dictionary. Most DamageSpecifier functions exist to somehow modifying this.
/// </summary>
[JsonIgnore]
[ViewVariables(VVAccess.ReadWrite)]
public Dictionary<string, FixedPoint2> DamageDict
{
@@ -49,7 +43,6 @@ namespace Content.Shared.Damage
}
set => _damageDict = value;
}
[JsonIgnore]
private Dictionary<string, FixedPoint2>? _damageDict;
/// <summary>
@@ -60,13 +53,11 @@ namespace Content.Shared.Damage
/// in another. For this purpose, you should instead use <see cref="TrimZeros()"/> and then check the <see
/// cref="Empty"/> property.
/// </remarks>
[JsonIgnore]
public FixedPoint2 Total => DamageDict.Values.Sum();
/// <summary>
/// Whether this damage specifier has any entries.
/// </summary>
[JsonIgnore]
public bool Empty => DamageDict.Count == 0;
#region constructors

View File

@@ -1,8 +1,6 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Robust.Shared.Serialization;
namespace Content.Shared.FixedPoint
@@ -12,7 +10,6 @@ namespace Content.Shared.FixedPoint
/// To enforce this level of precision, floats are shifted by 2 decimal points, rounded, and converted to an int.
/// </summary>
[Serializable]
[JsonConverter(typeof(FixedPointJsonConverter))]
public struct FixedPoint2 : ISelfSerialize, IComparable<FixedPoint2>, IEquatable<FixedPoint2>, IFormattable
{
private int _value;
@@ -294,19 +291,4 @@ namespace Content.Shared.FixedPoint
return acc;
}
}
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();
}
}
}