|
3 | 3 | [](https://github.com/SteamDatabase/ValveKeyValue/actions) |
4 | 4 | [](https://coveralls.io/github/SteamDatabase/ValveKeyValue) |
5 | 5 | [](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`. |
0 commit comments