Skip to content

Commit bcf5292

Browse files
committed
Update README
1 parent 7e8246c commit bcf5292

File tree

3 files changed

+124
-115
lines changed

3 files changed

+124
-115
lines changed

Assets/icon_128.png

7.37 KB
Loading

Assets/icon_64.png

4.04 KB
Loading

README.md

Lines changed: 124 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
![Icon](Assets/icon_64.png)
2+
13
BencodeNET
24
==========
35
[![license](https://img.shields.io/badge/license-Unlicense-blue.svg)](https://github.com/Krusen/BencodeNET/blob/master/LICENSE.md)
@@ -7,143 +9,105 @@ BencodeNET
79
[![NuGet](https://buildstats.info/nuget/bencodenet?includePreReleases=false)](https://www.nuget.org/packages/BencodeNET/)
810
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET.svg?type=shield)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET?ref=badge_shield)
911

10-
A .NET library for encoding and decoding bencode.
12+
A lightweight and fast .NET library for encoding and decoding bencode (e.g. torrent files and BitTorrent client/tracker communication).
1113

1214
- http://en.wikipedia.org/wiki/Bencode
1315
- https://wiki.theory.org/BitTorrentSpecification#Bencoding
1416

15-
Overview
17+
The main focus of this library is on supporting the bencode format; torrent file reading/manipulation is secondary.
18+
19+
Contents
1620
--------
1721

22+
- [Project status](#project-status)
1823
- [Installation](#installation)
19-
- [Usage](#usage)
24+
- [Getting started](#getting-started)
25+
- [Parsing](#parsing)
26+
- [Encoding](#encoding)
27+
- [String character encoding](#string-character-encoding)
2028
- [Torrents](#torrents)
2129
- [File modes](#file-modes)
2230
- [Non-standard fields](#non-standard-fields)
23-
- [Parsing](#parsing)
24-
- [Encoding](#encoding)
25-
- [String Character Encoding](#string-character-encoding)
26-
- [Upgrading from 1.x to 2.0](#upgrading-from-version-1x-to-20)
31+
- [Roadmap](#roadmap)
32+
- [Building the project](#building-the-project)
33+
- [Contributing](#contributing)
34+
- [Support](#support)
35+
36+
Project status
37+
--------------
38+
The project is in maintenance mode and only getting updated if issues are being reported or if new features are requested.
39+
40+
So, while I can't promise anything, go ahead and report any issues or feature requests by creating a new issue.
2741

2842

2943
Installation
3044
------------
31-
32-
Install the package **BencodeNET** from [NuGet](https://www.nuget.org/packages/BencodeNET/)
33-
or install it from the [Package Manager Console](https://docs.microsoft.com/da-dk/nuget/tools/package-manager-console):
45+
Install the package **BencodeNET** from [NuGet](https://www.nuget.org/packages/BencodeNET/), using `<PackageReference>` or from the command line:
3446

3547
```
36-
PM> Install-Package BencodeNET
37-
```
38-
39-
Usage
40-
-----
41-
### Torrents
42-
Working with torrent files:
43-
44-
```C#
45-
// Parse torrent by specifying the file path
46-
var parser = new BencodeParser(); // Default encoding is Encoding.UT8F, but you can specify another if you need to
47-
var torrent = parser.Parse<Torrent>("C:\ubuntu.torrent");
48-
49-
// Alternatively, handle the stream yourself
50-
using (var stream = File.OpenRead("C:\ubuntu.torrent"))
51-
{
52-
torrent = parser.Parse<Torrent>(stream);
53-
}
54-
55-
// Calculate the info hash
56-
string infoHash = torrent.GetInfoHash();
57-
// "B415C913643E5FF49FE37D304BBB5E6E11AD5101"
58-
59-
// or as bytes instead of a string
60-
byte[] infoHashBytes = torrent.GetInfoHashBytes();
61-
62-
// Get Magnet link
63-
string magnetLink = torrent.GetMagnetLink();
64-
// magnet:?xt=urn:btih:1CA512A4822EDC7C1B1CE354D7B8D2F84EE11C32&dn=ubuntu-14.10-desktop-amd64.iso&tr=http://torrent.ubuntu.com:6969/announce&tr=http://ipv6.torrent.ubuntu.com:6969/announce
48+
// .csproj using PackageReference
49+
<PackageReference Include="BencodeNET" Version="2.3.0" />
6550
66-
// Convert Torrent to its BDictionary representation
67-
BDictionary bencode = torrent.ToBDictionary();
51+
// .NET CLI
52+
> dotnet add package BencodeNET
6853
```
6954

70-
#### File modes
71-
The property `FileMode` indicates if the torrent is single-file or multi-file.
72-
73-
For single-file torrents the `File` property contains the relevant file info.
74-
The `Files` property is null.
75-
76-
For multi-file torrents the `Files` property contains a list of file info and the directory name.
77-
The `File` property is null.
78-
79-
#### Non-standard fields
80-
The `ExtraFields` property is for any non-standard fields which are not accessible through any other property.
81-
Data set on this property will overwrite any data from the `Torrent` itself when encoding it. This way you are able to add to or owerwrite fields.
8255

56+
Getting started
57+
---------------
8358
### Parsing
84-
Simple parsing of a bencoded string:
59+
Here are some simple examples for parsing bencode strings directly.
8560

8661
```C#
62+
using BencodeNET.Parsing;
63+
using BencodeNET.Objects;
64+
8765
var parser = new BencodeParser();
88-
BString bstring = parser.ParseString("12:Hellow World!");
66+
67+
// Parse unknown type
68+
IBObject bstring = parser.ParseString("12:Hellow World!");
8969
// "Hello World!" (BString)
9070
9171
// If you know the type of the bencode you are parsing, you can use the generic version of `ParseString()` instead.
92-
var bstring2 = parser.ParseString<BString>("12:Hello World!");
72+
BString bstring = parser.ParseString<BString>("12:Hello World!");
9373
// "Hello World!" (BString)
9474
95-
var bnumber = parser.ParseString<BNumber>("i42e");
75+
BNumber bnumber = parser.ParseString<BNumber>("i42e");
9676
// 42 (BNumber)
9777
98-
var blist = parser.ParseString<BList>("l3:foo3:bari42ee");
78+
BList blist = parser.ParseString<BList>("l3:foo3:bari42ee");
9979
// { "foo", "bar", 42 } (BList)
10080
101-
var bdictionary = parser.ParseString<BDictionary>("d3:fooi42e5:Hello6:World!e");
81+
BDictionary bdictionary = parser.ParseString<BDictionary>("d3:fooi42e5:Hello6:World!e");
10282
// { { "foo", 42 }, { "Hello", "World" } } (BDictionary)
10383
```
10484

105-
If you are unsure of the type you can just use the non-generic version:
85+
Usually you would probably either parse a `Stream` of some kind or a `PipeReader` if using .NET Core.
10686

10787
```C#
108-
IBObject bobject = parser.ParseString("12:Hello World!");
109-
110-
if (bobject is BString)
111-
{
112-
// The parsed object is a string
113-
}
114-
```
115-
116-
It is also possible to decode directly from a stream instead, for example a `FileStream` or a `MemoryStream`:
117-
118-
```C#
119-
using (var stream = File.OpenRead("Ubuntu.torrent"))
120-
{
121-
var bdictionary = parser.Parse<BDictionary>(stream);
122-
}
88+
BDictionary bdictionary = parser.Parse<BDictionary>(stream);
89+
BDictionary bdictionary = await parser.ParseAsync<BDictionary>(stream);
90+
BDictionary bdictionary = await parser.ParseAsync<BDictionary>(pipeReader);
12391
```
12492

12593
### Encoding
126-
You have the option to encode `BObject`s either as a `string`, a `byte[]`, to a `Stream` or directly to a file path.
94+
Encoding an object is simple and can be done in the following ways:
12795

12896
```C#
12997
var bstring = new BString("Hello World!");
98+
13099
bstring.EncodeAsString(); // "12:Hello World!"
131100
bstring.EncodeAsBytes(); // [ 49, 50, 58, 72, ... ]
132-
bstring.EncodeTo("C:\\data.bencode"); // Writes "12:Hello World!" to the specified file
133-
bstring.EncodeTo(new MemoryStream());
134101
135-
var bnumber = new BNumber(42);
136-
bnumber.EncodeAsString(); // "i42e"
137-
138-
var blist = new BList { "foo", 42, "bar" };
139-
blist.EncodeAsString(); // "l3:fooi42e3:bare"
140-
141-
var bdictionary = new BDictionary { { "foo", 42 }, { "Hello", "World!" } };
142-
bdictionary.EncodeAsString() // "d3:fooi42e5:Hello6:World!e"
102+
bstring.EncodeTo("C:\\data.bencode"); // Writes "12:Hello World!" to the specified file
103+
bstring.EncodeTo(stream);
104+
await bstring.EncodeToAsync(stream);
105+
bstring.EncodeTo(pipeWriter);
106+
await bstring.EncodeToAsync(pipeWriter);
143107
```
144108

145-
String Character Encoding
146-
-------------------------
109+
### String character encoding
110+
147111
By default `Encoding.UTF8` is used when rendering strings.
148112

149113
When parsing a string directly the encoding is used to convert the string to an array of bytes.
@@ -152,12 +116,14 @@ If no encoding is passed to `ToString` it will use the encoding the `BString` wa
152116

153117
```C#
154118
// Using the default encoding from Bencode.DefaultEncoding (UTF8)
155-
var bstring = Bencode.DecodeString("21:æøå äö èéê ñ");
119+
var parser = new BencodeParser();
120+
var bstring = parser.ParseString("21:æøå äö èéê ñ");
156121
bstring.ToString() // "æøå äö èéê ñ"
157122
bstring.ToString(Encoding.UTF8) // "æøå äö èéê ñ"
158123
159124
// Using ISO-8859-1
160-
bstring = Bencode.DecodeString("12:æøå äö èéê ñ", Encoding.GetEncoding("ISO-8859-1"));
125+
var parser = new BencodeParser(Encoding.GetEncoding("ISO-8859-1"));
126+
bstring = parser.ParseString("12:æøå äö èéê ñ");
161127
bstring.ToString(); // "æøå äö èéê ñ"
162128
bstring.ToString(Encoding.UTF8); // "??? ?? ??? ?"
163129
```
@@ -182,7 +148,7 @@ bstring.ToString(Encoding.GetEncoding("ISO-8859-1"));
182148
183149
// You have to specify the used encoding when creating the parser
184150
// BStrings will then use that as the default when encoding the string
185-
parser = new BencodeParser(Encoding.GetEncoding("ISO-8859-1"));
151+
var parser = new BencodeParser(Encoding.GetEncoding("ISO-8859-1"));
186152
bstring = parser.Parse<BString>(bytes);
187153
bstring.ToString();
188154
// "æøå äö èéê ñ"
@@ -207,36 +173,79 @@ blist.EncodeAsString(Encoding.UTF8); // "l12:??? ?? ??? ?e
207173
blist.EncodeAsString(Encoding.GetEncoding("ISO-8859-1")); // "l12:æøå äö èéê ñe""
208174
```
209175

210-
Upgrading from version 1.x to 2.0
211-
---------------------------------
212-
The API has changed quite a bit in version 2.0, but mostly naming wise and the usage is more or less
213-
the same with some added functionality and ease of use.
214-
215-
Probably the biggest difference is that in 1.x you would use the static class `Bencode` and the methods
216-
`DecodeString(string)`, `DecodeNumber(string)` etc. In 2.0 you have to create an instance of `BencodeParser`
217-
and use the methods on that.
176+
### Torrents
218177

219-
Use `BencodeParser.ParseString(string)` for parsing strings directly or `BencodeParser.Parse(...)`
220-
for parsing `Stream`, `byte[]` or a file by file path (`string`) without opening af stream yourself.
178+
Working with torrent files:
221179

222180
```C#
223-
// 1.x - Parsing strings directly
224-
BString bstring = Bencode.DecodeString("12:Hello World!");
225-
BNumber bnumber = Bencode.DecodeNumber("i42e");
226-
BList blist = Bencode.DecodeList("l3:foo3:bari42ee");
227-
BDictionary bdictionary = Bencode.DecodeDictionary("d3:fooi42e5:Hello6:World!e");
181+
using BencodeNET.Objects;
182+
using BencodeNET.Parsing;
183+
using BencodeNET.Torrents;
228184

229-
// 2.0 - Parsing strings directly
230-
var parser = new BencodeParser();
231-
BString bstring = parser.ParseString<BString>("12:Hello World!");
232-
BNumber bnumber = parser.ParseString<BNumber>("i42e");
233-
BList blist = parser.ParseString<BList>("l3:foo3:bari42ee");
234-
BDictionary bdictionary = parser.ParseString<BDictionary>("d3:fooi42e5:Hello6:World!e");
185+
// Parse torrent by specifying the file path
186+
var parser = new BencodeParser(); // Default encoding is Encoding.UTF8, but you can specify another if you need to
187+
Torrent torrent = parser.Parse<Torrent>("C:\\ubuntu.torrent");
188+
189+
// Or parse a stream
190+
Torrent torrent = parser.Parse<Torrent>(stream);
191+
192+
// Calculate the info hash
193+
string infoHash = torrent.GetInfoHash();
194+
// "B415C913643E5FF49FE37D304BBB5E6E11AD5101"
195+
196+
// or as bytes instead of a string
197+
byte[] infoHashBytes = torrent.GetInfoHashBytes();
198+
199+
// Get Magnet link
200+
string magnetLink = torrent.GetMagnetLink();
201+
// magnet:?xt=urn:btih:1CA512A4822EDC7C1B1CE354D7B8D2F84EE11C32&dn=ubuntu-14.10-desktop-amd64.iso&tr=http://torrent.ubuntu.com:6969/announce&tr=http://ipv6.torrent.ubuntu.com:6969/announce
235202
236-
// If you don't know the type you are parsing, you can use the non-generic method
237-
IBObject bobject = parser.ParseString("12:Hellow World!");
203+
// Convert Torrent to its BDictionary representation
204+
BDictionary bdictinoary = torrent.ToBDictionary();
238205
```
239206

207+
#### File modes
208+
The property `FileMode` indicates if the torrent is single-file or multi-file.
209+
210+
For single-file torrents the `File` property contains the relevant file info.
211+
The `Files` property is null.
212+
213+
For multi-file torrents the `Files` property contains a list of file info and the directory name.
214+
The `File` property is null.
215+
216+
#### Non-standard fields
217+
The `ExtraFields` property is for any non-standard fields which are not accessible through any other property.
218+
Data set on this property will overwrite any data from the `Torrent` itself when encoding it. This way you are able to add to or owerwrite fields.
219+
220+
221+
Roadmap
222+
-------
223+
The project is in maintenance mode and no new features are currently planned.
224+
Feel free to request new features by creating a new issue.
225+
226+
227+
Building the project
228+
---------------------------------
229+
Requirements:
230+
- .NET Core 3.0 SDK
231+
- Visual Studio 2019 (or other IDE support .NET Core 3.0)
232+
233+
Simply checkout the project, restore nuget packages and build the project.
234+
235+
236+
Contributing
237+
------------
238+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
239+
240+
Please make sure to update or add tests as appropriate.
241+
242+
243+
Support
244+
-------
245+
If you use this or one of my other libraries and want to say thank you or support the development feel free to buy me a Red Bull through [buymeacoffee.com](https://www.buymeacoffee.com/UCkS2tw) or through [ko-fi.com](https://ko-fi.com/krusen).
246+
247+
<a href="https://www.buymeacoffee.com/UCkS2tw" target="_blank"><img src="https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
248+
240249

241250
## License
242-
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET.svg?type=large)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET?ref=badge_large)
251+
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET.svg?type=large)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET?ref=badge_large)

0 commit comments

Comments
 (0)