Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            var  mode     = Mode.Unknown;
            bool showHelp = false;

            var options = new OptionSet()
            {
                { "stb", "convert XML to STB", v => mode = v != null ? Mode.ToStb : mode },
                { "xml", "convert STB to XML", v => mode = v != null ? Mode.ToXml : mode },
                { "h|help", "show this message and exit", v => showHelp = v != null },
            };

            List <string> extras;

            try
            {
                extras = options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("{0}: ", GetExecutableName());
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName());
                return;
            }

            if (mode == Mode.Unknown &&
                extras.Count >= 1)
            {
                var extension = Path.GetExtension(extras[0]);

                if (extension == ".stb")
                {
                    mode = Mode.ToXml;
                }
                else if (extension == ".xml")
                {
                    mode = Mode.ToStb;
                }
            }

            if (showHelp == true ||
                mode == Mode.Unknown ||
                extras.Count < 1 ||
                extras.Count > 2)
            {
                Console.WriteLine("Usage: {0} [OPTIONS]+ input [output]", GetExecutableName());
                Console.WriteLine();
                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                return;
            }

            if (mode == Mode.ToStb)
            {
                string inputPath  = extras[0];
                string outputPath = extras.Count > 1
                                        ? extras[1]
                                        : Path.ChangeExtension(inputPath, null) + "_converted.stb";

                var culture = CultureInfo.InvariantCulture;
                var stb     = new StageBinaryFile();
                using (var input = File.OpenRead(inputPath))
                {
                    var doc  = new XPathDocument(input);
                    var nav  = doc.CreateNavigator();
                    var root = nav.SelectSingleNode("/stage");
                    if (root == null)
                    {
                        throw new InvalidOperationException();
                    }

                    var rawRooms = root.Select("room");
                    var rooms    = new List <StageBinaryFile.Room>();
                    while (rawRooms.MoveNext() == true)
                    {
                        var rawRoom = rawRooms.Current;
                        var room    = new StageBinaryFile.Room();
                        ParseAttribute(rawRoom, "type", out room.Type, culture);
                        ParseAttribute(rawRoom, "variant", out room.Variant, culture);
                        ParseAttribute(rawRoom, "name", out room.Name);
                        ParseAttribute(rawRoom, "difficulty", out room.Difficulty, culture);
                        ParseAttribute(rawRoom, "weight", out room.Weight, culture);
                        ParseAttribute(rawRoom, "width", out room.Width, culture);
                        ParseAttribute(rawRoom, "height", out room.Height, culture);

                        var rawDoors = rawRoom.Select("door");
                        var doors    = new List <StageBinaryFile.Door>();
                        while (rawDoors.MoveNext() == true)
                        {
                            var rawDoor = rawDoors.Current;
                            var door    = new StageBinaryFile.Door();
                            ParseAttribute(rawDoor, "x", out door.X, culture);
                            ParseAttribute(rawDoor, "y", out door.Y, culture);
                            ParseAttribute(rawDoor, "exists", out door.Exists);
                            doors.Add(door);
                        }
                        room.Doors = doors.ToArray();

                        var rawSpawns = rawRoom.Select("spawn");
                        var spawns    = new List <StageBinaryFile.Spawn>();
                        while (rawSpawns.MoveNext() == true)
                        {
                            var rawSpawn = rawSpawns.Current;
                            var spawn    = new StageBinaryFile.Spawn();
                            ParseAttribute(rawSpawn, "x", out spawn.X, culture);
                            ParseAttribute(rawSpawn, "y", out spawn.Y, culture);

                            var rawEntities = rawSpawn.Select("entity");
                            var entities    = new List <StageBinaryFile.Entity>();
                            while (rawEntities.MoveNext() == true)
                            {
                                var rawEntity = rawEntities.Current;
                                var entity    = new StageBinaryFile.Entity();
                                ParseAttribute(rawEntity, "type", out entity.Type, culture);
                                ParseAttribute(rawEntity, "variant", out entity.Variant, culture);
                                ParseAttribute(rawEntity, "subtype", out entity.Subtype, culture);
                                ParseAttribute(rawEntity, "weight", out entity.Weight, culture);
                                entities.Add(entity);
                            }
                            spawn.Entities = entities.ToArray();

                            spawns.Add(spawn);
                        }
                        room.Spawns = spawns.ToArray();
                        rooms.Add(room);
                    }

                    stb.Rooms.Clear();
                    stb.Rooms.AddRange(rooms);
                }

                using (var output = File.Create(outputPath))
                {
                    stb.Serialize(output);
                    output.Flush();
                }
            }
            else if (mode == Mode.ToXml)
            {
                string inputPath  = extras[0];
                string outputPath = extras.Count > 1
                                        ? extras[1]
                                        : Path.ChangeExtension(inputPath, null) + "_converted.xml";

                var stb = new StageBinaryFile();
                using (var input = File.OpenRead(inputPath))
                {
                    stb.Deserialize(input);
                }

                var settings = new XmlWriterSettings
                {
                    Encoding           = Encoding.UTF8,
                    Indent             = true,
                    OmitXmlDeclaration = true
                };

                var culture = CultureInfo.InvariantCulture;
                using (var writer = XmlWriter.Create(outputPath, settings))
                {
                    writer.WriteStartDocument();
                    writer.WriteComment("Converted to ANM2 by Gibbed.Rebirth.ConvertStage");

                    writer.WriteStartElement("stage");

                    foreach (var room in stb.Rooms)
                    {
                        writer.WriteStartElement("room");
                        writer.WriteAttributeString("type", room.Type.ToString(culture));
                        writer.WriteAttributeString("variant", room.Variant.ToString(culture));
                        writer.WriteAttributeString("name", room.Name);
                        writer.WriteAttributeString("difficulty", room.Difficulty.ToString(culture));
                        writer.WriteAttributeString("weight", room.Weight.ToString(culture));
                        writer.WriteAttributeString("width", room.Width.ToString(culture));
                        writer.WriteAttributeString("height", room.Height.ToString(culture));

                        foreach (var door in room.Doors)
                        {
                            writer.WriteStartElement("door");
                            writer.WriteAttributeString("x", door.X.ToString(culture));
                            writer.WriteAttributeString("y", door.Y.ToString(culture));

                            if (door.Exists == true)
                            {
                                writer.WriteAttributeString("exists", door.Exists.ToString(culture));
                            }

                            writer.WriteEndElement();
                        }

                        foreach (var spawn in room.Spawns)
                        {
                            writer.WriteStartElement("spawn");
                            writer.WriteAttributeString("x", spawn.X.ToString(culture));
                            writer.WriteAttributeString("y", spawn.Y.ToString(culture));

                            foreach (var entity in spawn.Entities)
                            {
                                writer.WriteStartElement("entity");
                                writer.WriteAttributeString("type", entity.Type.ToString(culture));
                                writer.WriteAttributeString("variant", entity.Variant.ToString(culture));
                                writer.WriteAttributeString("subtype", entity.Subtype.ToString(culture));
                                writer.WriteAttributeString("weight", entity.Weight.ToString(culture));
                                writer.WriteEndElement();
                            }

                            writer.WriteEndElement();
                        }

                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
Ejemplo n.º 2
0
        private void doLayout(Rectangle newRectangle)
        {
            this.ControlsPanel.SuspendLayout();
            this.SuspendLayout();

            this.tabs.Location = new Point(0, 0);
            this.tabs.Size     = new Size(newRectangle.Width, newRectangle.Height);

            this.mainPanel.Location = new Point(0, 0);
            this.mainPanel.Size     = new Size(tabs.Size.Width, tabs.Size.Height);

            this.ControlsPanel.Location = new Point((int)(mainPanel.Size.Width * 0.8f), 0);
            this.ControlsPanel.Size     = new Size((int)(mainPanel.Size.Width * 0.2f), mainPanel.Size.Height);
            this.LoadButton.Location    = new Point(0, 0);
            this.LoadButton.Size        = new Size(ControlsPanel.Size.Width / 2, 25);
            this.saveButton.Location    = new Point(ControlsPanel.Size.Width / 2, 0);
            this.saveButton.Size        = new Size(ControlsPanel.Size.Width / 2, 25);
            this.roomComboBox.Location  = new Point(0, 30);
            this.roomComboBox.Size      = new Size(ControlsPanel.Size.Width, 15);
            this.EntityBox.Location     = new Point(0, 50);
            this.EntityBox.Size         = new Size(ControlsPanel.Size.Width, 15);
            this.LabelType.Location     = new Point(0, 75);
            this.LabelType.Size         = new Size(ControlsPanel.Size.Width / 3, 15);
            this.LabelVariant.Location  = new Point(ControlsPanel.Size.Width / 3, 75);
            this.LabelVariant.Size      = new Size(ControlsPanel.Size.Width / 3, 15);
            this.LabelSubtype.Location  = new Point(ControlsPanel.Size.Width / 3 * 2, 75);
            this.LabelSubtype.Size      = new Size(ControlsPanel.Size.Width / 3, 15);


            this.TextboxType.Size        = new Size(ControlsPanel.Size.Width / 3, 20);
            this.TextboxType.Location    = new Point(0, 90);
            this.TextboxVariant.Size     = new Size(ControlsPanel.Size.Width / 3, 20);
            this.TextboxVariant.Location = new Point(ControlsPanel.Size.Width / 3, 90);
            this.TextboxSubType.Size     = new Size(ControlsPanel.Size.Width / 3, 20);
            this.TextboxSubType.Location = new Point(ControlsPanel.Size.Width / 3 * 2, 90);


            this.addButton.Location         = new Point(0, 120);
            this.addButton.Size             = new Size(ControlsPanel.Size.Width / 2, 30);
            this.RemoveButton.Location      = new Point(ControlsPanel.Size.Width / 2, 120);
            this.RemoveButton.Size          = new Size(ControlsPanel.Size.Width / 2, 30);
            this.tileEntityListBox.Location = new Point(0, 150);
            this.tileEntityListBox.Size     = new Size(ControlsPanel.Size.Width, ControlsPanel.Height - 190);
            this.WeightButton.Location      = new Point(0, ControlsPanel.Height - 20);
            this.WeightButton.Size          = new Size(ControlsPanel.Width, 20);
            this.weightTextBox.Location     = new Point(0, ControlsPanel.Height - 40);
            this.weightTextBox.Size         = new Size(ControlsPanel.Width, 20);
            this.TilePanel.Location         = new Point(10, 10);
            this.TilePanel.Size             = new Size((int)(mainPanel.Size.Width * 0.8f - 20), mainPanel.Size.Height - 20);
            //this.lrToggleButton.Location = new Point(12, 411);
            //this.lrToggleButton.Size = new Size(351, 59);
            //this.udToggleButton.Location = new Point(369, 411);
            //this.udToggleButton.Size = new Size(357, 59);
            if (loadedFile != null)
            {
                if (!CurrentRoom.Equals(loadedFile.Rooms [this.roomComboBox.SelectedIndex]))
                {
                    CurrentRoom = loadedFile.Rooms[this.roomComboBox.SelectedIndex];
                    this.TilePanel.Controls.Clear();
                    this.editorButtons = new Button[CurrentRoom.Width * CurrentRoom.Height];
                    for (int i = 0; i < CurrentRoom.Width; i++)
                    {
                        for (int j = 0; j < CurrentRoom.Height; j++)
                        {
                            int index = i + j * CurrentRoom.Width;
                            this.editorButtons [index] = new Button();
                            this.TilePanel.Controls.Add(this.editorButtons [index]);
                            this.editorButtons [index].Click += this.OnClick;
                            this.editorButtons [index].Name   = i + "," + j;
                            this.editorButtons [index].SetBounds(
                                i * TilePanel.Width / 13,
                                j * TilePanel.Width / 13,
                                TilePanel.Width / 13,
                                TilePanel.Width / 13);
                            this.editorButtons[index].BackColor = Color.White;
                        }
                    }
                }
                else
                {
                    CurrentRoom = loadedFile.Rooms[this.roomComboBox.SelectedIndex];
                    for (int i = 0; i < CurrentRoom.Width; i++)
                    {
                        for (int j = 0; j < CurrentRoom.Height; j++)
                        {
                            int index = i + j * CurrentRoom.Width;
                            this.editorButtons [index].SetBounds(
                                i * TilePanel.Width / 13,
                                j * TilePanel.Width / 13,
                                TilePanel.Width / 13,
                                TilePanel.Width / 13);
                            this.editorButtons[index].BackColor = Color.White;
                        }
                    }
                }
            }

            this.ControlsPanel.ResumeLayout(false);
            this.ControlsPanel.PerformLayout();
            this.ResumeLayout(false);
        }