From 73fb132e414d0df4e6fbc1a158fda06aecf25971 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sun, 26 Mar 2017 22:30:23 +0100 Subject: [PATCH 01/21] Replace some SDL functions with our own --- src/openrct2/core/Diagnostics.cpp | 6 ++-- src/openrct2/game.c | 4 +-- src/openrct2/interface/chat.c | 5 +-- src/openrct2/network/NetworkConnection.cpp | 6 ++-- .../network/NetworkServerAdvertiser.cpp | 8 ++--- src/openrct2/network/NetworkTypes.h | 7 ++-- src/openrct2/network/network.cpp | 32 +++++++++---------- src/openrct2/network/twitch.cpp | 5 +-- src/openrct2/platform/crash.cpp | 3 +- src/openrct2/platform/platform.h | 1 + src/openrct2/platform/shared.c | 17 +++++++++- src/openrct2/rct2.c | 2 +- src/openrct2/scenario/scenario.c | 2 +- src/openrct2/util/sawyercoding.h | 1 - src/openrct2/util/util.c | 3 -- src/openrct2/world/mapgen.c | 3 +- 16 files changed, 60 insertions(+), 45 deletions(-) diff --git a/src/openrct2/core/Diagnostics.cpp b/src/openrct2/core/Diagnostics.cpp index 3f8cbcf87e..1d2c94941f 100644 --- a/src/openrct2/core/Diagnostics.cpp +++ b/src/openrct2/core/Diagnostics.cpp @@ -14,9 +14,7 @@ *****************************************************************************/ #pragma endregion -#include - -#if defined(DEBUG) && defined(__WINDOWS__) +#if defined(DEBUG) && defined(_WIN32) #define WIN32_LEAN_AND_MEAN #include #undef GetMessage @@ -29,7 +27,7 @@ namespace Debug void Break() { #if defined(DEBUG) -#if defined(__WINDOWS__) +#if defined(_WIN32) if (IsDebuggerPresent()) { DebugBreak(); diff --git a/src/openrct2/game.c b/src/openrct2/game.c index 582d3673a5..e12c5a83bc 100644 --- a/src/openrct2/game.c +++ b/src/openrct2/game.c @@ -427,7 +427,7 @@ void game_logic_update() // start autosave timer after update if (gLastAutoSaveUpdate == AUTOSAVE_PAUSE) - gLastAutoSaveUpdate = SDL_GetTicks(); + gLastAutoSaveUpdate = platform_get_ticks(); } /** @@ -585,7 +585,7 @@ sint32 game_do_command_p(sint32 command, sint32 *eax, sint32 *ebx, sint32 *ecx, // start autosave timer after game command if (gLastAutoSaveUpdate == AUTOSAVE_PAUSE) - gLastAutoSaveUpdate = SDL_GetTicks(); + gLastAutoSaveUpdate = platform_get_ticks(); return cost; } diff --git a/src/openrct2/interface/chat.c b/src/openrct2/interface/chat.c index 9cf2c2f7a6..2dfd7b021a 100644 --- a/src/openrct2/interface/chat.c +++ b/src/openrct2/interface/chat.c @@ -131,7 +131,8 @@ void chat_draw(rct_drawpixelinfo * dpi) // Draw chat history for (sint32 i = 0; i < CHAT_HISTORY_SIZE; i++, y -= stringHeight) { - if (!gChatOpen && SDL_TICKS_PASSED(SDL_GetTicks(), chat_history_get_time(i) + 10000)) { + uint32 expireTime = chat_history_get_time(i) + 10000; + if (!gChatOpen && platform_get_ticks() > expireTime) { break; } @@ -174,7 +175,7 @@ void chat_history_add(const char *src) sint32 index = _chatHistoryIndex % CHAT_HISTORY_SIZE; memset(_chatHistory[index], 0, CHAT_INPUT_SIZE); memcpy(_chatHistory[index], src, min(strlen(src), CHAT_INPUT_SIZE - 1)); - _chatHistoryTime[index] = SDL_GetTicks(); + _chatHistoryTime[index] = platform_get_ticks(); _chatHistoryIndex++; Mixer_Play_Effect(SOUND_NEWS_ITEM, 0, SDL_MIX_MAXVOLUME, 0, 1.5f, true); network_append_chat_log(src); diff --git a/src/openrct2/network/NetworkConnection.cpp b/src/openrct2/network/NetworkConnection.cpp index 75e8649ebf..54fa4904aa 100644 --- a/src/openrct2/network/NetworkConnection.cpp +++ b/src/openrct2/network/NetworkConnection.cpp @@ -85,7 +85,7 @@ sint32 NetworkConnection::ReadPacket() } if (InboundPacket.BytesTransferred == sizeof(InboundPacket.Size) + InboundPacket.Size) { - _lastPacketTime = SDL_GetTicks(); + _lastPacketTime = platform_get_ticks(); return NETWORK_READPACKET_SUCCESS; } } @@ -150,13 +150,13 @@ void NetworkConnection::SendQueuedPackets() void NetworkConnection::ResetLastPacketTime() { - _lastPacketTime = SDL_GetTicks(); + _lastPacketTime = platform_get_ticks(); } bool NetworkConnection::ReceivedPacketRecently() { #ifndef DEBUG - if (SDL_TICKS_PASSED(SDL_GetTicks(), _lastPacketTime + 7000)) + if (platform_get_ticks() > _lastPacketTime + 7000) { return false; } diff --git a/src/openrct2/network/NetworkServerAdvertiser.cpp b/src/openrct2/network/NetworkServerAdvertiser.cpp index 13b02df084..3fd3440121 100644 --- a/src/openrct2/network/NetworkServerAdvertiser.cpp +++ b/src/openrct2/network/NetworkServerAdvertiser.cpp @@ -77,13 +77,13 @@ public: { switch (_status) { case ADVERTISE_STATUS_UNREGISTERED: - if (_lastAdvertiseTime == 0 || SDL_TICKS_PASSED(SDL_GetTicks(), _lastAdvertiseTime + MASTER_SERVER_REGISTER_TIME)) + if (_lastAdvertiseTime == 0 || platform_get_ticks() > _lastAdvertiseTime + MASTER_SERVER_REGISTER_TIME) { SendRegistration(); } break; case ADVERTISE_STATUS_REGISTERED: - if (SDL_TICKS_PASSED(SDL_GetTicks(), _lastHeartbeatTime + MASTER_SERVER_HEARTBEAT_TIME)) + if (platform_get_ticks() > _lastHeartbeatTime + MASTER_SERVER_HEARTBEAT_TIME) { SendHeartbeat(); } @@ -97,7 +97,7 @@ public: private: void SendRegistration() { - _lastAdvertiseTime = SDL_GetTicks(); + _lastAdvertiseTime = platform_get_ticks(); // Send the registration request http_request_t request = { 0 }; @@ -139,7 +139,7 @@ private: request.root = jsonBody; request.type = HTTP_DATA_JSON; - _lastHeartbeatTime = SDL_GetTicks(); + _lastHeartbeatTime = platform_get_ticks(); http_request_async(&request, [](http_response_t *response) -> void { if (response == nullptr) diff --git a/src/openrct2/network/NetworkTypes.h b/src/openrct2/network/NetworkTypes.h index 5bfbacedaf..04e7d7b222 100644 --- a/src/openrct2/network/NetworkTypes.h +++ b/src/openrct2/network/NetworkTypes.h @@ -80,7 +80,7 @@ struct ByteSwapT<2> { static uint16 SwapBE(uint16 value) { - return SDL_SwapBE16(value); + return (uint16)((value << 8) | (value >> 8)); } }; @@ -89,7 +89,10 @@ struct ByteSwapT<4> { static uint32 SwapBE(uint32 value) { - return SDL_SwapBE32(value); + return (uint32)(((value << 24) | + ((value << 8) & 0x00FF0000) | + ((value >> 8) & 0x0000FF00) | + (value >> 24))); } }; diff --git a/src/openrct2/network/network.cpp b/src/openrct2/network/network.cpp index 6efd69232e..f84b6b89fa 100644 --- a/src/openrct2/network/network.cpp +++ b/src/openrct2/network/network.cpp @@ -14,8 +14,6 @@ *****************************************************************************/ #pragma endregion -#include - #include "../core/Guard.hpp" #include "../OpenRCT2.h" @@ -380,10 +378,12 @@ void Network::UpdateServer() it++; } } - if (SDL_TICKS_PASSED(SDL_GetTicks(), last_tick_sent_time + 25)) { + + uint32 ticks = platform_get_ticks(); + if (ticks > last_tick_sent_time + 25) { Server_Send_TICK(); } - if (SDL_TICKS_PASSED(SDL_GetTicks(), last_ping_sent_time + 3000)) { + if (ticks > last_ping_sent_time + 3000) { Server_Send_PING(); Server_Send_PINGLIST(); } @@ -427,7 +427,7 @@ void Network::UpdateClient() window_network_status_open(str_connecting, []() -> void { gNetwork.Close(); }); - server_connect_time = SDL_GetTicks(); + server_connect_time = platform_get_ticks(); } break; } @@ -1044,7 +1044,7 @@ void Network::Server_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx void Network::Server_Send_TICK() { - last_tick_sent_time = SDL_GetTicks(); + last_tick_sent_time = platform_get_ticks(); std::unique_ptr packet(NetworkPacket::Allocate()); *packet << (uint32)NETWORK_COMMAND_TICK << (uint32)gCurrentTicks << (uint32)gScenarioSrand0; uint32 flags = 0; @@ -1085,11 +1085,11 @@ void Network::Client_Send_PING() void Network::Server_Send_PING() { - last_ping_sent_time = SDL_GetTicks(); + last_ping_sent_time = platform_get_ticks(); std::unique_ptr packet(NetworkPacket::Allocate()); *packet << (uint32)NETWORK_COMMAND_PING; for (auto it = client_connection_list.begin(); it != client_connection_list.end(); it++) { - (*it)->PingTime = SDL_GetTicks(); + (*it)->PingTime = platform_get_ticks(); } SendPacketToClients(*packet, true); } @@ -1254,7 +1254,7 @@ void Network::ProcessGameCommandQueue() NetworkPlayer* player = GetPlayerByID(gc.playerid); if (player) { player->LastAction = NetworkActions::FindCommand(command); - player->LastActionTime = SDL_GetTicks(); + player->LastActionTime = platform_get_ticks(); player->AddMoneySpent(cost); } } @@ -1860,7 +1860,7 @@ void Network::Server_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket sint32 commandCommand = args[4]; - uint32 ticks = SDL_GetTicks(); //tick count is different by time last_action_time is set, keep same value. + uint32 ticks = platform_get_ticks(); //tick count is different by time last_action_time is set, keep same value. // Check if player's group permission allows command to run NetworkGroup* group = GetGroupByID(connection.Player->Group); @@ -1875,7 +1875,7 @@ void Network::Server_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket if (commandCommand == GAME_COMMAND_PLACE_SCENERY) { if ( ticks - connection.Player->LastPlaceSceneryTime < ACTION_COOLDOWN_TIME_PLACE_SCENERY && - // Incase SDL_GetTicks() wraps after ~49 days, ignore larger logged times. + // Incase platform_get_ticks() wraps after ~49 days, ignore larger logged times. ticks > connection.Player->LastPlaceSceneryTime ) { if (!(group->CanPerformCommand(MISC_COMMAND_TOGGLE_SCENERY_CLUSTER))) { @@ -1889,7 +1889,7 @@ void Network::Server_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket else if (commandCommand == GAME_COMMAND_DEMOLISH_RIDE) { if ( ticks - connection.Player->LastDemolishRideTime < ACTION_COOLDOWN_TIME_DEMOLISH_RIDE && - // Incase SDL_GetTicks() wraps after ~49 days, ignore larger logged times. + // Incase platform_get_ticks() wraps after ~49 days, ignore larger logged times. ticks > connection.Player->LastDemolishRideTime ) { Server_Send_SHOWERROR(connection, STR_CANT_DO_THIS, STR_NETWORK_ACTION_RATE_LIMIT_MESSAGE); @@ -1912,7 +1912,7 @@ void Network::Server_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket } connection.Player->LastAction = NetworkActions::FindCommand(commandCommand); - connection.Player->LastActionTime = SDL_GetTicks(); + connection.Player->LastActionTime = platform_get_ticks(); connection.Player->AddMoneySpent(cost); if (commandCommand == GAME_COMMAND_PLACE_SCENERY) { @@ -1986,7 +1986,7 @@ void Network::Client_Handle_PING(NetworkConnection& connection, NetworkPacket& p void Network::Server_Handle_PING(NetworkConnection& connection, NetworkPacket& packet) { - sint32 ping = SDL_GetTicks() - connection.PingTime; + sint32 ping = platform_get_ticks() - connection.PingTime; if (ping < 0) { ping = 0; } @@ -2204,7 +2204,7 @@ void network_add_player_money_spent(uint32 index, money32 cost) sint32 network_get_player_last_action(uint32 index, sint32 time) { - if (time && SDL_TICKS_PASSED(SDL_GetTicks(), gNetwork.player_list[index]->LastActionTime + time)) { + if (time && platform_get_ticks() > gNetwork.player_list[index]->LastActionTime + time) { return -999; } return gNetwork.player_list[index]->LastAction; @@ -2213,7 +2213,7 @@ sint32 network_get_player_last_action(uint32 index, sint32 time) void network_set_player_last_action(uint32 index, sint32 command) { gNetwork.player_list[index]->LastAction = NetworkActions::FindCommand(command); - gNetwork.player_list[index]->LastActionTime = SDL_GetTicks(); + gNetwork.player_list[index]->LastActionTime = platform_get_ticks(); } rct_xyz16 network_get_player_last_action_coord(uint32 index) diff --git a/src/openrct2/network/twitch.cpp b/src/openrct2/network/twitch.cpp index b1ac609c00..d6259dd74d 100644 --- a/src/openrct2/network/twitch.cpp +++ b/src/openrct2/network/twitch.cpp @@ -43,6 +43,7 @@ extern "C" #include "../localisation/localisation.h" #include "../management/news_item.h" #include "../peep/peep.h" + #include "../platform/platform.h" #include "../rct2.h" #include "../util/util.h" #include "../world/sprite.h" @@ -142,7 +143,7 @@ namespace Twitch switch (_twitchState) { case TWITCH_STATE_LEFT: { - uint32 currentTime = SDL_GetTicks(); + uint32 currentTime = platform_get_ticks(); uint32 timeSinceLastPulse = currentTime - _twitchLastPulseTick; if (_twitchLastPulseTick == 0 || timeSinceLastPulse > PulseTime) { @@ -153,7 +154,7 @@ namespace Twitch } case TWITCH_STATE_JOINED: { - uint32 currentTime = SDL_GetTicks(); + uint32 currentTime = platform_get_ticks(); uint32 timeSinceLastPulse = currentTime - _twitchLastPulseTick; if (_twitchLastPulseTick == 0 || timeSinceLastPulse > PulseTime) { _twitchLastPulseTick = currentTime; diff --git a/src/openrct2/platform/crash.cpp b/src/openrct2/platform/crash.cpp index f9960e3dd9..0a8c343f32 100644 --- a/src/openrct2/platform/crash.cpp +++ b/src/openrct2/platform/crash.cpp @@ -14,14 +14,13 @@ *****************************************************************************/ #pragma endregion -#include #include "crash.h" #ifdef USE_BREAKPAD #include #include -#if defined(__WINDOWS__) +#if defined(_WIN32) #include #include #include diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index 87843a2549..ac93a317c5 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -188,6 +188,7 @@ void platform_get_cursor_position(sint32 *x, sint32 *y); void platform_get_cursor_position_scaled(sint32 *x, sint32 *y); void platform_set_cursor_position(sint32 x, sint32 y); uint32 platform_get_ticks(); +void platform_sleep(uint32 ms); void platform_resolve_user_data_path(); void platform_resolve_openrct_data_path(); void platform_get_openrct_data_path(utf8 *outPath, size_t outSize); diff --git a/src/openrct2/platform/shared.c b/src/openrct2/platform/shared.c index cb03671919..e3115f5a8d 100644 --- a/src/openrct2/platform/shared.c +++ b/src/openrct2/platform/shared.c @@ -14,6 +14,7 @@ *****************************************************************************/ #pragma endregion +#include #include "../audio/audio.h" #include "../audio/AudioMixer.h" #include "../config/Config.h" @@ -761,7 +762,21 @@ void platform_set_cursor_position(sint32 x, sint32 y) uint32 platform_get_ticks() { - return SDL_GetTicks(); +#ifdef _WIN32 + return GetTickCount(); +#else + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { + log_fatal("clock_gettime failed"); + exit(-1); + } + return (uint32)(ts.tv_nsec / 1000000); +#endif +} + +void platform_sleep(uint32 ms) +{ + SDL_Delay(ms); } uint8 platform_get_currency_value(const char *currCode) { diff --git a/src/openrct2/rct2.c b/src/openrct2/rct2.c index ff5761e6f8..b24d470d6f 100644 --- a/src/openrct2/rct2.c +++ b/src/openrct2/rct2.c @@ -380,7 +380,7 @@ sint32 check_file_paths() void rct2_update() { - sint32 tickCount = SDL_GetTicks(); + sint32 tickCount = platform_get_ticks(); gTicksSinceLastUpdate = min(tickCount - gLastTickCount, 500); gLastTickCount = tickCount; if (game_is_not_paused()) { diff --git a/src/openrct2/scenario/scenario.c b/src/openrct2/scenario/scenario.c index 5910e84e68..946140c4fe 100644 --- a/src/openrct2/scenario/scenario.c +++ b/src/openrct2/scenario/scenario.c @@ -362,7 +362,7 @@ void scenario_autosave_check() if (gLastAutoSaveUpdate == AUTOSAVE_PAUSE) return; // Milliseconds since last save - uint32 timeSinceSave = SDL_GetTicks() - gLastAutoSaveUpdate; + uint32 timeSinceSave = platform_get_ticks() - gLastAutoSaveUpdate; bool shouldSave = false; switch (gConfigGeneral.autosave_frequency) { diff --git a/src/openrct2/util/sawyercoding.h b/src/openrct2/util/sawyercoding.h index 73571111fc..f289154387 100644 --- a/src/openrct2/util/sawyercoding.h +++ b/src/openrct2/util/sawyercoding.h @@ -17,7 +17,6 @@ #ifndef _SAWYERCODING_H_ #define _SAWYERCODING_H_ -#include #include "../common.h" #pragma pack(push, 1) diff --git a/src/openrct2/util/util.c b/src/openrct2/util/util.c index 3ab73ff708..a7a4433e83 100644 --- a/src/openrct2/util/util.c +++ b/src/openrct2/util/util.c @@ -14,10 +14,7 @@ *****************************************************************************/ #pragma endregion -// Include common.h before SDL, otherwise M_PI gets redefined #include "../common.h" - -#include #include "../core/Guard.hpp" #include "../localisation/localisation.h" #include "../platform/platform.h" diff --git a/src/openrct2/world/mapgen.c b/src/openrct2/world/mapgen.c index 1aa8f6e1c9..d61fd1f3a5 100644 --- a/src/openrct2/world/mapgen.c +++ b/src/openrct2/world/mapgen.c @@ -22,6 +22,7 @@ #include "../game.h" #include "../localisation/string_ids.h" #include "../object.h" +#include "../platform/platform.h" #include "../util/util.h" #include "../windows/error.h" #include "map.h" @@ -142,7 +143,7 @@ void mapgen_generate(mapgen_settings *settings) sint32 x, y, mapSize, floorTexture, wallTexture, waterLevel; rct_map_element *mapElement; - util_srand((sint32)SDL_GetTicks()); + util_srand((sint32)platform_get_ticks()); mapSize = settings->mapSize; floorTexture = settings->floor; From 459d79d2f3132c59993529f065a2a111bfac4873 Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 29 Mar 2017 20:33:43 +0100 Subject: [PATCH 02/21] Use FILE instead of SDL for FileStream --- src/openrct2/core/FileStream.hpp | 87 +++++++++++++++++++------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/src/openrct2/core/FileStream.hpp b/src/openrct2/core/FileStream.hpp index 8eb5aa9e44..1718e78906 100644 --- a/src/openrct2/core/FileStream.hpp +++ b/src/openrct2/core/FileStream.hpp @@ -17,10 +17,14 @@ #pragma once #include "../common.h" - -#include #include "IStream.hpp" #include "Math.hpp" +#include "String.hpp" + +extern "C" +{ + #include "../localisation/language.h" +} enum { @@ -30,12 +34,12 @@ enum }; /** - * A stream for reading and writing to files. Wraps an SDL_RWops, SDL2's cross platform file stream. + * A stream for reading and writing to files. */ class FileStream final : public IStream { private: - SDL_RWops * _file = 0; + FILE * _file = nullptr; bool _ownsFilePtr = false; bool _canRead = false; bool _canWrite = false; @@ -71,41 +75,35 @@ public: throw; } - _file = SDL_RWFromFile(path, mode); +#ifdef _WIN32 + wchar_t * pathW = utf8_to_widechar(path); + wchar_t * modeW = utf8_to_widechar(mode); + _file = _wfopen(pathW, modeW); + free(pathW); + free(modeW); +#else + _file = fopen(path, mode); +#endif if (_file == nullptr) { - throw IOException(SDL_GetError()); + throw IOException(String::StdFormat("Unable to open '%s'", path)); } - _fileSize = SDL_RWsize(_file); + + Seek(0, STREAM_SEEK_END); + _fileSize = GetPosition(); + Seek(0, STREAM_SEEK_BEGIN); + _ownsFilePtr = true; } - FileStream(SDL_RWops * ops, sint32 fileMode) - { - _file = ops; - switch (fileMode) { - case FILE_MODE_OPEN: - _canRead = true; - _canWrite = false; - break; - case FILE_MODE_WRITE: - _canRead = true; - _canWrite = true; - break; - default: - throw; - } - _fileSize = SDL_RWsize(_file); - } - - ~FileStream() + ~FileStream() override { if (!_disposed) { _disposed = true; if (_ownsFilePtr) { - SDL_RWclose(_file); + fclose(_file); } } } @@ -114,7 +112,14 @@ public: bool CanWrite() const override { return _canWrite; } uint64 GetLength() const override { return _fileSize; } - uint64 GetPosition() const override { return SDL_RWtell(_file); } + uint64 GetPosition() const override + { +#ifdef _MSC_VER + return _ftelli64(_file); +#else + return ftello64(_file); +#endif + } void SetPosition(uint64 position) override { @@ -123,17 +128,31 @@ public: void Seek(sint64 offset, sint32 origin) override { +#ifdef _MSC_VER switch (origin) { case STREAM_SEEK_BEGIN: - SDL_RWseek(_file, offset, RW_SEEK_SET); + _fseeki64(_file, offset, SEEK_SET); break; case STREAM_SEEK_CURRENT: - SDL_RWseek(_file, offset, RW_SEEK_CUR); + _fseeki64(_file, offset, SEEK_CUR); break; case STREAM_SEEK_END: - SDL_RWseek(_file, offset, RW_SEEK_END); + _fseeki64(_file, offset, SEEK_END); break; } +#else + switch (origin) { + case STREAM_SEEK_BEGIN: + fseeko64(_file, offset, SEEK_SET); + break; + case STREAM_SEEK_CURRENT: + fseeko64(_file, offset, SEEK_CUR); + break; + case STREAM_SEEK_END: + fseeko64(_file, offset, SEEK_END); + break; + } +#endif } void Read(void * buffer, uint64 length) override @@ -141,7 +160,7 @@ public: uint64 remainingBytes = GetLength() - GetPosition(); if (length <= remainingBytes) { - if (SDL_RWread(_file, buffer, (size_t)length, 1) == 1) + if (fread(buffer, (size_t)length, 1, _file) == 1) { return; } @@ -151,7 +170,7 @@ public: void Write(const void * buffer, uint64 length) override { - if (SDL_RWwrite(_file, buffer, (size_t)length, 1) != 1) + if (fwrite(buffer, (size_t)length, 1, _file) != 1) { throw IOException("Unable to write to file."); } @@ -162,7 +181,7 @@ public: uint64 TryRead(void * buffer, uint64 length) override { - size_t readBytes = SDL_RWread(_file, buffer, 1, (size_t)length); + size_t readBytes = fread(buffer, 1, (size_t)length, _file); return readBytes; } }; From 0ad94f92e325c4a7c95d88c701b23dbb3b6381d7 Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 29 Mar 2017 20:49:32 +0100 Subject: [PATCH 03/21] Replace Stopwatch with chrono --- src/openrct2/core/Stopwatch.cpp | 150 ----------------------- src/openrct2/core/Stopwatch.hpp | 52 -------- src/openrct2/core/stopwatch.h | 40 ------ src/openrct2/object/ObjectRepository.cpp | 11 +- 4 files changed, 6 insertions(+), 247 deletions(-) delete mode 100644 src/openrct2/core/Stopwatch.cpp delete mode 100644 src/openrct2/core/Stopwatch.hpp delete mode 100644 src/openrct2/core/stopwatch.h diff --git a/src/openrct2/core/Stopwatch.cpp b/src/openrct2/core/Stopwatch.cpp deleted file mode 100644 index 72772d6745..0000000000 --- a/src/openrct2/core/Stopwatch.cpp +++ /dev/null @@ -1,150 +0,0 @@ -#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers -/***************************************************************************** - * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. - * - * OpenRCT2 is the work of many authors, a full list can be found in contributors.md - * For more information, visit https://github.com/OpenRCT2/OpenRCT2 - * - * OpenRCT2 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, either version 3 of the License, or - * (at your option) any later version. - * - * A full copy of the GNU General Public License can be found in licence.txt - *****************************************************************************/ -#pragma endregion - -#include - -#include "Stopwatch.hpp" - -uint64 Stopwatch::Frequency = 0; - -Stopwatch::Stopwatch() -{ - Reset(); -} - -uint64 Stopwatch::GetElapsedTicks() const -{ - uint64 result = _total; - if (_isRunning) - { - uint64 ticks = QueryCurrentTicks(); - if (ticks != 0) { - result += QueryCurrentTicks() - _last; - } - } - - return result; -} - -uint64 Stopwatch::GetElapsedMilliseconds() const -{ - if (Frequency == 0) - { - Frequency = QueryFrequency(); - if (Frequency == 0) - { - return 0; - } - } - - return (GetElapsedTicks() * 1000) / Frequency; -} - -void Stopwatch::Reset() -{ - _isRunning = false; - _total = 0; - _last = 0; -} - -void Stopwatch::Start() -{ - if (_isRunning) return; - - uint64 ticks = QueryCurrentTicks(); - if (ticks != 0) - { - _last = ticks; - _isRunning = true; - } -} - -void Stopwatch::Restart() -{ - Reset(); - Start(); -} - -void Stopwatch::Stop() -{ - uint64 ticks = QueryCurrentTicks(); - if (ticks != 0) - { - _total += QueryCurrentTicks() - _last; - } - _isRunning = false; -} - -uint64 Stopwatch::QueryFrequency() -{ - return SDL_GetPerformanceFrequency(); -} - -uint64 Stopwatch::QueryCurrentTicks() -{ - return SDL_GetPerformanceCounter(); -} - -extern "C" -{ - #include "stopwatch.h" - - void stopwatch_create(stopwatch_t * stopwatch) - { - stopwatch->context = new Stopwatch(); - } - - void stopwatch_dispose(stopwatch_t * stopwatch) - { - delete static_cast(stopwatch->context); - } - - uint64 stopwatch_GetElapsedTicks(stopwatch_t * stopwatch) - { - Stopwatch * ctx = static_cast(stopwatch->context); - return ctx->GetElapsedTicks(); - } - - uint64 stopwatch_GetElapsedMilliseconds(stopwatch_t * stopwatch) - { - Stopwatch * ctx = static_cast(stopwatch->context); - return ctx->GetElapsedMilliseconds(); - } - - void stopwatch_Reset(stopwatch_t * stopwatch) - { - Stopwatch * ctx = static_cast(stopwatch->context); - return ctx->Reset(); - } - - void stopwatch_Start(stopwatch_t * stopwatch) - { - Stopwatch * ctx = static_cast(stopwatch->context); - return ctx->Start(); - } - - void stopwatch_Restart(stopwatch_t * stopwatch) - { - Stopwatch * ctx = static_cast(stopwatch->context); - return ctx->Restart(); - } - - void stopwatch_Stop(stopwatch_t * stopwatch) - { - Stopwatch * ctx = static_cast(stopwatch->context); - return ctx->Stop(); - } -} diff --git a/src/openrct2/core/Stopwatch.hpp b/src/openrct2/core/Stopwatch.hpp deleted file mode 100644 index 49eb065eb4..0000000000 --- a/src/openrct2/core/Stopwatch.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers -/***************************************************************************** - * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. - * - * OpenRCT2 is the work of many authors, a full list can be found in contributors.md - * For more information, visit https://github.com/OpenRCT2/OpenRCT2 - * - * OpenRCT2 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, either version 3 of the License, or - * (at your option) any later version. - * - * A full copy of the GNU General Public License can be found in licence.txt - *****************************************************************************/ -#pragma endregion - -#pragma once - -extern "C" -{ - #include "../common.h" -} - -/** - * Class to accurately measure elapsed time with high precision. - */ -class Stopwatch final -{ -private: - /** Number of ticks in a second. */ - static uint64 Frequency; - - uint64 _total; - uint64 _last; - bool _isRunning; - - static uint64 QueryFrequency(); - static uint64 QueryCurrentTicks(); - -public: - bool IsRunning() const { return _isRunning; } - - Stopwatch(); - - uint64 GetElapsedTicks() const; - uint64 GetElapsedMilliseconds() const; - - void Reset(); - void Start(); - void Restart(); - void Stop(); -}; diff --git a/src/openrct2/core/stopwatch.h b/src/openrct2/core/stopwatch.h deleted file mode 100644 index 84d93ffc0d..0000000000 --- a/src/openrct2/core/stopwatch.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers -/***************************************************************************** - * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. - * - * OpenRCT2 is the work of many authors, a full list can be found in contributors.md - * For more information, visit https://github.com/OpenRCT2/OpenRCT2 - * - * OpenRCT2 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, either version 3 of the License, or - * (at your option) any later version. - * - * A full copy of the GNU General Public License can be found in licence.txt - *****************************************************************************/ -#pragma endregion - -#ifndef _STOPWATCH_H_ -#define _STOPWATCH_H_ - -#include "../common.h" - -///////////////////////////// -// C wrapper for Stopwatch // -///////////////////////////// - -typedef struct stopwatch_t { - void *context; -} stopwatch_t; - -void stopwatch_create(stopwatch_t *stopwatch); -void stopwatch_dispose(stopwatch_t *stopwatch); - -uint64 stopwatch_GetElapsedTicks(stopwatch_t *stopwatch); -uint64 stopwatch_GetElapsedMilliseconds(stopwatch_t *stopwatch); -void stopwatch_Reset(stopwatch_t *stopwatch); -void stopwatch_Start(stopwatch_t *stopwatch); -void stopwatch_Restart(stopwatch_t *stopwatch); -void stopwatch_Stop(stopwatch_t *stopwatch); - -#endif diff --git a/src/openrct2/object/ObjectRepository.cpp b/src/openrct2/object/ObjectRepository.cpp index 9ffe6f038b..ddad3659b3 100644 --- a/src/openrct2/object/ObjectRepository.cpp +++ b/src/openrct2/object/ObjectRepository.cpp @@ -15,6 +15,7 @@ #pragma endregion #include +#include #include #include #include @@ -28,7 +29,6 @@ #include "../core/Memory.hpp" #include "../core/MemoryStream.h" #include "../core/Path.hpp" -#include "../core/Stopwatch.hpp" #include "../core/String.hpp" #include "../PlatformEnvironment.h" #include "../rct12/SawyerChunkReader.h" @@ -296,16 +296,17 @@ private: Console::WriteLine("Scanning %lu objects...", _queryDirectoryResult.TotalFiles); _numConflicts = 0; - auto stopwatch = Stopwatch(); - stopwatch.Start(); + auto startTime = std::chrono::high_resolution_clock::now(); const std::string &rct2Path = _env->GetDirectoryPath(DIRBASE::RCT2, DIRID::OBJECT); const std::string &openrct2Path = _env->GetDirectoryPath(DIRBASE::USER, DIRID::OBJECT); ScanDirectory(rct2Path); ScanDirectory(openrct2Path); - stopwatch.Stop(); - Console::WriteLine("Scanning complete in %.2f seconds.", stopwatch.GetElapsedMilliseconds() / 1000.0f); + auto endTime = std::chrono::high_resolution_clock::now(); + std::chrono::duration duration = endTime - startTime; + + Console::WriteLine("Scanning complete in %.2f seconds.", duration.count()); if (_numConflicts > 0) { Console::WriteLine("%d object conflicts found.", _numConflicts); From 90596e0f54f928436ef1790a4f46d3f988672314 Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 29 Mar 2017 20:58:16 +0100 Subject: [PATCH 04/21] Remove unnecessary SDL headers from NetworkTypes.h --- src/openrct2/network/NetworkTypes.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/openrct2/network/NetworkTypes.h b/src/openrct2/network/NetworkTypes.h index 04e7d7b222..3e22b1c934 100644 --- a/src/openrct2/network/NetworkTypes.h +++ b/src/openrct2/network/NetworkTypes.h @@ -16,14 +16,7 @@ #pragma once -#include -#include - -#ifndef DISABLE_NETWORK - - #include "../common.h" -#endif enum NETWORK_AUTH { From ec3652152ad842b8cf085825cd4cbf662cc36485 Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 29 Mar 2017 21:06:15 +0100 Subject: [PATCH 05/21] Quick port of server_list window to C++ So that C++ mutex can be used. --- .../{server_list.c => server_list.cpp} | 138 +++++++++--------- 1 file changed, 72 insertions(+), 66 deletions(-) rename src/openrct2/windows/{server_list.c => server_list.cpp} (90%) diff --git a/src/openrct2/windows/server_list.c b/src/openrct2/windows/server_list.cpp similarity index 90% rename from src/openrct2/windows/server_list.c rename to src/openrct2/windows/server_list.cpp index 083f398d6b..e9f0745bab 100644 --- a/src/openrct2/windows/server_list.c +++ b/src/openrct2/windows/server_list.cpp @@ -15,19 +15,23 @@ #pragma endregion #include "../config/Config.h" -#include "../interface/colour.h" -#include "../interface/themes.h" -#include "../interface/widget.h" -#include "../interface/window.h" -#include "../localisation/localisation.h" -#include "../network/http.h" #include "../network/network.h" #include "../network/ServerList.h" -#include "../sprites.h" -#include "../util/util.h" -#include "../windows/dropdown.h" -#include "../windows/tooltip.h" -#include "error.h" + +extern "C" +{ + #include "../interface/colour.h" + #include "../interface/themes.h" + #include "../interface/widget.h" + #include "../interface/window.h" + #include "../localisation/localisation.h" + #include "../network/http.h" + #include "../sprites.h" + #include "../util/util.h" + #include "../windows/dropdown.h" + #include "../windows/tooltip.h" + #include "error.h" +} #define WWIDTH_MIN 500 #define WHEIGHT_MIN 300 @@ -119,6 +123,7 @@ enum { }; static sint32 _hoverButtonIndex = -1; +static char * _version = NULL; static void server_list_get_item_button(sint32 buttonIndex, sint32 x, sint32 y, sint32 width, sint32 *outX, sint32 *outY); static void server_list_load_server_entries(); @@ -134,53 +139,56 @@ static void fetch_servers(); static void fetch_servers_callback(http_response_t* response); #endif -void window_server_list_open() +extern "C" { - rct_window* window; - - // Check if window is already open - window = window_bring_to_front_by_class(WC_SERVER_LIST); - if (window != NULL) - return; - - if (_mutex == 0) { - _mutex = SDL_CreateMutex(); + void window_server_list_open() + { + rct_window* window; + + // Check if window is already open + window = window_bring_to_front_by_class(WC_SERVER_LIST); + if (window != NULL) + return; + + if (_mutex == 0) { + _mutex = SDL_CreateMutex(); + } + + window = window_create_centred(WWIDTH_MIN, WHEIGHT_MIN, &window_server_list_events, WC_SERVER_LIST, WF_10 | WF_RESIZABLE); + + window_server_list_widgets[WIDX_PLAYER_NAME_INPUT].string = _playerName; + window->widgets = window_server_list_widgets; + window->enabled_widgets = ( + (1 << WIDX_CLOSE) | + (1 << WIDX_PLAYER_NAME_INPUT) | + (1 << WIDX_FETCH_SERVERS) | + (1 << WIDX_ADD_SERVER) | + (1 << WIDX_START_SERVER) + ); + window_init_scroll_widgets(window); + window->no_list_items = 0; + window->selected_list_item = -1; + window->frame_no = 0; + window->min_width = 320; + window->min_height = 90; + window->max_width = window->min_width; + window->max_height = window->min_height; + + window->page = 0; + window->list_information_type = 0; + window->colours[0] = COLOUR_GREY; + window->colours[1] = COLOUR_BORDEAUX_RED; + window->colours[2] = COLOUR_BORDEAUX_RED; + + window_set_resize(window, WWIDTH_MIN, WHEIGHT_MIN, WWIDTH_MAX, WHEIGHT_MAX); + + safe_strcpy(_playerName, gConfigNetwork.player_name, sizeof(_playerName)); + + server_list_load_server_entries(); + window->no_list_items = _numServerEntries; + + fetch_servers(); } - - window = window_create_centred(WWIDTH_MIN, WHEIGHT_MIN, &window_server_list_events, WC_SERVER_LIST, WF_10 | WF_RESIZABLE); - - window_server_list_widgets[WIDX_PLAYER_NAME_INPUT].string = _playerName; - window->widgets = window_server_list_widgets; - window->enabled_widgets = ( - (1 << WIDX_CLOSE) | - (1 << WIDX_PLAYER_NAME_INPUT) | - (1 << WIDX_FETCH_SERVERS) | - (1 << WIDX_ADD_SERVER) | - (1 << WIDX_START_SERVER) - ); - window_init_scroll_widgets(window); - window->no_list_items = 0; - window->selected_list_item = -1; - window->frame_no = 0; - window->min_width = 320; - window->min_height = 90; - window->max_width = window->min_width; - window->max_height = window->min_height; - - window->page = 0; - window->list_information_type = 0; - window->colours[0] = COLOUR_GREY; - window->colours[1] = COLOUR_BORDEAUX_RED; - window->colours[2] = COLOUR_BORDEAUX_RED; - - window_set_resize(window, WWIDTH_MIN, WHEIGHT_MIN, WWIDTH_MAX, WHEIGHT_MAX); - - safe_strcpy(_playerName, gConfigNetwork.player_name, sizeof(_playerName)); - - server_list_load_server_entries(); - window->no_list_items = _numServerEntries; - - fetch_servers(); } static void window_server_list_close(rct_window *w) @@ -288,8 +296,6 @@ static void window_server_list_scroll_mousedown(rct_window *w, sint32 scrollInde window_dropdown_show_text(ddx, ddy, 0, COLOUR_GREY, 0, 2); } -char *gVersion = NULL; - static void window_server_list_scroll_mouseover(rct_window *w, sint32 scrollIndex, sint32 x, sint32 y) { // Item @@ -367,7 +373,7 @@ static void window_server_list_invalidate(rct_window *w) { colour_scheme_update(w); - set_format_arg(0, char *, gVersion); + set_format_arg(0, char *, _version); window_server_list_widgets[WIDX_BACKGROUND].right = w->width - 1; window_server_list_widgets[WIDX_BACKGROUND].bottom = w->height - 1; window_server_list_widgets[WIDX_TITLE].right = w->width - 2; @@ -399,7 +405,7 @@ static void window_server_list_paint(rct_window *w, rct_drawpixelinfo *dpi) window_draw_widgets(w, dpi); gfx_draw_string_left(dpi, STR_PLAYER_NAME, NULL, COLOUR_WHITE, w->x + 6, w->y + w->widgets[WIDX_PLAYER_NAME_INPUT].top); - char *version = NETWORK_STREAM_ID; + const char * version = NETWORK_STREAM_ID; gfx_draw_string_left(dpi, STR_NETWORK_VERSION, (void*)&version, COLOUR_WHITE, w->x + 324, w->y + w->widgets[WIDX_START_SERVER].top); gfx_draw_string_left(dpi, STR_X_PLAYERS_ONLINE, (void*)&_numPlayersOnline, COLOUR_WHITE, w->x + 8, w->y + w->height - 15); @@ -424,7 +430,7 @@ static void window_server_list_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi // Draw hover highlight if (highlighted) { gfx_filter_rect(dpi, 0, y, width, y + ITEM_HEIGHT, PALETTE_DARKEN_1); - gVersion = serverDetails->version; + _version = serverDetails->version; w->widgets[WIDX_LIST].tooltip = STR_NETWORK_VERSION_TIP; } @@ -514,7 +520,7 @@ static void server_list_save_server_entries() } // Create temporary list of just favourite servers - server_entry * entries = calloc(count, sizeof(server_entry)); + server_entry * entries = (server_entry *)calloc(count, sizeof(server_entry)); sint32 eindex = 0; for (sint32 i = 0; i < _numServerEntries; i++) { server_entry *serverInfo = &_serverEntries[i]; @@ -566,9 +572,9 @@ static server_entry* add_server_entry(char *address) _numServerEntries++; if (_serverEntries == NULL) { - _serverEntries = malloc(_numServerEntries * sizeof(server_entry)); + _serverEntries = (server_entry *)malloc(_numServerEntries * sizeof(server_entry)); } else { - _serverEntries = realloc(_serverEntries, _numServerEntries * sizeof(server_entry)); + _serverEntries = (server_entry *)realloc(_serverEntries, _numServerEntries * sizeof(server_entry)); } sint32 index = _numServerEntries - 1; @@ -593,7 +599,7 @@ static void remove_server_entry(sint32 index) memmove(&_serverEntries[index], &_serverEntries[index + 1], serversToMove * sizeof(server_entry)); _numServerEntries--; - _serverEntries = realloc(_serverEntries, _numServerEntries * sizeof(server_entry)); + _serverEntries = (server_entry *)realloc(_serverEntries, _numServerEntries * sizeof(server_entry)); } SDL_UnlockMutex(_mutex); } @@ -637,7 +643,7 @@ static void sort_servers() static char *substr(char *start, sint32 length) { - char *result = malloc(length + 1); + char *result = (char *)malloc(length + 1); memcpy(result, start, length); result[length] = 0; return result; From b8931bd50336e99c52efc9580efe04683bd52ee5 Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 29 Mar 2017 21:29:39 +0100 Subject: [PATCH 06/21] Use C++ mutex instead of SDL for server_list.cpp --- CMakeLists.txt | 2 +- src/openrct2/windows/server_list.cpp | 89 ++++++++++++---------------- 2 files changed, 39 insertions(+), 52 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b3f8814120..8abf28f8f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -428,7 +428,7 @@ set_target_properties(${PROJECT} PROPERTIES PREFIX "") set_target_properties(${PROJECT} PROPERTIES COMPILE_FLAGS "-Wundef") # Link shared libs first -TARGET_LINK_LIBRARIES(${PROJECT} ${GLLIBS}) +TARGET_LINK_LIBRARIES(${PROJECT} pthread ${GLLIBS}) # if creating a static binary, precede libraries with -static, then name all the libs TARGET_LINK_LIBRARIES(${PROJECT} ${STATIC_START} ${SDL2LIBS} ${HTTPLIBS} ${NETWORKLIBS} ${SPEEX_LIBRARIES} ${DLLIB} ${REQUIREDLIBS} ${BREAKPAD_LIBS}) diff --git a/src/openrct2/windows/server_list.cpp b/src/openrct2/windows/server_list.cpp index e9f0745bab..284dbecd61 100644 --- a/src/openrct2/windows/server_list.cpp +++ b/src/openrct2/windows/server_list.cpp @@ -14,6 +14,7 @@ *****************************************************************************/ #pragma endregion +#include #include "../config/Config.h" #include "../network/network.h" #include "../network/ServerList.h" @@ -42,7 +43,7 @@ extern "C" static char _playerName[32 + 1]; static server_entry *_serverEntries = NULL; static sint32 _numServerEntries = 0; -static SDL_mutex *_mutex = 0; +static std::mutex _mutex; static uint32 _numPlayersOnline = 0; enum { @@ -150,10 +151,6 @@ extern "C" if (window != NULL) return; - if (_mutex == 0) { - _mutex = SDL_CreateMutex(); - } - window = window_create_centred(WWIDTH_MIN, WHEIGHT_MIN, &window_server_list_events, WC_SERVER_LIST, WF_10 | WF_RESIZABLE); window_server_list_widgets[WIDX_PLAYER_NAME_INPUT].string = _playerName; @@ -193,12 +190,8 @@ extern "C" static void window_server_list_close(rct_window *w) { + std::lock_guard guard(_mutex); dispose_server_entry_list(); - if (_mutex) { - SDL_LockMutex(_mutex); - SDL_DestroyMutex(_mutex); - _mutex = 0; - } } static void window_server_list_mouseup(rct_window *w, rct_widgetindex widgetIndex) @@ -257,8 +250,11 @@ static void window_server_list_dropdown(rct_window *w, rct_widgetindex widgetInd join_server(serverAddress); break; case DDIDX_FAVOURITE: - _serverEntries[serverIndex].favourite = !_serverEntries[serverIndex].favourite; - server_list_save_server_entries(); + { + std::lock_guard guard(_mutex); + _serverEntries[serverIndex].favourite = !_serverEntries[serverIndex].favourite; + server_list_save_server_entries(); + } break; } } @@ -361,9 +357,12 @@ static void window_server_list_textinput(rct_window *w, rct_widgetindex widgetIn break; case WIDX_ADD_SERVER: - add_server_entry(text); - sort_servers(); - server_list_save_server_entries(); + { + std::lock_guard guard(_mutex); + add_server_entry(text); + sort_servers(); + server_list_save_server_entries(); + } window_invalidate(w); break; } @@ -413,6 +412,8 @@ static void window_server_list_paint(rct_window *w, rct_drawpixelinfo *dpi) static void window_server_list_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, sint32 scrollIndex) { + std::lock_guard guard(_mutex); + uint8 paletteIndex = ColourMapA[w->colours[1]].mid_light; gfx_clear(dpi, paletteIndex); @@ -490,26 +491,19 @@ static void server_list_get_item_button(sint32 buttonIndex, sint32 x, sint32 y, static void server_list_load_server_entries() { - SDL_LockMutex(_mutex); - uint32 numEntries; server_entry * entries; if (server_list_read(&numEntries, &entries)) { + std::lock_guard guard(_mutex); dispose_server_entry_list(); - _numServerEntries = numEntries; _serverEntries = entries; - sort_servers(); } - - SDL_UnlockMutex(_mutex); } static void server_list_save_server_entries() { - SDL_LockMutex(_mutex); - // Get total number of favourite servers sint32 count = 0; for (sint32 i = 0; i < _numServerEntries; i++) { @@ -534,13 +528,10 @@ static void server_list_save_server_entries() // Free temporary list free(entries); - - SDL_UnlockMutex(_mutex); } static void dispose_server_entry_list() { - SDL_LockMutex(_mutex); if (_serverEntries != NULL) { for (sint32 i = 0; i < _numServerEntries; i++) { dispose_server_entry(&_serverEntries[i]); @@ -549,7 +540,6 @@ static void dispose_server_entry_list() _serverEntries = NULL; } _numServerEntries = 0; - SDL_UnlockMutex(_mutex); } static void dispose_server_entry(server_entry *serverInfo) @@ -562,10 +552,8 @@ static void dispose_server_entry(server_entry *serverInfo) static server_entry* add_server_entry(char *address) { - SDL_LockMutex(_mutex); for (sint32 i = 0; i < _numServerEntries; i++) { if (strcmp(_serverEntries[i].address, address) == 0) { - SDL_UnlockMutex(_mutex); return &_serverEntries[i]; } } @@ -587,13 +575,11 @@ static server_entry* add_server_entry(char *address) newserver->favourite = false; newserver->players = 0; newserver->maxplayers = 0; - SDL_UnlockMutex(_mutex); return newserver; } static void remove_server_entry(sint32 index) { - SDL_LockMutex(_mutex); if (_numServerEntries > index) { sint32 serversToMove = _numServerEntries - index - 1; memmove(&_serverEntries[index], &_serverEntries[index + 1], serversToMove * sizeof(server_entry)); @@ -601,7 +587,6 @@ static void remove_server_entry(sint32 index) _numServerEntries--; _serverEntries = (server_entry *)realloc(_serverEntries, _numServerEntries * sizeof(server_entry)); } - SDL_UnlockMutex(_mutex); } static sint32 server_compare(const void *a, const void *b) @@ -698,15 +683,16 @@ static void fetch_servers() masterServerUrl = gConfigNetwork.master_server_url; } - SDL_LockMutex(_mutex); - for (sint32 i = 0; i < _numServerEntries; i++) { - if (!_serverEntries[i].favourite) { - remove_server_entry(i); - i = 0; + { + std::lock_guard guard(_mutex); + for (sint32 i = 0; i < _numServerEntries; i++) { + if (!_serverEntries[i].favourite) { + remove_server_entry(i); + i = 0; + } } + sort_servers(); } - sort_servers(); - SDL_UnlockMutex(_mutex); http_request_t request = { 0 }; request.url = masterServerUrl; @@ -773,18 +759,19 @@ static void fetch_servers_callback(http_response_t* response) char address[256]; snprintf(address, sizeof(address), "%s:%d", json_string_value(addressIp), (sint32)json_integer_value(port)); - SDL_LockMutex(_mutex); - server_entry* newserver = add_server_entry(address); - SafeFree(newserver->name); - SafeFree(newserver->description); - SafeFree(newserver->version); - newserver->name = _strdup(json_string_value(name)); - newserver->requiresPassword = json_is_true(requiresPassword); - newserver->description = _strdup(description == NULL ? "" : json_string_value(description)); - newserver->version = _strdup(json_string_value(version)); - newserver->players = (uint8)json_integer_value(players); - newserver->maxplayers = (uint8)json_integer_value(maxPlayers); - SDL_UnlockMutex(_mutex); + { + std::lock_guard guard(_mutex); + server_entry* newserver = add_server_entry(address); + SafeFree(newserver->name); + SafeFree(newserver->description); + SafeFree(newserver->version); + newserver->name = _strdup(json_string_value(name)); + newserver->requiresPassword = json_is_true(requiresPassword); + newserver->description = _strdup(description == NULL ? "" : json_string_value(description)); + newserver->version = _strdup(json_string_value(version)); + newserver->players = (uint8)json_integer_value(players); + newserver->maxplayers = (uint8)json_integer_value(maxPlayers); + } } http_request_dispose(response); From 08b38172728a51c7fd62c3570ba08948937e480d Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 29 Mar 2017 22:17:55 +0100 Subject: [PATCH 07/21] Use C++ future, thread and chrono for TcpSocket --- src/openrct2/network/TcpSocket.cpp | 89 ++++++++++++------------------ 1 file changed, 35 insertions(+), 54 deletions(-) diff --git a/src/openrct2/network/TcpSocket.cpp b/src/openrct2/network/TcpSocket.cpp index 8ee4aeaeb0..0170296d5e 100644 --- a/src/openrct2/network/TcpSocket.cpp +++ b/src/openrct2/network/TcpSocket.cpp @@ -16,14 +16,14 @@ #ifndef DISABLE_NETWORK +#include +#include +#include + // MSVC: include here otherwise PI gets defined twice #include -#include -#include -#include - -#ifdef __WINDOWS__ +#ifdef _WIN32 // winsock2 must be included before windows.h #include #include @@ -58,15 +58,15 @@ #define FLAG_NO_PIPE MSG_NOSIGNAL #else #define FLAG_NO_PIPE 0 - #endif // defined(__LINUX__) -#endif // __WINDOWS__ + #endif // defined(__linux__) +#endif // _WIN32 #include "../core/Exception.hpp" #include "TcpSocket.h" -constexpr uint32 CONNECT_TIMEOUT_MS = 3000; +constexpr auto CONNECT_TIMEOUT = std::chrono::milliseconds(3000); -#ifdef __WINDOWS__ +#ifdef _WIN32 static bool _wsaInitialised = false; #endif @@ -79,13 +79,6 @@ public: explicit SocketException(const std::string &message) : Exception(message) { } }; -struct ConnectRequest -{ - TcpSocket * Socket; - std::string Address; - uint16 Port; -}; - class TcpSocket final : public ITcpSocket { private: @@ -93,25 +86,22 @@ private: uint16 _listeningPort = 0; SOCKET _socket = INVALID_SOCKET; - std::string _hostName; - - SDL_mutex * _connectMutex = nullptr; - std::string _error; + std::string _hostName; + std::future _connectFuture; + std::string _error; public: TcpSocket() { - _connectMutex = SDL_CreateMutex(); } ~TcpSocket() override { - SDL_LockMutex(_connectMutex); + if (_connectFuture.valid()) { - CloseSocket(); + _connectFuture.wait(); } - SDL_UnlockMutex(_connectMutex); - SDL_DestroyMutex(_connectMutex); + CloseSocket(); } SOCKET_STATUS GetStatus() override @@ -264,7 +254,6 @@ public: } // Connect - uint32 connectStartTick; sint32 connectResult = connect(_socket, (sockaddr *)&ss, ss_len); if (connectResult != SOCKET_ERROR || (LAST_SOCKET_ERROR() != EINPROGRESS && LAST_SOCKET_ERROR() != EWOULDBLOCK)) @@ -272,7 +261,7 @@ public: throw SocketException("Failed to connect."); } - connectStartTick = SDL_GetTicks(); + auto connectStartTime = std::chrono::system_clock::now(); sint32 error = 0; socklen_t len = sizeof(error); @@ -288,7 +277,7 @@ public: do { // Sleep for a bit - SDL_Delay(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); fd_set writeFD; FD_ZERO(&writeFD); @@ -310,7 +299,7 @@ public: return; } } - } while (!SDL_TICKS_PASSED(SDL_GetTicks(), connectStartTick + CONNECT_TIMEOUT_MS)); + } while ((std::chrono::system_clock::now() - connectStartTime) < CONNECT_TIMEOUT); // Connection request timed out throw SocketException("Connection timed out."); @@ -329,30 +318,22 @@ public: throw Exception("Socket not closed."); } - if (SDL_TryLockMutex(_connectMutex) == 0) + auto saddress = std::string(address); + std::promise barrier; + _connectFuture = barrier.get_future(); + auto thread = std::thread([this, saddress, port](std::promise barrier2) -> void { - // Spin off a worker thread for resolving the address - auto req = new ConnectRequest(); - req->Socket = this; - req->Address = std::string(address); - req->Port = port; - SDL_CreateThread([](void * pointer) -> sint32 + try { - auto req2 = static_cast(pointer); - try - { - req2->Socket->Connect(req2->Address.c_str(), req2->Port); - } - catch (const Exception & ex) - { - req2->Socket->_error = std::string(ex.GetMessage()); - } - - SDL_UnlockMutex(req2->Socket->_connectMutex); - delete req2; - return 0; - }, 0, req); - } + Connect(saddress.c_str(), port); + } + catch (const Exception &ex) + { + _error = std::string(ex.GetMessage()); + } + barrier2.set_value(); + }, std::move(barrier)); + thread.detach(); } void Disconnect() override @@ -419,11 +400,11 @@ public: void Close() override { - SDL_LockMutex(_connectMutex); + if (_connectFuture.valid()) { - CloseSocket(); + _connectFuture.wait(); } - SDL_UnlockMutex(_connectMutex); + CloseSocket(); } const char * GetHostName() const override From 80bdb25740c76ef9d549866a1253d412c57759bd Mon Sep 17 00:00:00 2001 From: Ted John Date: Thu, 30 Mar 2017 18:25:35 +0100 Subject: [PATCH 08/21] Change http.cpp to use std::thread --- src/openrct2/network/http.cpp | 166 ++++++++++++++++++++-------------- src/openrct2/network/http.h | 1 - 2 files changed, 96 insertions(+), 71 deletions(-) diff --git a/src/openrct2/network/http.cpp b/src/openrct2/network/http.cpp index 8d1aa14b81..11de25c7fa 100644 --- a/src/openrct2/network/http.cpp +++ b/src/openrct2/network/http.cpp @@ -14,9 +14,10 @@ *****************************************************************************/ #pragma endregion +#include + extern "C" { #include "http.h" - #include "../platform/platform.h" } #ifdef DISABLE_HTTP @@ -41,6 +42,70 @@ void http_dispose() { } #define MIME_TYPE_APPLICATION_JSON "application/json" #define OPENRCT2_USER_AGENT "OpenRCT2/" OPENRCT2_VERSION +struct HttpRequest2 +{ + void * Tag = nullptr; + std::string Method; + std::string Url; + http_data_type Type; + size_t Size = 0; + union + { + char * Buffer = 0; + json_t * Json; + } Body; + + HttpRequest2() { } + + HttpRequest2(const HttpRequest2 &request) + { + Tag = request.Tag; + Method = request.Method; + Url = request.Url; + Type = request.Type; + Size = request.Size; + if (request.Type == HTTP_DATA_JSON) + { + Body.Json = json_deep_copy(request.Body.Json); + } + else + { + Body.Buffer = new char[request.Size]; + memcpy(Body.Buffer, request.Body.Buffer, request.Size); + } + } + + explicit HttpRequest2(const http_request_t * request) + { + Tag = request->tag; + Method = std::string(request->method); + Url = std::string(request->url); + Type = request->type; + Size = request->size; + if (request->type == HTTP_DATA_JSON) + { + Body.Json = json_deep_copy(request->root); + } + else + { + Body.Buffer = new char[request->size]; + memcpy(Body.Buffer, request->body, request->size); + } + } + + ~HttpRequest2() + { + if (Type == HTTP_DATA_JSON) + { + json_decref(Body.Json); + } + else + { + delete Body.Buffer; + } + } +}; + typedef struct read_buffer { char *ptr; size_t length; @@ -85,7 +150,7 @@ static size_t http_request_write_func(void *ptr, size_t size, size_t nmemb, void return newBytesLength; } -http_response_t *http_request(const http_request_t *request) +static http_response_t *http_request(const HttpRequest2 &request) { CURL *curl; CURLcode curlResult; @@ -97,13 +162,13 @@ http_response_t *http_request(const http_request_t *request) if (curl == NULL) return NULL; - if (request->type == HTTP_DATA_JSON && request->root != NULL) { - readBuffer.ptr = json_dumps(request->root, JSON_COMPACT); + if (request.Type == HTTP_DATA_JSON && request.Body.Json != NULL) { + readBuffer.ptr = json_dumps(request.Body.Json, JSON_COMPACT); readBuffer.length = strlen(readBuffer.ptr); readBuffer.position = 0; - } else if (request->type == HTTP_DATA_RAW && request->body != NULL) { - readBuffer.ptr = request->body; - readBuffer.length = request->size; + } else if (request.Type == HTTP_DATA_RAW && request.Body.Buffer != NULL) { + readBuffer.ptr = request.Body.Buffer; + readBuffer.length = request.Size; readBuffer.position = 0; } @@ -113,10 +178,10 @@ http_response_t *http_request(const http_request_t *request) curl_slist *headers = NULL; - if (request->type == HTTP_DATA_JSON) { + if (request.Type == HTTP_DATA_JSON) { headers = curl_slist_append(headers, "Accept: " MIME_TYPE_APPLICATION_JSON); - if (request->root != NULL) { + if (request.Body.Json != NULL) { headers = curl_slist_append(headers, "Content-Type: " MIME_TYPE_APPLICATION_JSON); } } @@ -129,18 +194,18 @@ http_response_t *http_request(const http_request_t *request) curl_easy_setopt(curl, CURLOPT_POSTFIELDS, readBuffer.ptr); } - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, request->method); + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, request.Method.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true); curl_easy_setopt(curl, CURLOPT_USERAGENT, OPENRCT2_USER_AGENT); - curl_easy_setopt(curl, CURLOPT_URL, request->url); + curl_easy_setopt(curl, CURLOPT_URL, request.Url.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &writeBuffer); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_request_write_func); curlResult = curl_easy_perform(curl); - if (request->type == HTTP_DATA_JSON && request->root != NULL) { + if (request.Type == HTTP_DATA_JSON && request.Body.Json != NULL) { free(readBuffer.ptr); } @@ -173,7 +238,7 @@ http_response_t *http_request(const http_request_t *request) root = json_loads(writeBuffer.ptr, 0, &error); if (root != NULL) { response = (http_response_t*) malloc(sizeof(http_response_t)); - response->tag = request->tag; + response->tag = request.Tag; response->status_code = (sint32) httpStatusCode; response->root = root; response->type = HTTP_DATA_JSON; @@ -182,7 +247,7 @@ http_response_t *http_request(const http_request_t *request) free(writeBuffer.ptr); } else { response = (http_response_t*) malloc(sizeof(http_response_t)); - response->tag = request->tag; + response->tag = request.Tag; response->status_code = (sint32) httpStatusCode; response->body = writeBuffer.ptr; response->type = HTTP_DATA_RAW; @@ -194,54 +259,15 @@ http_response_t *http_request(const http_request_t *request) return response; } -void http_request_async(const http_request_t *request, void (*callback)(http_response_t*)) +void http_request_async(const http_request_t * request, void (*callback)(http_response_t*)) { - struct TempThreadArgs { - http_request_t request; - void (*callback)(http_response_t*); - }; - - TempThreadArgs *args = (TempThreadArgs*)malloc(sizeof(TempThreadArgs)); - args->request.url = _strdup(request->url); - args->request.method = request->method; - - if (request->type == HTTP_DATA_JSON) { - args->request.root = json_deep_copy(request->root); - } else { - char* bodyCopy = (char*) malloc(request->size); - memcpy(bodyCopy, request->body, request->size); - args->request.body = bodyCopy; - } - - args->request.type = request->type; - args->request.size = request->size; - args->request.tag = request->tag; - args->callback = callback; - - SDL_Thread *thread = SDL_CreateThread([](void *ptr) -> sint32 { - TempThreadArgs *args2 = (TempThreadArgs*)ptr; - - http_response_t *response = http_request(&args2->request); - args2->callback(response); - - free((char*)args2->request.url); - - if (args2->request.type == HTTP_DATA_JSON) { - json_decref((json_t*) args2->request.root); - } else { - free(args2->request.body); - } - - free(args2); - return 0; - }, NULL, args); - - if (thread == NULL) { - log_error("Unable to create thread!"); - callback(NULL); - } else { - SDL_DetachThread(thread); - } + auto request2 = HttpRequest2(request); + auto thread = std::thread([](const HttpRequest2 &req, void(*callback2)(http_response_t*)) -> void + { + http_response_t * response = http_request(req); + callback2(response); + }, std::move(request2), callback); + thread.detach(); } void http_request_dispose(http_response_t *response) @@ -269,15 +295,15 @@ const char *http_get_extension_from_url(const char *url, const char *fallback) bool http_download_park(const char *url, char tmpPath[L_tmpnam + 10]) { // Download park to buffer in memory - http_request_t request; - request.url = url; - request.method = "GET"; - request.type = HTTP_DATA_NONE; + HttpRequest2 request; + request.Url = url; + request.Method = "GET"; + request.Type = HTTP_DATA_NONE; - http_response_t *response = http_request(&request); + http_response_t *response = http_request(request); if (response == NULL || response->status_code != 200) { - Console::Error::WriteLine("Failed to download '%s'", request.url); + Console::Error::WriteLine("Failed to download '%s'", request.Url.c_str()); if (response != NULL) { http_request_dispose(response); } @@ -286,20 +312,20 @@ bool http_download_park(const char *url, char tmpPath[L_tmpnam + 10]) // Generate temporary filename that includes the original extension if (tmpnam(tmpPath) == NULL) { - Console::Error::WriteLine("Failed to generate temporary filename for downloaded park '%s'", request.url); + Console::Error::WriteLine("Failed to generate temporary filename for downloaded park '%s'", request.Url.c_str()); http_request_dispose(response); return false; } size_t remainingBytes = L_tmpnam + 10 - strlen(tmpPath); - const char *ext = http_get_extension_from_url(request.url, ".sv6"); + const char *ext = http_get_extension_from_url(request.Url.c_str(), ".sv6"); strncat(tmpPath, ext, remainingBytes); // Store park in temporary file and load it (discard ending NUL in response body) FILE* tmpFile = fopen(tmpPath, "wb"); if (tmpFile == NULL) { - Console::Error::WriteLine("Failed to write downloaded park '%s' to temporary file", request.url); + Console::Error::WriteLine("Failed to write downloaded park '%s' to temporary file", request.Url.c_str()); http_request_dispose(response); return false; } diff --git a/src/openrct2/network/http.h b/src/openrct2/network/http.h index 5806e9cd7d..842f4fe276 100644 --- a/src/openrct2/network/http.h +++ b/src/openrct2/network/http.h @@ -55,7 +55,6 @@ typedef struct http_response_t { #define HTTP_METHOD_PUT "PUT" #define HTTP_METHOD_DELETE "DELETE" -http_response_t *http_request(const http_request_t *request); void http_request_async(const http_request_t *request, void (*callback)(http_response_t*)); void http_request_dispose(http_response_t *response); From 4ba60cbf52827df14869d4efe2f9a77ab70a80bd Mon Sep 17 00:00:00 2001 From: Ted John Date: Thu, 30 Mar 2017 18:29:22 +0100 Subject: [PATCH 09/21] Replace SDLRW with fopen in interop.c --- src/openrct2/rct2/interop.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/openrct2/rct2/interop.c b/src/openrct2/rct2/interop.c index 591b3d5f3e..e427bbb175 100644 --- a/src/openrct2/rct2/interop.c +++ b/src/openrct2/rct2/interop.c @@ -14,6 +14,8 @@ *****************************************************************************/ #pragma endregion +#include + #include "../common.h" #if defined(__WINDOWS__) @@ -179,17 +181,20 @@ bool rct2_interop_setup_segment() utf8 segmentDataPath[MAX_PATH]; rct2_interop_get_segment_data_path(segmentDataPath, sizeof(segmentDataPath)); - SDL_RWops * rw = SDL_RWFromFile(segmentDataPath, "rb"); - if (rw == NULL) + + // Warning: for Windows this will fail if given a non-ASCII path, + // but given this code is temporary - its not worth resolving. + FILE * file = fopen(segmentDataPath, "rb"); + if (file == NULL) { log_error("failed to load file"); return false; } - if (SDL_RWread(rw, segments, len, 1) != 1) { + if (fread(segments, len, 1, file) != 1) { log_error("Unable to read chunk header!"); return false; } - SDL_RWclose(rw); + fclose(file); #endif // defined(USE_MMAP) && defined(__WINDOWS__) #if !defined(NO_RCT2) && defined(USE_MMAP) From 8e6ea776f6ad3974ef3bc35ba88ede141a0dbf52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Sun, 30 Apr 2017 12:13:24 +0100 Subject: [PATCH 10/21] Add time.h include to shared.c for clock_gettime --- src/openrct2/platform/shared.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/openrct2/platform/shared.c b/src/openrct2/platform/shared.c index e3115f5a8d..1c3b0c042c 100644 --- a/src/openrct2/platform/shared.c +++ b/src/openrct2/platform/shared.c @@ -15,6 +15,7 @@ #pragma endregion #include +#include #include "../audio/audio.h" #include "../audio/AudioMixer.h" #include "../config/Config.h" From 69907623762fb8d830da7db25b118999141b1d5f Mon Sep 17 00:00:00 2001 From: Marijn van der Werf Date: Sun, 30 Apr 2017 14:20:09 +0200 Subject: [PATCH 11/21] Update Xcode project --- OpenRCT2.xcodeproj/project.pbxproj | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/OpenRCT2.xcodeproj/project.pbxproj b/OpenRCT2.xcodeproj/project.pbxproj index ec0dd74686..e16baf74a9 100644 --- a/OpenRCT2.xcodeproj/project.pbxproj +++ b/OpenRCT2.xcodeproj/project.pbxproj @@ -227,6 +227,7 @@ C686F9541CDBC3B7009F9BFC /* submarine_ride.c in Sources */ = {isa = PBXBuildFile; fileRef = C686F9091CDBC3B7009F9BFC /* submarine_ride.c */; }; C686F9551CDBC3B7009F9BFC /* water_coaster.c in Sources */ = {isa = PBXBuildFile; fileRef = C686F90A1CDBC3B7009F9BFC /* water_coaster.c */; }; C686F9581CDBC4C7009F9BFC /* vehicle_paint.c in Sources */ = {isa = PBXBuildFile; fileRef = C686F9561CDBC4C7009F9BFC /* vehicle_paint.c */; }; + C68B2CB51EB60DDE0020651C /* server_list.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C68B2CB41EB60DDE0020651C /* server_list.cpp */; }; C6B5A7D41CDFE4CB00C9C006 /* S6Exporter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6B5A7D01CDFE4CB00C9C006 /* S6Exporter.cpp */; }; C6B5A7D51CDFE4CB00C9C006 /* S6Importer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6B5A7D21CDFE4CB00C9C006 /* S6Importer.cpp */; }; C6CABA7E1E13F11C00D33A6B /* hook.c in Sources */ = {isa = PBXBuildFile; fileRef = C6E96E181E0406F00076A04F /* hook.c */; }; @@ -282,7 +283,6 @@ D44272011CC81B3200D84D28 /* Guard.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D44270E71CC81B3200D84D28 /* Guard.cpp */; }; D44272021CC81B3200D84D28 /* Json.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D44270EB1CC81B3200D84D28 /* Json.cpp */; }; D44272031CC81B3200D84D28 /* Path.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D44270F01CC81B3200D84D28 /* Path.cpp */; }; - D44272041CC81B3200D84D28 /* Stopwatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D44270F21CC81B3200D84D28 /* Stopwatch.cpp */; }; D44272051CC81B3200D84D28 /* String.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D44270F51CC81B3200D84D28 /* String.cpp */; }; D44272061CC81B3200D84D28 /* textinputbuffer.c in Sources */ = {isa = PBXBuildFile; fileRef = D44270F91CC81B3200D84D28 /* textinputbuffer.c */; }; D44272081CC81B3200D84D28 /* diagnostic.c in Sources */ = {isa = PBXBuildFile; fileRef = D44270FE1CC81B3200D84D28 /* diagnostic.c */; }; @@ -392,7 +392,6 @@ D442727D1CC81B3200D84D28 /* ride_list.c in Sources */ = {isa = PBXBuildFile; fileRef = D44271BB1CC81B3200D84D28 /* ride_list.c */; }; D442727E1CC81B3200D84D28 /* save_prompt.c in Sources */ = {isa = PBXBuildFile; fileRef = D44271BC1CC81B3200D84D28 /* save_prompt.c */; }; D442727F1CC81B3200D84D28 /* scenery.c in Sources */ = {isa = PBXBuildFile; fileRef = D44271BD1CC81B3200D84D28 /* scenery.c */; }; - D44272801CC81B3200D84D28 /* server_list.c in Sources */ = {isa = PBXBuildFile; fileRef = D44271BE1CC81B3200D84D28 /* server_list.c */; }; D44272811CC81B3200D84D28 /* server_start.c in Sources */ = {isa = PBXBuildFile; fileRef = D44271BF1CC81B3200D84D28 /* server_start.c */; }; D44272821CC81B3200D84D28 /* shortcut_key_change.c in Sources */ = {isa = PBXBuildFile; fileRef = D44271C01CC81B3200D84D28 /* shortcut_key_change.c */; }; D44272831CC81B3200D84D28 /* shortcut_keys.c in Sources */ = {isa = PBXBuildFile; fileRef = D44271C11CC81B3200D84D28 /* shortcut_keys.c */; }; @@ -695,6 +694,7 @@ C686F90A1CDBC3B7009F9BFC /* water_coaster.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = water_coaster.c; sourceTree = ""; }; C686F9561CDBC4C7009F9BFC /* vehicle_paint.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = vehicle_paint.c; sourceTree = ""; }; C686F9571CDBC4C7009F9BFC /* vehicle_paint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = vehicle_paint.h; sourceTree = ""; }; + C68B2CB41EB60DDE0020651C /* server_list.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = server_list.cpp; sourceTree = ""; }; C6B5A7D01CDFE4CB00C9C006 /* S6Exporter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = S6Exporter.cpp; sourceTree = ""; }; C6B5A7D11CDFE4CB00C9C006 /* S6Exporter.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; path = S6Exporter.h; sourceTree = ""; }; C6B5A7D21CDFE4CB00C9C006 /* S6Importer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = S6Importer.cpp; sourceTree = ""; }; @@ -799,9 +799,6 @@ D44270EF1CC81B3200D84D28 /* Memory.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Memory.hpp; sourceTree = ""; usesTabs = 0; }; D44270F01CC81B3200D84D28 /* Path.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Path.cpp; sourceTree = ""; usesTabs = 0; }; D44270F11CC81B3200D84D28 /* Path.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Path.hpp; sourceTree = ""; usesTabs = 0; }; - D44270F21CC81B3200D84D28 /* Stopwatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Stopwatch.cpp; sourceTree = ""; usesTabs = 0; }; - D44270F31CC81B3200D84D28 /* stopwatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = stopwatch.h; sourceTree = ""; usesTabs = 1; wrapsLines = 1; }; - D44270F41CC81B3200D84D28 /* Stopwatch.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Stopwatch.hpp; sourceTree = ""; usesTabs = 0; }; D44270F51CC81B3200D84D28 /* String.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = String.cpp; sourceTree = ""; usesTabs = 0; }; D44270F61CC81B3200D84D28 /* String.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = String.hpp; sourceTree = ""; usesTabs = 0; }; D44270F71CC81B3200D84D28 /* StringBuilder.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = StringBuilder.hpp; sourceTree = ""; usesTabs = 0; }; @@ -970,7 +967,6 @@ D44271BB1CC81B3200D84D28 /* ride_list.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ride_list.c; sourceTree = ""; }; D44271BC1CC81B3200D84D28 /* save_prompt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = save_prompt.c; sourceTree = ""; }; D44271BD1CC81B3200D84D28 /* scenery.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = scenery.c; sourceTree = ""; }; - D44271BE1CC81B3200D84D28 /* server_list.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = server_list.c; sourceTree = ""; }; D44271BF1CC81B3200D84D28 /* server_start.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = server_start.c; sourceTree = ""; }; D44271C01CC81B3200D84D28 /* shortcut_key_change.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = shortcut_key_change.c; sourceTree = ""; }; D44271C11CC81B3200D84D28 /* shortcut_keys.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = shortcut_keys.c; sourceTree = ""; }; @@ -1724,9 +1720,6 @@ D464FEBD1D31A66E00CBABAC /* MemoryStream.h */, D44270F01CC81B3200D84D28 /* Path.cpp */, D44270F11CC81B3200D84D28 /* Path.hpp */, - D44270F21CC81B3200D84D28 /* Stopwatch.cpp */, - D44270F31CC81B3200D84D28 /* stopwatch.h */, - D44270F41CC81B3200D84D28 /* Stopwatch.hpp */, D44270F51CC81B3200D84D28 /* String.cpp */, D44270F61CC81B3200D84D28 /* String.hpp */, D44270F71CC81B3200D84D28 /* StringBuilder.hpp */, @@ -2007,7 +2000,7 @@ D44271B91CC81B3200D84D28 /* ride.c */, D44271BC1CC81B3200D84D28 /* save_prompt.c */, D44271BD1CC81B3200D84D28 /* scenery.c */, - D44271BE1CC81B3200D84D28 /* server_list.c */, + C68B2CB41EB60DDE0020651C /* server_list.cpp */, D44271BF1CC81B3200D84D28 /* server_start.c */, D44271C01CC81B3200D84D28 /* shortcut_key_change.c */, D44271C11CC81B3200D84D28 /* shortcut_keys.c */, @@ -2856,6 +2849,7 @@ D42E337E1E5C27D600D630AF /* IniReader.cpp in Sources */, D442723B1CC81B3200D84D28 /* crash.cpp in Sources */, C650B21A1CCABBDD00B4D91C /* tables.cpp in Sources */, + C68B2CB51EB60DDE0020651C /* server_list.cpp in Sources */, D44272291CC81B3200D84D28 /* LanguagePack.cpp in Sources */, D44272901CC81B3200D84D28 /* title_options.c in Sources */, C6CABA821E1466D600D33A6B /* FileClassifier.cpp in Sources */, @@ -2892,7 +2886,6 @@ D44272061CC81B3200D84D28 /* textinputbuffer.c in Sources */, C686F9191CDBC3B7009F9BFC /* mine_ride.c in Sources */, C650B2191CCABBDD00B4D91C /* S4Importer.cpp in Sources */, - D44272801CC81B3200D84D28 /* server_list.c in Sources */, D44272911CC81B3200D84D28 /* title_scenarioselect.c in Sources */, C686F8B61CDBC37E009F9BFC /* misc.c in Sources */, D44272411CC81B3200D84D28 /* rct1.c in Sources */, @@ -2951,7 +2944,6 @@ D44272401CC81B3200D84D28 /* windows.c in Sources */, D44272881CC81B3200D84D28 /* text_input.c in Sources */, D442720F1CC81B3200D84D28 /* scrolling_text.c in Sources */, - D44272041CC81B3200D84D28 /* Stopwatch.cpp in Sources */, D43407D81D0E14BE00C2B3D4 /* DrawImageShader.cpp in Sources */, 007A05D01CFB2C8B00F419C3 /* NetworkGroup.cpp in Sources */, 656F6C8E1E45BFC200E0F770 /* Version.cpp in Sources */, From ac2eb8b5da621a1c95191a81906af1bc072f4963 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sun, 30 Apr 2017 14:27:19 +0100 Subject: [PATCH 12/21] Fix FileStream for macOS --- src/openrct2/core/FileStream.hpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/openrct2/core/FileStream.hpp b/src/openrct2/core/FileStream.hpp index 1718e78906..683540308e 100644 --- a/src/openrct2/core/FileStream.hpp +++ b/src/openrct2/core/FileStream.hpp @@ -114,8 +114,10 @@ public: uint64 GetLength() const override { return _fileSize; } uint64 GetPosition() const override { -#ifdef _MSC_VER +#if defined(_MSC_VER) return _ftelli64(_file); +#elif (defined(__APPLE__) && defined(__MACH__)) + return ftello(_file); #else return ftello64(_file); #endif @@ -128,7 +130,7 @@ public: void Seek(sint64 offset, sint32 origin) override { -#ifdef _MSC_VER +#if defined(_MSC_VER) switch (origin) { case STREAM_SEEK_BEGIN: _fseeki64(_file, offset, SEEK_SET); @@ -140,6 +142,18 @@ public: _fseeki64(_file, offset, SEEK_END); break; } +#elif (defined(__APPLE__) && defined(__MACH__)) + switch (origin) { + case STREAM_SEEK_BEGIN: + fseeko(_file, offset, SEEK_SET); + break; + case STREAM_SEEK_CURRENT: + fseeko(_file, offset, SEEK_CUR); + break; + case STREAM_SEEK_END: + fseeko(_file, offset, SEEK_END); + break; + } #else switch (origin) { case STREAM_SEEK_BEGIN: From 6e443343a8a92041767de380283b2042f5307a5c Mon Sep 17 00:00:00 2001 From: Richard Jenkins Date: Thu, 4 May 2017 19:35:23 +0100 Subject: [PATCH 13/21] Update macOS deployment target to OS 10.8 --- OpenRCT2.xcodeproj/project.pbxproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OpenRCT2.xcodeproj/project.pbxproj b/OpenRCT2.xcodeproj/project.pbxproj index e16baf74a9..c95cfabaf2 100644 --- a/OpenRCT2.xcodeproj/project.pbxproj +++ b/OpenRCT2.xcodeproj/project.pbxproj @@ -3193,7 +3193,7 @@ GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; LD_NO_PIE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.7; + MACOSX_DEPLOYMENT_TARGET = 10.8; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; @@ -3239,7 +3239,7 @@ GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; LD_NO_PIE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.7; + MACOSX_DEPLOYMENT_TARGET = 10.8; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; }; From e1d964cb26817fcce326fc09f42e098a3cc9de17 Mon Sep 17 00:00:00 2001 From: Richard Jenkins Date: Thu, 4 May 2017 22:00:25 +0100 Subject: [PATCH 14/21] Implement platform_get_ticks() for macOS <10.12 --- src/openrct2/platform/shared.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/openrct2/platform/shared.c b/src/openrct2/platform/shared.c index 1c3b0c042c..bf1feff425 100644 --- a/src/openrct2/platform/shared.c +++ b/src/openrct2/platform/shared.c @@ -39,6 +39,16 @@ #include "../world/Climate.h" #include "platform.h" +#ifdef __APPLE__ + #include +#endif + +#if (defined __APPLE__ \ +&& __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200 \ +&& __clang_major__ >= 8) +#define MACOS_SIERRA_ONWARDS 1 +#endif + typedef void(*update_palette_func)(const uint8*, sint32, sint32); openrct2_cursor gCursorState; @@ -765,6 +775,15 @@ uint32 platform_get_ticks() { #ifdef _WIN32 return GetTickCount(); +#elif defined(__APPLE__) && !defined(MACOS_SIERRA_ONWARDS) + mach_timebase_info_data_t mach_base_info; + kern_return_t ret = mach_timebase_info(&mach_base_info); + if (ret == 0){ + return (uint32)(((mach_absolute_time() * mach_base_info.numer) / mach_base_info.denom) / 1000000); + } else{ + log_fatal("Unable to get mach_timebase_info."); + exit(-1); + } #else struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { From 3d6f13da13f7abc203a33d911b60a769041d3640 Mon Sep 17 00:00:00 2001 From: Richard Jenkins Date: Thu, 4 May 2017 22:10:00 +0100 Subject: [PATCH 15/21] Whitespace fix --- src/openrct2/platform/shared.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openrct2/platform/shared.c b/src/openrct2/platform/shared.c index bf1feff425..55e0e76f65 100644 --- a/src/openrct2/platform/shared.c +++ b/src/openrct2/platform/shared.c @@ -778,9 +778,9 @@ uint32 platform_get_ticks() #elif defined(__APPLE__) && !defined(MACOS_SIERRA_ONWARDS) mach_timebase_info_data_t mach_base_info; kern_return_t ret = mach_timebase_info(&mach_base_info); - if (ret == 0){ + if (ret == 0) { return (uint32)(((mach_absolute_time() * mach_base_info.numer) / mach_base_info.denom) / 1000000); - } else{ + } else { log_fatal("Unable to get mach_timebase_info."); exit(-1); } From f8b79a4a3f7118f7d56e218e298c0c00d7b770c7 Mon Sep 17 00:00:00 2001 From: Richard Jenkins Date: Thu, 4 May 2017 22:33:32 +0100 Subject: [PATCH 16/21] Cache mach_base_info --- src/openrct2/platform/shared.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/openrct2/platform/shared.c b/src/openrct2/platform/shared.c index 55e0e76f65..ee1ff74495 100644 --- a/src/openrct2/platform/shared.c +++ b/src/openrct2/platform/shared.c @@ -81,6 +81,11 @@ static float _gestureRadius; static void platform_create_window(); +#if defined(__APPLE__) && !defined(MACOS_SIERRA_ONWARDS) + mach_timebase_info_data_t mach_base_info = { 0 }; + bool mach_info_initialised = false; +#endif + static sint32 resolution_sort_func(const void *pa, const void *pb) { const resolution_t *a = (resolution_t*)pa; @@ -776,14 +781,15 @@ uint32 platform_get_ticks() #ifdef _WIN32 return GetTickCount(); #elif defined(__APPLE__) && !defined(MACOS_SIERRA_ONWARDS) - mach_timebase_info_data_t mach_base_info; - kern_return_t ret = mach_timebase_info(&mach_base_info); - if (ret == 0) { - return (uint32)(((mach_absolute_time() * mach_base_info.numer) / mach_base_info.denom) / 1000000); - } else { - log_fatal("Unable to get mach_timebase_info."); - exit(-1); + if (!mach_info_initialised) { + kern_return_t ret = mach_timebase_info(&mach_base_info); + if (ret != 0) { + log_fatal("Unable to get mach_timebase_info."); + exit(-1); + } + mach_info_initialised = true; } + return (uint32)(((mach_absolute_time() * mach_base_info.numer) / mach_base_info.denom) / 1000000); #else struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { From 28eb5fb279ba2d69301d4301e15a60ebceb245a5 Mon Sep 17 00:00:00 2001 From: Richard Jenkins Date: Fri, 5 May 2017 00:40:07 +0100 Subject: [PATCH 17/21] Move mach_timebase_info init to core_init() --- src/openrct2/platform/shared.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/openrct2/platform/shared.c b/src/openrct2/platform/shared.c index ee1ff74495..b25b2b838e 100644 --- a/src/openrct2/platform/shared.c +++ b/src/openrct2/platform/shared.c @@ -41,12 +41,7 @@ #ifdef __APPLE__ #include -#endif - -#if (defined __APPLE__ \ -&& __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200 \ -&& __clang_major__ >= 8) -#define MACOS_SIERRA_ONWARDS 1 + #include #endif typedef void(*update_palette_func)(const uint8*, sint32, sint32); @@ -82,8 +77,7 @@ static float _gestureRadius; static void platform_create_window(); #if defined(__APPLE__) && !defined(MACOS_SIERRA_ONWARDS) - mach_timebase_info_data_t mach_base_info = { 0 }; - bool mach_info_initialised = false; + mach_timebase_info_data_t _mach_base_info = { 0 }; #endif static sint32 resolution_sort_func(const void *pa, const void *pb) @@ -780,16 +774,8 @@ uint32 platform_get_ticks() { #ifdef _WIN32 return GetTickCount(); -#elif defined(__APPLE__) && !defined(MACOS_SIERRA_ONWARDS) - if (!mach_info_initialised) { - kern_return_t ret = mach_timebase_info(&mach_base_info); - if (ret != 0) { - log_fatal("Unable to get mach_timebase_info."); - exit(-1); - } - mach_info_initialised = true; - } - return (uint32)(((mach_absolute_time() * mach_base_info.numer) / mach_base_info.denom) / 1000000); +#elif defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200) + return (uint32)(((mach_absolute_time() * _mach_base_info.numer) / _mach_base_info.denom) / 1000000); #else struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { @@ -822,4 +808,12 @@ uint8 platform_get_currency_value(const char *currCode) { void core_init() { bitcount_init(); + +#if defined(__APPLE__) && !defined(MACOS_SIERRA_ONWARDS) + kern_return_t ret = mach_timebase_info(&_mach_base_info); + if (ret != 0) { + log_fatal("Unable to get mach_timebase_info."); + exit(-1); + } +#endif } From 7fc9bf62ce51a58a9112677581c6fbe9daea07e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Fri, 5 May 2017 08:46:03 +0200 Subject: [PATCH 18/21] Fix macros detecting macOS version --- src/openrct2/platform/shared.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/openrct2/platform/shared.c b/src/openrct2/platform/shared.c index b25b2b838e..cc9f92e33d 100644 --- a/src/openrct2/platform/shared.c +++ b/src/openrct2/platform/shared.c @@ -42,6 +42,9 @@ #ifdef __APPLE__ #include #include + #ifndef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ + #error Missing __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ define + #endif #endif typedef void(*update_palette_func)(const uint8*, sint32, sint32); @@ -76,7 +79,7 @@ static float _gestureRadius; static void platform_create_window(); -#if defined(__APPLE__) && !defined(MACOS_SIERRA_ONWARDS) +#if defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) mach_timebase_info_data_t _mach_base_info = { 0 }; #endif @@ -809,7 +812,7 @@ void core_init() { bitcount_init(); -#if defined(__APPLE__) && !defined(MACOS_SIERRA_ONWARDS) +#if defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) kern_return_t ret = mach_timebase_info(&_mach_base_info); if (ret != 0) { log_fatal("Unable to get mach_timebase_info."); From 03d111542571d4d8d77a97ad86753fb0ba6e9f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Fri, 5 May 2017 10:37:41 +0200 Subject: [PATCH 19/21] Fix macOS version detection macros again --- src/openrct2/platform/shared.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openrct2/platform/shared.c b/src/openrct2/platform/shared.c index cc9f92e33d..145f4e71c4 100644 --- a/src/openrct2/platform/shared.c +++ b/src/openrct2/platform/shared.c @@ -777,7 +777,7 @@ uint32 platform_get_ticks() { #ifdef _WIN32 return GetTickCount(); -#elif defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200) +#elif defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) return (uint32)(((mach_absolute_time() * _mach_base_info.numer) / _mach_base_info.denom) / 1000000); #else struct timespec ts; From a25ce1e646af9f40bef997f15e9a28197d29ce14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Fri, 5 May 2017 16:54:44 +0200 Subject: [PATCH 20/21] Make Network::server_connection dynamically instantiated --- src/openrct2/network/network.cpp | 56 +++++++++++++++++--------------- src/openrct2/network/network.h | 2 +- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/openrct2/network/network.cpp b/src/openrct2/network/network.cpp index f84b6b89fa..d608a6dbe4 100644 --- a/src/openrct2/network/network.cpp +++ b/src/openrct2/network/network.cpp @@ -133,6 +133,7 @@ bool Network::Init() status = NETWORK_STATUS_READY; + server_connection = new NetworkConnection(); ServerName = std::string(); ServerDescription = std::string(); ServerGreeting = std::string(); @@ -151,8 +152,8 @@ void Network::Close() return; } if (mode == NETWORK_MODE_CLIENT) { - delete server_connection.Socket; - server_connection.Socket = nullptr; + delete server_connection->Socket; + server_connection->Socket = nullptr; } else if (mode == NETWORK_MODE_SERVER) { delete listening_socket; listening_socket = nullptr; @@ -163,9 +164,10 @@ void Network::Close() mode = NETWORK_MODE_NONE; status = NETWORK_STATUS_NONE; _lastConnectStatus = SOCKET_STATUS_CLOSED; - server_connection.AuthStatus = NETWORK_AUTH_NONE; - server_connection.InboundPacket.Clear(); - server_connection.SetLastDisconnectReason(nullptr); + server_connection->AuthStatus = NETWORK_AUTH_NONE; + server_connection->InboundPacket.Clear(); + server_connection->SetLastDisconnectReason(nullptr); + SafeDelete(server_connection); client_connection_list.clear(); game_command_queue.clear(); @@ -190,9 +192,9 @@ bool Network::BeginClient(const char* host, uint16 port) mode = NETWORK_MODE_CLIENT; - assert(server_connection.Socket == nullptr); - server_connection.Socket = CreateTcpSocket(); - server_connection.Socket->ConnectAsync(host, port); + assert(server_connection->Socket == nullptr); + server_connection->Socket = CreateTcpSocket(); + server_connection->Socket->ConnectAsync(host, port); status = NETWORK_STATUS_CONNECTING; _lastConnectStatus = SOCKET_STATUS_CLOSED; @@ -337,7 +339,7 @@ sint32 Network::GetStatus() sint32 Network::GetAuthStatus() { if (GetMode() == NETWORK_MODE_CLIENT) { - return server_connection.AuthStatus; + return server_connection->AuthStatus; } else if (GetMode() == NETWORK_MODE_SERVER) { return NETWORK_AUTH_OK; @@ -403,7 +405,7 @@ void Network::UpdateClient() switch(status){ case NETWORK_STATUS_CONNECTING: { - switch (server_connection.Socket->GetStatus()) { + switch (server_connection->Socket->GetStatus()) { case SOCKET_STATUS_RESOLVING: { if (_lastConnectStatus != SOCKET_STATUS_RESOLVING) @@ -434,7 +436,7 @@ void Network::UpdateClient() case SOCKET_STATUS_CONNECTED: { status = NETWORK_STATUS_CONNECTED; - server_connection.ResetLastPacketTime(); + server_connection->ResetLastPacketTime(); Client_Send_TOKEN(); char str_authenticating[256]; format_string(str_authenticating, 256, STR_MULTIPLAYER_AUTHENTICATING, NULL); @@ -445,7 +447,7 @@ void Network::UpdateClient() } default: { - const char * error = server_connection.Socket->GetError(); + const char * error = server_connection->Socket->GetError(); if (error != nullptr) { Console::Error::WriteLine(error); } @@ -460,15 +462,15 @@ void Network::UpdateClient() } case NETWORK_STATUS_CONNECTED: { - if (!ProcessConnection(server_connection)) { + if (!ProcessConnection(*server_connection)) { // Do not show disconnect message window when password window closed/canceled - if (server_connection.AuthStatus == NETWORK_AUTH_REQUIREPASSWORD) { + if (server_connection->AuthStatus == NETWORK_AUTH_REQUIREPASSWORD) { window_network_status_close(); } else { char str_disconnected[256]; - if (server_connection.GetLastDisconnectReason()) { - const char * disconnect_reason = server_connection.GetLastDisconnectReason(); + if (server_connection->GetLastDisconnectReason()) { + const char * disconnect_reason = server_connection->GetLastDisconnectReason(); format_string(str_disconnected, 256, STR_MULTIPLAYER_DISCONNECTED_WITH_REASON, &disconnect_reason); } else { format_string(str_disconnected, 256, STR_MULTIPLAYER_DISCONNECTED_NO_REASON, NULL); @@ -609,7 +611,7 @@ void Network::SetPassword(const char* password) void Network::ShutdownClient() { if (GetMode() == NETWORK_MODE_CLIENT) { - server_connection.Socket->Disconnect(); + server_connection->Socket->Disconnect(); } } @@ -858,8 +860,8 @@ void Network::Client_Send_TOKEN() log_verbose("requesting token"); std::unique_ptr packet(NetworkPacket::Allocate()); *packet << (uint32)NETWORK_COMMAND_TOKEN; - server_connection.AuthStatus = NETWORK_AUTH_REQUESTED; - server_connection.QueuePacket(std::move(packet)); + server_connection->AuthStatus = NETWORK_AUTH_REQUESTED; + server_connection->QueuePacket(std::move(packet)); } void Network::Client_Send_AUTH(const char* name, const char* password, const char* pubkey, const char *sig, size_t sigsize) @@ -873,8 +875,8 @@ void Network::Client_Send_AUTH(const char* name, const char* password, const cha assert(sigsize <= (size_t)UINT32_MAX); *packet << (uint32)sigsize; packet->Write((const uint8 *)sig, sigsize); - server_connection.AuthStatus = NETWORK_AUTH_REQUESTED; - server_connection.QueuePacket(std::move(packet)); + server_connection->AuthStatus = NETWORK_AUTH_REQUESTED; + server_connection->QueuePacket(std::move(packet)); } void Network::Client_Send_OBJECTS(const std::vector &objects) @@ -887,7 +889,7 @@ void Network::Client_Send_OBJECTS(const std::vector &objects) log_verbose("client requests object %s", objects[i].c_str()); packet->Write((const uint8 *)objects[i].c_str(), 8); } - server_connection.QueuePacket(std::move(packet)); + server_connection->QueuePacket(std::move(packet)); } void Network::Server_Send_TOKEN(NetworkConnection& connection) @@ -1015,7 +1017,7 @@ void Network::Client_Send_CHAT(const char* text) std::unique_ptr packet(NetworkPacket::Allocate()); *packet << (uint32)NETWORK_COMMAND_CHAT; packet->WriteString(text); - server_connection.QueuePacket(std::move(packet)); + server_connection->QueuePacket(std::move(packet)); } void Network::Server_Send_CHAT(const char* text) @@ -1031,7 +1033,7 @@ void Network::Client_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx std::unique_ptr packet(NetworkPacket::Allocate()); *packet << (uint32)NETWORK_COMMAND_GAMECMD << (uint32)gCurrentTicks << eax << (ebx | GAME_COMMAND_FLAG_NETWORKED) << ecx << edx << esi << edi << ebp << callback; - server_connection.QueuePacket(std::move(packet)); + server_connection->QueuePacket(std::move(packet)); } void Network::Server_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 playerid, uint8 callback) @@ -1080,7 +1082,7 @@ void Network::Client_Send_PING() { std::unique_ptr packet(NetworkPacket::Allocate()); *packet << (uint32)NETWORK_COMMAND_PING; - server_connection.QueuePacket(std::move(packet)); + server_connection->QueuePacket(std::move(packet)); } void Network::Server_Send_PING() @@ -1963,7 +1965,7 @@ void Network::Client_Handle_PLAYERLIST(NetworkConnection& connection, NetworkPac if (player) { *player = tempplayer; if (player->Flags & NETWORK_PLAYER_FLAG_ISSERVER) { - server_connection.Player = player; + server_connection->Player = player; } } } @@ -2081,7 +2083,7 @@ void Network::Client_Send_GAMEINFO() log_verbose("requesting gameinfo"); std::unique_ptr packet(NetworkPacket::Allocate()); *packet << (uint32)NETWORK_COMMAND_GAMEINFO; - server_connection.QueuePacket(std::move(packet)); + server_connection->QueuePacket(std::move(packet)); } static std::string json_stdstring_value(const json_t * string) diff --git a/src/openrct2/network/network.h b/src/openrct2/network/network.h index 5b3b46e10a..536c5f7aa9 100644 --- a/src/openrct2/network/network.h +++ b/src/openrct2/network/network.h @@ -198,7 +198,7 @@ private: bool wsa_initialized = false; ITcpSocket * listening_socket = nullptr; uint16 listening_port = 0; - NetworkConnection server_connection; + NetworkConnection * server_connection = nullptr; SOCKET_STATUS _lastConnectStatus = SOCKET_STATUS_CLOSED; uint32 last_tick_sent_time = 0; uint32 last_ping_sent_time = 0; From 04b4ae429e481332a5fe60a606a729b7d24af350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Fri, 5 May 2017 22:25:47 +0200 Subject: [PATCH 21/21] Fix platform_get_ticks for POSIX Previously it did not include seconds --- src/openrct2/platform/shared.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openrct2/platform/shared.c b/src/openrct2/platform/shared.c index 145f4e71c4..119f174649 100644 --- a/src/openrct2/platform/shared.c +++ b/src/openrct2/platform/shared.c @@ -785,7 +785,7 @@ uint32 platform_get_ticks() log_fatal("clock_gettime failed"); exit(-1); } - return (uint32)(ts.tv_nsec / 1000000); + return (uint32)(ts.tv_sec * 1000 + ts.tv_nsec / 1000000); #endif }