Skip to content

Commit 952bd1f

Browse files
committed
Add readme
1 parent 41492b7 commit 952bd1f

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,100 @@
33
[![Build Status (GitHub)](https://img.shields.io/github/workflow/status/SteamDatabase/ValveKeyValue/.NET%20Core%20CI?label=Build&style=flat-square)](https://github.com/SteamDatabase/ValveKeyValue/actions)
44
[![Coverage Status](https://img.shields.io/coveralls/SteamDatabase/ValveKeyValue.svg?label=Test+Coverage&style=flat-square)](https://coveralls.io/github/SteamDatabase/ValveKeyValue)
55
[![NuGet](https://img.shields.io/nuget/v/ValveKeyValue.svg?label=NuGet&style=flat-square)](https://www.nuget.org/packages/ValveKeyValue/)
6+
7+
This library aims to be fully compatible with Valve's various implementations of
8+
KeyValues format parsing (believe us, it's not consistent).
9+
10+
# KeyValues1
11+
12+
Used by Steam and the Source engine.
13+
14+
## Deserializing text
15+
16+
### Basic deserialization
17+
```cs
18+
var stream = File.OpenRead("file.vdf"); // or any other Stream
19+
20+
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
21+
KVObject data = kv.Deserialize(stream);
22+
23+
Console.WriteLine(data["some key"]);
24+
```
25+
26+
### Typed deserialization
27+
```cs
28+
public class SimpleObject
29+
{
30+
public string Name { get; set; }
31+
public string Value { get; set; }
32+
}
33+
34+
var stream = File.OpenRead("file.vdf"); // or any other Stream
35+
36+
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
37+
KVObject data = kv.Deserialize<SimpleObject>(stream);
38+
```
39+
40+
### Options
41+
`Deserialize` method also accepts an `KVSerializerOptions` object.
42+
43+
By default, operating system specific conditionals are enabled based on the OS the code is running on (`RuntimeInformation`).
44+
45+
`KVSerializerOptions` accepts has the following options:
46+
47+
* `Conditions` - List of conditions to use to match conditional values.
48+
* `HasEscapeSequences` - Whether the parser should translate escape sequences (e.g. `\n`, `\t`).
49+
* `EnableValveNullByteBugBehavior` - Whether invalid escape sequences should truncate strings rather than throwing a `InvalidDataException`.
50+
* `FileLoader` - Provider for referenced files with `#include` or `#base` directives.
51+
52+
```cs
53+
var options = new KVSerializerOptions
54+
{
55+
HasEscapeSequences = true,
56+
};
57+
options.Conditions.Clear(); // Remove default conditionals set by the library
58+
options.Conditions.Add("X360WIDE");
59+
60+
var stream = File.OpenRead("file.vdf");
61+
62+
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
63+
var data = kv.Deserialize(stream, options);
64+
```
65+
66+
## Deserializing binary
67+
68+
Essentially the same text, just change `KeyValues1Text` to `KeyValues1Binary`.
69+
70+
## Serializing to text
71+
72+
```cs
73+
class DataObject
74+
{
75+
public string Name { get; set; }
76+
77+
public string Developer { get; set; }
78+
79+
[KVProperty("description")]
80+
public string Summary { get; set; }
81+
82+
[KVIgnore]
83+
public string ExtraData { get; set; }
84+
}
85+
86+
var data = new DataObject
87+
{
88+
Developer = "Valve Software",
89+
Name = "Dota 2",
90+
Summary = "Dota 2 is a complex game.",
91+
ExtraData = "This will not be serialized."
92+
};
93+
94+
using var stream = File.OpenWrite("file.vdf");
95+
96+
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
97+
kv.Serialize(stream, data, "root object name");
98+
```
99+
100+
## Serializing to binary
101+
102+
Essentially the same text, just change `KeyValues1Text` to `KeyValues1Binary`.

ValveKeyValue/ValveKeyValue/KVSerializerOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public sealed class KVSerializerOptions
1414
public IList<string> Conditions { get; } = new List<string>(GetDefaultConditions());
1515

1616
/// <summary>
17-
/// Gets or sets a value indicating whether gets or sets if the parser should translate escape sequences (e.g. <c>\n</c>, <c>\t</c>).
17+
/// Gets or sets a value indicating whether the parser should translate escape sequences (e.g. <c>\n</c>, <c>\t</c>).
1818
/// </summary>
1919
public bool HasEscapeSequences { get; set; }
2020

0 commit comments

Comments
 (0)