Skip to content

Commit 7c82eef

Browse files
committed
ZObject.SetParent() now keeps object tree intact
1 parent deea4f5 commit 7c82eef

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

ZMachine/ZObject.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ public ushort GetProperty(ushort property)
115115

116116
public void Insert(ZObject obj)
117117
{
118+
var oldParent = obj.GetParent();
119+
if (oldParent == this) return;
120+
118121
obj.SetParent(this);
119-
var child = GetChild();
120-
if (child != null)
121-
obj.SetSibling(GetChild());
122+
obj.SetSibling(GetChild());
122123

123124
SetChild(obj);
124-
125125
}
126126

127127

@@ -132,7 +132,14 @@ public ZObject GetParent()
132132

133133
public void SetParent(ZObject obj)
134134
{
135-
table.Machine.WriteByte(address+4, obj.Id);
135+
var oldParent = GetParent();
136+
if (oldParent != null)
137+
{
138+
oldParent.SetChild(GetSibling());
139+
}
140+
141+
var id = obj == null ? 0 : obj.Id;
142+
table.Machine.WriteByte(address+4, (byte)id);
136143
}
137144

138145
public ZObject GetSibling()
@@ -142,7 +149,8 @@ public ZObject GetSibling()
142149

143150
public void SetSibling(ZObject obj)
144151
{
145-
table.Machine.WriteByte(address+5, obj.Id);
152+
var id = obj == null ? 0 : obj.Id;
153+
table.Machine.WriteByte(address+5, (byte)id);
146154
}
147155

148156
public ZObject GetChild()
@@ -152,7 +160,8 @@ public ZObject GetChild()
152160

153161
public void SetChild(ZObject obj)
154162
{
155-
table.Machine.WriteByte(address+6, obj.Id);
163+
var id = obj == null ? 0 : obj.Id;
164+
table.Machine.WriteByte(address+6, (byte)id);
156165
}
157166

158167

0 commit comments

Comments
 (0)