First Simple Plugin
Now that we have set up the project, it's time to create and implement a simple plugin.
Configuration File (JSON)
Every plugin requires a configuration file. Create a new file named mobro_plugin_config.json in the project directory.
Note: The file name must be exactly as specified.
Add the following minimal configuration to the file:
{
"name": "example_plugin",
"displayName": "Example Plugin",
"author": "Me",
"description": "My first MoBro plugin",
"assembly": "Plugin.Example.dll",
"settings": []
}
nameis the unique identifier of the plugin. It's not shown to users.displayNameis the name shown to users.assemblyis the.dllfile MoBro loads. It's named after the project.
For a detailed explanation of all configuration options, check out In-depth: Plugin Configuration.
Creating Plugin.cs
To create the actual plugin, add a new Plugin class to the project. Plugins must implement the IMoBroPlugin
interface from the SDK:
using MoBro.Plugin.SDK;
namespace Plugin.Example;
public class Plugin : IMoBroPlugin
{
}
Congratulations! You now have a valid plugin — although it doesn't do anything yet. Let's proceed by creating a metric.
Creating a Metric
Metrics are the key components of a plugin. Each metric represents a single value, e.g. a temperature or the name of the operating system.
Metrics are created using the builder provided by the SDK. We'll create our metric in the Init() function, which
MoBro calls once right after creating the plugin:
using MoBro.Plugin.SDK;
using MoBro.Plugin.SDK.Builders;
using MoBro.Plugin.SDK.Enums;
namespace Plugin.Example;
public class Plugin : IMoBroPlugin
{
public void Init()
{
// Create a new metric
var metric = MoBroItem
.CreateMetric()
.WithId("first_metric")
.WithLabel("Metric", "My first metric")
.OfType(CoreMetricType.Text)
.OfCategory(CoreCategory.Miscellaneous)
.OfNoGroup()
.Build();
}
}
This creates a metric with the following details:
- ID:
first_metric(must be unique within the plugin) - Label:
Metric - Description:
My first metric - Type:
Text— the metric's value is a text - Category:
Miscellaneous - Group: none
Learn more about metrics, types and categories in the Metric Reference, MetricType Reference and Category Reference.
Registering the Metric
After creating a metric, it needs to be registered with MoBro. This is done using the IMoBroService provided by the
SDK. MoBro automatically injects the service at runtime, so we only need to add it as a constructor parameter:
using MoBro.Plugin.SDK;
using MoBro.Plugin.SDK.Builders;
using MoBro.Plugin.SDK.Enums;
using MoBro.Plugin.SDK.Services;
namespace Plugin.Example;
public class Plugin : IMoBroPlugin
{
private readonly IMoBroService _mobro;
public Plugin(IMoBroService mobro)
{
_mobro = mobro;
}
public void Init()
{
// Create a new metric
var metric = MoBroItem
.CreateMetric()
.WithId("first_metric")
.WithLabel("Metric", "My first metric")
.OfType(CoreMetricType.Text)
.OfCategory(CoreCategory.Miscellaneous)
.OfNoGroup()
.Build();
// Register the metric with MoBro
_mobro.Register(metric);
}
}
Awesome! Your metric is now registered with MoBro. However, it doesn't have a value yet. Let's fix that.
Updating the Metric
To update the metric's value, we'll use the IMoBroService again. A metric must be registered before its value can be
updated:
using MoBro.Plugin.SDK;
using MoBro.Plugin.SDK.Builders;
using MoBro.Plugin.SDK.Enums;
using MoBro.Plugin.SDK.Services;
namespace Plugin.Example;
public class Plugin : IMoBroPlugin
{
private readonly IMoBroService _mobro;
public Plugin(IMoBroService mobro)
{
_mobro = mobro;
}
public void Init()
{
// Create a new metric
var metric = MoBroItem
.CreateMetric()
.WithId("first_metric")
.WithLabel("Metric", "My first metric")
.OfType(CoreMetricType.Text)
.OfCategory(CoreCategory.Miscellaneous)
.OfNoGroup()
.Build();
// Register the metric with MoBro
_mobro.Register(metric);
// Update the metric's value
_mobro.UpdateMetricValue(metric.Id, "Hello World");
}
}
The value of the metric is now set to "Hello World".
The metric's value must match its type. In this case, the type is CoreMetricType.Text, so the value must be a string.
For more details on updating values and type restrictions, visit In-depth: Updating Metric Values.
Project Structure
After adding all the files mentioned above, your project structure should look like this:
Plugin.Example
├── mobro_plugin_config.json
├── Plugin.cs
├── Plugin.Example.csproj
└── Program.cs
And that's it! You've successfully created your first plugin! 🎉
Next, let's run it.