Esempio n. 1
0
        public bool LoadResumeDat()
        {
            BEncodeLoader bel = new BEncodeLoader();

            ResumeDat = bel.Load(ResumeDatPath);
            return(ResumeDat != null);
        }
Esempio n. 2
0
        private void RefreshResumeDat()
        {
            if (!this.CheckResumeDatPath())
            {
                return;
            }

            List <string> checkedItems = new List <String>();

            foreach (string torrent in this.lbUTTorrents.CheckedItems)
            {
                checkedItems.Add(torrent);
            }

            this.lbUTTorrents.Items.Clear();
            // open resume.dat file, fill checked list box with torrents available to choose from

            string file = TVSettings.Instance.ResumeDatPath;

            if (!File.Exists(file))
            {
                return;
            }
            BEncodeLoader bel       = new BEncodeLoader();
            BTFile        resumeDat = bel.Load(file);

            if (resumeDat == null)
            {
                return;
            }
            BTDictionary dict = resumeDat.GetDict();

            for (int i = 0; i < dict.Items.Count; i++)
            {
                BTItem it = dict.Items[i];
                if (it.Type == BTChunk.kDictionaryItem)
                {
                    BTDictionaryItem d2 = (BTDictionaryItem)(it);
                    if ((d2.Key != ".fileguard") && (d2.Data.Type == BTChunk.kDictionary))
                    {
                        this.lbUTTorrents.Items.Add(d2.Key);
                    }
                }
            }

            foreach (string torrent in checkedItems)
            {
                for (int i = 0; i < this.lbUTTorrents.Items.Count; i++)
                {
                    if (this.lbUTTorrents.Items[i].ToString() == torrent)
                    {
                        this.lbUTTorrents.SetItemChecked(i, true);
                    }
                }
            }
        }
Esempio n. 3
0
        private void SetupResumeDats()
        {
            List <string> checkedItems = new List <string>();

            foreach (string torrent in lbUTTorrents.CheckedItems)
            {
                checkedItems.Add(torrent);
            }

            lbUTTorrents.Items.Clear();

            string resume_dir = Path.Combine(Path.GetDirectoryName(TVSettings.Instance.ResumeDatPath), "resume_dir");

            ResumeDats = new Dictionary <string, string>();

            string[] files = Directory.GetFiles(resume_dir, "*.dat");
            foreach (string file in files)
            {
                BEncodeLoader bel       = new BEncodeLoader();
                BTFile        resumeDat = bel.Load(file);
                if (resumeDat == null)
                {
                    break;
                }
                BTDictionary dict = resumeDat.GetDict();
                foreach (BTItem it in dict.Items)
                {
                    if (it.Type == BTChunk.kDictionaryItem)
                    {
                        BTDictionaryItem d2 = (BTDictionaryItem)(it);
                        if ((d2.Key != ".fileguard") && (d2.Data.Type == BTChunk.kDictionary))
                        {
                            ResumeDats.Add(Path.GetFileNameWithoutExtension(file), d2.Key);
                            lbUTTorrents.Items.Add(d2.Key);
                        }
                    }
                }
            }

            foreach (string torrent in checkedItems)
            {
                for (int i = 0; i < lbUTTorrents.Items.Count; i++)
                {
                    if (lbUTTorrents.Items[i].ToString() == torrent)
                    {
                        lbUTTorrents.SetItemChecked(i, true);
                    }
                }
            }
        }
Esempio n. 4
0
        public BTFile Load(string filename)
        {
            BTFile f = new BTFile();

            FileStream sr = null;
            try
            {
                sr = new FileStream(filename, FileMode.Open, FileAccess.Read);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "TVRename Torrent Reader", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return null;
            }

            while (sr.Position < sr.Length)
                f.Items.Add(this.ReadNext(sr));

            sr.Close();

            return f;
        }
Esempio n. 5
0
        public BTFile Load(string filename)
        {
            BTFile f = new BTFile();

            System.IO.FileStream sr;
            try
            {
                sr = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return(null);
            }

            while (sr.Position < sr.Length)
            {
                f.Items.Add(ReadNext(sr));
            }

            sr.Close();

            return(f);
        }
Esempio n. 6
0
 public bool LoadResumeDat(CommandLineArgs args)
 {
     BEncodeLoader bel = new BEncodeLoader();
     this.ResumeDat = bel.Load(this.ResumeDatPath);
     return (this.ResumeDat != null);
 }
Esempio n. 7
0
        public bool ProcessTorrentFile(string torrentFile, TreeView tvTree, CommandLineArgs args)
        {
            // ----------------------------------------
            // read in torrent file

            if (tvTree != null)
            {
                tvTree.Nodes.Clear();
            }

            BEncodeLoader bel    = new BEncodeLoader();
            BTFile        btFile = bel.Load(torrentFile);

            if (btFile is null)
            {
                return(false);
            }

            BTItem bti = btFile.GetItem("info");

            if ((bti is null) || (bti.Type != BTChunk.kDictionary))
            {
                return(false);
            }

            BTDictionary infoDict = (BTDictionary)(bti);

            bti = infoDict.GetItem("piece length");
            if ((bti is null) || (bti.Type != BTChunk.kInteger))
            {
                return(false);
            }

            long pieceSize = ((BTInteger)bti).Value;

            bti = infoDict.GetItem("pieces");
            if ((bti is null) || (bti.Type != BTChunk.kString))
            {
                return(false);
            }

            BTString torrentPieces = (BTString)(bti);

            bti = infoDict.GetItem("files");

            if (bti is null) // single file torrent
            {
                bti = infoDict.GetItem("name");
                if ((bti is null) || (bti.Type != BTChunk.kString))
                {
                    return(false);
                }

                BTString di            = (BTString)(bti);
                string   nameInTorrent = di.AsString();

                BTItem fileSizeI = infoDict.GetItem("length");
                long   fileSize  = ((BTInteger)fileSizeI).Value;

                NewTorrentEntry(torrentFile, -1);
                if (DoHashChecking)
                {
                    byte[] torrentPieceHash = torrentPieces.StringTwentyBytePiece(0);

                    FileInfo fi = FindLocalFileWithHashAt(torrentPieceHash, 0, pieceSize, fileSize);
                    if (fi != null)
                    {
                        FoundFileOnDiskForFileInTorrent(torrentFile, fi, -1, nameInTorrent);
                    }
                    else
                    {
                        DidNotFindFileOnDiskForFileInTorrent(torrentFile, -1, nameInTorrent);
                    }
                }

                FinishedTorrentEntry(torrentFile, -1, nameInTorrent);

                // don't worry about updating overallPosition as this is the only file in the torrent
            }
            else
            {
                long overallPosition   = 0;
                long lastPieceLeftover = 0;

                if (bti.Type != BTChunk.kList)
                {
                    return(false);
                }

                BTList fileList = (BTList)(bti);

                // list of dictionaries
                for (int i = 0; i < fileList.Items.Count; i++)
                {
                    Prog(100 * i / fileList.Items.Count, i.ToString());
                    if (fileList.Items[i].Type != BTChunk.kDictionary)
                    {
                        return(false);
                    }

                    BTDictionary file    = (BTDictionary)(fileList.Items[i]);
                    BTItem       thePath = file.GetItem("path");
                    if (thePath.Type != BTChunk.kList)
                    {
                        return(false);
                    }

                    BTList pathList = (BTList)(thePath);
                    // want the last of the items in the list, which is the filename itself
                    int n = pathList.Items.Count - 1;
                    if (n < 0)
                    {
                        return(false);
                    }

                    BTString fileName = (BTString)(pathList.Items[n]);

                    BTItem fileSizeI = file.GetItem("length");
                    long   fileSize  = ((BTInteger)fileSizeI).Value;

                    int pieceNum = (int)(overallPosition / pieceSize);
                    if (overallPosition % pieceSize != 0)
                    {
                        pieceNum++;
                    }

                    NewTorrentEntry(torrentFile, i);

                    if (DoHashChecking)
                    {
                        byte[] torrentPieceHash = torrentPieces.StringTwentyBytePiece(pieceNum);

                        FileInfo fi = FindLocalFileWithHashAt(torrentPieceHash, lastPieceLeftover, pieceSize, fileSize);
                        if (fi != null)
                        {
                            FoundFileOnDiskForFileInTorrent(torrentFile, fi, i, fileName.AsString());
                        }
                        else
                        {
                            DidNotFindFileOnDiskForFileInTorrent(torrentFile, i, fileName.AsString());
                        }
                    }

                    FinishedTorrentEntry(torrentFile, i, fileName.AsString());

                    int sizeInPieces = (int)(fileSize / pieceSize);
                    if (fileSize % pieceSize != 0)
                    {
                        sizeInPieces++; // another partial piece
                    }
                    lastPieceLeftover = (lastPieceLeftover + (int)((sizeInPieces * pieceSize) - fileSize)) % pieceSize;
                    overallPosition  += fileSize;
                } // for each file in the torrent
            }

            if (tvTree != null)
            {
                tvTree.BeginUpdate();
                btFile.Tree(tvTree.Nodes);
                tvTree.ExpandAll();
                tvTree.EndUpdate();
                tvTree.Update();
            }

            Prog(0, string.Empty);

            return(true);
        }
Esempio n. 8
0
        public List <TorrentEntry> AllFilesBeingDownloaded()
        {
            List <TorrentEntry> r = new List <TorrentEntry>();

            BEncodeLoader bel = new BEncodeLoader();

            foreach (BTDictionaryItem dictitem in ResumeDat.GetDict().Items)
            {
                if ((dictitem.Type != BTChunk.kDictionaryItem))
                {
                    continue;
                }

                if ((dictitem.Key == ".fileguard") || (dictitem.Data.Type != BTChunk.kDictionary))
                {
                    continue;
                }

                if (dictitem.Data is BTError err)
                {
                    logger.Error($"Error finding BT items: {err.Message}");
                    return(r);
                }

                BTDictionary d2 = (BTDictionary)(dictitem.Data);

                BTItem p = d2.GetItem("prio");
                if ((p is null) || (p.Type != BTChunk.kString))
                {
                    continue;
                }

                BTString prioString    = (BTString)(p);
                string   directoryName = Path.GetDirectoryName(ResumeDatPath) + System.IO.Path.DirectorySeparatorChar;

                string torrentFile = dictitem.Key;
                if (!File.Exists(torrentFile))                 // if the torrent file doesn't exist
                {
                    torrentFile = directoryName + torrentFile; // ..try prepending the resume.dat folder's path to it.
                }
                if (!File.Exists(torrentFile))
                {
                    continue; // can't find it.  give up!
                }
                BTFile tor = bel.Load(torrentFile);
                if (tor is null)
                {
                    continue;
                }

                List <string> a = tor.AllFilesInTorrent();
                if (a is null)
                {
                    continue;
                }

                int c = 0;

                p = d2.GetItem("path");
                if ((p is null) || (p.Type != BTChunk.kString))
                {
                    continue;
                }

                string defaultFolder = ((BTString)p).AsString();

                BTItem targets    = d2.GetItem("targets");
                bool   hasTargets = ((targets != null) && (targets.Type == BTChunk.kList));
                BTList targetList = (BTList)(targets);

                //foreach (var i in d2.Items)
                //{
                //logger.Info($"   {i.Key}  {i.Data.AsText()}");
                //}

                foreach (string s in a)
                {
                    if ((c < prioString.Data.Length) && (prioString.Data[c] != BTPrio.Skip))
                    {
                        try
                        {
                            string saveTo = FileHelper
                                            .FileInFolder(defaultFolder, TVSettings.Instance.FilenameFriendly(s)).Name;

                            if (hasTargets)
                            {
                                // see if there is a target for this (the c'th) file
                                foreach (BTItem t in targetList.Items)
                                {
                                    BTList    l    = (BTList)(t);
                                    BTInteger n    = (BTInteger)(l.Items[0]);
                                    BTString  dest = (BTString)(l.Items[1]);
                                    if (n.Value == c)
                                    {
                                        saveTo = dest.AsString();
                                        break;
                                    }
                                }
                            }

                            int          percent   = (a.Count == 1) ? PercentBitsOn((BTString)(d2.GetItem("have"))) : -1;
                            bool         completed = ((BTInteger)d2.GetItem("order")).Value == -1;
                            TorrentEntry te        = new TorrentEntry(torrentFile, saveTo, percent, completed, torrentFile);
                            r.Add(te);
                        }
                        catch (System.IO.PathTooLongException ptle)
                        {
                            //this is not the file we are looking for
                            logger.Debug(ptle);
                        }
                    }

                    c++;
                }
            }

            return(r);
        }
Esempio n. 9
0
 public bool LoadResumeDat()
 {
     BEncodeLoader bel = new BEncodeLoader();
     this.ResumeDat = bel.Load(this.ResumeDatPath);
     return (this.ResumeDat != null);
 }