Remove and RemoveAt now get the item that was removed

This commit is contained in:
Paul Pacheco 2018-11-21 14:16:25 -06:00
parent 1e55a28f67
commit f22f4a2c8e
2 changed files with 53 additions and 7 deletions

View File

@ -99,7 +99,7 @@ public T GetItem(int i)
[EditorBrowsable(EditorBrowsableState.Never)] [EditorBrowsable(EditorBrowsableState.Never)]
public abstract class SyncList<T> : IList<T>, SyncObject public abstract class SyncList<T> : IList<T>, SyncObject
{ {
public delegate void SyncListChanged(Operation op, int itemIndex); public delegate void SyncListChanged(Operation op, int itemIndex, T item);
readonly List<T> m_Objects = new List<T>(); readonly List<T> m_Objects = new List<T>();
@ -164,7 +164,7 @@ void AddOperation(Operation op, int itemIndex, T item)
SyncListChanged listChanged = Callback; SyncListChanged listChanged = Callback;
if (listChanged != null) if (listChanged != null)
{ {
listChanged(op, itemIndex); listChanged(op, itemIndex, item);
} }
} }
@ -264,7 +264,7 @@ public void OnDeserializeDelta(NetworkReader reader)
// that we have not applied yet // that we have not applied yet
bool apply = changesAhead == 0; bool apply = changesAhead == 0;
int index = 0; int index = 0;
T item; T item = default(T);
switch (operation) switch (operation)
{ {
@ -305,6 +305,7 @@ public void OnDeserializeDelta(NetworkReader reader)
index = (int)reader.ReadPackedUInt32(); index = (int)reader.ReadPackedUInt32();
if (apply) if (apply)
{ {
item = m_Objects[index];
m_Objects.RemoveAt(index); m_Objects.RemoveAt(index);
} }
break; break;
@ -323,7 +324,7 @@ public void OnDeserializeDelta(NetworkReader reader)
SyncListChanged listChanged = Callback; SyncListChanged listChanged = Callback;
if (apply && listChanged != null) if (apply && listChanged != null)
{ {
listChanged(operation, index); listChanged(operation, index, item);
} }
// we just skipped this change // we just skipped this change

View File

@ -166,14 +166,59 @@ public void SyncListFloatTest()
public void CallbackTest() public void CallbackTest()
{ {
bool called = false; bool called = false;
int calledIndex = 0;
clientSyncList.Callback += (op, index) => { called = true; calledIndex = index; }; clientSyncList.Callback += (op, index, item) =>
{
called = true;
Assert.That(op, Is.EqualTo(SyncList<string>.Operation.OP_ADD));
Assert.That(index, Is.EqualTo(3));
Assert.That(item, Is.EqualTo("yay"));
};
serverSyncList.Add("yay"); serverSyncList.Add("yay");
SerializeDeltaTo(serverSyncList, clientSyncList); SerializeDeltaTo(serverSyncList, clientSyncList);
Assert.That(called, Is.True);
}
[Test]
public void CallbackRemoveTest()
{
bool called = false;
clientSyncList.Callback += (op, index, item) =>
{
called = true;
Assert.That(op, Is.EqualTo(SyncList<string>.Operation.OP_REMOVE));
Assert.That(item, Is.EqualTo("World"));
};
serverSyncList.Remove("World");
SerializeDeltaTo(serverSyncList, clientSyncList);
Assert.That(called, Is.True);
}
[Test]
public void CallbackRemoveAtTest()
{
bool called = false;
clientSyncList.Callback += (op, index, item) =>
{
called = true;
Assert.That(op, Is.EqualTo(SyncList<string>.Operation.OP_REMOVEAT));
Assert.That(index, Is.EqualTo(1));
Assert.That(item, Is.EqualTo("World"));
};
serverSyncList.RemoveAt(1);
SerializeDeltaTo(serverSyncList, clientSyncList);
Assert.That(called, Is.True); Assert.That(called, Is.True);
Assert.That(calledIndex, Is.EqualTo(3));
} }
[Test] [Test]