keyvalues

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2018 License: Unlicense Imports: 5 Imported by: 9

README

keyvalues

Go library for parsing Valve keyvalue format files. This library constructs a simple kv node tree that you can query any structure(s) and any property(s) of.

It has been tested against various gameinfo.txt engine files, but should work with other KeyValue files as well (such as .vmf or .vmt).

Usage
package main

import (
    "log"
    "os"
    "github.com/galaco/keyvalues"
)

func main() {
	file,_ := os.Open("gameinfo.txt")

	reader := keyvalues.NewReader(file)
	kv,_ := reader.Read()

    // counterstrike: source's gameinfo.txt would return "Counter-Strike Source"
    gameInfoNode,_ := kv.Find("GameInfo")
    gameNode,_ := gameInfoNode.Find("game")
    log.Println(gameNode.AsString())

    // counterstrike: source's gameinfo.txt would return 1
    noModelsNode,_ := gameInfoNode.Find("nomodels")
    log.Println(noModelsNode.AsInt())

    // counterstrike: source's gameinfo.txt would return 240
    fileSystemNode,_ := gameInfoNode.Find("FileSystem")
    appIdNode,_ := fileSystemNode.Find("SteamAppId")
    log.Println(appIdNode.AsInt())
}
Todo
  • Implement multi-line values. At present, a \n character in a quoted value will break the parser. This is how CS:GO Hammer behaves. However, other versions of Hammer support this, as well as all engine versions. Worth noting what spec is available doesn't cover this behaviour.
  • Implement pointer value type (unsure if there is any point to this besides matching spec)
  • Proper test coverage

Documentation

Index

Constants

View Source
const CHAR_COMMENT = "//"
View Source
const CHAR_DISCARD_CUTSET = "\t \r\n"
View Source
const CHAR_ENTER_SCOPE = "{"
View Source
const CHAR_ESCAPE = "\""
View Source
const CHAR_EXIT_SCOPE = "}"
View Source
const CHAR_SEPARATOR = " "
View Source
const CHAR_TAB = "\t"
View Source
const NODE_KEY_ROOT = "$root"
View Source
const ValueArray = ValueType("array")
View Source
const ValueFloat = ValueType("float")
View Source
const ValueInt = ValueType("integer")
View Source
const ValuePtr = ValueType("ptr")
View Source
const ValueString = ValueType("string")

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyValue

type KeyValue struct {
	// contains filtered or unexported fields
}

A KeyValue object, that may hold multiple Values

func (*KeyValue) AddChild

func (node *KeyValue) AddChild(value *KeyValue) error

Add adds a new KeyValue pair to an existing Key Existing key's value must be an Array type

func (*KeyValue) AsFloat

func (node *KeyValue) AsFloat() (float32, error)

AsFloat returns value as an int32, assuming it is of float type

func (*KeyValue) AsInt

func (node *KeyValue) AsInt() (int32, error)

AsInt returns value as an int32, assuming it is of integer type

func (*KeyValue) AsString

func (node *KeyValue) AsString() (string, error)

AsString returns value as a string, assuming it is of string type

func (*KeyValue) Children

func (node *KeyValue) Children() (children []*KeyValue, err error)

GetChildren gets all node child values This is used for keys that contain 1 or more children as its value rather than a basic type

func (*KeyValue) Find

func (node *KeyValue) Find(key string) (*KeyValue, error)

Find returns a keyvalue pair where the key matches input It will return the first found KeyValue in cases where the key is defined multiple times

func (*KeyValue) FindAll

func (node *KeyValue) FindAll(key string) (children []*KeyValue, err error)

FindAll returns all children of a given type for a node. This is different from properties, as a property is a string:<primitive> This will return an array of all KeyValues that match a given key, even though there should be only one.

func (*KeyValue) HasChildren

func (node *KeyValue) HasChildren() bool

HasChildren returns if this Key has KeyValues as its own value.

func (*KeyValue) Key

func (node *KeyValue) Key() string

Key returns KeyValues's key

func (*KeyValue) Type

func (node *KeyValue) Type() ValueType

Type returns type of this key's value

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

func NewReader

func NewReader(file io.Reader) Reader

Return a new Vmf Reader

func (*Reader) Read

func (reader *Reader) Read() (keyvalue KeyValue, err error)

Read buffer file into our defined structures Returns a fully mapped Vmf structure

type ValueType

type ValueType string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL