Skip to content

Commit e361944

Browse files
Make DamageSpecifier serializable (space-wizards#43049)
* pt1 damageable refactor * fixes * thusd unused * sparse damagespecifiers * fix thongs * aaaaaa * fix tests * de-crash
1 parent 9cdc009 commit e361944

File tree

46 files changed

+299
-413
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+299
-413
lines changed

Content.Client/HealthAnalyzer/UI/HealthAnalyzerControl.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void Populate(HealthAnalyzerUiState state)
135135
damageable.DamagePerGroup.OrderByDescending(damage => damage.Value)
136136
.ToDictionary(x => x.Key, x => x.Value);
137137

138-
IReadOnlyDictionary<string, FixedPoint2> damagePerType = damageable.Damage.DamageDict;
138+
var damagePerType = damageable.Damage.DamageDict;
139139

140140
DrawDiagnosticGroups(damageSortedGroups, damagePerType);
141141
}
@@ -153,7 +153,7 @@ private static string GetStatus(MobState mobState)
153153

154154
private void DrawDiagnosticGroups(
155155
Dictionary<string, FixedPoint2> groups,
156-
IReadOnlyDictionary<string, FixedPoint2> damageDict)
156+
IReadOnlyDictionary<ProtoId<DamageTypePrototype>, FixedPoint2> damageDict)
157157
{
158158
GroupsContainer.RemoveAllChildren();
159159

Content.IntegrationTests/Tests/Damageable/DamageSpecifierTest.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Collections.Generic;
22
using Content.Shared.Damage;
3+
using Content.Shared.Damage.Prototypes;
34
using Content.Shared.FixedPoint;
5+
using Robust.Shared.Prototypes;
46

57
namespace Content.IntegrationTests.Tests.Damageable;
68

@@ -39,51 +41,51 @@ public void TestDamageSpecifierOperations()
3941
Assert.That(difference, Is.EqualTo(output5));
4042
}
4143

42-
private static readonly Dictionary<string, FixedPoint2> Input1 = new()
44+
private static readonly Dictionary<ProtoId<DamageTypePrototype>, FixedPoint2> Input1 = new()
4345
{
4446
{ "A", 1.5f },
4547
{ "B", 2 },
4648
{ "C", 3 }
4749
};
4850

49-
private static readonly Dictionary<string, FixedPoint2> Input2 = new()
51+
private static readonly Dictionary<ProtoId<DamageTypePrototype>, FixedPoint2> Input2 = new()
5052
{
5153
{ "A", 1 },
5254
{ "B", 2 },
5355
{ "C", 5 },
5456
{ "D", 0.05f }
5557
};
5658

57-
private static readonly Dictionary<string, FixedPoint2> Output1 = new()
59+
private static readonly Dictionary<ProtoId<DamageTypePrototype>, FixedPoint2> Output1 = new()
5860
{
5961
{ "A", -1.5f },
6062
{ "B", -2 },
6163
{ "C", -3 }
6264
};
6365

64-
private static readonly Dictionary<string, FixedPoint2> Output2 = new()
66+
private static readonly Dictionary<ProtoId<DamageTypePrototype>, FixedPoint2> Output2 = new()
6567
{
6668
{ "A", 0.75f },
6769
{ "B", 1 },
6870
{ "C", 1.5 }
6971
};
7072

71-
private static readonly Dictionary<string, FixedPoint2> Output3 = new()
73+
private static readonly Dictionary<ProtoId<DamageTypePrototype>, FixedPoint2> Output3 = new()
7274
{
7375
{ "A", 3f },
7476
{ "B", 4 },
7577
{ "C", 6 }
7678
};
7779

78-
private static readonly Dictionary<string, FixedPoint2> Output4 = new()
80+
private static readonly Dictionary<ProtoId<DamageTypePrototype>, FixedPoint2> Output4 = new()
7981
{
8082
{ "A", 0.5f },
8183
{ "B", 0 },
8284
{ "C", -2 },
8385
{ "D", -0.05f }
8486
};
8587

86-
private static readonly Dictionary<string, FixedPoint2> Output5 = new()
88+
private static readonly Dictionary<ProtoId<DamageTypePrototype>, FixedPoint2> Output5 = new()
8789
{
8890
{ "A", 0.25f },
8991
{ "B", 0 },

Content.IntegrationTests/Tests/Damageable/DamageableTest.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,6 @@ await server.WaitAssertion(() =>
148148
{
149149
var uid = sDamageableEntity;
150150

151-
// Check that the correct types are supported.
152-
Assert.Multiple(() =>
153-
{
154-
Assert.That(sDamageableComponent.Damage.DamageDict.ContainsKey(type1.ID), Is.False);
155-
Assert.That(sDamageableComponent.Damage.DamageDict.ContainsKey(type2a.ID), Is.True);
156-
Assert.That(sDamageableComponent.Damage.DamageDict.ContainsKey(type2b.ID), Is.False);
157-
Assert.That(sDamageableComponent.Damage.DamageDict.ContainsKey(type3a.ID), Is.True);
158-
Assert.That(sDamageableComponent.Damage.DamageDict.ContainsKey(type3b.ID), Is.True);
159-
Assert.That(sDamageableComponent.Damage.DamageDict.ContainsKey(type3c.ID), Is.True);
160-
});
161-
162151
// Check that damage is evenly distributed over a group if its a nice multiple
163152
var types = group3.DamageTypes;
164153
var damageToDeal = FixedPoint2.New(types.Count * 5);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Text.Json;
2+
using Content.Shared.Damage;
3+
using Robust.Shared.Map;
4+
using Robust.Shared.Map.Components;
5+
6+
namespace Content.Server.Administration.Logs.Converters;
7+
8+
[AdminLogConverter]
9+
public sealed class DamageSpecifierConverter : AdminLogConverter<DamageSpecifier>
10+
{
11+
public override void Write(Utf8JsonWriter writer, DamageSpecifier value, JsonSerializerOptions options)
12+
{
13+
writer.WriteStartObject();
14+
foreach (var (damage, amount) in value.DamageDict)
15+
{
16+
writer.WriteNumber(damage.Id, amount.Double());
17+
}
18+
writer.WriteEndObject();
19+
}
20+
}

Content.Shared/Atmos/Components/DeltaPressureComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public sealed partial class DeltaPressureComponent : Component
6767
[DataField]
6868
public DamageSpecifier BaseDamage = new()
6969
{
70-
DamageDict = new Dictionary<string, FixedPoint2>
70+
DamageDict = new ()
7171
{
7272
{ "Structural", 10 },
7373
},

Content.Shared/Changeling/Components/ChangelingDevourComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public sealed partial class ChangelingDevourComponent : Component
9999
[DataField, AutoNetworkedField]
100100
public DamageSpecifier DamagePerTick = new()
101101
{
102-
DamageDict = new Dictionary<string, FixedPoint2>
102+
DamageDict = new ()
103103
{
104104
{ "Slash", 10},
105105
{ "Piercing", 10 },

Content.Shared/Cluwne/CluwneComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public sealed partial class CluwneComponent : Component
2626
{
2727
DamageDict = new()
2828
{
29-
{ "Genetic", 300.0 },
29+
{ "Cellular", 300.0 },
3030
},
3131
};
3232

Content.Shared/Damage/Components/DamageableComponent.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ namespace Content.Shared.Damage.Components;
1717
/// may also have resistances to certain damage types, defined via a <see cref="DamageModifierSetPrototype"/>.
1818
/// </remarks>
1919
[RegisterComponent]
20-
[NetworkedComponent]
20+
[NetworkedComponent, AutoGenerateComponentState(true)]
2121
[Access(typeof(DamageableSystem), Other = AccessPermissions.ReadExecute)]
2222
public sealed partial class DamageableComponent : Component
2323
{
2424
/// <summary>
2525
/// This <see cref="DamageContainerPrototype"/> specifies what damage types are supported by this component.
2626
/// If null, all damage types will be supported.
2727
/// </summary>
28-
[DataField("damageContainer")]
28+
[DataField("damageContainer"), AutoNetworkedField]
2929
// ReSharper disable once InconsistentNaming - This is wrong but fixing it is potentially annoying for downstreams.
3030
public ProtoId<DamageContainerPrototype>? DamageContainerID;
3131

@@ -37,7 +37,7 @@ public sealed partial class DamageableComponent : Component
3737
/// Though DamageModifierSets can be deserialized directly, we only want to use the prototype version here
3838
/// to reduce duplication.
3939
/// </remarks>
40-
[DataField("damageModifierSet")]
40+
[DataField("damageModifierSet"), AutoNetworkedField]
4141
public ProtoId<DamageModifierSetPrototype>? DamageModifierSetId;
4242

4343
/// <summary>
@@ -46,7 +46,7 @@ public sealed partial class DamageableComponent : Component
4646
/// <remarks>
4747
/// If this data-field is specified, this allows damageable components to be initialized with non-zero damage.
4848
/// </remarks>
49-
[DataField(readOnly: true)] //TODO FULL GAME SAVE
49+
[DataField, AutoNetworkedField]
5050
public DamageSpecifier Damage = new();
5151

5252
/// <summary>
@@ -87,20 +87,6 @@ public sealed partial class DamageableComponent : Component
8787
[DataField]
8888
public ProtoId<HealthIconPrototype> RottingIcon = "HealthIconRotting";
8989

90-
[DataField]
90+
[DataField, AutoNetworkedField]
9191
public FixedPoint2? HealthBarThreshold;
9292
}
93-
94-
[Serializable, NetSerializable]
95-
public sealed class DamageableComponentState(
96-
Dictionary<string, FixedPoint2> damageDict,
97-
ProtoId<DamageContainerPrototype>? damageContainerId,
98-
ProtoId<DamageModifierSetPrototype>? modifierSetId,
99-
FixedPoint2? healthBarThreshold)
100-
: ComponentState
101-
{
102-
public readonly Dictionary<string, FixedPoint2> DamageDict = damageDict;
103-
public readonly ProtoId<DamageContainerPrototype>? DamageContainerId = damageContainerId;
104-
public readonly ProtoId<DamageModifierSetPrototype>? ModifierSetId = modifierSetId;
105-
public readonly FixedPoint2? HealthBarThreshold = healthBarThreshold;
106-
}

Content.Shared/Damage/DamageSpecifier.cs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
using Content.Shared.Damage.Components;
44
using Content.Shared.Damage.Prototypes;
55
using Content.Shared.FixedPoint;
6-
using JetBrains.Annotations;
76
using Robust.Shared.Prototypes;
8-
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
97
using Robust.Shared.Utility;
108
using Robust.Shared.Serialization;
9+
using Robust.Shared.Serialization.TypeSerializers.Implementations.Generic;
1110

1211
namespace Content.Shared.Damage
1312
{
@@ -19,31 +18,13 @@ namespace Content.Shared.Damage
1918
/// functions to apply resistance sets and supports basic math operations to modify this dictionary.
2019
/// </remarks>
2120
[DataDefinition, Serializable, NetSerializable]
22-
public sealed partial class DamageSpecifier : IEquatable<DamageSpecifier>
21+
public sealed partial class DamageSpecifier : IEquatable<DamageSpecifier>, IRobustCloneable<DamageSpecifier>
2322
{
24-
// For the record I regret so many of the decisions i made when rewriting damageable
25-
// Why is it just shitting out dictionaries left and right
26-
// One day Arrays, stackalloc spans, and SIMD will save the day.
27-
// TODO DAMAGEABLE REFACTOR
28-
29-
// These exist solely so the wiki works. Please do not touch them or use them.
30-
[JsonPropertyName("types")]
31-
[DataField("types", customTypeSerializer: typeof(PrototypeIdDictionarySerializer<FixedPoint2, DamageTypePrototype>))]
32-
[UsedImplicitly]
33-
private Dictionary<string, FixedPoint2>? _damageTypeDictionary;
34-
35-
[JsonPropertyName("groups")]
36-
[DataField("groups", customTypeSerializer: typeof(PrototypeIdDictionarySerializer<FixedPoint2, DamageGroupPrototype>))]
37-
[UsedImplicitly]
38-
private Dictionary<string, FixedPoint2>? _damageGroupDictionary;
39-
4023
/// <summary>
4124
/// Main DamageSpecifier dictionary. Most DamageSpecifier functions exist to somehow modifying this.
4225
/// </summary>
43-
[JsonIgnore]
44-
[ViewVariables(VVAccess.ReadWrite)]
45-
[IncludeDataField(customTypeSerializer: typeof(DamageSpecifierDictionarySerializer), readOnly: true)]
46-
public Dictionary<string, FixedPoint2> DamageDict { get; set; } = new();
26+
[DataField("types")]
27+
public Dictionary<ProtoId<DamageTypePrototype>, FixedPoint2> DamageDict { get; set; } = new();
4728

4829
/// <summary>
4930
/// Returns a sum of the damage values.
@@ -84,6 +65,11 @@ public bool AnyPositive()
8465
[JsonIgnore]
8566
public bool Empty => DamageDict.Count == 0;
8667

68+
public DamageSpecifier Clone()
69+
{
70+
return new DamageSpecifier(this);
71+
}
72+
8773
public override string ToString()
8874
{
8975
return "DamageSpecifier(" + string.Join("; ", DamageDict.Select(x => x.Key + ":" + x.Value)) + ")";

Content.Shared/Damage/DamageSpecifierDictionarySerializer.cs

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)