Exemplo n.º 1
0
        public void VisitReadTwoDimArray(string variable, string reader, TwoDimArrayTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size0 = {0}.ReadInt32();", reader);
            builder.WriteLine("{0} = new {1}(size0);", variable, node.TypeName);
            builder.WriteLine("for(int i = 0; i < size0; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteLine("int size1 = {0}.ReadInt32();", reader);
            builder.WriteLine("{0}[i] = new {1}[size1];", variable, node.ParameterNodes[0].TypeName);
            builder.WriteLine("for(int j = 0; j < size1; ++j)");
            builder.LeftPar();
            builder.Indent();

            string target = variable + "[i, j]";

            builder.WriteIndents();
            builder.Write(target + " = ");
            VisitDeserialization(target, reader, node.Serialize, builder);
            builder.Write(";");
            builder.NextLine();

            builder.UnIndent();
            builder.RightPar();

            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 2
0
        protected void VisitReadPolyClass(string variable, string reader, PolyClassTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int type = {0}.ReadInt32();", reader);
            builder.WriteLine("switch(type)");
            builder.LeftPar();
            builder.Indent();

            var children = node.Children.ToArray();

            for (int index = 0; index < children.Length; index++)
            {
                var n = children[index] as SerializeNode;

                builder.WriteLine("case {0}:", index + 1);

                builder.Indent();
                builder.WriteIndents();
                builder.Write("{0} = ", variable);
                VisitDeserialization(variable, reader, n, builder);
                builder.Write(";");
                builder.NextLine();

                builder.WriteLine("break;");
                builder.UnIndent();
            }

            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 3
0
        public void VisitReadDictionary(string variable, string reader, DictionaryTypeNode node, TextBuilder builder)
        {
            string equalityComparer = GetEqualityComparer(node.SerializeKey.Type);

            builder.WriteLine("int size = {0}.ReadInt32();", reader);
            builder.WriteLine("if({0} == null) ", variable);
            builder.WriteLine("\t{0} = new {1}(size{2});", variable, node.TypeName,
                              string.IsNullOrEmpty(equalityComparer) ? "" : ", " + equalityComparer);
            builder.WriteLine("else");
            builder.WriteLine("\t{0}.Clear();", variable);
            builder.WriteLine("for(int i = 0; i < size; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            builder.Write("{0} key = ", node.ParameterNodes[0].TypeName);
            VisitDeserialization(string.Format("default({0})", node.ParameterNodes[0].TypeName), reader, node.SerializeKey, builder);
            builder.Write(";");
            builder.NextLine();

            builder.WriteIndents();
            builder.Write("{0} value = ", node.ParameterNodes[1].TypeName);
            VisitDeserialization(string.Format("default({0})", node.ParameterNodes[1].TypeName), reader, node.SerializeValue, builder);
            builder.Write(";");
            builder.NextLine();

            builder.WriteLine("{0}.Add(key, value);", variable);

            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 4
0
        protected virtual void VisitHeader(TypeNode node, string nameSpace, TextBuilder builder)
        {
            builder.WriteLine("// Auto generated code");
            builder.WriteLine("using Kernel;");
            builder.WriteLine("using Kernel.Config;");
            builder.WriteLine("using Kernel.Util;");
            builder.WriteLine("using System;");
            builder.WriteLine("using System.IO;");
            builder.EmptyLine();

            builder.WriteLine("namespace {0}", nameSpace);
            builder.LeftPar();
            builder.Indent();
            builder.WriteLine("public class {0} : IConfigSerializer", node.SerializerTypeName);
            builder.LeftPar();
            builder.Indent();
        }
Exemplo n.º 5
0
        public void VisitWritePolyClass(string variable, string writer, PolyClassTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("Type type = {0}.GetType();", variable);

            var children = node.Children.ToArray();

            for (int index = 0; index < children.Length; index++)
            {
                var n = children[index] as SerializeNode;

                builder.WriteIndents();
                if (index > 0)
                {
                    builder.Write("else ");
                }
                builder.Write("if(type == typeof({0}))", n.TypeName);
                builder.NextLine();

                builder.LeftPar();
                builder.Indent();

                builder.WriteLine("{0}.Write({1});", writer, index + 1);

                builder.WriteIndents();
                VisitSerialization(variable + " as " + n.TypeName, writer, n, builder);
                builder.Write(";");
                builder.NextLine();

                builder.UnIndent();
                builder.RightPar();
            }
            if (children.Length > 0)
            {
                builder.WriteLine("else");
                builder.LeftPar();
                builder.Indent();
                builder.WriteLine("{0}.Write(0);", writer);
                builder.UnIndent();
                builder.RightPar();
            }
        }
Exemplo n.º 6
0
        public void VisitWriteArray(string variable, string writer, ArrayTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size = {0}.Length;", variable);
            builder.WriteLine("{0}.Write(size);", writer);
            builder.WriteLine("for(int i = 0; i < size; ++i)");
            builder.LeftPar();
            builder.Indent();

            VisitWriteField(variable + "[i]", writer, node.Serialize, builder);

            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 7
0
        protected virtual void VisitReadPart(ReadPartNode node, TextBuilder builder)
        {
            builder.WriteLine("object IConfigSerializer.Read(IBinaryReader reader, object o)");
            builder.LeftPar();
            builder.Indent();
            builder.WriteLine("return Read(reader, ({0})o);", node.TypeName);
            builder.UnIndent();
            builder.RightPar();

            builder.EmptyLine();

            builder.WriteLine("public static {0} Read(IBinaryReader o, {0} d)", node.TypeName);
            builder.LeftPar();
            builder.Indent();

            if (!node.Type.IsValueType)
            {
                builder.WriteLine("if(o.ReadBoolean() == false) return null;");
                builder.EmptyLine();
            }

            var root = node.Parent as RootNode;

            if (root != null && root.CustomSerializeParams != null)
            {
                AppendReadFromCustom("o", root.CustomSerializeParams, builder);
            }
            else
            {
                AppendReadPart(node, builder);
            }

            builder.WriteLine("return d;");

            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 8
0
        public void VisitWriteTwoDimArray(string variable, string writer, TwoDimArrayTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size0 = {0}.Length;", variable);
            builder.WriteLine("{0}.Write(size0);", writer);
            builder.WriteLine("for(int i = 0; i < size0; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteLine("int size1 = {0}[i] != null ? {0}[i].Count : 0;", variable);
            builder.WriteLine("{0}.Write(size1);", writer);
            builder.WriteLine("for(int j = 0; j < size1; ++j)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            VisitSerialization(variable + "[i, j]", writer, node.Serialize, builder);
            builder.Write(";");
            builder.NextLine();

            builder.UnIndent();
            builder.RightPar();
            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 9
0
        protected virtual void VisitWritePart(WritePartNode node, TextBuilder builder)
        {
            builder.WriteLine("void IConfigSerializer.Write(IBinaryWriter writer, object o)");
            builder.LeftPar();
            builder.Indent();
            builder.WriteLine("Write(writer, ({0})o);", node.TypeName);
            builder.UnIndent();
            builder.RightPar();

            builder.EmptyLine();

            builder.WriteLine("public static void Write(IBinaryWriter o, {0} d)", node.TypeName);
            builder.LeftPar();
            builder.Indent();

            if (!node.Type.IsValueType)
            {
                builder.WriteLine("o.Write(d != null);");
                builder.WriteLine("if(d == null) return;");
                builder.EmptyLine();
            }

            var root = node.Parent as RootNode;

            if (root != null && root.CustomSerializeParams != null)
            {
                AppendWriteFromCustom("o", root.CustomSerializeParams, builder);
            }
            else
            {
                AppendWritePart(node, builder);
            }

            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 10
0
        public void VisitReadArray(string variable, string reader, ArrayTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size = {0}.ReadInt32();", reader);
            builder.WriteLine("if({0} == null || {0}.Length != size) {0} = new {1}[size];", variable, node.ParameterNodes[0].TypeName);
            builder.WriteLine("for(int i = 0; i < size; ++i)");
            builder.LeftPar();
            builder.Indent();

            string target = variable + "[i]";

            VisitReadField(target, reader, false, node.Serialize, builder);

            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 11
0
        public void VisitWriteHashSet(string variable, string writer, HashSetTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size = {0}.Count;", variable);
            builder.WriteLine("{0}.Write(size);", writer);
            builder.WriteLine("foreach(var elem in {0})", variable);
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            VisitSerialization("elem", writer, node.Serialize, builder);
            builder.Write(";");
            builder.NextLine();

            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 12
0
        public void VisitWriteList(string variable, string writer, ListTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size = {0}.Count;", variable);
            builder.WriteLine("{0}.Write(size);", writer);
            builder.WriteLine("for(int i = 0; i < size; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            VisitSerialization(variable + "[i]", writer, node.Serialize, builder);
            builder.Write(";");
            builder.NextLine();

            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 13
0
        public void VisitReadHashSet(string variable, string reader, HashSetTypeNode node, TextBuilder builder)
        {
            string equalityComparer = GetEqualityComparer(node.Serialize.Type);

            builder.WriteLine("int size = {0}.ReadInt32();", reader);
            builder.WriteLine("{0} = new {1}({2});", variable, node.TypeName, equalityComparer);
            builder.WriteLine("for(int i = 0; i < size; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            builder.Write("{0}.Add(", variable);
            VisitDeserialization(string.Format("default({0})", node.ParameterNodes[0].TypeName), reader, node.Serialize, builder);
            builder.Write(");");
            builder.NextLine();

            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 14
0
        public void VisitReadList(string variable, string reader, ListTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size = {0}.ReadInt32();", reader);
            builder.WriteLine("if({0} == null) ", variable);
            builder.WriteLine("\t{0} = new {1}(size);", variable, node.TypeName);
            builder.WriteLine("else if({0}.Count != size)", variable);
            builder.WriteLine("\t{0}.Clear();", variable);
            builder.WriteLine("for(int i = 0; i < size; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            builder.Write("var temp = ", variable);
            VisitDeserialization(string.Format("(d.Count > i ? d[i] : default({0}))", node.ParameterNodes[0].TypeName), reader, node.Serialize, builder);
            builder.Write(";");
            builder.NextLine();

            builder.WriteLine("if(d.Count <= i) d.Add(temp); else d[i] = temp;");

            builder.UnIndent();
            builder.RightPar();
        }
Exemplo n.º 15
0
        public void Export(SerializationGenerator generator, string nameSpace, string md5, string targetFolder)
        {
            var builder = new TextBuilder();

            builder.WriteLine("using System;");
            builder.WriteLine("using System.Collections.Generic;");
            builder.WriteLine("using System.IO;");
            builder.WriteLine("using Kernel;");
            builder.WriteLine("using Kernel.Config;");
            builder.EmptyLine();

            builder.WriteLine("namespace {0}", nameSpace);
            builder.LeftPar();
            builder.Indent();

            builder.WriteLine("public partial class ConfigSerializer : ConfigSerializerBase");
            builder.LeftPar();
            builder.Indent();

            builder.WriteLine("private static readonly ConfigFieldInfo[] fields =");
            builder.WriteLine("{");
            builder.Indent();

            foreach (var pair in generator.RegisteredTypes)
            {
                if (pair.IsEntry && !pair.Root.PoliClass)
                {
                    VisitEntry(pair, builder);
                }
            }

            builder.UnIndent();
            builder.WriteLine("};");

            builder.EmptyLine();

            builder.WriteLine("public override ConfigFieldInfo[] Fields { get { return fields; } }");

            builder.EmptyLine();

            builder.WriteLine("public override string Hash {{ get {{ return \"{0}\"; }} }}", md5);

            builder.EmptyLine();

            builder.WriteLine("private static readonly Dictionary<Type, IConfigSerializer> serializers = new Dictionary<Type, IConfigSerializer>");
            builder.WriteLine("{");
            builder.Indent();

            foreach (var pair in generator.RegisteredTypes)
            {
                if ((pair.Root.PoliClass || !XmlUtil.IsPolymorphicClass(pair.Type)) && !pair.IsEnum)
                {
                    VisitSerializer(pair, builder);
                }
            }

            builder.UnIndent();
            builder.WriteLine("};");

            builder.EmptyLine();

            builder.WriteLine("public override Dictionary<Type, IConfigSerializer> Serializers { get { return serializers; } }");

            builder.UnIndent();
            builder.RightPar();

            builder.UnIndent();
            builder.RightPar();

            builder.WriteToFile(Path.Combine(targetFolder, "ConfigSerializer.cs"));
        }