Universal OMSI File Parser Documentation

Introduction

The public class CfgHelper<T> provides functionality for parsing OMSI configuration files (e.g. *.sli, *.sco, *.cfg) into instances of a specified class T. It has two constructors that take a file path and an optional instance of type T. The TryParse method attempts to parse the configuration file and returns a bool indicating success or failure, and the resulting instance is returned via the out parameter. The Parse method parses the configuration file and returns an instance of type T. If the specified file path is invalid or the instance of T could not be created, the default value of T is returned. Any errors that occur during parsing are stored in the Errors property, which is a List<string>. The parsing is performed using reflection.

Setting up the project

Visual Studio project creation

Open Visual Studio and create a new project by selecting "File" -> "New" -> "Project". In the "New Project" window, select "Visual C#" and then "Console App (.NET Framework)" as the project type. Give your project a name, such as "TestParserApp", and choose a location to save it. Click "Create" to create the project.

Add reference in Visual Studio

Right-click on the solution and select "Add Reference".

Visual Studio reference manager

In the "Reference Manager" window, select "Browse" and navigate to the location of your DLL file. Select the DLL file and click "Add". Click "OK" to close the "Reference Manager" window.

C# using statement

Now, in your code file, you can add a "using" statement to reference the namespace of the DLL: using UniversalOMSIFileParser;

Use DLL classes and methods

You can now use the classes and methods from the DLL in your code.

Reconstructing OMSI CFG file sections in C#

OMSI CFG FILE C#
[keyword] public bool value = false;
[keyword]
value
public string value = string.Empty;
public byte value;
public int value = 0;
public float value = 0f;
[keyword]
count
value1
value2
value3
...
public List<string> value = new();
public List<byte> value = new();
public List<int> value = new();
public List<float> value = new();
[keyword1]
value1
value2
public List<MyClass> value = new();
[keyword2]
value3
...
public List<MyClass> value = new();

CfgHelper<T> Class

The class provides functionality for parsing OMSI configuration files (e.g. *.sli, *.sco, *.cfg) into instances of a specified class T.

Namespace

  • UniversalOMSIFileParser

Type Parameters

  • T - The result type.

Properties

  • List<string> Errors - Gets a list of errors that occurred during parsing.

Constructors

  • CfgHelper(string filePath) - Initializes a new instance.
  • CfgHelper(string filePath, T instance) - Initializes a new instance with an existing instance used to set the parsed values into the properties of the instance instead of creating a new instance of T.

Methods

  • bool TryParse(out T result) - Tries to parse the configuration file.
  • T Parse() - Parses the configuration file using reflection.

Remarks

The CfgHelper<T> class provides functionality for parsing OMSI configuration files (e.g. *.sli, *.sco, *.cfg) into instances of a specified class T.

The parsing is done using reflection.

Attributes

The following attributes can be used to modify the behavior of the parsing process:

  • CfgFileExtension: This attribute is used to define the file extension that the parser should use to identify if the file is valid.
  • CfgOverwriteName: This attribute is used to overwrite the name of a field in the class with a specific keyword.
  • CfgUseAlsoFor: This attribute is used to search for matching fields in a subclass when the current keyword is equal to a specified keyword.
  • CfgEachKeywordList: This attribute is used to indicate that a field should be treated as a list of values, where each value corresponds to a separate keyword in the configuration file.
  • CfgUseEndKeyword: This attribute is used to indicate that the parser should stop parsing the current field when it encounters the 'end' keyword.
  • CfgReadTillEmptyString: This attribute is used to indicate that the parser should keep reading values for the current field until it encounters an empty string.
  • CfgDictionaryKey: This attribute is used to indicate that the current field should be used as the key for a dictionary.

Example

Example: Parsing [onlyeditor], [profile], and [profilepnt] keywords in a .sli file using C#

To parse a splines [onlyeditor], [profile] and [profilepnt] keywords, recreate the spline class in C#. Here is a possible way:

					
// “main class” that is passed to the parser
[CfgFileExtension(".sli")]
public class SplineFile
{
    // onlyeditor can be true or by default false => Boolean
    public bool onlyeditor = false;

    // profile points are linked with the previously defined profile
    // They have more than one value => own profile class
    [CfgOverwriteName("profile")]
    [CfgUseAlsoFor(new string[] { "profilepnt" })]
    public List<Profile> profilePoints = new List<Profile>();
}

// reconstruction of [profilepnt]
public class SplinePoint
{
    public float x;
    public float y;
    public float u;
    public float v;

    public SplinePoint(float x, float y, float u, float v)
    {
        this.x = x;
        this.y = y;
        this.u = u;
        this.v = v;
    }
}

// reconstruction of [profile]
public class Profile
{
    public int textureIndex = 0;
    [CfgOverwriteName("profilepnt")]
    public List<SplinePoint> profilePoints = new List>SplinePoint<();

    public Profile(int textureIndex)
    {
        this.textureIndex = textureIndex;
    }
}
					
				  

The public class CfgFileExtension : Attribute tells the parser to return the default value of T when the passed file path does not match public string Extension of public class CfgFileExtension : Attribute.

The [onlyeditor] is a Boolean. When the parser finds the keyword by name, it will set onlyeditor = true.

The public List<Profile> profilePoints does not match the name of the keyword. Use the public class CfgOverwriteName : Attribute to overwrite the name. The parser instantiates the constructor of public class Profile and adds it to the list.

For the [profilepnt] keyword, the parser searches for a matching field in the public class Profile field. Here, it also calls the constructor and adds it to the list.

To use the parser, you have to create a new instance with the public CfgHelper(string filePath) constructor and invoke its public T? Parse() method:

					
CfgHelper<SplineFile> cfgHelper = new CfgHelper<SplineFile>(@"C:\Users\JohnDoe\Desktop\test.sli");
SplineFile? splineFile = cfgHelper.Parse();
					
				  

Check if the instance is (not) null before usage:

					
if (null == splineFile)
	return;
					
				  

When everything worked you can use the parsed instance.

Console.WriteLine(splineFile.onlyeditor);