| @@ -0,0 +1,117 @@ | ||
| /* $Id$ */ | ||
|
|
||
| /* | ||
| * 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_presence.cpp Base implementation of social presence support. */ | ||
|
|
||
| #include "../stdafx.h" | ||
| #include <string> | ||
| #include "social_presence.h" | ||
| #include "social_plugin_api.h" | ||
|
|
||
|
|
||
| static bool _social_loaded = false; | ||
| static OpenTTD_SocialPluginApi _social_api{}; | ||
| static OpenTTD_SocialPluginCallbacks _social_callbacks{}; | ||
|
|
||
| static struct { | ||
| std::string server_name; | ||
| std::string server_cookie; | ||
| } _social_multiplayer_status; | ||
|
|
||
| /* Implemented by platform */ | ||
| extern OpenTTD_SocialPluginInit SocialLoadPlugin(); | ||
|
|
||
|
|
||
| static void Callback_handle_join_request(void *join_request_cookie, const char *friend_name) | ||
| { | ||
| SocialHandleJoinRequest(join_request_cookie, friend_name); | ||
| } | ||
|
|
||
| static void Callback_cancel_join_request(void *join_request_cookie) | ||
| { | ||
| SocialCancelJoinRequest(join_request_cookie); | ||
| } | ||
|
|
||
| static void Callback_join_requested_game(const char *server_cookie) | ||
| { | ||
| SocialJoinRequestedGame(server_cookie); | ||
| } | ||
|
|
||
|
|
||
| void SocialStartup() | ||
| { | ||
| if (_social_loaded) return; | ||
|
|
||
| OpenTTD_SocialPluginInit init = SocialLoadPlugin(); | ||
|
|
||
| _social_callbacks.handle_join_request = Callback_handle_join_request; | ||
| _social_callbacks.cancel_join_request = Callback_cancel_join_request; | ||
| _social_callbacks.join_requested_game = Callback_join_requested_game; | ||
|
|
||
| if (init != nullptr) { | ||
| _social_loaded = init(OTTD_SOCIAL_PLUGIN_API_VERSION, &_social_api, &_social_callbacks) != 0; | ||
| } | ||
| } | ||
|
|
||
| void SocialShutdown() | ||
| { | ||
| if (_social_loaded) _social_api.shutdown(); | ||
| _social_loaded = false; | ||
| } | ||
|
|
||
| void SocialEventLoop() | ||
| { | ||
| if (_social_loaded) _social_api.event_loop(); | ||
| } | ||
|
|
||
| void SocialEnterSingleplayer() | ||
| { | ||
| if (_social_loaded) _social_api.enter_singleplayer(); | ||
| } | ||
|
|
||
| void SocialBeginEnterMultiplayer(const std::string &server_name, const std::string &server_cookie) | ||
| { | ||
| _social_multiplayer_status.server_name = server_name; | ||
| _social_multiplayer_status.server_cookie = server_cookie; | ||
| } | ||
|
|
||
| void SocialCompleteEnterMultiplayer() | ||
| { | ||
| if (_social_loaded && !_social_multiplayer_status.server_cookie.empty()) { | ||
| _social_api.enter_multiplayer(_social_multiplayer_status.server_name.c_str(), _social_multiplayer_status.server_cookie.c_str()); | ||
| } | ||
| } | ||
|
|
||
| void SocialEnterCompany(const std::string &company_name, CompanyID company_id) | ||
| { | ||
| if (_social_loaded) _social_api.enter_company(company_name.c_str(), company_id); | ||
| } | ||
|
|
||
| void SocialEnterSpectate() | ||
| { | ||
| if (_social_loaded) _social_api.enter_spectate(); | ||
| } | ||
|
|
||
| void SocialExitGameplay() | ||
| { | ||
| if (_social_loaded) _social_api.exit_gameplay(); | ||
| } | ||
|
|
||
| void SocialRespondJoinRequest(void *join_request_cookie, SocialJoinRequestResponse response) | ||
| { | ||
| OpenTTD_SocialPluginApi_JoinRequestResponse api_rsp; | ||
| switch (response) { | ||
| case SocialJoinRequestResponse::Ignore: api_rsp = OTTD_JRR_IGNORE; break; | ||
| case SocialJoinRequestResponse::Accept: api_rsp = OTTD_JRR_ACCEPT; break; | ||
| case SocialJoinRequestResponse::Reject: api_rsp = OTTD_JRR_REJECT; break; | ||
| default: return; | ||
| } | ||
|
|
||
| if (_social_loaded) _social_api.respond_join_request(join_request_cookie, api_rsp); | ||
| } |
| @@ -0,0 +1,57 @@ | ||
| /* $Id$ */ | ||
|
|
||
| /* | ||
| * 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_presence.h Interface definitions for game to report/respond to social media presence. */ | ||
|
|
||
| #ifndef NETWORK_SOCIAL_PRESENCE_H | ||
| #define NETWORK_SOCIAL_PRESENCE_H | ||
|
|
||
| #include "../company_type.h" | ||
| #include <string> | ||
|
|
||
| enum class SocialJoinRequestResponse { | ||
| Accept, | ||
| Reject, | ||
| Ignore, | ||
| }; | ||
|
|
||
| /* Functions implemented by social plug-in */ | ||
|
|
||
| /** Main loop calls this to detect and initialise social plug-in */ | ||
| void SocialStartup(); | ||
| /** Main loop calls this to shut down social plug-in */ | ||
| void SocialShutdown(); | ||
| /** Main loop calls this, let social plug-in handle events */ | ||
| void SocialEventLoop(); | ||
| /** Game calls this when player starts/loads a singleplayer game */ | ||
| void SocialEnterSingleplayer(); | ||
| /** GUI calls this when player joins/starts a multiplayer */ | ||
| void SocialBeginEnterMultiplayer(const std::string &server_name, const std::string &server_cookie); | ||
| /** Network code calls this when joining/starting a multiplayer game completes */ | ||
| void SocialCompleteEnterMultiplayer(); | ||
| /** Game calls this when player joins a company, or player's company changes name */ | ||
| void SocialEnterCompany(const std::string &company_name, CompanyID company_id); | ||
| /** Game calls this when player enters spectate-mode */ | ||
| void SocialEnterSpectate(); | ||
| /** Game calls this when player leaves main gameplay mode */ | ||
| void SocialExitGameplay(); | ||
| /** Game calls this when player accepts a remote join request */ | ||
| void SocialRespondJoinRequest(void *join_request_cookie, SocialJoinRequestResponse response); | ||
|
|
||
|
|
||
| /* Functions called from social plug-in on certain events */ | ||
|
|
||
| /** Social plug-in calls this (from inside SocialEventLoop) if it receives a join request from a friend */ | ||
| void SocialHandleJoinRequest(void *join_request_cookie, const std::string &friend_name); | ||
| /** Social plug-in calls this if a friend retracts a join request */ | ||
| void SocialCancelJoinRequest(void *join_request_cookie); | ||
| /** Social plug-in calls this if the user received an accept on a join request */ | ||
| void SocialJoinRequestedGame(const std::string &server_cookie); | ||
|
|
||
| #endif /* NETWORK_SOCIAL_PRESENCE_H */ |