Fix storagecomp serialization (#23780)
* Fix storagecomp serialization * Fix state
This commit is contained in:
@@ -19,29 +19,31 @@ using Content.Shared.Timing;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Shared.Audio.Systems;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Storage.EntitySystems;
|
||||
|
||||
public abstract class SharedStorageSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototype = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototype = default!;
|
||||
[Dependency] protected readonly IRobustRandom Random = default!;
|
||||
[Dependency] protected readonly ActionBlockerSystem ActionBlocker = default!;
|
||||
[Dependency] private readonly EntityLookupSystem _entityLookupSystem = default!;
|
||||
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
||||
[Dependency] protected readonly SharedAudioSystem Audio = default!;
|
||||
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
|
||||
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
|
||||
[Dependency] private readonly EntityLookupSystem _entityLookupSystem = default!;
|
||||
[Dependency] protected readonly SharedEntityStorageSystem EntityStorage = default!;
|
||||
[Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
|
||||
[Dependency] protected readonly SharedItemSystem ItemSystem = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
|
||||
[Dependency] private readonly SharedHandsSystem _sharedHandsSystem = default!;
|
||||
[Dependency] protected readonly ActionBlockerSystem ActionBlocker = default!;
|
||||
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
||||
[Dependency] protected readonly SharedAudioSystem Audio = default!;
|
||||
[Dependency] protected readonly SharedTransformSystem TransformSystem = default!;
|
||||
[Dependency] private readonly SharedStackSystem _stack = default!;
|
||||
[Dependency] protected readonly SharedTransformSystem TransformSystem = default!;
|
||||
[Dependency] private readonly SharedUserInterfaceSystem _ui = default!;
|
||||
[Dependency] protected readonly UseDelaySystem UseDelay = default!;
|
||||
|
||||
@@ -69,6 +71,8 @@ public abstract class SharedStorageSystem : EntitySystem
|
||||
_xformQuery = GetEntityQuery<TransformComponent>();
|
||||
_prototype.PrototypesReloaded += OnPrototypesReloaded;
|
||||
|
||||
SubscribeLocalEvent<StorageComponent, ComponentGetState>(OnStorageGetState);
|
||||
SubscribeLocalEvent<StorageComponent, ComponentHandleState>(OnStorageHandleState);
|
||||
SubscribeLocalEvent<StorageComponent, ComponentInit>(OnComponentInit, before: new[] { typeof(SharedContainerSystem) });
|
||||
SubscribeLocalEvent<StorageComponent, GetVerbsEvent<UtilityVerb>>(AddTransferVerbs);
|
||||
SubscribeLocalEvent<StorageComponent, InteractUsingEvent>(OnInteractUsing, after: new[] { typeof(ItemSlotsSystem) });
|
||||
@@ -92,6 +96,43 @@ public abstract class SharedStorageSystem : EntitySystem
|
||||
UpdatePrototypeCache();
|
||||
}
|
||||
|
||||
private void OnStorageGetState(EntityUid uid, StorageComponent component, ref ComponentGetState args)
|
||||
{
|
||||
var storedItems = new Dictionary<NetEntity, ItemStorageLocation>();
|
||||
|
||||
foreach (var (ent, location) in component.StoredItems)
|
||||
{
|
||||
storedItems[GetNetEntity(ent)] = location;
|
||||
}
|
||||
|
||||
args.State = new StorageComponentState()
|
||||
{
|
||||
Grid = component.Grid,
|
||||
IsUiOpen = component.IsUiOpen,
|
||||
MaxItemSize = component.MaxItemSize,
|
||||
StoredItems = storedItems
|
||||
};
|
||||
}
|
||||
|
||||
private void OnStorageHandleState(EntityUid uid, StorageComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not StorageComponentState state)
|
||||
return;
|
||||
|
||||
component.Grid.Clear();
|
||||
component.Grid.AddRange(state.Grid);
|
||||
component.IsUiOpen = state.IsUiOpen;
|
||||
component.MaxItemSize = state.MaxItemSize;
|
||||
|
||||
component.StoredItems.Clear();
|
||||
|
||||
foreach (var (nent, location) in state.StoredItems)
|
||||
{
|
||||
var ent = EnsureEntity<StorageComponent>(nent, uid);
|
||||
component.StoredItems[ent] = location;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
_prototype.PrototypesReloaded -= OnPrototypesReloaded;
|
||||
@@ -497,7 +538,7 @@ public abstract class SharedStorageSystem : EntitySystem
|
||||
if (args.Container.ID != StorageComponent.ContainerId)
|
||||
return;
|
||||
|
||||
if (!entity.Comp.StoredItems.ContainsKey(GetNetEntity(args.Entity)))
|
||||
if (!entity.Comp.StoredItems.ContainsKey(args.Entity))
|
||||
{
|
||||
if (!TryGetAvailableGridSpace((entity.Owner, entity.Comp), (args.Entity, null), out var location))
|
||||
{
|
||||
@@ -505,7 +546,7 @@ public abstract class SharedStorageSystem : EntitySystem
|
||||
return;
|
||||
}
|
||||
|
||||
entity.Comp.StoredItems[GetNetEntity(args.Entity)] = location.Value;
|
||||
entity.Comp.StoredItems[args.Entity] = location.Value;
|
||||
Dirty(entity, entity.Comp);
|
||||
}
|
||||
|
||||
@@ -522,7 +563,7 @@ public abstract class SharedStorageSystem : EntitySystem
|
||||
if (args.Container.ID != StorageComponent.ContainerId)
|
||||
return;
|
||||
|
||||
entity.Comp.StoredItems.Remove(GetNetEntity(args.Entity));
|
||||
entity.Comp.StoredItems.Remove(args.Entity);
|
||||
Dirty(entity, entity.Comp);
|
||||
|
||||
UpdateAppearance((entity, entity.Comp, null));
|
||||
@@ -655,7 +696,7 @@ public abstract class SharedStorageSystem : EntitySystem
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ignoreLocation && !storageComp.StoredItems.ContainsKey(GetNetEntity(insertEnt)))
|
||||
if (!ignoreLocation && !storageComp.StoredItems.ContainsKey(insertEnt))
|
||||
{
|
||||
if (!TryGetAvailableGridSpace((uid, storageComp), (insertEnt, item), out _))
|
||||
{
|
||||
@@ -698,7 +739,7 @@ public abstract class SharedStorageSystem : EntitySystem
|
||||
if (!ItemFitsInGridLocation(insertEnt, uid, location))
|
||||
return false;
|
||||
|
||||
uid.Comp.StoredItems[GetNetEntity(insertEnt)] = location;
|
||||
uid.Comp.StoredItems[insertEnt] = location;
|
||||
Dirty(uid, uid.Comp);
|
||||
|
||||
if (Insert(uid,
|
||||
@@ -713,7 +754,7 @@ public abstract class SharedStorageSystem : EntitySystem
|
||||
return true;
|
||||
}
|
||||
|
||||
uid.Comp.StoredItems.Remove(GetNetEntity(insertEnt));
|
||||
uid.Comp.StoredItems.Remove(insertEnt);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -869,7 +910,7 @@ public abstract class SharedStorageSystem : EntitySystem
|
||||
if (!ItemFitsInGridLocation(itemEnt, storageEnt, location.Position, location.Rotation))
|
||||
return false;
|
||||
|
||||
storageEnt.Comp.StoredItems[GetNetEntity(itemEnt)] = location;
|
||||
storageEnt.Comp.StoredItems[itemEnt] = location;
|
||||
Dirty(storageEnt, storageEnt.Comp);
|
||||
return true;
|
||||
}
|
||||
@@ -997,10 +1038,8 @@ public abstract class SharedStorageSystem : EntitySystem
|
||||
if (!validGrid)
|
||||
return false;
|
||||
|
||||
foreach (var (netEnt, storedItem) in storageEnt.Comp.StoredItems)
|
||||
foreach (var (ent, storedItem) in storageEnt.Comp.StoredItems)
|
||||
{
|
||||
var ent = GetEntity(netEnt);
|
||||
|
||||
if (ent == itemEnt.Owner)
|
||||
continue;
|
||||
|
||||
@@ -1102,4 +1141,16 @@ public abstract class SharedStorageSystem : EntitySystem
|
||||
/// </summary>
|
||||
public abstract void PlayPickupAnimation(EntityUid uid, EntityCoordinates initialCoordinates,
|
||||
EntityCoordinates finalCoordinates, Angle initialRotation, EntityUid? user = null);
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
protected sealed class StorageComponentState : ComponentState
|
||||
{
|
||||
public bool IsUiOpen;
|
||||
|
||||
public Dictionary<NetEntity, ItemStorageLocation> StoredItems = new();
|
||||
|
||||
public List<Box2i> Grid = new();
|
||||
|
||||
public ProtoId<ItemSizePrototype>? MaxItemSize;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user