Add AppearanceSystem Data dictionaries and RemoveData (#5288)

* Improved Appearance

* PR changes
This commit is contained in:
SlamBamActionman
2024-07-13 07:33:05 +02:00
committed by GitHub
parent b8924a04cf
commit 4920ecaa64
2 changed files with 29 additions and 3 deletions

View File

@@ -2,8 +2,11 @@ using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.GameStates;
using Robust.Shared.Utility;
using Robust.Shared.Serialization.Manager;
namespace Robust.Client.GameObjects
{
@@ -11,6 +14,7 @@ namespace Robust.Client.GameObjects
public sealed class AppearanceSystem : SharedAppearanceSystem
{
private readonly Queue<(EntityUid uid, AppearanceComponent)> _queuedUpdates = new();
[Dependency] private readonly ISerializationManager _serialization = default!;
public override void Initialize()
{
@@ -74,10 +78,13 @@ namespace Robust.Client.GameObjects
foreach (var (key, value) in data)
{
object? serializationObject;
if (value.GetType().IsValueType)
newDict[key] = value;
else if (value is ICloneable cloneable)
newDict[key] = cloneable.Clone();
else if ((serializationObject = _serialization.CreateCopy(value)) != null)
newDict[key] = serializationObject;
else
throw new NotSupportedException("Invalid object in appearance data dictionary. Appearance data must be cloneable");
}

View File

@@ -29,6 +29,11 @@ public abstract class SharedAppearanceSystem : EntitySystem
{
}
private bool CheckIfApplyingState(AppearanceComponent component)
{
return _timing.ApplyingState && component.NetSyncEnabled; // TODO consider removing this and avoiding the component resolve altogether.
}
public void SetData(EntityUid uid, Enum key, object value, AppearanceComponent? component = null)
{
if (!Resolve(uid, ref component, false))
@@ -36,20 +41,34 @@ public abstract class SharedAppearanceSystem : EntitySystem
// If appearance data is changing due to server state application, the server's comp state is getting applied
// anyways, so we can skip this.
if (_timing.ApplyingState
&& component.NetSyncEnabled) // TODO consider removing this and avoiding the component resolve altogether.
if (CheckIfApplyingState(component))
return;
if (component.AppearanceData.TryGetValue(key, out var existing) && existing.Equals(value))
return;
DebugTools.Assert(value.GetType().IsValueType || value is ICloneable, "Appearance data values must be cloneable.");
//Commented out until there is a suitable way to check that ISerializationManager.CopyTo works without doing the copying
//DebugTools.Assert(value.GetType().IsValueType || value is ICloneable, "Appearance data values must be cloneable.");
component.AppearanceData[key] = value;
Dirty(uid, component);
QueueUpdate(uid, component);
}
public void RemoveData(EntityUid uid, Enum key, AppearanceComponent? component = null)
{
if (!Resolve(uid, ref component, false))
return;
if (CheckIfApplyingState(component))
return;
component.AppearanceData.Remove(key);
Dirty(uid, component);
QueueUpdate(uid, component);
}
public bool TryGetData<T>(EntityUid uid, Enum key, [NotNullWhen(true)] out T value, AppearanceComponent? component = null)
{
if (Resolve(uid, ref component) &&