コード例 #1
0
        void RipMap(string P8Text, TmxMap ActiveMap, string TMXPath, string SpritePath)
        {
            string MapText = P8Text.Substring(P8Text.LastIndexOf("__map__") + "__map__".Length);

            MapText = MapText.Remove(MapText.IndexOf("__"));

            MapText = Regex.Replace(MapText, @"\t|\n|\r", "");

            // Map data in GFX section
            string GfxText = P8Text.Substring(P8Text.LastIndexOf("__gfx__") + "__gfx__".Length);

            GfxText = GfxText.Remove(GfxText.IndexOf("__"));

            GfxText = Regex.Replace(GfxText, @"\t|\n|\r", "");

            // Map data starts half way through gfx data.
            GfxText = GfxText.Remove(0, GfxText.Length / 2);

            string Temp = "";

            // Reverse
            for (int i = 0; i < GfxText.Length; i += 2)
            {
                string str       = GfxText.Substring(i, 2);
                char[] charArray = str.ToCharArray();
                Array.Reverse(charArray);
                Temp += new string(charArray);
            }
            GfxText = Temp;

            byte[] MapBytes = StringToByteArray(MapText + GfxText);

            string MapTmxString = "\n";

            for (int i = 0; i < MapBytes.Length; i++)
            {
                // +1 because Tiled uses index 1 to represent sprite 0.
                MapTmxString += (MapBytes[i] + 1).ToString();
                MapTmxString += ',';
                if (i != 0 && i % 128 == 0)
                {
                    MapTmxString += '\n';
                }
            }
            // Remove extra characters.
            ActiveMap.Layer.Data.MapData = MapTmxString.Remove(MapTmxString.LastIndexOf(',')) + "\n";

            ActiveMap.TileSet.Image.Source = SpritePath;

            // SPRITE FLAGS
            //

            // Data is stored in GFF section.
            string FlagText = P8Text.Substring(P8Text.LastIndexOf("__gff__") + "__gff__".Length);

            FlagText = FlagText.Remove(FlagText.IndexOf("__"));

            FlagText = Regex.Replace(FlagText, @"\t|\n|\r", "");
            byte[] FlagBytes = StringToByteArray(FlagText);

            // Store that flags. They are in order of the sprites.
            for (int i = 0; i < FlagBytes.Length; i++)
            {
                if (FlagBytes[i] != 0)
                {
                    ActiveMap.TileSet.TileList.Add(new TmxMap.TmxTileSet.TmxTile()
                    {
                        ID         = i,
                        Properties = new TmxMap.TmxTileSet.TmxTile.TmxTileProperties()
                        {
                            PropertyList = new System.Collections.Generic.List <TmxMap.TmxTileSet.TmxTile.TmxTileProperties.TmxTileProperty>()
                            {
                                new TmxMap.TmxTileSet.TmxTile.TmxTileProperties.TmxTileProperty()
                                {
                                    Value = FlagBytes[i].ToString()
                                }
                            }
                        }
                    });
                }
            }

            // Save the map out to file.
            XmlSerializer           XmlSerial = new XmlSerializer(ActiveMap.GetType());
            XmlSerializerNamespaces NS        = new XmlSerializerNamespaces();

            NS.Add("", ""); // Remove namespace tags: https://stackoverflow.com/questions/625927/omitting-all-xsi-and-xsd-namespaces-when-serializing-an-object-in-net

            var        Path = TMXPath;
            FileStream File = System.IO.File.Create(Path);

            using (StreamWriter sw = new StreamWriter(File, Encoding.GetEncoding("UTF-8")))
            {
                XmlSerial.Serialize(sw, ActiveMap, NS);
            }
        }