Don't generate OnSerialize/OnDeserialize if not needed (#218)

This commit is contained in:
Paul Pacheco 2019-01-01 11:21:55 -06:00 committed by vis2k
parent 3f0d3af349
commit f8ff8969f5
2 changed files with 32 additions and 18 deletions

View File

@ -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);

View File

@ -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)
{