Example #1
0
        public override void Generate(HashSet <RootType> referTypes)
        {
            if (Type.IsEnum)
            {
                GenerateEnum(referTypes);
                return;
            }

            AddChild(Header = new TypeNode(Root), referTypes);

            List <FieldInfo> fields;
            var customInterface = Type.GetInterfaces().FirstOrDefault(t => t.Name.StartsWith("ICustomSerialization"));

            if (customInterface != null)
            {
                var arguments = customInterface.GetGenericArguments();
                CustomSerializeParams = new SerializeNode[arguments.Length];
                for (int i = 0; i < arguments.Length; i++)
                {
                    AddChild(CustomSerializeParams[i] = SerializeNode.Create(arguments[i]), referTypes);
                }
                fields = new List <FieldInfo>();
            }
            else
            {
                fields = GetSerializeFields(Type);
            }

            AddChild(ReadPart  = new ReadPartNode(Root, fields), referTypes);
            AddChild(WritePart = new WritePartNode(Root, fields), referTypes);

            FileName = Header.SerializerTypeName;
        }
Example #2
0
        public void VisitReadField(string variable, string reader, bool readOnly, SerializeNode node, TextBuilder builder)
        {
            if (node == null)
            {
                return;
            }

            builder.WriteIndents();
            if (node.Category == SerializeNode.NodeCategory.LOCALE)
            {
                builder.Write("{0}.Set(StringSerializer.Read({1}, null));", variable, reader);
            }
            else
            {
                if (!readOnly)
                {
                    builder.Write(variable);
                    builder.Write(" = ");
                }
                VisitDeserialization(variable, reader, node, builder);
                if (!readOnly && node.Category == SerializeNode.NodeCategory.CLASS)
                {
                    builder.Write(" as {0}", node.TypeName);
                }
                builder.Write(";");
            }
            builder.NextLine();
        }
Example #3
0
        public void VisitPrimitiveDeserialization(string reader, SerializeNode node, TextBuilder builder)
        {
            string str;

            if (primitiveTypeString.TryGetValue(node.Type, out str))
            {
                builder.Write("{0}.Read{1}()", reader, str);
            }
        }
Example #4
0
        public override void Generate(HashSet <RootType> referTypes)
        {
            base.Generate(referTypes);

            foreach (var subType in GetSortedNoneAbstractTypes(Type))
            {
                if (subType == Type)
                {
                    var typeNode      = new TypeNode(new RootType(subType, false));
                    var serializeNode = new SerializeNode(subType, SerializeNode.NodeCategory.CLASS, typeNode);

                    AddChild(serializeNode, referTypes);
                }
                else
                {
                    AddChild(SerializeNode.Create(subType), referTypes);
                }
            }
        }
Example #5
0
        public void VisitWriteField(string variable, string writer, SerializeNode node, TextBuilder builder)
        {
            if (node == null)
            {
                return;
            }

            builder.WriteIndents();
            if (node.Category == SerializeNode.NodeCategory.LOCALE)
            {
                builder.Write("StringSerializer.Write({0}, {1}.ToString());", writer, variable);
            }
            else
            {
                VisitSerialization(variable, writer, node, builder);
                builder.Write(";");
            }
            builder.NextLine();
        }
Example #6
0
        public void VisitDeserialization(string variable, string reader, SerializeNode node, TextBuilder builder)
        {
            switch (node.Category)
            {
            case SerializeNode.NodeCategory.PRIMITIVE:
                VisitPrimitiveDeserialization(reader, node, builder);
                break;

            case SerializeNode.NodeCategory.ENUM:
                VisitEnumDeserialization(reader, node, builder);
                break;

            case SerializeNode.NodeCategory.TIMESPAN:
                VisitTimeSpanDeserialization(reader, node, builder);
                break;

            case SerializeNode.NodeCategory.DATETIME:
                VisitDateTimeDeserialization(reader, node, builder);
                break;

            case SerializeNode.NodeCategory.COLOR:
                VisitColorDeserialization(reader, node, builder);
                break;

            case SerializeNode.NodeCategory.POLY_CLASS:
            case SerializeNode.NodeCategory.CLASS:
            case SerializeNode.NodeCategory.ARRAY:
            case SerializeNode.NodeCategory.LIST:
            case SerializeNode.NodeCategory.DICTIONARY:
                VisitClassDeserialization(variable, reader, node, builder);
                break;

            case SerializeNode.NodeCategory.STRUCT:
                VisitStructDeserialization(variable, reader, node, builder);
                break;

            case SerializeNode.NodeCategory.NULLABLE:
                VisitNullableDeserialization(variable, reader, node as NullableSerializationNode, builder);
                break;
            }
        }
Example #7
0
 protected virtual void VisitClassDeserialization(string variable, string reader, SerializeNode node, TextBuilder builder)
 {
     builder.Write("{0}.Read({1}, {3} as {2})", node.TypeNode.SerializerTypeName, reader, node.TypeName, variable);
 }
Example #8
0
 public void VisitStructDeserialization(string variable, string reader, SerializeNode node, TextBuilder builder)
 {
     builder.Write("{0}.Read({1}, {2})", node.TypeNode.SerializerTypeName, reader, variable);
 }
Example #9
0
 public void VisitColorDeserialization(string reader, SerializeNode node, TextBuilder builder)
 {
     builder.Write("(Kernel.Engine.Color){0}.ReadUInt32()", reader);
 }
Example #10
0
 public void VisitDateTimeDeserialization(string reader, SerializeNode node, TextBuilder builder)
 {
     builder.Write("TimeUtil.CTimeToUtcDate({0}.ReadInt64())", reader);
 }
Example #11
0
 public void VisitTimeSpanDeserialization(string reader, SerializeNode node, TextBuilder builder)
 {
     builder.Write("new SerializableTimeSpan({0}.ReadInt64() * TimeSpan.TicksPerSecond)", reader);
 }
Example #12
0
 public virtual void VisitFixedDeserialization(string reader, SerializeNode node, TextBuilder builder)
 {
     builder.Write("{0}.ReadFixed()", reader);
 }
Example #13
0
 public override void Generate(HashSet <RootType> referTypes)
 {
     base.Generate(referTypes);
     AddChild(Serialize = SerializeNode.Create(TypeUtil.GetHashSetValueType(Type)), referTypes);
 }
Example #14
0
 public NullableSerializationNode(Type type, SerializeNode subTypeNode, CustomSerialierAttribute customAttribute)
     : base(type, NodeCategory.NULLABLE, customAttr: customAttribute)
 {
     Serialize = subTypeNode;
 }
Example #15
0
 public override void Generate(HashSet <RootType> referTypes)
 {
     base.Generate(referTypes);
     AddChild(SerializeKey   = SerializeNode.Create(TypeUtil.GetDictionaryKeyType(Type)), referTypes);
     AddChild(SerializeValue = SerializeNode.Create(TypeUtil.GetDictionaryValueType(Type)), referTypes);
 }
Example #16
0
 public void VisitEnumDeserialization(string reader, SerializeNode node, TextBuilder builder)
 {
     builder.Write("({0}){1}.ReadInt32()", node.TypeName, reader);
 }
Example #17
0
 public void VisitClassSerialization(string variable, string writer, SerializeNode node, TextBuilder builder)
 {
     builder.Write("{0}.Write({1}, {2})", node.TypeNode.SerializerTypeName, writer, variable);
 }
Example #18
0
 public override void Generate(HashSet <RootType> referTypes)
 {
     AddChild(FieldType = new TypeNode(Type, customAttribute), referTypes);
     AddChild(Serialize = SerializeNode.Create(field.FieldType, customAttribute), referTypes);
 }