Example #1
0
        public static void Write(IBinaryWriter o, Kernel.Config.FieldLayout d)
        {
            o.Write(d != null);
            if (d == null)
            {
                return;
            }

            Type type = d.GetType();

            if (type == typeof(Kernel.Config.FieldLayoutOfConst))
            {
                o.Write(1);
                FieldLayoutOfConstSerializer.Write(o, d as Kernel.Config.FieldLayoutOfConst);
            }
            else if (type == typeof(Kernel.Config.FieldLayoutOfIntKey))
            {
                o.Write(2);
                FieldLayoutOfIntKeySerializer.Write(o, d as Kernel.Config.FieldLayoutOfIntKey);
            }
            else if (type == typeof(Kernel.Config.FieldLayoutOfStringKey))
            {
                o.Write(3);
                FieldLayoutOfStringKeySerializer.Write(o, d as Kernel.Config.FieldLayoutOfStringKey);
            }
            else
            {
                o.Write(0);
            }
        }
Example #2
0
        public static Kernel.Config.FieldLayout Read(IBinaryReader o, Kernel.Config.FieldLayout d)
        {
            if (o.ReadBoolean() == false)
            {
                return(null);
            }

            int type = o.ReadInt32();

            switch (type)
            {
            case 1:
                d = FieldLayoutOfConstSerializer.Read(o, d as Kernel.Config.FieldLayoutOfConst);
                break;

            case 2:
                d = FieldLayoutOfIntKeySerializer.Read(o, d as Kernel.Config.FieldLayoutOfIntKey);
                break;

            case 3:
                d = FieldLayoutOfStringKeySerializer.Read(o, d as Kernel.Config.FieldLayoutOfStringKey);
                break;
            }
            return(d);
        }
Example #3
0
        public static Kernel.Config.FieldLayout[] Read(IBinaryReader o, Kernel.Config.FieldLayout[] d)
        {
            if (o.ReadBoolean() == false)
            {
                return(null);
            }

            int size = o.ReadInt32();

            if (d == null || d.Length != size)
            {
                d = new Kernel.Config.FieldLayout[size];
            }
            for (int i = 0; i < size; ++i)
            {
                d[i] = Poli_FieldLayoutSerializer.Read(o, d[i] as Kernel.Config.FieldLayout);
            }
            return(d);
        }
Example #4
0
        public void WriteToBinary(BinWriter writer)
        {
            using (MemoryStream bodyStream = new MemoryStream())
                using (BinWriter bodyWriter = new BinWriter(bodyStream))
                {
                    writer.Write(Hash);

                    if (Fields == null)
                    {
                        return;
                    }

                    ConfigHeader header = new ConfigHeader();
                    header.Contents = new FieldLayout[Fields.Length];

                    for (int i = 0; i < Fields.Length; i++)
                    {
                        var f = Fields[i];

                        int offset = (int)bodyWriter.BaseStream.Position;

                        var elemSerializer = Serializers[f.ElemType];
                        if (f.FieldMode == ConfigFieldInfo.Mode.CONST)
                        {
                            header.Contents[i] = FieldLayout.CreateFieldLayout(f.FieldMode, offset, null);
                            object obj = ConfigManager.Instance.GetConfig(f.ElemType);
                            elemSerializer.Write(bodyWriter, obj);
                        }
                        else
                        {
                            var dict = (IDictionary)ConfigManager.Instance.GetConfig(f.ElemType);
                            if (dict == null)
                            {
                                dict = TypeUtil.CreateInstance(f.ConfigType) as IDictionary;
                            }

                            var layout = header.Contents[i] = FieldLayout.CreateFieldLayout(f.FieldMode, offset, TypeUtil.GetDictionaryKeyType(dict.GetType()));

                            var e = dict.GetEnumerator();
                            while (e.MoveNext())
                            {
                                layout.Add(e.Key, (int)bodyWriter.BaseStream.Position);
                                elemSerializer.Write(bodyWriter, e.Value);
                            }

                            layout.Size = (int)bodyWriter.BaseStream.Position - offset;
                        }
                    }

                    using (var headerStream = new MemoryStream())
                        using (var headerWriter = new BinWriter(headerStream))
                        {
                            Serializers[typeof(ConfigHeader)].Write(headerWriter, header);

                            header.HeaderSize = (int)headerWriter.BaseStream.Position;
                            header.BodySize   = (int)bodyWriter.BaseStream.Position;

                            Serializers[typeof(ConfigHeader)].Write(writer, header);

                            writer.Write(bodyStream.GetBuffer());
                        }
                }
        }