/// <summary> /// This method is called internally to load in all the files found within the "Files" section /// of the .torrents infohash /// </summary> /// <param name="list">The list containing the files available to download</param> private void LoadTorrentFiles(BEncodedList list) { var files = new List <TorrentFile>(); var sb = new StringBuilder(32); foreach (var dict in list.Cast <BEncodedDictionary>()) { long length = 0; string path = null; byte[] md5sum = null; byte[] ed2k = null; byte[] sha1 = null; foreach (var keypair in dict) { switch (keypair.Key.Text) { case ("sha1"): sha1 = ((BEncodedString)keypair.Value).TextBytes; break; case ("ed2k"): ed2k = ((BEncodedString)keypair.Value).TextBytes; break; case ("length"): length = long.Parse(keypair.Value.ToString()); break; case ("path.utf-8"): foreach (var str in ((BEncodedList)keypair.Value).Cast <BEncodedString>()) { sb.Append(str.Text); sb.Append(Path.DirectorySeparatorChar); } path = sb.ToString(0, sb.Length - 1); sb.Remove(0, sb.Length); break; case ("path"): if (string.IsNullOrEmpty(path)) { foreach (var str in ((BEncodedList)keypair.Value).Cast <BEncodedString>()) { sb.Append(str.Text); sb.Append(Path.DirectorySeparatorChar); } path = sb.ToString(0, sb.Length - 1); sb.Remove(0, sb.Length); } break; case ("md5sum"): md5sum = ((BEncodedString)keypair.Value).TextBytes; break; default: break; //FIXME: Log unknown values } } // A zero length file always belongs to the same piece as the previous file int endIndex; int startIndex; if (length == 0) { if (files.Count > 0) { startIndex = files[files.Count - 1].EndPieceIndex; endIndex = files[files.Count - 1].EndPieceIndex; } else { startIndex = 0; endIndex = 0; } } else { startIndex = (int)(Size / PieceLength); endIndex = (int)((Size + length) / PieceLength); if ((Size + length) % PieceLength == 0) { endIndex--; } } Size += length; files.Add(new TorrentFile(path, length, path, startIndex, endIndex, md5sum, ed2k, sha1)); } Files = files.ToArray(); }