Skip to content

Commit 06998fe

Browse files
authored
Merge pull request #69 from colyseus/fix-array-changes
use Dictionary for ArraySchema internally to prevent duplicated entries
2 parents 3e0c5a2 + b2b9bff commit 06998fe

File tree

1 file changed

+29
-10
lines changed
  • Assets/Plugins/Colyseus/Serializer/Schema

1 file changed

+29
-10
lines changed

Assets/Plugins/Colyseus/Serializer/Schema/Schema.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,19 @@ public interface ISchemaCollection
126126

127127
public class ArraySchema<T> : ISchemaCollection
128128
{
129-
public List<T> Items;
129+
public Dictionary<int, T> Items;
130130
public event EventHandler<KeyValueEventArgs<T, int>> OnAdd;
131131
public event EventHandler<KeyValueEventArgs<T, int>> OnChange;
132132
public event EventHandler<KeyValueEventArgs<T, int>> OnRemove;
133133

134134
public ArraySchema()
135135
{
136-
Items = new List<T>();
136+
Items = new Dictionary<int, T>();
137137
}
138138

139-
public ArraySchema(List<T> items = null)
139+
public ArraySchema(Dictionary<int, T> items = null)
140140
{
141-
Items = items ?? new List<T>();
141+
Items = items ?? new Dictionary<int, T>();
142142
}
143143

144144
public ISchemaCollection Clone()
@@ -170,18 +170,21 @@ public int Count
170170
public T this[int index]
171171
{
172172
get {
173-
return (Items.Count > index) ? (T)Items[index] : default(T);
173+
T value;
174+
Items.TryGetValue(index, out value);
175+
return value;
174176
}
175-
set { Items.Insert(index, value); }
177+
set { Items[index] = value; }
176178
}
177179

178180
public object this[object key]
179181
{
180182
get {
181-
int k = (int)key;
182-
return (Items.Count > k) ? (T)Items[k] : default(T);
183+
T value;
184+
Items.TryGetValue((int)key, out value);
185+
return value;
183186
}
184-
set { Items.Insert((int)key, (T)value); }
187+
set { Items[(int)key] = (T)value; }
185188
}
186189

187190
public object GetItems()
@@ -191,7 +194,15 @@ public object GetItems()
191194

192195
public void SetItems(object items)
193196
{
194-
Items = (List<T>) items;
197+
Items = (Dictionary<int, T>) items;
198+
}
199+
200+
public void ForEach (Action<T> action)
201+
{
202+
foreach (KeyValuePair<int, T> item in Items)
203+
{
204+
action(item.Value);
205+
}
195206
}
196207

197208
public void TriggerAll()
@@ -360,6 +371,14 @@ public void SetItems(object items)
360371
throw new NotImplementedException();
361372
}
362373

374+
public void ForEach (Action<string, T> action)
375+
{
376+
foreach (KeyValuePair<string, T> item in Items)
377+
{
378+
action(item.Key, item.Value);
379+
}
380+
}
381+
363382
public void TriggerAll()
364383
{
365384
if (OnAdd == null) { return; }

0 commit comments

Comments
 (0)