public static TorrentInfo ParseInfoSection(BDictionary dic) { var info = new TorrentInfo(); bool?singleFile = null; foreach (var(key, value) in dic) { if (key == Constants.InfoNameKey) { info.Name = (BString)value; } else if (key == Constants.InfoLengthKey) { if (singleFile == false) { throw new ParsingException(MalformedTorrent); } singleFile = true; info.Files.Add((null, (BNumber)value)); } else if (key == Constants.InfoPieceLengthKey) { info.PieceLength = (BNumber)value; } else if (key == Constants.InfoPiecesKey) { info.Pieces = (BString)value; } else if (key == Constants.InfoFilesKey) { if (singleFile == true) { throw new ParsingException(MalformedTorrent); } singleFile = false; info.Files.AddRange(((BList)value).Select(_ => { var dict = (BDictionary)_; return((BList)dict[Constants.InfoFilePathKey], (BNumber)dict[Constants.InfoFileLengthKey]); })); } else { info.Extensions.Add(key, value); } } if (info.Files.Count <= 0 || info.PieceLength <= 0 || info.Pieces == null) { throw new ParsingException(MalformedTorrent); } return(info); }
public static BDictionary GetInfoDictionary(TorrentInfo info) { var infoDic = new BDictionary { { Constants.InfoPieceLengthKey, info.PieceLength }, { Constants.InfoPiecesKey, info.Pieces } }; if (info.Name != null) { infoDic.Add(Constants.InfoNameKey, info.Name); } foreach (var(key, value) in info.Extensions) { infoDic.Add(key, value); } if (info.Files.Count == 1) { var(path, len) = info.Files[0]; infoDic.Add(Constants.InfoLengthKey, len); } else { var lst = new BList(info.Files.Count); foreach (var(key, value) in info.Files) { var fdic = new BDictionary { { Constants.InfoFilePathKey, key }, { Constants.InfoFileLengthKey, value } }; lst.Add(fdic); } infoDic.Add(Constants.InfoFilesKey, lst); } return(infoDic); }