Feature: Plugin framework for Social Integration with Steam, Discord, GOG, etc (#11628)
parent
75f21065c9
commit
d3b2a576de
@ -0,0 +1,4 @@
|
||||
add_files(
|
||||
openttd_social_integration_api.h
|
||||
openttd_social_integration_api_v1.h
|
||||
)
|
@ -0,0 +1,20 @@
|
||||
Copyright 2024 OpenTTD project
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files
|
||||
(the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included
|
||||
in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2024 OpenTTD project
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge,
|
||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
* and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Although all the source-files created by OpenTTD are licensed under the
|
||||
* GPL-v2, this file is an exception. This file is part of the API for
|
||||
* social integration plugins, and licensed under the MIT license, to allow
|
||||
* for non-free implementations.
|
||||
*/
|
||||
|
||||
/** @file openttd_social_integration_api.h Interface definitions for plugins to report/respond to social integration. */
|
||||
|
||||
#ifndef OPENTTD_SOCIAL_INTEGRATION_API_H
|
||||
#define OPENTTD_SOCIAL_INTEGRATION_API_H
|
||||
|
||||
#include "openttd_social_integration_api_v1.h"
|
||||
|
||||
#endif /* OPENTTD_SOCIAL_INTEGRATION_API_H */
|
@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright 2024 OpenTTD project
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge,
|
||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
* and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Although all the source-files created by OpenTTD are licensed under the
|
||||
* GPL-v2, this file is an exception. This file is part of the API for
|
||||
* social integration plugins, and licensed under the MIT license, to allow
|
||||
* for non-free implementations.
|
||||
*/
|
||||
|
||||
/** @file v1.h Version 1 definition of the OpenTTD Social Integration Plugin API. */
|
||||
|
||||
#ifndef OPENTTD_SOCIAL_INTEGRATION_API_V1_H
|
||||
#define OPENTTD_SOCIAL_INTEGRATION_API_V1_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** Pointers supplied by the plugin for OpenTTD to use. */
|
||||
struct OpenTTD_SocialIntegration_v1_PluginInfo {
|
||||
/**
|
||||
* The Social Platform this plugin is for.
|
||||
*
|
||||
* UTF-8, nul-terminated. The plugin is and remains the owner of the memory.
|
||||
*
|
||||
* As there can only be one plugin active for each Social Platform, this
|
||||
* value is used to determine which plugin to use.
|
||||
*
|
||||
* A complete list of names can be found here:
|
||||
* https://wiki.openttd.org/en/Development/Social%20Integration
|
||||
*
|
||||
* Please use names from that list, including capitalization.
|
||||
*
|
||||
* If you create a plugin for a new Social Platform, please add it to the
|
||||
* wiki page.
|
||||
*/
|
||||
const char *social_platform;
|
||||
|
||||
const char *name; ///< Full name of the plugin. UTF-8, nul-terminated. The plugin is and remains the owner of the memory.
|
||||
const char *version; ///< Version of the plugin. UTF-8, nul-terminated. The plugin is and remains the owner of the memory.
|
||||
};
|
||||
|
||||
/** Pointers supplied by the plugin for OpenTTD to use. */
|
||||
struct OpenTTD_SocialIntegration_v1_PluginApi {
|
||||
/**
|
||||
* OpenTTD tells the plugin to shut down.
|
||||
*
|
||||
* The plugin should free any resources it allocated, and must not call any of the callback functions after this call.
|
||||
*/
|
||||
void (*shutdown)();
|
||||
|
||||
/**
|
||||
* OpenTTD calls this function at regular intervals, to handle any callbacks the plugin might have.
|
||||
*
|
||||
* It is also safe to call the OpenTTD_SocialIntegrationCallbacks functions here.
|
||||
*
|
||||
* @return True if the plugin wants to be called again, false if the plugin wants to be unloaded.
|
||||
*/
|
||||
bool (*run_callbacks)();
|
||||
|
||||
/**
|
||||
* The player has entered the main menu.
|
||||
*/
|
||||
void (*event_enter_main_menu)();
|
||||
|
||||
/**
|
||||
* The player has entered the Scenario Editor.
|
||||
*
|
||||
* @param map_width The width of the map in tiles.
|
||||
* @param map_height The height of the map in tiles.
|
||||
*/
|
||||
void (*event_enter_scenario_editor)(unsigned int map_width, unsigned int map_height);
|
||||
|
||||
/**
|
||||
* The player has entered a singleplayer game.
|
||||
*
|
||||
* @param map_width The width of the map in tiles.
|
||||
* @param map_height The height of the map in tiles.
|
||||
*/
|
||||
void (*event_enter_singleplayer)(unsigned int map_width, unsigned int map_height);
|
||||
|
||||
/**
|
||||
* The player has entered a multiplayer game.
|
||||
*
|
||||
* @param map_width The width of the map in tiles.
|
||||
* @param map_height The height of the map in tiles.
|
||||
*/
|
||||
void (*event_enter_multiplayer)(unsigned int map_width, unsigned int map_height);
|
||||
|
||||
/**
|
||||
* The player is joining a multiplayer game.
|
||||
*
|
||||
* This is followed by event_enter_multiplayer() if the join was successful.
|
||||
*/
|
||||
void (*event_joining_multiplayer)();
|
||||
};
|
||||
|
||||
/** Pointers supplied by OpenTTD, for the plugin to use. */
|
||||
struct OpenTTD_SocialIntegration_v1_OpenTTDInfo {
|
||||
const char *openttd_version; ///< Version of OpenTTD. UTF-8, nul-terminated. OpenTTD is and remains the owner of the memory.
|
||||
};
|
||||
|
||||
/** The result of the initialization. */
|
||||
enum OpenTTD_SocialIntegration_v1_InitResult : int {
|
||||
OTTD_SOCIAL_INTEGRATION_V1_INIT_SUCCESS = 1, ///< Plugin initialized successfully.
|
||||
OTTD_SOCIAL_INTEGRATION_V1_INIT_FAILED = -1, ///< Plugin failed to initialize (generic error).
|
||||
OTTD_SOCIAL_INTEGRATION_V1_INIT_PLATFORM_NOT_RUNNING = -2, ///< The Social Platform is not running.
|
||||
};
|
||||
|
||||
/**
|
||||
* Type of the Init function the plugin is expected to export from its dynamic library.
|
||||
*
|
||||
* The plugin has to export the implementation of this function as "SocialIntegration_vN_Init", where N is the API version this entry point is for.
|
||||
* A single plugin can have multiple versions implemented.
|
||||
*
|
||||
* @param[out] plugin_api Structure the plugin must fill with pointers. Can contain nullptr if the plugin does not support a feature. The plugin is owner of the memory.
|
||||
* @param[in] openttd_info Structure that OpenTTD filled with pointers. All pointers will remain valid until shutdown(). OpenTTD is owner of the memory.
|
||||
* @return The status of the initialization.
|
||||
*/
|
||||
typedef OpenTTD_SocialIntegration_v1_InitResult (*OpenTTD_SocialIntegration_v1_Init)(OpenTTD_SocialIntegration_v1_PluginApi *plugin_api, const OpenTTD_SocialIntegration_v1_OpenTTDInfo *openttd_info);
|
||||
|
||||
/**
|
||||
* Type of the GetInfo function the plugin is expected to export from its dynamic library.
|
||||
*
|
||||
* The plugin has to export the implementation of this function as "SocialIntegration_vN_GetInfo", where N is the API version this entry point is for.
|
||||
* A single plugin can have multiple versions implemented.
|
||||
*
|
||||
* @param[out] plugin_info Structure the plugin must fill with pointers. The plugin is owner of the memory.
|
||||
*/
|
||||
typedef void (*OpenTTD_SocialIntegration_v1_GetInfo)(OpenTTD_SocialIntegration_v1_PluginInfo *plugin_info);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* OPENTTD_SOCIAL_INTEGRATION_API_V1_H */
|
@ -0,0 +1,279 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file signature.cpp Implementation of signature validation routines. */
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
#include "signature.h"
|
||||
|
||||
#include "debug.h"
|
||||
#include "fileio_func.h"
|
||||
#include "string_func.h"
|
||||
|
||||
#include "3rdparty/monocypher/monocypher.h"
|
||||
#include "3rdparty/monocypher/monocypher-ed25519.h"
|
||||
#include "3rdparty/nlohmann/json.hpp"
|
||||
|
||||
#include "safeguards.h"
|
||||
|
||||
/** The public keys used for signature validation. */
|
||||
static const std::initializer_list<std::array<uint8_t, 32>> _public_keys_v1 = {
|
||||
/* 2024-01-20 - Public key for Social Integration Plugins. */
|
||||
{ 0xed, 0x5d, 0x57, 0x47, 0x21, 0x99, 0x8b, 0x02, 0xdf, 0x6e, 0x3d, 0x69, 0xe1, 0x87, 0xca, 0xd0, 0x0e, 0x88, 0xc3, 0xe2, 0xb2, 0xa6, 0x7b, 0xc0, 0x42, 0xc8, 0xd6, 0x4b, 0x65, 0xe6, 0x48, 0xf7 },
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate the 32-byte blake2b hash of a file.
|
||||
*
|
||||
* @param filename The filename to calculate the hash of.
|
||||
* @return The 32-byte blake2b hash of the file, hex-encoded.
|
||||
*/
|
||||
static std::string CalculateHashV1(const std::string &filename)
|
||||
{
|
||||
FILE *f = FioFOpenFile(filename, "rb", NO_DIRECTORY);
|
||||
if (f == nullptr) {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::array<uint8_t, 32> digest;
|
||||
crypto_blake2b_ctx ctx;
|
||||
crypto_blake2b_init(&ctx, digest.size());
|
||||
|
||||
while (!feof(f)) {
|
||||
std::array<uint8_t, 1024> buf;
|
||||
size_t len = fread(buf.data(), 1, buf.size(), f);
|
||||
|
||||
crypto_blake2b_update(&ctx, buf.data(), len);
|
||||
}
|
||||
fclose(f);
|
||||
|
||||
crypto_blake2b_final(&ctx, digest.data());
|
||||
return FormatArrayAsHex(digest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate whether the checksum of a file is the same.
|
||||
*
|
||||
* @param filename The filename to validate the checksum of.
|
||||
* @param checksum The expected checksum.
|
||||
* @return True iff the checksum of the file is the same as the expected checksum.
|
||||
*/
|
||||
static bool ValidateChecksum(const std::string &filename, const std::string &checksum)
|
||||
{
|
||||
/* Checksums are "<version>$<hash>". Split out the version. */
|
||||
auto pos = checksum.find('$');
|
||||
assert(pos != std::string::npos); // Already validated by ValidateSchema().
|
||||
const std::string version = checksum.substr(0, pos);
|
||||
const std::string hash = checksum.substr(pos + 1);
|
||||
|
||||
/* Calculate the checksum over the file. */
|
||||
std::string calculated_hash;
|
||||
if (version == "1") {
|
||||
calculated_hash = CalculateHashV1(filename);
|
||||
} else {
|
||||
Debug(misc, 0, "Failed to validate signature: unknown checksum version: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Validate the checksum is the same. */
|
||||
if (calculated_hash.empty()) {
|
||||
Debug(misc, 0, "Failed to validate signature: couldn't calculate checksum for: {}", filename);
|
||||
return false;
|
||||
}
|
||||
if (calculated_hash != hash) {
|
||||
Debug(misc, 0, "Failed to validate signature: checksum mismatch for: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate whether the signature is valid for this set of files.
|
||||
*
|
||||
* @param signature The signature to validate.
|
||||
* @param files The files to validate the signature against.
|
||||
* @param filename The filename of the signatures file (for error-reporting).
|
||||
* @return True iff the signature is valid for this set of files.
|
||||
*/
|
||||
static bool ValidateSignature(const std::string &signature, const nlohmann::json &files, const std::string &filename)
|
||||
{
|
||||
/* Signatures are "<version>$<signature>". Split out the version. */
|
||||
auto pos = signature.find('$');
|
||||
assert(pos != std::string::npos); // Already validated by ValidateSchema().
|
||||
const std::string version = signature.substr(0, pos);
|
||||
const std::string sig_value = signature.substr(pos + 1);
|
||||
|
||||
/* Create the message we are going to validate. */
|
||||
std::string message = files.dump(-1);
|
||||
|
||||
/* Validate the signature. */
|
||||
if (version == "1") {
|
||||
std::array<uint8_t, 64> sig;
|
||||
if (sig_value.size() != 128 || !ConvertHexToBytes(sig_value, sig)) {
|
||||
Debug(misc, 0, "Failed to validate signature: invalid signature: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto &pk_value : _public_keys_v1) {
|
||||
/* Check if the message is valid with this public key. */
|
||||
auto res = crypto_ed25519_check(sig.data(), pk_value.data(), reinterpret_cast<uint8_t *>(message.data()), message.size());
|
||||
if (res == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Debug(misc, 0, "Failed to validate signature: signature validation failed: {}", filename);
|
||||
return false;
|
||||
} else {
|
||||
Debug(misc, 0, "Failed to validate signature: unknown signature version: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the signatures file complies with the JSON schema.
|
||||
*
|
||||
* @param signatures The signatures JSON to validate.
|
||||
* @param filename The filename of the signatures file (for error-reporting).
|
||||
* @return True iff the signatures file complies with the JSON schema.
|
||||
*/
|
||||
static bool ValidateSchema(const nlohmann::json &signatures, const std::string &filename)
|
||||
{
|
||||
if (signatures["files"].is_null()) {
|
||||
Debug(misc, 0, "Failed to validate signature: no files found: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (signatures["signature"].is_null()) {
|
||||
Debug(misc, 0, "Failed to validate signature: no signature found: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto &signature : signatures["files"]) {
|
||||
if (signature["filename"].is_null() || signature["checksum"].is_null()) {
|
||||
Debug(misc, 0, "Failed to validate signature: invalid entry in files: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string sig_filename = signature["filename"];
|
||||
const std::string sig_checksum = signature["checksum"];
|
||||
|
||||
if (sig_filename.empty() || sig_checksum.empty()) {
|
||||
Debug(misc, 0, "Failed to validate signature: invalid entry in files: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto pos = sig_checksum.find('$');
|
||||
if (pos == std::string::npos) {
|
||||
Debug(misc, 0, "Failed to validate signature: invalid checksum format: {}", filename);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const std::string signature = signatures["signature"];
|
||||
auto pos = signature.find('$');
|
||||
if (pos == std::string::npos) {
|
||||
Debug(misc, 0, "Failed to validate signature: invalid signature format: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the signatures mentioned in the signature file are matching
|
||||
* the files in question.
|
||||
*
|
||||
* @return True iff the files in the signature file passed validation.
|
||||
*/
|
||||
static bool _ValidateSignatureFile(const std::string &filename)
|
||||
{
|
||||
size_t filesize;
|
||||
FILE *f = FioFOpenFile(filename, "rb", NO_DIRECTORY, &filesize);
|
||||
if (f == nullptr) {
|
||||
Debug(misc, 0, "Failed to validate signature: file not found: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string text(filesize, '\0');
|
||||
size_t len = fread(text.data(), filesize, 1, f);
|
||||
if (len != 1) {
|
||||
Debug(misc, 0, "Failed to validate signature: failed to read file: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
nlohmann::json signatures;
|
||||
try {
|
||||
signatures = nlohmann::json::parse(text);
|
||||
} catch (nlohmann::json::exception &) {
|
||||
Debug(misc, 0, "Failed to validate signature: not a valid JSON file: {}", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* The JSON file should look like:
|
||||
*
|
||||
* {
|
||||
* "files": [
|
||||
* {
|
||||
* "checksum": "version$hash"
|
||||
* "filename": "filename",
|
||||
* },
|
||||
* ...
|
||||
* ],
|
||||
* "signature": "version$signature"
|
||||
* }
|
||||
*
|
||||
* The signature is a signed message of the content of "files", dumped as
|
||||
* JSON without spaces / newlines, keys in the order as indicated above.
|
||||
*/
|
||||
|
||||
if (!ValidateSchema(signatures, filename)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ValidateSignature(signatures["signature"], signatures["files"], filename)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string dirname = std::filesystem::path(filename).parent_path().string();
|
||||
|
||||
for (auto &signature : signatures["files"]) {
|
||||
const std::string sig_filename = dirname + PATHSEPCHAR + signature["filename"].get<std::string>();
|
||||
const std::string sig_checksum = signature["checksum"];
|
||||
|
||||
if (!ValidateChecksum(sig_filename, sig_checksum)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the signatures mentioned in the signature file are matching
|
||||
* the files in question.
|
||||
*
|
||||
* @note if ALLOW_INVALID_SIGNATURE is defined, this function will always
|
||||
* return true (but will still report any errors in the console).
|
||||
*
|
||||
* @return True iff the files in the signature file passed validation.
|
||||
*/
|
||||
bool ValidateSignatureFile(const std::string &filename)
|
||||
{
|
||||
auto res = _ValidateSignatureFile(filename);;
|
||||
#if defined(ALLOW_INVALID_SIGNATURE)
|
||||
(void)res; // Ignore the result.
|
||||
return true;
|
||||
#else
|
||||
return res;
|
||||
#endif
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file signature.h Routines to validate signature files. */
|
||||
|
||||
#ifndef SIGNATURE_H
|
||||
#define SIGNATURE_H
|
||||
|
||||
bool ValidateSignatureFile(const std::string &filename);
|
||||
|
||||
#endif /* SIGNATURE_H */
|
@ -0,0 +1,246 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file social_integration.cpp Base implementation of social integration support. */
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
#include "social_integration.h"
|
||||
#include "3rdparty/openttd_social_integration_api/openttd_social_integration_api.h"
|
||||
|
||||
#include "debug.h"
|
||||
#include "fileio_func.h"
|
||||
#include "library_loader.h"
|
||||
#include "rev.h"
|
||||
#include "string_func.h"
|
||||
#include "signature.h"
|
||||
|
||||
#include "safeguards.h"
|
||||
|
||||
/**
|
||||
* Container to track information per plugin.
|
||||
*/
|
||||
class InternalSocialIntegrationPlugin {
|
||||
public:
|
||||
InternalSocialIntegrationPlugin(const std::string &filename, const std::string &basepath) : library(nullptr), external(basepath)
|
||||
{
|
||||
openttd_info.openttd_version = _openttd_revision;
|
||||
|
||||
if (!ValidateSignatureFile(fmt::format("{}.sig", filename))) {
|
||||
external.state = SocialIntegrationPlugin::INVALID_SIGNATURE;
|
||||
return;
|
||||
}
|
||||
|
||||
this->library = std::make_unique<LibraryLoader>(filename);
|
||||
}
|
||||
|
||||
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.
|
||||
|
||||
std::unique_ptr<LibraryLoader> library = nullptr; ///< Library handle.
|
||||
|
||||
SocialIntegrationPlugin external; ///< Information of the plugin to be used by other parts of our codebase.
|
||||
};
|
||||
|
||||
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 Platform twice.
|
||||
|
||||
/** Helper for scanning for files with SocialIntegration as extension */
|
||||
class SocialIntegrationFileScanner : FileScanner {
|
||||
public:
|
||||
void Scan()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
std::string extension = "-social.dll";
|
||||
#elif defined(__APPLE__)
|
||||
std::string extension = "-social.dylib";
|
||||
#else
|
||||
std::string extension = "-social.so";
|
||||
#endif
|
||||
|
||||
this->FileScanner::Scan(extension.c_str(), SOCIAL_INTEGRATION_DIR, false);
|
||||
}
|
||||
|
||||
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &) override
|
||||
{
|
||||
std::string basepath = filename.substr(basepath_length);
|
||||
Debug(misc, 1, "[Social Integration: {}] Loading ...", basepath);
|
||||
|
||||
auto &plugin = _plugins.emplace_back(std::make_unique<InternalSocialIntegrationPlugin>(filename, basepath));
|
||||
|
||||
/* Validation failed, so no library was loaded. */
|
||||
if (plugin->library == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (plugin->library->HasError()) {
|
||||
plugin->external.state = SocialIntegrationPlugin::FAILED;
|
||||
|
||||
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()) {
|
||||
plugin->external.state = SocialIntegrationPlugin::UNSUPPORTED_API;
|
||||
|
||||
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()) {
|
||||
plugin->external.state = SocialIntegrationPlugin::UNSUPPORTED_API;
|
||||
|
||||
Debug(misc, 0, "[Social Integration: {}] Failed to find symbol SocialPlugin_v1_Init: {}", basepath, plugin->library->GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
getinfo_func(&plugin->plugin_info);
|
||||
/* Setup the information for the outside world to see. */
|
||||
plugin->external.social_platform = plugin->plugin_info.social_platform;
|
||||
plugin->external.name = plugin->plugin_info.name;
|
||||
plugin->external.version = plugin->plugin_info.version;
|
||||
|
||||
/* Lowercase the string for comparison. */
|
||||
std::string lc_social_platform = plugin->plugin_info.social_platform;
|
||||
strtolower(lc_social_platform);
|
||||
|
||||
/* Prevent more than one plugin for a certain Social Platform to be loaded, as that never ends well. */
|
||||
if (_loaded_social_platform.find(lc_social_platform) != _loaded_social_platform.end()) {
|
||||
plugin->external.state = SocialIntegrationPlugin::DUPLICATE;
|
||||
|
||||
Debug(misc, 0, "[Social Integration: {}] Another plugin for {} is already loaded", basepath, plugin->plugin_info.social_platform);
|
||||
return false;
|
||||
}
|
||||
_loaded_social_platform.insert(lc_social_platform);
|
||||
|
||||
auto state = init_func(&plugin->plugin_api, &plugin->openttd_info);
|
||||
switch (state) {
|
||||
case OTTD_SOCIAL_INTEGRATION_V1_INIT_SUCCESS:
|
||||
plugin->external.state = SocialIntegrationPlugin::RUNNING;
|
||||
|
||||
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:
|
||||
plugin->external.state = SocialIntegrationPlugin::FAILED;
|
||||
|
||||
Debug(misc, 0, "[Social Integration: {}] Failed to initialize", basepath);
|
||||
return false;
|
||||
|
||||
case OTTD_SOCIAL_INTEGRATION_V1_INIT_PLATFORM_NOT_RUNNING:
|
||||
plugin->external.state = SocialIntegrationPlugin::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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<SocialIntegrationPlugin *> SocialIntegration::GetPlugins()
|
||||
{
|
||||
std::vector<SocialIntegrationPlugin *> plugins;
|
||||
|
||||
for (auto &plugin : _plugins) {
|
||||
plugins.push_back(&plugin->external);
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
void SocialIntegration::Initialize()
|
||||
{
|
||||
SocialIntegrationFileScanner fs;
|
||||
fs.Scan();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper to call a function pointer of a plugin if it isn't a nullptr.
|
||||
*
|
||||
* @param plugin Plugin to call the function pointer on.
|
||||
* @param func Function pointer to call.
|
||||
*/
|
||||
template <typename T, typename... Ts>
|
||||
static void PluginCall(std::unique_ptr<InternalSocialIntegrationPlugin> &plugin, T func, Ts... args)
|
||||
{
|
||||
if (plugin->external.state != SocialIntegrationPlugin::RUNNING) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (func != nullptr) {
|
||||
func(args...);
|
||||
}
|
||||
}
|
||||
|
||||
void SocialIntegration::Shutdown()
|
||||
{
|
||||
for (auto &plugin : _plugins) {
|
||||
PluginCall(plugin, plugin->plugin_api.shutdown);
|
||||
}
|
||||
|
||||
_plugins.clear();
|
||||
_loaded_social_platform.clear();
|
||||
}
|
||||
|
||||
void SocialIntegration::RunCallbacks()
|
||||
{
|
||||
for (auto &plugin : _plugins) {
|
||||
if (plugin->external.state != SocialIntegrationPlugin::RUNNING) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (plugin->plugin_api.run_callbacks != nullptr) {
|
||||
if (!plugin->plugin_api.run_callbacks()) {
|
||||
Debug(misc, 1, "[Social Plugin: {}] Requested to be unloaded", plugin->external.basepath);
|
||||
|
||||
_loaded_social_platform.erase(plugin->plugin_info.social_platform);
|
||||
plugin->external.state = SocialIntegrationPlugin::UNLOADED;
|
||||
PluginCall(plugin, plugin->plugin_api.shutdown);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SocialIntegration::EventEnterMainMenu()
|
||||
{
|
||||
for (auto &plugin : _plugins) {
|
||||
PluginCall(plugin, plugin->plugin_api.event_enter_main_menu);
|
||||
}
|
||||
}
|
||||
|
||||
void SocialIntegration::EventEnterScenarioEditor(uint map_width, uint map_height)
|
||||
{
|
||||
for (auto &plugin : _plugins) {
|
||||
PluginCall(plugin, plugin->plugin_api.event_enter_scenario_editor, map_width, map_height);
|
||||
}
|
||||
}
|
||||
|
||||
void SocialIntegration::EventEnterSingleplayer(uint map_width, uint map_height)
|
||||
{
|
||||
for (auto &plugin : _plugins) {
|
||||
PluginCall(plugin, plugin->plugin_api.event_enter_singleplayer, map_width, map_height);
|
||||
}
|
||||
}
|
||||
|
||||
void SocialIntegration::EventEnterMultiplayer(uint map_width, uint map_height)
|
||||
{
|
||||
for (auto &plugin : _plugins) {
|
||||
PluginCall(plugin, plugin->plugin_api.event_enter_multiplayer, map_width, map_height);
|
||||
}
|
||||
}
|
||||
|
||||
void SocialIntegration::EventJoiningMultiplayer()
|
||||
{
|
||||
for (auto &plugin : _plugins) {
|
||||
PluginCall(plugin, plugin->plugin_api.event_joining_multiplayer);
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file social_integration.h Interface definitions for game to report/respond to social integration. */
|
||||
|
||||
#ifndef SOCIAL_INTEGRATION_H
|
||||
#define SOCIAL_INTEGRATION_H
|
||||
|
||||
class SocialIntegrationPlugin {
|
||||
public:
|
||||
enum State {
|
||||
RUNNING, ///< The plugin is successfully loaded and running.
|
||||
|
||||
FAILED, ///< The plugin failed to initialize.
|
||||
PLATFORM_NOT_RUNNING, ///< The plugin failed to initialize because the Social Platform is not running.
|
||||
UNLOADED, ///< The plugin is unloaded upon request.
|
||||
DUPLICATE, ///< Another plugin of the same Social Platform is already loaded.
|
||||
UNSUPPORTED_API, ///< The plugin does not support the current API version.
|
||||
INVALID_SIGNATURE, ///< The signature of the plugin is invalid.
|
||||
};
|
||||
|
||||
std::string basepath; ///< Base path of the plugin.
|
||||
|
||||
std::string social_platform = "unknown"; ///< Social platform this plugin is for.
|
||||
std::string name = ""; ///< Name of the plugin.
|
||||
std::string version = ""; ///< Version of the plugin.
|
||||
|
||||
State state = FAILED; ///< Result of the plugin's init function.
|
||||
|
||||
SocialIntegrationPlugin(const std::string &basepath) : basepath(basepath) {}
|
||||
};
|
||||
|
||||
class SocialIntegration {
|
||||
public:
|
||||
/**
|
||||
* Get the list of loaded social integration plugins.
|
||||
*/
|
||||
static std::vector<SocialIntegrationPlugin *> GetPlugins();
|
||||
|
||||
/**
|
||||
* Initialize the social integration system, loading any social integration plugins that are available.
|
||||
*/
|
||||
static void Initialize();
|
||||
|
||||
/**
|
||||
* Shutdown the social integration system, and all social integration plugins that are loaded.
|
||||
*/
|
||||
static void Shutdown();
|
||||
|
||||
/**
|
||||
* Allow any social integration library to handle their own events.
|
||||
*/
|
||||
static void RunCallbacks();
|
||||
|
||||
/**
|
||||
* Event: user entered the main menu.
|
||||
*/
|
||||
static void EventEnterMainMenu();
|
||||
|
||||
/**
|
||||
* Event: user entered the Scenario Editor.
|
||||
*/
|
||||
static void EventEnterScenarioEditor(uint map_width, uint map_height);
|
||||
|
||||
/**
|
||||
* Event: user entered a singleplayer game.
|
||||
*/
|
||||
static void EventEnterSingleplayer(uint map_width, uint map_height);
|
||||
|
||||
/**
|
||||
* Event: user entered a multiplayer game.
|
||||
*/
|
||||
static void EventEnterMultiplayer(uint map_width, uint map_height);
|
||||
|
||||
/**
|
||||
* Event: user is joining a multiplayer game.
|
||||
*/
|
||||
static void EventJoiningMultiplayer();
|
||||
};
|
||||
|
||||
#endif /* SOCIAL_INTEGRATION_H */
|
Loading…
Reference in New Issue