From 4197de03b7fdf2b6979f1d639611c20579f1626d Mon Sep 17 00:00:00 2001 From: "gb.luke" Date: Thu, 8 Jul 2010 17:28:24 +0000 Subject: [PATCH] - Removed some old code (it's in SVN anyway) - Added an exception catcher on the download function --- NUS Downloader/Form1.cs | 374 +----------------------------------- NUS Downloader/NusClient.cs | 1 + 2 files changed, 3 insertions(+), 372 deletions(-) diff --git a/NUS Downloader/Form1.cs b/NUS Downloader/Form1.cs index 9446922..3fd2d72 100644 --- a/NUS Downloader/Form1.cs +++ b/NUS Downloader/Form1.cs @@ -882,326 +882,17 @@ namespace NUS_Downloader else wadName = WAD_Saveas_Filename; - nusClient.DownloadTitle(titleidbox.Text, titleversion.Text, Path.Combine(CURRENT_DIR, "titles"), wadName, storeTypes); - - WriteStatus("NUS Download Finished."); - - /* THIS CODE WAD PRE-libWiiSharp - // Preparations for Downloading - Control.CheckForIllegalCrossThreadCalls = false; // this function would need major rewriting to get rid of this... - if (!(script_mode)) - WriteStatus("Starting NUS Download. Please be patient!"); - SetEnableforDownload(false); - downloadstartbtn.Text = "Starting NUS Download!"; - - // Creates the directory - if (!script_mode) - CreateTitleDirectory(); - - // Wii / DSi - bool wiimode = (consoleCBox.SelectedIndex == 0); - string activeNusUrl; - if (wiimode) - activeNusUrl = WII_NUS_URL; - else - activeNusUrl = DSI_NUS_URL; - - string titleid = titleidbox.Text; - string title_v = titleversion.Text; - - // WebClient configuration - WebClient nusWC = new WebClient(); - nusWC = ConfigureWithProxy(nusWC); - nusWC.Headers.Add("User-Agent", "wii libnup/1.0"); // Set UserAgent to Wii value - - // Create\Configure NusClient - libWiiSharp.NusClient nusClient = new libWiiSharp.NusClient(); - nusClient.ConfigureNusClient(nusWC); - nusClient.UseLocalFiles = localuse.Checked; - - // Get placement directory early... - string titledirectory; - if (titleversion.Text == "") - titledirectory = Path.Combine(CURRENT_DIR, titleid); - else - titledirectory = Path.Combine(CURRENT_DIR, (titleid + "v" + title_v)); - - if (script_mode) - titledirectory = Path.Combine(CURRENT_DIR, "output_" + Path.GetFileNameWithoutExtension(script_filename)); - - downloadstartbtn.Text = "Prerequisites: (0/2)"; - - // Windows 7? Windows 7 Taskbar progress can be used. - if (IsWin7()) - dlprogress.ShowInTaskbar = true; - - // Download TMD before the rest... - string tmdfull = "tmd"; - if (String.IsNullOrEmpty(title_v) == false) - tmdfull += "." + title_v; - - byte[] tmd; try { - tmd = nusClient.DownloadTMD(titleid, title_v, activeNusUrl); - //DownloadNUSFile(titleid, tmdfull, titledirectory, 0, wiimode); + nusClient.DownloadTitle(titleidbox.Text, titleversion.Text, Path.Combine(CURRENT_DIR, "titles"), wadName, storeTypes); } catch (Exception ex) { - WriteStatus("Download Failed: " + tmdfull); - WriteStatus(" - Reason: " + ex.Message.ToString().Replace("The remote server returned an error: ", "")); - SetEnableforDownload(true); - downloadstartbtn.Text = "Start NUS Download!"; - dlprogress.Value = 0; - DeleteTitleDirectory(); - return; - } - downloadstartbtn.Text = "Prerequisites: (1/2)"; - dlprogress.Value = 50; - - if (script_mode) - { - packbox.Checked = true; - packbox_CheckedChanged("scripted", new EventArgs()); - keepenccontents.Checked = false; - wadnamebox.Enabled = false; - } - - // Download cetk (ticket) after tmd... - //bool ticket_exists = true; - byte[] cetkbuf = new byte[0]; - try - { - cetkbuf = nusClient.DownloadTicket(titleid, activeNusUrl); - //DownloadNUSFile(titleid, "cetk", titledirectory, 0, wiimode); - } - catch (Exception ex) - { - //WriteStatus("Download Failed: cetk"); - - - WriteStatus("Ticket not found! Continuing, however WAD packing and decryption are not possible!"); - WriteStatus(" - Reason: " + - ex.Message.ToString().Replace("The remote server returned an error: ", "")); - - packbox.Checked = false; - decryptbox.Checked = false; - WAD_Saveas_Filename = String.Empty; - //ticket_exists = false; - } - - downloadstartbtn.Text = "Prerequisites: (2/2)"; - dlprogress.Value = 100; - - //byte[] cetkbuf = new byte[0]; - byte[] titlekey = new byte[0]; - if ((cetkbuf.Length > 0) == true) - { - // Create ticket file holder - //cetkbuf = FileLocationToByteArray(Path.Combine(titledirectory, "cetk")); - - // Obtain TitleKey - titlekey = new byte[16]; - if (decryptbox.Checked == true) - { - // Load TitleKey into it's byte[] - // It is currently encrypted... - for (int i = 0; i < 16; i++) - { - titlekey[i] = cetkbuf[0x1BF + i]; - } - - // IV (TITLEID+0000s) - byte[] iv = new byte[16]; - for (int i = 0; i < 8; i++) - { - iv[i] = cetkbuf[0x1DC + i]; - } - for (int i = 0; i < 8; i++) - { - iv[i + 8] = 0x00; - } - - // Standard/Korea Common Key - byte[] keyBytes; - if (cetkbuf[0x01F1] == 0x01) - { - WriteStatus("Key Type: Korean"); - keyBytes = libWiiSharp.CommonKey.GetKoreanKey(); - } - else - { - WriteStatus("Key Type: Standard"); - if (wiimode) - keyBytes = libWiiSharp.CommonKey.GetStandardKey(); - else - keyBytes = File.ReadAllBytes(Path.Combine(CURRENT_DIR, "dsikey.bin")); - } - - initCrypt(iv, keyBytes); - - WriteStatus("Title Key: " + DisplayBytes(Decrypt(titlekey), "")); - titlekey = Decrypt(titlekey); - } - } - - // Read the tmd as a stream... - //byte[] tmd = FileLocationToByteArray(Path.Combine(titledirectory, tmdfull)); - - if ((cetkbuf.Length > 0) == true) - { - // Locate Certs ************************************** - if (!(CertsValid())) - { - WriteStatus("Searching for certs..."); - ScanForCerts(tmd); - ScanForCerts(cetkbuf); - } - else - WriteStatus("Using cached certs..."); - // /Locate Cert ************************************** - } - - // Read Title Version... - if (String.IsNullOrEmpty(title_v)) - { - string tmdversion = ""; - for (int x = 476; x < 478; x++) - { - tmdversion += MakeProperLength(ConvertToHex(Convert.ToString(tmd[x]))); - } - titleversion.Text = Convert.ToString(int.Parse(tmdversion, System.Globalization.NumberStyles.HexNumber)); - title_v = tmdversion; - } - - //Read System Version (Needed IOS) - string sysversion = IOSNeededFromTMD(tmd); - if (sysversion != "0") - WriteStatus("Requires: IOS" + sysversion); - - // Read Content #... - string contentstrnum = ""; - for (int x = 478; x < 480; x++) - { - contentstrnum += TrimLeadingZeros(Convert.ToString(tmd[x])); - } - WriteStatus("Content #: " + contentstrnum); - downloadstartbtn.Text = String.Format("Content: (0/{0})", contentstrnum); - dlprogress.Value = 0; - - // Gather information... - string[] tmdcontents = GetContentNames(tmd, Convert.ToInt32(contentstrnum)); - string[] tmdsizes = GetContentSizes(tmd, Convert.ToInt32(contentstrnum)); - byte[] tmdhashes = GetContentHashes(tmd, Convert.ToInt32(contentstrnum)); - byte[] tmdindices = GetContentIndices(tmd, Convert.ToInt32(contentstrnum)); - - // Progress bar total size tally info... - float totalcontentsize = 0; - float currentcontentlocation = 0; - for (int i = 0; i < tmdsizes.Length; i++) - { - totalcontentsize += int.Parse(tmdsizes[i], System.Globalization.NumberStyles.HexNumber); - } - WriteStatus("Total Size: " + (long)totalcontentsize + " bytes"); - - // Write files from NUS out... - File.WriteAllBytes(Path.Combine(titledirectory, tmdfull), tmd); - File.WriteAllBytes(Path.Combine(titledirectory, "cetk"), cetkbuf); - - // Download each content - for (int i = 0; i < tmdcontents.Length; i++) - { - byte[] contbuf; - try - { - // If it exists we leave it... - if ((nusClient.UseLocalFiles) && (File.Exists(Path.Combine(titledirectory, tmdcontents[i])))) - { - WriteStatus("Leaving local " + tmdcontents[i] + "."); - contbuf = File.ReadAllBytes(Path.Combine(titledirectory, tmdcontents[i])); - } - else - { - contbuf = nusClient.DownloadSingleContent(titleid, title_v, tmdcontents[i], activeNusUrl); - //DownloadNUSFile(titleid, tmdcontents[i], titledirectory, - //int.Parse(tmdsizes[i], System.Globalization.NumberStyles.HexNumber), wiimode); - } - } - catch (Exception ex) - { - WriteStatus("Download Failed: " + tmdcontents[i]); - WriteStatus(" - Reason: " + - ex.Message.ToString().Replace("The remote server returned an error: ", "")); - SetEnableforDownload(true); - downloadstartbtn.Text = "Start NUS Download!"; - dlprogress.Value = 0; - DeleteTitleDirectory(); - return; - } - - if (File.Exists(Path.Combine(titledirectory, tmdcontents[i])) == false) - File.WriteAllBytes(Path.Combine(titledirectory, tmdcontents[i]), contbuf); - - // Progress reporting advances... - downloadstartbtn.Text = String.Format("Content: ({0} / {1})", (i + 1), contentstrnum); - currentcontentlocation += int.Parse(tmdsizes[i], System.Globalization.NumberStyles.HexNumber); - - // Decrypt stuff... - if (decryptbox.Checked == true) - { - // Create content file holder - //byte[] contbuf = - //FileLocationToByteArray(Path.Combine(titledirectory, tmdcontents[i])); - - // IV (00+IDX+more000) - byte[] iv = new byte[16]; - for (int x = 0; x < 16; x++) - { - iv[x] = 0x00; - } - iv[1] = tmdindices[i]; - - initCrypt(iv, titlekey); - - - - FileStream decfs = - new FileStream( - Path.Combine(titledirectory, (tmdcontents[i] + ".app")), - FileMode.Create); - decfs.Write(Decrypt(contbuf), 0, int.Parse(tmdsizes[i], System.Globalization.NumberStyles.HexNumber)); - decfs.Close(); - WriteStatus(" - Decrypted: " + tmdcontents[i] + ".app"); - - // Hash Check... - byte[] hash = new byte[20]; - for (int x = 0; x < 20; x++) - { - hash[x] = tmdhashes[(i*20) + x]; - } - byte[] deccont = Decrypt(contbuf); - Array.Resize(ref deccont, int.Parse(tmdsizes[i], System.Globalization.NumberStyles.HexNumber)); - if ((Convert.ToBase64String(ComputeSHA(deccont))) == Convert.ToBase64String(hash)) - { - WriteStatus(" - Hash Check: Pass"); - } - else - { - WriteStatus(" - Hash Check: Fail"); - WriteStatus(" - True Hash: " + DisplayBytes(hash, "")); - WriteStatus(" - You Have: " + DisplayBytes(ComputeSHA(Decrypt(contbuf)), "")); - } - } - - dlprogress.Value = Convert.ToInt32(((currentcontentlocation/totalcontentsize)*100)); + WriteStatus("Uhoh, the download bombed: " + ex.Message + " ):"); } WriteStatus("NUS Download Finished."); - if ((packbox.Checked == true) && (wiimode == true)) - { - PackWAD(titleid, tmdfull, titledirectory); - } */ } void nusClient_Debug(object sender, libWiiSharp.MessageEventArgs e) @@ -3172,67 +2863,6 @@ namespace NUS_Downloader return Bytes; }*/ - /*private void commonKeykeybinToolStripMenuItem_Click(object sender, EventArgs e) - { - BackgroundWorker keyFetcher = new BackgroundWorker(); - keyFetcher.DoWork += new DoWorkEventHandler(RetrieveCommonKey); - keyFetcher.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CommonKey_Retrieved); - keyFetcher.RunWorkerAsync("key.bin"); - } - - private void koreanKeykkeybinToolStripMenuItem_Click(object sender, EventArgs e) - { - BackgroundWorker keyFetcher = new BackgroundWorker(); - keyFetcher.DoWork += new DoWorkEventHandler(RetrieveCommonKey); - keyFetcher.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CommonKey_Retrieved); - keyFetcher.RunWorkerAsync("kkey.bin"); - } - - void CommonKey_Retrieved(object sender, RunWorkerCompletedEventArgs e) - { - BootChecks(); - } - - void RetrieveCommonKey(object sender, DoWorkEventArgs e) - { - WriteStatus(String.Format("Retrieving Key ({0})...", e.Argument.ToString())); - - // Begin the epic grab for freedom - WebClient keyclient = new WebClient(); - - // Proxy - keyclient = ConfigureWithProxy(keyclient); - - string htmlwithkey; - if (e.Argument.ToString() == "key.bin") - { - htmlwithkey = keyclient.DownloadString("http://hackmii.com/2008/04/keys-keys-keys/"); - - // Find our start point - string startofcommonkey = "Common key ("; - htmlwithkey = htmlwithkey.Substring( - htmlwithkey.IndexOf(startofcommonkey) + startofcommonkey.Length, 32); - WriteStatus(" - Got the Common Key as: "); - WriteStatus(" " + htmlwithkey); - byte[] commonkey = HexStringToByteArray(htmlwithkey); - WriteCommonKey("key.bin", commonkey); - } - else if (e.Argument.ToString() == "kkey.bin") - { - htmlwithkey = keyclient.DownloadString("http://hackmii.com/2008/09/korean-wii/"); - - // Find our start point - string startofcommonkey = "those.

"; - htmlwithkey = htmlwithkey.Substring( - htmlwithkey.IndexOf(startofcommonkey) + startofcommonkey.Length + 6, 47); - htmlwithkey = htmlwithkey.Replace(" ", ""); - WriteStatus(" - Got the Korean Key as: "); - WriteStatus(" " + htmlwithkey); - byte[] commonkey = HexStringToByteArray(htmlwithkey); - WriteCommonKey("kkey.bin", commonkey); - } - }*/ - string CombinePaths(params string[] parts) { string result = String.Empty; diff --git a/NUS Downloader/NusClient.cs b/NUS Downloader/NusClient.cs index 68ffc7c..45fc917 100644 --- a/NUS Downloader/NusClient.cs +++ b/NUS Downloader/NusClient.cs @@ -310,6 +310,7 @@ namespace libWiiSharp byte[] tmdFileWithCerts; try { + fireDebug(titleUrl + "/" + tmdFile); tmdFileWithCerts = wcNus.DownloadData(titleUrl + tmdFile); tmd = TMD.Load(tmdFileWithCerts); }