maps.txt modifying

maps.txt modifying

Postby joppiesaus » 24 Jun 2013, 19:16

Hello everyone,

I have a problem. I am making a plugin that let's you convert normal maps into lava maps.
And for that, you must insert a piece of XML code in maps.txt.
I am doing that with streamwriter and reader, because that allows you more options(custom code).

The code must be inserted after <Maps> in maps.txt.
But the string(the code) doesn't insert it after that. It even doesn't insert!
Here is the code:
Code: Select all
        public void saveToXML()
        {
            string path = string.Format(@"{0}\lava\maps.txt", Application.StartupPath);
            string all;
            string indx = "<Maps>"; // <-- that thing is the start of the XML code, after that it must be inserted.
            string finalXmlCode;

            //generate the code
            string XmlCode = System.Environment.NewLine + "  <Map name=\"" + levelName + "\" phase1=\"0\" phase2=\"0\" author=\"" + textBoxAuthor.Text + "\">" + System.Environment.NewLine
                + "    <Source block=\"" + textBoxStartingBlock + "\" x=\"" + textBox_x + "\" y=\"" + textBox_y + "\" z=\"" + textBox_z + "\" type=\"" + textBoxType + "\" delay=\"0\" />" + System.Environment.NewLine
                + "  </Map>";
            if (customCode)
            {
                finalXmlCode = XmlCode + System.Environment.NewLine + textBoxCustomCode;
            }
            else
            {
                finalXmlCode = XmlCode;
            }
            //read the thing, and set all.
            using (StreamReader read = new StreamReader(path))
            {
                all = read.ReadToEnd();
            }

            //here goes the magic wrong: it doesn't get inserted in the maps.txt
            int indxOf = all.IndexOf(indx);
            all.Insert(indxOf + indx.Length, finalXmlCode);
           
            //writing stuff
            using (StreamWriter write = new StreamWriter(path))
            {
                write.Write(all);
            }
        }


Please don't use this to make your own lava map converter and publish it before I made my converter.

Thanks for the help! <ok>
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: maps.txt modifying

Postby ismellike » 24 Jun 2013, 19:21

Looks really cool joppie.
I have not really done anything with xml serializer, but if you want you can look at the /add in the custom commands section which pretty much does the same thing, you can.
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: maps.txt modifying

Postby dzienny » 24 Jun 2013, 23:24

I highly advise using XDocument or XmlDocument class to perform an XML file editing. XDocument seems like a better choice of the two.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: maps.txt modifying

Postby joppiesaus » 25 Jun 2013, 12:30

dzienny wrote:I highly advise using XDocument or XmlDocument class to perform an XML file editing. XDocument seems like a better choice of the two.


I didn't use XML stuff, because it is harder, and inserting a piece of custom code is less easier.
Still you are right.

ismellike wrote:Looks really cool joppie.
I have not really done anything with xml serializer, but if you want you can look at the /add in the custom commands section which pretty much does the same thing, you can.


Thank you, your method is more easy.:)
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 3 guests

cron