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.
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:
Field | Type | Default | Restrictions | Description |
---|---|---|---|---|
type | string | - | Requiredcheckbox OR number OR select OR string | Type of the settings field. |
name | string | - | Required Length: 1 - 128 Pattern: ^[\w-]+$ | The name (key) of the field |
label | string | - | Required Length: 1 - 128 | The visible label of the field |
description | string? | - | An optional description of the field | |
required | string | false | Whether the field is required |
Checkbox
A checkbox settings field
Additional Fields
Field | Type | Description |
---|---|---|
defaultValue | bool? | The optional default value |
Number
A numeric settings field
Additional Fields
Field | Type | Description |
---|---|---|
defaultValue | double? | The optional default value |
min | double? | The optional minimum value to validate the field |
max | double? | The optional maximum value to validate the field |
String
A textual settings field
Additional Fields
Field | Type | Description |
---|---|---|
defaultValue | string? | The optional default value |
regex | string? | A optional regex to validate the field |
Select
A select settings field
Contains a list of pre-defined selectable options
Additional Fields
Field | Type | Description |
---|---|---|
defaultValue | string? | The optional default value |
options | array | The array of selectable options |
Options
Field | Type | Restrictions | Description |
---|---|---|---|
label | string | Required Length: 1 - 128 | The visible label of the option |
value | string | Required 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).
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.
{
"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"
}
]
}
]
}