KeyValues is a simple key-value pair format used by Valve in Steam and the Source engine for configuration files, game data, and more (.vdf, .res, .acf, etc.). This library aims to be fully compatible with Valve's various implementations of KeyValues format parsing (believe us, it's not consistent).
Used by Steam and the Source engine.
var stream = File.OpenRead("file.vdf"); // or any other Stream
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
KVObject data = kv.Deserialize(stream);
Console.WriteLine(data["some key"]);public class SimpleObject
{
public string Name { get; set; }
public string Value { get; set; }
}
var stream = File.OpenRead("file.vdf"); // or any other Stream
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
KVObject data = kv.Deserialize<SimpleObject>(stream);The Deserialize method also accepts a KVSerializerOptions object.
By default, operating system specific conditionals are enabled based on the OS the code is running on (RuntimeInformation).
KVSerializerOptions has the following options:
Conditions- List of conditions to use to match conditional values.HasEscapeSequences- Whether the parser should translate escape sequences (e.g.\n,\t).EnableValveNullByteBugBehavior- Whether invalid escape sequences should truncate strings rather than throwing anInvalidDataException.FileLoader- Provider for referenced files with#includeor#basedirectives.
var options = new KVSerializerOptions
{
HasEscapeSequences = true,
};
options.Conditions.Clear(); // Remove default conditionals set by the library
options.Conditions.Add("X360WIDE");
var stream = File.OpenRead("file.vdf");
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
var data = kv.Deserialize(stream, options);Essentially the same as text, just change KeyValues1Text to KeyValues1Binary.
class DataObject
{
public string Name { get; set; }
public string Developer { get; set; }
[KVProperty("description")]
public string Summary { get; set; }
[KVIgnore]
public string ExtraData { get; set; }
}
var data = new DataObject
{
Developer = "Valve Software",
Name = "Dota 2",
Summary = "Dota 2 is a complex game.",
ExtraData = "This will not be serialized."
};
using var stream = File.OpenWrite("file.vdf");
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
kv.Serialize(stream, data, "root object name");Essentially the same as text, just change KeyValues1Text to KeyValues1Binary.
This library does not currently support KeyValues2 (Datamodel). If you need KV2/Datamodel support, use our fork of Datamodel.NET instead.
This library does not currently support KeyValues3. There is an open pull request for KV3 support.
If you need KV3 support, use ValveResourceFormat which supports parsing Source 2 formats including KV3.
