public void DownloadMod(ModToDownload mod) { _windowManager.ShowDialog(new DownloadFilesViewModel(IoC.Get <IEventAggregator>(), mod.DownloadURL, "bnsfiles\\mods", mod.modOption)); }
private bool MTDownloader(object data) { ModToDownload mtd = (ModToDownload)data; SetStatusLabelText("Downloading Mod " + mtd.modname); DownloadedData = new Dictionary <int, MemoryStream>(); DownloadedBytes = 0; Logger.Info("Download process for mod " + mtd.modname + " starting, URI: " + mtd.url); long mSize = 0; HttpWebRequest wRequest = WebRequest.CreateHttp(mtd.url); wRequest.Method = "HEAD"; wRequest.Timeout = 2000; HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse(); if (wResponse.StatusCode == HttpStatusCode.OK) { mSize = wResponse.ContentLength; } else { Logger.Error("Cannot get length of mod file, exiting. " + mtd.url); return(false); } wResponse.Dispose(); SetProgressBarMaxValue((int)mSize + 1); SetProgressBarValue(0); WebClient wc = new WebClient(); byte[] dt = wc.DownloadData(mtd.url); Logger.Info("Size:" + mSize); ModThread mds = new ModThread(); mds.modname = mtd.modname; mds.threads = new List <BackgroundWorker>(); int threadSize = (int)mSize / 4; int endingThreadSize = threadSize + (int)mSize % 4; threadPermFail = false; for (int i = 0; i < 4; i++) { ModDownloadInfo mdi = new ModDownloadInfo(); if (i == 3) { mdi.startPosition = i * threadSize; mdi.Length = endingThreadSize + 1; } else { mdi.startPosition = i * threadSize; mdi.Length = threadSize; } mdi.url = mtd.url; BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += new DoWorkEventHandler(DownloadThread); mds.threads.Add(bw); mds.threads.Last().RunWorkerAsync(mdi); } Logger.Info("Download started, thread:1"); while (true) { if ((DownloadedBytes % 50) == 0) { SetProgressBarValue((int)DownloadedBytes); } if (DownloadedBytes >= mSize) { SetProgressBarValue((int)mSize); CompositeAndWriteFile(mtd.modname); EnableBtns(); return(true); } else { if (threadPermFail == true) { MessageBox.Show("Error, a thread failed to download a part of the file.\r\nFile Name:" + mtd.modname); Logger.Error("Download " + mtd.modname + " Failed."); return(false); } } } }