Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UpdateClientSettings"/> class.
        /// </summary>
        /// <param name="targetPath">The path that the updated files should ultimately be located. This is usually the same directory
        /// that the program is running in.</param>
        /// <param name="settingsPath">The path to the directory containing the settings.</param>
        /// <param name="resetAppPath">The full path to the application that will be run if this program ever needs to reset
        /// itself, such as to update itself. This is typically the path to the current application.</param>
        public UpdateClientSettings(string targetPath, string settingsPath, string resetAppPath)
        {
            TargetPath   = targetPath;
            ResetAppPath = resetAppPath;

            // Set the default values
            TempPath                = PathHelper.CombineDifferentPaths(settingsPath, "_temp");
            LocalFileServerPath     = PathHelper.CombineDifferentPaths(settingsPath, MasterServerReader.CurrentDownloadSourcesFilePath);
            LocalMasterServerPath   = PathHelper.CombineDifferentPaths(settingsPath, MasterServerReader.CurrentMasterServersFilePath);
            OfflineFileReplacerPath = PathHelper.CombineDifferentPaths(settingsPath, GlobalSettings.ReplacerFileName);
            VersionFilePath         = PathHelper.CombineDifferentPaths(settingsPath, "current_version");
        }
Beispiel #2
0
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Form.Load"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data. </param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var targetDir    = Application.StartupPath;
            var settingsPath = PathHelper.CombineDifferentPaths(Application.StartupPath, "UpdaterSettings");
            var resetAppPath = Application.ExecutablePath;
            var settings     = new UpdateClientSettings(targetDir, settingsPath, resetAppPath);

            _uc = new UpdateClient(settings);
            _uc.StateChanged            += _uc_StateChanged;
            _uc.LiveVersionFound        += _uc_LiveVersionFound;
            _uc.FileDownloaded          += _uc_FileDownloaded;
            _uc.FileDownloadFailed      += _uc_FileDownloadFailed;
            _uc.FileMoveFailed          += _uc_FileMoveFailed;
            _uc.MasterServerReaderError += _uc_MasterServerReaderError;
            _uc.IsRunningChanged        += _uc_IsRunningChanged;
            _uc.HasErrorsChanged        += _uc_HasErrorsChanged;

            _uc.Start();
        }
Beispiel #3
0
        /// <summary>
        /// Checks the files provided in the given <see cref="VersionFileList"/> and compares them to the local files
        /// to see what files need to be updated.
        /// </summary>
        /// <param name="vfl">The <see cref="VersionFileList"/> containing the files to be updated.</param>
        /// <returns>The relative paths to the files that need to be updated.</returns>
        IEnumerable <string> FindFilesToUpdate(VersionFileList vfl)
        {
            var ret = new List <string>();

            // Loop through each file
            foreach (var updateFileInfo in vfl.Files)
            {
                // Get the local file path
                var localFilePath = PathHelper.CombineDifferentPaths(Settings.TargetPath, updateFileInfo.FilePath);

                // If the file does not exist, add it
                if (!File.Exists(localFilePath))
                {
                    ret.Add(updateFileInfo.FilePath);
                    continue;
                }

                // Get the info for the local file
                try
                {
                    var localFileInfo = new FileInfo(localFilePath);

                    // If the size of the local file doesn't equal the size of the updated file, avoid
                    // checking the hash and just update it
                    if (localFileInfo.Length != updateFileInfo.Size)
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.DebugFormat(
                                "Update check on file `{0}`: File needs update - size mismatch (current: {1}, expected: {2}).",
                                updateFileInfo.FilePath, localFileInfo.Length, updateFileInfo.Size);
                        }

                        ret.Add(updateFileInfo.FilePath);
                        continue;
                    }

                    // File exists and is of the correct size, so compare the hash of the local file to the expected hash
                    var localFileHash = Hasher.GetFileHash(localFilePath);
                    if (!StringComparer.Ordinal.Equals(localFileHash, updateFileInfo.Hash))
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.DebugFormat(
                                "Update check on file `{0}`: File needs update - hash mismatch (current: {1}, expected: {2}).",
                                updateFileInfo.FilePath, localFileHash, updateFileInfo.Hash);
                        }

                        ret.Add(updateFileInfo.FilePath);
                        continue;
                    }
                }
                catch (IOException ex)
                {
                    const string errmsg =
                        "Failed to analyze file `{0}` to see if it needs to be updated. Will assume update is required. Exception: {1}";

                    if (log.IsErrorEnabled)
                    {
                        log.ErrorFormat(errmsg, updateFileInfo.FilePath, ex);
                    }

                    Debug.Fail(string.Format(errmsg, updateFileInfo.FilePath, ex));

                    // On an IOException, assume the file needs to be updated
                    ret.Add(updateFileInfo.FilePath);
                    continue;
                }

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Update check on file `{0}`: file is up-to-date.", updateFileInfo.FilePath);
                }
            }

            if (log.IsInfoEnabled)
            {
                log.InfoFormat("Found `{0}` files needing to be updated.", ret.Count);
            }

            return(ret);
        }
Beispiel #4
0
 /// <summary>
 /// Gets the temporary path to use to download a remote file. The file is downloaded to here completely first before
 /// moving it to the real target path.
 /// </summary>
 /// <param name="remoteFile">The remote file being downloaded.</param>
 /// <returns>The temporary path for the <paramref name="remoteFile"/>.</returns>
 protected string GetTempPath(string remoteFile)
 {
     return(PathHelper.CombineDifferentPaths(TempPath, remoteFile));
 }
Beispiel #5
0
        /// <summary>
        /// Starts downloading a file.
        /// </summary>
        /// <param name="remoteFile">The file to download.</param>
        /// <param name="localFilePath">The complete file path that will be used to store the downloaded file.</param>
        /// <param name="version">The file version to download.</param>
        /// <returns>True if the download was successfully started; otherwise false.</returns>
        public bool Download(string remoteFile, string localFilePath, int?version)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Attempting Download on `{0}`. RemoteFile: {1}. LocalFilePath: {2}. Version: {3}", this,
                                remoteFile, localFilePath, version.HasValue ? version.Value.ToString() : "[NULL]");
            }

            var uriPath = RootPath;

            // Add the version directory if needed
            if (version.HasValue)
            {
                uriPath = PathHelper.CombineDifferentPaths(uriPath, PathHelper.GetVersionString(version.Value));
            }

            // Add the file to get to the path
            uriPath = PathHelper.CombineDifferentPaths(uriPath, remoteFile);

            var uri = new Uri(uriPath);

            // Ensure the directory exists
            var dir = Path.GetDirectoryName(localFilePath);

            if (dir != null)
            {
                if (!Directory.Exists(dir))
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Creating directory: {0}", dir);
                    }
                    Directory.CreateDirectory(dir);
                }
            }

            // Get the WebClient to use
            lock (_webClientsSync)
            {
                // Check for a free WebClient
                if (_webClients.Count == 0)
                {
                    if (log.IsInfoEnabled)
                    {
                        log.InfoFormat(
                            "Could not start download on `{0}` since no WebClients are available. RemoteFile: {1}. LocalFilePath: {2}. Version: {3}",
                            this, remoteFile, localFilePath, version.HasValue ? version.Value.ToString() : "[NULL]");
                    }
                    return(false);
                }

                // Grab the free WebClient and start the download
                var wc = _webClients.Pop();
                wc.DownloadFileAsync(uri, localFilePath, new AsyncDownloadInfo(remoteFile, localFilePath));

                if (log.IsInfoEnabled)
                {
                    log.InfoFormat(
                        "Starting download on `{0}` using WebClient {4}. RemoteFile: {1}. LocalFilePath: {2}. Version: {3}", this,
                        remoteFile, localFilePath, version.HasValue ? version.Value.ToString() : "[NULL]", wc);
                }
            }

            return(true);
        }