Skip to content

Commit

Permalink
move atom name/desc handling to always defer to appearance
Browse files Browse the repository at this point in the history
  • Loading branch information
TobleroneSwordfish committed Dec 18, 2024
1 parent e2df05e commit 8f0b2be
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 21 deletions.
10 changes: 10 additions & 0 deletions Content.Tests/DMProject/Tests/Image/appearance.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/obj/thingtocopy
name = "hello"
desc = "this is a thing"

/proc/RunTest()
var/obj/thingtocopy/T = new()
var/obj/otherthing = new()
otherthing.appearance = T.appearance
ASSERT(otherthing.name == T.name)
ASSERT(otherthing.desc == T.desc)
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private void Update() {
// Would be nice if we could use ViewVariables instead, but I couldn't find a nice way to do that
// Would be especially nice if we could use VV to make these editable
AddPropertyIfNotDefault("Name", appearance.Name, IconAppearance.Default.Name);
AddPropertyIfNotDefault("Desc", appearance.Desc, IconAppearance.Default.Desc);
AddPropertyIfNotDefault("Icon State", appearance.IconState, IconAppearance.Default.IconState);
AddPropertyIfNotDefault("Direction", appearance.Direction, IconAppearance.Default.Direction);
AddPropertyIfNotDefault("Inherits Direction", appearance.InheritsDirection, IconAppearance.Default.InheritsDirection);
Expand Down
9 changes: 9 additions & 0 deletions OpenDreamRuntime/AtomManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public void DeleteMovableEntity(DreamObjectMovable movable) {
public bool IsValidAppearanceVar(string name) {
switch (name) {
case "name":
case "desc":
case "icon":
case "icon_state":
case "dir":
Expand Down Expand Up @@ -248,6 +249,10 @@ public void SetAppearanceVar(IconAppearance appearance, string varName, DreamVal
value.TryGetValueAsString(out var name);
appearance.Name = name ?? string.Empty;
break;
case "desc":
value.TryGetValueAsString(out var desc);
appearance.Desc = desc ?? string.Empty;
break;
case "icon":
if (_resourceManager.TryLoadIcon(value, out var icon)) {
appearance.Icon = icon.Id;
Expand Down Expand Up @@ -387,6 +392,8 @@ public DreamValue GetAppearanceVar(IconAppearance appearance, string varName) {
switch (varName) {
case "name":
return new(appearance.Name);
case "desc":
return new(appearance.Desc);
case "icon":
if (appearance.Icon == null)
return DreamValue.Null;
Expand Down Expand Up @@ -589,6 +596,7 @@ public IconAppearance GetAppearanceFromDefinition(DreamObjectDefinition def) {
return appearance;

def.TryGetVariable("name", out var nameVar);
def.TryGetVariable("desc", out var descVar);
def.TryGetVariable("icon", out var iconVar);
def.TryGetVariable("icon_state", out var stateVar);
def.TryGetVariable("color", out var colorVar);
Expand All @@ -609,6 +617,7 @@ public IconAppearance GetAppearanceFromDefinition(DreamObjectDefinition def) {

appearance = new IconAppearance();
SetAppearanceVar(appearance, "name", nameVar);
SetAppearanceVar(appearance, "desc", descVar);
SetAppearanceVar(appearance, "icon", iconVar);
SetAppearanceVar(appearance, "icon_state", stateVar);
SetAppearanceVar(appearance, "color", colorVar);
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Objects/DreamObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Utility;
using System.ComponentModel;

namespace OpenDreamRuntime.Objects {
[Virtual]
Expand Down
26 changes: 6 additions & 20 deletions OpenDreamRuntime/Objects/Types/DreamObjectAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace OpenDreamRuntime.Objects.Types;

[Virtual]
public class DreamObjectAtom : DreamObject {
public string? Name;
public string? Desc;
public readonly DreamOverlaysList Overlays;
public readonly DreamOverlaysList Underlays;
Expand All @@ -22,11 +21,6 @@ public DreamObjectAtom(DreamObjectDefinition objectDefinition) : base(objectDefi
AtomManager.AddAtom(this);
}

public override void Initialize(DreamProcArguments args) {
ObjectDefinition.Variables["name"].TryGetValueAsString(out Name);
ObjectDefinition.Variables["desc"].TryGetValueAsString(out Desc);
}

protected override void HandleDeletion(bool possiblyThreaded) {
// SAFETY: RemoveAtom is not threadsafe.
if (possiblyThreaded) {
Expand All @@ -38,6 +32,12 @@ protected override void HandleDeletion(bool possiblyThreaded) {

base.HandleDeletion(possiblyThreaded);
}
public string GetDesc() {
if (!TryGetVariable("desc", out DreamValue descVar) || !descVar.TryGetValueAsString(out string? desc))
return ObjectDefinition.Type.ToString();

return desc;
}

protected override bool TryGetVar(string varName, out DreamValue value) {
switch (varName) {
Expand All @@ -50,13 +50,6 @@ protected override bool TryGetVar(string varName, out DreamValue value) {
case "loc":
value = DreamValue.Null;
return true;

case "name":
value = (Name != null) ? new(Name) : DreamValue.Null;
return true;
case "desc":
value = (Desc != null) ? new(Desc) : DreamValue.Null;
return true;
case "appearance":
var appearanceCopy = new IconAppearance(AtomManager.MustGetAppearance(this)!);

Expand Down Expand Up @@ -102,13 +95,6 @@ protected override void SetVar(string varName, DreamValue value) {
case "z":
case "loc":
break;

case "name":
value.TryGetValueAsString(out Name);
break;
case "desc":
value.TryGetValueAsString(out Desc);
break;
case "appearance":
if (!AtomManager.TryCreateAppearanceFrom(value, out var newAppearance))
return; // Ignore attempts to set an invalid appearance
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamRuntime/Objects/Types/DreamObjectMovable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override void Initialize(DreamProcArguments args) {

if (EntityManager.TryGetComponent(Entity, out MetaDataComponent? metaData)) {
MetaDataSystem?.SetEntityName(Entity, GetDisplayName(), metaData);
MetaDataSystem?.SetEntityDescription(Entity, Desc ?? string.Empty, metaData);
MetaDataSystem?.SetEntityDescription(Entity, GetDesc(), metaData);
}

args.GetArgument(0).TryGetValueAsDreamObject<DreamObjectAtom>(out var loc);
Expand Down
4 changes: 4 additions & 0 deletions OpenDreamShared/Dream/IconAppearance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public sealed class IconAppearance : IEquatable<IconAppearance> {
public static readonly IconAppearance Default = new();

[ViewVariables] public string Name = string.Empty;
[ViewVariables] public string Desc = string.Empty;
[ViewVariables] public int? Icon;
[ViewVariables] public string? IconState;
[ViewVariables] public AtomDirection Direction = AtomDirection.South;
Expand Down Expand Up @@ -82,6 +83,7 @@ public IconAppearance() {

public IconAppearance(IconAppearance appearance) {
Name = appearance.Name;
Desc = appearance.Desc;
Icon = appearance.Icon;
IconState = appearance.IconState;
Direction = appearance.Direction;
Expand Down Expand Up @@ -119,6 +121,7 @@ public bool Equals(IconAppearance? appearance) {
if (appearance == null) return false;

if (appearance.Name != Name) return false;
if (appearance.Desc != Desc) return false;
if (appearance.Icon != Icon) return false;
if (appearance.IconState != IconState) return false;
if (appearance.Direction != Direction) return false;
Expand Down Expand Up @@ -207,6 +210,7 @@ public override int GetHashCode() {
HashCode hashCode = new HashCode();

hashCode.Add(Name);
hashCode.Add(Desc);
hashCode.Add(Icon);
hashCode.Add(IconState);
hashCode.Add(Direction);
Expand Down
9 changes: 9 additions & 0 deletions OpenDreamShared/Network/Messages/MsgAllAppearances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public sealed class MsgAllAppearances(Dictionary<int, IconAppearance> allAppeara

private enum Property : byte {
Name,
Desc,
Icon,
IconState,
Direction,
Expand Down Expand Up @@ -66,6 +67,9 @@ public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer
case Property.Name:
appearance.Name = buffer.ReadString();
break;
case Property.Desc:
appearance.Desc = buffer.ReadString();
break;
case Property.Id:
appearanceId = buffer.ReadVariableInt32();
break;
Expand Down Expand Up @@ -230,6 +234,11 @@ public override void WriteToBuffer(NetOutgoingMessage buffer, IRobustSerializer
buffer.Write(appearance.Name);
}

if (appearance.Desc != IconAppearance.Default.Desc) {
buffer.Write((byte)Property.Desc);
buffer.Write(appearance.Desc);
}

if (appearance.Icon != null) {
buffer.Write((byte)Property.Icon);
buffer.WriteVariableInt32(appearance.Icon.Value);
Expand Down

0 comments on commit 8f0b2be

Please sign in to comment.