GithubHelp home page GithubHelp logo

pinkdiamond1 / valvekeyvalue Goto Github PK

View Code? Open in Web Editor NEW

This project forked from valveresourceformat/valvekeyvalue

0.0 1.0 0.0 387 KB

๐Ÿ“ƒ Next-generation Valve's key value framework for .NET

License: MIT License

C# 100.00%

valvekeyvalue's Introduction

Valve's KeyValue for .NET

Build Status (GitHub) Coverage Status NuGet

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

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"]);

Typed deserialization

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

Options

Deserialize method also accepts an KVSerializerOptions object.

By default, operating system specific conditionals are enabled based on the OS the code is running on (RuntimeInformation).

KVSerializerOptions accepts 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 a InvalidDataException.
  • FileLoader - Provider for referenced files with #include or #base directives.
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);

Deserializing binary

Essentially the same text, just change KeyValues1Text to KeyValues1Binary.

Serializing to text

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");

Serializing to binary

Essentially the same text, just change KeyValues1Text to KeyValues1Binary.

valvekeyvalue's People

Contributors

justarchi avatar xpaw avatar yaakov-h avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.