moving WeaverLists functions to WeaverLists (#2285)

The plan is to remove WeaverLists at some point so moving the functions
out of weaver to here for now and then move them closer to where they
are actaully used when we start to remove WeaverLists.
This commit is contained in:
James Frowen 2020-09-28 20:21:30 +01:00 committed by GitHub
parent 694de7255b
commit 796aa3baf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 35 deletions

View File

@ -442,7 +442,7 @@ void GenerateSerialization()
// generate a writer call for any dirty variable in this class // generate a writer call for any dirty variable in this class
// start at number of syncvars in parent // start at number of syncvars in parent
int dirtyBit = Weaver.GetSyncVarStart(netBehaviourSubclass.BaseType.FullName); int dirtyBit = Weaver.WeaveLists.GetSyncVarStart(netBehaviourSubclass.BaseType.FullName);
foreach (FieldDefinition syncVar in syncVars) foreach (FieldDefinition syncVar in syncVars)
{ {
Instruction varLabel = worker.Create(OpCodes.Nop); Instruction varLabel = worker.Create(OpCodes.Nop);
@ -775,7 +775,7 @@ void GenerateDeSerialization()
// conditionally read each syncvar // conditionally read each syncvar
// start at number of syncvars in parent // start at number of syncvars in parent
int dirtyBit = Weaver.GetSyncVarStart(netBehaviourSubclass.BaseType.FullName); int dirtyBit = Weaver.WeaveLists.GetSyncVarStart(netBehaviourSubclass.BaseType.FullName);
foreach (FieldDefinition syncVar in syncVars) foreach (FieldDefinition syncVar in syncVars)
{ {
Instruction varLabel = serWorker.Create(OpCodes.Nop); Instruction varLabel = serWorker.Create(OpCodes.Nop);

View File

@ -311,7 +311,7 @@ public static (List<FieldDefinition> syncVars, Dictionary<FieldDefinition, Field
// the mapping of dirtybits to sync-vars is implicit in the order of the fields here. this order is recorded in m_replacementProperties. // the mapping of dirtybits to sync-vars is implicit in the order of the fields here. this order is recorded in m_replacementProperties.
// start assigning syncvars at the place the base class stopped, if any // start assigning syncvars at the place the base class stopped, if any
int dirtyBitCounter = Weaver.GetSyncVarStart(td.BaseType.FullName); int dirtyBitCounter = Weaver.WeaveLists.GetSyncVarStart(td.BaseType.FullName);
// find syncvars // find syncvars
foreach (FieldDefinition fd in td.Fields) foreach (FieldDefinition fd in td.Fields)
@ -355,7 +355,7 @@ public static (List<FieldDefinition> syncVars, Dictionary<FieldDefinition, Field
{ {
td.Fields.Add(fd); td.Fields.Add(fd);
} }
Weaver.SetNumSyncVars(td.FullName, syncVars.Count); Weaver.WeaveLists.SetNumSyncVars(td.FullName, syncVars.Count);
return (syncVars, syncVarNetIds); return (syncVars, syncVarNetIds);
} }

View File

@ -118,7 +118,7 @@ static void RegisterReadFunc(string name, MethodDefinition newReaderFunc)
readFuncs[name] = newReaderFunc; readFuncs[name] = newReaderFunc;
Weaver.WeaveLists.generatedReadFunctions.Add(newReaderFunc); Weaver.WeaveLists.generatedReadFunctions.Add(newReaderFunc);
Weaver.ConfirmGeneratedCodeClass(); Weaver.WeaveLists.ConfirmGeneratedCodeClass();
Weaver.WeaveLists.generateContainerClass.Methods.Add(newReaderFunc); Weaver.WeaveLists.generateContainerClass.Methods.Add(newReaderFunc);
} }

View File

@ -23,6 +23,37 @@ class WeaverLists
public Dictionary<string, int> numSyncVars = new Dictionary<string, int>(); public Dictionary<string, int> numSyncVars = new Dictionary<string, int>();
public HashSet<string> ProcessedMessages = new HashSet<string>(); public HashSet<string> ProcessedMessages = new HashSet<string>();
public int GetSyncVarStart(string className)
{
return numSyncVars.ContainsKey(className)
? numSyncVars[className]
: 0;
}
public void SetNumSyncVars(string className, int num)
{
numSyncVars[className] = num;
}
public void ConfirmGeneratedCodeClass()
{
if (generateContainerClass == null)
{
generateContainerClass = new TypeDefinition("Mirror", "GeneratedNetworkCode",
TypeAttributes.BeforeFieldInit | TypeAttributes.Class | TypeAttributes.AnsiClass | TypeAttributes.Public | TypeAttributes.AutoClass,
WeaverTypes.Import<object>());
const MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
MethodDefinition method = new MethodDefinition(".ctor", methodAttributes, WeaverTypes.Import(typeof(void)));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, Resolvers.ResolveMethod(WeaverTypes.Import<object>(), Weaver.CurrentAssembly, ".ctor")));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
generateContainerClass.Methods.Add(method);
}
}
} }
internal static class Weaver internal static class Weaver
@ -64,35 +95,6 @@ public static void Warning(string message, MemberReference mr)
Log.Warning($"{message} (at {mr})"); Log.Warning($"{message} (at {mr})");
} }
public static int GetSyncVarStart(string className)
{
return WeaveLists.numSyncVars.ContainsKey(className)
? WeaveLists.numSyncVars[className]
: 0;
}
public static void SetNumSyncVars(string className, int num)
{
WeaveLists.numSyncVars[className] = num;
}
internal static void ConfirmGeneratedCodeClass()
{
if (WeaveLists.generateContainerClass == null)
{
WeaveLists.generateContainerClass = new TypeDefinition("Mirror", "GeneratedNetworkCode",
TypeAttributes.BeforeFieldInit | TypeAttributes.Class | TypeAttributes.AnsiClass | TypeAttributes.Public | TypeAttributes.AutoClass,
WeaverTypes.Import<object>());
const MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
MethodDefinition method = new MethodDefinition(".ctor", methodAttributes, WeaverTypes.Import(typeof(void)));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, Resolvers.ResolveMethod(WeaverTypes.Import<object>(), CurrentAssembly, ".ctor")));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
WeaveLists.generateContainerClass.Methods.Add(method);
}
}
static void CheckMonoBehaviour(TypeDefinition td) static void CheckMonoBehaviour(TypeDefinition td)
{ {

View File

@ -27,7 +27,7 @@ static void RegisterWriteFunc(string name, MethodDefinition newWriterFunc)
writeFuncs[name] = newWriterFunc; writeFuncs[name] = newWriterFunc;
Weaver.WeaveLists.generatedWriteFunctions.Add(newWriterFunc); Weaver.WeaveLists.generatedWriteFunctions.Add(newWriterFunc);
Weaver.ConfirmGeneratedCodeClass(); Weaver.WeaveLists.ConfirmGeneratedCodeClass();
Weaver.WeaveLists.generateContainerClass.Methods.Add(newWriterFunc); Weaver.WeaveLists.generateContainerClass.Methods.Add(newWriterFunc);
} }