Ejemplo n.º 1
0
        private MethodReference GenerateSerialization()
        {
            Weaver.DLog(this.m_TypeDef, "  GenerateSerialization", new object[0]);
            foreach (MethodDefinition definition in this.m_TypeDef.Methods)
            {
                if (definition.Name == "SerializeItem")
                {
                    return(definition);
                }
            }
            MethodDefinition item = new MethodDefinition("SerializeItem", MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Virtual, Weaver.voidType)
            {
                Parameters =
                {
                    new ParameterDefinition("writer", ParameterAttributes.None, Weaver.scriptDef.MainModule.ImportReference(Weaver.NetworkWriterType)),
                    new ParameterDefinition("item",   ParameterAttributes.None, this.m_ItemType)
                }
            };
            ILProcessor iLProcessor = item.Body.GetILProcessor();

            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();
                    if (definition4.HasGenericParameters)
                    {
                        Weaver.fail = true;
                        Log.Error(string.Concat(new object[] { "GenerateSerialization for ", this.m_TypeDef.Name, " [", definition4, "/", definition4.FullName, "]. UNet [MessageBase] member cannot have generic parameters." }));
                        return(null);
                    }
                    if (definition4.IsInterface)
                    {
                        Weaver.fail = true;
                        Log.Error(string.Concat(new object[] { "GenerateSerialization for ", this.m_TypeDef.Name, " [", definition4, "/", definition4.FullName, "]. UNet [MessageBase] member cannot be an interface." }));
                        return(null);
                    }
                    MethodReference writeFunc = Weaver.GetWriteFunc(definition3.FieldType);
                    if (writeFunc != null)
                    {
                        iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_1));
                        iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_2));
                        iLProcessor.Append(iLProcessor.Create(OpCodes.Ldfld, field));
                        iLProcessor.Append(iLProcessor.Create(OpCodes.Call, writeFunc));
                    }
                    else
                    {
                        Weaver.fail = true;
                        Log.Error(string.Concat(new object[] { "GenerateSerialization for ", this.m_TypeDef.Name, " unknown type [", definition4, "/", definition4.FullName, "]. UNet [MessageBase] member variables must be basic types." }));
                        return(null);
                    }
                }
            }
            iLProcessor.Append(iLProcessor.Create(OpCodes.Ret));
            this.m_TypeDef.Methods.Add(item);
            return(item);
        }
        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);
        }
Ejemplo n.º 3
0
        // 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);
        }
Ejemplo n.º 4
0
        private void GenerateSerialization()
        {
            Weaver.DLog(this.m_td, "  GenerateSerialization", new object[0]);
            foreach (MethodDefinition current in this.m_td.Methods)
            {
                if (current.Name == "Serialize")
                {
                    return;
                }
            }
            if (this.m_td.Fields.Count == 0)
            {
                return;
            }
            MethodDefinition methodDefinition = new MethodDefinition("Serialize", MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.HideBySig, Weaver.voidType);

            methodDefinition.Parameters.Add(new ParameterDefinition("writer", ParameterAttributes.None, Weaver.scriptDef.MainModule.Import(Weaver.NetworkWriterType)));
            ILProcessor iLProcessor = methodDefinition.Body.GetILProcessor();

            foreach (FieldDefinition current2 in this.m_td.Fields)
            {
                if (!current2.IsStatic && !current2.IsPrivate && !current2.IsSpecialName)
                {
                    if (current2.FieldType.Resolve().HasGenericParameters)
                    {
                        Weaver.fail = true;
                        Log.Error(string.Concat(new object[]
                        {
                            "GenerateSerialization for ",
                            this.m_td.Name,
                            " [",
                            current2.FieldType,
                            "/",
                            current2.FieldType.FullName,
                            "]. UNet [MessageBase] member cannot have generic parameters."
                        }));
                        return;
                    }
                    if (current2.FieldType.Resolve().IsInterface)
                    {
                        Weaver.fail = true;
                        Log.Error(string.Concat(new object[]
                        {
                            "GenerateSerialization for ",
                            this.m_td.Name,
                            " [",
                            current2.FieldType,
                            "/",
                            current2.FieldType.FullName,
                            "]. UNet [MessageBase] member cannot be an interface."
                        }));
                        return;
                    }
                    MethodReference writeFunc = Weaver.GetWriteFunc(current2.FieldType);
                    if (writeFunc == null)
                    {
                        Weaver.fail = true;
                        Log.Error(string.Concat(new object[]
                        {
                            "GenerateSerialization for ",
                            this.m_td.Name,
                            " unknown type [",
                            current2.FieldType,
                            "/",
                            current2.FieldType.FullName,
                            "]. UNet [MessageBase] member variables must be basic types."
                        }));
                        return;
                    }
                    iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_1));
                    iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_0));
                    iLProcessor.Append(iLProcessor.Create(OpCodes.Ldfld, current2));
                    iLProcessor.Append(iLProcessor.Create(OpCodes.Call, writeFunc));
                }
            }
            iLProcessor.Append(iLProcessor.Create(OpCodes.Ret));
            this.m_td.Methods.Add(methodDefinition);
        }