Skip to content

Commit

Permalink
Add AppearanceSystem Data dictionaries and RemoveData (#5288)
Browse files Browse the repository at this point in the history
* Improved Appearance

* PR changes
  • Loading branch information
SlamBamActionman authored Jul 13, 2024
1 parent b8924a0 commit 4920eca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Robust.Client/GameObjects/EntitySystems/AppearanceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
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
{
[UsedImplicitly]
public sealed class AppearanceSystem : SharedAppearanceSystem
{
private readonly Queue<(EntityUid uid, AppearanceComponent)> _queuedUpdates = new();
[Dependency] private readonly ISerializationManager _serialization = default!;

public override void Initialize()
{
Expand Down Expand Up @@ -74,10 +78,13 @@ private Dictionary<Enum, object> CloneAppearanceData(Dictionary<Enum, object> da

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");
}
Expand Down
25 changes: 22 additions & 3 deletions Robust.Shared/GameObjects/Systems/SharedAppearanceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,46 @@ public virtual void QueueUpdate(EntityUid uid, AppearanceComponent component)
{
}

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))
return;

// 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) &&
Expand Down

0 comments on commit 4920eca

Please sign in to comment.