Example #1
0
        private void GetMetadataCDN_Click(object sender, EventArgs e)
        {
            if (Meta.npdm.TitleID == null)
            {
                Meta = GameList.ParseGameFiles(PathText.Text);
            }
            GameList.GameListNACP nacp = NintendoCDN.GetGameMetadata(Meta.npdm.TitleID);
            GameName.Text      = nacp.TitleName;
            PublisherName.Text = nacp.Publisher;

            Image  image       = Image.FromFile("./Images/GameThumbnails/" + Meta.npdm.TitleID + ".jpg");
            Bitmap scaledImage = ResizeImage(image, 256, 256);

            Thumbnail.Image = scaledImage;
        }
        public static GameList.GameListNACP GetGameMetadata(string TitleID)
        {
            Process          hactool   = null;
            ProcessStartInfo startInfo = new ProcessStartInfo();

            did = RandomString(16);

            // Set up the inital Certificate and URL
            ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertifications;
            string           MetaURL = "https://atum.hac.lp1.d4c.nintendo.net/t/a/" + TitleID + "/" + ver;
            X509Certificate2 Cert    = new X509Certificate2(CertPath, "switch");

            // Get the Meta NCA ID
            HttpWebRequest GetMetaNCAID = (HttpWebRequest)WebRequest.Create(MetaURL);

            GetMetaNCAID.ClientCertificates.Add(Cert);
            GetMetaNCAID.Method    = "HEAD";
            GetMetaNCAID.UserAgent = "NintendoSDK Firmware/" + fw + " (platform:NX; did:" + did + "; eid:lp1)";
            HttpWebResponse ParseMetaNCAID = null;

            try
            {
                ParseMetaNCAID = (HttpWebResponse)GetMetaNCAID.GetResponse();
            }
            catch (WebException we)
            {
                HttpStatusCode statusCode = ((HttpWebResponse)we.Response).StatusCode;

                if (statusCode == HttpStatusCode.NotFound)
                {
                    return(new GameList.GameListNACP());                                       // If it's not found on the CDN then just return null values.
                }
                else
                {
                    throw we;  // Else just throw the Exception.
                }
            }

            ParseMetaNCAID.Close();

            // Download The Meta NCA
            string         MetaNCAURL = "https://atum.hac.lp1.d4c.nintendo.net/c/a/" + ParseMetaNCAID.GetResponseHeader("x-nintendo-content-id");
            HttpWebRequest GetMetaNCA = (HttpWebRequest)WebRequest.Create(MetaNCAURL);

            GetMetaNCA.ClientCertificates.Add(Cert);
            GetMetaNCA.UserAgent = "NintendoSDK Firmware/" + fw + " (platform:NX; did:" + did + "; eid:lp1)";
            HttpWebResponse ParseMetaNCA = (HttpWebResponse)GetMetaNCA.GetResponse();
            BinaryReader    MetaNCA      = new BinaryReader(ParseMetaNCA.GetResponseStream());

            File.WriteAllBytes(ver, MetaNCA.ReadBytes(100000));
            MetaNCA.Close();
            ParseMetaNCA.Close();

            // Use Hactool to extract the CNMT
            startInfo.FileName        = "hactool.exe";
            startInfo.Arguments       = "-k keys.txt " + ver + " --section0dir=CNMT";
            startInfo.WindowStyle     = ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = true;
            hactool = Process.Start(startInfo);
            hactool.Start();
            hactool.WaitForExit();

            Thread.Sleep(300);

            File.Delete(ver);

            // Parse the CNMT to get the NCA ID
            BinaryReader OpenCNMT  = new BinaryReader(File.Open("CNMT/Application_" + TitleID + ".cnmt", FileMode.Open));
            string       ParseCNMT = ByteArrayToString(OpenCNMT.ReadBytes(1000));

            OpenCNMT.Close();
            string ContNCAID = "";

            if (ParseCNMT.Substring(32, 2) == "03")
            {
                if (ParseCNMT.Substring(316, 2) == "03")
                {
                    ContNCAID = ParseCNMT.Substring(272, 32);
                }
                else if (ParseCNMT.Substring(428, 2) == "03")
                {
                    ContNCAID = ParseCNMT.Substring(384, 32);
                }
            }
            else if (ParseCNMT.Substring(32, 2) == "04")
            {
                if (ParseCNMT.Substring(316, 2) == "03")
                {
                    ContNCAID = ParseCNMT.Substring(272, 32);
                }
                else if (ParseCNMT.Substring(428, 2) == "03")
                {
                    ContNCAID = ParseCNMT.Substring(384, 32);
                }
                else if (ParseCNMT.Substring(540, 2) == "03")
                {
                    ContNCAID = ParseCNMT.Substring(496, 32);
                }
            }

            Directory.Delete("./CNMT", true);

            // Get the Control NCA
            string         ContNCAURL = "https://atum.hac.lp1.d4c.nintendo.net/c/c/" + ContNCAID;
            HttpWebRequest GetContNCA = (HttpWebRequest)WebRequest.Create(ContNCAURL);

            GetContNCA.ClientCertificates.Add(Cert);
            GetContNCA.UserAgent = "NintendoSDK Firmware/" + fw + " (platform:NX; did:" + did + "; eid:lp1)";
            HttpWebResponse ParseContNCA = (HttpWebResponse)GetContNCA.GetResponse();
            BinaryReader    ContNCA      = new BinaryReader(ParseContNCA.GetResponseStream());

            File.WriteAllBytes("TempCont", ContNCA.ReadBytes(10000000));
            ParseContNCA.Close();

            // Finally extract the Control NCA into a folder called Cont
            startInfo.Arguments = " -k keys.txt TempCont --section0dir=Cont";
            hactool             = Process.Start(startInfo);
            hactool.Start();
            hactool.WaitForExit();

            Thread.Sleep(300);

            File.Delete("TempCont");

            GameList.GameListNACP res = GameList.ParseControlNacp("./Cont/control.nacp");

            // Get the Icon File (and default to American English)
            string IconFile = "";

            if (File.Exists("Cont/icon_AmericanEnglish.dat"))
            {
                IconFile = "Cont/icon_AmericanEnglish.dat";
            }
            else
            {
                foreach (string s in Directory.GetFiles("./Cont"))
                {
                    if (Path.GetFileName(s).StartsWith("icon_"))
                    {
                        IconFile = s;
                        break;
                    }
                }
            }

            if (File.Exists("./Images/GameThumbnails/" + TitleID + ".jpg"))
            {
                File.Delete("./Images/GameThumbnails/" + TitleID + ".jpg");
            }
            File.Copy(IconFile, "./Images/GameThumbnails/" + TitleID + ".jpg");

            Directory.Delete("./Cont", true);

            return(res);
        }