2015-08-16 04:07:00 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* Copyright (c) 2014 Ted John
|
|
|
|
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
|
|
|
*
|
|
|
|
* This file is part of 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.
|
|
|
|
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2016-01-29 20:39:31 +01:00
|
|
|
#include "../config.h"
|
2015-11-04 00:31:09 +01:00
|
|
|
#include "../interface/colour.h"
|
2015-08-16 04:07:00 +02:00
|
|
|
#include "../interface/themes.h"
|
|
|
|
#include "../interface/widget.h"
|
|
|
|
#include "../interface/window.h"
|
|
|
|
#include "../localisation/localisation.h"
|
2015-11-04 00:31:09 +01:00
|
|
|
#include "../network/http.h"
|
2015-08-16 04:07:00 +02:00
|
|
|
#include "../network/network.h"
|
|
|
|
#include "../sprites.h"
|
2015-11-04 00:31:09 +01:00
|
|
|
#include "../windows/dropdown.h"
|
2016-01-30 22:28:51 +01:00
|
|
|
#include "../windows/tooltip.h"
|
2015-10-30 15:18:29 +01:00
|
|
|
#include "../util/util.h"
|
2015-08-16 17:36:57 +02:00
|
|
|
#include "error.h"
|
2015-08-16 04:07:00 +02:00
|
|
|
|
|
|
|
#define WWIDTH_MIN 500
|
|
|
|
#define WHEIGHT_MIN 300
|
|
|
|
#define WWIDTH_MAX 1200
|
|
|
|
#define WHEIGHT_MAX 800
|
2015-11-04 00:31:09 +01:00
|
|
|
#define ITEM_HEIGHT (3 + 9 + 3)
|
2015-08-16 04:07:00 +02:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *address;
|
|
|
|
utf8 *name;
|
2015-11-08 00:47:50 +01:00
|
|
|
bool requiresPassword;
|
2015-08-16 04:07:00 +02:00
|
|
|
utf8 *description;
|
2015-11-04 00:31:09 +01:00
|
|
|
char *version;
|
2016-03-01 19:51:01 +01:00
|
|
|
bool favourite;
|
2015-11-04 00:31:09 +01:00
|
|
|
uint8 players;
|
|
|
|
uint8 maxplayers;
|
2016-03-01 19:51:01 +01:00
|
|
|
} server_entry;
|
2015-08-16 04:07:00 +02:00
|
|
|
|
2016-03-01 19:45:30 +01:00
|
|
|
static char _playerName[32 + 1];
|
2016-03-01 19:51:01 +01:00
|
|
|
static server_entry *_severEntries = NULL;
|
|
|
|
static int _numServerEntries = 0;
|
2016-03-01 19:45:30 +01:00
|
|
|
static SDL_mutex *_mutex = 0;
|
|
|
|
static uint32 _numPlayersOnline = 0;
|
2015-08-16 04:07:00 +02:00
|
|
|
|
|
|
|
enum {
|
|
|
|
WIDX_BACKGROUND,
|
|
|
|
WIDX_TITLE,
|
|
|
|
WIDX_CLOSE,
|
|
|
|
WIDX_PLAYER_NAME_INPUT,
|
|
|
|
WIDX_LIST,
|
2015-11-04 00:31:09 +01:00
|
|
|
WIDX_FETCH_SERVERS,
|
2015-08-16 04:07:00 +02:00
|
|
|
WIDX_ADD_SERVER,
|
|
|
|
WIDX_START_SERVER
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
WIDX_LIST_REMOVE,
|
|
|
|
WIDX_LIST_SPECTATE
|
|
|
|
};
|
|
|
|
|
|
|
|
static rct_widget window_server_list_widgets[] = {
|
|
|
|
{ WWT_FRAME, 0, 0, 340, 0, 90, 0xFFFFFFFF, STR_NONE }, // panel / background
|
|
|
|
{ WWT_CAPTION, 0, 1, 338, 1, 14, STR_SERVER_LIST, STR_WINDOW_TITLE_TIP }, // title bar
|
|
|
|
{ WWT_CLOSEBOX, 0, 327, 337, 2, 13, STR_CLOSE_X, STR_CLOSE_WINDOW_TIP }, // close x button
|
|
|
|
{ WWT_TEXT_BOX, 1, 100, 344, 20, 31, (uint32)_playerName, STR_NONE }, // player name text box
|
|
|
|
{ WWT_SCROLL, 1, 6, 337, 37, 50, STR_NONE, STR_NONE }, // server list
|
2015-11-04 00:31:09 +01:00
|
|
|
{ WWT_DROPDOWN_BUTTON, 1, 6, 106, 53, 64, STR_FETCH_SERVERS, STR_NONE }, // fetch servers button
|
|
|
|
{ WWT_DROPDOWN_BUTTON, 1, 112, 212, 53, 64, STR_ADD_SERVER, STR_NONE }, // add server button
|
|
|
|
{ WWT_DROPDOWN_BUTTON, 1, 218, 318, 53, 64, STR_START_SERVER, STR_NONE }, // start server button
|
2015-08-16 04:07:00 +02:00
|
|
|
{ WIDGETS_END },
|
|
|
|
};
|
|
|
|
|
2015-08-16 12:42:41 +02:00
|
|
|
static void window_server_list_close(rct_window *w);
|
2015-08-16 04:07:00 +02:00
|
|
|
static void window_server_list_mouseup(rct_window *w, int widgetIndex);
|
|
|
|
static void window_server_list_resize(rct_window *w);
|
2015-11-04 00:31:09 +01:00
|
|
|
static void window_server_list_dropdown(rct_window *w, int widgetIndex, int dropdownIndex);
|
2015-08-16 04:07:00 +02:00
|
|
|
static void window_server_list_update(rct_window *w);
|
|
|
|
static void window_server_list_scroll_getsize(rct_window *w, int scrollIndex, int *width, int *height);
|
|
|
|
static void window_server_list_scroll_mousedown(rct_window *w, int scrollIndex, int x, int y);
|
|
|
|
static void window_server_list_scroll_mouseover(rct_window *w, int scrollIndex, int x, int y);
|
|
|
|
static void window_server_list_textinput(rct_window *w, int widgetIndex, char *text);
|
|
|
|
static void window_server_list_invalidate(rct_window *w);
|
|
|
|
static void window_server_list_paint(rct_window *w, rct_drawpixelinfo *dpi);
|
|
|
|
static void window_server_list_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scrollIndex);
|
|
|
|
|
|
|
|
static rct_window_event_list window_server_list_events = {
|
2015-08-16 12:42:41 +02:00
|
|
|
window_server_list_close,
|
2015-08-16 04:07:00 +02:00
|
|
|
window_server_list_mouseup,
|
|
|
|
window_server_list_resize,
|
|
|
|
NULL,
|
2015-11-04 00:31:09 +01:00
|
|
|
window_server_list_dropdown,
|
2015-08-16 04:07:00 +02:00
|
|
|
NULL,
|
|
|
|
window_server_list_update,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
window_server_list_scroll_getsize,
|
|
|
|
window_server_list_scroll_mousedown,
|
|
|
|
NULL,
|
|
|
|
window_server_list_scroll_mouseover,
|
|
|
|
window_server_list_textinput,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
window_server_list_invalidate,
|
|
|
|
window_server_list_paint,
|
|
|
|
window_server_list_scrollpaint
|
|
|
|
};
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
enum {
|
|
|
|
DDIDX_JOIN,
|
2016-03-01 22:25:21 +01:00
|
|
|
DDIDX_FAVOURITE
|
2015-11-04 00:31:09 +01:00
|
|
|
};
|
|
|
|
|
2015-08-16 04:07:00 +02:00
|
|
|
static int _hoverButtonIndex = -1;
|
|
|
|
|
2015-08-16 17:36:57 +02:00
|
|
|
static void server_list_get_item_button(int buttonIndex, int x, int y, int width, int *outX, int *outY);
|
2016-03-01 19:51:01 +01:00
|
|
|
static void server_list_load_server_entries();
|
|
|
|
static void server_list_save_server_entries();
|
|
|
|
static void dispose_server_entry_list();
|
|
|
|
static void dispose_server_entry(server_entry *serverInfo);
|
|
|
|
static server_entry* add_server_entry(char *address);
|
|
|
|
static void remove_server_entry(int index);
|
2015-11-04 00:31:09 +01:00
|
|
|
static void join_server(char *address);
|
|
|
|
static void fetch_servers();
|
|
|
|
#ifndef DISABLE_HTTP
|
|
|
|
static void fetch_servers_callback(http_json_response* response);
|
|
|
|
#endif
|
2015-08-16 12:42:41 +02:00
|
|
|
|
2015-08-16 04:07:00 +02:00
|
|
|
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;
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
if (_mutex == 0) {
|
|
|
|
_mutex = SDL_CreateMutex();
|
|
|
|
}
|
|
|
|
|
2015-08-16 04:07:00 +02:00
|
|
|
window = window_create_centred(WWIDTH_MIN, WHEIGHT_MIN, &window_server_list_events, WC_SERVER_LIST, WF_10 | WF_RESIZABLE);
|
|
|
|
|
|
|
|
window->widgets = window_server_list_widgets;
|
|
|
|
window->enabled_widgets = (
|
|
|
|
(1 << WIDX_CLOSE) |
|
|
|
|
(1 << WIDX_PLAYER_NAME_INPUT) |
|
2015-11-04 00:31:09 +01:00
|
|
|
(1 << WIDX_FETCH_SERVERS) |
|
2015-08-16 04:07:00 +02:00
|
|
|
(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] = 1;
|
|
|
|
window->colours[1] = 26;
|
|
|
|
window->colours[2] = 26;
|
|
|
|
|
|
|
|
window_set_resize(window, WWIDTH_MIN, WHEIGHT_MIN, WWIDTH_MAX, WHEIGHT_MAX);
|
|
|
|
|
2016-01-18 20:49:52 +01:00
|
|
|
safe_strcpy(_playerName, gConfigNetwork.player_name, sizeof(_playerName));
|
2015-08-16 12:42:41 +02:00
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
server_list_load_server_entries();
|
|
|
|
window->no_list_items = _numServerEntries;
|
2015-11-04 00:31:09 +01:00
|
|
|
|
|
|
|
fetch_servers();
|
2015-08-16 04:07:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 12:42:41 +02:00
|
|
|
static void window_server_list_close(rct_window *w)
|
|
|
|
{
|
2016-03-01 19:51:01 +01:00
|
|
|
dispose_server_entry_list();
|
2015-11-04 00:31:09 +01:00
|
|
|
if (_mutex) {
|
|
|
|
SDL_LockMutex(_mutex);
|
|
|
|
SDL_DestroyMutex(_mutex);
|
|
|
|
_mutex = 0;
|
|
|
|
}
|
2015-08-16 12:42:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 04:07:00 +02:00
|
|
|
static void window_server_list_mouseup(rct_window *w, int widgetIndex)
|
|
|
|
{
|
|
|
|
switch (widgetIndex) {
|
|
|
|
case WIDX_CLOSE:
|
|
|
|
window_close(w);
|
|
|
|
break;
|
|
|
|
case WIDX_PLAYER_NAME_INPUT:
|
|
|
|
window_start_textbox(w, widgetIndex, 1170, (uint32)_playerName, 63);
|
|
|
|
break;
|
2016-01-19 20:46:55 +01:00
|
|
|
case WIDX_LIST:{
|
|
|
|
int serverIndex = w->selected_list_item;
|
2016-03-01 19:51:01 +01:00
|
|
|
if (serverIndex >= 0 && serverIndex < _numServerEntries) {
|
2016-04-19 00:08:01 +02:00
|
|
|
if (strcmp(_severEntries[serverIndex].version, NETWORK_STREAM_ID) != 0) {
|
|
|
|
RCT2_GLOBAL(RCT2_ADDRESS_COMMON_FORMAT_ARGS, void *) = _severEntries[serverIndex].version;
|
|
|
|
window_error_open(STR_UNABLE_TO_CONNECT_TO_SERVER, STR_MULTIPLAYER_INCORRECT_SOFTWARE_VERSION);
|
|
|
|
break;
|
|
|
|
}
|
2016-03-01 19:51:01 +01:00
|
|
|
char *serverAddress = _severEntries[serverIndex].address;
|
2016-01-19 20:46:55 +01:00
|
|
|
join_server(serverAddress);
|
|
|
|
}
|
|
|
|
}break;
|
2015-11-04 00:31:09 +01:00
|
|
|
case WIDX_FETCH_SERVERS:
|
|
|
|
fetch_servers();
|
|
|
|
break;
|
2015-08-16 04:07:00 +02:00
|
|
|
case WIDX_ADD_SERVER:
|
|
|
|
window_text_input_open(w, widgetIndex, STR_ADD_SERVER, STR_ENTER_HOSTNAME_OR_IP_ADDRESS, STR_NONE, 0, 128);
|
|
|
|
break;
|
2015-08-16 17:36:57 +02:00
|
|
|
case WIDX_START_SERVER:
|
2015-11-02 01:28:53 +01:00
|
|
|
window_server_start_open();
|
2015-08-16 17:36:57 +02:00
|
|
|
break;
|
2015-08-16 04:07:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void window_server_list_resize(rct_window *w)
|
|
|
|
{
|
|
|
|
window_set_resize(w, WWIDTH_MIN, WHEIGHT_MIN, WWIDTH_MAX, WHEIGHT_MAX);
|
|
|
|
}
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
static void window_server_list_dropdown(rct_window *w, int widgetIndex, int dropdownIndex)
|
|
|
|
{
|
|
|
|
int serverIndex = w->selected_list_item;
|
|
|
|
if (serverIndex < 0) return;
|
2016-03-01 19:51:01 +01:00
|
|
|
if (serverIndex >= _numServerEntries) return;
|
2015-11-04 00:31:09 +01:00
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
char *serverAddress = _severEntries[serverIndex].address;
|
2015-11-04 00:31:09 +01:00
|
|
|
|
|
|
|
switch (dropdownIndex) {
|
|
|
|
case DDIDX_JOIN:
|
2016-04-19 00:08:01 +02:00
|
|
|
if (strcmp(_severEntries[serverIndex].version, NETWORK_STREAM_ID) != 0) {
|
|
|
|
RCT2_GLOBAL(RCT2_ADDRESS_COMMON_FORMAT_ARGS, void *) = _severEntries[serverIndex].version;
|
|
|
|
window_error_open(STR_UNABLE_TO_CONNECT_TO_SERVER, STR_MULTIPLAYER_INCORRECT_SOFTWARE_VERSION);
|
|
|
|
break;
|
|
|
|
}
|
2015-11-04 00:31:09 +01:00
|
|
|
join_server(serverAddress);
|
|
|
|
break;
|
2016-03-01 22:25:21 +01:00
|
|
|
case DDIDX_FAVOURITE:
|
2016-03-01 19:51:01 +01:00
|
|
|
_severEntries[serverIndex].favourite = !_severEntries[serverIndex].favourite;
|
|
|
|
server_list_save_server_entries();
|
2015-11-04 00:31:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 04:07:00 +02:00
|
|
|
static void window_server_list_update(rct_window *w)
|
|
|
|
{
|
|
|
|
if (gCurrentTextBox.window.classification == w->classification && gCurrentTextBox.window.number == w->number) {
|
|
|
|
window_update_textbox_caret();
|
|
|
|
widget_invalidate(w, WIDX_PLAYER_NAME_INPUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void window_server_list_scroll_getsize(rct_window *w, int scrollIndex, int *width, int *height)
|
|
|
|
{
|
|
|
|
*width = 0;
|
|
|
|
*height = w->no_list_items * ITEM_HEIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void window_server_list_scroll_mousedown(rct_window *w, int scrollIndex, int x, int y)
|
|
|
|
{
|
2015-08-16 17:36:57 +02:00
|
|
|
int serverIndex = w->selected_list_item;
|
|
|
|
if (serverIndex < 0) return;
|
2016-03-01 19:51:01 +01:00
|
|
|
if (serverIndex >= _numServerEntries) return;
|
2015-08-16 17:36:57 +02:00
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
char *serverAddress = _severEntries[serverIndex].address;
|
2015-10-20 20:16:30 +02:00
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
rct_widget *listWidget = &w->widgets[WIDX_LIST];
|
2016-01-19 21:11:16 +01:00
|
|
|
int ddx = w->x + listWidget->left + x + 2 - w->scrolls[0].h_left;
|
|
|
|
int ddy = w->y + listWidget->top + y + 2 - w->scrolls[0].v_top;
|
2015-11-04 00:31:09 +01:00
|
|
|
|
|
|
|
gDropdownItemsFormat[0] = STR_JOIN_GAME;
|
2016-03-01 19:51:01 +01:00
|
|
|
if (_severEntries[serverIndex].favourite) {
|
|
|
|
gDropdownItemsFormat[1] = STR_REMOVE_FROM_FAVOURITES;
|
2015-11-04 00:31:09 +01:00
|
|
|
} else {
|
2016-03-01 19:51:01 +01:00
|
|
|
gDropdownItemsFormat[1] = STR_ADD_TO_FAVOURITES;
|
2015-08-16 04:07:00 +02:00
|
|
|
}
|
2015-11-04 00:31:09 +01:00
|
|
|
window_dropdown_show_text(ddx, ddy, 0, COLOUR_GREY, 0, 2);
|
2015-08-16 04:07:00 +02:00
|
|
|
}
|
|
|
|
|
2016-01-30 22:28:51 +01:00
|
|
|
char *gVersion = NULL;
|
|
|
|
|
2015-08-16 04:07:00 +02:00
|
|
|
static void window_server_list_scroll_mouseover(rct_window *w, int scrollIndex, int x, int y)
|
|
|
|
{
|
|
|
|
// Item
|
|
|
|
int index = y / ITEM_HEIGHT;
|
|
|
|
if (index < 0 || index >= w->no_list_items) {
|
|
|
|
index = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hoverButtonIndex = -1;
|
|
|
|
if (index != -1) {
|
|
|
|
int width = w->widgets[WIDX_LIST].right - w->widgets[WIDX_LIST].left;
|
|
|
|
int sy = index * ITEM_HEIGHT;
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
int bx, by;
|
|
|
|
|
|
|
|
server_list_get_item_button(i, 0, sy, width, &bx, &by);
|
|
|
|
if (x >= bx && y >= by && x < bx + 24 && y < by + 24) {
|
|
|
|
hoverButtonIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 22:28:51 +01:00
|
|
|
int width = w->widgets[WIDX_LIST].right - w->widgets[WIDX_LIST].left;
|
|
|
|
int right = width - 3 - 14 - 10;
|
|
|
|
if (x < right)
|
|
|
|
{
|
|
|
|
w->widgets[WIDX_LIST].tooltip = STR_NONE;
|
|
|
|
window_tooltip_close();
|
|
|
|
}
|
|
|
|
|
2015-08-16 04:07:00 +02:00
|
|
|
if (w->selected_list_item != index || _hoverButtonIndex != hoverButtonIndex) {
|
|
|
|
w->selected_list_item = index;
|
|
|
|
_hoverButtonIndex = hoverButtonIndex;
|
2016-01-30 22:28:51 +01:00
|
|
|
window_tooltip_close();
|
2015-08-16 04:07:00 +02:00
|
|
|
window_invalidate(w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void window_server_list_textinput(rct_window *w, int widgetIndex, char *text)
|
|
|
|
{
|
2015-08-16 17:36:57 +02:00
|
|
|
if (text == NULL || text[0] == 0) return;
|
2015-08-16 04:07:00 +02:00
|
|
|
|
2015-08-16 17:36:57 +02:00
|
|
|
switch (widgetIndex) {
|
|
|
|
case WIDX_PLAYER_NAME_INPUT:
|
|
|
|
if (strcmp(_playerName, text) == 0)
|
|
|
|
return;
|
2015-08-16 04:07:00 +02:00
|
|
|
|
2015-08-16 17:36:57 +02:00
|
|
|
if (strlen(text) == 0) {
|
|
|
|
memset(_playerName, 0, sizeof(_playerName));
|
|
|
|
} else {
|
|
|
|
memset(_playerName, 0, sizeof(_playerName));
|
2016-01-18 20:49:52 +01:00
|
|
|
safe_strcpy(_playerName, text, sizeof(_playerName));
|
2015-08-16 17:36:57 +02:00
|
|
|
}
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
if (strlen(_playerName) > 0) {
|
|
|
|
SafeFree(gConfigNetwork.player_name);
|
|
|
|
gConfigNetwork.player_name = _strdup(_playerName);
|
|
|
|
config_save_default();
|
|
|
|
}
|
|
|
|
|
2015-08-16 17:36:57 +02:00
|
|
|
widget_invalidate(w, WIDX_PLAYER_NAME_INPUT);
|
|
|
|
break;
|
2015-08-16 04:07:00 +02:00
|
|
|
|
2015-08-16 17:36:57 +02:00
|
|
|
case WIDX_ADD_SERVER:
|
2016-03-01 19:51:01 +01:00
|
|
|
add_server_entry(text);
|
|
|
|
server_list_save_server_entries();
|
2015-08-16 17:36:57 +02:00
|
|
|
window_invalidate(w);
|
|
|
|
break;
|
|
|
|
}
|
2015-08-16 04:07:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void window_server_list_invalidate(rct_window *w)
|
|
|
|
{
|
2016-01-30 15:00:41 +01:00
|
|
|
colour_scheme_update(w);
|
|
|
|
|
2016-01-30 22:28:51 +01:00
|
|
|
RCT2_GLOBAL(RCT2_ADDRESS_COMMON_FORMAT_ARGS, char *) = gVersion;
|
2015-08-16 04:07:00 +02:00
|
|
|
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;
|
|
|
|
window_server_list_widgets[WIDX_CLOSE].left = w->width - 2 - 11;
|
|
|
|
window_server_list_widgets[WIDX_CLOSE].right = w->width - 2 - 11 + 10;
|
|
|
|
|
2016-03-01 19:45:30 +01:00
|
|
|
int margin = 6;
|
|
|
|
int buttonHeight = 11;
|
|
|
|
int buttonTop = w->height - margin - buttonHeight - 13;
|
|
|
|
int buttonBottom = buttonTop + buttonHeight;
|
|
|
|
int listBottom = buttonTop - margin;
|
|
|
|
|
2015-08-16 04:07:00 +02:00
|
|
|
window_server_list_widgets[WIDX_PLAYER_NAME_INPUT].right = w->width - 6;
|
|
|
|
window_server_list_widgets[WIDX_LIST].left = 6;
|
|
|
|
window_server_list_widgets[WIDX_LIST].right = w->width - 6;
|
2016-03-01 19:45:30 +01:00
|
|
|
window_server_list_widgets[WIDX_LIST].bottom = listBottom;
|
|
|
|
window_server_list_widgets[WIDX_FETCH_SERVERS].top = buttonTop;
|
|
|
|
window_server_list_widgets[WIDX_FETCH_SERVERS].bottom = buttonBottom;
|
|
|
|
window_server_list_widgets[WIDX_ADD_SERVER].top = buttonTop;
|
|
|
|
window_server_list_widgets[WIDX_ADD_SERVER].bottom = buttonBottom;
|
|
|
|
window_server_list_widgets[WIDX_START_SERVER].top = buttonTop;
|
|
|
|
window_server_list_widgets[WIDX_START_SERVER].bottom = buttonBottom;
|
2015-11-04 00:31:09 +01:00
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
w->no_list_items = _numServerEntries;
|
2015-08-16 04:07:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void window_server_list_paint(rct_window *w, rct_drawpixelinfo *dpi)
|
|
|
|
{
|
|
|
|
window_draw_widgets(w, dpi);
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
gfx_draw_string_left(dpi, STR_PLAYER_NAME, NULL, COLOUR_WHITE, w->x + 6, w->y + w->widgets[WIDX_PLAYER_NAME_INPUT].top);
|
2016-01-30 22:28:51 +01:00
|
|
|
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);
|
2016-03-01 19:45:30 +01:00
|
|
|
|
|
|
|
gfx_draw_string_left(dpi, STR_X_PLAYERS_ONLINE, (void*)&_numPlayersOnline, COLOUR_WHITE, w->x + 8, w->y + w->height - 15);
|
2015-08-16 04:07:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void window_server_list_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scrollIndex)
|
|
|
|
{
|
|
|
|
uint32 colour;
|
|
|
|
|
2015-11-08 19:55:32 +01:00
|
|
|
colour = ColourMapA[w->colours[1]].mid_light;
|
2015-08-16 04:07:00 +02:00
|
|
|
colour = (colour << 24) | (colour << 16) | (colour << 8) | colour;
|
|
|
|
gfx_clear(dpi, colour);
|
|
|
|
|
|
|
|
int width = w->widgets[WIDX_LIST].right - w->widgets[WIDX_LIST].left;
|
|
|
|
|
|
|
|
int y = 0;
|
2016-01-30 22:28:51 +01:00
|
|
|
w->widgets[WIDX_LIST].tooltip = STR_NONE;
|
2015-08-16 04:07:00 +02:00
|
|
|
for (int i = 0; i < w->no_list_items; i++) {
|
|
|
|
if (y >= dpi->y + dpi->height) continue;
|
|
|
|
// if (y + ITEM_HEIGHT < dpi->y) continue;
|
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
server_entry *serverDetails = &_severEntries[i];
|
2015-08-16 04:07:00 +02:00
|
|
|
bool highlighted = i == w->selected_list_item;
|
|
|
|
|
|
|
|
// Draw hover highlight
|
|
|
|
if (highlighted) {
|
|
|
|
gfx_fill_rect(dpi, 0, y, width, y + ITEM_HEIGHT, 0x02000031);
|
2016-01-30 22:28:51 +01:00
|
|
|
gVersion = serverDetails->version;
|
|
|
|
w->widgets[WIDX_LIST].tooltip = STR_NETWORK_VERSION_TIP;
|
2015-08-16 04:07:00 +02:00
|
|
|
}
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
int colour = w->colours[1];
|
2016-03-01 19:51:01 +01:00
|
|
|
if (serverDetails->favourite) {
|
2015-11-04 00:31:09 +01:00
|
|
|
colour = COLOUR_YELLOW;
|
|
|
|
}
|
|
|
|
|
2015-08-16 04:07:00 +02:00
|
|
|
// Draw server information
|
2015-12-29 02:01:26 +01:00
|
|
|
if (highlighted && !str_is_null_or_empty(serverDetails->description)) {
|
|
|
|
gfx_draw_string(dpi, serverDetails->description, colour, 3, y + 3);
|
2015-08-16 04:07:00 +02:00
|
|
|
} else {
|
2015-11-04 00:31:09 +01:00
|
|
|
gfx_draw_string(dpi, serverDetails->name, colour, 3, y + 3);
|
2015-08-16 04:07:00 +02:00
|
|
|
}
|
2015-11-27 19:54:59 +01:00
|
|
|
|
|
|
|
int right = width - 3 - 14;
|
|
|
|
|
|
|
|
// Draw compatibility icon
|
|
|
|
right -= 10;
|
|
|
|
int compatibilitySpriteId;
|
|
|
|
if (str_is_null_or_empty(serverDetails->version)) {
|
|
|
|
// Server not online...
|
|
|
|
compatibilitySpriteId = SPR_G2_RCT1_CLOSE_BUTTON_0;
|
|
|
|
} else {
|
|
|
|
// Server online... check version
|
2016-01-30 22:28:51 +01:00
|
|
|
bool correctVersion = strcmp(serverDetails->version, NETWORK_STREAM_ID) == 0;
|
2015-11-27 19:54:59 +01:00
|
|
|
compatibilitySpriteId = correctVersion ? SPR_G2_RCT1_OPEN_BUTTON_2 : SPR_G2_RCT1_CLOSE_BUTTON_2;
|
|
|
|
}
|
|
|
|
gfx_draw_sprite(dpi, compatibilitySpriteId, right, y + 1, 0);
|
|
|
|
right -= 4;
|
|
|
|
|
|
|
|
// Draw lock icon
|
|
|
|
right -= 8;
|
|
|
|
if (serverDetails->requiresPassword) {
|
|
|
|
gfx_draw_sprite(dpi, SPR_G2_LOCKED, right, y + 4, 0);
|
|
|
|
}
|
|
|
|
right -= 6;
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
// Draw number of players
|
|
|
|
char players[32];
|
|
|
|
players[0] = 0;
|
|
|
|
if (serverDetails->maxplayers > 0) {
|
|
|
|
sprintf(players, "%d/%d", serverDetails->players, serverDetails->maxplayers);
|
2015-08-16 04:07:00 +02:00
|
|
|
}
|
2015-11-27 19:54:59 +01:00
|
|
|
int numPlayersStringWidth = gfx_get_string_width(players);
|
|
|
|
gfx_draw_string(dpi, players, w->colours[1], right - numPlayersStringWidth, y + 3);
|
2015-08-16 04:07:00 +02:00
|
|
|
|
|
|
|
y += ITEM_HEIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server_list_get_item_button(int buttonIndex, int x, int y, int width, int *outX, int *outY)
|
|
|
|
{
|
|
|
|
*outX = width - 3 - 36 - (30 * buttonIndex);
|
|
|
|
*outY = y + 2;
|
|
|
|
}
|
2015-08-16 12:42:41 +02:00
|
|
|
|
2015-08-29 14:12:52 +02:00
|
|
|
static char *freadstralloc(SDL_RWops *file)
|
2015-08-16 12:42:41 +02:00
|
|
|
{
|
|
|
|
int capacity = 64;
|
|
|
|
char *buffer = malloc(capacity);
|
|
|
|
|
|
|
|
int length = 0;
|
|
|
|
int c;
|
|
|
|
for (;;) {
|
2015-08-29 14:12:52 +02:00
|
|
|
c = 0;
|
|
|
|
if (SDL_RWread(file, &c, 1, 1) != 1) break;
|
2015-08-16 12:42:41 +02:00
|
|
|
if (c == 0) break;
|
|
|
|
|
2016-04-01 19:57:18 +02:00
|
|
|
if (length >= capacity) {
|
2015-08-16 12:42:41 +02:00
|
|
|
capacity *= 2;
|
|
|
|
buffer = realloc(buffer, capacity);
|
|
|
|
}
|
|
|
|
buffer[length] = c;
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = realloc(buffer, length + 1);
|
|
|
|
buffer[length] = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
static void server_list_load_server_entries()
|
2015-08-16 12:42:41 +02:00
|
|
|
{
|
|
|
|
utf8 path[MAX_PATH];
|
2015-08-29 14:12:52 +02:00
|
|
|
SDL_RWops *file;
|
2015-08-16 12:42:41 +02:00
|
|
|
|
|
|
|
platform_get_user_directory(path, NULL);
|
|
|
|
strcat(path, "servers.cfg");
|
|
|
|
|
2015-08-29 14:12:52 +02:00
|
|
|
file = SDL_RWFromFile(path, "rb");
|
2015-08-16 12:42:41 +02:00
|
|
|
if (file == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
SDL_LockMutex(_mutex);
|
2016-03-01 19:51:01 +01:00
|
|
|
dispose_server_entry_list();
|
2015-08-16 12:42:41 +02:00
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
// Read number of server entries
|
|
|
|
SDL_RWread(file, &_numServerEntries, sizeof(uint32), 1);
|
|
|
|
_severEntries = malloc(_numServerEntries * sizeof(server_entry));
|
2015-10-20 20:16:30 +02:00
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
// Load each server entry
|
|
|
|
for (int i = 0; i < _numServerEntries; i++) {
|
|
|
|
server_entry *serverInfo = &_severEntries[i];
|
2015-08-16 12:42:41 +02:00
|
|
|
|
|
|
|
serverInfo->address = freadstralloc(file);
|
|
|
|
serverInfo->name = freadstralloc(file);
|
2015-11-08 00:47:50 +01:00
|
|
|
serverInfo->requiresPassword = false;
|
2015-08-16 12:42:41 +02:00
|
|
|
serverInfo->description = freadstralloc(file);
|
2015-11-04 00:31:09 +01:00
|
|
|
serverInfo->version = _strdup("");
|
2016-03-01 19:51:01 +01:00
|
|
|
serverInfo->favourite = true;
|
2015-11-04 00:31:09 +01:00
|
|
|
serverInfo->players = 0;
|
|
|
|
serverInfo->maxplayers = 0;
|
2015-08-16 12:42:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 14:12:52 +02:00
|
|
|
SDL_RWclose(file);
|
2015-11-04 00:31:09 +01:00
|
|
|
SDL_UnlockMutex(_mutex);
|
2015-08-16 12:42:41 +02:00
|
|
|
}
|
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
static void server_list_save_server_entries()
|
2015-08-16 12:42:41 +02:00
|
|
|
{
|
|
|
|
utf8 path[MAX_PATH];
|
2015-08-29 14:12:52 +02:00
|
|
|
SDL_RWops *file;
|
2015-08-16 12:42:41 +02:00
|
|
|
|
|
|
|
platform_get_user_directory(path, NULL);
|
|
|
|
strcat(path, "servers.cfg");
|
|
|
|
|
2015-08-29 14:12:52 +02:00
|
|
|
file = SDL_RWFromFile(path, "wb");
|
2015-08-16 12:42:41 +02:00
|
|
|
if (file == NULL) {
|
|
|
|
log_error("Unable to save servers.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
SDL_LockMutex(_mutex);
|
|
|
|
int count = 0;
|
2016-03-01 19:51:01 +01:00
|
|
|
for (int i = 0; i < _numServerEntries; i++) {
|
|
|
|
server_entry *serverInfo = &_severEntries[i];
|
|
|
|
if (serverInfo->favourite) {
|
2015-11-04 00:31:09 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 19:51:01 +01:00
|
|
|
// Write number of server entries
|
2015-11-04 00:31:09 +01:00
|
|
|
SDL_RWwrite(file, &count, sizeof(uint32), 1);
|
2015-08-16 12:42:41 +02:00
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
// Write each server entry
|
|
|
|
for (int i = 0; i < _numServerEntries; i++) {
|
|
|
|
server_entry *serverInfo = &_severEntries[i];
|
|
|
|
if (serverInfo->favourite) {
|
2015-11-04 00:31:09 +01:00
|
|
|
SDL_RWwrite(file, serverInfo->address, strlen(serverInfo->address) + 1, 1);
|
|
|
|
SDL_RWwrite(file, serverInfo->name, strlen(serverInfo->name) + 1, 1);
|
|
|
|
SDL_RWwrite(file, serverInfo->description, strlen(serverInfo->description) + 1, 1);
|
|
|
|
}
|
2015-08-16 12:42:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 14:12:52 +02:00
|
|
|
SDL_RWclose(file);
|
2015-11-04 00:31:09 +01:00
|
|
|
SDL_UnlockMutex(_mutex);
|
2015-08-16 12:42:41 +02:00
|
|
|
}
|
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
static void dispose_server_entry_list()
|
2015-08-16 12:42:41 +02:00
|
|
|
{
|
2015-11-04 00:31:09 +01:00
|
|
|
SDL_LockMutex(_mutex);
|
2016-03-01 19:51:01 +01:00
|
|
|
if (_severEntries != NULL) {
|
|
|
|
for (int i = 0; i < _numServerEntries; i++) {
|
|
|
|
dispose_server_entry(&_severEntries[i]);
|
2015-08-16 12:42:41 +02:00
|
|
|
}
|
2016-03-01 19:51:01 +01:00
|
|
|
free(_severEntries);
|
|
|
|
_severEntries = NULL;
|
2015-08-16 12:42:41 +02:00
|
|
|
}
|
2016-03-01 19:51:01 +01:00
|
|
|
_numServerEntries = 0;
|
2015-11-04 00:31:09 +01:00
|
|
|
SDL_UnlockMutex(_mutex);
|
2015-08-16 12:42:41 +02:00
|
|
|
}
|
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
static void dispose_server_entry(server_entry *serverInfo)
|
2015-08-16 12:42:41 +02:00
|
|
|
{
|
|
|
|
SafeFree(serverInfo->address);
|
|
|
|
SafeFree(serverInfo->name);
|
|
|
|
SafeFree(serverInfo->description);
|
2015-11-04 00:31:09 +01:00
|
|
|
SafeFree(serverInfo->version);
|
2015-08-16 12:42:41 +02:00
|
|
|
}
|
2015-08-16 17:36:57 +02:00
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
static server_entry* add_server_entry(char *address)
|
2015-08-16 17:36:57 +02:00
|
|
|
{
|
2015-11-04 00:31:09 +01:00
|
|
|
SDL_LockMutex(_mutex);
|
2016-03-01 19:51:01 +01:00
|
|
|
for (int i = 0; i < _numServerEntries; i++) {
|
|
|
|
if (strcmp(_severEntries[i].address, address) == 0) {
|
2015-11-04 00:31:09 +01:00
|
|
|
SDL_UnlockMutex(_mutex);
|
2016-03-01 19:51:01 +01:00
|
|
|
return &_severEntries[i];
|
2015-11-04 00:31:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
_numServerEntries++;
|
|
|
|
if (_severEntries == NULL) {
|
|
|
|
_severEntries = malloc(_numServerEntries * sizeof(server_entry));
|
2015-08-16 17:36:57 +02:00
|
|
|
} else {
|
2016-03-01 19:51:01 +01:00
|
|
|
_severEntries = realloc(_severEntries, _numServerEntries * sizeof(server_entry));
|
2015-08-16 17:36:57 +02:00
|
|
|
}
|
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
int index = _numServerEntries - 1;
|
|
|
|
server_entry* newserver = &_severEntries[index];
|
2015-11-04 00:31:09 +01:00
|
|
|
newserver->address = _strdup(address);
|
|
|
|
newserver->name = _strdup(address);
|
2015-11-08 00:47:50 +01:00
|
|
|
newserver->requiresPassword = false;
|
2015-11-04 00:31:09 +01:00
|
|
|
newserver->description = _strdup("");
|
|
|
|
newserver->version = _strdup("");
|
2016-03-01 19:51:01 +01:00
|
|
|
newserver->favourite = false;
|
2015-11-04 00:31:09 +01:00
|
|
|
newserver->players = 0;
|
|
|
|
newserver->maxplayers = 0;
|
|
|
|
SDL_UnlockMutex(_mutex);
|
|
|
|
return newserver;
|
2015-08-16 17:36:57 +02:00
|
|
|
}
|
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
static void remove_server_entry(int index)
|
2015-08-16 17:36:57 +02:00
|
|
|
{
|
2015-11-04 00:31:09 +01:00
|
|
|
SDL_LockMutex(_mutex);
|
2016-03-01 19:51:01 +01:00
|
|
|
if (_numServerEntries > index) {
|
|
|
|
int serversToMove = _numServerEntries - index - 1;
|
|
|
|
memmove(&_severEntries[index], &_severEntries[index + 1], serversToMove * sizeof(server_entry));
|
2015-08-16 17:36:57 +02:00
|
|
|
|
2016-03-01 19:51:01 +01:00
|
|
|
_numServerEntries--;
|
|
|
|
_severEntries = realloc(_severEntries, _numServerEntries * sizeof(server_entry));
|
2015-11-04 00:31:09 +01:00
|
|
|
}
|
|
|
|
SDL_UnlockMutex(_mutex);
|
2015-08-16 17:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *substr(char *start, int length)
|
|
|
|
{
|
|
|
|
char *result = malloc(length + 1);
|
|
|
|
memcpy(result, start, length);
|
|
|
|
result[length] = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
static void join_server(char *address)
|
2015-08-16 17:36:57 +02:00
|
|
|
{
|
|
|
|
int port = gConfigNetwork.default_port;
|
|
|
|
|
2015-10-27 04:43:33 +01:00
|
|
|
bool addresscopied = false;
|
|
|
|
|
|
|
|
char *endbracket = strrchr(address, ']');
|
|
|
|
char *startbracket = strrchr(address, '[');
|
|
|
|
char *dot = strchr(address, '.');
|
|
|
|
|
|
|
|
char *colon = strrchr(address, ':');
|
|
|
|
if (colon != NULL && (endbracket != NULL || dot != NULL)) {
|
2015-08-16 17:36:57 +02:00
|
|
|
address = substr(address, colon - address);
|
|
|
|
sscanf(colon + 1, "%d", &port);
|
2015-10-27 04:43:33 +01:00
|
|
|
addresscopied = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startbracket && endbracket) {
|
|
|
|
address = substr(startbracket + 1, endbracket - startbracket - 1);
|
|
|
|
addresscopied = true;
|
2015-08-16 17:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!network_begin_client(address, port)) {
|
|
|
|
window_error_open(STR_UNABLE_TO_CONNECT_TO_SERVER, STR_NONE);
|
|
|
|
}
|
|
|
|
|
2015-10-27 04:43:33 +01:00
|
|
|
if (addresscopied) {
|
2015-08-16 17:36:57 +02:00
|
|
|
free(address);
|
|
|
|
}
|
|
|
|
}
|
2015-11-04 00:31:09 +01:00
|
|
|
|
2016-03-01 19:45:30 +01:00
|
|
|
static uint32 get_total_player_count()
|
|
|
|
{
|
|
|
|
uint32 numPlayers = 0;
|
2016-03-01 19:51:01 +01:00
|
|
|
for (int i = 0; i < _numServerEntries; i++) {
|
|
|
|
server_entry *serverDetails = &_severEntries[i];
|
2016-03-01 19:45:30 +01:00
|
|
|
numPlayers += serverDetails->players;
|
|
|
|
}
|
|
|
|
return numPlayers;
|
|
|
|
}
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
static void fetch_servers()
|
|
|
|
{
|
|
|
|
#ifndef DISABLE_HTTP
|
2015-11-08 00:56:16 +01:00
|
|
|
const char *masterServerUrl = OPENRCT2_MASTER_SERVER_URL;
|
|
|
|
if (!str_is_null_or_empty(gConfigNetwork.master_server_url)) {
|
|
|
|
masterServerUrl = gConfigNetwork.master_server_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_LockMutex(_mutex);
|
2016-03-01 19:51:01 +01:00
|
|
|
for (int i = 0; i < _numServerEntries; i++) {
|
|
|
|
if (!_severEntries[i].favourite) {
|
|
|
|
remove_server_entry(i);
|
2015-11-08 00:56:16 +01:00
|
|
|
i = 0;
|
2015-11-04 00:31:09 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-08 00:56:16 +01:00
|
|
|
SDL_UnlockMutex(_mutex);
|
2015-11-08 23:03:35 +01:00
|
|
|
|
|
|
|
http_json_request request;
|
|
|
|
request.url = masterServerUrl;
|
|
|
|
request.method = HTTP_METHOD_GET;
|
|
|
|
request.body = NULL;
|
|
|
|
http_request_json_async(&request, fetch_servers_callback);
|
2015-11-04 00:31:09 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef DISABLE_HTTP
|
|
|
|
static void fetch_servers_callback(http_json_response* response)
|
|
|
|
{
|
2015-11-08 03:12:12 +01:00
|
|
|
if (response == NULL) {
|
|
|
|
log_warning("Unable to connect to master server");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_t *jsonStatus = json_object_get(response->root, "status");
|
|
|
|
if (!json_is_number(jsonStatus)) {
|
2015-11-04 00:31:09 +01:00
|
|
|
http_request_json_dispose(response);
|
2015-11-08 03:12:12 +01:00
|
|
|
log_warning("Invalid response from master server");
|
|
|
|
return;
|
2015-11-04 00:31:09 +01:00
|
|
|
}
|
2015-11-08 03:12:12 +01:00
|
|
|
|
|
|
|
int status = (int)json_integer_value(jsonStatus);
|
|
|
|
if (status != 200) {
|
|
|
|
http_request_json_dispose(response);
|
|
|
|
log_warning("Master server failed to return servers");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_t *jsonServers = json_object_get(response->root, "servers");
|
|
|
|
if (!json_is_array(jsonServers)) {
|
|
|
|
http_request_json_dispose(response);
|
|
|
|
log_warning("Invalid response from master server");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-08 04:55:30 +01:00
|
|
|
int count = json_array_size(jsonServers);
|
2015-11-08 03:12:12 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2015-11-08 04:55:30 +01:00
|
|
|
json_t *server = json_array_get(jsonServers, i);
|
2015-11-08 03:12:12 +01:00
|
|
|
if (!json_is_object(server)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-08 04:55:30 +01:00
|
|
|
json_t *port = json_object_get(server, "port");
|
2015-11-08 03:12:12 +01:00
|
|
|
json_t *name = json_object_get(server, "name");
|
2015-11-08 04:55:30 +01:00
|
|
|
json_t *description = json_object_get(server, "description");
|
2015-11-08 03:12:12 +01:00
|
|
|
json_t *requiresPassword = json_object_get(server, "requiresPassword");
|
|
|
|
json_t *version = json_object_get(server, "version");
|
|
|
|
json_t *players = json_object_get(server, "players");
|
2015-11-08 04:55:30 +01:00
|
|
|
json_t *maxPlayers = json_object_get(server, "maxPlayers");
|
|
|
|
json_t *ip = json_object_get(server, "ip");
|
|
|
|
json_t *ip4 = json_object_get(ip, "v4");
|
|
|
|
json_t *ip6 = json_object_get(ip, "v6");
|
|
|
|
json_t *addressIp = json_array_get(ip4, 0);
|
|
|
|
|
2015-12-21 22:07:25 +01:00
|
|
|
if (name == NULL || version == NULL)
|
|
|
|
{
|
|
|
|
log_verbose("Cowardly refusing to add server without name or version specified.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-08 04:55:30 +01:00
|
|
|
char address[256];
|
|
|
|
snprintf(address, sizeof(address), "%s:%d", json_string_value(addressIp), (int)json_integer_value(port));
|
2015-11-08 03:12:12 +01:00
|
|
|
|
|
|
|
SDL_LockMutex(_mutex);
|
2016-03-01 19:51:01 +01:00
|
|
|
server_entry* newserver = add_server_entry(address);
|
2015-11-08 03:12:12 +01:00
|
|
|
SafeFree(newserver->name);
|
2015-11-08 04:55:30 +01:00
|
|
|
SafeFree(newserver->description);
|
|
|
|
SafeFree(newserver->version);
|
2015-11-08 03:12:12 +01:00
|
|
|
newserver->name = _strdup(json_string_value(name));
|
|
|
|
newserver->requiresPassword = json_boolean_value(requiresPassword);
|
2015-12-21 22:07:25 +01:00
|
|
|
newserver->description = _strdup(description == NULL ? "" : json_string_value(description));
|
2015-11-08 03:12:12 +01:00
|
|
|
newserver->version = _strdup(json_string_value(version));
|
|
|
|
newserver->players = (uint8)json_integer_value(players);
|
2015-11-08 04:55:30 +01:00
|
|
|
newserver->maxplayers = (uint8)json_integer_value(maxPlayers);
|
2015-11-08 03:12:12 +01:00
|
|
|
SDL_UnlockMutex(_mutex);
|
|
|
|
}
|
|
|
|
http_request_json_dispose(response);
|
|
|
|
|
2016-03-01 19:45:30 +01:00
|
|
|
_numPlayersOnline = get_total_player_count();
|
|
|
|
|
2015-11-08 23:03:35 +01:00
|
|
|
rct_window *window = window_find_by_class(WC_SERVER_LIST);
|
2015-11-04 00:31:09 +01:00
|
|
|
if (window != NULL) {
|
|
|
|
window_invalidate(window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|