How to Make a Plugin Interface for Your Program
Decoupling program behaviors pattern
Hello guys, my name is Marcelo Mancini aka Hipreme/MrcSnm, 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
- What behaviors do you want to expose to plugin code
- 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.
- How you will expose the main program functionality
- This almost always means creating an
abstract class, an
interfacewhich you will define functions that the plugin will must implement.
- Contract programming could be really amazing at this part of plugin programming, defining which
returnstatements are valid (if your language does not support it, just creates a function that calls your target function and check inside this wrapper function)
- Define an entry and exit point for the plugin
- 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:
- Execute the entry point defined inside the plugin(Execution arguments are accepted!)
- 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.
- If resources were asked for, Main program should provide resources/parameters to the plugin functions.
- Main program returns control to the plugin
- Plugin will have an exit point which occurs after main program brought the resources.
- 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:
- No serialization needed(Required for text file)
- Errors are easier to find, as you will have a compiler by your side
- No need to create an language interface for your main program to expose functionalities to the scripting language.
- 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
- Lesser need to sanity check, which will be needed for text files and json files(compiler does that automatically).
DLLis for Windows only, meaning that you will need to do conditional compilation for loading a
so(shared object) file.
- 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).
- 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!