Difference between revisions of "API:PluginConfiguration"

From Kerbal Space Program Wiki
Jump to: navigation, search
(Created page with "{{Namespace:KSP.IO}} == PluginConfiguration Class == From User:N3X15: <blockquote> PluginConfiguration was something I threw together a while ago to try and improve set...")
(No difference)

Revision as of 03:15, 18 May 2012

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 or set a named value within the class.

Methods

The following are methods included in PluginConfiguration.

Signature Description
static PluginConfiguration CreateForType<T>(Vessel flight = null) Create a configuration instance for the assembly hosting type T.
T GetValue<T>(string key) Get the requested item from the node, or null if it isn't present
T GetValue<T>(string key, T _default) Get the requested item from the root configuration node, or the default if it isn't present
void load() Load from the configuration file.
void save() Save to the configuration file.
void SetValue(string key, object value) Set a value in the root configuration node.