Skip to content

Commit

Permalink
schema: keep insertion order of maps during decoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Apr 14, 2019
1 parent 617553a commit 5cc1fe2
Showing 1 changed file with 79 additions and 9 deletions.
88 changes: 79 additions & 9 deletions Assets/Plugins/Colyseus/Serializer/Schema/Schema.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Reflection;

Expand Down Expand Up @@ -194,19 +195,19 @@ public void InvokeOnRemove(object item, object index)

public class MapSchema<T> : ISchemaCollection
{
public Dictionary<string, T> Items;
public OrderedDictionary Items = new OrderedDictionary();
public event EventHandler<KeyValueEventArgs<T, string>> OnAdd;
public event EventHandler<KeyValueEventArgs<T, string>> OnChange;
public event EventHandler<KeyValueEventArgs<T, string>> OnRemove;

public MapSchema()
{
Items = new Dictionary<string, T>();
Items = new OrderedDictionary();
}

public MapSchema(Dictionary<string, T> items = null)
public MapSchema(OrderedDictionary items = null)
{
Items = items ?? new Dictionary<string, T>();
Items = items ?? new OrderedDictionary();
}

public ISchemaCollection Clone()
Expand Down Expand Up @@ -234,7 +235,7 @@ public bool HasSchemaChild
{
get {
T value;
Items.TryGetValue(key, out value);
TryGetValue(key, out value);
return value;
}
set { Items[key] = value; }
Expand All @@ -244,20 +245,89 @@ public bool HasSchemaChild
{
get {
T value;
Items.TryGetValue(key as string, out value);
TryGetValue(key as string, out value);
return value;
}
set { Items[(string) key] = (T) value; }
}

public object GetItems()
{
return Items;
}

public void Add(KeyValuePair<string, T> item)
{
Items[item.Key] = item.Value;
}

public void Clear()
{
Items.Clear();
}

public bool Contains(KeyValuePair<string, T> item)
{
return Items.Contains(item.Key);
}

public bool Remove(KeyValuePair<string, T> item)
{
T value;
if (TryGetValue(item.Key, out value) && Equals(value, item.Value))
{
Remove(item.Key);
return true;
}
return false;
}

public int Count
{
get { return Items.Count; }
}

public object GetItems()
public bool ContainsKey(string key)
{
return Items;
return Items.Contains(key);
}

public void Add(string key, T value)
{
Items.Add(key, value);
}

public bool Remove(string key)
{
var result = Items.Contains(key);
if (result)
{
Items.Remove(key);
}
return result;
}

public bool TryGetValue(string key, out T value)
{
object foundValue;
if ((foundValue = Items[key]) != null || Items.Contains(key))
{
// Either found with a non-null value, or contained value is null.
value = (T)foundValue;
return true;
}
value = default(T);
return false;
}

public ICollection Keys
{
get { return Items.Keys; }
}

public ICollection Values
{
get { return Items.Values; }
}

public void SetItems(object items)
Expand Down Expand Up @@ -489,7 +559,7 @@ public void Decode(byte[] bytes, Iterator it = null)

bool hasIndexChange = false;

IDictionary items = currentValue.GetItems() as IDictionary;
OrderedDictionary items = currentValue.GetItems() as OrderedDictionary;
string[] mapKeys = new string[items.Keys.Count];
items.Keys.CopyTo(mapKeys, 0);

Expand Down

0 comments on commit 5cc1fe2

Please sign in to comment.