1
0
Fork 0

Compare commits

...

2 Commits

Author SHA1 Message Date
yeah-its-gloria 0a035d48f3
Linux loader attempt 2023-10-24 05:36:29 +02:00
yeah-its-gloria 0bf94569e3
Adjust existing code to review before proceeding 2023-10-24 04:35:37 +02:00
9 changed files with 119 additions and 46 deletions

2
.gitignore vendored
View File

@ -1,6 +1,4 @@
/.cache
/.vs /.vs
/.vscode
/build* /build*
CMakeSettings.json CMakeSettings.json
docs/aidocs/* docs/aidocs/*

View File

@ -846,7 +846,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet
SetLocalCompany(_network_join.company); SetLocalCompany(_network_join.company);
} }
JoinData data = { OTTD_Social_Event_Server_Joined_Data data = {
"Unknown", // TODO: fix this "Unknown", // TODO: fix this
this->connection_string.c_str() this->connection_string.c_str()
}; };

View File

@ -195,7 +195,7 @@ bool ClientNetworkCoordinatorSocketHandler::Receive_GC_REGISTER_ACK(Packet *p)
* attempt to re-use when registering again. */ * attempt to re-use when registering again. */
_network_server_invite_code = _settings_client.network.server_invite_code; _network_server_invite_code = _settings_client.network.server_invite_code;
JoinData data = { OTTD_Social_Event_Server_Joined_Data data = {
_settings_client.network.server_name.c_str(), _settings_client.network.server_name.c_str(),
_network_server_invite_code.c_str() _network_server_invite_code.c_str()
}; };

View File

@ -5,7 +5,7 @@
* 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/>. * 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 network_chat_gui.cpp GUI for handling chat messages. */ /** @file loader.cpp Loader code for social plugins. */
#include "../../stdafx.h" #include "../../stdafx.h"
#include "../network_func.h" #include "../network_func.h"
@ -16,7 +16,9 @@ void callback(const char* serverName) {
} }
SocialPlatformLoader::SocialPlatformLoader() { SocialPlatformLoader::SocialPlatformLoader() {
#if !defined(__EMSCRIPTEN__)
LoadSocialPlatforms(this->plugins); LoadSocialPlatforms(this->plugins);
#endif
for (SocialPlatformPlugin plugin : plugins) { for (SocialPlatformPlugin plugin : plugins) {
plugin.initialize(callback, &plugin.userdata); plugin.initialize(callback, &plugin.userdata);
@ -35,7 +37,7 @@ void SocialPlatformLoader::RunDispatch() {
} }
} }
void SocialPlatformLoader::NewState(eventCode event, void* parameter) { void SocialPlatformLoader::NewState(OTTD_Social_Event event, void* parameter) {
for (SocialPlatformPlugin plugin : plugins) { for (SocialPlatformPlugin plugin : plugins) {
plugin.newState(event, parameter, plugin.userdata); plugin.newState(event, parameter, plugin.userdata);
} }

View File

@ -5,12 +5,11 @@
* 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/>. * 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 network_client.h Client part of the network protocol. */ /** @file loader.h Social plugin loader class. */
#ifndef NETWORK_SOCIAL_LOADER_H #ifndef NETWORK_SOCIAL_LOADER_H
#define NETWORK_SOCIAL_LOADER_H #define NETWORK_SOCIAL_LOADER_H
#include "../../core/alloc_type.hpp"
#include "social_api.h" #include "social_api.h"
struct SocialPlatformPlugin { struct SocialPlatformPlugin {
@ -24,11 +23,11 @@ struct SocialPlatformPlugin {
void* userdata; void* userdata;
}; };
class SocialPlatformLoader : public ZeroedMemoryAllocator { class SocialPlatformLoader {
public: public:
void Shutdown(); void Shutdown();
void RunDispatch(); void RunDispatch();
void NewState(eventCode event, void* parameter); void NewState(OTTD_Social_Event event, void* parameter);
static SocialPlatformLoader* GetInstance(); static SocialPlatformLoader* GetInstance();
@ -38,7 +37,9 @@ private:
std::vector<SocialPlatformPlugin> plugins; std::vector<SocialPlatformPlugin> plugins;
}; };
#if !defined(__EMSCRIPTEN__)
/* Defined in os/<os>/social_<os>.cpp. */ /* Defined in os/<os>/social_<os>.cpp. */
void LoadSocialPlatforms(std::vector<SocialPlatformPlugin>& plugins); void LoadSocialPlatforms(std::vector<SocialPlatformPlugin>& plugins);
#endif
#endif #endif

View File

@ -14,7 +14,7 @@
extern "C" { extern "C" {
#endif #endif
struct JoinData { struct OTTD_Social_Event_Server_Joined_Data {
// Name of the server as shown to players. // Name of the server as shown to players.
const char* server_name; const char* server_name;
@ -22,7 +22,7 @@ struct JoinData {
const char* connection_string; const char* connection_string;
}; };
enum eventCode { enum OTTD_Social_Event {
// Called when the player has entered the main menu. // Called when the player has entered the main menu.
// Parameter: N/A // Parameter: N/A
OTTD_SOCIAL_EVENT_MENU, OTTD_SOCIAL_EVENT_MENU,
@ -46,7 +46,9 @@ typedef void (* OTTD_Social_JoinCallback)(const char* serverName);
// Initializes the plugin. // Initializes the plugin.
// The plugin is free to initialize the memory pointed to at the given address with any structure it needs to keep data around. // The plugin is free to initialize the memory pointed to at the given address with any structure it needs to keep data around.
// The plugin loader will keep track of this memory for the plugin. // The plugin loader will keep track of this memory for the plugin. It remains valid until the shutdown function is called.
//
// The callback function is a static reference to a function, however, the plugin should leverage its user data to keep track of it.
typedef bool (* OTTD_Social_Initialize)(OTTD_Social_JoinCallback callback, void **userdata); typedef bool (* OTTD_Social_Initialize)(OTTD_Social_JoinCallback callback, void **userdata);
// Called by the plugin loader to indicate that OpenTTD is currently shutting down. // Called by the plugin loader to indicate that OpenTTD is currently shutting down.
@ -57,7 +59,9 @@ typedef void (* OTTD_Social_Shutdown)(void *userdata);
typedef void (* OTTD_Social_Dispatch)(void *userdata); typedef void (* OTTD_Social_Dispatch)(void *userdata);
// Called when the game's state changes. // Called when the game's state changes.
typedef void (* OTTD_Social_NewState)(eventCode event, void* parameter, void *userdata); //
// The data pointed to by parameter is only valid during the duration of this function.
typedef void (* OTTD_Social_NewState)(OTTD_Social_Event event, void* parameter, void *userdata);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -6,6 +6,7 @@ add_files(
add_files( add_files(
unix.cpp unix.cpp
social_unix.cpp
CONDITION UNIX CONDITION UNIX
) )

View File

@ -0,0 +1,65 @@
/*
* 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_unix.cpp Unix like backing implementation for social plugin loading. */
#include "../../stdafx.h"
#include "../../fileio_func.h"
#include "../../string_func.h"
#include "../../network/social/loader.h"
#include <dlfcn.h>
#include <dirent.h>
#include <stdio.h>
#include "../../safeguards.h"
void LoadSocialPlatforms(std::vector<SocialPlatformPlugin>& plugins) {
std::string search_dir = FioGetDirectory(SP_BINARY_DIR, BASE_DIR);
DIR* directory = opendir(OTTD2FS(search_dir).c_str());
if (directory == nullptr) {
return;
}
while (true) {
struct dirent* entry = readdir(directory);
if (entry == nullptr) {
break;
}
if (!StrEndsWith(FS2OTTD(entry->d_name), ".ots")) {
continue;
}
std::string library_path = search_dir + FS2OTTD(entry->d_name);
void* library = dlopen(library_path.c_str(), 0);
if (library == nullptr) {
continue;
}
SocialPlatformPlugin plugin = {
library,
(OTTD_Social_Initialize)dlsym(library, "OTTD_Social_Initialize"),
(OTTD_Social_Shutdown)dlsym(library, "OTTD_Social_Shutdown"),
(OTTD_Social_Dispatch)dlsym(library, "OTTD_Social_Dispatch"),
(OTTD_Social_NewState)dlsym(library, "OTTD_Social_NewState"),
nullptr
};
if (plugin.initialize == nullptr || plugin.shutdown == nullptr || plugin.dispatch == nullptr || plugin.newState == nullptr) {
dlclose(library);
continue;
}
plugins.push_back(plugin);
}
closedir(directory);
}

View File

@ -5,14 +5,12 @@
* 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/>. * 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 survey_win.cpp Windows implementation of OS-specific survey information. */ /** @file social_win.cpp Win32 backing implementation for social plugin loading. */
#include "../../stdafx.h" #include "../../stdafx.h"
#include "../../network/social/loader.h"
#include "../../fileio_func.h" #include "../../fileio_func.h"
#include "../../network/social/loader.h"
#include <thread>
#include <windows.h> #include <windows.h>
#include "../../safeguards.h" #include "../../safeguards.h"
@ -20,9 +18,12 @@
void LoadSocialPlatforms(std::vector<SocialPlatformPlugin>& plugins) { void LoadSocialPlatforms(std::vector<SocialPlatformPlugin>& plugins) {
std::string search_dir = FioGetDirectory(SP_BINARY_DIR, BASE_DIR); std::string search_dir = FioGetDirectory(SP_BINARY_DIR, BASE_DIR);
WIN32_FIND_DATAW find_data = {}; WIN32_FIND_DATAW find_data = { };
HANDLE find_handle = FindFirstFileW(OTTD2FS(search_dir + "*.ots").c_str(), &find_data); HANDLE find_handle = FindFirstFileW(OTTD2FS(search_dir + "*.ots").c_str(), &find_data);
if (find_handle != INVALID_HANDLE_VALUE) { if (find_handle == INVALID_HANDLE_VALUE) {
return;
}
do { do {
std::string library_path = search_dir + FS2OTTD(find_data.cFileName); std::string library_path = search_dir + FS2OTTD(find_data.cFileName);
HMODULE library = LoadLibraryW(OTTD2FS(library_path).c_str()); HMODULE library = LoadLibraryW(OTTD2FS(library_path).c_str());
@ -43,11 +44,12 @@ void LoadSocialPlatforms(std::vector<SocialPlatformPlugin>& plugins) {
if (plugin.initialize == nullptr || plugin.shutdown == nullptr || plugin.dispatch == nullptr || plugin.newState == nullptr) { if (plugin.initialize == nullptr || plugin.shutdown == nullptr || plugin.dispatch == nullptr || plugin.newState == nullptr) {
FreeLibrary(library); FreeLibrary(library);
continue;
} }
plugins.push_back(plugin); plugins.push_back(plugin);
} while (FindNextFileW(find_handle, &find_data)); } while (FindNextFileW(find_handle, &find_data) == TRUE);
FindClose(find_handle); FindClose(find_handle);
}
} }