Ejemplo n.º 1
0
        MethodReference GenerateDeserialization()
        {
            Weaver.DLog(m_TypeDef, "  GenerateDeserialization");
            foreach (var m in m_TypeDef.Methods)
            {
                if (m.Name == "DeserializeItem")
                {
                    return(m);
                }
            }

            MethodDefinition serializeFunc = new MethodDefinition("DeserializeItem", MethodAttributes.Public |
                                                                  MethodAttributes.Virtual |
                                                                  MethodAttributes.Public |
                                                                  MethodAttributes.HideBySig,
                                                                  m_ItemType);

            serializeFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, Weaver.scriptDef.MainModule.ImportReference(Weaver.NetworkReaderType)));

            ILProcessor serWorker = serializeFunc.Body.GetILProcessor();

            serWorker.Body.InitLocals = true;
            serWorker.Body.Variables.Add(new VariableDefinition(m_ItemType));

            // init item instance
            serWorker.Append(serWorker.Create(OpCodes.Ldloca, 0));
            serWorker.Append(serWorker.Create(OpCodes.Initobj, m_ItemType));


            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();

                MethodReference readerFunc = Weaver.GetReadFunc(field.FieldType);
                if (readerFunc != null)
                {
                    serWorker.Append(serWorker.Create(OpCodes.Ldloca, 0));
                    serWorker.Append(serWorker.Create(OpCodes.Ldarg_1));
                    serWorker.Append(serWorker.Create(OpCodes.Call, readerFunc));
                    serWorker.Append(serWorker.Create(OpCodes.Stfld, importedField));
                }
                else
                {
                    Weaver.fail = true;
                    Log.Error("GenerateDeserialization for " + m_TypeDef.Name + " unknown type [" + ft + "]. UNet [SyncVar] member variables must be basic types.");
                    return(null);
                }
            }
            serWorker.Append(serWorker.Create(OpCodes.Ldloc_0));
            serWorker.Append(serWorker.Create(OpCodes.Ret));

            m_TypeDef.Methods.Add(serializeFunc);
            return(serializeFunc);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
 private void GenerateDeSerialization()
 {
     Weaver.DLog(this.m_td, "  GenerateDeserialization", new object[0]);
     foreach (MethodDefinition definition in this.m_td.Methods)
     {
         if (definition.Name == "Deserialize")
         {
             return;
         }
     }
     if (this.m_td.Fields.Count != 0)
     {
         MethodDefinition item = new MethodDefinition("Deserialize", MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Virtual, Weaver.voidType)
         {
             Parameters = { new ParameterDefinition("reader", ParameterAttributes.None, Weaver.scriptDef.MainModule.ImportReference(Weaver.NetworkReaderType)) }
         };
         ILProcessor iLProcessor = item.Body.GetILProcessor();
         foreach (FieldDefinition definition3 in this.m_td.Fields)
         {
             if ((!definition3.IsStatic && !definition3.IsPrivate) && !definition3.IsSpecialName)
             {
                 MethodReference readFunc = Weaver.GetReadFunc(definition3.FieldType);
                 if (readFunc != null)
                 {
                     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, definition3));
                 }
                 else
                 {
                     Weaver.fail = true;
                     Log.Error(string.Concat(new object[] { "GenerateDeSerialization for ", this.m_td.Name, " unknown type [", definition3.FieldType, "]. UNet [SyncVar] member variables must be basic types." }));
                     return;
                 }
             }
         }
         iLProcessor.Append(iLProcessor.Create(OpCodes.Ret));
         this.m_td.Methods.Add(item);
     }
 }