Beispiel #1
0
        /// <summary>
        /// Saves current values in the <see cref="dataStore"/> to a xml file using Linq.
        /// </summary>
        private void Write()
        {
            // get path to settings file
            var directoryName = Path.GetDirectoryName(this.FileName);

            // check to ensure that the directory exists
            if (directoryName != null && !Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            // create a XmlDocument to store the settings in
            var doc         = new XmlDocument();
            var declaration = doc.CreateXmlDeclaration("1.0", null, null);

            // create root settings element
            var settings = doc.CreateElement("settings");

            doc.AppendChild(settings);
            doc.InsertBefore(declaration, doc.DocumentElement);

            // read existing settings file values
            var existingValues = XmlDocumentSettingsHelpers.ReadSettings(this.FileName, true);

            // setup a comparer and merge the existing values from the xml file to the settings currently being managed
            var comparer =
                EqualityComparerCallback <KeyValuePair <string, object> > .Compare(
                    (x, y) => string.CompareOrdinal(x.Key, y.Key) == 0);

            var entries = this.dataStore.Union(existingValues, comparer);

            // create series of entries for each setting in the XmlDocument object
            var nodesToWrite = entries.OrderBy(x => x.Key).Select(
                x =>
            {
                var entry       = doc.CreateElement("entry");
                entry.InnerText = x.Value.ToString();
                var key         = doc.CreateAttribute("key");
                key.InnerText   = x.Key;
                entry.Attributes.Append(key);
                return(entry);
            });

            // add nodes to settings root node
            foreach (var node in nodesToWrite)
            {
                settings.AppendChild(node);
            }

            // save the XmlDocument to an xml file
            doc.Save(this.FileName);

            // if we are not compiling against the unity web player then record the last write time
#if !UNITY_WEBPLAYER
            this.lastWriteTime = File.GetLastWriteTime(this.FileName);
#endif
        }
Beispiel #2
0
        /// <summary>
        /// Reads values into the <see cref="dataStore"/> field using Linq.
        /// </summary>
        /// <exception cref="FileNotFoundException">
        /// If the xml file where settings are saved no longer exists.
        /// </exception>
        /// <exception cref="FileLoadException">
        /// If the xml settings file could not be read properly.
        /// </exception>
        private void Read()
        {
#if !UNITY_WEBPLAYER
            // only update reading settings file every 5 seconds
            if (DateTime.Now < this.lastReadTime + TimeSpan.FromSeconds(this.readDelayInSeconds))
            {
                return;
            }
#endif

            // check if the file exists
            if (!File.Exists(this.FileName))
            {
                throw new FileNotFoundException("Could not find settings file.", this.FileName);
            }

            // if we are not compiling against the unity web player then check if last write time is greater then the files write time
#if !UNITY_WEBPLAYER
            var info      = new FileInfo(this.FileName);
            var writeTime = info.LastWriteTime;

            // check if the file has been written to since last read attempt
            if (writeTime <= this.lastWriteTime)
            {
                return;
            }
#endif

            // try to read settings
            var results = XmlDocumentSettingsHelpers.ReadSettings(this.FileName, true);

            // store the settings
            this.dataStore = results.ToDictionary(k => k.Key, v => v.Value);

            // if we are not compiling against the unity web player then record the write time
#if !UNITY_WEBPLAYER
            this.lastWriteTime = writeTime;
            this.lastReadTime  = DateTime.Now;
#endif
        }