private void btnOpen_Click(object sender, EventArgs e)
 {
     if (ofdTik.ShowDialog() == DialogResult.OK && ofdTik.FileNames.Count() > 0)
     {
         List <TitleInfo> list = new List <TitleInfo> {
         };
         foreach (string filename in ofdTik.FileNames)
         {
             byte[]    data     = File.ReadAllBytes(filename);
             string    hexID    = HelperFunctions.getTitleIDFromTicket(data);
             string    basename = System.IO.Path.GetFileNameWithoutExtension(filename);
             TitleInfo info     = new TitleInfo(hexID, "", (basename.ToLower() != "title" ? hexID + " - " + basename : hexID), "", "", true);
             info.ticket = data;
             list.Add(info);
         }
         frmDownload.OpenDownloadForm(list);
     }
 }
Exemple #2
0
        private async Task cacheTicket(TitleInfo item)
        {
            if (item.hasTicket)
            {
                try
                {
                    string tikFilePath = Path.Combine(Common.TicketsPath, item.titleID + ".tik");
                    if (File.Exists(tikFilePath))
                    {
                        item.ticket = File.ReadAllBytes(tikFilePath);
                    }
                    else
                    {
                        byte[] ticket = await HelperFunctions.DownloadTitleKeyWebsiteTicket(item.titleID);

                        File.WriteAllBytes(tikFilePath, ticket);
                        item.ticket = ticket;
                    }
                }
                catch { }
            }
        }
        static void Main(string[] args)
        {
            try
            {
                System.Version version        = Assembly.GetExecutingAssembly().GetName().Version;
                SemVersion     currentVersion = new SemVersion(version.Major, version.Minor, version.Build);
                HttpWebRequest request        = WebRequest.Create("https://api.github.com/repos/DanTheMan827/uTikDownloadHelper/releases") as HttpWebRequest;
                request.UserAgent = "uTikDownloadHelper " + currentVersion;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader    reader   = new System.IO.StreamReader(response.GetResponseStream(), ASCIIEncoding.UTF8);

                dynamic json = JArray.Parse(reader.ReadToEnd());

                SemVersion newestVersion = SemVersion.Parse(((String)json[0].tag_name).Substring(1));

                if (newestVersion > currentVersion)
                {
                    if (MessageBox.Show(String.Format("v{0} is now available, you have v{1}.\r\n\r\nDo you want to visit the download page?", newestVersion.ToString(), currentVersion.ToString()), "New Version", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        Process.Start((String)json[0].html_url);
                        return;
                    }
                }
            }
            catch (Exception ex) { }

            Directory.CreateDirectory(Common.TicketsPath);
            if (Common.Settings.ticketWebsite == null)
            {
                Common.Settings.ticketWebsite = "";
            }

            if (args.Length == 0)
            {
                using (frmList frm = new frmList())
                {
                    FormContext.AddForm(frm);
                    Application.Run(FormContext);
                }
                return;
            }

            if (args[0].ToLower().EndsWith(".tmd"))
            {
                if (File.Exists(args[0]))
                {
                    frmDownload.DownloadMissing(args[0], true);
                    Application.Run(FormContext);
                }

                return;
            }

            string ticketInputPath = args[0];

            Byte[] ticket = { };

            if (ticketInputPath.ToLower().StartsWith("http"))
            {
                try
                {
                    ticket = (new System.Net.WebClient()).DownloadData(ticketInputPath);
                } catch (Exception e)
                {
                    MessageBox.Show(e.Message.ToString(), "Error Downloading Ticket");
                    return;
                }
            }
            else
            {
                ticket = File.ReadAllBytes(ticketInputPath);
            }

            if (ticket.Length < 848)
            {
                MessageBox.Show("Invalid Ticket");
            }

            using (var frm = new frmDownload()){
                string    hexID = HelperFunctions.getTitleIDFromTicket(ticket);
                TitleInfo info  = new TitleInfo(hexID, "", hexID, "", "", true);
                info.ticket = ticket;
                frm.TitleQueue.Add(info);
                FormContext.AddForm(frm);
                Application.Run(FormContext);
            }
        }
        static void Main(string[] args)
        {
            Directory.CreateDirectory(Common.TicketsPath);
            if (Common.Settings.ticketWebsite == null)
            {
                Common.Settings.ticketWebsite = "";
            }

            if (args.Length == 0)
            {
                using (frmList frm = new frmList())
                {
                    FormContext.AddForm(frm);
                    Application.Run(FormContext);
                }
                return;
            }

            if (args[0].ToLower().EndsWith(".tmd"))
            {
                if (File.Exists(args[0]))
                {
                    frmDownload.DownloadMissing(args[0], true);
                    Application.Run(FormContext);
                }

                return;
            }

            string ticketInputPath = args[0];

            Byte[] ticket = { };

            if (ticketInputPath.ToLower().StartsWith("http"))
            {
                try
                {
                    ticket = (new System.Net.WebClient()).DownloadData(ticketInputPath);
                } catch (Exception e)
                {
                    MessageBox.Show(e.Message.ToString(), "Error Downloading Ticket");
                    return;
                }
            }
            else
            {
                ticket = File.ReadAllBytes(ticketInputPath);
            }

            if (ticket.Length < 848)
            {
                MessageBox.Show("Invalid Ticket");
            }

            using (var frm = new frmDownload()){
                string    hexID = HelperFunctions.getTitleIDFromTicket(ticket);
                TitleInfo info  = new TitleInfo(hexID, "", hexID, "", "", true);
                info.ticket = ticket;
                frm.TitleQueue.Add(info);
                FormContext.AddForm(frm);
                Application.Run(FormContext);
            }
        }
        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 frmDownload_Shown(object sender, EventArgs e)
        {
            BringToFront();
            if (TitleQueue.Count == 0)
            {
                if (DownloadQueue.Count > 0)
                {
                    ProcessDownloadQueue(DownloadQueue.ToArray());
                }
                else
                {
                    Close();
                }

                return;
            }

            if (AutoDownloadType == DownloadType.None)
            {
                TitleInfo QueueItem = TitleQueue[0];
                this.Text = QueueItem.displayName;

                if (!QueueItem.isUpdate)
                {
                    await Task.Run(() => {
                        var result = getDownloadItem(QueueItem, DownloadType.Game);
                        if (result.HasValue && result.Value.ticket.Length > 0)
                        {
                            TitleItem   = result.Value;
                            TitleExists = true;
                        }
                    });
                }

                if (QueueItem.dlcKey.Length > 0)
                {
                    await Task.Run(() =>
                    {
                        var result = getDownloadItem(QueueItem, DownloadType.DLC);
                        if (result.HasValue)
                        {
                            DLCItem   = result.Value;
                            DLCExists = true;
                        }
                    });
                }

                await Task.Run(() =>
                {
                    var result = getDownloadItem(QueueItem, DownloadType.Update);
                    if (result.HasValue)
                    {
                        UpdateItem   = result.Value;
                        UpdateExists = true;
                    }
                });

                if (TitleExists)
                {
                    chkTitle.Enabled = true;
                    chkTitle.Checked = true;
                    if (TitleItem.Value.madeTicket)
                    {
                        chkTitle.ForeColor = Color.Red;
                    }
                }

                if (DLCExists)
                {
                    chkDLC.Enabled   = true;
                    chkDLC.ForeColor = Color.Red;
                }

                if (UpdateExists)
                {
                    chkUpdate.Enabled = true;
                }


                btnDownload.Enabled = TitleExists || UpdateExists || DLCExists;
                lblDownloadingMetadata.Dispose();
            }
            else
            {
                var previousMax = progMain.Maximum;
                List <TaskQueueValues> taskValues = new List <TaskQueueValues> {
                };
                for (int i = 0; i < TitleQueue.Count; i++)
                {
                    var item = TitleQueue[i];

                    if ((AutoDownloadType & DownloadType.Game) != 0)
                    {
                        taskValues.Add(new TaskQueueValues(item, DownloadType.Game));
                    }

                    if ((AutoDownloadType & DownloadType.DLC) != 0)
                    {
                        taskValues.Add(new TaskQueueValues(item, DownloadType.DLC));
                    }

                    if ((AutoDownloadType & DownloadType.Update) != 0)
                    {
                        taskValues.Add(new TaskQueueValues(item, DownloadType.Update));
                    }
                }
                progMain.Maximum = taskValues.Count();

                int runningWorkers = 0;

                for (int i = 0; i < 4; i++)
                {
                    runningWorkers++;

                    #pragma warning disable CS4014
                    Task.Run(() =>
                    #pragma warning restore CS4014
                    {
                        TaskQueueValues value;
                        while (true)
                        {
                            lock (taskValues)
                            {
                                if (taskValues.Count == 0)
                                {
                                    break;
                                }

                                value = taskValues[0];
                                taskValues.RemoveAt(0);
                            }

                            var result = getDownloadItem(value.info, value.type);
                            if (result.HasValue)
                            {
                                DownloadQueue.Add(result.Value);
                            }

                            adjustProgMainValue(1);
                        }
                        runningWorkers--;
                    });
                }

                await Task.Run(() =>
                {
                    while (runningWorkers > 0)
                    {
                        Thread.Sleep(100);
                    }
                });

                progMain.Value   = 0;
                progMain.Maximum = previousMax;
                lblDownloadingMetadata.Dispose();
            }
            if (DownloadQueue.Count > 0 || AutoClose)
            {
                ProcessDownloadQueue(DownloadQueue.ToArray());
            }
        }
 public TaskQueueValues(TitleInfo info, DownloadType type)
 {
     this.info = info;
     this.type = type;
 }
Exemple #8
0
        public async Task <TitleListResult> getTitleList()
        {
            string          result;
            TitleListResult status            = TitleListResult.Online;
            string          dataCacheFilename = Path.Combine(Common.CachePath, "json");

            try
            {
                result = await client.DownloadStringTaskAsync(new Uri("http://" + Common.Settings.ticketWebsite + "/json"));

                File.WriteAllText(dataCacheFilename, result);
            }
            catch
            {
                if (File.Exists(dataCacheFilename))
                {
                    result = File.ReadAllText(dataCacheFilename);
                    status = TitleListResult.Offline;
                }
                else
                {
                    return(TitleListResult.NotFound);
                }
            }

            titles.Clear();

            dynamic json = JArray.Parse(result);

            foreach (dynamic obj in json)
            {
                if (((String)(obj.titleID)).Length == 16)
                {
                    TitleInfo info = new TitleInfo((String)(obj.titleID), (String)(obj.titleKey), (String)(obj.name), (String)(obj.region), "", obj.ticket == "1");

                    if (!info.titleID.StartsWith("0005000e") && info.hasTicket)
                    {
                        factory.StartNew(async(o) =>
                        {
                            await cacheTicket((TitleInfo)o);
                        }, info);
                    }

                    if (info.titleID.Length == 16 && allowedTitleTypes.Contains(info.titleID.Substring(4, 4)))
                    {
                        titles.Add(info);
                    }
                }
            }


            var dlc     = titles.Where(o => o.titleID.Substring(4, 4) == "000c").ToList();
            var updates = titles.Where(o => o.titleID.Substring(4, 4) == "000e").ToList();

            titles = titles.Where(o => o.titleID.Substring(4, 4) == "0000").ToList();

            foreach (TitleInfo info in dlc.ToArray())
            {
                TitleInfo[] existing = titles.Where(o => o.titleID == info.gameID).ToArray();

                if (existing.Length == 0)
                {
                    info.titleID  = info.gameID;
                    info.dlcKey   = info.titleKey;
                    info.titleKey = "";
                }
                else
                {
                    existing[frmList.COLUMN_INDEX_TITLE_ID].dlcKey = info.titleKey;
                    dlc.Remove(info);
                }
            }

            foreach (TitleInfo info in updates.ToArray())
            {
                TitleInfo[] existing = titles.Where(o => o.titleID == info.gameID).ToArray();

                if (existing.Length == 0)
                {
                    info.titleID   = info.gameID;
                    info.updateKey = info.titleKey;
                    info.titleKey  = "";
                }
                else
                {
                    existing[frmList.COLUMN_INDEX_TITLE_ID].updateKey = info.titleKey;
                    updates.Remove(info);
                }
            }

            titles.AddRange(dlc);
            titles.AddRange(updates);

            titles = titles.OrderBy(o => o.name).ToList();

            OnListUpdated();

            return(status);
        }