Merge pull request #3350 from janisozaur/network-version

Network version
This commit is contained in:
Ted John 2016-04-19 21:08:30 +01:00
commit 6c6662a3ee
5 changed files with 51 additions and 35 deletions

View File

@ -4060,7 +4060,7 @@ STR_5751 :No Data
STR_5752 :{OUTLINE}{RED}{STRING} has disconnected
STR_5753 :{OUTLINE}{RED}{STRING} has disconnected ({STRING})
STR_5754 :Bad Player Name
STR_5755 :Incorrect Software Version
STR_5755 :Incorrect Software Version (Server is using {STRING})
STR_5756 :Bad Password
STR_5757 :Server Full
STR_5758 :{OUTLINE}{GREEN}{STRING} has joined the game

View File

@ -478,10 +478,10 @@ void NetworkConnection::setLastDisconnectReason(const char *src)
strncpy(last_disconnect_reason, src, NETWORK_DISCONNECT_REASON_BUFFER_SIZE - 1);
}
void NetworkConnection::setLastDisconnectReason(const rct_string_id string_id)
void NetworkConnection::setLastDisconnectReason(const rct_string_id string_id, void *args)
{
char buffer[NETWORK_DISCONNECT_REASON_BUFFER_SIZE];
format_string(buffer, string_id, NULL);
format_string(buffer, string_id, args);
setLastDisconnectReason(buffer);
}
@ -1328,6 +1328,9 @@ void Network::Server_Send_AUTH(NetworkConnection& connection)
}
std::unique_ptr<NetworkPacket> packet = std::move(NetworkPacket::Allocate());
*packet << (uint32)NETWORK_COMMAND_AUTH << (uint32)connection.authstatus << (uint8)new_playerid;
if (connection.authstatus == NETWORK_AUTH_BADVERSION) {
packet->WriteString(NETWORK_STREAM_ID);
}
connection.QueuePacket(std::move(packet));
if (connection.authstatus != NETWORK_AUTH_OK && connection.authstatus != NETWORK_AUTH_REQUIREPASSWORD) {
shutdown(connection.socket, SHUT_RD);
@ -1696,9 +1699,12 @@ void Network::Client_Handle_AUTH(NetworkConnection& connection, NetworkPacket& p
shutdown(connection.socket, SHUT_RDWR);
break;
case NETWORK_AUTH_BADVERSION:
connection.setLastDisconnectReason(STR_MULTIPLAYER_INCORRECT_SOFTWARE_VERSION);
{
const char *version = packet.ReadString();
connection.setLastDisconnectReason(STR_MULTIPLAYER_INCORRECT_SOFTWARE_VERSION, &version);
shutdown(connection.socket, SHUT_RDWR);
break;
}
case NETWORK_AUTH_BADPASSWORD:
connection.setLastDisconnectReason(STR_MULTIPLAYER_BAD_PASSWORD);
shutdown(connection.socket, SHUT_RDWR);

View File

@ -245,7 +245,7 @@ public:
const char *getLastDisconnectReason() const;
void setLastDisconnectReason(const char *src);
void setLastDisconnectReason(const rct_string_id string_id);
void setLastDisconnectReason(const rct_string_id string_id, void *args = nullptr);
SOCKET socket = INVALID_SOCKET;
NetworkPacket inboundpacket;

View File

@ -35,9 +35,9 @@ enum WINDOW_NETWORK_STATUS_WIDGET_IDX {
};
static rct_widget window_network_status_widgets[] = {
{ WWT_FRAME, 0, 0, 340, 0, 90, 0x0FFFFFFFF, STR_NONE }, // panel / background
{ WWT_CAPTION, 0, 1, 338, 1, 14, STR_NONE, STR_WINDOW_TITLE_TIP }, // title bar
{ WWT_CLOSEBOX, 0, 327, 337, 2, 13, STR_CLOSE_X, STR_CLOSE_WINDOW_TIP }, // close x button
{ WWT_FRAME, 0, 0, 440, 0, 90, 0x0FFFFFFFF, STR_NONE }, // panel / background
{ WWT_CAPTION, 0, 1, 438, 1, 14, STR_NONE, STR_WINDOW_TITLE_TIP }, // title bar
{ WWT_CLOSEBOX, 0, 427, 437, 2, 13, STR_CLOSE_X, STR_CLOSE_WINDOW_TIP }, // close x button
{ WIDGETS_END },
};
@ -93,7 +93,7 @@ void window_network_status_open(const char* text, close_callback onClose)
if (window != NULL)
return;
window = window_create_centred(320, 90, &window_network_status_events, WC_NETWORK_STATUS, WF_10 | WF_TRANSPARENT);
window = window_create_centred(420, 90, &window_network_status_events, WC_NETWORK_STATUS, WF_10 | WF_TRANSPARENT);
window->widgets = window_network_status_widgets;
window->enabled_widgets = 1 << WIDX_CLOSE;
@ -182,7 +182,7 @@ static void window_network_status_paint(rct_window *w, rct_drawpixelinfo *dpi)
char* lineCh = buffer;
lineCh = utf8_write_codepoint(lineCh, FORMAT_BLACK);
strcpy(lineCh, window_network_status_text);
gfx_clip_string(buffer, 230);
gfx_clip_string(buffer, w->widgets[WIDX_BACKGROUND].right - 50);
int x = w->x + (w->width / 2);
int y = w->y + (w->height / 2);
x -= gfx_get_string_width(buffer) / 2;

View File

@ -50,7 +50,7 @@ typedef struct {
} server_entry;
static char _playerName[32 + 1];
static server_entry *_severEntries = NULL;
static server_entry *_serverEntries = NULL;
static int _numServerEntries = 0;
static SDL_mutex *_mutex = 0;
static uint32 _numPlayersOnline = 0;
@ -217,7 +217,12 @@ static void window_server_list_mouseup(rct_window *w, int widgetIndex)
case WIDX_LIST:{
int serverIndex = w->selected_list_item;
if (serverIndex >= 0 && serverIndex < _numServerEntries) {
char *serverAddress = _severEntries[serverIndex].address;
if (strcmp(_serverEntries[serverIndex].version, NETWORK_STREAM_ID) != 0) {
RCT2_GLOBAL(RCT2_ADDRESS_COMMON_FORMAT_ARGS, void *) = _serverEntries[serverIndex].version;
window_error_open(STR_UNABLE_TO_CONNECT_TO_SERVER, STR_MULTIPLAYER_INCORRECT_SOFTWARE_VERSION);
break;
}
char *serverAddress = _serverEntries[serverIndex].address;
join_server(serverAddress);
}
}break;
@ -244,14 +249,19 @@ static void window_server_list_dropdown(rct_window *w, int widgetIndex, int drop
if (serverIndex < 0) return;
if (serverIndex >= _numServerEntries) return;
char *serverAddress = _severEntries[serverIndex].address;
char *serverAddress = _serverEntries[serverIndex].address;
switch (dropdownIndex) {
case DDIDX_JOIN:
if (strcmp(_serverEntries[serverIndex].version, NETWORK_STREAM_ID) != 0) {
RCT2_GLOBAL(RCT2_ADDRESS_COMMON_FORMAT_ARGS, void *) = _serverEntries[serverIndex].version;
window_error_open(STR_UNABLE_TO_CONNECT_TO_SERVER, STR_MULTIPLAYER_INCORRECT_SOFTWARE_VERSION);
break;
}
join_server(serverAddress);
break;
case DDIDX_FAVOURITE:
_severEntries[serverIndex].favourite = !_severEntries[serverIndex].favourite;
_serverEntries[serverIndex].favourite = !_serverEntries[serverIndex].favourite;
server_list_save_server_entries();
break;
}
@ -277,14 +287,14 @@ static void window_server_list_scroll_mousedown(rct_window *w, int scrollIndex,
if (serverIndex < 0) return;
if (serverIndex >= _numServerEntries) return;
char *serverAddress = _severEntries[serverIndex].address;
char *serverAddress = _serverEntries[serverIndex].address;
rct_widget *listWidget = &w->widgets[WIDX_LIST];
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;
gDropdownItemsFormat[0] = STR_JOIN_GAME;
if (_severEntries[serverIndex].favourite) {
if (_serverEntries[serverIndex].favourite) {
gDropdownItemsFormat[1] = STR_REMOVE_FROM_FAVOURITES;
} else {
gDropdownItemsFormat[1] = STR_ADD_TO_FAVOURITES;
@ -424,7 +434,7 @@ static void window_server_list_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi
if (y >= dpi->y + dpi->height) continue;
// if (y + ITEM_HEIGHT < dpi->y) continue;
server_entry *serverDetails = &_severEntries[i];
server_entry *serverDetails = &_serverEntries[i];
bool highlighted = i == w->selected_list_item;
// Draw hover highlight
@ -531,11 +541,11 @@ static void server_list_load_server_entries()
// Read number of server entries
SDL_RWread(file, &_numServerEntries, sizeof(uint32), 1);
_severEntries = malloc(_numServerEntries * sizeof(server_entry));
_serverEntries = malloc(_numServerEntries * sizeof(server_entry));
// Load each server entry
for (int i = 0; i < _numServerEntries; i++) {
server_entry *serverInfo = &_severEntries[i];
server_entry *serverInfo = &_serverEntries[i];
serverInfo->address = freadstralloc(file);
serverInfo->name = freadstralloc(file);
@ -568,7 +578,7 @@ static void server_list_save_server_entries()
SDL_LockMutex(_mutex);
int count = 0;
for (int i = 0; i < _numServerEntries; i++) {
server_entry *serverInfo = &_severEntries[i];
server_entry *serverInfo = &_serverEntries[i];
if (serverInfo->favourite) {
count++;
}
@ -578,7 +588,7 @@ static void server_list_save_server_entries()
// Write each server entry
for (int i = 0; i < _numServerEntries; i++) {
server_entry *serverInfo = &_severEntries[i];
server_entry *serverInfo = &_serverEntries[i];
if (serverInfo->favourite) {
SDL_RWwrite(file, serverInfo->address, strlen(serverInfo->address) + 1, 1);
SDL_RWwrite(file, serverInfo->name, strlen(serverInfo->name) + 1, 1);
@ -593,12 +603,12 @@ static void server_list_save_server_entries()
static void dispose_server_entry_list()
{
SDL_LockMutex(_mutex);
if (_severEntries != NULL) {
if (_serverEntries != NULL) {
for (int i = 0; i < _numServerEntries; i++) {
dispose_server_entry(&_severEntries[i]);
dispose_server_entry(&_serverEntries[i]);
}
free(_severEntries);
_severEntries = NULL;
free(_serverEntries);
_serverEntries = NULL;
}
_numServerEntries = 0;
SDL_UnlockMutex(_mutex);
@ -616,21 +626,21 @@ static server_entry* add_server_entry(char *address)
{
SDL_LockMutex(_mutex);
for (int i = 0; i < _numServerEntries; i++) {
if (strcmp(_severEntries[i].address, address) == 0) {
if (strcmp(_serverEntries[i].address, address) == 0) {
SDL_UnlockMutex(_mutex);
return &_severEntries[i];
return &_serverEntries[i];
}
}
_numServerEntries++;
if (_severEntries == NULL) {
_severEntries = malloc(_numServerEntries * sizeof(server_entry));
if (_serverEntries == NULL) {
_serverEntries = malloc(_numServerEntries * sizeof(server_entry));
} else {
_severEntries = realloc(_severEntries, _numServerEntries * sizeof(server_entry));
_serverEntries = realloc(_serverEntries, _numServerEntries * sizeof(server_entry));
}
int index = _numServerEntries - 1;
server_entry* newserver = &_severEntries[index];
server_entry* newserver = &_serverEntries[index];
newserver->address = _strdup(address);
newserver->name = _strdup(address);
newserver->requiresPassword = false;
@ -648,10 +658,10 @@ static void remove_server_entry(int index)
SDL_LockMutex(_mutex);
if (_numServerEntries > index) {
int serversToMove = _numServerEntries - index - 1;
memmove(&_severEntries[index], &_severEntries[index + 1], serversToMove * sizeof(server_entry));
memmove(&_serverEntries[index], &_serverEntries[index + 1], serversToMove * sizeof(server_entry));
_numServerEntries--;
_severEntries = realloc(_severEntries, _numServerEntries * sizeof(server_entry));
_serverEntries = realloc(_serverEntries, _numServerEntries * sizeof(server_entry));
}
SDL_UnlockMutex(_mutex);
}
@ -699,7 +709,7 @@ static uint32 get_total_player_count()
{
uint32 numPlayers = 0;
for (int i = 0; i < _numServerEntries; i++) {
server_entry *serverDetails = &_severEntries[i];
server_entry *serverDetails = &_serverEntries[i];
numPlayers += serverDetails->players;
}
return numPlayers;
@ -715,7 +725,7 @@ static void fetch_servers()
SDL_LockMutex(_mutex);
for (int i = 0; i < _numServerEntries; i++) {
if (!_severEntries[i].favourite) {
if (!_serverEntries[i].favourite) {
remove_server_entry(i);
i = 0;
}