コード例 #1
0
 /// <summary>
 /// Install updates
 /// </summary>
 public void InstallUpdates()
 {
     if (updateData == null)
     {
         UpdateTaskFinished.Invoke(this, new UpdateTaskFinishedEventArgs(true, null));
     }
     else
     {
         WebClient wc = new WebClient();
         wc.DownloadProgressChanged += OnDownloadProgressChanged;
         wc.DownloadFileCompleted   += OnDownloadFileCompleted;
         try
         {
             if (!(Directory.Exists("./updates")))
             {
                 Directory.CreateDirectory("./updates");
             }
             wc.DownloadFileAsync(new Uri(updateData.uri), "./updates/" + updateData.version + ".zip");
         }
         catch (Exception e)
         {
             UpdateTaskFinished.Invoke(this, new UpdateTaskFinishedEventArgs(true, e.ToString()));
         }
     }
 }
コード例 #2
0
 /// <summary>
 /// Install updates
 /// </summary>
 public void InstallUpdates()
 {
     if (latestReleaseData == null)
     {
         UpdateTaskFinished.Invoke(this, new UpdateTaskFinishedEventArgs(true, null));
     }
     else
     {
         try
         {
             Uri       uri = new Uri(URI);
             WebClient wc  = new WebClient();
             wc.DownloadProgressChanged += OnDownloadProgressChanged;
             wc.DownloadFileCompleted   += OnDownloadFileCompleted;
             if (!(Directory.Exists("./updates")))
             {
                 Directory.CreateDirectory("./updates");
             }
             downloadPath = "./updates/" + Path.GetFileName(uri.LocalPath);
             wc.DownloadFileAsync(uri, downloadPath);
         }
         catch (Exception e)
         {
             UpdateTaskFinished.Invoke(this, new UpdateTaskFinishedEventArgs(true, e.ToString()));
         }
     }
 }
コード例 #3
0
        /// <summary>
        /// On download file completed
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Asynchronous operation event arguments</param>
        private void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            bool   is_canceled = e.Cancelled;
            string error       = ((e.Error == null) ? null : e.Error.ToString());

            if ((!is_canceled) && (error == null))
            {
                try
                {
                    // Verify checksum
                    string update_file_name = "./updates/" + updateData.version + ".zip";
                    string hash             = SHA512FromFile(update_file_name, ref error);
                    if (error == null)
                    {
                        if (hash.Length > 0)
                        {
                            if (hash == updateData.sha512)
                            {
                                // Backup old files
                                if (!(Directory.Exists("./backups")))
                                {
                                    Directory.CreateDirectory("./backups");
                                }
                                string backup_file_name = "./backups/pre." + updateData.version + ".zip";
                                try
                                {
                                    if (File.Exists(backup_file_name))
                                    {
                                        File.Delete(backup_file_name);
                                    }
                                    using (ZipArchive zip_archive = ZipFile.Open(backup_file_name, ZipArchiveMode.Create))
                                    {
                                        string[] file_names        = Directory.GetFiles(".", "*", SearchOption.AllDirectories);
                                        string   current_directory = Directory.GetCurrentDirectory();
                                        if (current_directory.Length > 0)
                                        {
                                            if (current_directory[current_directory.Length - 1] != Path.DirectorySeparatorChar)
                                            {
                                                current_directory += Path.DirectorySeparatorChar;
                                            }
                                        }
                                        foreach (string file_name in file_names)
                                        {
                                            string path = Path.GetFullPath(file_name);
                                            if (path.StartsWith(current_directory))
                                            {
                                                if (!(path.Contains("backups" + Path.DirectorySeparatorChar)))
                                                {
                                                    zip_archive.CreateEntryFromFile(path, path.Substring(current_directory.Length).Replace('\\', '/'));
                                                }
                                            }
                                        }
                                    }

                                    // Unzip updates
                                    try
                                    {
                                        using (ZipArchive zip_archive = ZipFile.Open(update_file_name, ZipArchiveMode.Read))
                                        {
                                            foreach (ZipArchiveEntry entry in zip_archive.Entries)
                                            {
                                                string entry_name = entry.FullName.Replace('\\', '/');
                                                using (Stream reader = entry.Open())
                                                {
                                                    try
                                                    {
                                                        using (FileStream file_stream = new FileStream("./" + entry_name, FileMode.Create))
                                                        {
                                                            reader.CopyTo(file_stream);
                                                        }
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Console.Error.WriteLine(ex);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        // Revert changes
                                        Console.Error.WriteLine(ex);
                                        try
                                        {
                                            if (File.Exists(backup_file_name))
                                            {
                                                using (ZipArchive zip_archive = ZipFile.Open(backup_file_name, ZipArchiveMode.Read))
                                                {
                                                    foreach (ZipArchiveEntry entry in zip_archive.Entries)
                                                    {
                                                        string entry_name = entry.FullName.Replace('\\', '/');
                                                        using (Stream reader = entry.Open())
                                                        {
                                                            try
                                                            {
                                                                using (FileStream file_stream = new FileStream("./" + entry_name, FileMode.Create))
                                                                {
                                                                    reader.CopyTo(file_stream);
                                                                }
                                                            }
                                                            catch (Exception exc)
                                                            {
                                                                Console.Error.WriteLine(exc);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        catch
                                        {
                                            is_canceled = true;
                                            error       = "Attempt on reverting changes failed.\r\n\r\nBackup file: " + backup_file_name;
                                        }
                                    }
                                }
                                catch
                                {
                                    is_canceled = true;
                                    error       = "Can't create backup from files.";
                                }
                            }
                            else
                            {
                                is_canceled = true;
                                error       = "SHA512 hash for file \"" + update_file_name + "\" is invalid.";
                            }
                        }
                    }
                    else
                    {
                        is_canceled = true;
                    }
                }
                catch (Exception ex)
                {
                    is_canceled = true;
                    error       = ex.ToString();
                }
            }
            UpdateTaskFinished.Invoke(this, new UpdateTaskFinishedEventArgs(is_canceled, error));
        }
コード例 #4
0
        /// <summary>
        /// On download file completed
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Asynchronous operation event arguments</param>
        private void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            bool   is_canceled = e.Cancelled;
            string error       = ((e.Error == null) ? null : e.Error.ToString());

            if ((!is_canceled) && (error == null))
            {
                try
                {
                    // Backup old files
                    if (!(Directory.Exists("./backups")))
                    {
                        Directory.CreateDirectory("./backups");
                    }
                    string backup_file_name = "./backups/pre." + Version + ".zip";
                    try
                    {
                        if (File.Exists(backup_file_name))
                        {
                            File.Delete(backup_file_name);
                        }
                        using (ZipArchive zip_archive = ZipFile.Open(backup_file_name, ZipArchiveMode.Create))
                        {
                            string[] file_names        = Directory.GetFiles(".", "*", SearchOption.AllDirectories);
                            string   current_directory = Directory.GetCurrentDirectory();
                            if (current_directory.Length > 0)
                            {
                                if (current_directory[current_directory.Length - 1] != Path.DirectorySeparatorChar)
                                {
                                    current_directory += Path.DirectorySeparatorChar;
                                }
                            }
                            foreach (string file_name in file_names)
                            {
                                string path = Path.GetFullPath(file_name);
                                if (path.StartsWith(current_directory))
                                {
                                    if (!(path.Contains("backups" + Path.DirectorySeparatorChar)))
                                    {
                                        zip_archive.CreateEntryFromFile(path, path.Substring(current_directory.Length).Replace('\\', '/'));
                                    }
                                }
                            }
                        }

                        // Move or unzip updates
                        try
                        {
                            if (downloadPath.ToLower().EndsWith(".zip"))
                            {
                                using (ZipArchive zip_archive = ZipFile.Open(downloadPath, ZipArchiveMode.Read))
                                {
                                    foreach (ZipArchiveEntry entry in zip_archive.Entries)
                                    {
                                        string entry_name = entry.FullName.Replace('\\', '/');
                                        using (Stream reader = entry.Open())
                                        {
                                            try
                                            {
                                                using (FileStream file_stream = new FileStream("./" + entry_name, FileMode.Create))
                                                {
                                                    reader.CopyTo(file_stream);
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                Console.Error.WriteLine(ex);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                string dest_file_path = "./" + Path.GetFileName(downloadPath);
                                if (File.Exists(dest_file_path))
                                {
                                    File.Delete(dest_file_path);
                                }
                                File.Copy(downloadPath, dest_file_path);
                            }

                            // Write last data
                            try
                            {
                                if (File.Exists(lastUpdateDataPath))
                                {
                                    File.Delete(lastUpdateDataPath);
                                }
                                using (FileStream file_stream = File.Open(lastUpdateDataPath, FileMode.Create))
                                {
                                    serializer.WriteObject(file_stream, latestReleaseData);
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.Error.WriteLine(ex);
                            }
                        }
                        catch (Exception ex)
                        {
                            // Revert changes
                            Console.Error.WriteLine(ex);
                            try
                            {
                                if (File.Exists(backup_file_name))
                                {
                                    using (ZipArchive zip_archive = ZipFile.Open(backup_file_name, ZipArchiveMode.Read))
                                    {
                                        foreach (ZipArchiveEntry entry in zip_archive.Entries)
                                        {
                                            string entry_name = entry.FullName.Replace('\\', '/');
                                            using (Stream reader = entry.Open())
                                            {
                                                try
                                                {
                                                    using (FileStream file_stream = new FileStream("./" + entry_name, FileMode.Create))
                                                    {
                                                        reader.CopyTo(file_stream);
                                                    }
                                                }
                                                catch (Exception exc)
                                                {
                                                    Console.Error.WriteLine(exc);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            catch
                            {
                                is_canceled = true;
                                error       = "Attempt on reverting changes failed.\r\n\r\nBackup file: " + backup_file_name;
                            }
                        }
                    }
                    catch
                    {
                        is_canceled = true;
                        error       = "Can't create backup from files.";
                    }
                }
                catch (Exception ex)
                {
                    is_canceled = true;
                    error       = ex.ToString();
                }
            }
            UpdateTaskFinished.Invoke(this, new UpdateTaskFinishedEventArgs(is_canceled, error));
        }