Ejemplo n.º 1
0
        /// <summary>
        /// Read a node tree from a stream</summary>
        /// <param name="uri">URI of stream</param>
        /// <param name="bReadTempSettings">Whether to additionally read from the project temporary settings file</param>
        /// <returns>Node tree, created from stream</returns>
        public DomNode Read(Uri uri, bool bReadTempSettings)
        {
            DomNode rootProject = null;

            try
            {
                const int attempts = 5;
                const int waitMs   = 1000;

                // Read project settings file
                using (var stream = SledUtil.TryOpen(
                           uri.LocalPath,
                           FileMode.Open,
                           FileAccess.Read,
                           FileShare.Read,
                           attempts,
                           waitMs))
                {
                    rootProject = ReadInternal(stream);
                }
            }
            catch (Exception ex)
            {
                SledOutDevice.OutLine(
                    SledMessageType.Error,
                    "Encountered an error when " +
                    "loading project file! {0}",
                    ex.Message);
            }

            if (rootProject == null)
            {
                return(null);
            }

            // Should we read the project
            // temporary settings file?
            if (!bReadTempSettings)
            {
                return(rootProject);
            }

            // Read from project temporary settings file
            DomNode rootTemp = null;

            try
            {
                // Path to project file
                var szAbsProjPath = uri.LocalPath;
                // Path to hidden project temporary settings file
                var szAbsTempPath = Path.ChangeExtension(szAbsProjPath, ".sus");

                if (!File.Exists(szAbsTempPath))
                {
                    return(rootProject);
                }

                // Read project temporary settings file
                using (var stream = new FileStream(szAbsTempPath, FileMode.Open, FileAccess.Read))
                {
                    rootTemp = ReadInternal(stream);
                }
            }
            catch (Exception ex)
            {
                SledOutDevice.OutLine(
                    SledMessageType.Error,
                    "Encountered an error when loading " +
                    "project temporary settings file! {0}",
                    ex.Message);
            }

            // If null then fall back to project file
            if (rootTemp == null)
            {
                return(rootProject);
            }

            foreach (var copier in s_lstCopiers)
            {
                try
                {
                    copier.Run(rootProject, rootTemp);
                }
                catch (Exception ex)
                {
                    SledOutDevice.OutLine(
                        SledMessageType.Error,
                        "Encountered an error when syncing " +
                        "user settings file to project file! {0}",
                        ex.Message);
                }
            }

            return(rootProject);
        }