internal DialogResult Run(BackgroundWorker worker, DownloadInstructions instr) { this.worker = worker; this.instr = instr; return(ShowDialog()); }
void worker_DoWork(object sender, DoWorkEventArgs e) { check = false; BackgroundWorker worker = sender as BackgroundWorker; DownloadInstructions instr = (DownloadInstructions)e.Argument; bool harddiskMode = Directory.Exists(instr.From); string to = instr.To; string from = instr.From; //downloadLocation; ModuleUpdateFilePath[] download = instr.Files; //config[localVersion.ToString()].GetStrings("game_add"); string[] remove = new string[0]; //config[localVersion.ToString()].GetStrings("game_remove"); if (download.Length == 0) { finished.Set(); return; } int totalsize = 0; using (WebClientEx client = new WebClientEx()) { // make backup worker.ReportProgress(0, Program.Text[instr.Text].Get("backup")); string backupPath = "tmp/backup/"; Directory.CreateDirectory(backupPath); foreach (ModuleUpdateFilePath str in download) { string dir = Path.GetDirectoryName(str.File); if (!Directory.Exists(backupPath + dir)) { Directory.CreateDirectory(backupPath + dir); } if (System.IO.File.Exists(str.File)) { System.IO.File.Copy(str.File, backupPath + str.File, true); } } // remove old files foreach (string str in remove) { string[] filenames = Directory.GetFiles(gamePath, str); foreach (string filename in filenames) { System.IO.File.Delete(filename); } } // retrieve file size worker.ReportProgress(0, Program.Text[instr.Text].Get("retrieve")); int index = 0; // get info do { string filename = from + download[index].FullPath; try { if (harddiskMode) { FileInfo info = new FileInfo(filename); totalsize += (int)info.Length; } else { totalsize += GetHttpFileSize(filename); } } catch { MessageBox.Show(string.Format(Program.Text[instr.Text].Get("error1"), filename), Program.Text[instr.Text].Get("error")); worker.ReportProgress(-1); finished.Set(); return; } index++; } while (!worker.CancellationPending && index < download.Length); if (worker.CancellationPending) { finished.Set(); return; } int downloaded = 0; bool open = false; // download string msg = Program.Text[instr.Text].Get("download"); worker.ReportProgress(0, string.Format(msg, GetSizeString(instr.Text, downloaded), GetSizeString(instr.Text, totalsize))); index = 0; Stream strResponse = null; Stream strLocal = null; byte[] downBuffer = new byte[2048]; int progress = 0; do { if (!open) { string filename = from + download[index].FullPath; msg = Program.Text[instr.Text].Get("download_file"); worker.ReportProgress(progress, string.Format(msg, download[index], GetSizeString(instr.Text, downloaded), GetSizeString(instr.Text, totalsize))); try { // get download stream if (harddiskMode) { strResponse = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); } else { strResponse = client.OpenRead(filename); } } catch { MessageBox.Show(string.Format(Program.Text[instr.Text].Get("error2"), filename), Program.Text[instr.Text].Get("error")); worker.ReportProgress(-1); break; } // create local file strLocal = new FileStream(to + download[index].File, FileMode.Create, FileAccess.Write, FileShare.None); open = true; } int bytesSize = 0; // download 2kb bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length); if (bytesSize > 0) { // save to harddrive strLocal.Write(downBuffer, 0, bytesSize); // report progress downloaded += bytesSize; // update progress if changed float fprogress = downloaded * 100.0f / totalsize; //if (progress != (int)fprogress) { progress = (int)fprogress; msg = Program.Text[instr.Text].Get("download_file"); worker.ReportProgress(progress, string.Format(msg, download[index].File, GetSizeString(instr.Text, downloaded), GetSizeString(instr.Text, totalsize))); } } else { strLocal.Close(); strResponse.Close(); open = false; } if (!open) { index++; } } while (!worker.CancellationPending && index < download.Length); strLocal.Close(); strResponse.Close(); // download was cancelled if (worker.CancellationPending) { worker.ReportProgress(progress, Program.Text[instr.Text].Get("rollback")); // delete downloaded files foreach (ModuleUpdateFilePath file in download) { if (System.IO.File.Exists(to + file.File)) { System.IO.File.Delete(to + file.File); } } // restore backup foreach (ModuleUpdateFilePath file in download) { if (System.IO.File.Exists(backupPath + file.File)) { System.IO.File.Copy(backupPath + file.File, to + file.File, true); } } } // remove backup Directory.Delete(backupPath, true); } if (worker.CancellationPending) { worker.ReportProgress(100, Program.Text[instr.Text].Get("cancel")); } else { worker.ReportProgress(100, string.Format(Program.Text[instr.Text].Get("finish"), GetSizeString(instr.Text, totalsize))); } finished.Set(); check = true; }