mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 19:10:32 +00:00
fix(SyncSet): AddOperation now properly handles oldItem
This commit is contained in:
parent
0867186289
commit
1199a1b42d
@ -66,16 +66,36 @@ public override void Reset()
|
|||||||
// this should be called after a successful sync
|
// this should be called after a successful sync
|
||||||
public override void ClearChanges() => changes.Clear();
|
public override void ClearChanges() => changes.Clear();
|
||||||
|
|
||||||
void AddOperation(Operation op, T item, bool checkAccess)
|
void AddOperation(Operation op, T oldItem, T newItem, bool checkAccess)
|
||||||
{
|
{
|
||||||
if (checkAccess && IsReadOnly)
|
if (checkAccess && IsReadOnly)
|
||||||
throw new InvalidOperationException("SyncSets can only be modified by the owner.");
|
throw new InvalidOperationException("SyncSets can only be modified by the owner.");
|
||||||
|
|
||||||
Change change = new Change
|
Change change = default;
|
||||||
|
switch (op)
|
||||||
|
{
|
||||||
|
case Operation.OP_ADD:
|
||||||
|
change = new Change
|
||||||
{
|
{
|
||||||
operation = op,
|
operation = op,
|
||||||
item = item
|
item = newItem
|
||||||
};
|
};
|
||||||
|
break;
|
||||||
|
case Operation.OP_REMOVE:
|
||||||
|
change = new Change
|
||||||
|
{
|
||||||
|
operation = op,
|
||||||
|
item = oldItem
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case Operation.OP_CLEAR:
|
||||||
|
change = new Change
|
||||||
|
{
|
||||||
|
operation = op,
|
||||||
|
item = default
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (IsRecording())
|
if (IsRecording())
|
||||||
{
|
{
|
||||||
@ -86,22 +106,27 @@ void AddOperation(Operation op, T item, bool checkAccess)
|
|||||||
switch (op)
|
switch (op)
|
||||||
{
|
{
|
||||||
case Operation.OP_ADD:
|
case Operation.OP_ADD:
|
||||||
OnAdd?.Invoke(item);
|
OnAdd?.Invoke(newItem);
|
||||||
|
#pragma warning disable CS0618 // Type or member is obsolete
|
||||||
|
Callback?.Invoke(op, newItem);
|
||||||
|
#pragma warning restore CS0618 // Type or member is obsolete
|
||||||
break;
|
break;
|
||||||
case Operation.OP_REMOVE:
|
case Operation.OP_REMOVE:
|
||||||
OnRemove?.Invoke(item);
|
OnRemove?.Invoke(oldItem);
|
||||||
|
#pragma warning disable CS0618 // Type or member is obsolete
|
||||||
|
Callback?.Invoke(op, oldItem);
|
||||||
|
#pragma warning restore CS0618 // Type or member is obsolete
|
||||||
break;
|
break;
|
||||||
case Operation.OP_CLEAR:
|
case Operation.OP_CLEAR:
|
||||||
OnClear?.Invoke();
|
OnClear?.Invoke();
|
||||||
|
#pragma warning disable CS0618 // Type or member is obsolete
|
||||||
|
Callback?.Invoke(op, default);
|
||||||
|
#pragma warning restore CS0618 // Type or member is obsolete
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma warning disable CS0618 // Type or member is obsolete
|
|
||||||
Callback?.Invoke(op, item);
|
|
||||||
#pragma warning restore CS0618 // Type or member is obsolete
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddOperation(Operation op, bool checkAccess) => AddOperation(op, default, checkAccess);
|
void AddOperation(Operation op, bool checkAccess) => AddOperation(op, default, default, checkAccess);
|
||||||
|
|
||||||
public override void OnSerializeAll(NetworkWriter writer)
|
public override void OnSerializeAll(NetworkWriter writer)
|
||||||
{
|
{
|
||||||
@ -173,33 +198,34 @@ public override void OnDeserializeDelta(NetworkReader reader)
|
|||||||
// apply the operation only if it is a new change
|
// apply the operation only if it is a new change
|
||||||
// that we have not applied yet
|
// that we have not applied yet
|
||||||
bool apply = changesAhead == 0;
|
bool apply = changesAhead == 0;
|
||||||
T item = default;
|
T oldItem = default;
|
||||||
|
T newItem = default;
|
||||||
|
|
||||||
switch (operation)
|
switch (operation)
|
||||||
{
|
{
|
||||||
case Operation.OP_ADD:
|
case Operation.OP_ADD:
|
||||||
item = reader.Read<T>();
|
newItem = reader.Read<T>();
|
||||||
if (apply)
|
if (apply)
|
||||||
{
|
{
|
||||||
objects.Add(item);
|
objects.Add(newItem);
|
||||||
// add dirty + changes.
|
// add dirty + changes.
|
||||||
// ClientToServer needs to set dirty in server OnDeserialize.
|
// ClientToServer needs to set dirty in server OnDeserialize.
|
||||||
// no access check: server OnDeserialize can always
|
// no access check: server OnDeserialize can always
|
||||||
// write, even for ClientToServer (for broadcasting).
|
// write, even for ClientToServer (for broadcasting).
|
||||||
AddOperation(Operation.OP_ADD, item, false);
|
AddOperation(Operation.OP_ADD, default, newItem, false);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Operation.OP_REMOVE:
|
case Operation.OP_REMOVE:
|
||||||
item = reader.Read<T>();
|
oldItem = reader.Read<T>();
|
||||||
if (apply)
|
if (apply)
|
||||||
{
|
{
|
||||||
objects.Remove(item);
|
objects.Remove(oldItem);
|
||||||
// add dirty + changes.
|
// add dirty + changes.
|
||||||
// ClientToServer needs to set dirty in server OnDeserialize.
|
// ClientToServer needs to set dirty in server OnDeserialize.
|
||||||
// no access check: server OnDeserialize can always
|
// no access check: server OnDeserialize can always
|
||||||
// write, even for ClientToServer (for broadcasting).
|
// write, even for ClientToServer (for broadcasting).
|
||||||
AddOperation(Operation.OP_REMOVE, item, false);
|
AddOperation(Operation.OP_REMOVE, oldItem, default, false);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -230,7 +256,7 @@ public bool Add(T item)
|
|||||||
{
|
{
|
||||||
if (objects.Add(item))
|
if (objects.Add(item))
|
||||||
{
|
{
|
||||||
AddOperation(Operation.OP_ADD, item, true);
|
AddOperation(Operation.OP_ADD, default, item, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -239,7 +265,7 @@ public bool Add(T item)
|
|||||||
void ICollection<T>.Add(T item)
|
void ICollection<T>.Add(T item)
|
||||||
{
|
{
|
||||||
if (objects.Add(item))
|
if (objects.Add(item))
|
||||||
AddOperation(Operation.OP_ADD, item, true);
|
AddOperation(Operation.OP_ADD, default, item, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
@ -258,7 +284,7 @@ public bool Remove(T item)
|
|||||||
{
|
{
|
||||||
if (objects.Remove(item))
|
if (objects.Remove(item))
|
||||||
{
|
{
|
||||||
AddOperation(Operation.OP_REMOVE, item, true);
|
AddOperation(Operation.OP_REMOVE, item, default, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user