First, simple plugin
After setting up the project, we will now start to create and implement an actual (simple) plugin.
Configuration file (Json)
Every plugin requires a configuration file. So let's add a new and empty configuration file
named mobro_plugin_config.json
to the project (The file needs to be named exactly like this).
Now let's add a minimal configuration to that file:
{
"name": "example_plugin",
"displayName": "Example Plugin",
"author": "",
"description": null,
"assembly": "Plugin.Example.dll",
"localization": null,
"settings": []
}
More details at In depth: Plugin configuration
Plugin.cs
Now we're going to create the actual plugin. For this we will add a new Plugin
class to the project.
A Plugin must implement the IMoBroPlugin
interface from the SDK. So let's do that in our newly created Plugin
class.
So the class currently only contains:
using MoBro.Plugin.SDK;
namespace Plugin.Example;
public class Plugin : IMoBroPlugin
{
}
With that in place we actually already created a valid plugin. Well it just doesn't do anything yet...
So let's continue and create our first metric.
Create a metric
In order to create a metric we will utilize the builder provided by the SDK.
For now, let's just create the metric in the Plugins constructor like this:
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();
}
[...]
}
We've now created a new metric and assigned it the id first_metric
, the label Metric
and a description
of My first metric
.
This metric is of type Text
and we've assigned it to the Miscellaneous
category.
All metrics of a plugin must be assigned a unique id.
More on metric and their types in the Metric reference
More on categories in the Category reference
Register the metric
Now that we have created our first metric, we need to register it to MoBro.
To do this we will utilize the IMoBroService
provided by the SDK. To get an instance of this service we will just add
it
as a constructor parameter. The instance will automatically be injected on runtime, so we don't need to worry about
that (more on that in In depth: Using services).
So let's use the service and register our 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 metric to MoBro
mobro.Register(metric);
}
Awesome, now MoBro knows about the metric our plugin provides.
But the metric is still missing a value.
Update the metric
In order to update a metric we will again utilize the IMoBroService
.
Note that a metric needs to be registered first before its value can be updated.
In order to update the value we just pass in the id of our metric alongside the new updated value.
public Plugin(IMoBroService mobro)
{
// create a new metric
var metric = [...]
// register metric to MoBro
mobro.Register(metric);
// update metric value
mobro.UpdateMetricValue(metric.Id, "Hello World");
}
We've now successfully updated the value of our metric to the string Hello World
.
The value of a metric must conform to its type.
In this case we've set the type to CoreMetricType.Text
so we must set a string as its value.
More details on updating metrics and the type restrictions of metric values in In depth: Updating metric values.
Project structure
After adding all the files as mentioned above your resulting project structure should now look like this:
Plugin.Example
├── bin
├── obj
├── Plugin.Example.csproj
├── mobro_plugin_config.json
├── Plugin.cs
└── Program.cs