Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Nov 1, 2020
1 parent c860a87 commit e4bdfe3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
23 changes: 19 additions & 4 deletions Assets/Plugins/Colyseus/Serializer/Schema/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ public class DataChange
public delegate void KeyValueEventHandler<T, K>(T value, K key);
public delegate void OnRemoveEventHandler();

public interface ISchemaCollection
public interface ISchemaCollection : IRef
{
void MoveEventHandlers(ISchemaCollection previousInstance);
void InvokeOnAdd(object item, object index);
void InvokeOnChange(object item, object index);
void InvokeOnRemove(object item, object index);
Expand Down Expand Up @@ -115,6 +114,8 @@ public interface IRef

object GetByIndex(int index);
void DeleteByIndex(int index);

void MoveEventHandlers(IRef previousInstance);
}

public class Schema : IRef
Expand Down Expand Up @@ -176,6 +177,21 @@ public object this[string propertyName]
return fieldChildTypes;
}

public void MoveEventHandlers(IRef previousInstance)
{
OnChange = ((Schema)previousInstance).OnChange;
OnRemove = ((Schema)previousInstance).OnRemove;

foreach (var item in ((Schema)previousInstance).fieldsByIndex)
{
var child = GetByIndex(item.Key);
if (child is IRef)
{
((IRef)child).MoveEventHandlers((IRef)previousInstance.GetByIndex(item.Key));
}
}
}

public void Decode(byte[] bytes, Iterator it = null, ReferenceTracker refs = null)
{
var decode = Decoder.GetInstance();
Expand Down Expand Up @@ -349,8 +365,7 @@ public void Decode(byte[] bytes, Iterator it = null, ReferenceTracker refs = nul

if (previousValue != null)
{
((Schema)value).OnChange = ((Schema)previousValue).OnChange;
((Schema)value).OnRemove = ((Schema)previousValue).OnRemove;
((Schema)value).MoveEventHandlers((Schema)previousValue);

if (
((IRef)previousValue).__refId > 0 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Colyseus.Schema
{
public class ArraySchema<T> : ISchemaCollection, IRef
public class ArraySchema<T> : ISchemaCollection
{
public Dictionary<int, T> Items;
public event KeyValueEventHandler<T, int> OnAdd;
Expand Down Expand Up @@ -159,7 +159,7 @@ public void TriggerAll()
}
}

public void MoveEventHandlers(ISchemaCollection previousInstance)
public void MoveEventHandlers(IRef previousInstance)
{
OnAdd = ((ArraySchema<T>)previousInstance).OnAdd;
OnChange = ((ArraySchema<T>)previousInstance).OnChange;
Expand Down
4 changes: 2 additions & 2 deletions Assets/Plugins/Colyseus/Serializer/Schema/Types/MapSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Colyseus.Schema
{
public class MapSchema<T> : ISchemaCollection, IRef
public class MapSchema<T> : ISchemaCollection
{
public OrderedDictionary Items = new OrderedDictionary();
public event KeyValueEventHandler<T, string> OnAdd;
Expand Down Expand Up @@ -225,7 +225,7 @@ public void TriggerAll()
}
}

public void MoveEventHandlers(ISchemaCollection previousInstance)
public void MoveEventHandlers(IRef previousInstance)
{
OnAdd = ((MapSchema<T>)previousInstance).OnAdd;
OnChange = ((MapSchema<T>)previousInstance).OnChange;
Expand Down
2 changes: 1 addition & 1 deletion Server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"dependencies": {
"@colyseus/social": "^0.10.0",
"colyseus": "^0.14.0",
"colyseus": "^0.14.1",
"cors": "^2.8.5",
"express": "^4.13.3",
"express-jwt": "^5.3.1",
Expand Down

0 comments on commit e4bdfe3

Please sign in to comment.