How to Make a Plugin Interface for Your Program

Decoupling program behaviors pattern

Hipreme
Hipreme
Oct 22 · 4 min read
Image for post
Image for post

Introduction

Hello guys, my name is Marcelo Mancini aka , I’m mainly a game developer and I have been coding a lot for open-source community.

Right now, what I just did is to help D language community, basically a program that is able to create bindings. Bindings, are a way to use code made in another language in a target language, right now, what I’m targetting is C++. An example binding can be seen at BindBC-Cimgui

So, this article says about making a plugin, but what does it mean?

What is a plugin

Plugin is some way to add a feature to a software without changing the software at all. Creating a plugin firstly requires an interface(a plug to insert your plugin). To create this interface, there is some things that you need to define first:

What you need to define a plugin interface

  • This is the why and the first thing that you will need to answer, for creating a plugin, your program will firstly need to have some behavior to be connected/exposed to the plugin code.
  • This almost always means creating an abstract class, an interface which you will define functions that the plugin will must implement.
  1. Contract programming could be really amazing at this part of plugin programming, defining which return statements are valid (if your language does not support it, just creates a function that calls your target function and check inside this wrapper function)
  • This is one of the gotchas for creating your plugin interface, your main program and the plugin each must have an entry point, the entry point for your plugin defined in the interface must match the functionality you want to expose, for doing that, you main program must:
  1. Execute the entry point defined inside the plugin(Execution arguments are accepted!)
  2. Plugin must return a known value to the main program for knowing if an error ocurred or not, or possibly some messages for how the main program must process this plugin return value.
  3. If resources were asked for, Main program should provide resources/parameters to the plugin functions.
  4. Main program returns control to the plugin
  5. Plugin will have an exit point which occurs after main program brought the resources.
  6. Think how the plugin will be attached to your program
  • There are too many ways to do that, from reading a text file, a json file, to scripting a plugin or even native code plugin/compiled binaries. My recommendation from all of that is using compiled binaries, most known as (.DLL file), whose advantages are:
  1. No serialization needed(Required for text file)
  2. Errors are easier to find, as you will have a compiler by your side
  3. No need to create an language interface for your main program to expose functionalities to the scripting language.
  4. It almost inherently works, create a common file to be included by the plugin side and the main program side, this will define a contract which those two must follow
  5. Lesser need to sanity check, which will be needed for text files and json files(compiler does that automatically).
  • Disadvantages:
  1. The DLL is for Windows only, meaning that you will need to do conditional compilation for loading a dll or a so(shared object) file.
  2. The architectures must math. A 64 bit OS can’t run 32 bit dll, if you provide precompiled binaries, you will need to specify that, if you don’t want to the plugin coder need to have this information, you will need to create a function in your code that compiles the plugin for them(Which is my choice).
  3. Although seem as a disadvantage, you can expose your plugin coding with people with less experience about compiling, and even providing compilers erros inside your main program.

Example code for a plugin interface

Here, I’ll be showing how I made my plugin interface, which can be found at BindBC-generator.

Those codes here present are made in the D language, but it must be easy to follow if you have some Java, C# or C++ experience.

There is no secret on this code with the except of contract programming that could be new for some people, as I said before, it is important for having really nice function check as providing error informations and help information should be the first thing to be done.

Example code for a plugin implementation

This is a simple way how your plugin must be implemented on the plugin coder side.

So now you should have a base on how to create your own plugin! If you have any question, just post it on the comment section and I’ll be sure to provide the best answer I can!

Learning is how I live, Marcelo Silva Nascimento Mancini

Github

Linkedin

Hipreme/MrcSnm

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Hipreme

Written by

Hipreme

Game programmer, focused in trying to learn the most I can, aspiring D-lang programmer

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Hipreme

Written by

Hipreme

Game programmer, focused in trying to learn the most I can, aspiring D-lang programmer

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store