API:PluginConfiguration

From Kerbal Space Program Wiki
(Redirected from PluginConfiguration)
Jump to: navigation, search

KSP.IO Namespace
BinaryReader · BinaryWriter · File · FileInfo · FileStream · IOException · IOTools · IOUtils · MemoryStream · PluginConfigNode · PluginConfiguration · TextReader · TextWriter


PluginConfiguration Class

From User:N3X15:

PluginConfiguration was something I threw together a while ago to try and improve settings serialization. The INI files we were using just couldn't cope with newlines and had all sorts of horrible workarounds. Instead of INI files, it writes structured XML files that look like this:

<config>
    <int name="int">4</int>
    <long name="long">45</long>
    <short name="short">4</short>
    <byte name="byte">255</byte>
    <bool name="bool">1</bool>
    <vector3 name="vector3">
        <x>0</x>
        <y>1</y>
        <z>2</z>
    </vector3>
    <vector3d name="vector3d">
        <x>0</x>
        <y>1</y>
        <z>2.05</z>
    </vector3d>
    <string name="string">string</string>
</config>

Despite looking a bit messy, it's actually a lot easier to use and doesn't have as many drawbacks as INI files. Newlines are preserved, and most importantly, types are also preserved. Oh, and it's UTF-8 encoded, so internationalization won't be as much as a problem, theoretically.

Here's how to use it:

PluginConfiguration cfg = PluginConfiguration.CreateForType<MyCoolModule>();
cfg["a string"] = "I love KSP!";
cfg["another setting"] = new Vector3d(0,1,2);
cfg.save();
// Later...
cfg.load();
settingAString = cfg.GetValue<string>("a string");
settingAVector = cfg.GetValue<Vector3d>("another setting");

Properties

The following are public properties available in PluginConfiguration.

Signature Description
Object this[string key] { get; set; } Return configuration key from the root node.

Methods

The following are methods included in PluginConfiguration.

Signature Description
static PluginConfiguration CreateForType<T>(Vessel flight = null) Initialize the configuration object
void save() Commit changes to disk
T GetValue<T>(string key) Get a typed value from the root node.
T GetValue<T>(string key, T _default) Get a typed value from the root node, and set to a default if it doesn't exist.
void SetValue(string key, object value) Set a configuration key's value
void load() Load from disk