Exemplo n.º 1
0
        public bool Save(string pathToSaveFile, bool overwrite = false)
        {
            if (!IsValidPath(pathToSaveFile))
            {
                Debug.LogError("EgoXproject: Invalid Save Path: " + pathToSaveFile);
                return(false);
            }

            if (!overwrite)
            {
                if (_path != pathToSaveFile && File.Exists(pathToSaveFile))
                {
                    Debug.LogError("EgoXproject: File exists: " + pathToSaveFile);
                    return(false);
                }
            }

            XDocument doc = new XDocument(_decl);
            bool      dtd = false;

            if (_loaded)
            {
                if (_usedDocType != null)
                {
                    doc.AddFirst(_usedDocType);
                    dtd = true;
                }
            }
            else
            {
                doc.AddFirst(_docType);
                dtd = true;
            }

            XElement plist = new XElement("plist");

            plist.SetAttributeValue("version", _version);
            plist.Add(_dict.Xml());
            doc.Add(plist);

            var stringWriter = new Utf8StringWriter();

            doc.Save(stringWriter);
            var content = stringWriter.GetStringBuilder().ToString();

            //http://stackoverflow.com/questions/10303315/convert-stringwriter-to-string
            string[] separator = { System.Environment.NewLine };
            string[] lines     = content.Split(separator, StringSplitOptions.RemoveEmptyEntries);

            //Remove the spurious [] that get added to DOCTYPE. Grrr.
            if (dtd)
            {
                for (int ii = 0; ii < lines.Length; ii++)
                {
                    var line = lines[ii];

                    if (line.Contains("<!DOCTYPE") && line.Contains("[]"))
                    {
                        lines[ii] = line.Replace("[]", "");
                        break;
                    }
                }
            }

            File.WriteAllLines(pathToSaveFile, lines);
            //TODO this is a crap check.
            bool bSaved = File.Exists(pathToSaveFile);

            if (bSaved)
            {
                _path = pathToSaveFile;
            }

            return(bSaved);
        }