Management and Lifecycle
This document outlines the management and lifecycle of plugins within the oglr
game engine.
Plugin Loading
Plugins are loaded at runtime, using the oglr::plugin::PluginManager
class.
Process:
- The
PluginManager
searches for.dll
files in a specified directory (plugins/
by default). - For each
.dll
file, thePluginManager
attempts to load it using theLoadLibrary
function. - If the library loads successfully, the
PluginManager
searches for a function namedoglr_plugin_entry_point
. This function should return a pointer to aPlugin
object. - The
PluginManager
stores the loaded plugin’s entry point, along with other information.
Example:
// plugins/my_plugin.cpp
#include "oglr/plugin/Plugin.h"
class MyPlugin : public oglr::plugin::Plugin {
public:
MyPlugin() : Plugin("MyPlugin") {}
};
extern "C" OGLR_PLUGIN_EXPORT oglr::plugin::Plugin *oglr_plugin_entry_point() {
return new MyPlugin();
}
Source: oglr/plugin/PluginManager.cpp
Plugin Unloading
Plugins are unloaded when the game shuts down or when the PluginManager::unloadPlugin
function is called.
Process:
- The
PluginManager
calls thePlugin::onUnload
function for each plugin. - The
PluginManager
calls theFreeLibrary
function to unload the plugin’s library.
Source: oglr/plugin/PluginManager.cpp
Plugin Lifecycle
Plugins go through a series of states during their lifecycle.
States:
- Loading: The plugin is being loaded into memory.
- Ready: The plugin has been loaded and is ready to be used.
- Unloading: The plugin is being unloaded from memory.
- Unloaded: The plugin has been unloaded from memory.
Example:
// plugins/my_plugin.cpp
#include "oglr/plugin/Plugin.h"
class MyPlugin : public oglr::plugin::Plugin {
public:
MyPlugin() : Plugin("MyPlugin") {}
void onReady() override {
// Perform initialization tasks here
std::cout << "MyPlugin is ready!" << std::endl;
}
void onUnload() override {
// Perform cleanup tasks here
std::cout << "MyPlugin is unloading!" << std::endl;
}
};
Source: oglr/plugin/Plugin.h
Plugin Configuration
Plugins can be configured using a PluginConfig
object. This object is passed to the Plugin
constructor.
Example:
// plugins/my_plugin.cpp
#include "oglr/plugin/Plugin.h"
class MyPlugin : public oglr::plugin::Plugin {
public:
MyPlugin(const PluginConfig& config) : Plugin("MyPlugin", config) {}
void onReady() override {
// Access configuration values here
std::cout << "MyPlugin configuration: " << config.getString("my_config_value") << std::endl;
}
};
extern "C" OGLR_PLUGIN_EXPORT oglr::plugin::Plugin *oglr_plugin_entry_point() {
oglr::plugin::PluginConfig config;
// Set configuration values here
config.setString("my_config_value", "hello world");
return new MyPlugin(config);
}
Source: oglr/plugin/PluginConfig.h
Plugin Dependencies
Plugins can declare dependencies on other plugins. These dependencies are specified in the PluginConfig
object.
Example:
// plugins/my_plugin.cpp
#include "oglr/plugin/Plugin.h"
class MyPlugin : public oglr::plugin::Plugin {
public:
MyPlugin(const PluginConfig& config) : Plugin("MyPlugin", config) {}
void onReady() override {
// Check if a dependency is available
if (hasDependency("my_dependency")) {
std::cout << "MyPlugin dependency is available!" << std::endl;
} else {
std::cout << "MyPlugin dependency is not available!" << std::endl;
}
}
};
extern "C" OGLR_PLUGIN_EXPORT oglr::plugin::Plugin *oglr_plugin_entry_point() {
oglr::plugin::PluginConfig config;
// Specify dependencies
config.addDependency("my_dependency");
return new MyPlugin(config);
}
Source: oglr/plugin/Plugin.h
, oglr/plugin/PluginConfig.h