Exemple #1
0
        private static int DownloadContent(TMD tmd, string outputDir, string titleUrl)
        {
            for (var i = 0; i < tmd.NumOfContents; i++)
            {
                var i1   = i;
                var numc = tmd.NumOfContents;
                var size = Toolbelt.SizeSuffix((long)tmd.Contents[i1].Size);
                Toolbelt.AppendLog($"Downloading Content #{i1 + 1} of {numc}... ({size})");
                var contentPath = Path.Combine(outputDir, tmd.Contents[i1].ContentID.ToString("x8"));

                if (!Toolbelt.IsValid(tmd.Contents[i1], contentPath))
                {
                    try
                    {
                        var downloadUrl = $"{titleUrl}/{tmd.Contents[i1].ContentID:x8}";
                        Web.DownloadFile(downloadUrl, contentPath);
                    }
                    catch (Exception ex)
                    {
                        Toolbelt.AppendLog($"Downloading Content #{i1 + 1} of {numc} failed...\n{ex.Message}");
                        break;
                    }
                }
                ReportProgress(0, tmd.NumOfContents - 1, i1);
            }
            ReportProgress(0, 100, 0);
            return(1);
        }
Exemple #2
0
        private void UpdateProgressBar(int percent, long _toReceive, long _received)
        {
            if (percent <= 0 || _toReceive <= 0 || _received <= 0)
            {
                return;
            }

            try {
                Invoke(new Action(() => progressBar.Value = percent));

                var toReceive = Toolbelt.SizeSuffix(_toReceive);
                var received  = Toolbelt.SizeSuffix(_received);

                progressOverlay.Invoke(new Action(() => { progressOverlay.Text = $@"{received} / {toReceive}"; }));
            }
            catch (Exception ex) {
                TextLog.MesgLog.WriteError($"{ex.Message}\n{ex.StackTrace}");
            }
        }
        /// <summary>
        /// Grabs a title from NUS, you can define several store types.
        /// Leave the title version empty for the latest.
        /// </summary>
        /// <param name="titleId"></param>
        /// <param name="titleVersion"></param>
        /// <param name="outputDir"></param>
        /// <param name="storeTypes"></param>
        public void DownloadTitle(string titleId, string titleVersion, string outputDir, StoreType[] storeTypes)
        {
            FireDebug("Downloading Title {0} v{1}...", titleId, (string.IsNullOrEmpty(titleVersion)) ? "[Latest]" : titleVersion);

            if (storeTypes.Length < 1)
            {
                FireDebug("  No store types were defined..."); throw new Exception("You must at least define one store type!");
            }

            var titleInfo = Database.GetTitle(titleId);

            string titleUrl  = $"{nusUrl}{titleId}/";
            string titleUrl2 = $"{nusUrl2}{titleId}/";

            bool storeEncrypted = false;
            bool storeDecrypted = false;

            FireProgress(0);

            foreach (StoreType st in storeTypes)
            {
                switch (st)
                {
                case StoreType.DecryptedContent:
                    FireDebug("    [=] Storing Decrypted Content...");
                    storeDecrypted = true;
                    break;

                case StoreType.EncryptedContent:
                    FireDebug("    [=] Storing Encrypted Content...");
                    storeEncrypted = true;
                    break;

                case StoreType.All:
                    FireDebug("    [=] Storing Decrypted Content...");
                    FireDebug("    [=] Storing Encrypted Content...");
                    FireDebug("    [=] Storing WAD...");
                    storeDecrypted = true;
                    storeEncrypted = true;
                    break;

                case StoreType.Empty:
                    break;
                }
            }

            FireDebug("  - Checking for Internet connection...");
            if (!CheckInet())
            {
                FireDebug("   + Connection not found...");
                throw new Exception("You're not connected to the internet!");
            }

            if (!Directory.Exists(outputDir))
            {
                Directory.CreateDirectory(outputDir);
            }
            if (!Directory.Exists(Path.Combine(outputDir, titleInfo.Name)))
            {
                Directory.CreateDirectory(Path.Combine(outputDir, titleInfo.Name));
            }
            outputDir = Path.Combine(outputDir, titleInfo.Name);

            string tmdFile = "tmd" + (string.IsNullOrEmpty(titleVersion) ? string.Empty : string.Format(".{0}", titleVersion));

            //Download TMD
            FireDebug("  - Downloading TMD...");
            TMD tmd;

            byte[] tmdFileWithCerts;
            try
            {
                tmdFileWithCerts = wcNus.DownloadData(titleUrl + tmdFile);
                tmd = TMD.Load(tmdFileWithCerts);
            }
            catch (Exception ex) { FireDebug("   + Downloading TMD Failed..."); throw new Exception("Downloading TMD Failed:\n" + ex.Message); }

            //Parse TMD
            FireDebug("  - Parsing TMD...");

            if (string.IsNullOrEmpty(titleVersion))
            {
                FireDebug("    + Title Version: {0}", tmd.TitleVersion);
            }
            FireDebug("    + {0} Contents", tmd.NumOfContents);

            if (!Directory.Exists(Path.Combine(outputDir, tmd.TitleVersion.ToString())))
            {
                Directory.CreateDirectory(Path.Combine(outputDir, tmd.TitleVersion.ToString()));
            }
            outputDir = Path.Combine(outputDir, tmd.TitleVersion.ToString());

            titleversion = tmd.TitleVersion;

            File.WriteAllBytes(Path.Combine(outputDir, tmdFile), tmdFileWithCerts);

            FireProgress(5);

            //Download cetk
            FireDebug("  - Downloading Ticket...");
            try
            {
                wcNus.DownloadFile(Path.Combine(titleUrl, "cetk"), Path.Combine(outputDir, "cetk"));
            }
            catch (Exception ex)
            {
                try
                {
                    if (titleInfo.Ticket == "1")
                    {
                        var cetkUrl = $"{WII_TIK_URL}{titleId.ToLower()}.tik";
                        wcNus.DownloadFile(cetkUrl, Path.Combine(outputDir, "cetk"));
                    }
                }
                catch
                {
                    continueWithoutTicket = false;
                    if (!continueWithoutTicket || !storeEncrypted)
                    {
                        FireDebug("   + Downloading Ticket Failed...");
                        throw new Exception("Downloading Ticket Failed:\n" + ex.Message);
                    }

                    if (!(File.Exists(Path.Combine(outputDir, "cetk"))))
                    {
                        storeDecrypted = false;
                    }
                }
            }

            FireProgress(10);

            // Parse Ticket
            Ticket tik = new Ticket();

            if (File.Exists(Path.Combine(outputDir, "cetk")))
            {
                FireDebug("   + Parsing Ticket...");
                tik = Ticket.Load(Path.Combine(outputDir, "cetk"));

                // DSi ticket? Must make sure to use DSi Key :D
                if (nusUrl == DSI_NUS_URL)
                {
                    tik.DSiTicket = true;
                }
            }
            else
            {
                FireDebug("   + Ticket Unavailable...");
            }

            string[] encryptedContents = new string[tmd.NumOfContents];

            //Download Content
            for (int i = 0; i < tmd.NumOfContents; i++)
            {
                Form1.token.ThrowIfCancellationRequested();

                var size = Toolbelt.SizeSuffix(tmd.Contents[i].Size);
                FireDebug("  - Downloading Content #{0} of {1}... ({2} bytes)", i + 1, tmd.NumOfContents, size);
                FireProgress(((i + 1) * 60 / tmd.NumOfContents) + 10);

                var contentPath = Path.Combine(outputDir, tmd.Contents[i].ContentID.ToString("x8"));
                if (useLocalFiles && Toolbelt.IsValid(tmd.Contents[i], contentPath))
                {
                    FireDebug("   + Using Local File, Skipping..."); continue;
                }

                try
                {
                    var downloadUrl = titleUrl + tmd.Contents[i].ContentID.ToString("x8");
                    var outputdir   = Path.Combine(outputDir, tmd.Contents[i].ContentID.ToString("x8"));
                    wcNus.DownloadFile(downloadUrl, outputdir);

                    encryptedContents[i] = tmd.Contents[i].ContentID.ToString("x8");
                }
                catch (Exception ex)
                {
                    FireDebug("  - Downloading Content #{0} of {1} failed...", i + 1, tmd.NumOfContents);
                    throw new Exception("Downloading Content Failed:\n" + ex.Message);
                }
            }

            //Decrypt Content
            if (storeDecrypted)
            {
                FireDebug("  - Decrypting Content...");
                Toolbelt.CDecrypt(this, outputDir);
            }

            //Delete not wanted files
            if (!storeEncrypted)
            {
                FireDebug("  - Deleting Encrypted Contents...");
                for (int i = 0; i < tmd.Contents.Length; i++)
                {
                    if (File.Exists(Path.Combine(outputDir, tmd.Contents[i].ContentID.ToString("x8"))))
                    {
                        File.Delete(Path.Combine(outputDir, tmd.Contents[i].ContentID.ToString("x8")));
                    }
                }
            }

            if (!storeDecrypted && !storeEncrypted)
            {
                FireDebug("  - Deleting TMD and Ticket...");
                File.Delete(Path.Combine(outputDir, tmdFile));
                File.Delete(Path.Combine(outputDir, "cetk"));
            }

            FireDebug("Downloading Title {0} v{1} Finished...", titleId, tmd.TitleVersion /*(string.IsNullOrEmpty(titleVersion)) ? "[Latest]" : titleVersion*/);
            FireProgress(100);
        }