breaking: NetworkBehaviour.SerializeSyncVars return type changed from bool to void to simplify weaver (and reduce branches)

This commit is contained in:
vis2k 2022-09-23 14:14:07 +07:00
parent 7c7f83362b
commit 9c5e10f71b
2 changed files with 7 additions and 30 deletions

View File

@ -971,10 +971,8 @@ public virtual void OnDeserialize(NetworkReader reader, bool initialState)
} }
// USED BY WEAVER // USED BY WEAVER
protected virtual bool SerializeSyncVars(NetworkWriter writer, bool initialState) protected virtual void SerializeSyncVars(NetworkWriter writer, bool initialState)
{ {
return false;
// SyncVar are written here in subclass // SyncVar are written here in subclass
// if initialState // if initialState
@ -996,21 +994,17 @@ protected virtual void DeserializeSyncVars(NetworkReader reader, bool initialSta
// read dirty SyncVars // read dirty SyncVars
} }
public bool SerializeObjectsAll(NetworkWriter writer) public void SerializeObjectsAll(NetworkWriter writer)
{ {
bool dirty = false;
for (int i = 0; i < syncObjects.Count; i++) for (int i = 0; i < syncObjects.Count; i++)
{ {
SyncObject syncObject = syncObjects[i]; SyncObject syncObject = syncObjects[i];
syncObject.OnSerializeAll(writer); syncObject.OnSerializeAll(writer);
dirty = true;
} }
return dirty;
} }
public bool SerializeObjectsDelta(NetworkWriter writer) public void SerializeObjectsDelta(NetworkWriter writer)
{ {
bool dirty = false;
// write the mask // write the mask
writer.WriteULong(syncObjectDirtyBits); writer.WriteULong(syncObjectDirtyBits);
// serializable objects, such as synclists // serializable objects, such as synclists
@ -1021,10 +1015,8 @@ public bool SerializeObjectsDelta(NetworkWriter writer)
if ((syncObjectDirtyBits & (1UL << i)) != 0) if ((syncObjectDirtyBits & (1UL << i)) != 0)
{ {
syncObject.OnSerializeDelta(writer); syncObject.OnSerializeDelta(writer);
dirty = true;
} }
} }
return dirty;
} }
internal void DeSerializeObjectsAll(NetworkReader reader) internal void DeSerializeObjectsAll(NetworkReader reader)

View File

@ -397,7 +397,7 @@ void GenerateSerialization(ref bool WeavingFailed)
MethodDefinition serialize = new MethodDefinition(SerializeMethodName, MethodDefinition serialize = new MethodDefinition(SerializeMethodName,
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig,
weaverTypes.Import<bool>()); weaverTypes.Import(typeof(void)));
serialize.Parameters.Add(new ParameterDefinition("writer", ParameterAttributes.None, weaverTypes.Import<NetworkWriter>())); serialize.Parameters.Add(new ParameterDefinition("writer", ParameterAttributes.None, weaverTypes.Import<NetworkWriter>()));
serialize.Parameters.Add(new ParameterDefinition("forceAll", ParameterAttributes.None, weaverTypes.Import<bool>())); serialize.Parameters.Add(new ParameterDefinition("forceAll", ParameterAttributes.None, weaverTypes.Import<bool>()));
@ -405,11 +405,7 @@ void GenerateSerialization(ref bool WeavingFailed)
serialize.Body.InitLocals = true; serialize.Body.InitLocals = true;
// loc_0, this local variable is to determine if any variable was dirty // base.SerializeSyncVars(writer, forceAll);
VariableDefinition dirtyLocal = new VariableDefinition(weaverTypes.Import<bool>());
serialize.Body.Variables.Add(dirtyLocal);
// bool result = base.SerializeSyncVars(writer, forceAll);
MethodReference baseSerialize = Resolvers.TryResolveMethodInParents(netBehaviourSubclass.BaseType, assembly, SerializeMethodName); MethodReference baseSerialize = Resolvers.TryResolveMethodInParents(netBehaviourSubclass.BaseType, assembly, SerializeMethodName);
if (baseSerialize != null) if (baseSerialize != null)
{ {
@ -420,8 +416,6 @@ void GenerateSerialization(ref bool WeavingFailed)
// forceAll // forceAll
worker.Emit(OpCodes.Ldarg_2); worker.Emit(OpCodes.Ldarg_2);
worker.Emit(OpCodes.Call, baseSerialize); worker.Emit(OpCodes.Call, baseSerialize);
// set dirtyLocal to result of base.OnSerialize()
worker.Emit(OpCodes.Stloc_0);
} }
// Generates: // Generates:
@ -429,7 +423,6 @@ void GenerateSerialization(ref bool WeavingFailed)
// { // {
// writer.WriteInt(health); // writer.WriteInt(health);
// ... // ...
// return true;
// } // }
Instruction initialStateLabel = worker.Create(OpCodes.Nop); Instruction initialStateLabel = worker.Create(OpCodes.Nop);
// forceAll // forceAll
@ -463,8 +456,7 @@ void GenerateSerialization(ref bool WeavingFailed)
} }
} }
// if (forceAll) then always return true at the end of the 'if' case // if (forceAll) then always return at the end of the 'if' case
worker.Emit(OpCodes.Ldc_I4_1);
worker.Emit(OpCodes.Ret); worker.Emit(OpCodes.Ret);
// end the 'if' case for "if (forceAll)" // end the 'if' case for "if (forceAll)"
@ -488,7 +480,6 @@ void GenerateSerialization(ref bool WeavingFailed)
int dirtyBit = syncVarAccessLists.GetSyncVarStart(netBehaviourSubclass.BaseType.FullName); int dirtyBit = syncVarAccessLists.GetSyncVarStart(netBehaviourSubclass.BaseType.FullName);
foreach (FieldDefinition syncVarDef in syncVars) foreach (FieldDefinition syncVarDef in syncVars)
{ {
FieldReference syncVar = syncVarDef; FieldReference syncVar = syncVarDef;
if (netBehaviourSubclass.HasGenericParameters) if (netBehaviourSubclass.HasGenericParameters)
{ {
@ -524,11 +515,6 @@ void GenerateSerialization(ref bool WeavingFailed)
return; return;
} }
// something was dirty
worker.Emit(OpCodes.Ldc_I4_1);
// set dirtyLocal to true
worker.Emit(OpCodes.Stloc_0);
worker.Append(varLabel); worker.Append(varLabel);
dirtyBit += 1; dirtyBit += 1;
} }
@ -537,8 +523,7 @@ void GenerateSerialization(ref bool WeavingFailed)
//worker.Emit(OpCodes.Ldstr, $"Injected Serialize {netBehaviourSubclass.Name}"); //worker.Emit(OpCodes.Ldstr, $"Injected Serialize {netBehaviourSubclass.Name}");
//worker.Emit(OpCodes.Call, WeaverTypes.logErrorReference); //worker.Emit(OpCodes.Call, WeaverTypes.logErrorReference);
// generate: return dirtyLocal // generate: return
worker.Emit(OpCodes.Ldloc_0);
worker.Emit(OpCodes.Ret); worker.Emit(OpCodes.Ret);
netBehaviourSubclass.Methods.Add(serialize); netBehaviourSubclass.Methods.Add(serialize);
} }