Add SaveLoadSavePrototype test (#18859)

This commit is contained in:
Leon Friedrich
2023-08-08 19:27:16 +12:00
committed by GitHub
parent b345625de9
commit 6982f238e8
16 changed files with 272 additions and 117 deletions

View File

@@ -3,77 +3,91 @@ using Robust.Shared.Serialization;
namespace Content.Shared.Atmos.Monitor;
// mostly based around floats and percentages, no literals
// except for the range boundaries
[Prototype("alarmThreshold")]
[Serializable, NetSerializable]
public sealed class AtmosAlarmThreshold : IPrototype, ISerializationHooks
public sealed class AtmosAlarmThresholdPrototype : IPrototype
{
[IdDataField]
public string ID { get; } = default!;
[DataField("ignore")]
public readonly bool Ignore;
[DataField("upperBound")]
public readonly AlarmThresholdSetting UpperBound = AlarmThresholdSetting.Disabled;
[DataField("lowerBound")]
public readonly AlarmThresholdSetting LowerBound = AlarmThresholdSetting.Disabled;
[DataField("upperWarnAround")]
public readonly AlarmThresholdSetting UpperWarningPercentage = AlarmThresholdSetting.Disabled;
[DataField("lowerWarnAround")]
public readonly AlarmThresholdSetting LowerWarningPercentage = AlarmThresholdSetting.Disabled;
}
[Serializable, NetSerializable, DataDefinition]
public sealed class AtmosAlarmThreshold
{
[DataField("ignore")]
public bool Ignore;
[DataField("upperBound")]
private AlarmThresholdSetting _UpperBound;
private AlarmThresholdSetting _upperBound = AlarmThresholdSetting.Disabled;
public AlarmThresholdSetting UpperBound { get { return _UpperBound; } private set
[DataField("lowerBound")]
private AlarmThresholdSetting _lowerBound = AlarmThresholdSetting.Disabled;
[DataField("upperWarnAround")]
public AlarmThresholdSetting UpperWarningPercentage = AlarmThresholdSetting.Disabled;
[DataField("lowerWarnAround")]
public AlarmThresholdSetting LowerWarningPercentage = AlarmThresholdSetting.Disabled;
public AlarmThresholdSetting UpperBound
{
get => _upperBound;
set
{
// Because the warnings are stored as percentages of the bounds,
// Make a copy of the calculated bounds, so that the real warning amount
// doesn't change value when user changes the bounds
var oldWarning = UpperWarningBound;
_UpperBound = value;
_upperBound = value;
UpperWarningBound = oldWarning;
}
}
[DataField("lowerBound")]
public AlarmThresholdSetting _LowerBound;
public AlarmThresholdSetting LowerBound { get { return _LowerBound; } private set
public AlarmThresholdSetting LowerBound
{
get => _lowerBound;
set
{
// Because the warnings are stored as percentages of the bounds,
// Make a copy of the calculated bounds, so that the real warning amount
// doesn't change value when user changes the bounds
var oldWarning = LowerWarningBound;
_LowerBound = value;
_lowerBound = value;
LowerWarningBound = oldWarning;
}
}
// upper warning percentage
// must always cause UpperWarningBound
// to be smaller
[DataField("upperWarnAround")]
public AlarmThresholdSetting UpperWarningPercentage { get; private set; }
// lower warning percentage
// must always cause LowerWarningBound
// to be larger
[DataField("lowerWarnAround")]
public AlarmThresholdSetting LowerWarningPercentage { get; private set; }
[ViewVariables]
public AlarmThresholdSetting UpperWarningBound
{
get { return CalculateWarningBound(AtmosMonitorThresholdBound.Upper); }
set { UpperWarningPercentage = CalculateWarningPercentage(AtmosMonitorThresholdBound.Upper, value); }
get => CalculateWarningBound(AtmosMonitorThresholdBound.Upper);
set => UpperWarningPercentage = CalculateWarningPercentage(AtmosMonitorThresholdBound.Upper, value);
}
[ViewVariables]
public AlarmThresholdSetting LowerWarningBound
{
get { return CalculateWarningBound(AtmosMonitorThresholdBound.Lower); }
set { LowerWarningPercentage = CalculateWarningPercentage(AtmosMonitorThresholdBound.Lower, value); }
get => CalculateWarningBound(AtmosMonitorThresholdBound.Lower);
set => LowerWarningPercentage = CalculateWarningPercentage(AtmosMonitorThresholdBound.Lower, value);
}
public AtmosAlarmThreshold()
{
UpperBound = new AlarmThresholdSetting();
LowerBound = new AlarmThresholdSetting();
UpperWarningPercentage = new AlarmThresholdSetting();
LowerWarningPercentage = new AlarmThresholdSetting();
}
public AtmosAlarmThreshold(AtmosAlarmThreshold other)
@@ -85,12 +99,13 @@ public sealed class AtmosAlarmThreshold : IPrototype, ISerializationHooks
LowerWarningPercentage = other.LowerWarningPercentage;
}
void ISerializationHooks.AfterDeserialization()
public AtmosAlarmThreshold(AtmosAlarmThresholdPrototype proto)
{
UpperBound = new AlarmThresholdSetting{ Enabled = UpperBound.Value != 0, Value = UpperBound.Value };
LowerBound = new AlarmThresholdSetting{ Enabled = LowerBound.Value != 0, Value = LowerBound.Value };
UpperWarningPercentage = new AlarmThresholdSetting{ Enabled = UpperWarningPercentage.Value != 0, Value = UpperWarningPercentage.Value };
LowerWarningPercentage = new AlarmThresholdSetting{ Enabled = LowerWarningPercentage.Value != 0, Value = LowerWarningPercentage.Value };
Ignore = proto.Ignore;
UpperBound = proto.UpperBound;
LowerBound = proto.LowerBound;
UpperWarningPercentage = proto.UpperWarningPercentage;
LowerWarningPercentage = proto.LowerWarningPercentage;
}
// utility function to check a threshold against some calculated value
@@ -238,38 +253,41 @@ public sealed class AtmosAlarmThreshold : IPrototype, ISerializationHooks
break;
}
}
}
[DataDefinition, Serializable]
public struct AlarmThresholdSetting
[DataDefinition, Serializable]
public readonly struct AlarmThresholdSetting
{
[DataField("enabled")]
public bool Enabled { get; init; } = true;
[DataField("threshold")]
public float Value { get; init; } = 1;
public static AlarmThresholdSetting Disabled = new() {Enabled = false, Value = 0};
public AlarmThresholdSetting()
{
[DataField("enabled")]
public bool Enabled { get; set; } = false;
[DataField("threshold")]
public float Value { get; set; } = 0;
}
public AlarmThresholdSetting()
{
}
public static bool operator <=(float a, AlarmThresholdSetting b)
{
return b.Enabled && a <= b.Value;
}
public static bool operator <=(float a, AlarmThresholdSetting b)
{
return b.Enabled && a <= b.Value;
}
public static bool operator >=(float a, AlarmThresholdSetting b)
{
return b.Enabled && a >= b.Value;
}
public static bool operator >=(float a, AlarmThresholdSetting b)
{
return b.Enabled && a >= b.Value;
}
public AlarmThresholdSetting WithThreshold(float threshold)
{
return this with {Value = threshold};
}
public AlarmThresholdSetting WithThreshold(float threshold)
{
return new AlarmThresholdSetting{ Enabled = Enabled, Value = threshold };
}
public AlarmThresholdSetting WithEnabled(bool enabled)
{
return new AlarmThresholdSetting{ Enabled = enabled, Value = Value };
}
public AlarmThresholdSetting WithEnabled(bool enabled)
{
return this with {Enabled = enabled};
}
}