Exposing common API from main program *to* dylib in Rust

69 views Asked by At

I'm planning on making a game in Rust, and I'd like to have native plugins/mods in the form of dynamic libraries. I'm planning on using libloading for this.

Mods will be able to add new entities and items based on the existing game framework; I haven't worked out any specifics yet, but they should either use the game's traits on new structs, or construct existing structs using new stats, textures, etc.

The problem I'm facing is figuring out how to "export" the game's structs and implementations for mods/libraries to use, without compiling the mod along with the entire game.
For structs I'd like to expose to mods, I could probably throw together a minimal "header file" for my game and use it in each mod, but duplicating the actual code for each plugin seems very inefficient. Ideally, mods would use the game's native functions for common things like interacting with the world or displaying a GUI.

In libloading you can export symbols from a library to the main program, but I haven't heard of things working the other way around, even though it seems like it would be a fairly important feature to me. I've tried doing some research but I don't know what keywords to use, or even if this type of thing has a specific name.

Not being particularly experienced with Rust, I've only thought of 2 ways forward: either pass loaded libraries callbacks so they can use native functions, but I'm not sure how it'd work with structs, and I'm sure it'd get complicated quick; or I could give up on libraries and use a Lua as a modding interface, but at that point I might as well make all of the game's content in Lua.

Thanks!

0

There are 0 answers