1
0

DamageSpecifierDictionarySerializer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Content.Shared.Damage.Prototypes;
  2. using Content.Shared.FixedPoint;
  3. using Robust.Shared.Prototypes;
  4. using Robust.Shared.Serialization;
  5. using Robust.Shared.Serialization.Manager;
  6. using Robust.Shared.Serialization.Markdown.Mapping;
  7. using Robust.Shared.Serialization.Markdown.Validation;
  8. using Robust.Shared.Serialization.Markdown.Value;
  9. using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
  10. using Robust.Shared.Serialization.TypeSerializers.Interfaces;
  11. namespace Content.Shared.Damage;
  12. //todo writing
  13. public sealed class DamageSpecifierDictionarySerializer : ITypeReader<Dictionary<string, FixedPoint2>, MappingDataNode>
  14. {
  15. private ITypeValidator<Dictionary<string, FixedPoint2>, MappingDataNode> _damageTypeSerializer = new PrototypeIdDictionarySerializer<FixedPoint2, DamageTypePrototype>();
  16. private ITypeValidator<Dictionary<string, FixedPoint2>, MappingDataNode> _damageGroupSerializer = new PrototypeIdDictionarySerializer<FixedPoint2, DamageGroupPrototype>();
  17. public ValidationNode Validate(ISerializationManager serializationManager, MappingDataNode node,
  18. IDependencyCollection dependencies, ISerializationContext? context = null)
  19. {
  20. var vals = new Dictionary<ValidationNode, ValidationNode>();
  21. if (node.TryGet<MappingDataNode>("types", out var typesNode))
  22. {
  23. vals.Add(new ValidatedValueNode(new ValueDataNode("types")), _damageTypeSerializer.Validate(serializationManager, typesNode, dependencies, context));
  24. }
  25. if (node.TryGet<MappingDataNode>("groups", out var groupsNode))
  26. {
  27. vals.Add(new ValidatedValueNode(new ValueDataNode("groups")), _damageGroupSerializer.Validate(serializationManager, groupsNode, dependencies, context));
  28. }
  29. return new ValidatedMappingNode(vals);
  30. }
  31. public Dictionary<string, FixedPoint2> Read(ISerializationManager serializationManager, MappingDataNode node, IDependencyCollection dependencies,
  32. SerializationHookContext hookCtx, ISerializationContext? context = null, ISerializationManager.InstantiationDelegate<Dictionary<string, FixedPoint2>>? instanceProvider = null)
  33. {
  34. var dict = instanceProvider != null ? instanceProvider() : new();
  35. // Add all the damage types by just copying the type dictionary (if it is not null).
  36. if (node.TryGet<MappingDataNode>("types", out var typesNode))
  37. {
  38. serializationManager.Read(typesNode, instanceProvider: () => dict, notNullableOverride: true);
  39. }
  40. if (!node.TryGet<MappingDataNode>("groups", out var groupsNode))
  41. return dict;
  42. // Then resolve damage groups and add them
  43. var prototypeManager = dependencies.Resolve<IPrototypeManager>();
  44. foreach (var entry in serializationManager.Read<Dictionary<string, FixedPoint2>>(groupsNode, notNullableOverride: true))
  45. {
  46. if (!prototypeManager.TryIndex<DamageGroupPrototype>(entry.Key, out var group))
  47. {
  48. // This can happen if deserialized before prototypes are loaded.
  49. // i made this a warning bc it was failing tests -paul
  50. dependencies.Resolve<ILogManager>().RootSawmill.Error($"Unknown damage group given to DamageSpecifier: {entry.Key}");
  51. continue;
  52. }
  53. // Simply distribute evenly (except for rounding).
  54. // We do this by reducing remaining the # of types and damage every loop.
  55. var remainingTypes = group.DamageTypes.Count;
  56. var remainingDamage = entry.Value;
  57. foreach (var damageType in group.DamageTypes)
  58. {
  59. var damage = remainingDamage / FixedPoint2.New(remainingTypes);
  60. if (!dict.TryAdd(damageType, damage))
  61. {
  62. // Key already exists, add values
  63. dict[damageType] += damage;
  64. }
  65. remainingDamage -= damage;
  66. remainingTypes -= 1;
  67. }
  68. }
  69. return dict;
  70. }
  71. }