Skip to content
Michael Ermishin edited this page Aug 6, 2017 · 1 revision

Welcome to the ConfigManager wiki!

Here you will find information about the project, it's structure and usage.

Getting started

Writing a simple configuration

The following example shows list with a link to the next element.

Pay attention that the last 'next' key was not used. It will prevent new list node from creation.

File example.cfg

list
  value 10
  next
    value 20
    next
      value 13

Reading configuration

Now we will read configuration from file into special data structure. The following code template is reading configuration from file "example.cfg".

using ConfigManager;

class Program {
    public static void Main(string[] args) {
        ConfigValue config = Config.LoadFromFile("example.cfg");
    }
}

Getting values

After reading configuration into specialized dictionary we are able to get any values from it. There are several methods for accessing values:

Get(string)

This function searches for given string key on current level. We can chain this method in order to get deeper values. Returns null if key does not exists in config.

GetByPath(string)

This functions acts almost like Get, but might search also in enclosed values. Path might includes

  • identifier - value identifer([a-z]+) that will be searched on current level.
  • index - value index([0-9]+). Gets N-th value if multiple ones have the same key(name).
  • data index - value's data index(\$[0-9]+) starts with '$' sign. Get's N-th data from current value.

Returns null if value can not be found by given path

Using and converting data

After finding key that we need we are able to get a value coresponding to it.

The following example illustrates how it works on config we've made at the beginning of the tutorial.

// Getting top level list from loaded config
ConfigValue list = config.Get("list");

// Getting deeper value and catching It's data with needed type
int firstValue = list.Get("value").AsInt();

For all conversion methods visit a documentation page: Data conversion

Loading configuration into class

Configuration might be loaded into class in several different ways. If it was already parsed - you might call Config.LoadToClass<T>(configRootValue) with specified type. T - points to class wich should be filled with values. Class should have public constructor with no parameters in order to be configured with ConfigManager.

Minimal class that represents List we will parse from configuration.

Pay attention that default constructor is generated automatically if you didn't declared them manually.

public class List {
    public int Value;
    public List Next;
}

The following code will read config in newly created class:

List list = Config.LoadToClassFromFile<List>("example.cfg");