Skip to main content

Plugin settings

Plugin settings are defined as an array in the mobro_plugin_config.json file ( see In depth: Plugin configuration).

By defining the settings in the config file, MoBro will automatically recognize them and handle everything from exposing them to the user, validation and persistence.

tip

It's always a good idea to provide sensible default values for plugin settings (where possible).
This allows a plugin to be started right after installation without forcing the user to configure it first.

Settings Fields

Multiple different kinds of settings fields are supported.
All of them share these common configuration fields:

FieldTypeDefaultRestrictionsDescription
typestring-Required
checkbox OR number OR select OR string
Type of the settings field.
namestring-Required
Length: 1 - 128
Pattern: ^[\w-]+$
The name (key) of the field
labelstring-Required
Length: 1 - 128
The visible label of the field
descriptionstring?-An optional description of the field
requiredstringfalseWhether the field is required

Checkbox

A checkbox settings field

Additional Fields

FieldTypeDescription
defaultValuebool?The optional default value

Number

A numeric settings field

Additional Fields

FieldTypeDescription
defaultValuedouble?The optional default value
mindouble?The optional minimum value to validate the field
maxdouble?The optional maximum value to validate the field

String

A textual settings field

Additional Fields

FieldTypeDescription
defaultValuestring?The optional default value
regexstring?A optional regex to validate the field

Select

A select settings field
Contains a list of pre-defined selectable options

Additional Fields

FieldTypeDescription
defaultValuestring?The optional default value
optionsarrayThe array of selectable options

Options

FieldTypeRestrictionsDescription
labelstringRequired
Length: 1 - 128
The visible label of the option
valuestringRequired
Length: 1 - 128
Pattern: ^[\w-]+$
The value of the option

Accessing settings

The current values of the defined settings are accessed by using the IMoBroSettings service which can simply be injected via the Plugins constructor (see In depth: Using services).

Plugin.cs
public class Plugin : IMoBroPlugin
{
private readonly IMoBroService _mobro;
private readonly IMoBroSettings _settings;

public Plugin(IMoBroService mobro, IMoBroSettings settings)
{
_mobro = mobro;
_settings = settings;
}

public void Init()
{
// get the value of the setting for the update frequency
var updateFrequencySetting = _settings.GetValue<int>("update_frequency");
}
}

Example configuration

The following configuration file is taken from the MoBroHardwareMonitor plugin.

This configuration file defines multiple different settings:

  • Update Frequency
    A select field to let the user choose the metric polling frequency from a list of pre-defined values
  • Monitoring categories
    Checkboxes to toggle which category of metrics to monitor
  • Process monitoring
    A number field to define how many processes to include as metrics and an additional select field to define the sorting

And as you can tell from the keys being used as label and description of the settings field, this configuration also uses localization.

Plugin.MoBroHardwareMonitor/mobro_plugin_config.json
{
"name": "modbros_mobrohardwaremonitor",
"displayName": "MoBro Hardware Monitor",
"author": "ModBros",
"description": null,
"assembly": "Plugin.MoBroHardwareMonitor.dll",
"localization": "Resources/Localization",
"settings": [
{
"type": "select",
"name": "update_frequency",
"label": "settings.update.title",
"description": "settings.update.desc",
"required": false,
"defaultValue": "1000",
"options": [
{
"label": "500 ms",
"value": "500"
}, {
"label": "750 ms",
"value": "750"
}, {
"label": "1000 ms",
"value": "1000"
}, {
"label": "1500 ms",
"value": "1500"
}, {
"label": "2000 ms",
"value": "2000"
}, {
"label": "3000 ms",
"value": "3000"
}, {
"label": "4000 ms",
"value": "4000"
}, {
"label": "5000 ms",
"value": "5000"
}
]
}, {
"type": "checkbox",
"name": "cpu_metrics",
"label": "settings.cpu.title",
"description": "settings.cpu.desc",
"defaultValue": true,
"required": false
}, {
"type": "checkbox",
"name": "gpu_metrics",
"label": "settings.gpu.title",
"description": "settings.gpu.desc",
"defaultValue": true,
"required": false
}, {
"type": "checkbox",
"name": "ram_metrics",
"label": "settings.ram.title",
"description": "settings.ram.desc",
"defaultValue": true,
"required": false
}, {
"type": "number",
"name": "num_processes",
"label": "settings.num_processes.title",
"description": "settings.num_processes.desc",
"required": false,
"defaultValue": 0,
"min": 0,
"max": 20
}, {
"type": "select",
"name": "processes_sort",
"label": "settings.processes_sort.title",
"description": "settings.processes_sort.desc",
"required": false,
"defaultValue": "cpu",
"options": [
{
"label": "settings.processes_sort.option_cpu",
"value": "cpu"
}, {
"label": "settings.processes_sort.option_ram",
"value": "ram"
}
]
}
]
}