Show / Hide Table of Contents

Class MyIni

A configuration class to parse and create a text string resembling the old fashioned INI format, but with support for multiline values.

Do not forget that parsing is a time-consuming task. Keep your parsing to a minimum.

Inheritance
System.Object
MyIni
Namespace: VRage.Game.ModAPI.Ingame.Utilities
Assembly: VRage.Game.dll
Syntax
public class MyIni : Object
Remarks

This class is NOT THREAD SAFE as it's optimized for programmable block use.

Examples

Using MyIni to deal with CustomData end-user configuration:

The CustomData:

[kernel]
output=DebugTextPanel
bootText=
|-- HAL9000 --
|Good morning, Dave.

The code:

MyIni _ini = new MyIni();
IMyTextPanel _outputTextPanel;

public Program() { MyIniParseResult result; if (!_ini.TryParse(Me.CustomData, out result) { Echo($"CustomData error:\nLine {result}"); }

// Get the kernel section's output value. If this value is set, the system attempts // to retrieve a text panel with the value set. Otherwise output is ignored. var name = _ini.Get("kernel", "output").ToString(); if (name != null) { _outputTextPanel = GridTerminalSystem.GetBlockWithName<IMyTextPanel>(name); if (_outputTextPanel == null) Echo($"No text panel named {name}"); }

// Get the kernel section's boottext value. If no value is given, a default value will be returned. var bootText = _ini.Get("kernel", "bootText").ToString("Kernel is starting up..."); _outputTextPanel?.WritePublicText(bootText); }

public void Main() { // Do your stuff. Only parse the configuration when you have to. }

Using MyIni to deal with internal storage:

MyIni _storage = new MyIni();
Vector3D _startupPosition;
bool _hasTarget;
Vector3D _currentTarget;

public Program() { // You only need to parse here in the constructor. if (_ini.TryParse(Storage) { var str = _ini.Get("state", "startupPosition").ToString(); Vector3D.TryParse(str, out _startupPosition); str = _ini.Get("state", "currentTarget").ToString(); Vector3D.TryParse(str, out _currentTarget); _hasTarget = _ini.Get("state", "hasTarget").ToBoolean(); } else { // Set up defaults, the storage is nonexistent or corrupt _startupPosition = Me.CubeGrid.Position; } }

public void Save() { // You only need to update Storage when the Save method is called. _ini.Set("state", "startupPosition", _startupPosition); _ini.Set("state", "currentTarget", _currentTarget); Storage = _ini.ToString(); }

public void Main() { // Do your stuff }

Constructors

MyIni()

Declaration
public MyIni()

Properties

EndComment

Get or set a comment to be placed after the last section or item. Is null if the section does not exist or has no comment.

Declaration
public string EndComment { get; set; }
Property Value
Type Description
System.String

EndContent

You can terminate a configuration stream by entering "---" on a separate line. This property will contain all the content after this line.

Declaration
public string EndContent { get; set; }
Property Value
Type Description
System.String

Methods

AddSection(String)

Adds an empty section

Declaration
public void AddSection(string section)
Parameters
Type Name Description
System.String section

Clear()

Empties this configuration

Declaration
public void Clear()

ContainsKey(String, String)

Determines whether a configuration key (section/key) exists in the currently parsed configuration.

Declaration
public bool ContainsKey(string section, string name)
Parameters
Type Name Description
System.String section
System.String name
Returns
Type Description
System.Boolean

ContainsKey(MyIniKey)

Determines whether a configuration key (section/key) exists in the currently parsed configuration.

Declaration
public bool ContainsKey(MyIniKey key)
Parameters
Type Name Description
MyIniKey key
Returns
Type Description
System.Boolean

ContainsSection(String)

Determines whether a section of a given name exists in the currently parsed configuration.

Declaration
public bool ContainsSection(string section)
Parameters
Type Name Description
System.String section
Returns
Type Description
System.Boolean

Delete(String, String)

Deletes the given configuration key.

Declaration
public void Delete(string section, string name)
Parameters
Type Name Description
System.String section
System.String name

Delete(MyIniKey)

Deletes the given configuration key.

Declaration
public void Delete(MyIniKey key)
Parameters
Type Name Description
MyIniKey key

DeleteSection(String)

Deletes an entire selection

Declaration
public bool DeleteSection(string section)
Parameters
Type Name Description
System.String section
Returns
Type Description
System.Boolean

true if a section was deleted; false otherwise.

Get(String, String)

Gets the MyIniValue of the given configuration key.

Declaration
public MyIniValue Get(string section, string name)
Parameters
Type Name Description
System.String section
System.String name
Returns
Type Description
MyIniValue

Get(MyIniKey)

Gets the MyIniValue of the given configuration key.

Declaration
public MyIniValue Get(MyIniKey key)
Parameters
Type Name Description
MyIniKey key
Returns
Type Description
MyIniValue

GetComment(String, String)

Gets any comment that might be associated with the given key. Returns null if the key does not exist or has no comment.

Declaration
public string GetComment(string section, string name)
Parameters
Type Name Description
System.String section
System.String name
Returns
Type Description
System.String

GetComment(MyIniKey)

Gets any comment that might be associated with the given key. Returns null if the key does not exist or has no comment.

Declaration
public string GetComment(MyIniKey key)
Parameters
Type Name Description
MyIniKey key
Returns
Type Description
System.String

GetKeys(List<MyIniKey>)

Fills the provided list with all configuration keys within the currently parsed configuration.

Declaration
public void GetKeys(List<MyIniKey> keys)
Parameters
Type Name Description
System.Collections.Generic.List<MyIniKey> keys

GetKeys(String, List<MyIniKey>)

Fills the provided list with the configuration keys within the given section.

Declaration
public void GetKeys(string section, List<MyIniKey> keys)
Parameters
Type Name Description
System.String section
System.Collections.Generic.List<MyIniKey> keys

GetSectionComment(String)

Get any comment that might be associated with the given section. Returns null if the section does not exist or has no comment.

Declaration
public string GetSectionComment(string section)
Parameters
Type Name Description
System.String section
Returns
Type Description
System.String

GetSections(List<String>)

Fills the provided list with the names of all the sections in the currently parsed configuration.

Declaration
public void GetSections(List<string> names)
Parameters
Type Name Description
System.Collections.Generic.List<System.String> names

HasSection(String, String)

Determines if the given configuration contains what looks like the given section. It does not verify that the content is actually in a valid format, just if there's a line starting with [section].

Declaration
public static bool HasSection(string config, string section)
Parameters
Type Name Description
System.String config
System.String section
Returns
Type Description
System.Boolean

Invalidate()

Forces regeneration of the ini content. Only really useful if you want to reformat the configuration file.

Declaration
public void Invalidate()

Set(String, String, Boolean)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, bool value)
Parameters
Type Name Description
System.String section
System.String name
System.Boolean value

Set(String, String, Byte)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, byte value)
Parameters
Type Name Description
System.String section
System.String name
System.Byte value

Set(String, String, Decimal)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, Decimal value)
Parameters
Type Name Description
System.String section
System.String name
System.Decimal value

Set(String, String, Double)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, double value)
Parameters
Type Name Description
System.String section
System.String name
System.Double value

Set(String, String, Int16)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, short value)
Parameters
Type Name Description
System.String section
System.String name
System.Int16 value

Set(String, String, Int32)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, int value)
Parameters
Type Name Description
System.String section
System.String name
System.Int32 value

Set(String, String, Int64)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, long value)
Parameters
Type Name Description
System.String section
System.String name
System.Int64 value

Set(String, String, SByte)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, sbyte value)
Parameters
Type Name Description
System.String section
System.String name
System.SByte value

Set(String, String, Single)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, float value)
Parameters
Type Name Description
System.String section
System.String name
System.Single value

Set(String, String, String)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, string value)
Parameters
Type Name Description
System.String section
System.String name
System.String value

Set(String, String, UInt16)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, ushort value)
Parameters
Type Name Description
System.String section
System.String name
System.UInt16 value

Set(String, String, UInt32)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, uint value)
Parameters
Type Name Description
System.String section
System.String name
System.UInt32 value

Set(String, String, UInt64)

Sets the value of the given configuration key.

Declaration
public void Set(string section, string name, ulong value)
Parameters
Type Name Description
System.String section
System.String name
System.UInt64 value

Set(MyIniKey, Boolean)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, bool value)
Parameters
Type Name Description
MyIniKey key
System.Boolean value

Set(MyIniKey, Byte)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, byte value)
Parameters
Type Name Description
MyIniKey key
System.Byte value

Set(MyIniKey, Decimal)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, Decimal value)
Parameters
Type Name Description
MyIniKey key
System.Decimal value

Set(MyIniKey, Double)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, double value)
Parameters
Type Name Description
MyIniKey key
System.Double value

Set(MyIniKey, Int16)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, short value)
Parameters
Type Name Description
MyIniKey key
System.Int16 value

Set(MyIniKey, Int32)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, int value)
Parameters
Type Name Description
MyIniKey key
System.Int32 value

Set(MyIniKey, Int64)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, long value)
Parameters
Type Name Description
MyIniKey key
System.Int64 value

Set(MyIniKey, SByte)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, sbyte value)
Parameters
Type Name Description
MyIniKey key
System.SByte value

Set(MyIniKey, Single)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, float value)
Parameters
Type Name Description
MyIniKey key
System.Single value

Set(MyIniKey, String)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, string value)
Parameters
Type Name Description
MyIniKey key
System.String value

Set(MyIniKey, UInt16)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, ushort value)
Parameters
Type Name Description
MyIniKey key
System.UInt16 value

Set(MyIniKey, UInt32)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, uint value)
Parameters
Type Name Description
MyIniKey key
System.UInt32 value

Set(MyIniKey, UInt64)

Sets the value of the given configuration key.

Declaration
public void Set(MyIniKey key, ulong value)
Parameters
Type Name Description
MyIniKey key
System.UInt64 value

SetComment(String, String, String)

Sets a comment on a given item. The item must already exist. Set the comment to null to remove it.

Declaration
public void SetComment(string section, string name, string comment)
Parameters
Type Name Description
System.String section
System.String name
System.String comment

SetComment(MyIniKey, String)

Sets a comment on a given item. The item must already exist. Set the comment to null to remove it.

Declaration
public void SetComment(MyIniKey key, string comment)
Parameters
Type Name Description
MyIniKey key
System.String comment

SetEndComment(String)

Sets a comment to be placed after the last section or item. Set the comment to null to remove it.

Declaration
public void SetEndComment(string comment)
Parameters
Type Name Description
System.String comment

SetSectionComment(String, String)

Sets a comment on a given section. The section must already exist. Set the comment to null to remove it.

Declaration
public void SetSectionComment(string section, string comment)
Parameters
Type Name Description
System.String section
System.String comment

ToString()

Generates a configuration file from the currently parsed configuration

Declaration
public override string ToString()
Returns
Type Description
System.String

TryParse(String)

Attempts to parse the given content as a configuration file.

Declaration
public bool TryParse(string content)
Parameters
Type Name Description
System.String content
Returns
Type Description
System.Boolean

true if the parse succeeds, false otherwise

TryParse(String, String)

Attempts to parse the given content as a configuration file. OBSERVE: Use only for read-only operations. If you parse a single section and run ToString(), you will only get the parsed section, the rest will be discarded.

Declaration
public bool TryParse(string content, string section)
Parameters
Type Name Description
System.String content
System.String section

The specific section to parse. Any other section in the content will be ignored.

Returns
Type Description
System.Boolean

true if the parse succeeds, false otherwise

TryParse(String, String, out MyIniParseResult)

Attempts to parse the given content as a configuration file. OBSERVE: Use only for read-only operations. If you parse a single section and run ToString(), you will only get the parsed section, the rest will be discarded.

Declaration
public bool TryParse(string content, string section, out MyIniParseResult result)
Parameters
Type Name Description
System.String content
System.String section

The specific section to parse. Any other section in the content will be ignored.

MyIniParseResult result

If unsuccessful, this value contains information about why the parse failed

Returns
Type Description
System.Boolean

true if the parse succeeds, false otherwise

TryParse(String, out MyIniParseResult)

Attempts to parse the given content as a configuration file.

Declaration
public bool TryParse(string content, out MyIniParseResult result)
Parameters
Type Name Description
System.String content
MyIniParseResult result

If unsuccessful, this value contains information about why the parse failed

Returns
Type Description
System.Boolean

true if the parse succeeds, false otherwise

☀
☾
In This Article
Back to top
Generated by DocFX
☀
☾