Ejemplo n.º 1
0
        public MyGroupBox(Map m)
        {
            map = m;
            AutoSize = true;
            MinimumSize = new System.Drawing.Size(10, m.height);
            Text = m.name;

            radioDiff.Location = new System.Drawing.Point(8, 15);
            radioDiff.Text = "Difference";
            radioDiff.Checked = true;
            radioDiff.CheckedChanged += radioButton_CheckedChanged;
            Controls.Add(radioDiff);
            radioOld.Location = new System.Drawing.Point(8, 33);
            radioOld.Text = "Old";
            radioOld.CheckedChanged += radioButton_CheckedChanged;
            Controls.Add(radioOld);
            radioNew.Location = new System.Drawing.Point(8, 51);
            radioNew.Text = "New";
            radioNew.CheckedChanged += radioButton_CheckedChanged;
            Controls.Add(radioNew);

            if (map.isDifferent)
            {
                saveButton.Text = "Save";
                saveButton.Location = new System.Drawing.Point(8, 78);
                saveButton.Click += buttonSave_Click;
                Controls.Add(saveButton);
            }
            int width = map.oldMinimap == null ? map.minimap.Width
                                               : Math.Max(map.minimap.Width, map.oldMinimap.Width);
            int height = map.oldMinimap == null ? map.minimap.Height
                                                : Math.Max(map.minimap.Height, map.oldMinimap.Height);
            picbox.MinimumSize = new Size(width, height);
            picbox.AutoSize = true;
            picbox.ImageAlign = ContentAlignment.TopLeft;
            if (map.diffMinimap == null)
                picbox.Text = map.diffText;
            else
                picbox.Image = map.diffMinimap;
        }
Ejemplo n.º 2
0
        private static bool readLayer(Map map, int widthLayer, int heightLayer, Stream input)
        {
            for (int y = 0; y < heightLayer; y++)
            {
                for (int x = 0; x < widthLayer; x++)
                {
                    byte[] buffer = new byte[4];
                    input.Read(buffer, 0, 4);
                    uint number = BitConverter.ToUInt32(buffer, 0);

                    if (number == 0 || number > 0x20000000)
                        continue;
                    var order = map.tilesetList.Last(n => n.Key <= number);
                    Tile tile;
                    try
                    {
                        tile = order.Value.tiles[number - order.Key];
                    }
                    catch (Exception e)
                    {
                        Logger.bw.ReportProgress(1, "Don't have tile number " + number +
                                                    " in tileset with firstgid " + order.Key
                                                    + ". " + e.Message);
                        return false;
                    }
                    if (tile == null)
                    {
                        Logger.bw.ReportProgress(1, "Layer has invalid tile.");
                        return false;
                    }
                    tile.addToLayer(map.layers, x, y);
                }
            }
            return true;
        }
Ejemplo n.º 3
0
        internal static Map readTmx(string tmx)
        {
            XElement xml;
            int width;
            int height;
            int tilewidthOfMap;
            int tileheightOfMap;

            try
            {
                xml = XElement.Load(tmx);
            }
            catch (Exception e)
            {
                Logger.bw.ReportProgress(1, e.Message);
                return null;
            }

            try
            {
                width = Convert.ToInt32(xml.Attribute("width").Value);
                height = Convert.ToInt32(xml.Attribute("height").Value);
            }
            catch (Exception e)
            {
                Logger.bw.ReportProgress(1, "Map has no \"width\" or \"height\". " + e.Message);
                return null;
            }

            try
            {
                tilewidthOfMap = Convert.ToInt32(xml.Attribute("tilewidth").Value);
                tileheightOfMap = Convert.ToInt32(xml.Attribute("tileheight").Value);
            }
            catch (Exception e)
            {
                Logger.bw.ReportProgress(1, "Can't read tilewidth or tileheight. " + e.Message);
                return null;
            }
            if (tilewidthOfMap != 32 || tileheightOfMap != 32)
            {
                Logger.bw.ReportProgress(1, "Tilewidth and tileheight have to be 32x32.");
                return null;
            }

            Map map = new Map(width, height);

            foreach (XElement child in xml.Elements())
            {
                if (child.Name == "tileset")
                {
                    Tileset tileset;
                    uint firstgid;
                    string key;

                    try
                    {
                        firstgid = Convert.ToUInt32(child.Attribute("firstgid").Value);
                    }
                    catch (Exception e)
                    {
                        Logger.bw.ReportProgress(1, "Tileset has no \"firstgid\" attribute. " + e.Message);
                        return null;
                    }

                    var sourceAttribute = child.Attribute("source");
                    var imageElement = child.Element("image");
                    if (sourceAttribute != null) // external tileset
                    {
                        key = sourceAttribute.Value;
                        if (!tilesets.TryGetValue(key, out tileset))
                        {
                            XElement xmlExtern;
                            string path = Path.Combine(pathMaps, key);
                            try
                            {
                                xmlExtern = XElement.Load(path);
                            }
                            catch (Exception e)
                            {
                                Logger.bw.ReportProgress(1, "Problem loading tileset " + path + ": " + e.Message);
                                return null;
                            }
                            tileset = readTileset(xmlExtern, Path.GetDirectoryName(path));
                            if (tileset == null)
                                return null;
                            tilesets.Add(key, tileset);
                        }
                    }
                    else if (imageElement != null) // internal tileset
                    {
                        tileset = readTileset(child, Repo.pathMaps);
                        if (tileset == null)
                            return null;
                    }
                    else
                    {
                        Logger.bw.ReportProgress(1, "Tileset with firstgid " + firstgid + " has neither image nor source.");
                        return null;
                    }

                    map.tilesetList.Add(firstgid, tileset);
                }
                else if (child.Name == "layer")
                {
                    string name;
                    int widthLayer;
                    int heightLayer;
                    XElement data;
                    string encoding = "";
                    string compression = "";

                    try
                    {
                        name = child.Attribute("name").Value;
                    }
                    catch (Exception e)
                    {
                        Logger.bw.ReportProgress(1, "Layer has no \"name\" attribute. " + e.Message);
                        return null;
                    }
                    if (name.ToLowerInvariant() == "collision")
                        continue;

                    try
                    {
                        widthLayer = Convert.ToInt32(child.Attribute("width").Value);
                        heightLayer = Convert.ToInt32(child.Attribute("height").Value);
                    }
                    catch (Exception e)
                    {
                        Logger.bw.ReportProgress(1, "Layer \"" + name + "\" has no \"width\" or \"height\". " + e.Message);
                        return null;
                    }

                    try
                    {
                        data = child.Element("data");
                    }
                    catch (Exception e)
                    {
                        Logger.bw.ReportProgress(1, "Layer \"" + name + "\" has no \"data\" attribute. " + e.Message);
                        return null;
                    }
                    foreach (var a in data.Attributes())
                    {
                        if (a.Name == "encoding")
                            encoding = a.Value;
                        else if (a.Name == "compression")
                            compression = a.Value;
                    }

                    string dataString = data.Value;
                    if (encoding == "csv" && compression == "")
                    {
                        using (Stream numberStream = new MemoryStream())
                        {
                            foreach (string s in Regex.Split(dataString, ","))
                            {
                                uint number;
                                try
                                {
                                    number = uint.Parse(s, System.Globalization.NumberStyles.Integer);
                                }
                                catch (Exception e)
                                {
                                    Logger.bw.ReportProgress(1, "In Layer \"" + name + "\" can't parse number \"" + s + "\". "
                                                 + e.Message);
                                    return null;
                                }
                                byte[] ba = BitConverter.GetBytes(number);
                                numberStream.Write(ba, 0, 4);
                            }
                            numberStream.Position = 0;
                            if (!readLayer(map, widthLayer, heightLayer, numberStream))
                                return null;
                        }
                    }
                    else if (encoding == "base64" && compression == "gzip")
                    {
                        byte[] compressed = System.Convert.FromBase64String(dataString);
                        using (var compressedStream = new MemoryStream(compressed))
                        using (var decompressedStream = new GZipStream(compressedStream, CompressionMode.Decompress))
                        {
                            if (!readLayer(map, widthLayer, heightLayer, decompressedStream))
                                return null;
                        }
                    }
                    else if (encoding == "base64" && compression == "zlib")
                    {
                        byte[] compressed = System.Convert.FromBase64String(dataString);
                        using (MemoryStream output = new MemoryStream())
                        using (var compressedStream = new MemoryStream(compressed))
                        using (var zStream = new ZOutputStream(output))
                        {
                            CopyStream(compressedStream, zStream);
                            byte[] array = output.ToArray();
                            using (MemoryStream decompressedStream = new MemoryStream(array))
                            {
                                if (!readLayer(map, widthLayer, heightLayer, decompressedStream))
                                    return null;
                            }
                        }
                    }
                    else
                    {
                        Logger.bw.ReportProgress(1, "Layer \"" + name + "\" has unsupported encoding or compression.");
                        return null;
                    }
                }
            }
            return map;
        }