Difference between revisions of "API:PluginConfiguration"

From Kerbal Space Program Wiki
Jump to: navigation, search
 
Line 45: Line 45:
 
</blockquote>
 
</blockquote>
 
{{PropertiesStart}}
 
{{PropertiesStart}}
{{ThisProperty
+
{{Property
|type=object
+
|type=Object
|key=string
+
|name=this[{{arg|string|key}}]
|getset=get; set;
+
|desc=Return configuration key from the root node.
|desc=Get or set a named value within the class.
+
|hasget=1
 +
|hasset=1
 
}}
 
}}
 
{{PropertiesEnd}}
 
{{PropertiesEnd}}
Line 57: Line 58:
 
|returndesc=
 
|returndesc=
 
|name=CreateForType<T>
 
|name=CreateForType<T>
|args=Vessel flight = null
+
|args={{arg|Vessel|flight|default=null}}
|desc=Create a configuration instance for the assembly hosting type T.
+
|desc=Initialize the configuration object
 +
}}
 +
{{Method
 +
|returntype=void
 +
|returndesc=
 +
|name=save
 +
|args=
 +
|desc=Commit changes to disk
 
}}
 
}}
 
{{Method
 
{{Method
Line 64: Line 72:
 
|returndesc=
 
|returndesc=
 
|name=GetValue<T>
 
|name=GetValue<T>
|args=string key
+
|args={{arg|string|key}}
|desc=Get the requested item from the node, or null if it isn't present
+
|desc=Get a typed value from the root node.
 
}}
 
}}
 
{{Method
 
{{Method
Line 71: Line 79:
 
|returndesc=
 
|returndesc=
 
|name=GetValue<T>
 
|name=GetValue<T>
|args=string key, T _default
+
|args={{arg|string|key}}, {{arg|T|_default}}
|desc=Get the requested item from the root configuration node, or the default if it isn't present
+
|desc=Get a typed value from the root node, and set to a default if it doesn't exist.
 
}}
 
}}
 
{{Method
 
{{Method
 
|returntype=void
 
|returntype=void
 
|returndesc=
 
|returndesc=
|name=load
+
|name=SetValue
|args=
+
|args={{arg|string|key}}, {{arg|object|value}}
|desc=Load from the configuration file.
+
|desc=Set a configuration key's value
 
}}
 
}}
 
{{Method
 
{{Method
 
|returntype=void
 
|returntype=void
 
|returndesc=
 
|returndesc=
|name=save
+
|name=load
 
|args=
 
|args=
|desc=Save to the configuration file.
+
|desc=Load from disk
}}
 
{{Method
 
|returntype=void
 
|returndesc=
 
|name=SetValue
 
|args=string key, object value
 
|desc=Set a value in the root configuration node.
 
 
}}
 
}}
 
{{MethodsEnd}}
 
{{MethodsEnd}}

Latest revision as of 22:41, 12 October 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; 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