private Nullable <DownloadItem> getDownloadItem(TitleInfo item, DownloadType type) { TitleInfo info; lock (item) { info = item; } switch (type) { case DownloadType.Game: if (!info.isUpdate) { try { bool madeTicket = false; TMD titleTMD = AsyncHelpers.RunSync <TMD>(() => NUS.DownloadTMD(info.titleID)); if (info.ticket.Length == 0 && info.hasTicket) { info.ticket = AsyncHelpers.RunSync <byte[]>(() => HelperFunctions.DownloadTitleKeyWebsiteTicket(info.titleID)); } if (!info.hasTicket && info.titleKey.Length > 0) { madeTicket = true; info.ticket = info.getGeneratedTitleTicket(titleTMD.TitleVersion); } return(new DownloadItem( info.displayName + (madeTicket ? " (FakeSign)" : ""), titleTMD, AsyncHelpers.RunSync <NUS.UrlFilenamePair[]>(() => NUS.GetTitleContentURLs(titleTMD, true)), info.ticket, null, null, madeTicket )); } catch { } } break; case DownloadType.DLC: try { if (info.dlcKey.Length > 0) { TMD dlcTMD = AsyncHelpers.RunSync <TMD>(() => NUS.DownloadTMD(info.dlcID)); return(new DownloadItem( info.DisplayNameWithVersion(dlcTMD.TitleVersion, "DLC") + " (FakeSign)", dlcTMD, AsyncHelpers.RunSync <NUS.UrlFilenamePair[]>(() => NUS.GetTitleContentURLs(dlcTMD, true)), info.getDLCTicket(dlcTMD.TitleVersion), null, null, true )); } } catch { } break; case DownloadType.Update: try { TMD updateTMD = AsyncHelpers.RunSync <TMD>(() => NUS.DownloadTMD(info.updateID)); return(new DownloadItem( info.DisplayNameWithVersion(updateTMD.TitleVersion, "Update"), updateTMD, AsyncHelpers.RunSync <NUS.UrlFilenamePair[]>(() => NUS.GetTitleContentURLs(updateTMD, true)), AsyncHelpers.RunSync <byte[]>(() => NUS.DownloadTicket(info.updateID)) )); } catch { } break; } return(null); }
private async void ProcessDownloadQueue(DownloadItem[] items) { if (DownloadPath == null) { if (ChooseFolder() == false) { return; } } string basePath = DownloadPath; Downloading = true; dataDownloadedSinceLastTick = 0; dataToDownload = 0; progressTimer.Enabled = true; int count = 0; string previousTitle = this.Text; stopwatch1.Start(); stopwatch2.Start(); var errors = new List <string> { }; bool hideWget = Common.Settings.hideWget; bool shellExecute = Common.Settings.shellExecute; btnDownload.Enabled = false; foreach (DownloadItem title in DownloadQueue) { count++; completeFileDataDownloaded = 0; this.Text = "(" + count + "/" + DownloadQueue.Count + ")" + title.name; dataDownloadedSinceLastTick = 0; dataToDownload = title.tmd.TitleContentSize; var itemPath = HelperFunctions.GetAutoIncrementedDirectory(basePath, title.name, "title.tmd", HelperFunctions.md5sum(title.tmd.rawBytes)); if (title.absolutePath != null) { itemPath = title.absolutePath; } if (!Directory.Exists(itemPath)) { Directory.CreateDirectory(itemPath); } byte[] ticket = (title.ticket != null ? title.ticket : new byte[] { }); File.WriteAllBytes(Path.Combine(itemPath, "title.tmd"), title.tmd.rawBytes); if (ticket.Length > 0) { if (title.tmd.TitleID.ToLower()[7] != "e"[0]) { HelperFunctions.patchTicket(ref ticket); } File.WriteAllBytes(Path.Combine(itemPath, "title.tik"), ticket); } if (!File.Exists(Path.Combine(itemPath, "title.cert"))) { File.WriteAllBytes(Path.Combine(itemPath, "title.cert"), NUS.TitleCert); } DownloadPath = itemPath; bool error = false; stopwatch1.Restart(); stopwatch2.Restart(); foreach (var url in title.URLs) { string filePath = Path.Combine(itemPath, url.Filename); if (!File.Exists(filePath)) { File.Create(filePath).Close(); } CurrentFile = new FileInfo(filePath); lblCurrentFile.Text = url.Filename; bool resume = false; if (CurrentFile.Length > 0 && CurrentFile.Length < url.Size) { resume = true; } else if (CurrentFile.Length > 0 && url.Size > 0) { completeFileDataDownloaded += CurrentFile.Length; continue; } for (var i = 0; i < Common.Settings.downloadTries; i++) { int exitCode = await Task.Run(() => { var procStIfo = new ProcessStartInfo(); procStIfo.FileName = Program.ResourceFiles.wget; procStIfo.Arguments = HelperFunctions.escapeCommandArgument(url.URL) + (resume ? " -c":"") + " -O " + HelperFunctions.escapeCommandArgument(filePath); procStIfo.UseShellExecute = shellExecute; procStIfo.CreateNoWindow = hideWget; if (shellExecute == true && hideWget == false) { procStIfo.WindowStyle = ProcessWindowStyle.Minimized; } runningProcess = new Process(); runningProcess.StartInfo = procStIfo; runningProcess.Start(); runningProcess.WaitForExit(); return(runningProcess.ExitCode); }); if (!isClosing) { error = (exitCode != 0); } if (isClosing || exitCode == 0) { break; } } if (isClosing || error) { break; } progressTimer_Tick(null, null); CurrentFile.Refresh(); completeFileDataDownloaded += CurrentFile.Length; } if (error || isClosing) { progressTimer.Enabled = false; if (title.absolutePath == null) { if (error || MessageBox.Show(Localization.Strings.DeleteIncompleteFilesQuestion, Localization.Strings.IncompleteFiles, MessageBoxButtons.YesNo) == DialogResult.Yes) { Directory.Delete(itemPath, true); } } if (!isClosing) { errors.Add(title.name); } } if (isClosing) { break; } } progressTimer.Enabled = false; stopwatch1.Stop(); stopwatch2.Stop(); Downloading = false; this.Text = previousTitle; DownloadPath = null; if (errors.Count > 0) { MessageBox.Show((errors.Count > 1 ? Localization.Strings.FollowingTitlesEncounteredAnErorr : Localization.Strings.FollowingTitleEnounteredAnError) + "\n\n" + String.Join("\n", errors.ToArray()), Localization.Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show(Localization.Strings.DownloadsCompletedSuccessfully, Localization.Strings.Success); } btnDownload.Enabled = true; if (AutoClose && !isClosing) { Close(); } }
public void getSizes() { var titlesCopy = titles.titles.Where(o => !titleSizes.Keys.Contains(o.titleID)).ToList(); var titlesCopy2 = titles.titles.Where(o => titleSizes.Keys.Contains(o.titleID)).ToList(); List <Task> workers = new List <Task> { }; for (var i = 0; i < 8; i++) { Task.Run(async() => { while (true) { TitleInfo item; lock (titlesCopy) { if (titlesCopy.Count == 0) { break; } item = titlesCopy[COLUMN_INDEX_TITLE_ID]; titlesCopy.RemoveAt(0); } String contentSize; titleSizes.TryGetValue(item.titleID, out contentSize); if (contentSize == null || contentSize.Length == 0) { try { contentSize = HelperFunctions.SizeSuffix((await NUS.DownloadTMD(item.titleID)).TitleContentSize); } catch (Exception ex) { Debug.WriteLine(ex?.Message); Debug.WriteLine(ex?.InnerException?.Message); contentSize = ""; } if (titleSizes.ContainsKey(item.titleID) == false && contentSize != "") { titleSizes.Add(item.titleID, contentSize); } } if (contentSize != "") { this.Invoke((MethodInvoker) delegate { if (item.titleKey.Length > 0) { item.size = contentSize; frmList_SizeChanged(null, null); } }); } } }); } Task.Run(() => { this.Invoke((MethodInvoker) delegate { lstMain.BeginUpdate(); foreach (TitleInfo item in titlesCopy2) { string size = ""; titleSizes.TryGetValue(item.titleID, out size); if (size != null) { item.size = size; } } frmList_SizeChanged(null, null); lstMain.EndUpdate(); }); while (workers.Count > 0) { ; } Common.Settings.cachedSizes = titleSizes; }); }