示例#1
0
        public async System.Threading.Tasks.Task LoadFastResumeData()
        {
            IsTsunamiEnabled = false;
            if (Directory.Exists("Fastresume"))
            {
                // FARE TRY CATCH FINALLY
                log.Debug("processing");
                //Window loading = new Pages.Loading();
                //loading.Show();
                //System.Windows.Controls.Label lb = (System.Windows.Controls.Label)loading.FindName("txtLoading");
                //System.Windows.Controls.ProgressBar pb = (System.Windows.Controls.ProgressBar)loading.FindName("progressLoading");

                string[] files = Directory.GetFiles("Fastresume", "*.fastresume");

                //int i = 0;

                if (files.Length > 0)
                {
                    // fast resuming
                    //pb.Maximum = files.Length+0.01;
                    FileToLoad = files.Length + 0.01;

                    foreach (string s in files)
                    {
                        //i++;
                        FileLoading++;
                        //lb.Content = "Loading "+i+" of "+files.Length+" torrents";
                        StringLoading = "Loading " + FileLoading + " of " + files.Length + " torrents";
                        log.Info(stringLoading);
                        //pb.Value = i;
                        var data      = File.ReadAllBytes(s);
                        var info_hash = Path.GetFileNameWithoutExtension(s);
                        var filename  = "Fastresume/" + info_hash + ".torrent";
                        Core.TorrentInfo ti;
                        if (File.Exists(filename))
                        {
                            ti = new Core.TorrentInfo(filename);
                        }
                        else
                        {
                            ti = new Core.TorrentInfo(new Core.Sha1Hash(info_hash));
                        }
                        using (var atp = new Core.AddTorrentParams())
                            using (ti)
                            {
                                atp.ti          = ti;
                                atp.save_path   = Settings.User.PathDownload;
                                atp.resume_data = (sbyte[])(Array)data;
                                atp.flags      &= ~Core.ATPFlags.flag_auto_managed; // remove auto managed flag
                                atp.flags      &= ~Core.ATPFlags.flag_paused;       // remove pause on added torrent
                                await System.Threading.Tasks.Task.Run(() => _torrentSession.add_torrent(atp));
                            }
                        log.Debug("{0} loaded. waiting 250ms", FileLoading);
                        await System.Threading.Tasks.Task.Delay(250);
                    }
                }
                else
                {
                    log.Debug("nothing to fast resume, sleep");
                    //lb.Content = "Tsunami is loading...";
                    StringLoading = "Tsunami is loading...";
                    //pb.Maximum = 10.001;
                    FileToLoad = 10.001;
                    while (FileLoading < 10)
                    {
                        //    i++;
                        FileLoading++;
                        //    pb.Value = i;
                        await System.Threading.Tasks.Task.Delay(250);
                    }
                }
                //loading.Close();
            }
            else
            {
                log.Debug("no fastresume folder found, creating");
                Directory.CreateDirectory("Fastresume");
                StringLoading = "Tsunami is loading...";
                FileToLoad    = 10.001;
                while (FileLoading < 10)
                {
                    FileLoading++;
                    await System.Threading.Tasks.Task.Delay(250);
                }
            }
            IsTsunamiEnabled = true;
            log.Debug("finished");
        }