Allow solutions to store extra reagent data (#19323)
This commit is contained in:
44
Content.Shared/Chemistry/Reagent/ReagentData.cs
Normal file
44
Content.Shared/Chemistry/Reagent/ReagentData.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Chemistry.Reagent;
|
||||
|
||||
[ImplicitDataDefinitionForInheritors, Serializable, NetSerializable]
|
||||
public abstract partial class ReagentData : IEquatable<ReagentData>
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert to a string representation. This if for logging & debugging. This is not localized and should not be
|
||||
/// shown to players.
|
||||
/// </summary>
|
||||
public virtual string ToString(string prototype, FixedPoint2 quantity)
|
||||
{
|
||||
return $"{prototype}:{GetType().Name}:{quantity}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert to a string representation. This if for logging & debugging. This is not localized and should not be
|
||||
/// shown to players.
|
||||
/// </summary>
|
||||
public virtual string ToString(string prototype)
|
||||
{
|
||||
return $"{prototype}:{GetType().Name}";
|
||||
}
|
||||
|
||||
public abstract bool Equals(ReagentData? other);
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj))
|
||||
return false;
|
||||
if (ReferenceEquals(this, obj))
|
||||
return true;
|
||||
if (obj.GetType() != GetType())
|
||||
return false;
|
||||
|
||||
return Equals((ReagentData) obj);
|
||||
}
|
||||
|
||||
public abstract override int GetHashCode();
|
||||
|
||||
public abstract ReagentData Clone();
|
||||
}
|
||||
93
Content.Shared/Chemistry/Reagent/ReagentId.cs
Normal file
93
Content.Shared/Chemistry/Reagent/ReagentId.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Shared.Chemistry.Reagent;
|
||||
|
||||
/// <summary>
|
||||
/// Struct used to uniquely identify a reagent. This is usually just a ReagentPrototype id string, however some reagents
|
||||
/// contain additional data (e.g., blood could store DNA data).
|
||||
/// </summary>
|
||||
[Serializable, NetSerializable]
|
||||
[DataDefinition]
|
||||
public partial struct ReagentId : IEquatable<ReagentId>
|
||||
{
|
||||
// TODO rename data field.
|
||||
[DataField("ReagentId", customTypeSerializer: typeof(PrototypeIdSerializer<ReagentPrototype>), required: true)]
|
||||
public string Prototype { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Any additional data that is unique to this reagent type. E.g., for blood this could be DNA data.
|
||||
/// </summary>
|
||||
[DataField("data")]
|
||||
public ReagentData? Data { get; private set; }
|
||||
|
||||
public ReagentId(string prototype, ReagentData? data)
|
||||
{
|
||||
Prototype = prototype;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public ReagentId()
|
||||
{
|
||||
Prototype = default!;
|
||||
}
|
||||
|
||||
public bool Equals(ReagentId other)
|
||||
{
|
||||
if (Prototype != other.Prototype)
|
||||
return false;
|
||||
|
||||
if (Data == null)
|
||||
return other.Data == null;
|
||||
|
||||
if (other.Data == null)
|
||||
return false;
|
||||
|
||||
if (Data.GetType() != other.Data.GetType())
|
||||
return false;
|
||||
|
||||
return Data.Equals(other.Data);
|
||||
}
|
||||
|
||||
public bool Equals(string prototype, ReagentData? otherData = null)
|
||||
{
|
||||
if (Prototype != prototype)
|
||||
return false;
|
||||
|
||||
if (Data == null)
|
||||
return otherData == null;
|
||||
|
||||
return Data.Equals(otherData);
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is ReagentId other && Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(Prototype, Data);
|
||||
}
|
||||
|
||||
public string ToString(FixedPoint2 quantity)
|
||||
{
|
||||
return Data?.ToString(Prototype, quantity) ?? $"{Prototype}:{quantity}";
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Data?.ToString(Prototype) ?? Prototype;
|
||||
}
|
||||
|
||||
public static bool operator ==(ReagentId left, ReagentId right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(ReagentId left, ReagentId right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
}
|
||||
@@ -141,7 +141,7 @@ namespace Content.Shared.Chemistry.Reagent
|
||||
return removed;
|
||||
}
|
||||
|
||||
public void ReactionPlant(EntityUid? plantHolder, Solution.ReagentQuantity amount, Solution solution)
|
||||
public void ReactionPlant(EntityUid? plantHolder, ReagentQuantity amount, Solution solution)
|
||||
{
|
||||
if (plantHolder == null)
|
||||
return;
|
||||
|
||||
77
Content.Shared/Chemistry/Reagent/ReagentQuantity.cs
Normal file
77
Content.Shared/Chemistry/Reagent/ReagentQuantity.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Chemistry.Reagent;
|
||||
|
||||
/// <summary>
|
||||
/// Simple struct for storing a <see cref="ReagentId"/> & quantity tuple.
|
||||
/// </summary>
|
||||
[Serializable, NetSerializable]
|
||||
[DataDefinition]
|
||||
public partial struct ReagentQuantity : IEquatable<ReagentQuantity>
|
||||
{
|
||||
[DataField("Quantity", required:true)]
|
||||
public FixedPoint2 Quantity { get; private set; }
|
||||
|
||||
[IncludeDataField]
|
||||
[ViewVariables]
|
||||
public ReagentId Reagent { get; private set; }
|
||||
|
||||
public ReagentQuantity(string reagentId, FixedPoint2 quantity, ReagentData? data)
|
||||
: this(new ReagentId(reagentId, data), quantity)
|
||||
{
|
||||
}
|
||||
|
||||
public ReagentQuantity(ReagentId reagent, FixedPoint2 quantity)
|
||||
{
|
||||
Reagent = reagent;
|
||||
Quantity = quantity;
|
||||
}
|
||||
|
||||
public ReagentQuantity() : this(default, default)
|
||||
{
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Reagent.ToString(Quantity);
|
||||
}
|
||||
|
||||
public void Deconstruct(out string prototype, out FixedPoint2 quantity, out ReagentData? data)
|
||||
{
|
||||
prototype = Reagent.Prototype;
|
||||
quantity = Quantity;
|
||||
data = Reagent.Data;
|
||||
}
|
||||
|
||||
public void Deconstruct(out ReagentId id, out FixedPoint2 quantity)
|
||||
{
|
||||
id = Reagent;
|
||||
quantity = Quantity;
|
||||
}
|
||||
|
||||
public bool Equals(ReagentQuantity other)
|
||||
{
|
||||
return Quantity != other.Quantity && Reagent.Equals(other.Reagent);
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is ReagentQuantity other && Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(Reagent.GetHashCode(), Quantity);
|
||||
}
|
||||
|
||||
public static bool operator ==(ReagentQuantity left, ReagentQuantity right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(ReagentQuantity left, ReagentQuantity right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user