1
0
Fork 0

Add: initialize plugin when found

pull/11628/head
Patric Stout 2024-01-21 10:53:26 +01:00
parent 3122a992bf
commit e354b747f9
3 changed files with 69 additions and 0 deletions

View File

@ -289,6 +289,7 @@ static void ShutdownGame()
if (_network_available) NetworkShutDown(); // Shut down the network and close any open connections if (_network_available) NetworkShutDown(); // Shut down the network and close any open connections
SocialIntegration::Shutdown();
DriverFactoryBase::ShutdownDrivers(); DriverFactoryBase::ShutdownDrivers();
UnInitWindowSystem(); UnInitWindowSystem();

View File

@ -20,6 +20,25 @@
#include "safeguards.h" #include "safeguards.h"
/**
* Container to track information per plugin.
*/
class InternalSocialIntegrationPlugin {
public:
InternalSocialIntegrationPlugin(const std::string &filename, const std::string &basepath) : library(filename)
{
openttd_info.openttd_version = _openttd_revision;
}
OpenTTD_SocialIntegration_v1_PluginInfo plugin_info = {}; ///< Information supplied by plugin.
OpenTTD_SocialIntegration_v1_PluginApi plugin_api = {}; ///< API supplied by plugin.
OpenTTD_SocialIntegration_v1_OpenTTDInfo openttd_info = {}; ///< Information supplied by OpenTTD.
LibraryLoader library; ///< Library handle.
};
static std::vector<std::unique_ptr<InternalSocialIntegrationPlugin>> _plugins; ///< List of loaded plugins.
/** Helper for scanning for files with SocialIntegration as extension */ /** Helper for scanning for files with SocialIntegration as extension */
class SocialIntegrationFileScanner : FileScanner { class SocialIntegrationFileScanner : FileScanner {
public: public:
@ -40,6 +59,45 @@ public:
{ {
std::string basepath = filename.substr(basepath_length); std::string basepath = filename.substr(basepath_length);
Debug(misc, 1, "[Social Integration: {}] Loading ...", basepath); Debug(misc, 1, "[Social Integration: {}] Loading ...", basepath);
auto &plugin = _plugins.emplace_back(std::make_unique<InternalSocialIntegrationPlugin>(filename, basepath));
if (plugin->library.HasError()) {
Debug(misc, 0, "[Social Integration: {}] Failed to load library: {}", basepath, plugin->library.GetLastError());
return false;
}
OpenTTD_SocialIntegration_v1_GetInfo getinfo_func = plugin->library.GetFunction("SocialIntegration_v1_GetInfo");
if (plugin->library.HasError()) {
Debug(misc, 0, "[Social Integration: {}] Failed to find symbol SocialPlugin_v1_GetInfo: {}", basepath, plugin->library.GetLastError());
return false;
}
OpenTTD_SocialIntegration_v1_Init init_func = plugin->library.GetFunction("SocialIntegration_v1_Init");
if (plugin->library.HasError()) {
Debug(misc, 0, "[Social Integration: {}] Failed to find symbol SocialPlugin_v1_Init: {}", basepath, plugin->library.GetLastError());
return false;
}
getinfo_func(&plugin->plugin_info);
auto state = init_func(&plugin->plugin_api, &plugin->openttd_info);
switch (state) {
case OTTD_SOCIAL_INTEGRATION_V1_INIT_SUCCESS:
Debug(misc, 1, "[Social Integration: {}] Loaded for {}: {} ({})", basepath, plugin->plugin_info.social_platform, plugin->plugin_info.name, plugin->plugin_info.version);
return true;
case OTTD_SOCIAL_INTEGRATION_V1_INIT_FAILED:
Debug(misc, 0, "[Social Integration: {}] Failed to initialize", basepath);
return false;
case OTTD_SOCIAL_INTEGRATION_V1_INIT_PLATFORM_NOT_RUNNING:
Debug(misc, 1, "[Social Integration: {}] Failed to initialize: {} is not running", basepath, plugin->plugin_info.social_platform);
return false;
default:
NOT_REACHED();
}
} }
}; };
@ -48,3 +106,8 @@ void SocialIntegration::Initialize()
SocialIntegrationFileScanner fs; SocialIntegrationFileScanner fs;
fs.Scan(); fs.Scan();
} }
void SocialIntegration::Shutdown()
{
_plugins.clear();
}

View File

@ -16,6 +16,11 @@ public:
* Initialize the social integration system, loading any social integration plugins that are available. * Initialize the social integration system, loading any social integration plugins that are available.
*/ */
static void Initialize(); static void Initialize();
/**
* Shutdown the social integration system, and all social integration plugins that are loaded.
*/
static void Shutdown();
}; };
#endif /* SOCIAL_INTEGRATION_H */ #endif /* SOCIAL_INTEGRATION_H */