Skip to content

Repository files navigation

Valve Key Value for .NET

Build StatusNuGetCode Coverage

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).

KeyValues1

Used by Steam and the Source engine.

Deserializing text

Basic deserialization

varstream=File.OpenRead("file.vdf");// or any other Streamvarkv=KVSerializer.Create(KVSerializationFormat.KeyValues1Text);KVObjectdata=kv.Deserialize(stream);Console.WriteLine(data["some key"]);

Typed deserialization

publicclassSimpleObject{publicstringName{get;set;}publicstringValue{get;set;}}varstream=File.OpenRead("file.vdf");// or any other Streamvarkv=KVSerializer.Create(KVSerializationFormat.KeyValues1Text);KVObjectdata=kv.Deserialize<SimpleObject>(stream);

Options

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 an InvalidDataException.
  • FileLoader - Provider for referenced files with #include or #base directives.
varoptions=newKVSerializerOptions{HasEscapeSequences=true,};options.Conditions.Clear();// Remove default conditionals set by the libraryoptions.Conditions.Add("X360WIDE");varstream=File.OpenRead("file.vdf");varkv=KVSerializer.Create(KVSerializationFormat.KeyValues1Text);vardata=kv.Deserialize(stream,options);

Deserializing binary

Essentially the same as text, just change KeyValues1Text to KeyValues1Binary.

Serializing to text

classDataObject{publicstringName{get;set;}publicstringDeveloper{get;set;}[KVProperty("description")]publicstringSummary{get;set;}[KVIgnore]publicstringExtraData{get;set;}}vardata=newDataObject{Developer="Valve Software",Name="Dota 2",Summary="Dota 2 is a complex game.",ExtraData="This will not be serialized."};usingvarstream=File.OpenWrite("file.vdf");varkv=KVSerializer.Create(KVSerializationFormat.KeyValues1Text);kv.Serialize(stream,data,"root object name");

Serializing to binary

Essentially the same as text, just change KeyValues1Text to KeyValues1Binary.

KeyValues2 (Datamodel)

This library does not currently support KeyValues2 (Datamodel). If you need KV2/Datamodel support, use our fork of Datamodel.NET instead.

KeyValues3

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.

About

Unofficial Unity package of ValveKeyValue - 📃 Next-generation Valve's key value framework for .NET

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages