mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
Don't generate OnSerialize/OnDeserialize if not needed (#218)
This commit is contained in:
parent
3f0d3af349
commit
f8ff8969f5
@ -349,6 +349,12 @@ void GenerateSerialization()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_SyncVars.Count == 0)
|
||||||
|
{
|
||||||
|
// no synvars, no need for custom OnSerialize
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
MethodDefinition serialize = new MethodDefinition("OnSerialize", MethodAttributes.Public |
|
MethodDefinition serialize = new MethodDefinition("OnSerialize", MethodAttributes.Public |
|
||||||
MethodAttributes.Virtual |
|
MethodAttributes.Virtual |
|
||||||
MethodAttributes.HideBySig,
|
MethodAttributes.HideBySig,
|
||||||
@ -364,7 +370,7 @@ void GenerateSerialization()
|
|||||||
VariableDefinition dirtyLocal = new VariableDefinition(Weaver.boolType);
|
VariableDefinition dirtyLocal = new VariableDefinition(Weaver.boolType);
|
||||||
serialize.Body.Variables.Add(dirtyLocal);
|
serialize.Body.Variables.Add(dirtyLocal);
|
||||||
|
|
||||||
MethodReference baseSerialize = Weaver.ResolveMethod(m_td.BaseType, "OnSerialize");
|
MethodReference baseSerialize = Weaver.ResolveMethodInParents(m_td.BaseType, "OnSerialize");
|
||||||
if (baseSerialize != null)
|
if (baseSerialize != null)
|
||||||
{
|
{
|
||||||
serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); // base
|
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()
|
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);
|
// Generates: if (forceAll);
|
||||||
Instruction initialStateLabel = serWorker.Create(OpCodes.Nop);
|
Instruction initialStateLabel = serWorker.Create(OpCodes.Nop);
|
||||||
serWorker.Append(serWorker.Create(OpCodes.Ldarg_2)); // forceAll
|
serWorker.Append(serWorker.Create(OpCodes.Ldarg_2)); // forceAll
|
||||||
@ -555,6 +552,12 @@ void GenerateDeSerialization()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_SyncVars.Count == 0)
|
||||||
|
{
|
||||||
|
// no synvars, no need for custom OnDeserialize
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
MethodDefinition serialize = new MethodDefinition("OnDeserialize", MethodAttributes.Public |
|
MethodDefinition serialize = new MethodDefinition("OnDeserialize", MethodAttributes.Public |
|
||||||
MethodAttributes.Virtual |
|
MethodAttributes.Virtual |
|
||||||
MethodAttributes.HideBySig,
|
MethodAttributes.HideBySig,
|
||||||
@ -564,7 +567,7 @@ void GenerateDeSerialization()
|
|||||||
serialize.Parameters.Add(new ParameterDefinition("initialState", ParameterAttributes.None, Weaver.boolType));
|
serialize.Parameters.Add(new ParameterDefinition("initialState", ParameterAttributes.None, Weaver.boolType));
|
||||||
ILProcessor serWorker = serialize.Body.GetILProcessor();
|
ILProcessor serWorker = serialize.Body.GetILProcessor();
|
||||||
|
|
||||||
MethodReference baseDeserialize = Weaver.ResolveMethod(m_td.BaseType, "OnDeserialize");
|
MethodReference baseDeserialize = Weaver.ResolveMethodInParents(m_td.BaseType, "OnDeserialize");
|
||||||
if (baseDeserialize != null)
|
if (baseDeserialize != null)
|
||||||
{
|
{
|
||||||
serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); // base
|
serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); // base
|
||||||
@ -573,13 +576,6 @@ void GenerateDeSerialization()
|
|||||||
serWorker.Append(serWorker.Create(OpCodes.Call, baseDeserialize));
|
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);
|
// Generates: if (initialState);
|
||||||
Instruction initialStateLabel = serWorker.Create(OpCodes.Nop);
|
Instruction initialStateLabel = serWorker.Create(OpCodes.Nop);
|
||||||
|
|
||||||
|
@ -1033,6 +1033,24 @@ static bool ProcessNetworkBehaviourType(TypeDefinition td)
|
|||||||
}
|
}
|
||||||
return false;
|
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)
|
public static MethodReference ResolveMethod(TypeReference t, string name)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user