diff --git a/Mirror/Weaver/Processors/NetworkBehaviourProcessor.cs b/Mirror/Weaver/Processors/NetworkBehaviourProcessor.cs index efcd7477d..b091a9f27 100644 --- a/Mirror/Weaver/Processors/NetworkBehaviourProcessor.cs +++ b/Mirror/Weaver/Processors/NetworkBehaviourProcessor.cs @@ -349,6 +349,12 @@ void GenerateSerialization() return; } + if (m_SyncVars.Count == 0) + { + // no synvars, no need for custom OnSerialize + return; + } + MethodDefinition serialize = new MethodDefinition("OnSerialize", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, @@ -364,7 +370,7 @@ void GenerateSerialization() VariableDefinition dirtyLocal = new VariableDefinition(Weaver.boolType); serialize.Body.Variables.Add(dirtyLocal); - MethodReference baseSerialize = Weaver.ResolveMethod(m_td.BaseType, "OnSerialize"); + MethodReference baseSerialize = Weaver.ResolveMethodInParents(m_td.BaseType, "OnSerialize"); if (baseSerialize != null) { serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); // base @@ -374,15 +380,6 @@ void GenerateSerialization() serWorker.Append(serWorker.Create(OpCodes.Stloc_0)); // set dirtyLocal to result of base.OnSerialize() } - if (m_SyncVars.Count == 0) - { - // generate: return dirtyLocal - serWorker.Append(serWorker.Create(OpCodes.Ldloc_0)); - serWorker.Append(serWorker.Create(OpCodes.Ret)); - m_td.Methods.Add(serialize); - return; - } - // Generates: if (forceAll); Instruction initialStateLabel = serWorker.Create(OpCodes.Nop); serWorker.Append(serWorker.Create(OpCodes.Ldarg_2)); // forceAll @@ -555,6 +552,12 @@ void GenerateDeSerialization() return; } + if (m_SyncVars.Count == 0) + { + // no synvars, no need for custom OnDeserialize + return; + } + MethodDefinition serialize = new MethodDefinition("OnDeserialize", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, @@ -564,7 +567,7 @@ void GenerateDeSerialization() serialize.Parameters.Add(new ParameterDefinition("initialState", ParameterAttributes.None, Weaver.boolType)); ILProcessor serWorker = serialize.Body.GetILProcessor(); - MethodReference baseDeserialize = Weaver.ResolveMethod(m_td.BaseType, "OnDeserialize"); + MethodReference baseDeserialize = Weaver.ResolveMethodInParents(m_td.BaseType, "OnDeserialize"); if (baseDeserialize != null) { serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); // base @@ -573,13 +576,6 @@ void GenerateDeSerialization() serWorker.Append(serWorker.Create(OpCodes.Call, baseDeserialize)); } - if (m_SyncVars.Count == 0) - { - serWorker.Append(serWorker.Create(OpCodes.Ret)); - m_td.Methods.Add(serialize); - return; - } - // Generates: if (initialState); Instruction initialStateLabel = serWorker.Create(OpCodes.Nop); diff --git a/Mirror/Weaver/Weaver.cs b/Mirror/Weaver/Weaver.cs index 33e96ac3e..5260a508a 100644 --- a/Mirror/Weaver/Weaver.cs +++ b/Mirror/Weaver/Weaver.cs @@ -1033,6 +1033,24 @@ static bool ProcessNetworkBehaviourType(TypeDefinition td) } return false; } + public static MethodReference ResolveMethodInParents(TypeReference t, string name) + { + if (t == null) + { + Log.Error("Type missing for " + name); + fail = true; + return null; + } + foreach (var methodRef in t.Resolve().Methods) + { + if (methodRef.Name == name) + { + return scriptDef.MainModule.ImportReference(methodRef); + } + } + // Could not find the method in this class, try the parent + return ResolveMethodInParents(t.Resolve().BaseType, name); + } public static MethodReference ResolveMethod(TypeReference t, string name) {