Weaver SyncList code moved into SyncListProcessor

This commit is contained in:
vis2k 2019-01-01 18:45:48 +01:00
parent b9ec4a5dc5
commit 5ecb40f055
3 changed files with 39 additions and 28 deletions

View File

@ -72,6 +72,7 @@
<Compile Include="Processors\NetworkBehaviourProcessor.cs" /> <Compile Include="Processors\NetworkBehaviourProcessor.cs" />
<Compile Include="Processors\RpcProcessor.cs" /> <Compile Include="Processors\RpcProcessor.cs" />
<Compile Include="Processors\SyncEventProcessor.cs" /> <Compile Include="Processors\SyncEventProcessor.cs" />
<Compile Include="Processors\SyncListProcessor.cs" />
<Compile Include="Processors\SyncObjectProcessor.cs" /> <Compile Include="Processors\SyncObjectProcessor.cs" />
<Compile Include="Processors\SyncVarProcessor.cs" /> <Compile Include="Processors\SyncVarProcessor.cs" />
<Compile Include="Processors\TargetRpcProcessor.cs" /> <Compile Include="Processors\TargetRpcProcessor.cs" />

View File

@ -266,7 +266,7 @@ void GenerateConstants()
foreach (FieldDefinition fd in m_SyncObjects) foreach (FieldDefinition fd in m_SyncObjects)
{ {
GenerateSyncListInstanceInitializer(ctorWorker, fd); SyncListProcessor.GenerateSyncListInstanceInitializer(ctorWorker, fd);
SyncObjectProcessor.GenerateSyncObjectInitializer(ctorWorker, fd); SyncObjectProcessor.GenerateSyncObjectInitializer(ctorWorker, fd);
} }
@ -283,33 +283,6 @@ void GenerateConstants()
m_td.Attributes = m_td.Attributes & ~TypeAttributes.BeforeFieldInit; m_td.Attributes = m_td.Attributes & ~TypeAttributes.BeforeFieldInit;
} }
// generates 'syncListInt = new SyncListInt()' if user didn't do that yet
void GenerateSyncListInstanceInitializer(ILProcessor ctorWorker, FieldDefinition fd)
{
// check the ctor's instructions for an Stfld op-code for this specific sync list field.
foreach (var ins in ctorWorker.Body.Instructions)
{
if (ins.OpCode.Code == Code.Stfld)
{
var field = (FieldDefinition)ins.Operand;
if (field.DeclaringType == fd.DeclaringType && field.Name == fd.Name)
{
// Already initialized by the user in the field definition, e.g:
// public SyncListInt Foo = new SyncListInt();
return;
}
}
}
// Not initialized by the user in the field definition, e.g:
// public SyncListInt Foo;
var listCtor = Weaver.scriptDef.MainModule.ImportReference(fd.FieldType.Resolve().Methods.First<MethodDefinition>(x => x.Name == ".ctor" && !x.HasParameters));
ctorWorker.Append(ctorWorker.Create(OpCodes.Ldarg_0));
ctorWorker.Append(ctorWorker.Create(OpCodes.Newobj, listCtor));
ctorWorker.Append(ctorWorker.Create(OpCodes.Stfld, fd));
}
/* /*
// This generates code like: // This generates code like:
NetworkBehaviour.RegisterCommandDelegate(base.GetType(), "CmdThrust", new NetworkBehaviour.CmdDelegate(ShipControl.InvokeCmdCmdThrust)); NetworkBehaviour.RegisterCommandDelegate(base.GetType(), "CmdThrust", new NetworkBehaviour.CmdDelegate(ShipControl.InvokeCmdCmdThrust));

View File

@ -0,0 +1,37 @@
// SyncList code
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace Mirror.Weaver
{
public static class SyncListProcessor
{
// generates 'syncListInt = new SyncListInt()' if user didn't do that yet
public static void GenerateSyncListInstanceInitializer(ILProcessor ctorWorker, FieldDefinition fd)
{
// check the ctor's instructions for an Stfld op-code for this specific sync list field.
foreach (var ins in ctorWorker.Body.Instructions)
{
if (ins.OpCode.Code == Code.Stfld)
{
var field = (FieldDefinition)ins.Operand;
if (field.DeclaringType == fd.DeclaringType && field.Name == fd.Name)
{
// Already initialized by the user in the field definition, e.g:
// public SyncListInt Foo = new SyncListInt();
return;
}
}
}
// Not initialized by the user in the field definition, e.g:
// public SyncListInt Foo;
var listCtor = Weaver.scriptDef.MainModule.ImportReference(fd.FieldType.Resolve().Methods.First<MethodDefinition>(x => x.Name == ".ctor" && !x.HasParameters));
ctorWorker.Append(ctorWorker.Create(OpCodes.Ldarg_0));
ctorWorker.Append(ctorWorker.Create(OpCodes.Newobj, listCtor));
ctorWorker.Append(ctorWorker.Create(OpCodes.Stfld, fd));
}
}
}