Ejemplo n.º 1
0
        public void SaveChanges(string newXmlFilePath, string rootDirectory, List<string> list)
        {
            if(_additionalData==null)
                _additionalData = new XElement("additionalData");

            var xCfg = _additionalData.Element("configuration");
            if(xCfg==null)
            {
                xCfg = new XElement("configuration");
                _additionalData.Add(xCfg);
            }

            var xRootDir = xCfg.Element("rootDirectory");
            if(xRootDir==null)
            {
                xRootDir=new XElement("rootDirectory");
                xCfg.Add(xRootDir);
            }

            string rootDirRelative = string.Empty;
            if(!string.IsNullOrEmpty(RootDirectory))
            {
                var newDir = Path.GetFullPath(Path.GetDirectoryName(newXmlFilePath));
                rootDirRelative = PathHelper.GetRelativePath(newDir, RootDirectory);
            }
            xRootDir.Value = rootDirRelative;

            var xCharacters = _additionalData.Element("characters");
            if(xCharacters!=null)
                xCharacters.Remove();

            xCharacters = new XElement("characters");
            var xProperties = new XElement("characterProperties");
            var map = _dialogs.AttributeCollection.WriteXml(xProperties);
            xCharacters.Add(xProperties);

            var tmp = new XElement("characters");
            xCharacters.Add(tmp);
            xCharacters = tmp;

            foreach (var ch in _dialogs.Characters.Values)
            {
                var xCh = new XElement("character");
                xCh.Add(new XAttribute("name", ch.Name));
                ch.WriteProperties(xCh, id => map[id]);
                xCharacters.Add(xCh);
            }

            _additionalData.Add(xCharacters);

            var xDialogs = _additionalData.Element("dialogs");
            if(xDialogs==null)
            {
                xDialogs = new XElement("dialogs");
                _additionalData.Add(xDialogs);
            }

            var dlgDict = (from xD in xDialogs.Elements("dialog")
                           let path = xD.Attribute("path")
                           where path != null && !string.IsNullOrEmpty(path.Value)
                           select new {Key = path.Value, Value = xD}).ToDictionary(pair => pair.Key, pair => pair.Value);

            xDialogs.RemoveAll();

            var ignoredDialogs = new HashSet<string>();
            if (list != null)
            {
                foreach (var key in _directoryDialogMapping.Keys)
                    ignoredDialogs.Add(key);
                foreach (var str in list.Select(s => s.ToLower()))
                    ignoredDialogs.Remove(str);
            }

            foreach(var pair in _directoryDialogMapping)
            {
                if(ignoredDialogs.Contains(pair.Key))
                    continue;

                XElement xDlg;
                if (!dlgDict.TryGetValue(pair.Key, out xDlg))
                    xDlg = new XElement("dialog");
                else
                    dlgDict.Remove(pair.Key);

                var dlg = pair.Value;
                if(dlg.HasChanges)
                {
                    xDlg.RemoveAll();
                    xDlg.Add(new XAttribute("path", pair.Key));
                    var pref = new UserPreferences(dlg);
                    var fPath = Path.Combine(rootDirectory, dlg.RelativePath + ".dlg");
                    var dirPath = Path.GetDirectoryName(fPath);
                    if (!Directory.Exists(dirPath))
                        Directory.CreateDirectory(dirPath);

                    pref.PrepareForWriting(xDlg, dlg);
                    dlg.Dialog.SaveToFile(fPath, pref);
                }

                xDialogs.Add(xDlg);
            }

            foreach(var pair in dlgDict)
            {
                string fullPath = Path.Combine(xRootDir.Value, pair.Key + ".dlg");
                if(File.Exists(fullPath))
                {
                    if (_dialogsToRemove.Contains(pair.Key))
                    {
                        try
                        {
                            File.Delete(fullPath);
                            _dialogsToRemove.Remove(pair.Key);
                        }
                        catch
                        {
                            //Nobody cares
                            Console.WriteLine(@"Unable to delete file '{0}'", fullPath);
                        }
                    }
                    else
                        xDialogs.Add(pair.Value);
                }
            }

            foreach(var dlgToRemove in _dialogsToRemove)
            {
                string path = Path.Combine(xRootDir.Value, dlgToRemove + ".dlg");
                if(!File.Exists(path))
                    continue;
                try
                {
                    File.Delete(path);
                }
                catch
                {
                    //Nobody cares
                    Console.WriteLine(@"Unable to delete file '{0}'", path);
                }
            }

            _dialogsToRemove.Clear();

            using (var fStream=new FileStream(newXmlFilePath,FileMode.Create,FileAccess.Write))
            {
                using(var writer=XmlWriter.Create(fStream,new XmlWriterSettings{Indent=true}))
                {
                    var xDoc = new XDocument(_additionalData);
                    xDoc.WriteTo(writer);
                }
            }

            XmlFile = newXmlFilePath;
            RootDirectory = rootDirectory;
            HasChanges = false;
        }
Ejemplo n.º 2
0
        public DialogObject GetOrCreateDialogObject(string relativePath)
        {
            string relPath = relativePath.ToLower();

            DialogObject result;
            if(_directoryDialogMapping.TryGetValue(relPath,out result))
                return result;

            result=new DialogObject();
            string fPath = null;
            if (RootDirectory != null)
                fPath = Path.Combine(RootDirectory, relPath + ".dlg");

            var changed = _dialogs.HasChanges;

            var dlgInfo = new DialogInfo(_dialogs);
            result.Dialog = dlgInfo;
            if (File.Exists(fPath))
            {
                var pref = new UserPreferences(result);
                var xDialog = GetDialogNode(relativePath);
                pref.PrepareForReading(xDialog ?? new XElement("dialog", new XAttribute("path", relativePath)));
                dlgInfo.LoadFromFile(fPath, pref);
            }
            result.RelativePath = relativePath;
            result.HasChanges = false;
            _dialogs.Add(dlgInfo);

            if(!changed)
                _dialogs.HasChanges = false;

            _directoryDialogMapping.Add(relPath, result);
            return result;
        }