HaloMap.Meta.Meta.SaveMetaToFile C# (CSharp) Method

SaveMetaToFile() public method

Used to export the current meta to a file.
public SaveMetaToFile ( string outputFileName, bool renameTag ) : string
outputFileName string The filename to save the Meta to.
renameTag bool The rename tag.
return string
        public string SaveMetaToFile(string outputFileName, bool renameTag)
        {
            string[] splitOut = outputFileName.Split('\\');
            // if the filename contains an extension, remove it from the split list
            if (splitOut[splitOut.Length - 1].Contains("."))
            {
                splitOut[splitOut.Length - 1] = splitOut[splitOut.Length - 1].Substring(0, splitOut[splitOut.Length - 1].IndexOf('.'));
            }
            string[] splitOrig = this.name.Split('\\');

            // use the starting path for the tag name...
            string tempName = string.Empty;
            for (int i = 0; i < splitOrig.Length; i++)
            {
                if (splitOrig[i] != splitOrig[splitOrig.Length - 1])
                    tempName += splitOrig[i];
                else
                    tempName += splitOut[splitOut.Length - 1];
                if (i < splitOrig.Length - 1)
                    tempName += "\\";
            }

            if (renameTag)
            {
                tempName = askForTagName(tempName);
                if (tempName == string.Empty)
                {
                    return this.name;
                }
            }
            outputFileName = outputFileName.Replace("<", "_");
            outputFileName = outputFileName.Replace(">", "_");

            // write memorysteam of meta to file
            BinaryWriter BW = new BinaryWriter(new FileStream(outputFileName, FileMode.Create));
            BW.BaseStream.Position = 0;
            BW.BaseStream.Write(this.MS.ToArray(), 0, this.size);
            BW.Flush();
            BW.Close();

            XmlTextWriter xtw = new XmlTextWriter(outputFileName + ".xml",  Encoding.Default);
            xtw.Formatting = Formatting.Indented;
            xtw.WriteStartElement("Meta");
            xtw.WriteAttributeString("TagType", this.type);
            xtw.WriteAttributeString("TagName", tempName);
            xtw.WriteAttributeString("Offset", this.offset.ToString());
            xtw.WriteAttributeString("Size", this.size.ToString());
            xtw.WriteAttributeString("Magic", this.magic.ToString());
            xtw.WriteAttributeString("Parsed", this.parsed.ToString());
            xtw.WriteAttributeString("Date", DateTime.Today.ToShortDateString());
            xtw.WriteAttributeString("Time", DateTime.Now.ToShortTimeString());
            xtw.WriteAttributeString("EntityVersion", "0.1");
            xtw.WriteAttributeString("Padding", this.padding.ToString());

            // xtw.
            for (int x = 0; x < this.items.Count; x++)
            {
                Item i = this.items[x];
                if (i.intagname == this.name)
                    i.intagname = tempName;
                switch (i.type)
                {
                    case ItemType.Reflexive:
                        Reflexive r = (Reflexive)i;
                        xtw.WriteStartElement("Reflexive");
                        xtw.WriteAttributeString("Description", r.description);
                        xtw.WriteAttributeString("Offset", r.offset.ToString());
                        xtw.WriteAttributeString("ChunkCount", r.chunkcount.ToString());
                        xtw.WriteAttributeString("ChunkSize", r.chunksize.ToString());
                        xtw.WriteAttributeString("Translation", r.translation.ToString());
                        xtw.WriteAttributeString("PointsToTagType", r.pointstotagtype);
                        xtw.WriteAttributeString("PointsToTagName", r.pointstotagname);
                        xtw.WriteAttributeString("TagType", r.intagtype);
                        xtw.WriteAttributeString("TagName", r.intagname);
                        xtw.WriteEndElement();
                        break;

                    case ItemType.Ident:
                        Ident id = (Ident)i;
                        xtw.WriteStartElement("Ident");
                        xtw.WriteAttributeString("Description", id.description);
                        xtw.WriteAttributeString("Offset", id.offset.ToString());
                        xtw.WriteAttributeString("PointsToTagType", id.pointstotagtype);
                        xtw.WriteAttributeString("PointsToTagName", id.pointstotagname);
                        xtw.WriteAttributeString("TagType", id.intagtype);
                        xtw.WriteAttributeString("TagName", id.intagname);
                        xtw.WriteEndElement();
                        break;
                    case ItemType.String:
                        String s = (String)i;
                        xtw.WriteStartElement("String");
                        xtw.WriteAttributeString("Description", s.description);
                        xtw.WriteAttributeString("Offset", s.offset.ToString());
                        xtw.WriteAttributeString("StringName", s.name);
                        xtw.WriteAttributeString("TagType", s.intagtype);
                        xtw.WriteAttributeString("TagName", s.intagname);
                        xtw.WriteEndElement();
                        break;
                }
                if (i.intagname == tempName)
                    i.intagname = this.name;
            }

            xtw.WriteEndElement();
            xtw.Flush();
            xtw.Close();
            if (this.rawType != RawDataContainerType.Empty)
            {
                this.raw.SaveRawToFile(outputFileName, this);
            }

            return tempName;
        }

Usage Example

Example #1
0
        /// <summary>
        /// The save recursive.
        /// </summary>
        /// <param name="outputFilePath">The output file path.</param>
        /// <param name="parsed">The parsed.</param>
        /// <param name="pb">The pb.</param>
        /// <remarks></remarks>
        public void SaveRecursive(string outputFilePath, bool parsed, ToolStripProgressBar pb)
        {
            ArrayList metas = RecursivelyLoadMetas(parsed, pb);

            #region Get name tag name
            // Contains the orignal meta name
            string             origName   = ((Meta)metas[0]).name;
            Globals.askForName metaRename = new Globals.askForName("Enter new name for Meta", "Select Exported tag name:", "&Export Recursively", origName, origName.Substring(origName.LastIndexOf('\\') + 1), true);
            if (metaRename.ShowDialog() == DialogResult.Cancel)
            {
                pb.Value = 0;
                return;
            }
            // Contains the changed meta name
            string newName = metaRename.getTextBoxValue();
            metaRename.Dispose();
            #endregion

            // If set to true, will rename any linked tags with the same tag names
            // eg. [eqip] objects\powerups\shotgun_ammo\shotgun_ammo renamed to objects\powerups\shotgun_ammo_max\shotgun_ammo_max
            //    would also have it's name changed in [hltm] & [mode]
            bool recursivelyRenameTags = false;
            if (newName != this.name)
            {
                if (MessageBox.Show("Do you wish to recursively rename similar tags?\n", "Rename recursively?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    recursivelyRenameTags = true;
                }
            }



            int          y  = newName.LastIndexOf("\\") + 1;
            StreamWriter SW = new StreamWriter(new FileStream(
                                                   outputFilePath + "\\" + newName.Substring(y, newName.Length - y) + ".info", FileMode.Create));
            for (int x = 0; x < metas.Count; x++)
            {
                Meta m = (Meta)metas[x];

                string temp1 = m.name;

                #region Recursive Renaming
                List <string> backup = new List <string>();
                // Backup all the items PointsToTagName values &
                // change these to reflect recursive renaming
                // Rename the first Meta
                if (x == 0 && !recursivelyRenameTags)
                {
                    temp1 = newName;
                }
                else if (recursivelyRenameTags && temp1 == origName)
                {
                    temp1 = newName;
                    for (int i = 0; i < m.items.Count; i++)
                    {
                        switch (m.items[i].type)
                        {
                        case ItemType.Ident:
                            backup.Add(((Ident)m.items[i]).pointstotagname);
                            if (((Ident)m.items[i]).pointstotagname == this.name)
                            {
                                ((Ident)m.items[i]).pointstotagname = newName;
                            }
                            break;

                        case ItemType.Reflexive:
                            backup.Add(((Reflexive)m.items[i]).pointstotagname);
                            if (((Reflexive)m.items[i]).pointstotagname == this.name)
                            {
                                ((Reflexive)m.items[i]).pointstotagname = newName;
                            }
                            break;

                        default:
                            backup.Add(string.Empty);
                            break;
                        }
                    }
                }
                #endregion

                // directory to create
                int    loc   = temp1.LastIndexOf("\\") + 1;
                string temp2 = outputFilePath + "\\" + temp1.Substring(0, loc);
                Directory.CreateDirectory(temp2);

                // name of meta
                string tempname = temp1.Substring(loc, temp1.Length - loc);
                string temp3    = temp2 + tempname + "." + m.type;
                temp3 = temp3.Replace("<", "_");
                temp3 = temp3.Replace(">", "_");
                m.SortItemsByOffset();
                m.SaveMetaToFile(temp3, false);

                #region Recursive Renaming Restore
                // Restore Items PointsToTagName values
                if (recursivelyRenameTags)
                {
                    for (int i = 0; i < m.items.Count; i++)
                    {
                        switch (m.items[i].type)
                        {
                        case ItemType.Ident:
                            if (((Ident)m.items[i]).pointstotagname == this.name)
                            {
                                ((Ident)m.items[i]).pointstotagname = backup[i];
                            }
                            break;

                        case ItemType.Reflexive:
                            if (((Reflexive)m.items[i]).pointstotagname == this.name)
                            {
                                ((Reflexive)m.items[i]).pointstotagname = backup[i];
                            }
                            break;
                        }
                    }
                    backup.Clear();
                }
                #endregion

                SW.WriteLine(temp3);
            }

            SW.Flush();
            SW.Close();

            // Reset Progress Bar
            pb.Value = 0;
        }