Plugin Tutorial: Getting Started
In this tutorial, we'll guide you through the process of setting up your development environment and creating your first Melon plugin. By the end of this tutorial, you'll have a basic understanding of how the plugin system works and a simple plugin that integrates with Melon.
Prerequisites
- Development Environment: Visual Studio 2019 or later (or any C# IDE)
- .NET Framework: Ensure you have .NET Framework 4.7.2 or later installed.
- MelonLib.dll: This is the core library that your plugin will reference. Obtain this from the Melon project.
- Basic Knowledge of C#: Familiarity with C# programming, classes, and interfaces.
Understanding the Plugin System
How Plugins Work in Melon
Plugins in Melon are C# class libraries (.dll
files) that
implement the IPlugin
interface. When Melon starts, it
scans the Plugins
directory for any DLLs and loads them.
Plugins:
-
Access Melon's core functionalities through provided interfaces
IHost and IWebApi
- These interfaces give access to Melon APIs and Server APIs respectively
- UI can be made easily with built in methods for option menus, progress bars, etc.
- Interact with Melon's API endpoints, extending or overriding existing endpoints or creating new ones.
The IPlugin Interface
Every plugin must implement the IPlugin
interface, which
defines the core methods and properties Melon expects:
-
Properties:
Name
: The name of the plugin.Version
: Plugin version.Authors
: Authors of the plugin.Description
: A brief description.-
Host
: Provides access to Melon's host services. -
WebApi
: Provides access to Melon's web API services.
-
Methods:
-
LoadMelonCommands
: Initialize plugin with Melon host. -
LoadMelonServerCommands
: Initialize plugin with Melon web API. LoadUI
: Load UI elements.UnloadUI
: Unload UI elements.-
Execute
: Start background processes or middleware. Destroy
: Clean up resources.GetHelpOptions
: Provide command-line options.
-
Step-by-Step Guide
1. Set Up Your Development Environment
a. Create a New Class Library Project
- Open Visual Studio.
- Click on Create a new project.
- Select Class Library (.NET Framework). Ensure it's targeting .NET Framework 4.7.2 or compatible with Melon's version.
- Name your project, e.g.,
MyFirstMelonPlugin
. - Choose a location and click Create.
b. Add References to MelonLib
Since MelonLib is not available via NuGet yet, you'll need to reference it manually.
- Right-click on your project in the Solution Explorer.
- Choose Add > Project Reference....
- In the Reference Manager, click on Browse....
-
Navigate to the location of
MelonLib.dll
(In your Melon install directory) and select it. - Click Add, then OK.
c. Verify Target Framework
Ensure your plugin targets the same .NET Framework version as Melon, which is currently 8.0.
- Right-click on your project and select Properties.
- Under the Application tab, check the Target framework.
- If necessary, change it to match Melon's target framework.
2. Implement the IPlugin Interface
Create a class that implements IPlugin
.
a. Create the Plugin Class
using Melon.Interface;
using Melon.Models;
namespace MyFirstMelonPlugin
{
public class MyPlugin : IPlugin
{
public string Name => "My First Plugin";
public string Version => "1.0.0";
public string Authors => "Your Name";
public string Description => "A simple plugin for Melon.";
public IHost Host { get; set; }
public IWebApi WebApi { get; set; }
public void LoadMelonCommands(IHost host)
{
Host = host;
}
public void LoadMelonServerCommands(IWebApi webapi)
{
WebApi = webapi;
}
public int LoadUI()
{
// Add UI elements here (will be covered in later tutorials)
return 0;
}
public int UnloadUI()
{
// Remove UI elements here
return 0;
}
public int Execute()
{
// Start background processes or middleware here
return 0;
}
public int Destroy()
{
// Clean up any resources or stop processes here
return 0;
}
public Dictionary<string, string> GetHelpOptions()
{
// Provide command-line options if necessary (will be covered in later tutorials)
return new Dictionary<string, string>();
}
}
}
b. Understanding the Code
-
Class
MyPlugin
: ImplementsIPlugin
, making it recognizable by Melon. -
Properties:
- Provide basic information about your plugin.
- Name and Authors combo should be unique from other plugins, Melon may have conflicts if two plugins are loaded with the same Name + Authors.
- Authors should separate authors in string by comma.
- Version should be standard
major.minor.patch
.
-
Methods:
-
LoadMelonCommands: Called by Melon to pass the
IHost
interface. -
LoadMelonServerCommands: Called by Melon to pass
the
IWebApi
interface. - LoadUI: Use this to add elements to Melon's UI.
- UnloadUI: Remove any UI elements you've added.
- Execute: Start any continuous processes or middleware.
- Destroy: Clean up resources or stop processes.
- GetHelpOptions: Return any command-line options your plugin supports.
-
LoadMelonCommands: Called by Melon to pass the
3. Build Your Plugin
-
Build the Solution:
- Go to Build > Build Solution or press Ctrl+Shift+B.
- Ensure there are no build errors.
-
Locate the DLL:
-
Navigate to your project's
bin/Debug
orbin/Release
folder. - You should find
MyFirstMelonPlugin.dll
.
-
Navigate to your project's
4. Install the Plugin
- Copy
MyFirstMelonPlugin.dll
. - Navigate to Melon's app data directory.
- Find or create a
Plugins
folder. - Paste your DLL into this folder.
5. Run Melon and Verify
-
Start Melon:
- Run Melon as you normally would.
-
Melon should automatically load plugins from the
Plugins
directory. - Go to Settings -> Plugins -> View Plugins and see if your plugin shows up.
6. Troubleshooting
-
Plugin Not Detected:
-
Ensure the DLL is in the correct
Plugins
folder. - Check for any exceptions in Melon's logs, located in the app data folder root.
-
Ensure the DLL is in the correct
-
Version Incompatibility:
- Ensure your plugin targets the same .NET Framework as Melon.
-
Ensure
MelonLib.dll
is compatible with this version of Melon.
Back to Introduction <--- ---> Next Tutorial: First UI