Ejemplo n.º 1
0
        public static ByamlContext FromObject(object obj)
        {
            ByamlContext ctx = new ByamlContext();

            ctx.SerializeObject(obj);
            return(ctx);
        }
Ejemplo n.º 2
0
            public static ByamlEntry ParseEntry(ByamlContext ctx, ByamlNodeType t, BinaryDataReader br)
            {
                switch (t)
                {
                case ByamlNodeType.StringValue: return(new ByamlEntry(t, ctx.Stringtable[br.ReadInt32()]));

                case ByamlNodeType.PathValue: throw new ByamlException("Paths not supported");

                case ByamlNodeType.PathTable: throw new ByamlException("Paths not supported");

                case ByamlNodeType.BooleanValue: return(new ByamlEntry(t, Convert.ToBoolean(br.ReadInt32())));

                case ByamlNodeType.IntegerValue: return(new ByamlEntry(t, br.ReadInt32()));

                case ByamlNodeType.FloatValue: return(new ByamlEntry(t, br.ReadSingle()));

                case ByamlNodeType.Dictionary: return(ParseDictionary(ctx, br));

                case ByamlNodeType.Array: return(ParseArray(ctx, br));

                case ByamlNodeType.StringTable: return(ParseStringTable(ctx, br));

                default: throw new ByamlException($"Invalid or unknow node type : 0x{t:X}");
                }
            }
Ejemplo n.º 3
0
            public static void WriteValue(ByamlContext ctx, ByamlEntry entry, BinaryDataWriter bw)
            {
                switch (entry.NodeType)
                {
                case ByamlNodeType.StringValue:
                    bw.Write(Array.IndexOf(ctx.Stringtable.ToArray(), (string)entry.Value));
                    break;

                case ByamlNodeType.BooleanValue:
                    bw.Write((bool)entry.Value, BooleanDataFormat.Dword);
                    break;

                case ByamlNodeType.IntegerValue:
                    bw.Write((int)entry.Value);
                    break;

                case ByamlNodeType.FloatValue:
                    bw.Write((float)entry.Value);
                    break;

                case ByamlNodeType.PathTable:
                case ByamlNodeType.StringTable:
                case ByamlNodeType.Array:
                case ByamlNodeType.Dictionary:
                case ByamlNodeType.PathValue:
                    throw new ByamlException("Invalid Node Type");

                default:
                    throw new ByamlException("Invalid or unknown node type");
                }
            }
Ejemplo n.º 4
0
            public static ByamlEntry ParseDictionary(ByamlContext ctx, BinaryDataReader br)
            {
                Dictionary <string, ByamlEntry> dict = new Dictionary <string, ByamlEntry>();

                long startPos = br.Position;

                ByamlNodeType t = (ByamlNodeType)br.ReadByte();

                if (t != ByamlNodeType.Dictionary)
                {
                    throw new ByamlException("Invalid Dictionary");
                }

                int count = br.ReadInt24();

                for (int i = 0; i < count; i++)
                {
                    string name = ctx.NameTable[br.ReadInt24()];
                    Console.WriteLine(name);
                    ByamlNodeType t2 = (ByamlNodeType)br.ReadByte();

                    if (!NodeIsValue(t2))
                    {
                        uint off = br.ReadUInt32();
                        using (br.TemporarySeek(off, SeekOrigin.Begin))
                            dict.Add(name, ParseEntry(ctx, t2, br));
                    }
                    else
                    {
                        dict.Add(name, ParseEntry(ctx, t2, br));
                    }
                }

                return(new ByamlEntry(t, dict));
            }
Ejemplo n.º 5
0
            public static void WriteArray(ByamlContext ctx, ByamlEntry entry, BinaryDataWriter bw)
            {
                var list = (List <ByamlEntry>)entry.Value;

                bw.Write((byte)entry.NodeType);
                bw.WriteInt24(list.Count);

                List <long> tempOffs = new List <long>();

                //node types
                foreach (var item in list)
                {
                    bw.Write((byte)item.NodeType);
                }

                bw.Align(4);

                //values
                foreach (var item in list)
                {
                    if (NodeIsValue(item.NodeType))
                    {
                        WriteValue(ctx, item, bw);
                    }
                    else
                    {
                        tempOffs.Add(bw.Position);
                        bw.Write(0); //temp
                    }
                }

                //array/dictionary
                int index = 0;

                foreach (var item in list)
                {
                    if (!NodeIsValue(item.NodeType))
                    {
                        long curPos = bw.Position;
                        using (bw.TemporarySeek(tempOffs[index++], SeekOrigin.Begin))
                            bw.Write((int)curPos);

                        if (item.NodeType == ByamlNodeType.Array)
                        {
                            WriteArray(ctx, item, bw);
                        }
                        else if (item.NodeType == ByamlNodeType.Dictionary)
                        {
                            WriteDictionary(ctx, item, bw);
                        }
                    }
                }
            }
Ejemplo n.º 6
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "";
            openFileDialog1.Filter   = BYAML_FILTER;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var ctx = new ByamlContext(openFileDialog1.FileName);

                fest = ctx.Deserialize <ByamlFestival>();

                updateControls();
            }
        }
Ejemplo n.º 7
0
            public static void WriteDictionary(ByamlContext ctx, ByamlEntry entry, BinaryDataWriter bw)
            {
                var dict = (Dictionary <string, ByamlEntry>)entry.Value;

                bw.Write((byte)entry.NodeType);
                bw.WriteInt24(dict.Count);

                List <long> tempOffs = new List <long>();

                //value
                foreach (var item in dict)
                {
                    bw.WriteInt24(Array.IndexOf(ctx.NameTable.ToArray(), item.Key));
                    bw.Write((byte)item.Value.NodeType);

                    if (NodeIsValue(item.Value.NodeType))
                    {
                        WriteValue(ctx, item.Value, bw);
                    }
                    else
                    {
                        tempOffs.Add(bw.Position);
                        bw.Write(0); //temp
                    }
                }

                //array/dictionary
                int index = 0;

                foreach (var item in dict)
                {
                    if (!NodeIsValue(item.Value.NodeType))
                    {
                        long curPos = bw.Position;
                        using (bw.TemporarySeek(tempOffs[index++], SeekOrigin.Begin))
                            bw.Write((int)curPos);

                        if (item.Value.NodeType == ByamlNodeType.Array)
                        {
                            WriteArray(ctx, item.Value, bw);
                        }
                        else if (item.Value.NodeType == ByamlNodeType.Dictionary)
                        {
                            WriteDictionary(ctx, item.Value, bw);
                        }
                    }
                }
            }
Ejemplo n.º 8
0
            public static ByamlEntry ParseArray(ByamlContext ctx, BinaryDataReader br)
            {
                List <ByamlEntry> array = new List <ByamlEntry>();

                long startPos = br.Position;

                ByamlNodeType t = (ByamlNodeType)br.ReadByte();

                if (t != ByamlNodeType.Array)
                {
                    throw new ByamlException("Invalid Array");
                }

                int count = br.ReadInt24();

                List <ByamlNodeType> types = new List <ByamlNodeType>();

                for (int i = 0; i < count; i++)
                {
                    types.Add((ByamlNodeType)br.ReadByte());
                }

                br.Align(4);

                for (int i = 0; i < count; i++)
                {
                    if (!NodeIsValue(types[i]))
                    {
                        using (br.TemporarySeek(br.ReadUInt32(), SeekOrigin.Begin))
                            array.Add(ParseEntry(ctx, types[i], br));
                    }
                    else
                    {
                        array.Add(ParseEntry(ctx, types[i], br));
                    }
                }

                return(new ByamlEntry(t, array));
            }
Ejemplo n.º 9
0
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = "";
            saveFileDialog1.Filter   = BYAML_FILTER;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var ctx = ByamlContext.FromObject(fest);

                BymlOptionForm form = new BymlOptionForm();
                if (form.ShowDialog() == DialogResult.OK)
                {
                    ctx.BOM          = form.SelectedByteOrder;
                    ctx.ByamlVersion = form.SelectedVersion;

                    ctx.Write(saveFileDialog1.FileName);
                    SystemSounds.Asterisk.Play();
                }
                else
                {
                    SystemSounds.Hand.Play();
                }
            }
        }
Ejemplo n.º 10
0
            //read byaml
            public static ByamlEntry ParseStringTable(ByamlContext ctx, BinaryDataReader br)
            {
                long startPos = br.Position;

                ByamlNodeType type = (ByamlNodeType)br.ReadByte();

                if (type != ByamlNodeType.StringTable)
                {
                    throw new ByamlException("Invalid String table");
                }

                int         count = br.ReadInt24();
                List <uint> offs  = br.ReadUInt32s(count + 1).ToList();

                List <string> values = new List <string>();

                for (int i = 0; i < count; i++)
                {
                    br.Position = startPos + offs[i];
                    values.Add(Encoding.UTF8.GetString(br.ReadBytes((int)(offs[i + 1] - offs[i]))).Replace("\0", ""));
                }
                return(new ByamlEntry(type, values));
            }