2018-07-27 17:11:58 +02:00
|
|
|
using System.Collections.Generic;
|
2019-04-26 15:51:05 +02:00
|
|
|
using Content.Shared.Materials;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.Interfaces.Reflection;
|
|
|
|
|
using Robust.Shared.Interfaces.Serialization;
|
|
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Prototypes;
|
|
|
|
|
using Robust.Shared.Serialization;
|
|
|
|
|
using Robust.Shared.Utility;
|
2018-07-27 17:11:58 +02:00
|
|
|
|
2019-04-26 15:51:05 +02:00
|
|
|
namespace Content.Shared.GameObjects.Components.Materials
|
2018-07-27 17:11:58 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Component to store data such as "this object is made out of steel".
|
|
|
|
|
/// This is not a storage system for say smelteries.
|
|
|
|
|
/// </summary>
|
2019-07-31 15:02:36 +02:00
|
|
|
[RegisterComponent]
|
2018-07-27 17:11:58 +02:00
|
|
|
public class MaterialComponent : Component
|
|
|
|
|
{
|
|
|
|
|
public const string SerializationCache = "mat";
|
|
|
|
|
public override string Name => "Material";
|
|
|
|
|
|
2019-04-26 15:51:05 +02:00
|
|
|
public Dictionary<object, Material> MaterialTypes => _materialTypes;
|
|
|
|
|
private Dictionary<object, Material> _materialTypes;
|
2018-07-27 17:11:58 +02:00
|
|
|
|
|
|
|
|
public override void ExposeData(ObjectSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
base.ExposeData(serializer);
|
|
|
|
|
|
|
|
|
|
// TODO: Writing.
|
2020-07-23 01:46:09 +02:00
|
|
|
if (serializer.Writing)
|
2018-07-27 17:11:58 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (serializer.TryGetCacheData(SerializationCache, out Dictionary<object, Material> cached))
|
|
|
|
|
{
|
2019-04-26 15:51:05 +02:00
|
|
|
_materialTypes = cached.ShallowClone();
|
2018-07-27 17:11:58 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 15:51:05 +02:00
|
|
|
_materialTypes = new Dictionary<object, Material>();
|
2018-07-27 17:11:58 +02:00
|
|
|
|
|
|
|
|
if (serializer.TryReadDataField("materials", out List<MaterialDataEntry> list))
|
|
|
|
|
{
|
|
|
|
|
var protoMan = IoCManager.Resolve<IPrototypeManager>();
|
|
|
|
|
foreach (var entry in list)
|
|
|
|
|
{
|
|
|
|
|
var proto = protoMan.Index<MaterialPrototype>(entry.Value);
|
2019-04-26 15:51:05 +02:00
|
|
|
_materialTypes[entry.Key] = proto.Material;
|
2018-07-27 17:11:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 15:51:05 +02:00
|
|
|
serializer.SetCacheData(SerializationCache, _materialTypes.ShallowClone());
|
2018-07-27 17:11:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MaterialDataEntry : IExposeData
|
|
|
|
|
{
|
|
|
|
|
public object Key;
|
|
|
|
|
public string Value;
|
|
|
|
|
|
|
|
|
|
public void ExposeData(ObjectSerializer serializer)
|
|
|
|
|
{
|
2020-07-23 01:46:09 +02:00
|
|
|
if (serializer.Writing)
|
2018-07-27 17:11:58 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var refl = IoCManager.Resolve<IReflectionManager>();
|
2018-08-02 08:29:55 +02:00
|
|
|
Value = serializer.ReadDataField<string>("mat");
|
|
|
|
|
var key = serializer.ReadDataField<string>("key");
|
2018-07-27 17:11:58 +02:00
|
|
|
if (refl.TryParseEnumReference(key, out var @enum))
|
|
|
|
|
{
|
|
|
|
|
Key = @enum;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Key = key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-02 08:29:55 +02:00
|
|
|
|
|
|
|
|
public enum MaterialKeys
|
|
|
|
|
{
|
|
|
|
|
Stack,
|
|
|
|
|
}
|
2018-07-27 17:11:58 +02:00
|
|
|
}
|