Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::ecs::Plugins Class Reference

The plugins a game loaded, kept loaded as long as it lives. More...

#include <aegis2d/ecs/plugins.hpp>

Public Member Functions

 Plugins ()=default
 
 Plugins (const Plugins &)=delete
 
Plugins & operator= (const Plugins &)=delete
 
 Plugins (Plugins &&) noexcept=default
 
Plugins & operator= (Plugins &&) noexcept=default
 
 ~Plugins ()=default
 
std::string load (const std::filesystem::path &path, PluginContext &context)
 Loads a plugin and lets it add its systems and assets.
 
std::size_t load_folder (const std::filesystem::path &folder, PluginContext &context)
 Loads every plugin of a folder, in the order of their file names.
 
std::size_t size () const noexcept
 Number of plugins loaded.
 

Detailed Description

The plugins a game loaded, kept loaded as long as it lives.

A plugin is an Aegis2D library (see core::LibraryInfo) that also exports a start function with C linkage, built against the same engine version:

extern "C" MY_PLUGIN_EXPORT const aegis2d::core::LibraryInfo* aegis2d_library_entry() {
static constexpr aegis2d::core::LibraryInfo info{
return &info;
}
extern "C" MY_PLUGIN_EXPORT void aegis2d_plugin_start(aegis2d::ecs::PluginContext& game) {
game.systems().add("drift", [](aegis2d::ecs::Registry& registry,
const aegis2d::ecs::Tick&) { ... });
game.add_asset_folder("plugins/drift/assets");
}
What a plugin can add to the game, given to its start function.
Definition plugins.hpp:20
void add_asset_folder(std::filesystem::path folder)
Adds a folder of assets the plugin brings, which the game loads like its own.
Definition plugins.hpp:32
Scheduler & systems() const noexcept
The game's systems: the plugin adds its own, e.g. systems().add("drift", ...).
Definition plugins.hpp:28
Entry point of the ECS: creates entities and stores their components.
Definition registry.hpp:34
System & add(std::string name, Args &&... args)
Builds a system in place and adds it after the others.
Definition system.hpp:118
constexpr std::uint32_t library_api_version
Version of the interface between the engine and the libraries it loads.
Definition shared_library.hpp:19
What an Aegis2D library tells about itself, from its C entry point.
Definition shared_library.hpp:36
std::uint32_t api_version
library_api_version the library was built with.
Definition shared_library.hpp:38
One simulation step, as the systems see it.
Definition system.hpp:23

The game loads them at start:

ecs::Plugins plugins; // before the scheduler: the systems run code of the plugins
ecs::PluginContext context{systems};
plugins.load_folder(core::executable_dir() / "plugins", context);
The plugins a game loaded, kept loaded as long as it lives.
Definition plugins.hpp:77
std::size_t load_folder(const std::filesystem::path &folder, PluginContext &context)
Loads every plugin of a folder, in the order of their file names.
Ordered list of named systems, all updated once per tick.
Definition system.hpp:100
std::filesystem::path executable_dir()
Folder containing the running executable.

The systems a plugin adds run the plugin's code, so the Plugins must outlive the Scheduler: declare it first. A plugin reads the game's components through the registry, which finds them by typeid: build it with the default visibility of its symbols, like the game, so that both see the same types.

Constructor & Destructor Documentation

◆ Plugins() [1/3]

aegis2d::ecs::Plugins::Plugins ( )
default

◆ Plugins() [2/3]

aegis2d::ecs::Plugins::Plugins ( const Plugins &  )
delete

◆ Plugins() [3/3]

aegis2d::ecs::Plugins::Plugins ( Plugins &&  )
defaultnoexcept

◆ ~Plugins()

aegis2d::ecs::Plugins::~Plugins ( )
default

Member Function Documentation

◆ load()

std::string aegis2d::ecs::Plugins::load ( const std::filesystem::path &  path,
PluginContext &  context 
)

Loads a plugin and lets it add its systems and assets.

Checks it with core::load_library(), then calls its start function with context. The library stays loaded even if the start function throws, since it may have added systems before.

Parameters
pathPath of the plugin's library.
contextWhat the plugin can add to.
Returns
The plugin's name, from its LibraryInfo.
Exceptions
core::LibraryErrorif it cannot be loaded, is of another version or has no start function, or if the start function throws (with its message).

◆ load_folder()

std::size_t aegis2d::ecs::Plugins::load_folder ( const std::filesystem::path &  folder,
PluginContext &  context 
)

Loads every plugin of a folder, in the order of their file names.

Files whose extension is not the system's library one (.so, .dylib or .so on macOS, .dll) are skipped; a missing folder loads nothing.

Parameters
folderFolder of the plugins, such as executable_dir() / "plugins".
contextWhat the plugins can add to.
Returns
The number of plugins loaded.
Exceptions
core::LibraryErrorat the first plugin that cannot be loaded.

◆ operator=() [1/2]

Plugins & aegis2d::ecs::Plugins::operator= ( const Plugins &  )
delete

◆ operator=() [2/2]

Plugins & aegis2d::ecs::Plugins::operator= ( Plugins &&  )
defaultnoexcept

◆ size()

std::size_t aegis2d::ecs::Plugins::size ( ) const
inlinenoexcept

Number of plugins loaded.

Returns
The number of libraries kept loaded.

The documentation for this class was generated from the following file: