From 50df4d1b1f47cab89fe718e5fc5655015d6c826d Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Tue, 31 Jul 2018 23:47:49 -0500 Subject: [PATCH] Serialize initial message by setting all dirty flags --- .../Runtime/NetworkBehaviour.cs | 6 ++ .../Runtime/NetworkIdentity.cs | 4 + .../Weaver/UNetBehaviourProcessor.cs | 90 ------------------- 3 files changed, 10 insertions(+), 90 deletions(-) diff --git a/Unity-Technologies-networking/Runtime/NetworkBehaviour.cs b/Unity-Technologies-networking/Runtime/NetworkBehaviour.cs index b17296f52..34dc8623b 100644 --- a/Unity-Technologies-networking/Runtime/NetworkBehaviour.cs +++ b/Unity-Technologies-networking/Runtime/NetworkBehaviour.cs @@ -548,6 +548,12 @@ public void ClearAllDirtyBits() m_SyncVarDirtyBits = 0L; } + public void SetAllDirtyBits() + { + m_LastSendTime = float.MinValue; + m_SyncVarDirtyBits = ~0uL; + } + internal bool IsDirty() { return diff --git a/Unity-Technologies-networking/Runtime/NetworkIdentity.cs b/Unity-Technologies-networking/Runtime/NetworkIdentity.cs index d69cf357e..a205df1de 100644 --- a/Unity-Technologies-networking/Runtime/NetworkIdentity.cs +++ b/Unity-Technologies-networking/Runtime/NetworkIdentity.cs @@ -467,6 +467,10 @@ internal bool OnSerializeAllSafely(NetworkBehaviour[] components, NetworkWriter // set bit #i to 1 in dirty mask dirtyComponentsMask |= (ulong)(1L << i); + if (initialState) + { + comp.SetAllDirtyBits(); + } // serialize and clear dirty bits in any case, since the component was clearly dirty if we got here if (LogFilter.logDebug) { Debug.Log("OnSerializeAllSafely: " + name + " -> " + comp.GetType() + " initial=" + initialState + " channelId=" + channelId); } OnSerializeSafely(comp, payload, initialState); diff --git a/Unity-Technologies-networking/Weaver/UNetBehaviourProcessor.cs b/Unity-Technologies-networking/Weaver/UNetBehaviourProcessor.cs index faf5f681c..6fc969601 100644 --- a/Unity-Technologies-networking/Weaver/UNetBehaviourProcessor.cs +++ b/Unity-Technologies-networking/Weaver/UNetBehaviourProcessor.cs @@ -514,7 +514,6 @@ void GenerateSerialization() serialize.Body.Variables.Add(dirtyLocal); // call base class - bool baseClassSerialize = false; if (m_td.BaseType.FullName != Weaver.NetworkBehaviourType.FullName) { MethodReference baseSerialize = Weaver.ResolveMethod(m_td.BaseType, "OnSerialize"); @@ -525,7 +524,6 @@ void GenerateSerialization() serWorker.Append(serWorker.Create(OpCodes.Ldarg_2)); // forceAll serWorker.Append(serWorker.Create(OpCodes.Call, baseSerialize)); serWorker.Append(serWorker.Create(OpCodes.Stloc_0)); // set dirtyLocal to result of base.OnSerialize() - baseClassSerialize = true; } } @@ -539,39 +537,6 @@ void GenerateSerialization() return; } - // Generates: if (forceAll); - Instruction initialStateLabel = serWorker.Create(OpCodes.Nop); - serWorker.Append(serWorker.Create(OpCodes.Ldarg_2)); // forceAll - serWorker.Append(serWorker.Create(OpCodes.Brfalse, initialStateLabel)); - - foreach (FieldDefinition syncVar in m_SyncVars) - { - // Generates a writer call for each sync variable - serWorker.Append(serWorker.Create(OpCodes.Ldarg_1)); // writer - serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); // this - serWorker.Append(serWorker.Create(OpCodes.Ldfld, syncVar)); - MethodReference writeFunc = Weaver.GetWriteFunc(syncVar.FieldType); - if (writeFunc != null) - { - serWorker.Append(serWorker.Create(OpCodes.Call, writeFunc)); - } - else - { - Weaver.fail = true; - Log.Error("GenerateSerialization for " + m_td.Name + " unknown type [" + syncVar.FieldType + "]. UNet [SyncVar] member variables must be basic types."); - return; - } - } - - // always return true if forceAll - - // Generates: return true - serWorker.Append(serWorker.Create(OpCodes.Ldc_I4_1)); - serWorker.Append(serWorker.Create(OpCodes.Ret)); - - // Generates: end if (forceAll); - serWorker.Append(initialStateLabel); - // write dirty bits before the data fields // Generates: writer.WritePackedUInt64 (base.get_syncVarDirtyBits ()); serWorker.Append(serWorker.Create(OpCodes.Ldarg_1)); // writer @@ -869,61 +834,6 @@ void GenerateDeSerialization() return; } - // Generates: if (initialState); - Instruction initialStateLabel = serWorker.Create(OpCodes.Nop); - - serWorker.Append(serWorker.Create(OpCodes.Ldarg_2)); - serWorker.Append(serWorker.Create(OpCodes.Brfalse, initialStateLabel)); - - foreach (var syncVar in m_SyncVars) - { - MethodReference readByReferenceFunc = Weaver.GetReadByReferenceFunc(syncVar.FieldType); - if (readByReferenceFunc != null) - { - serWorker.Append(serWorker.Create(OpCodes.Ldarg_1)); - serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); - serWorker.Append(serWorker.Create(OpCodes.Ldfld, syncVar)); - serWorker.Append(serWorker.Create(OpCodes.Call, readByReferenceFunc)); - } - else - { - // assign value - serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); - serWorker.Append(serWorker.Create(OpCodes.Ldarg_1)); - - if (syncVar.FieldType.FullName == Weaver.gameObjectType.FullName) - { - // GameObject SyncVar - assign to generated netId var - FieldDefinition netIdField = m_SyncVarNetIds[m_NetIdFieldCounter]; - m_NetIdFieldCounter += 1; - - serWorker.Append(serWorker.Create(OpCodes.Callvirt, Weaver.NetworkReaderReadNetworkInstanceId)); - serWorker.Append(serWorker.Create(OpCodes.Stfld, netIdField)); - } - else - { - MethodReference readFunc = Weaver.GetReadFunc(syncVar.FieldType); - if (readFunc != null) - { - serWorker.Append(serWorker.Create(OpCodes.Call, readFunc)); - } - else - { - Log.Error("GenerateDeSerialization for " + m_td.Name + " unknown type [" + syncVar.FieldType + "]. UNet [SyncVar] member variables must be basic types."); - Weaver.fail = true; - return; - } - serWorker.Append(serWorker.Create(OpCodes.Stfld, syncVar)); - } - } - } - - serWorker.Append(serWorker.Create(OpCodes.Ret)); - - // Generates: end if (initialState); - serWorker.Append(initialStateLabel); - - // setup local for dirty bits serialize.Body.InitLocals = true; VariableDefinition dirtyBitsLocal = new VariableDefinition(Weaver.int64Type);