mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
icon fixes
This commit is contained in:
@@ -1,78 +1,43 @@
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Robust.Client.Utility;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Robust.Client.GameObjects
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class IconComponent : Component
|
||||
public class IconComponent : Component, ISerializationHooks
|
||||
{
|
||||
public override string Name => "Icon";
|
||||
public IDirectionalTextureProvider? Icon { get; private set; }
|
||||
|
||||
[DataField("sprite")]
|
||||
private ResourcePath? rsi;
|
||||
[DataField("state")]
|
||||
private string? stateID;
|
||||
|
||||
void ISerializationHooks.AfterDeserialization()
|
||||
{
|
||||
if (rsi != null && stateID != null)
|
||||
{
|
||||
Icon = new SpriteSpecifier.Rsi(rsi, stateID).Frame0();
|
||||
}
|
||||
}
|
||||
|
||||
[Dependency] private readonly IResourceCache _resourceCache = default!;
|
||||
|
||||
public const string LogCategory = "go.comp.icon";
|
||||
const string SerializationCache = "icon";
|
||||
|
||||
//TODO Paul: fix dis, also dont forget about exposedata to set the icon
|
||||
//TODO actually, do we even need this now?
|
||||
private static IRsiStateLike TextureForConfig(IComponent compData, IResourceCache resourceCache)
|
||||
private static IRsiStateLike TextureForConfig(IconComponent compData, IResourceCache resourceCache)
|
||||
{
|
||||
/*IDirectionalTextureProvider dirTex;
|
||||
|
||||
//var tex = compData.ReadDataField<string?>("texture", null);
|
||||
if (serializer.TryGetCacheData<IRsiStateLike>(SerializationCache, out var dirTex))
|
||||
{
|
||||
dirTex = resourceCache.GetResource<TextureResource>(SpriteComponent.TextureRoot / (string)texObj).Texture;
|
||||
//todo compData.SetCacheData(SerializationCache, dirTex);
|
||||
return dirTex;
|
||||
}
|
||||
|
||||
RSI rsi;
|
||||
|
||||
if (compData.TryGetValue("sprite", out var rsiPathObj) && string.IsNullOrWhiteSpace((string?)rsiPathObj))
|
||||
{
|
||||
dirTex = resourceCache.GetFallback<TextureResource>().Texture;
|
||||
//todo compData.SetCacheData(SerializationCache, dirTex);
|
||||
return dirTex;
|
||||
}
|
||||
|
||||
var path = SpriteComponent.TextureRoot / (string)rsiPathObj!;
|
||||
|
||||
try
|
||||
{
|
||||
rsi = resourceCache.GetResource<RSIResource>(path).RSI;
|
||||
}
|
||||
catch
|
||||
{
|
||||
dirTex = resourceCache.GetFallback<TextureResource>().Texture;
|
||||
//todo compData.SetCacheData(SerializationCache, dirTex);
|
||||
return dirTex;
|
||||
}
|
||||
|
||||
if (compData.TryGetValue("state", out var stateObj) && string.IsNullOrWhiteSpace((string?)stateObj))
|
||||
{
|
||||
Logger.ErrorS(LogCategory, "No state specified.");
|
||||
dirTex = resourceCache.GetFallback<TextureResource>().Texture;
|
||||
//todo compData.SetCacheData(SerializationCache, dirTex);
|
||||
return dirTex;
|
||||
}
|
||||
|
||||
if (rsi.TryGetState((string)stateObj!, out var state))
|
||||
{
|
||||
//todo compData.SetCacheData(SerializationCache, state);
|
||||
return state;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.ErrorS(LogCategory, "State '{0}' does not exist on RSI.", (string)stateObj!);
|
||||
return resourceCache.GetFallback<TextureResource>().Texture;
|
||||
}*/
|
||||
//TODO Paul: LATER
|
||||
return resourceCache.GetFallback<TextureResource>().Texture;
|
||||
return compData.Icon?.Default ?? resourceCache.GetFallback<TextureResource>().Texture;
|
||||
}
|
||||
|
||||
public static IRsiStateLike? GetPrototypeIcon(EntityPrototype prototype, IResourceCache resourceCache)
|
||||
@@ -82,7 +47,7 @@ namespace Robust.Client.GameObjects
|
||||
return null;
|
||||
}
|
||||
|
||||
return TextureForConfig(compData, resourceCache);
|
||||
return TextureForConfig((IconComponent)compData, resourceCache);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Serialization.Manager;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.Manager.Result;
|
||||
@@ -12,87 +14,138 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
{
|
||||
[TypeSerializer]
|
||||
public class SpriteSpecifierSerializer :
|
||||
ITypeReaderWriter<SpriteSpecifier, ValueDataNode>,
|
||||
ITypeReaderWriter<SpriteSpecifier, MappingDataNode>,
|
||||
ITypeCopier<Rsi>,
|
||||
ITypeCopier<Texture>,
|
||||
ITypeCopier<EntityPrototype>
|
||||
ITypeReaderWriter<Texture, ValueDataNode>,
|
||||
ITypeReaderWriter<EntityPrototype, ValueDataNode>,
|
||||
ITypeReaderWriter<Rsi, MappingDataNode>,
|
||||
ITypeReader<SpriteSpecifier, MappingDataNode>,
|
||||
ITypeReader<SpriteSpecifier, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager,
|
||||
ValueDataNode node, ISerializationContext? context = null)
|
||||
DeserializationResult ITypeReader<Texture, ValueDataNode>.Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
var path = serializationManager.ReadValueOrThrow<ResourcePath>(node, context);
|
||||
var texture = new Texture(path);
|
||||
|
||||
return new DeserializedValue<SpriteSpecifier>(texture);
|
||||
return DeserializationResult.Value(new Texture(path));
|
||||
}
|
||||
|
||||
public ValidatedNode Validate(ISerializationManager serializationManager, ValueDataNode node,
|
||||
ISerializationContext? context = null)
|
||||
DeserializationResult ITypeReader<SpriteSpecifier, ValueDataNode>.Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
return serializationManager.ReadValue<ResourcePath>(node) != null ? new ValidatedValueNode(node) : new ErrorNode(node);
|
||||
}
|
||||
|
||||
public DeserializationResult Read(ISerializationManager serializationManager,
|
||||
MappingDataNode node, ISerializationContext? context = null)
|
||||
{
|
||||
if (node.TryGetNode("sprite", out var spriteNode)
|
||||
&& node.TryGetNode("state", out var rawStateNode)
|
||||
&& rawStateNode is ValueDataNode stateNode)
|
||||
try
|
||||
{
|
||||
var path = serializationManager.ReadValueOrThrow<ResourcePath>(spriteNode, context);
|
||||
var rsi = new Rsi(path, stateNode.Value);
|
||||
return ((ITypeReader<Texture, ValueDataNode>) this).Read(serializationManager, node, context);
|
||||
}
|
||||
catch { /* ignored */ }
|
||||
|
||||
return new DeserializedValue<SpriteSpecifier>(rsi);
|
||||
try
|
||||
{
|
||||
return ((ITypeReader<EntityPrototype, ValueDataNode>) this).Read(serializationManager, node, context);
|
||||
}
|
||||
catch { /* ignored */ }
|
||||
|
||||
throw new InvalidMappingException(
|
||||
"SpriteSpecifier was neither a Texture nor an EntityPrototype but got provided a valuedatanode");
|
||||
}
|
||||
|
||||
DeserializationResult ITypeReader<EntityPrototype, ValueDataNode>.Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
return DeserializationResult.Value(new EntityPrototype(node.Value));
|
||||
}
|
||||
|
||||
DeserializationResult ITypeReader<Rsi, MappingDataNode>.Read(ISerializationManager serializationManager, MappingDataNode node,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
if (!node.TryGetNode("sprite", out var pathNode))
|
||||
{
|
||||
throw new InvalidMappingException("Expected sprite-node");
|
||||
}
|
||||
|
||||
throw new InvalidNodeTypeException();
|
||||
}
|
||||
|
||||
public ValidatedNode Validate(ISerializationManager serializationManager, MappingDataNode node,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return node.HasNode("sprite") &&
|
||||
node.TryGetNode("state", out var stateNode) &&
|
||||
stateNode is ValueDataNode &&
|
||||
serializationManager.ReadValue<ResourcePath>(stateNode) != null
|
||||
? new ValidatedValueNode(node)
|
||||
: new ErrorNode(node);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, SpriteSpecifier value,
|
||||
bool alwaysWrite = false,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
switch (value)
|
||||
if (!node.TryGetNode("state", out var stateNode) || stateNode is not ValueDataNode valueDataNode)
|
||||
{
|
||||
case Texture tex:
|
||||
return serializationManager.WriteValue(tex.TexturePath, alwaysWrite, context);
|
||||
case Rsi rsi:
|
||||
var mapping = new MappingDataNode();
|
||||
mapping.AddNode("sprite", serializationManager.WriteValue(rsi.RsiPath, alwaysWrite, context));
|
||||
mapping.AddNode("state", new ValueDataNode(rsi.RsiState));
|
||||
return mapping;
|
||||
throw new InvalidMappingException("Expected state-node as a valuenode");
|
||||
}
|
||||
|
||||
throw new NotImplementedException();
|
||||
var path = serializationManager.ReadValueOrThrow<ResourcePath>(pathNode, context);
|
||||
return DeserializationResult.Value(new Rsi(path, valueDataNode.Value));
|
||||
}
|
||||
|
||||
public Rsi Copy(ISerializationManager serializationManager, Rsi source, Rsi target, ISerializationContext? context = null)
|
||||
|
||||
DeserializationResult ITypeReader<SpriteSpecifier, MappingDataNode>.Read(ISerializationManager serializationManager, MappingDataNode node,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
return new(source.RsiPath, source.RsiState);
|
||||
return ((ITypeReader<Rsi, MappingDataNode>) this).Read(serializationManager, node, context);
|
||||
}
|
||||
|
||||
public Texture Copy(ISerializationManager serializationManager, Texture source, Texture target, ISerializationContext? context = null)
|
||||
ValidatedNode ITypeReader<SpriteSpecifier, ValueDataNode>.Validate(ISerializationManager serializationManager, ValueDataNode node,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
return new(source.TexturePath);
|
||||
var texNode = ((ITypeReader<Texture, ValueDataNode>) this).Validate(serializationManager, node, context);
|
||||
if (texNode is ErrorNode) return texNode;
|
||||
|
||||
var protNode = ((ITypeReader<EntityPrototype, ValueDataNode>) this).Validate(serializationManager, node, context);
|
||||
if (protNode is ErrorNode) return texNode;
|
||||
|
||||
return new ValidatedValueNode(node);
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public EntityPrototype Copy(ISerializationManager serializationManager, EntityPrototype source,
|
||||
EntityPrototype target, ISerializationContext? context = null)
|
||||
ValidatedNode ITypeReader<EntityPrototype, ValueDataNode>.Validate(ISerializationManager serializationManager, ValueDataNode node,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
return new(source.EntityPrototypeId);
|
||||
return string.IsNullOrWhiteSpace(node.Value) ? new ErrorNode(node) : new ValidatedValueNode(node);
|
||||
}
|
||||
|
||||
|
||||
ValidatedNode ITypeReader<Texture, ValueDataNode>.Validate(ISerializationManager serializationManager, ValueDataNode node,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
return serializationManager.ValidateNode(typeof(ResourcePath), node, context);
|
||||
}
|
||||
|
||||
ValidatedNode ITypeReader<SpriteSpecifier, MappingDataNode>.Validate(ISerializationManager serializationManager, MappingDataNode node,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
return ((ITypeReader<Rsi, MappingDataNode>) this).Validate(serializationManager, node, context);
|
||||
}
|
||||
|
||||
|
||||
ValidatedNode ITypeReader<Rsi, MappingDataNode>.Validate(ISerializationManager serializationManager, MappingDataNode node,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
if (!node.TryGetNode("sprite", out var pathNode))
|
||||
{
|
||||
return new ErrorNode(node);
|
||||
}
|
||||
|
||||
if (!node.TryGetNode("state", out var stateNode) || stateNode is not ValueDataNode valueDataNode)
|
||||
{
|
||||
return new ErrorNode(node);
|
||||
}
|
||||
|
||||
var path = serializationManager.ValidateNode(typeof(ResourcePath), pathNode, context);
|
||||
if (path is ErrorNode) return new ErrorNode(node);
|
||||
return new ValidatedValueNode(node);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, Texture value, bool alwaysWrite = false,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return serializationManager.WriteValue(value.TexturePath, alwaysWrite, context);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, EntityPrototype value, bool alwaysWrite = false,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new ValueDataNode(value.EntityPrototypeId);
|
||||
}
|
||||
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, Rsi value, bool alwaysWrite = false,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
var mapping = new MappingDataNode();
|
||||
mapping.AddNode("sprite", serializationManager.WriteValue(value.RsiPath));
|
||||
mapping.AddNode("state", new ValueDataNode(value.RsiState));
|
||||
return mapping;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user