src/MonoTorrent.Client/MonoTorrent/Torrent.cs
/// <summary>
/// This method is called internally to load the information found within the "Info" section
/// of the .torrent file
/// </summary>
/// <param name="dictionary">The dictionary representing the Info section of the .torrent file</param>
This method is called internally to load the information found within the "Info" section
of the .torrent file
This method is called internally to load the information found within the " Info " section of the . torrent file
void ProcessInfo (BEncodedDictionary dictionary)
        {
            InfoMetadata = dictionary.Encode ();
            PieceLength = int.Parse (dictionary["piece length"].ToString ());
            bool hasV1Data = false;
            bool hasV2Data = false;
            if (dictionary.TryGetValue ("meta version", out BEncodedValue metaVersion)) {
                if (metaVersion is BEncodedNumber metadataVersion) {
                    hasV2Data = metadataVersion.Number == 2;
                }
            } else {
                hasV1Data = true;
            }
            hasV1Data = dictionary.ContainsKey ("pieces");
            hasV2Data |= dictionary.ContainsKey ("file tree");
            if (!hasV1Data) {
                if (hasV2Data) {
                    if (!SupportsV2Torrents) {
                        throw new TorrentException ("This torrent contains metadata for bittorrent v2 only. MonoTorrent only supports v1 torrents, or hybrid torrents with v1 and v2 metadata.");
                    }
                } else {
                    throw new TorrentException ("Unsupported torrent version detected.");
                }
            }
            if (hasV1Data)
                LoadHashPieces (((BEncodedString) dictionary["pieces"]).AsMemory ());
            foreach (KeyValuePair<BEncodedString, BEncodedValue> keypair in dictionary) {
                switch (keypair.Key.Text) {
                    case ("source"):
                        Source = keypair.Value.ToString ();
                        break;
                    case ("sha1"):
                        SHA1 = ((BEncodedString) keypair.Value).Span.ToArray ();
                        break;
                    case ("ed2k"):
                        ED2K = ((BEncodedString) keypair.Value).Span.ToArray ();
                        break;
                    case ("publisher-url.utf-8"):
                        if (keypair.Value.ToString ().Length > 0)
                            PublisherUrl = keypair.Value.ToString ();
                        break;
                    case ("publisher-url"):
                        if ((string.IsNullOrEmpty (PublisherUrl)) && (keypair.Value.ToString ().Length > 0))
                            PublisherUrl = keypair.Value.ToString ();
                        break;
                    case ("publisher.utf-8"):
                        if (keypair.Value.ToString ().Length > 0)
                            Publisher = keypair.Value.ToString ();
                        break;
                    case ("publisher"):
                        if ((string.IsNullOrEmpty (Publisher)) && (keypair.Value.ToString ().Length > 0))
                            Publisher = keypair.Value.ToString ();
                        break;
                    case ("files"):
                        if (Files == null)
                            Files = LoadTorrentFilesV1 ((BEncodedList) keypair.Value, PieceLength);
                        break;
                    case "file tree":
                        Files = LoadTorrentFilesV2 ((BEncodedDictionary) dictionary["file tree"], PieceLength);
                        break;
                    case ("name.utf-8"):
                        if (keypair.Value.ToString ().Length > 0)
                            Name = keypair.Value.ToString ();
                        break;
                    case ("name"):
                        if ((string.IsNullOrEmpty (Name)) && (keypair.Value.ToString ().Length > 0))
                            Name = keypair.Value.ToString ();
                        break;
                    case ("piece length"):  
                        break;
                    case ("length"):
                        break;      
                    case ("private"):
                        IsPrivate = (keypair.Value.ToString () == "1") ? true : false;
                        break;
                    default:
                        break;
                }
            }
            if (Files != null)
                Size = Files.Select (f => f.Length).Sum ();
            if (Files == null && hasV1Data)   
            {
                long length = long.Parse (dictionary["length"].ToString ());
                Size = length;
                string path = Name;
                var md5 = dictionary.TryGetValue("md5", out BEncodedValue value) ? ((BEncodedString) value).AsMemory () : ReadOnlyMemory<byte>.Empty;
                var ed2k = dictionary.TryGetValue ("ed2k", out value) ? ((BEncodedString) value).AsMemory () : ReadOnlyMemory<byte>.Empty;
                var sha1 = dictionary.TryGetValue ("sha1", out value) ? ((BEncodedString) value).AsMemory () : ReadOnlyMemory<byte>.Empty;
                int endPiece = Math.Min (Pieces.Count - 1, (int) ((Size + (PieceLength - 1)) / PieceLength));
                Files = Array.AsReadOnly (new[] { new TorrentFile (path, length, 0, endPiece, 0, md5, ed2k, sha1) });
            }
        }
void ProcessInfo ( BEncodedDictionary dictionary ) { InfoMetadata = dictionary . Encode ( ) ; PieceLength = int . Parse ( dictionary [ " " ] . ToString ( ) ) ; bool hasV1Data = false ; bool hasV2Data = false ; if ( dictionary . TryGetValue ( " " , out BEncodedValue metaVersion ) ) { if ( metaVersion is BEncodedNumber metadataVersion ) { hasV2Data = metadataVersion . Number == 2 ; } } else { hasV1Data = true ; } hasV1Data = dictionary . ContainsKey ( " " ) ; hasV2Data |= dictionary . ContainsKey ( " " ) ; if ( ! hasV1Data ) { if ( hasV2Data ) { if ( ! SupportsV2Torrents ) { throw new TorrentException ( " " ) ; } } else { throw new TorrentException ( " " ) ; } } if ( hasV1Data ) LoadHashPieces ( ( ( BEncodedString ) dictionary [ " " ] ) . AsMemory ( ) ) ; foreach ( KeyValuePair < BEncodedString , BEncodedValue > keypair in dictionary ) { switch ( keypair . Key . Text ) { case ( " " ) : Source = keypair . Value . ToString ( ) ; break ; case ( " " ) : SHA1 = ( ( BEncodedString ) keypair . Value ) . Span . ToArray ( ) ; break ; case ( " " ) : ED2K = ( ( BEncodedString ) keypair . Value ) . Span . ToArray ( ) ; break ; case ( " " ) : if ( keypair . Value . ToString ( ) . Length > 0 ) PublisherUrl = keypair . Value . ToString ( ) ; break ; case ( " " ) : if ( ( string . IsNullOrEmpty ( PublisherUrl ) ) && ( keypair . Value . ToString ( ) . Length > 0 ) ) PublisherUrl = keypair . Value . ToString ( ) ; break ; case ( " " ) : if ( keypair . Value . ToString ( ) . Length > 0 ) Publisher = keypair . Value . ToString ( ) ; break ; case ( " " ) : if ( ( string . IsNullOrEmpty ( Publisher ) ) && ( keypair . Value . ToString ( ) . Length > 0 ) ) Publisher = keypair . Value . ToString ( ) ; break ; case ( " " ) : if ( Files == null ) Files = LoadTorrentFilesV1 ( ( BEncodedList ) keypair . Value , PieceLength ) ; break ; case " " : Files = LoadTorrentFilesV2 ( ( BEncodedDictionary ) dictionary [ " " ] , PieceLength ) ; break ; case ( " " ) : if ( keypair . Value . ToString ( ) . Length > 0 ) Name = keypair . Value . ToString ( ) ; break ; case ( " " ) : if ( ( string . IsNullOrEmpty ( Name ) ) && ( keypair . Value . ToString ( ) . Length > 0 ) ) Name = keypair . Value . ToString ( ) ; break ; case ( " " ) : break ; case ( " " ) : break ; case ( " " ) : IsPrivate = ( keypair . Value . ToString ( ) == " " ) ? true : false ; break ; default : break ; } } if ( Files != null ) Size = Files . Select ( f => f . Length ) . Sum ( ) ; if ( Files == null && hasV1Data ) { long length = long . Parse ( dictionary [ " " ] . ToString ( ) ) ; Size = length ; string path = Name ; var md5 = dictionary . TryGetValue ( " " , out BEncodedValue value ) ? ( ( BEncodedString ) value ) . AsMemory ( ) : ReadOnlyMemory < byte > . Empty ; var ed2k = dictionary . TryGetValue ( " " , out value ) ? ( ( BEncodedString ) value ) . AsMemory ( ) : ReadOnlyMemory < byte > . Empty ; var sha1 = dictionary . TryGetValue ( " " , out value ) ? ( ( BEncodedString ) value ) . AsMemory ( ) : ReadOnlyMemory < byte > . Empty ; int endPiece = Math . Min ( Pieces . Count - 1 , ( int ) ( ( Size + ( PieceLength - 1 ) ) / PieceLength ) ) ; Files = Array . AsReadOnly ( new [ ] { new TorrentFile ( path , length , 0 , endPiece , 0 , md5 , ed2k , sha1 ) } ) ; } }
This method is called internally to load the information found within the "Info" section
of the .torrent file
This method is called internally to load the information found within the " Info " section of the . torrent file
// This is the list of files using the v1 torrent format. // Only load if we have not processed filesv2 // This is the list of files using the v2 torrent format. // Already handled // This is a singlefile torrent // Not a multi-file torrent
dictionary
BEncodedDictionary
The dictionary representing the Info section of the .torrent file
The dictionary representing the Info section of the . torrent file