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. Let's create a new file named mobro_plugin_config.json
. 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": null,
"assembly": "Plugin.Example.dll",
"localization": null,
"settings": []
}
For a detailed explanation of the configuration options, check out In-depth: Plugin configuration.
Creating Plugin.cs
To create the actual plugin, we'll add a new Plugin
class to the project. Plugins must implement the IMoBroPlugin
interface from the SDK. Start with the following structure:
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 key components of a plugin. To create one, we'll use the builder provided by the SDK. Start by adding the metric in the plugin constructor:
using MoBro.Plugin.SDK;
using MoBro.Plugin.SDK.Builders;
using MoBro.Plugin.SDK.Enums;
namespace Plugin.Example;
public class Plugin : IMoBroPlugin
{
public Plugin()
{
// 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
- Label:
Metric
- Description:
My first metric
- Type:
Text
- Category:
Miscellaneous
All metric IDs in a plugin must be unique.
Learn more about metrics and types in the Metric Reference and Category Reference.
Registering the Metric
After creating a metric, we need to register it with MoBro. This is done using the IMoBroService
, provided by the SDK.
The service object will be automatically injected at runtime, so you only need to add it as a constructor parameter.
Here's how you can register the metric:
public Plugin(IMoBroService mobro)
{
// Create a new metric
var metric = MoBroItem
.CreateMetric()
.WithId("first_metric")
.WithLabel("Hello")
.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, its value is still missing. Let's fix that.
Updating the Metric
To update the metric's value, we'll again use the IMoBroService
. Ensure that the metric is registered before
attempting to update its value.
Here's how to update the value of the metric:
public Plugin(IMoBroService mobro)
{
// Create a new metric
var metric = [...]
// Register the metric with MoBro
mobro.Register(metric);
// Update the metric's value
mobro.UpdateMetricValue(metric.Id, "Hello World");
}
The value of the metric has now been successfully updated to "Hello World"
.
The metric's value must match its defined 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
├── bin
├── obj
├── Plugin.Example.csproj
├── mobro_plugin_config.json
├── Plugin.cs
└── Program.cs
And that's it! You've successfully created your first plugin! 🎉