Request .MAP TO .XML Coding issue

DiabloVodkaShot

LOMCN VIP
VIP
Feb 20, 2009
2,272
182
265
Hi Guys,

Been working on this with one or two others for a few weeks now, we are trying to get .map to convert to .xml.

I have exported all the .libs to images for testing purposes ( tiles objects etc). But when i run the scripts and output using the class's i get black tiles and things just dont seem to work out as they should, see screenshot.

Attached is my class scripts, i am not an amazing coder so have most likely made a silly mistake. Any help appreciated.

TMXCode.cs

Code:
using System;using System.Xml.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Map_Editor;
using System.Collections;
using System.IO;
using System.Drawing;


namespace Map_Editor
{
    [XmlRoot(ElementName = "tileset")]
    public class Tileset
    {
        [XmlAttribute(AttributeName = "firstgid")]
        public string Firstgid { get; set; }
        [XmlAttribute(AttributeName = "name")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName = "tilewidth")]
        public string Tilewidth { get; set; }
        [XmlAttribute(AttributeName = "tileheight")]
        public string Tileheight { get; set; }
        [XmlAttribute(AttributeName = "tilecount")]
        public string Tilecount { get; set; }
        [XmlAttribute(AttributeName = "columns")]
        public string Columns { get; set; }
        [XmlElement(ElementName = "tile")]
        public List<Tile> Tile { get; set; }
    }


    [XmlRoot(ElementName = "image")]
    public class Image
    {
        [XmlAttribute(AttributeName = "width")]
        public string Width { get; set; }
        [XmlAttribute(AttributeName = "height")]
        public string Height { get; set; }
        [XmlAttribute(AttributeName = "source")]
        public string Source { get; set; }
    }


    [XmlRoot(ElementName = "tile")]
    public class Tile
    {
        [XmlElement(ElementName = "image")]
        public Image Image { get; set; }
        [XmlAttribute(AttributeName = "id")]
        public string Id { get; set; }
    }


    [XmlRoot(ElementName = "data")]
    public class Data
    {
        [XmlAttribute(AttributeName = "encoding")]
        public string Encoding { get; set; }
    }


    [XmlRoot(ElementName = "layer")]
    public class Layer
    {
        [XmlElement(ElementName = "data")]
        public Data Data { get; set; }
        [XmlAttribute(AttributeName = "name")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName = "width")]
        public string Width { get; set; }
        [XmlAttribute(AttributeName = "height")]
        public string Height { get; set; }
    }


    [XmlRoot(ElementName = "map")]
    public class Map
    {
        [XmlElement(ElementName = "tileset")]
        public List<Tileset> Tileset { get; set; }
        [XmlElement(ElementName = "layer")]
        public List<Layer> Layer { get; set; }
        [XmlAttribute(AttributeName = "version")]
        public string Version { get; set; }
        [XmlAttribute(AttributeName = "orientation")]
        public string Orientation { get; set; }
        [XmlAttribute(AttributeName = "renderorder")]
        public string Renderorder { get; set; }
        [XmlAttribute(AttributeName = "width")]
        public string Width { get; set; }
        [XmlAttribute(AttributeName = "height")]
        public string Height { get; set; }
        [XmlAttribute(AttributeName = "tilewidth")]
        public string Tilewidth { get; set; }
        [XmlAttribute(AttributeName = "tileheight")]
        public string Tileheight { get; set; }
        [XmlAttribute(AttributeName = "nextobjectid")]
        public string Nextobjectid { get; set; }
    }


    public class TxmData
    {
        public int[,] backImages;
        public int[,] middleImages;
        public int[,] frontImages;
        public string[,] backLibrary;
        public string[,] middleLibrary;
        public string[,] frontLibrary;
        internal int[,] backIndex;
        internal int[,] middleIndex;
        internal int[,] frontIndex;


        ArrayList indexesBack = new ArrayList();
        ArrayList indexesMiddle = new ArrayList();
        ArrayList indexesFront = new ArrayList();
        ArrayList storageBack = new ArrayList();
        ArrayList storageMiddle = new ArrayList();
        ArrayList storageFront = new ArrayList();
        private int mapsize = 0;
        private Dictionary<int, int> mapBackValues = new Dictionary<int, int>();
        private Dictionary<int, int> mapMiddleValues = new Dictionary<int, int>();
        private Dictionary<int, int> mapFrontValues = new Dictionary<int, int>();


        private void ParseIndexes(TxmData arr, int size)
        {
            for (int x = 0; x < size; x++)
            {
                for (int y = 0; y < size; y++)
                {
                    if (indexesBack.Contains(arr.backImages[x, y]) == false)
                    {
                        if (Convert.ToInt32(arr.backImages[x, y]) > 0 && Convert.ToInt32(arr.backImages[x, y]) < 32768)
                            indexesBack.Add(arr.backImages[x, y]);
                    }
                    if (indexesMiddle.Contains(arr.middleImages[x, y]) == false)
                    {
                        if (Convert.ToInt32(arr.middleImages[x, y]) > 0 && Convert.ToInt32(arr.middleImages[x, y]) < 32768)
                            indexesMiddle.Add(arr.middleImages[x, y]);
                    }
                    if (indexesFront.Contains(arr.frontImages[x, y]) == false)
                    {
                        if (Convert.ToInt32(arr.frontImages[x, y]) > 0 && Convert.ToInt32(arr.frontImages[x, y]) < 32768)
                            indexesFront.Add(arr.frontImages[x, y]);
                    }
                }
            }


            indexesBack.Sort();
            indexesMiddle.Sort();
            indexesFront.Sort();
        }


        public void Serialize(List<Map> lista)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Map>));
            //TextWriter writer = new StreamWriter(@"C:\Xml.txt");
            using (TextWriter writers = new StreamWriter(@"C:\Users\Tihomir Losic\Documents\work\Mir\LibraryEditorplacement\Exported\map0.tmx"))
            {
                serializer.Serialize(writers, lista);
            }
        }


        private void MapValues(TxmData arr)
        {
            for (int x = 0; x < mapsize; x++)
            {
                for (int y = 0; y < mapsize; y++)
                {
                    if (storageBack.Contains(arr.backImages[x, y]) == false)
                        storageBack.Add(arr.backImages[x, y]);
                    if (storageMiddle.Contains(arr.middleImages[x, y]) == false)
                        storageMiddle.Add(arr.middleImages[x, y]);
                    if (storageFront.Contains(arr.frontImages[x, y]) == false)
                        storageFront.Add(arr.frontImages[x, y]);
                }
            }


            storageBack.Sort();
            storageMiddle.Sort();
            storageFront.Sort();


            for (int cnt = 0; cnt < storageBack.Count; cnt++)
            {
                mapBackValues.Add(cnt, Convert.ToInt32(storageBack[cnt]));
            }
            for (int cnt = 0; cnt < storageMiddle.Count; cnt++)
            {
                mapMiddleValues.Add(cnt, Convert.ToInt32(storageMiddle[cnt]));
            }
            for (int cnt = 0; cnt < storageFront.Count; cnt++)
            {
                mapFrontValues.Add(cnt, Convert.ToInt32(storageFront[cnt]));
            }
        }


        private void CreateBackCSV(TxmData data)
        {


            var myString = String.Empty;


            for (int x = 0; x < mapsize; x++)
            {
                for(int y = 0; y < mapsize; y++)
                {
                    var myKey = mapBackValues.Where(kvp => kvp.Value == data.backImages[x, y]).Select(kvp => kvp.Key).FirstOrDefault();
                    data.backImages[x, y] = myKey;
                }
            }


            //var a = File.Open, FileMode.Create);
            using (StreamWriter outfile = new StreamWriter(@"C:\Users\Tihomir Losic\Documents\work\Mir\LibraryEditorplacement\Exported\map0back.csv"))
            { 
                for (int y = 0; y < mapsize; y++)
                {
                    string content = "";
                    for (int x = 0; x < mapsize; x++)
                    {
                        content += (data.backImages[x, y]+1).ToString()+ ",";
                    }
                    outfile.WriteLine(content);
                }
            }
        }


        private void CreateMiddleCSV(TxmData data)
        {


            var myString = String.Empty;


            for (int x = 0; x < mapsize; x++)
            {
                for (int y = 0; y < mapsize; y++)
                {
                    var myKey = mapMiddleValues.Where(kvp => kvp.Value == data.middleImages[x, y]).Select(kvp => kvp.Key).FirstOrDefault();
                    data.middleImages[x, y] = myKey;
                }
            }


            //var a = File.Open, FileMode.Create);
            using (StreamWriter outfile = new StreamWriter(@"C:\Users\Tihomir Losic\Documents\work\Mir\LibraryEditorplacement\Exported\map0middle.csv"))
            {
                for (int y = 0; y < mapsize; y++)
                {
                    string content = "";
                    for (int x = 0; x < mapsize; x++)
                    {
                        content += (data.middleImages[x, y] + 1).ToString() + ",";
                    }
                    outfile.WriteLine(content);
                }
            }
        }


        private void CreateFrontCSV(TxmData data)
        {


            var myString = String.Empty;


            for (int x = 0; x < mapsize; x++)
            {
                for (int y = 0; y < mapsize; y++)
                {
                    var myKey = mapFrontValues.Where(kvp => kvp.Value == data.frontImages[x, y]).Select(kvp => kvp.Key).FirstOrDefault();
                    data.frontImages[x, y] = myKey;
                }
            }


            //var a = File.Open, FileMode.Create);
            using (StreamWriter outfile = new StreamWriter(@"C:\Users\Tihomir Losic\Documents\work\Mir\LibraryEditorplacement\Exported\map0front.csv"))
            {
                for (int y = 0; y < mapsize; y++)
                {
                    string content = "";
                    for (int x = 0; x < mapsize; x++)
                    {
                        content += (data.frontImages[x, y] + 1).ToString() + ",";
                    }
                    outfile.WriteLine(content);
                }
            }
        }


        private int GetWidth(string path)
        {
            FileInfo file = new FileInfo(path);
            if(file.Exists)
            { 
            var sizeInBytes = file.Length;


            Bitmap img = new Bitmap(path);


            var imageWidth = img.Width;


            return imageWidth;
            }
            else
                return 96;
        }


        private int GetHeight(string path)
        {
            FileInfo file = new FileInfo(path);
            if (file.Exists)
            {
                var sizeInBytes = file.Length;


                Bitmap img = new Bitmap(path);


                var imageHeight = img.Height;


                return imageHeight;
            }
            else
                return 64;
        }


        public void CreateXML(TxmData input, int size)
        {
            mapsize = size;
            MapValues(input);
            ParseIndexes(input, size);
            CreateBackCSV(input);
            CreateMiddleCSV(input);
            CreateFrontCSV(input);


            var lengthBack = indexesBack.Count;
            var lengthMiddle = indexesMiddle.Count;
            var lengthFront = indexesFront.Count;
            var length = lengthFront + lengthMiddle + lengthBack;
            List<Map> maps = new List<Map>();


            Image img = new Image();
            Tile tl = new Tile();


            img.Source = "";
            img.Height = "";
            img.Width = "";
            Map map = new Map();
            map.Version = "1.0";
            map.Orientation = "orthogonal";
            map.Renderorder = "right-down";
            map.Width = size.ToString();
            map.Height = size.ToString();
            map.Tileheight = "32";
            map.Tilewidth = "48";
            map.Nextobjectid = 1.ToString();


            Tile[] backTiles = new Tile[lengthBack];
            Tile[] middleTiles = new Tile[lengthMiddle];
            Tile[] frontTiles = new Tile[lengthFront];


            List<Tileset> tsList = new List<Tileset>();


            Image[] backImages = new Image[lengthBack];
            Image[] middleImages = new Image[lengthMiddle];
            Image[] frontImages = new Image[lengthFront];


            Layer[] layers = new Layer[3];


            Data data = new Data();
            data.Encoding = "csv";


            for (int i = 0; i < lengthBack; i++)
            {
                backImages[i] = new Image();
                backImages[i].Height = "";
                backImages[i].Width = "";
                backImages[i].Source = "";


                backTiles[i] = new Tile();
                backTiles[i].Id = "";
                backTiles[i].Image = backImages[i];
            }


            for (int i = 0; i < lengthMiddle; i++)
            {
                middleImages[i] = new Image();
                middleImages[i].Height = "";
                middleImages[i].Width = "";
                middleImages[i].Source = "";


                middleTiles[i] = new Tile();
                middleTiles[i].Id = "";
                middleTiles[i].Image = middleImages[i];
            }


            for (int i = 0; i < lengthFront; i++)
            {
                frontImages[i] = new Image();
                frontImages[i].Height = "";
                frontImages[i].Width = "";
                frontImages[i].Source = "";


                frontTiles[i] = new Tile();
                frontTiles[i].Id = "";
                frontTiles[i].Image = frontImages[i];
            }


            for (int i = 0; i < layers.Length; i++)
            {
                layers[i] = new Layer();
                layers[i].Data = data;
                layers[i].Height = mapsize.ToString();
                layers[i].Width = mapsize.ToString();
                layers[i].Name = "";
            }


            // Filling xml feed
            layers[0].Name = "BackImage";
            layers[1].Name = "MiddleImage";
            layers[2].Name = "FrontImage";


            Tileset backTileSet = new Tileset();
            backTileSet.Columns = 0.ToString();
            backTileSet.Name = "backImageTileSet";
            backTileSet.Tilewidth = "1";
            backTileSet.Tileheight = "1";
            backTileSet.Firstgid = "1";
            backTileSet.Tilecount = length.ToString();
           
            // backimage tileset
            for (int i = 0; i < lengthMiddle; i++)
            {
                string path = "C://Users//Tihomir Losic//Documents//work//Mir//LibraryEditorplacement//Exported//Tiles//";
                path += indexesBack[i].ToString() + ".bmp";


                string filename = "Tiles/" + indexesBack[i].ToString() + ".bmp";


                backImages[i].Height = GetHeight(path).ToString();
                backImages[i].Width = GetWidth(path).ToString(); ;
                backImages[i].Source = filename;
            }


            for (int i = 0; i < lengthBack; i++)
            {
                backTiles[i].Id = (i+1).ToString();
                backTiles[i].Image = backImages[i];
            }


            backTileSet.Tile = backTiles.ToList();


            tsList.Add(backTileSet);


            // Middle Layer


            Tileset middleTileSet = new Tileset();
            middleTileSet.Columns = 0.ToString();
            middleTileSet.Name = "middleImageTileSet";
            middleTileSet.Tilewidth = "1";
            middleTileSet.Tileheight = "1";
            middleTileSet.Firstgid = "1";
            middleTileSet.Tilecount = length.ToString();


            for (int i = 0; i < lengthMiddle; i++)
            {
                string path = "C://Users//Tihomir Losic//Documents//work//Mir//LibraryEditorplacement//Exported//Smtiles//";
                path += indexesMiddle[i].ToString() + ".bmp";
                string filename = "Smtiles/" + indexesMiddle[i].ToString() + ".bmp";


                middleImages[i].Height = GetHeight(path).ToString();
                middleImages[i].Width = GetWidth(path).ToString();
                middleImages[i].Source = filename;
            }


            for (int i = 0; i < lengthMiddle; i++)
            {
                middleTiles[i].Id = (i + 1).ToString();
                middleTiles[i].Image = middleImages[i];
            }


            middleTileSet.Tile = middleTiles.ToList();


            tsList.Add(middleTileSet);


            // Front Layer


            Tileset frontTileSet = new Tileset();
            frontTileSet.Columns = 0.ToString();
            frontTileSet.Name = "frontImageTileSet";
            frontTileSet.Tilewidth = "1";
            frontTileSet.Tileheight = "1";
            frontTileSet.Firstgid = "1";
            frontTileSet.Tilecount = length.ToString();


            for (int i = 0; i < lengthFront; i++)
            {
                string path = "C://Users//Tihomir Losic//Documents//work//Mir//LibraryEditorplacement//Exported//Objects//";
                path += indexesFront[i].ToString() + ".bmp";
                string filename = "Objects/" + indexesFront[i].ToString() + ".bmp";


                frontImages[i].Height = GetHeight(path).ToString();
                frontImages[i].Width = GetWidth(path).ToString();
                frontImages[i].Source = filename;
            }


            for (int i = 0; i < lengthFront; i++)
            {
                frontTiles[i].Id = (i + 1).ToString();
                frontTiles[i].Image = frontImages[i];
            }


            frontTileSet.Tile = frontTiles.ToList();


            tsList.Add(frontTileSet);


            map.Tileset = tsList;
            map.Layer = layers.ToList();


            maps.Add(map);
            Serialize(maps);
        }
    }
}

There is a second part but Post too long.... wont let me post. Any help would be great.
 

Attachments

  • Screen Shot 2017-01-17 at 1.47.34 AM.jpg
    Screen Shot 2017-01-17 at 1.47.34 AM.jpg
    257.7 KB · Views: 57
Last edited:

DiabloVodkaShot

LOMCN VIP
VIP
Feb 20, 2009
2,272
182
265
Second bit of code:

TMX CodeCallerFunction.cs
private void LoadDataIntoTmx(CellInfo[,] m2CellInfo) {

var sizeMapCells = Convert.ToInt16(Math.Sqrt(m2CellInfo.Length));


helper = new TxmData();
tmxIndexData = new TxmData();
tmxIndexData.backImages = new int[sizeMapCells, sizeMapCells];
tmxIndexData.middleImages = new int[sizeMapCells, sizeMapCells];
tmxIndexData.frontImages = new int[sizeMapCells, sizeMapCells];
tmxIndexData.backLibrary = new string[sizeMapCells, sizeMapCells];
tmxIndexData.middleLibrary = new string[sizeMapCells, sizeMapCells];
tmxIndexData.frontLibrary = new string[sizeMapCells, sizeMapCells];
tmxIndexData.backIndex = new int[sizeMapCells, sizeMapCells];
tmxIndexData.middleIndex = new int[sizeMapCells, sizeMapCells];
tmxIndexData.frontIndex = new int[sizeMapCells, sizeMapCells];


for (int x = 0; x < sizeMapCells; x++)
{
for (int y = 0; y < sizeMapCells; y++)
{
// num. of images
tmxIndexData.backImages[x, y] = (m2CellInfo[x, y].BackImage & 0x1FFFF) - 1;
tmxIndexData.middleImages[x, y] = (m2CellInfo[x, y].MiddleImage & 0x1FFFF) - 1;
tmxIndexData.frontImages[x, y] = (m2CellInfo[x, y].FrontImage & 0x1FFFF) - 1;
// libs
tmxIndexData.backLibrary[x, y] = GetLibName(m2CellInfo[cellX, cellY].BackIndex);
tmxIndexData.middleLibrary[x, y] = GetLibName(m2CellInfo[cellX, cellY].MiddleIndex);
tmxIndexData.frontLibrary[x, y] = GetLibName(m2CellInfo[cellX, cellY].FrontIndex);
// indexes
tmxIndexData.backIndex[x, y] = m2CellInfo[cellX, cellY].BackIndex;
tmxIndexData.middleIndex[x, y] = m2CellInfo[cellX, cellY].MiddleIndex;
tmxIndexData.frontIndex[x, y] = m2CellInfo[cellX, cellY].FrontIndex;
}
}


//helper.CreateXML(tmxIndexData, sizeMapCells);
}
 
Upvote 0