Beispiel #1
0
        public bool LoadResumeDat()
        {
            BEncodeLoader bel = new BEncodeLoader();

            ResumeDat = bel.Load(ResumeDatPath);
            return(ResumeDat != null);
        }
Beispiel #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);
                    }
                }
            }
        }
Beispiel #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);
                    }
                }
            }
        }
Beispiel #4
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 == null)
                return false;

            BTItem bti = btFile.GetItem("info");
            if ((bti == null) || (bti.Type != BTChunk.kDictionary))
                return false;

            BTDictionary infoDict = (BTDictionary) (bti);

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

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

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

            BTString torrentPieces = (BTString) (bti);

            bti = infoDict.GetItem("files");

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

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

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

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

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

                // don't worry about updating overallPosition as this is the only file in the torrent
            }
            else
            {
                Int64 overallPosition = 0;
                Int64 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++)
                {
                    this.Prog(100 * i / fileList.Items.Count);
                    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");
                    Int64 fileSize = ((BTInteger) fileSizeI).Value;

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

                    this.NewTorrentEntry(torrentFile, i);

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

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

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

                    int sizeInPieces = (int) (fileSize / pieceSize);
                    if (fileSize % pieceSize != 0)
                        sizeInPieces++; // another partial piece

                    lastPieceLeftover = (lastPieceLeftover + (Int32) ((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();
            }

            this.Prog(0);

            return true;
        }
Beispiel #5
0
 public bool LoadResumeDat(CommandLineArgs args)
 {
     BEncodeLoader bel = new BEncodeLoader();
     this.ResumeDat = bel.Load(this.ResumeDatPath);
     return (this.ResumeDat != null);
 }
Beispiel #6
0
        public System.Collections.Generic.List<TorrentEntry> AllFilesBeingDownloaded(TVSettings settings, CommandLineArgs args)
        {
            System.Collections.Generic.List<TorrentEntry> r = new System.Collections.Generic.List<TorrentEntry>();

            BEncodeLoader bel = new BEncodeLoader();
            foreach (BTDictionaryItem it in this.ResumeDat.GetDict().Items)
            {
                if ((it.Type != BTChunk.kDictionaryItem))
                    continue;

                BTDictionaryItem dictitem = (BTDictionaryItem) (it);

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

                string torrentFile = dictitem.Key;
                BTDictionary d2 = (BTDictionary) (dictitem.Data);

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

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

                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 == null)
                    continue;

                List<string> a = tor.AllFilesInTorrent();
                if (a != null)
                {
                    int c = 0;

                    p = d2.GetItem("path");
                    if ((p == 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 (string s in a)
                    {
                        if ((c < prioString.Data.Length) && (prioString.Data[c] != BTPrio.Skip))
                        {
                            string saveTo = Helpers.FileInFolder(defaultFolder, settings.FilenameFriendly(s)).Name;
                            if (hasTargets)
                            {
                                // see if there is a target for this (the c'th) file
                                for (int i = 0; i < targetList.Items.Count; i++)
                                {
                                    BTList l = (BTList) (targetList.Items[i]);
                                    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;
                            TorrentEntry te = new TorrentEntry(torrentFile, saveTo, percent);
                            r.Add(te);
                        }
                        c++;
                    }
                }
            }

            return r;
        }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #9
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 = this.mDoc.Settings.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);
                }
            }
        }
Beispiel #10
0
 public bool LoadResumeDat()
 {
     BEncodeLoader bel = new BEncodeLoader();
     this.ResumeDat = bel.Load(this.ResumeDatPath);
     return (this.ResumeDat != null);
 }