Exemple #1
0
        /// <summary>
        /// Downloads config from specified Uri and compares version to runtime exe version.  Starts update if update is marked as 'Mandatory'.
        /// </summary>
        public PatchStatus InitializePatchStatus()
        {
            PatchStatus result = new PatchStatus();

            Status = result;

            //assemble accurate path and file names, check for exe existence, bail if not present
            RuntimeExeInfo rei = new RuntimeExeInfo(_settings.RuntimeExe);

            result.ExeInfo = rei;

            if (!rei.Exists)
            {
                SetLogMessage("Could not find file {0}; aborting.", true, rei.FullPath);
                return(result);
            }

            //this will always return a valid config, but it will be empty on load failure (CurrVer = 0.0.0.0)
            UpdateConfig uc = LoadConfig();

            result.PatchIsMandatory = uc.IsMandatory;

            if (uc.CurrentVer > rei.Version)
            {
                SetLogMessage("Current version is {0}, getting new version: {1}", rei.Version, uc.CurrentVersion);

                if (uc.LastMandatoryVer > rei.Version)
                {
                    result.PatchIsMandatory = true;
                    SetLogMessage("Current patch is Mandatory due to version age: {0} / {1}", rei.Version, uc.LastMandatoryVer);
                }

                #region ensure patch folders exist
                string downloadFolderRoot = _settings.DownloadFolder;
                if (!Directory.Exists(downloadFolderRoot))
                {
                    Directory.CreateDirectory(downloadFolderRoot);
                }

                Uri    patchUri = new Uri(uc.PatchUri);
                string fileName = patchUri.Segments[patchUri.Segments.Length - 1];

                string patchFolder = Path.Combine(downloadFolderRoot, Path.GetFileNameWithoutExtension(fileName));
                if (!Directory.Exists(patchFolder))
                {
                    Directory.CreateDirectory(patchFolder);
                }
                #endregion

                //download/copy the patch file
                string patchFilePath = Path.Combine(patchFolder, fileName);
                result.PatchIsValid = PatchFileIsValid(patchFilePath, uc.PatchSizeBytes);
                if (!result.PatchIsValid)
                {
                    result.PatchIsValid = GetPatchFile(patchUri, patchFilePath, uc.PatchSizeBytes);
                }
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Load the configuration file specified in this exe's config "UpdateConfigUri" setting.
        /// </summary>
        /// <returns>The initialized UpdateConfig.  Values will be empty if the a load failure occurs.</returns>
        public UpdateConfig LoadConfig()
        {
            Uri          configUri = new Uri(_settings.UpdateConfigUri);
            UpdateConfig uc        = new UpdateConfig();

            try
            {
                XmlDocument xmlDoc = new XmlDocument();

                if (configUri.IsUnc)
                {
                    SetLogMessage("Getting config via unc from {0}", configUri.LocalPath);
                    xmlDoc.Load(configUri.LocalPath);
                }
                else if (configUri.Scheme == __uriSchemeHttp)
                {
                    SetLogMessage("Getting config via http from {0}", configUri.AbsoluteUri);
                    Stream respStream = HttpRequestSync(configUri.AbsoluteUri);
                    if (respStream != null)
                    {
                        xmlDoc.Load(respStream);
                        respStream.Close();
                    }
                }
                else if (configUri.Scheme == __uriSchemeFtp)
                {
                    SetLogMessage("Getting file via ftp from {0}", configUri.AbsoluteUri);
                    Stream respStream = FtpRequestSync(configUri.AbsoluteUri);
                    if (respStream != null)
                    {
                        xmlDoc.Load(respStream);
                        respStream.Close();
                    }
                }
                else
                {
                    throw new UriFormatException("Unknown Uri format");
                }

                uc = UpdateConfig.Deserialize(new StringReader(xmlDoc.OuterXml));
            }
            catch (Exception ex)
            {
                SetLogMessage("Failed to retrieve config file: {0}.", ex, configUri);
            }

            return(uc);
        }