13#include "3rdparty/openttd_social_integration_api/openttd_social_integration_api.h"
38 this->
library = std::make_unique<LibraryLoader>(filename);
45 std::unique_ptr<LibraryLoader>
library =
nullptr;
50static std::vector<std::unique_ptr<InternalSocialIntegrationPlugin>>
_plugins;
59 std::string extension =
"-social.dll";
60#elif defined(__APPLE__)
61 std::string extension =
"-social.dylib";
63 std::string extension =
"-social.so";
69 bool AddFile(
const std::string &filename,
size_t basepath_length,
const std::string &)
override
71 std::string basepath = filename.substr(basepath_length);
72 Debug(misc, 1,
"[Social Integration: {}] Loading ...", basepath);
74 auto &plugin =
_plugins.emplace_back(std::make_unique<InternalSocialIntegrationPlugin>(filename, basepath));
77 if (plugin->library ==
nullptr) {
81 if (plugin->library->HasError()) {
84 Debug(misc, 0,
"[Social Integration: {}] Failed to load library: {}", basepath, plugin->library->GetLastError());
88 OpenTTD_SocialIntegration_v1_GetInfo getinfo_func = plugin->library->GetFunction(
"SocialIntegration_v1_GetInfo");
89 if (plugin->library->HasError()) {
92 Debug(misc, 0,
"[Social Integration: {}] Failed to find symbol SocialPlugin_v1_GetInfo: {}", basepath, plugin->library->GetLastError());
96 OpenTTD_SocialIntegration_v1_Init init_func = plugin->library->GetFunction(
"SocialIntegration_v1_Init");
97 if (plugin->library->HasError()) {
100 Debug(misc, 0,
"[Social Integration: {}] Failed to find symbol SocialPlugin_v1_Init: {}", basepath, plugin->library->GetLastError());
104 getinfo_func(&plugin->plugin_info);
106 plugin->external.social_platform = plugin->plugin_info.social_platform;
107 plugin->external.name = plugin->plugin_info.name;
108 plugin->external.version = plugin->plugin_info.version;
111 std::string lc_social_platform = plugin->plugin_info.social_platform;
112 strtolower(lc_social_platform);
118 Debug(misc, 0,
"[Social Integration: {}] Another plugin for {} is already loaded", basepath, plugin->plugin_info.social_platform);
123 auto state = init_func(&plugin->plugin_api, &plugin->openttd_info);
125 case OTTD_SOCIAL_INTEGRATION_V1_INIT_SUCCESS:
128 Debug(misc, 1,
"[Social Integration: {}] Loaded for {}: {} ({})", basepath, plugin->plugin_info.social_platform, plugin->plugin_info.name, plugin->plugin_info.version);
131 case OTTD_SOCIAL_INTEGRATION_V1_INIT_FAILED:
134 Debug(misc, 0,
"[Social Integration: {}] Failed to initialize", basepath);
137 case OTTD_SOCIAL_INTEGRATION_V1_INIT_PLATFORM_NOT_RUNNING:
140 Debug(misc, 1,
"[Social Integration: {}] Failed to initialize: {} is not running", basepath, plugin->plugin_info.social_platform);
151 std::vector<SocialIntegrationPlugin *> plugins;
154 plugins.push_back(&plugin->external);
172template <
typename T,
typename... Ts>
173static void PluginCall(std::unique_ptr<InternalSocialIntegrationPlugin> &plugin, T func, Ts... args)
179 if (func !=
nullptr) {
187 PluginCall(plugin, plugin->plugin_api.shutdown);
201 if (plugin->plugin_api.run_callbacks !=
nullptr) {
202 if (!plugin->plugin_api.run_callbacks()) {
203 Debug(misc, 1,
"[Social Plugin: {}] Requested to be unloaded", plugin->external.basepath);
207 PluginCall(plugin, plugin->plugin_api.shutdown);
216 PluginCall(plugin, plugin->plugin_api.event_enter_main_menu);
223 PluginCall(plugin, plugin->plugin_api.event_enter_scenario_editor, map_width, map_height);
230 PluginCall(plugin, plugin->plugin_api.event_enter_singleplayer, map_width, map_height);
237 PluginCall(plugin, plugin->plugin_api.event_enter_multiplayer, map_width, map_height);
244 PluginCall(plugin, plugin->plugin_api.event_joining_multiplayer);
Helper for scanning for files with a given name.
uint Scan(std::string_view extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Container to track information per plugin.
std::unique_ptr< LibraryLoader > library
Library handle.
SocialIntegrationPlugin external
Information of the plugin to be used by other parts of our codebase.
OpenTTD_SocialIntegration_v1_OpenTTDInfo openttd_info
Information supplied by OpenTTD.
OpenTTD_SocialIntegration_v1_PluginApi plugin_api
API supplied by plugin.
OpenTTD_SocialIntegration_v1_PluginInfo plugin_info
Information supplied by plugin.
Helper for scanning for files with SocialIntegration as extension.
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &) override
Add a file with the given filename.
@ PLATFORM_NOT_RUNNING
The plugin failed to initialize because the Social Platform is not running.
@ UNSUPPORTED_API
The plugin does not support the current API version.
@ RUNNING
The plugin is successfully loaded and running.
@ FAILED
The plugin failed to initialize.
@ DUPLICATE
Another plugin of the same Social Platform is already loaded.
@ INVALID_SIGNATURE
The signature of the plugin is invalid.
@ UNLOADED
The plugin is unloaded upon request.
State state
Result of the plugin's init function.
static void EventEnterScenarioEditor(uint map_width, uint map_height)
Event: user entered the Scenario Editor.
static void EventEnterSingleplayer(uint map_width, uint map_height)
Event: user entered a singleplayer game.
static std::vector< SocialIntegrationPlugin * > GetPlugins()
Get the list of loaded social integration plugins.
static void RunCallbacks()
Allow any social integration library to handle their own events.
static void EventEnterMultiplayer(uint map_width, uint map_height)
Event: user entered a multiplayer game.
static void Shutdown()
Shutdown the social integration system, and all social integration plugins that are loaded.
static void EventEnterMainMenu()
Event: user entered the main menu.
static void EventJoiningMultiplayer()
Event: user is joining a multiplayer game.
static void Initialize()
Initialize the social integration system, loading any social integration plugins that are available.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Functions for Standard In/Out file operations.
@ SOCIAL_INTEGRATION_DIR
Subdirectory for all social integration plugins.
Functions/types related to loading libraries dynamically.
declaration of OTTD revision dependent variables
A number of safeguards to prevent using unsafe methods.
bool ValidateSignatureFile(const std::string &filename)
Validate that the signatures mentioned in the signature file are matching the files in question.
Routines to validate signature files.
static void PluginCall(std::unique_ptr< InternalSocialIntegrationPlugin > &plugin, T func, Ts... args)
Wrapper to call a function pointer of a plugin if it isn't a nullptr.
static std::vector< std::unique_ptr< InternalSocialIntegrationPlugin > > _plugins
List of loaded plugins.
static std::set< std::string > _loaded_social_platform
List of Social Platform plugins already loaded. Used to prevent loading a plugin for the same Social ...
Interface definitions for game to report/respond to social integration.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.