public void Process() { Weaver.DLog(this.m_td, "MessageClassProcessor Start", new object[0]); Weaver.ResetRecursionCount(); this.GenerateSerialization(); if (Weaver.fail) { return; } this.GenerateDeSerialization(); Weaver.DLog(this.m_td, "MessageClassProcessor Done", new object[0]); }
private void GenerateSerialization() { Weaver.DLog(this.m_td, " GenerateSerialization", new object[0]); foreach (MethodDefinition definition in this.m_td.Methods) { if (definition.Name == "Serialize") { return; } } if (this.m_td.Fields.Count != 0) { MethodDefinition item = new MethodDefinition("Serialize", MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Virtual, Weaver.voidType) { Parameters = { new ParameterDefinition("writer", ParameterAttributes.None, Weaver.scriptDef.MainModule.ImportReference(Weaver.NetworkWriterType)) } }; ILProcessor iLProcessor = item.Body.GetILProcessor(); foreach (FieldDefinition definition3 in this.m_td.Fields) { if ((!definition3.IsStatic && !definition3.IsPrivate) && !definition3.IsSpecialName) { if (definition3.FieldType.Resolve().HasGenericParameters) { Weaver.fail = true; Log.Error(string.Concat(new object[] { "GenerateSerialization for ", this.m_td.Name, " [", definition3.FieldType, "/", definition3.FieldType.FullName, "]. UNet [MessageBase] member cannot have generic parameters." })); return; } if (definition3.FieldType.Resolve().IsInterface) { Weaver.fail = true; Log.Error(string.Concat(new object[] { "GenerateSerialization for ", this.m_td.Name, " [", definition3.FieldType, "/", definition3.FieldType.FullName, "]. UNet [MessageBase] member cannot be an interface." })); return; } MethodReference writeFunc = Weaver.GetWriteFunc(definition3.FieldType); if (writeFunc != null) { iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_1)); iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_0)); iLProcessor.Append(iLProcessor.Create(OpCodes.Ldfld, definition3)); iLProcessor.Append(iLProcessor.Create(OpCodes.Call, writeFunc)); } else { Weaver.fail = true; Log.Error(string.Concat(new object[] { "GenerateSerialization for ", this.m_td.Name, " unknown type [", definition3.FieldType, "/", definition3.FieldType.FullName, "]. UNet [MessageBase] member variables must be basic types." })); return; } } } iLProcessor.Append(iLProcessor.Create(OpCodes.Ret)); this.m_td.Methods.Add(item); } }
private MethodReference GenerateDeserialization() { Weaver.DLog(this.m_TypeDef, " GenerateDeserialization", new object[0]); foreach (MethodDefinition current in this.m_TypeDef.Methods) { if (current.Name == "DeserializeItem") { MethodReference result = current; return(result); } } MethodDefinition methodDefinition = new MethodDefinition("DeserializeItem", MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.HideBySig, this.m_ItemType); methodDefinition.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, Weaver.scriptDef.MainModule.Import(Weaver.NetworkReaderType))); ILProcessor iLProcessor = methodDefinition.Body.GetILProcessor(); iLProcessor.Body.InitLocals = true; iLProcessor.Body.Variables.Add(new VariableDefinition("result", this.m_ItemType)); iLProcessor.Append(iLProcessor.Create(OpCodes.Ldloca, 0)); iLProcessor.Append(iLProcessor.Create(OpCodes.Initobj, this.m_ItemType)); foreach (FieldDefinition current2 in this.m_ItemType.Resolve().Fields) { if (!current2.IsStatic && !current2.IsPrivate && !current2.IsSpecialName) { FieldReference fieldReference = Weaver.scriptDef.MainModule.Import(current2); TypeDefinition typeDefinition = fieldReference.FieldType.Resolve(); MethodReference readFunc = Weaver.GetReadFunc(current2.FieldType); if (readFunc == null) { Weaver.fail = true; Log.Error(string.Concat(new object[] { "GenerateDeserialization for ", this.m_TypeDef.Name, " unknown type [", typeDefinition, "]. UNet [SyncVar] member variables must be basic types." })); MethodReference result = null; return(result); } iLProcessor.Append(iLProcessor.Create(OpCodes.Ldloca, 0)); iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_1)); iLProcessor.Append(iLProcessor.Create(OpCodes.Call, readFunc)); iLProcessor.Append(iLProcessor.Create(OpCodes.Stfld, fieldReference)); } } iLProcessor.Append(iLProcessor.Create(OpCodes.Ldloc_0)); iLProcessor.Append(iLProcessor.Create(OpCodes.Ret)); this.m_TypeDef.Methods.Add(methodDefinition); return(methodDefinition); }
void GenerateDeSerialization() { Weaver.DLog(m_td, " GenerateDeserialization"); foreach (var m in m_td.Methods) { if (m.Name == "Deserialize") { return; } } if (m_td.Fields.Count == 0) { return; } MethodDefinition serializeFunc = new MethodDefinition("Deserialize", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, Weaver.voidType); serializeFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, Weaver.scriptDef.MainModule.ImportReference(Weaver.NetworkReaderType))); ILProcessor serWorker = serializeFunc.Body.GetILProcessor(); foreach (var field in m_td.Fields) { if (field.IsStatic || field.IsPrivate || field.IsSpecialName) { continue; } MethodReference readerFunc = Weaver.GetReadFunc(field.FieldType); if (readerFunc != null) { serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); serWorker.Append(serWorker.Create(OpCodes.Ldarg_1)); serWorker.Append(serWorker.Create(OpCodes.Call, readerFunc)); serWorker.Append(serWorker.Create(OpCodes.Stfld, field)); } else { Weaver.fail = true; Log.Error("GenerateDeSerialization for " + m_td.Name + " unknown type [" + field.FieldType + "]. [SyncVar] member variables must be basic types."); return; } } serWorker.Append(serWorker.Create(OpCodes.Ret)); m_td.Methods.Add(serializeFunc); }
public void Process() { Weaver.DLog(m_td, "MessageClassProcessor Start"); Weaver.ResetRecursionCount(); GenerateSerialization(); if (Weaver.fail) { return; } GenerateDeSerialization(); Weaver.DLog(m_td, "MessageClassProcessor Done"); }
private MethodReference GenerateDeserialization() { Weaver.DLog(this.m_TypeDef, " GenerateDeserialization", new object[0]); foreach (MethodDefinition definition in this.m_TypeDef.Methods) { if (definition.Name == "DeserializeItem") { return(definition); } } MethodDefinition item = new MethodDefinition("DeserializeItem", MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Virtual, this.m_ItemType) { Parameters = { new ParameterDefinition("reader", ParameterAttributes.None, Weaver.scriptDef.MainModule.ImportReference(Weaver.NetworkReaderType)) } }; ILProcessor iLProcessor = item.Body.GetILProcessor(); iLProcessor.Body.InitLocals = true; iLProcessor.Body.Variables.Add(new VariableDefinition("result", this.m_ItemType)); iLProcessor.Append(iLProcessor.Create(OpCodes.Ldloca, 0)); iLProcessor.Append(iLProcessor.Create(OpCodes.Initobj, this.m_ItemType)); foreach (FieldDefinition definition3 in this.m_ItemType.Resolve().Fields) { if ((!definition3.IsStatic && !definition3.IsPrivate) && !definition3.IsSpecialName) { FieldReference field = Weaver.scriptDef.MainModule.ImportReference(definition3); TypeDefinition definition4 = field.FieldType.Resolve(); MethodReference readFunc = Weaver.GetReadFunc(definition3.FieldType); if (readFunc != null) { iLProcessor.Append(iLProcessor.Create(OpCodes.Ldloca, 0)); iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_1)); iLProcessor.Append(iLProcessor.Create(OpCodes.Call, readFunc)); iLProcessor.Append(iLProcessor.Create(OpCodes.Stfld, field)); } else { Weaver.fail = true; Log.Error(string.Concat(new object[] { "GenerateDeserialization for ", this.m_TypeDef.Name, " unknown type [", definition4, "]. UNet [SyncVar] member variables must be basic types." })); return(null); } } } iLProcessor.Append(iLProcessor.Create(OpCodes.Ldloc_0)); iLProcessor.Append(iLProcessor.Create(OpCodes.Ret)); this.m_TypeDef.Methods.Add(item); return(item); }
private void GenerateDeSerialization() { Weaver.DLog(this.m_td, " GenerateDeserialization", new object[0]); foreach (MethodDefinition current in this.m_td.Methods) { if (current.Name == "Deserialize") { return; } } if (this.m_td.Fields.Count == 0) { return; } MethodDefinition methodDefinition = new MethodDefinition("Deserialize", MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.HideBySig, Weaver.voidType); methodDefinition.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, Weaver.scriptDef.MainModule.Import(Weaver.NetworkReaderType))); ILProcessor iLProcessor = methodDefinition.Body.GetILProcessor(); foreach (FieldDefinition current2 in this.m_td.Fields) { if (!current2.IsStatic && !current2.IsPrivate && !current2.IsSpecialName) { MethodReference readFunc = Weaver.GetReadFunc(current2.FieldType); if (readFunc == null) { Weaver.fail = true; Log.Error(string.Concat(new object[] { "GenerateDeSerialization for ", this.m_td.Name, " unknown type [", current2.FieldType, "]. UNet [SyncVar] member variables must be basic types." })); return; } iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_0)); iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_1)); iLProcessor.Append(iLProcessor.Create(OpCodes.Call, readFunc)); iLProcessor.Append(iLProcessor.Create(OpCodes.Stfld, current2)); } } iLProcessor.Append(iLProcessor.Create(OpCodes.Ret)); this.m_td.Methods.Add(methodDefinition); }
void GenerateSerialization() { Weaver.DLog(m_td, " GenerateSerialization"); foreach (var m in m_td.Methods) { if (m.Name == "Serialize") { return; } } if (m_td.Fields.Count == 0) { return; } // check for self-referencing types foreach (var field in m_td.Fields) { if (field.FieldType.FullName == m_td.FullName) { Weaver.fail = true; Log.Error("GenerateSerialization for " + m_td.Name + " [" + field.FullName + "]. [MessageBase] member cannot be self referencing."); return; } } MethodDefinition serializeFunc = new MethodDefinition("Serialize", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, Weaver.voidType); serializeFunc.Parameters.Add(new ParameterDefinition("writer", ParameterAttributes.None, Weaver.scriptDef.MainModule.ImportReference(Weaver.NetworkWriterType))); ILProcessor serWorker = serializeFunc.Body.GetILProcessor(); foreach (var field in m_td.Fields) { if (field.IsStatic || field.IsPrivate || field.IsSpecialName) { continue; } if (field.FieldType.Resolve().HasGenericParameters) { Weaver.fail = true; Log.Error("GenerateSerialization for " + m_td.Name + " [" + field.FieldType + "/" + field.FieldType.FullName + "]. [MessageBase] member cannot have generic parameters."); return; } if (field.FieldType.Resolve().IsInterface) { Weaver.fail = true; Log.Error("GenerateSerialization for " + m_td.Name + " [" + field.FieldType + "/" + field.FieldType.FullName + "]. [MessageBase] member cannot be an interface."); return; } MethodReference writeFunc = Weaver.GetWriteFunc(field.FieldType); if (writeFunc != null) { serWorker.Append(serWorker.Create(OpCodes.Ldarg_1)); serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); serWorker.Append(serWorker.Create(OpCodes.Ldfld, field)); serWorker.Append(serWorker.Create(OpCodes.Call, writeFunc)); } else { Weaver.fail = true; Log.Error("GenerateSerialization for " + m_td.Name + " unknown type [" + field.FieldType + "/" + field.FieldType.FullName + "]. [MessageBase] member variables must be basic types."); return; } } serWorker.Append(serWorker.Create(OpCodes.Ret)); m_td.Methods.Add(serializeFunc); }
public MessageClassProcessor(TypeDefinition td) { Weaver.DLog(td, "MessageClassProcessor for " + td.Name); m_td = td; }
public MessageClassProcessor(TypeDefinition td, Weaver weaver) { m_td = td; m_Weaver = weaver; m_Weaver.DLog(td, "MessageClassProcessor for " + td.Name); }
// serialization of individual element MethodReference GenerateSerialization() { Weaver.DLog(m_TypeDef, " GenerateSerialization"); foreach (var m in m_TypeDef.Methods) { if (m.Name == "SerializeItem") { return(m); } } MethodDefinition serializeFunc = new MethodDefinition("SerializeItem", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Public | MethodAttributes.HideBySig, Weaver.voidType); serializeFunc.Parameters.Add(new ParameterDefinition("writer", ParameterAttributes.None, Weaver.scriptDef.MainModule.ImportReference(Weaver.NetworkWriterType))); serializeFunc.Parameters.Add(new ParameterDefinition("item", ParameterAttributes.None, m_ItemType)); ILProcessor serWorker = serializeFunc.Body.GetILProcessor(); if (m_ItemType.IsGenericInstance) { Weaver.fail = true; Log.Error("GenerateSerialization for " + Helpers.PrettyPrintType(m_ItemType) + " failed. Struct passed into SyncListStruct<T> can't have generic parameters"); return(null); } foreach (var field in m_ItemType.Resolve().Fields) { if (field.IsStatic || field.IsPrivate || field.IsSpecialName) { continue; } var importedField = Weaver.scriptDef.MainModule.ImportReference(field); var ft = importedField.FieldType.Resolve(); if (ft.HasGenericParameters) { Weaver.fail = true; Log.Error("GenerateSerialization for " + m_TypeDef.Name + " [" + ft + "/" + ft.FullName + "]. UNet [MessageBase] member cannot have generic parameters."); return(null); } if (ft.IsInterface) { Weaver.fail = true; Log.Error("GenerateSerialization for " + m_TypeDef.Name + " [" + ft + "/" + ft.FullName + "]. UNet [MessageBase] member cannot be an interface."); return(null); } MethodReference writeFunc = Weaver.GetWriteFunc(field.FieldType); if (writeFunc != null) { serWorker.Append(serWorker.Create(OpCodes.Ldarg_1)); serWorker.Append(serWorker.Create(OpCodes.Ldarg_2)); serWorker.Append(serWorker.Create(OpCodes.Ldfld, importedField)); serWorker.Append(serWorker.Create(OpCodes.Call, writeFunc)); } else { Weaver.fail = true; Log.Error("GenerateSerialization for " + m_TypeDef.Name + " unknown type [" + ft + "/" + ft.FullName + "]. UNet [MessageBase] member variables must be basic types."); return(null); } } serWorker.Append(serWorker.Create(OpCodes.Ret)); m_TypeDef.Methods.Add(serializeFunc); return(serializeFunc); }
public SyncListStructProcessor(TypeDefinition typeDef) { Weaver.DLog(typeDef, "SyncListStructProcessor for " + typeDef.Name); m_TypeDef = typeDef; }
public MessageClassProcessor(TypeDefinition td) { Weaver.DLog(td, "MessageClassProcessor for " + td.Name, new object[0]); this.m_td = td; }
public SyncListStructProcessor(TypeDefinition typeDef, Weaver weaver) { m_TypeDef = typeDef; m_Weaver = weaver; m_Weaver.DLog(typeDef, "SyncListStructProcessor for " + typeDef.Name); }
public SyncListStructProcessor(TypeDefinition typeDef) { Weaver.DLog(typeDef, "SyncListStructProcessor for " + typeDef.Name, new object[0]); this.m_TypeDef = typeDef; }
private MethodReference GenerateSerialization() { Weaver.DLog(this.m_TypeDef, " GenerateSerialization", new object[0]); foreach (MethodDefinition current in this.m_TypeDef.Methods) { if (current.Name == "SerializeItem") { MethodReference result = current; return(result); } } MethodDefinition methodDefinition = new MethodDefinition("SerializeItem", MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.HideBySig, Weaver.voidType); methodDefinition.Parameters.Add(new ParameterDefinition("writer", ParameterAttributes.None, Weaver.scriptDef.MainModule.Import(Weaver.NetworkWriterType))); methodDefinition.Parameters.Add(new ParameterDefinition("item", ParameterAttributes.None, this.m_ItemType)); ILProcessor iLProcessor = methodDefinition.Body.GetILProcessor(); foreach (FieldDefinition current2 in this.m_ItemType.Resolve().Fields) { if (!current2.IsStatic && !current2.IsPrivate && !current2.IsSpecialName) { FieldReference fieldReference = Weaver.scriptDef.MainModule.Import(current2); TypeDefinition typeDefinition = fieldReference.FieldType.Resolve(); if (typeDefinition.HasGenericParameters) { Weaver.fail = true; Log.Error(string.Concat(new object[] { "GenerateSerialization for ", this.m_TypeDef.Name, " [", typeDefinition, "/", typeDefinition.FullName, "]. UNet [MessageBase] member cannot have generic parameters." })); MethodReference result = null; return(result); } if (typeDefinition.IsInterface) { Weaver.fail = true; Log.Error(string.Concat(new object[] { "GenerateSerialization for ", this.m_TypeDef.Name, " [", typeDefinition, "/", typeDefinition.FullName, "]. UNet [MessageBase] member cannot be an interface." })); MethodReference result = null; return(result); } MethodReference writeFunc = Weaver.GetWriteFunc(current2.FieldType); if (writeFunc == null) { Weaver.fail = true; Log.Error(string.Concat(new object[] { "GenerateSerialization for ", this.m_TypeDef.Name, " unknown type [", typeDefinition, "/", typeDefinition.FullName, "]. UNet [MessageBase] member variables must be basic types." })); MethodReference result = null; return(result); } iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_1)); iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_2)); iLProcessor.Append(iLProcessor.Create(OpCodes.Ldfld, fieldReference)); iLProcessor.Append(iLProcessor.Create(OpCodes.Call, writeFunc)); } } iLProcessor.Append(iLProcessor.Create(OpCodes.Ret)); this.m_TypeDef.Methods.Add(methodDefinition); return(methodDefinition); }