Import Functions
Learn how to import functions from other plugins written in different languages and use them in your own.
To use functions from another plugin in your C# plugin, you need to generate language-specific header files. These headers provide the necessary wrappers to call functions exported by other plugins. This guide explains how to generate these headers and how to use them in your C# plugin.
Generating Header Files
Plugify provides a unified generator tool to automatically generate header files for importing functions from other plugins. These headers include wrapper functions that handle the function calls and parameter passing.
Steps to Generate Bindings
Using the Online Generator:
Visit plugify-gen tool to generate bindings through a user-friendly web interface. Simply upload your plugin manifest file (.pplugin) and select C# as the target language to generate the corresponding C# module.
Using the Command-Line Tool:
You can also download and use the generator tool locally from the plugify-gen repository.
Example usage:
Integrate the Generated Module:
- The tool generates the folder
imported/plugin_from_another_language/withcore.cs,enums.cs,aliases.csanddelegates.csin the specified output directory. - Copy the generated folder to your project directory.
- Declare the plugin in the
dependenciesof your manifest, so that it loads before yours.
Project Integration
After generating the module, integrate it into your C# project:
Copy Module to Project
Copy the generated module folder to your project directory:
Add to .csproj
SDK-style projects compile every .cs file in the project folder, so the copied files are picked up automatically. If your project lists files explicitly, add them to your .csproj:
Or manually add each file:
Using Namespace
Use the generated namespace in your C# files:
Using Generated Wrapper Functions
The generated module contains wrapper functions that allow you to call functions from the other plugin. These wrappers handle the function address lookup and parameter passing.
Example Generated Header
Here’s an example of a generated header file for a plugin named plugin_from_another_language:
How It Works
ParamCallbackis the method you call in your C# code.- The wrapper (
___ParamCallback) converts strings and arrays to Plugify types and cleans them up after the call. - The function pointer
__ParamCallbackis set by the language module when your plugin loads. If the other plugin is also written in C#, the module points_ParamCallbackstraight at its method, without the conversion.
Example: Using the Generated Header
Here’s how you can use the generated header in your C# plugin:
When is Header Generation Necessary?
In C#, you need generated bindings for every plugin you import, whatever language that plugin is written in. Without them, the compiler cannot reference the exported functions. Plugins written in dynamically-typed languages like Python do not need this step, because method binding happens at runtime.
Best Practices
- Use the Generator Tool: Always use the Plugify generator tool (online or command-line) to generate headers for imported functions.
- Include Generated Headers: Include the generated headers in your plugin source files to access the exported functions.
- Test Thoroughly: Test the imported functions to ensure they work as expected.
- Document Dependencies: Clearly document the plugins and functions your plugin depends on.
Conclusion
Importing functions from another plugin in C# is straightforward when you use the Plugify generator tool to generate the necessary headers. These headers provide wrapper functions that handle function address lookup and parameter passing, making it easy to integrate functionality from other plugins. By following the steps and best practices outlined in this guide, you can create robust and interoperable plugins in the Plugify ecosystem.