Refactor stacks to use prototypes (#3387)

* Refactor stacks to use prototypes

* Fix not assigned warning

* Add names to stacks

* Make machine baords and material constructions use the name as well

* Remove defaulting stacks to prototype id

* Fix tests

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
DrSmugleaf
2021-02-25 06:18:29 +01:00
committed by GitHub
parent 7c32574547
commit fdcbece63d
33 changed files with 326 additions and 186 deletions

View File

@@ -1,8 +1,10 @@
using System;
using Content.Shared.Stacks;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Players;
using Robust.Shared.Reflection;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -10,6 +12,8 @@ namespace Content.Shared.GameObjects.Components
{
public abstract class SharedStackComponent : Component
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private const string SerializationCache = "stack";
public sealed override string Name => "Stack";
@@ -47,7 +51,9 @@ namespace Content.Shared.GameObjects.Components
[ViewVariables] public int AvailableSpace => MaxCount - Count;
[ViewVariables] public object StackType { get; private set; }
[ViewVariables] public string StackTypeId { get; private set; } = string.Empty;
public StackPrototype StackType => _prototypeManager.Index<StackPrototype>(StackTypeId);
public override void ExposeData(ObjectSerializer serializer)
{
@@ -59,31 +65,29 @@ namespace Content.Shared.GameObjects.Components
return;
}
if (serializer.TryGetCacheData(SerializationCache, out object stackType))
if (serializer.TryGetCacheData(SerializationCache, out string stackType))
{
StackType = stackType;
StackTypeId = stackType;
return;
}
if (serializer.TryReadDataFieldCached("stacktype", out string raw))
{
var refl = IoCManager.Resolve<IReflectionManager>();
if (refl.TryParseEnumReference(raw, out var @enum))
{
stackType = @enum;
}
else
{
stackType = raw;
}
}
else
{
stackType = Owner.Prototype.ID;
}
serializer.DataFieldCached(ref stackType, "stackType", string.Empty);
serializer.SetCacheData(SerializationCache, stackType);
StackType = stackType;
if (!string.IsNullOrEmpty(stackType))
{
serializer.SetCacheData(SerializationCache, stackType);
StackTypeId = stackType;
}
}
protected override void Startup()
{
base.Startup();
if (!_prototypeManager.HasIndex<StackPrototype>(StackTypeId))
{
Logger.Error($"No {nameof(StackPrototype)} found with id {StackTypeId}");
}
}
public override ComponentState GetComponentState(ICommonSession player)
@@ -116,34 +120,4 @@ namespace Content.Shared.GameObjects.Components
}
}
}
public enum StackType
{
Metal,
Glass,
ReinforcedGlass,
Plasteel,
Cable,
Wood,
Plastic,
MVCable,
HVCable,
Gold,
Plasma,
Ointment,
Gauze,
Brutepack,
FloorTileSteel,
FloorTileCarpet,
FloorTileWhite,
FloorTileDark,
FloorTileWood,
MetalRod,
PaperRolling,
CigaretteFilter,
GroundTobacco,
GroundCannabis,
LeavesTobaccoDried,
LeavesCannabisDried
}
}