Hotfix revolver serialization (#8561)

* Hotfix revolver serialization

* a
This commit is contained in:
metalgearsloth
2022-06-03 10:38:21 +10:00
committed by GitHub
parent 4954016d4f
commit e74dfd7826
2 changed files with 15 additions and 6 deletions

View File

@@ -30,8 +30,9 @@ public sealed class RevolverAmmoProviderComponent : AmmoProviderComponent
// Like BallisticAmmoProvider we defer spawning until necessary
// AmmoSlots is the instantiated ammo and Chambers is the unspawned ammo (that may or may not have been shot).
// TODO: Using an array would be better but this throws!
[DataField("ammoSlots")]
public EntityUid?[] AmmoSlots = Array.Empty<EntityUid?>();
public List<EntityUid?> AmmoSlots = new();
[DataField("chambers")]
public bool?[] Chambers = Array.Empty<bool?>();

View File

@@ -55,13 +55,14 @@ public partial class SharedGunSystem
var oldIndex = component.CurrentIndex;
component.CurrentIndex = state.CurrentIndex;
component.AmmoSlots = new EntityUid?[state.AmmoSlots.Length];
component.AmmoSlots.EnsureCapacity(state.AmmoSlots.Count);
component.AmmoSlots.Clear();
component.Chambers = new bool?[state.Chambers.Length];
DebugTools.Assert(component.AmmoSlots.Length == component.Chambers.Length);
DebugTools.Assert(component.AmmoSlots.Count == component.Chambers.Length);
// Need to copy across the state rather than the ref.
for (var i = 0; i < component.AmmoSlots.Length; i++)
for (var i = 0; i < component.AmmoSlots.Count; i++)
{
component.AmmoSlots[i] = state.AmmoSlots[i];
component.Chambers[i] = state.Chambers[i];
@@ -298,7 +299,14 @@ public partial class SharedGunSystem
private void OnRevolverInit(EntityUid uid, RevolverAmmoProviderComponent component, ComponentInit args)
{
component.AmmoContainer = Containers.EnsureContainer<Container>(uid, RevolverContainer);
component.AmmoSlots = new EntityUid?[component.Capacity];
component.AmmoSlots.EnsureCapacity(component.Capacity);
var remainder = component.Capacity - component.AmmoSlots.Count;
for (var i = 0; i < remainder; i++)
{
component.AmmoSlots.Add(null);
}
component.Chambers = new bool?[component.Capacity];
if (component.FillPrototype != null)
@@ -320,7 +328,7 @@ public partial class SharedGunSystem
protected sealed class RevolverAmmoProviderComponentState : ComponentState
{
public int CurrentIndex;
public EntityUid?[] AmmoSlots = default!;
public List<EntityUid?> AmmoSlots = default!;
public bool?[] Chambers = default!;
}