public static void Generate(Type arrayType, string completePath, bool clear, bool appendInFile)
        {
            Type   elementType     = arrayType.GetElementType();
            string elementFullName = elementType.GetPrettyFullName();

            StreamWriter writer = GetFileStream(completePath);

            bool success = true;

            if (!appendInFile)
            {
                writer.WriteLine("// THIS CODE IS GENERATED");
                writer.WriteLine("// DO NOT MODIFY IT");
                writer.WriteLine();
                writer.WriteLine("using System;");
                writer.WriteLine("using System.Collections.Generic;");
            }

            writer.WriteLine();
            writer.WriteLine("public static class " + NetSerializationCodeGenUtility.GetSerializerNameFromType(arrayType));
            writer.WriteLine("{");
            writer.WriteLine("    public static int GetSerializedBitSize(ref " + elementFullName + "[] obj)");
            writer.WriteLine("    {");

            if (clear)
            {
                writer.WriteLine("        return 0;");
            }
            else
            {
                writer.WriteLine("        if (obj == null)");
                writer.WriteLine("            return 1;");
                writer.WriteLine("        int result = 1 + sizeof(Int32) * 8;");
                writer.WriteLine("        for (int i = 0; i < obj.Length; i++)");
                writer.WriteLine("        {");
                writer.WriteLine("            " + ModelHelpers.GetSerializerFieldLine_GetNetBitSize(elementType, "[i]"));
                writer.WriteLine("        }");
                writer.WriteLine("        return result;");
            }

            writer.WriteLine("    }");

            writer.WriteLine();

            writer.WriteLine("    public static void Serialize(ref " + elementFullName + "[] obj, BitStreamWriter writer)");
            writer.WriteLine("    {");
            if (!clear)
            {
                writer.WriteLine("        if (obj == null)");
                writer.WriteLine("        {");
                writer.WriteLine("            writer.WriteBit(false);");
                writer.WriteLine("            return;");
                writer.WriteLine("        }");
                writer.WriteLine("        writer.WriteBit(true);");
                writer.WriteLine("        writer.WriteInt32(obj.Length);");
                writer.WriteLine("        for (int i = 0; i < obj.Length; i++)");
                writer.WriteLine("        {");
                writer.WriteLine("            " + ModelHelpers.GetSerializerFieldLine_Serialize(elementType, "[i]"));
                writer.WriteLine("        }");
            }
            writer.WriteLine("    }");

            writer.WriteLine();

            writer.WriteLine("    public static void Deserialize(ref " + elementFullName + "[] obj, BitStreamReader reader)");
            writer.WriteLine("    {");
            if (!clear)
            {
                writer.WriteLine("        if (reader.ReadBit() == false)");
                writer.WriteLine("        {");
                writer.WriteLine("            obj = null;");
                writer.WriteLine("            return;");
                writer.WriteLine("        }");
                writer.WriteLine("        obj = new " + elementFullName + "[reader.ReadInt32()];");
                writer.WriteLine("        for (int i = 0; i < obj.Length; i++)");
                writer.WriteLine("        {");
                writer.WriteLine("            " + ModelHelpers.GetSerializerFieldLine_Deserialize(elementType, "[i]"));
                writer.WriteLine("        }");
            }
            writer.WriteLine("    }");

            writer.WriteLine("}");


            if (!success)
            {
                Debug.LogWarning("Error in code generation for type: " + elementType);
            }
        }
Exemple #2
0
        public static void Generate(Type type, string completePath, bool clear, bool appendInFile)
        {
            NetSerializableAttribute serializableAttribute = type.GetCustomAttribute <NetSerializableAttribute>();

            List <FieldInfo> fields = new List <FieldInfo>(type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));

            fields.RemoveAll((f) => NetSerializationCodeGenUtility.ShouldIgnoreCodeGeneration(f, type));

            string typeFullName           = type.GetPrettyFullName();
            Type   baseClass              = type.BaseType == typeof(object) || type.BaseType == typeof(ValueType) ? null : type.BaseType;
            bool   couldBeDynamic         = !NetSerializationCodeGenUtility.ConsideredAsValueType(type);
            bool   isDynamicType          = serializableAttribute != null ? serializableAttribute.IsBaseClass : false;
            string methodObjParameter_Rcv = couldBeDynamic ?
                                            (typeFullName + " obj") :
                                            ("ref " + typeFullName + " obj");


            StreamWriter writer = GetFileStream(completePath);

            if (!appendInFile)
            {
                writer.Write(
                    @"// THIS CODE IS GENERATED
// DO NOT MODIFY IT

using System;
using System.Collections.Generic;
");
            }


            writer.WriteLine("public static class " + NetSerializationCodeGenUtility.GetSerializerNameFromType(type));
            writer.WriteLine("{");

            if (couldBeDynamic)
            {
                writer.WriteLine("    public static int GetSerializedBitSize_Class(" + methodObjParameter_Rcv + ")");
                writer.WriteLine("    {");
                if (clear)
                {
                    writer.WriteLine("        return 0;");
                }
                else
                {
                    writer.WriteLine("        if (obj == null)");
                    writer.WriteLine("            return 1;");
                    if (isDynamicType)
                    {
                        writer.WriteLine("        return 1 + NetSerializer.GetSerializedBitSize(obj);");
                    }
                    else
                    {
                        writer.WriteLine("        return 1 + GetSerializedBitSize(obj);");
                    }
                }
                writer.WriteLine("    }");
                writer.WriteLine();
            }

            writer.WriteLine("    public static int GetSerializedBitSize(" + methodObjParameter_Rcv + ")");
            writer.WriteLine("    {");

            if (clear)
            {
                writer.WriteLine("        return 0;");
            }
            else
            {
                writer.WriteLine("        int result = 0;");

                foreach (var field in fields)
                {
                    if (field.FieldType.IsArray)
                    {
                        s_generateArrayCode.Add(field.FieldType);
                    }
                    else if (field.FieldType.IsGenericType && field.FieldType.GetGenericTypeDefinition() == typeof(List <>))
                    {
                        s_generateListCode.Add(field.FieldType);
                    }
                    writer.WriteLine("        " + ModelHelpers.GetSerializerFieldLine_GetNetBitSize(field.FieldType, '.' + field.Name));
                }
                if (baseClass != null)
                {
                    writer.WriteLine("        " + ModelHelpers.GetSerializerFieldLine_GetNetBitSizeBaseClass(baseClass));
                }
                writer.WriteLine("        return result;");
            }

            writer.WriteLine("    }");

            writer.WriteLine();


            if (couldBeDynamic)
            {
                writer.WriteLine("    public static void Serialize_Class(" + methodObjParameter_Rcv + ", BitStreamWriter writer)");
                writer.WriteLine("    {");
                if (clear)
                {
                    // nothing
                }
                else
                {
                    writer.WriteLine("        if (obj == null)");
                    writer.WriteLine("        {");
                    writer.WriteLine("            writer.WriteBit(false);");
                    writer.WriteLine("            return;");
                    writer.WriteLine("        }");
                    writer.WriteLine("        writer.WriteBit(true);");

                    if (isDynamicType)
                    {
                        writer.WriteLine("        NetSerializer.Serialize(obj, writer);");
                    }
                    else
                    {
                        writer.WriteLine("        Serialize(obj, writer);");
                    }
                }
                writer.WriteLine("    }");
            }


            writer.WriteLine("    public static void Serialize(" + methodObjParameter_Rcv + ", BitStreamWriter writer)");
            writer.WriteLine("    {");
            if (!clear)
            {
                foreach (var field in fields)
                {
                    writer.WriteLine("        " + ModelHelpers.GetSerializerFieldLine_Serialize(field.FieldType, '.' + field.Name));
                }
                if (baseClass != null)
                {
                    writer.WriteLine("        " + ModelHelpers.GetSerializerFieldLine_SerializeBaseClass(baseClass));
                }
            }
            writer.WriteLine("    }");

            writer.WriteLine();

            if (couldBeDynamic)
            {
                writer.WriteLine("    public static " + typeFullName + " Deserialize_Class(BitStreamReader reader)");
                writer.WriteLine("    {");
                if (clear)
                {
                    writer.WriteLine("        return null;");
                }
                else
                {
                    writer.WriteLine("        if (reader.ReadBit() == false)");
                    writer.WriteLine("        {");
                    writer.WriteLine("            return null;");
                    writer.WriteLine("        }");

                    if (isDynamicType)
                    {
                        writer.WriteLine("        return (" + typeFullName + ")NetSerializer.Deserialize(reader);");
                    }
                    else
                    {
                        writer.WriteLine("        " + typeFullName + " obj = new " + typeFullName + "();");
                        writer.WriteLine("        Deserialize(obj, reader);");
                        writer.WriteLine("        return obj;");
                    }
                }
                writer.WriteLine("    }");
            }


            writer.WriteLine("    public static void Deserialize(" + methodObjParameter_Rcv + ", BitStreamReader reader)");
            writer.WriteLine("    {");
            if (!clear)
            {
                foreach (var field in fields)
                {
                    writer.WriteLine("        " + ModelHelpers.GetSerializerFieldLine_Deserialize(field.FieldType, '.' + field.Name));
                }
                if (baseClass != null)
                {
                    writer.WriteLine("        " + ModelHelpers.GetSerializerFieldLine_DeserializeBaseClass(baseClass));
                }
            }
            writer.WriteLine("    }");

            writer.WriteLine("}");
        }
        public static void Generate(Type listType, string completePath, bool clear, bool appendInFile)
        {
            Type   elementType     = listType.GetGenericArguments()[0];
            string elementFullName = elementType.GetPrettyFullName();

            StreamWriter writer = GetFileStream(completePath);

            bool success = true;

            if (!appendInFile)
            {
                writer.WriteLine("// THIS CODE IS GENERATED");
                writer.WriteLine("// DO NOT MODIFY IT");
                writer.WriteLine();
                writer.WriteLine("using System;");
                writer.WriteLine("using System.Collections.Generic;");
            }

            writer.WriteLine();
            writer.WriteLine("public static class " + NetSerializationCodeGenUtility.GetSerializerNameFromType(listType));
            writer.WriteLine("{");
            writer.WriteLine($"    public static int GetSerializedBitSize_Class(List<{elementFullName}> obj)");
            writer.WriteLine("    {");

            if (clear)
            {
                writer.WriteLine("        return 0;");
            }
            else
            {
                writer.WriteLine("        if (obj == null)");
                writer.WriteLine("            return 1;");
                writer.WriteLine("        int result = 1 + sizeof(Int32) * 8;");
                writer.WriteLine("        for (int i = 0; i < obj.Count; i++)");
                writer.WriteLine("        {");
                writer.WriteLine("            var x = obj[i];");
                writer.WriteLine("            " + ModelHelpers.GetSerializerFieldLine_GetNetBitSize(elementType, null, "x"));
                writer.WriteLine("        }");
                writer.WriteLine("        return result;");
            }

            writer.WriteLine("    }");

            writer.WriteLine();

            writer.WriteLine($"    public static void Serialize_Class(List<{elementFullName}> obj, BitStreamWriter writer)");
            writer.WriteLine("    {");
            if (!clear)
            {
                writer.WriteLine("        if (obj == null)");
                writer.WriteLine("        {");
                writer.WriteLine("            writer.WriteBit(false);");
                writer.WriteLine("            return;");
                writer.WriteLine("        }");
                writer.WriteLine("        writer.WriteBit(true);");
                writer.WriteLine("        writer.WriteInt32(obj.Count);");
                writer.WriteLine("        for (int i = 0; i < obj.Count; i++)");
                writer.WriteLine("        {");
                writer.WriteLine("            var x = obj[i];");
                writer.WriteLine("            " + ModelHelpers.GetSerializerFieldLine_Serialize(elementType, null, "x"));
                writer.WriteLine("        }");
            }
            writer.WriteLine("    }");

            writer.WriteLine();

            writer.WriteLine($"    public static List<{elementFullName}> Deserialize_Class(BitStreamReader reader)");
            writer.WriteLine("    {");
            if (!clear)
            {
                writer.WriteLine("        if (reader.ReadBit() == false)");
                writer.WriteLine("        {");
                writer.WriteLine("            return null;");
                writer.WriteLine("        }");
                writer.WriteLine("        int size = reader.ReadInt32();");
                writer.WriteLine($"        List<{elementFullName}> obj = new List<{elementFullName}>(size);");
                writer.WriteLine("        for (int i = 0; i < size; i++)");
                writer.WriteLine("        {");
                writer.WriteLine($"            {elementFullName} x = default;");
                writer.WriteLine("            " + ModelHelpers.GetSerializerFieldLine_Deserialize(elementType, null, "x"));
                writer.WriteLine($"            obj.Add(x);");
                writer.WriteLine("        }");
                writer.WriteLine("        return obj;");
            }
            writer.WriteLine("    }");

            writer.WriteLine("}");


            if (!success)
            {
                Debug.LogWarning("Error in code generation for type: " + elementType);
            }
        }