SyncVarProcessor.syncVarNetIds converted to Dictionary for easier usage and to avoid the netIdFieldCounters

This commit is contained in:
vis2k 2019-01-15 15:54:45 +01:00
parent 70048fc380
commit 57b848cc34
2 changed files with 8 additions and 11 deletions

View File

@ -11,7 +11,7 @@ class NetworkBehaviourProcessor
{ {
readonly List<FieldDefinition> m_SyncVars = new List<FieldDefinition>(); readonly List<FieldDefinition> m_SyncVars = new List<FieldDefinition>();
readonly List<FieldDefinition> m_SyncObjects = new List<FieldDefinition>(); readonly List<FieldDefinition> m_SyncObjects = new List<FieldDefinition>();
readonly List<FieldDefinition> m_SyncVarNetIds = new List<FieldDefinition>(); readonly Dictionary<FieldDefinition, FieldDefinition> m_SyncVarNetIds = new Dictionary<FieldDefinition, FieldDefinition>(); // <SyncVarField,NetIdField>
readonly List<MethodDefinition> m_Cmds = new List<MethodDefinition>(); readonly List<MethodDefinition> m_Cmds = new List<MethodDefinition>();
readonly List<MethodDefinition> m_Rpcs = new List<MethodDefinition>(); readonly List<MethodDefinition> m_Rpcs = new List<MethodDefinition>();
readonly List<MethodDefinition> m_TargetRpcs = new List<MethodDefinition>(); readonly List<MethodDefinition> m_TargetRpcs = new List<MethodDefinition>();
@ -486,7 +486,6 @@ void GenerateDeSerialization()
serWorker.Append(serWorker.Create(OpCodes.Ldarg_2)); serWorker.Append(serWorker.Create(OpCodes.Ldarg_2));
serWorker.Append(serWorker.Create(OpCodes.Brfalse, initialStateLabel)); serWorker.Append(serWorker.Create(OpCodes.Brfalse, initialStateLabel));
int netIdFieldCounter = 0;
foreach (FieldDefinition syncVar in m_SyncVars) foreach (FieldDefinition syncVar in m_SyncVars)
{ {
// assign value // assign value
@ -501,8 +500,7 @@ void GenerateDeSerialization()
// OnDeserialize reads to __netId manually so we can use // OnDeserialize reads to __netId manually so we can use
// lookups in the getter (so it still works if objects // lookups in the getter (so it still works if objects
// move in and out of range repeatedly) // move in and out of range repeatedly)
FieldDefinition netIdField = m_SyncVarNetIds[netIdFieldCounter]; FieldDefinition netIdField = m_SyncVarNetIds[syncVar];
netIdFieldCounter += 1;
serWorker.Append(serWorker.Create(OpCodes.Callvirt, Weaver.NetworkReaderReadPacked32)); serWorker.Append(serWorker.Create(OpCodes.Callvirt, Weaver.NetworkReaderReadPacked32));
serWorker.Append(serWorker.Create(OpCodes.Stfld, netIdField)); serWorker.Append(serWorker.Create(OpCodes.Stfld, netIdField));
@ -540,7 +538,6 @@ void GenerateDeSerialization()
serWorker.Append(serWorker.Create(OpCodes.Stloc_0)); serWorker.Append(serWorker.Create(OpCodes.Stloc_0));
// conditionally read each syncvar // conditionally read each syncvar
netIdFieldCounter = 0; // reset
int dirtyBit = Weaver.GetSyncVarStart(m_td.BaseType.FullName); // start at number of syncvars in parent int dirtyBit = Weaver.GetSyncVarStart(m_td.BaseType.FullName); // start at number of syncvars in parent
foreach (FieldDefinition syncVar in m_SyncVars) foreach (FieldDefinition syncVar in m_SyncVars)
{ {
@ -567,8 +564,7 @@ void GenerateDeSerialization()
// OnDeserialize reads to __netId manually so we can use // OnDeserialize reads to __netId manually so we can use
// lookups in the getter (so it still works if objects // lookups in the getter (so it still works if objects
// move in and out of range repeatedly) // move in and out of range repeatedly)
FieldDefinition netIdField = m_SyncVarNetIds[netIdFieldCounter]; FieldDefinition netIdField = m_SyncVarNetIds[syncVar];
netIdFieldCounter += 1;
if (foundMethod == null) if (foundMethod == null)
{ {

View File

@ -191,7 +191,7 @@ public static MethodDefinition ProcessSyncVarSet(TypeDefinition td, FieldDefinit
return set; return set;
} }
public static void ProcessSyncVar(TypeDefinition td, FieldDefinition fd, List<FieldDefinition> syncVarNetIds, long dirtyBit) public static void ProcessSyncVar(TypeDefinition td, FieldDefinition fd, Dictionary<FieldDefinition, FieldDefinition> syncVarNetIds, long dirtyBit)
{ {
string originalName = fd.Name; string originalName = fd.Name;
Weaver.DLog(td, "Sync Var " + fd.Name + " " + fd.FieldType + " " + Weaver.gameObjectType); Weaver.DLog(td, "Sync Var " + fd.Name + " " + fd.FieldType + " " + Weaver.gameObjectType);
@ -205,7 +205,7 @@ public static void ProcessSyncVar(TypeDefinition td, FieldDefinition fd, List<Fi
FieldAttributes.Private, FieldAttributes.Private,
Weaver.uint32Type); Weaver.uint32Type);
syncVarNetIds.Add(netFieldId); syncVarNetIds[fd] = netFieldId;
Weaver.lists.netIdFields.Add(netFieldId); Weaver.lists.netIdFields.Add(netFieldId);
} }
@ -236,7 +236,7 @@ public static void ProcessSyncVar(TypeDefinition td, FieldDefinition fd, List<Fi
} }
} }
public static void ProcessSyncVars(TypeDefinition td, List<FieldDefinition> syncVars, List<FieldDefinition> syncObjects, List<FieldDefinition> syncVarNetIds) public static void ProcessSyncVars(TypeDefinition td, List<FieldDefinition> syncVars, List<FieldDefinition> syncObjects, Dictionary<FieldDefinition, FieldDefinition> syncVarNetIds)
{ {
int numSyncVars = 0; int numSyncVars = 0;
@ -353,7 +353,8 @@ public static void ProcessSyncVars(TypeDefinition td, List<FieldDefinition> sync
} }
} }
foreach (FieldDefinition fd in syncVarNetIds) // add all the new SyncVar __netId fields
foreach (FieldDefinition fd in syncVarNetIds.Values)
{ {
td.Fields.Add(fd); td.Fields.Add(fd);
} }