From 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: More...
Public Member Functions | |
T | GetValue< T > (string key) |
Get a typed value from the root node. More... | |
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. More... | |
void | load () |
Load from disk More... | |
void | save () |
Commit changes to disk More... | |
void | SetValue (string key, object value) |
Set a configuration key's value More... | |
Static Public Member Functions | |
static PluginConfiguration | CreateForType< T > (Vessel flight=null) |
Initialize the configuration object More... | |
Protected Member Functions | |
PluginConfiguration (string pathToFile) | |
Properties | |
object | this[string key] [get, set] |
Return configuration key from the root node More... | |
From 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");
|
protected |
|
static |
Initialize the configuration object
T |
flight |
T KSP.IO.PluginConfiguration.GetValue< T > | ( | string | key | ) |
Get a typed value from the root node.
T |
key |
T KSP.IO.PluginConfiguration.GetValue< T > | ( | string | key, |
T | _default | ||
) |
Get a typed value from the root node, and set to a default if it doesn't exist.
T |
key | |
_default |
void KSP.IO.PluginConfiguration.load | ( | ) |
Load from disk
void KSP.IO.PluginConfiguration.save | ( | ) |
Commit changes to disk
void KSP.IO.PluginConfiguration.SetValue | ( | string | key, |
object | value | ||
) |
Set a configuration key's value
key | |
value |
|
getset |
Return configuration key from the root node
key |