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

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:

The IPlugin Interface

Every plugin must implement the IPlugin interface, which defines the core methods and properties Melon expects:

Step-by-Step Guide

1. Set Up Your Development Environment

a. Create a New Class Library Project

  1. Open Visual Studio.
  2. Click on Create a new project.
  3. Select Class Library (.NET Framework). Ensure it's targeting .NET Framework 4.7.2 or compatible with Melon's version.
  4. Name your project, e.g., MyFirstMelonPlugin.
  5. 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.

  1. Right-click on your project in the Solution Explorer.
  2. Choose Add > Project Reference....
  3. In the Reference Manager, click on Browse....
  4. Navigate to the location of MelonLib.dll (In your Melon install directory) and select it.
  5. Click Add, then OK.

c. Verify Target Framework

Ensure your plugin targets the same .NET Framework version as Melon, which is currently 8.0.

  1. Right-click on your project and select Properties.
  2. Under the Application tab, check the Target framework.
  3. 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

3. Build Your Plugin

  1. Build the Solution:

    • Go to Build > Build Solution or press Ctrl+Shift+B.
    • Ensure there are no build errors.
  2. Locate the DLL:

    • Navigate to your project's bin/Debug or bin/Release folder.
    • You should find MyFirstMelonPlugin.dll.

4. Install the Plugin

5. Run Melon and Verify

  1. 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

Back to Introduction <--- ---> Next Tutorial: First UI