project_files/frontlib/net/netconn.c
author Grigory Ustinov <grenka@altlinux.org>
Tue, 06 Nov 2018 20:02:45 +0300
changeset 14150 7c1e0a854dd8
parent 11046 47a8c19ecb60
permissions -rw-r--r--
Update frontend russian translation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
     3
 * Copyright (c) 2006-2008 Igor Ulyanov <iulyanov@gmail.com>
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10108
diff changeset
     4
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
     5
 * Copyright (c) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
     6
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
     7
 * This program is free software; you can redistribute it and/or modify
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
     8
 * it under the terms of the GNU General Public License as published by
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
     9
 * the Free Software Foundation; version 2 of the License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    10
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    11
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    14
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    15
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    16
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    17
 * along with this program; if not, write to the Free Software
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 10017
diff changeset
    18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    19
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    20
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    21
#include "netconn_internal.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    22
#include "netprotocol.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    23
#include "../util/logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    24
#include "../util/util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    25
#include "../md5/md5.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    26
#include "../base64/base64.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    27
#include "../model/mapcfg.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    28
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    29
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    30
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    31
#include <errno.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    32
#include <ctype.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    33
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    34
flib_netconn *flib_netconn_create(const char *playerName, const char *dataDirPath, const char *host, int port) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    35
    flib_netconn *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    36
    if(!log_badargs_if4(playerName==NULL, host==NULL, port<1, port>65535)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    37
        flib_netconn *newConn = flib_calloc(1, sizeof(flib_netconn));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    38
        if(newConn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    39
            newConn->netBase = flib_netbase_create(host, port);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    40
            newConn->playerName = flib_strdupnull(playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    41
            newConn->dataDirPath = flib_strdupnull(dataDirPath);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    42
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    43
            newConn->netconnState = NETCONN_STATE_CONNECTING;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    44
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    45
            newConn->isChief = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    46
            newConn->map = flib_map_create_named("", "NoSuchMap");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    47
            newConn->pendingTeamlist.teamCount = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    48
            newConn->pendingTeamlist.teams = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    49
            newConn->teamlist.teamCount = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    50
            newConn->teamlist.teams = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    51
            newConn->scheme = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    52
            newConn->style = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    53
            newConn->weaponset = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    54
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    55
            newConn->running = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    56
            newConn->destroyRequested = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    57
            netconn_clearCallbacks(newConn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    58
            if(newConn->netBase && newConn->playerName && newConn->dataDirPath && newConn->map) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    59
                result = newConn;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    60
                newConn = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    61
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    62
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    63
        flib_netconn_destroy(newConn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    64
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    65
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    66
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    67
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    68
void flib_netconn_destroy(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    69
    if(conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    70
        if(conn->running) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    71
            /*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    72
             * The function was called from a callback, so the tick function is still running
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    73
             * and we delay the actual destruction. We ensure no further callbacks will be
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    74
             * sent to prevent surprises.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    75
             */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    76
            netconn_clearCallbacks(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    77
            conn->destroyRequested = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    78
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    79
            flib_netbase_destroy(conn->netBase);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    80
            free(conn->playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    81
            free(conn->dataDirPath);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    82
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    83
            flib_map_destroy(conn->map);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    84
            flib_teamlist_clear(&conn->pendingTeamlist);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    85
            flib_teamlist_clear(&conn->teamlist);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    86
            flib_scheme_destroy(conn->scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    87
            free(conn->style);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    88
            flib_weaponset_destroy(conn->weaponset);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    89
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    90
            free(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    91
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    92
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    93
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    94
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    95
bool flib_netconn_is_chief(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    96
    if(!log_badargs_if(conn==NULL) && conn->netconnState==NETCONN_STATE_ROOM) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    97
        return conn->isChief;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    98
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    99
    return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   100
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   101
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   102
const char *flib_netconn_get_playername(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   103
    if(!log_badargs_if(conn==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   104
        return conn->playerName;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   105
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   106
    return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   107
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   108
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   109
void netconn_leaveRoom(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   110
    conn->netconnState = NETCONN_STATE_LOBBY;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   111
    conn->isChief = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   112
    flib_map_destroy(conn->map);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   113
    conn->map = flib_map_create_named("", "NoSuchMap");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   114
    flib_teamlist_clear(&conn->pendingTeamlist);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   115
    flib_teamlist_clear(&conn->teamlist);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   116
    flib_scheme_destroy(conn->scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   117
    conn->scheme = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   118
    free(conn->style);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   119
    conn->style = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   120
    flib_weaponset_destroy(conn->weaponset);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   121
    conn->weaponset = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   122
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   123
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   124
void netconn_setMap(flib_netconn *conn, const flib_map *map) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   125
    flib_map *copy = flib_map_copy(map);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   126
    if(copy) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   127
        flib_map_destroy(conn->map);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   128
        conn->map = copy;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   129
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   130
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   131
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   132
void netconn_setWeaponset(flib_netconn *conn, const flib_weaponset *weaponset) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   133
    flib_weaponset *copy = flib_weaponset_copy(weaponset);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   134
    if(copy) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   135
        flib_weaponset_destroy(conn->weaponset);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   136
        conn->weaponset = copy;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   137
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   138
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   139
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   140
void netconn_setScript(flib_netconn *conn, const char *script) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   141
    char *copy = flib_strdupnull(script);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   142
    if(copy) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   143
        free(conn->style);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   144
        conn->style = copy;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   145
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   146
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   147
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   148
void netconn_setScheme(flib_netconn *conn, const flib_scheme *scheme) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   149
    flib_scheme *copy = flib_scheme_copy(scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   150
    if(copy) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   151
        flib_scheme_destroy(conn->scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   152
        conn->scheme = copy;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   153
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   154
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   155
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   156
flib_gamesetup *flib_netconn_create_gamesetup(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   157
    flib_gamesetup *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   158
    if(!log_badargs_if(conn==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   159
        if(conn->teamlist.teamCount==0 || !conn->scheme || !conn->weaponset) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   160
            flib_log_e("Incomplete room state");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   161
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   162
            flib_gamesetup stackSetup = {0};
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   163
            stackSetup.gamescheme = conn->scheme;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   164
            stackSetup.map = conn->map;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   165
            stackSetup.style = conn->style;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   166
            stackSetup.teamlist = &conn->teamlist;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   167
            result = flib_gamesetup_copy(&stackSetup);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   168
            if(result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   169
                bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   170
                for(int i=0; i<result->teamlist->teamCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   171
                    if(flib_team_set_weaponset(result->teamlist->teams[i], conn->weaponset)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   172
                        error = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   173
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   174
                    flib_team_set_health(result->teamlist->teams[i], flib_scheme_get_setting(conn->scheme, "health", 100));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   175
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   176
                if(result->map->mapgen == MAPGEN_NAMED && result->map->name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   177
                    flib_mapcfg mapcfg;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   178
                    if(!flib_mapcfg_read(conn->dataDirPath, result->map->name, &mapcfg)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   179
                        free(result->map->theme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   180
                        result->map->theme = flib_strdupnull(mapcfg.theme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   181
                        if(!result->map->theme) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   182
                            error = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   183
                        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   184
                    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   185
                        flib_log_e("Unable to read map config for map %s", result->map->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   186
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   187
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   188
                if(error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   189
                    flib_gamesetup_destroy(result);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   190
                    result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   191
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   192
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   193
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   194
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   195
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   196
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   197
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   198
static void flib_netconn_wrappedtick(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   199
    flib_netmsg *netmsg;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   200
    flib_netbase *net = conn->netBase;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   201
    bool exit = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   202
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   203
    while(!exit && !conn->destroyRequested && (netmsg=flib_netbase_recv_message(conn->netBase))) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   204
        if(netmsg->partCount==0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   205
            flib_log_w("Empty server message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   206
            continue;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   207
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   208
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   209
        if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   210
            char *buf = flib_join(netmsg->parts, netmsg->partCount, "|");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   211
            if(buf) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   212
                flib_log_d("[Net In]%s", buf);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   213
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   214
            free(buf);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   215
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   216
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   217
        const char *cmd = netmsg->parts[0];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   218
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   219
        if (!strcmp(cmd, "NICK") && netmsg->partCount>=2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   220
            if(netmsg->partCount<2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   221
                flib_log_w("Net: Malformed NICK message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   222
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   223
                char *nick = flib_strdupnull(netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   224
                if(nick) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   225
                    free(conn->playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   226
                    conn->playerName = nick;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   227
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   228
                    conn->netconnState = NETCONN_STATE_DISCONNECTED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   229
                    conn->onDisconnectedCb(conn->onDisconnectedCtx, NETCONN_DISCONNECT_INTERNAL_ERROR, "Out of memory");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   230
                    exit = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   231
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   232
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   233
        } else if (!strcmp(cmd, "PROTO")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   234
            // The server just echoes this back apparently
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   235
        } else if (!strcmp(cmd, "ERROR")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   236
            if (netmsg->partCount >= 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   237
                conn->onMessageCb(conn->onMessageCtx, NETCONN_MSG_TYPE_ERROR, netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   238
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   239
                conn->onMessageCb(conn->onMessageCtx, NETCONN_MSG_TYPE_ERROR, "Unknown Error");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   240
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   241
        } else if(!strcmp(cmd, "WARNING")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   242
            if (netmsg->partCount >= 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   243
                conn->onMessageCb(conn->onMessageCtx, NETCONN_MSG_TYPE_WARNING, netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   244
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   245
                conn->onMessageCb(conn->onMessageCtx, NETCONN_MSG_TYPE_WARNING, "Unknown Warning");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   246
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   247
        } else if(!strcmp(cmd, "CONNECTED")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   248
            if(netmsg->partCount<3 || atol(netmsg->parts[2])<MIN_SERVER_VERSION) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   249
                flib_log_w("Net: Server too old");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   250
                flib_netbase_sendf(net, "%s\n%s\n\n", "QUIT", "Server too old");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   251
                conn->netconnState = NETCONN_STATE_DISCONNECTED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   252
                conn->onDisconnectedCb(conn->onDisconnectedCtx, NETCONN_DISCONNECT_SERVER_TOO_OLD, "Server too old");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   253
                exit = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   254
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   255
                flib_netbase_sendf(net, "%s\n%s\n\n", "NICK", conn->playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   256
                flib_netbase_sendf(net, "%s\n%i\n\n", "PROTO", (int)PROTOCOL_VERSION);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   257
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   258
        } else if(!strcmp(cmd, "PING")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   259
            if (netmsg->partCount > 1) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   260
                flib_netbase_sendf(net, "%s\n%s\n\n", "PONG", netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   261
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   262
                flib_netbase_sendf(net, "%s\n\n", "PONG");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   263
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   264
        } else if(!strcmp(cmd, "ROOMS")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   265
            if(netmsg->partCount % 8 != 1) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   266
                flib_log_w("Net: Malformed ROOMS message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   267
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   268
                int roomCount = netmsg->partCount/8;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   269
                flib_room **rooms = flib_room_array_from_netmsg(netmsg->parts+1, roomCount);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   270
                if(rooms) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   271
                    conn->onRoomlistCb(conn->onRoomlistCtx, (const flib_room**)rooms, roomCount);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   272
                    for(int i=0; i<roomCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   273
                        flib_room_destroy(rooms[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   274
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   275
                    free(rooms);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   276
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   277
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   278
        } else if (!strcmp(cmd, "SERVER_MESSAGE")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   279
            if(netmsg->partCount < 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   280
                flib_log_w("Net: Empty SERVERMESSAGE message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   281
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   282
                conn->onMessageCb(conn->onMessageCtx, NETCONN_MSG_TYPE_SERVERMESSAGE, netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   283
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   284
        } else if (!strcmp(cmd, "CHAT")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   285
            if(netmsg->partCount < 3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   286
                flib_log_w("Net: Empty CHAT message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   287
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   288
                conn->onChatCb(conn->onChatCtx, netmsg->parts[1], netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   289
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   290
        } else if (!strcmp(cmd, "INFO")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   291
            if(netmsg->partCount < 5) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   292
                flib_log_w("Net: Malformed INFO message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   293
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   294
                char *joined = flib_join(netmsg->parts+1, netmsg->partCount-1, "\n");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   295
                if(joined) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   296
                    conn->onMessageCb(conn->onMessageCtx, NETCONN_MSG_TYPE_PLAYERINFO, joined);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   297
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   298
                free(joined);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   299
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   300
        } else if(!strcmp(cmd, "SERVER_VARS")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   301
            for(int offset=1; offset+2<netmsg->partCount; offset+=2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   302
                conn->onServerVarCb(conn->onServerVarCtx, netmsg->parts[offset], netmsg->parts[offset+1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   303
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   304
        } else if (!strcmp(cmd, "CLIENT_FLAGS")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   305
            if(netmsg->partCount < 3 || strlen(netmsg->parts[1]) < 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   306
                flib_log_w("Net: Malformed CLIENT_FLAGS message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   307
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   308
                const char *flags = netmsg->parts[1];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   309
                bool setFlag = flags[0] == '+';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   310
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   311
                for(int j = 2; j < netmsg->partCount; ++j) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   312
                    bool isSelf = !strcmp(conn->playerName, netmsg->parts[j]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   313
                    if(isSelf && strchr(flags, 'h')) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   314
                        conn->isChief = setFlag;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   315
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   316
                    conn->onClientFlagsCb(conn->onClientFlagsCtx, netmsg->parts[j], flags+1, setFlag);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   317
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   318
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   319
        } else if (!strcmp(cmd, "ADD_TEAM")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   320
            if(netmsg->partCount != 24 || conn->netconnState!=NETCONN_STATE_ROOM) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   321
                flib_log_w("Net: Bad ADD_TEAM message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   322
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   323
                flib_team *team = flib_team_from_netmsg(netmsg->parts+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   324
                if(!team || flib_teamlist_insert(&conn->teamlist, team, conn->teamlist.teamCount)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   325
                    flib_team_destroy(team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   326
                    conn->netconnState = NETCONN_STATE_DISCONNECTED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   327
                    conn->onDisconnectedCb(conn->onDisconnectedCtx, NETCONN_DISCONNECT_INTERNAL_ERROR, "Internal error");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   328
                    exit = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   329
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   330
                    conn->onTeamAddCb(conn->onTeamAddCtx, team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   331
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   332
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   333
        } else if (!strcmp(cmd, "REMOVE_TEAM")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   334
            if(netmsg->partCount != 2 || conn->netconnState!=NETCONN_STATE_ROOM) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   335
                flib_log_w("Net: Bad REMOVETEAM message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   336
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   337
                flib_teamlist_delete(&conn->teamlist, netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   338
                conn->onTeamDeleteCb(conn->onTeamDeleteCtx, netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   339
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   340
        } else if(!strcmp(cmd, "ROOMABANDONED")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   341
            netconn_leaveRoom(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   342
            conn->onLeaveRoomCb(conn->onLeaveRoomCtx, NETCONN_ROOMLEAVE_ABANDONED, "Room destroyed");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   343
        } else if(!strcmp(cmd, "KICKED")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   344
            netconn_leaveRoom(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   345
            conn->onLeaveRoomCb(conn->onLeaveRoomCtx, NETCONN_ROOMLEAVE_KICKED, "You got kicked");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   346
        } else if(!strcmp(cmd, "JOINED")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   347
            if(netmsg->partCount < 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   348
                flib_log_w("Net: Bad JOINED message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   349
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   350
                for(int i = 1; i < netmsg->partCount; ++i)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   351
                {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   352
                    bool isMe = !strcmp(conn->playerName, netmsg->parts[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   353
                    if (isMe) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   354
                        conn->netconnState = NETCONN_STATE_ROOM;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   355
                        conn->onEnterRoomCb(conn->onEnterRoomCtx, conn->isChief);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   356
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   357
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   358
                    conn->onRoomJoinCb(conn->onRoomJoinCtx, netmsg->parts[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   359
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   360
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   361
        } else if(!strcmp(cmd, "LOBBY:JOINED")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   362
            if(netmsg->partCount < 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   363
                flib_log_w("Net: Bad JOINED message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   364
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   365
                for(int i = 1; i < netmsg->partCount; ++i)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   366
                {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   367
                    bool isMe = !strcmp(conn->playerName, netmsg->parts[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   368
                    if (isMe && conn->netconnState == NETCONN_STATE_CONNECTING) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   369
                        conn->onConnectedCb(conn->onConnectedCtx);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   370
                        conn->netconnState = NETCONN_STATE_LOBBY;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   371
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   372
                    conn->onLobbyJoinCb(conn->onLobbyJoinCtx, netmsg->parts[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   373
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   374
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   375
        } else if(!strcmp(cmd, "LEFT")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   376
            if(netmsg->partCount < 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   377
                flib_log_w("Net: Bad LEFT message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   378
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   379
                conn->onRoomLeaveCb(conn->onRoomLeaveCtx, netmsg->parts[1], netmsg->partCount>2 ? netmsg->parts[2] : NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   380
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   381
        } else if(!strcmp(cmd, "ROOM") && netmsg->partCount >= 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   382
            const char *subcmd = netmsg->parts[1];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   383
            if(!strcmp(subcmd, "ADD") && netmsg->partCount == 10) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   384
                flib_room *room = flib_room_from_netmsg(netmsg->parts+2);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   385
                if(room) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   386
                    conn->onRoomAddCb(conn->onRoomAddCtx, room);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   387
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   388
                flib_room_destroy(room);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   389
            } else if(!strcmp(subcmd, "UPD") && netmsg->partCount == 11) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   390
                flib_room *room = flib_room_from_netmsg(netmsg->parts+3);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   391
                if(room) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   392
                    conn->onRoomUpdateCb(conn->onRoomUpdateCtx, netmsg->parts[2], room);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   393
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   394
                flib_room_destroy(room);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   395
            } else if(!strcmp(subcmd, "DEL") && netmsg->partCount == 3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   396
                conn->onRoomDeleteCb(conn->onRoomDeleteCtx, netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   397
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   398
                flib_log_w("Net: Unknown or malformed ROOM subcommand: %s", subcmd);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   399
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   400
        } else if(!strcmp(cmd, "LOBBY:LEFT")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   401
            if(netmsg->partCount < 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   402
                flib_log_w("Net: Bad LOBBY:LEFT message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   403
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   404
                conn->onLobbyLeaveCb(conn->onLobbyLeaveCtx, netmsg->parts[1], netmsg->partCount>2 ? netmsg->parts[2] : NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   405
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   406
        } else if (!strcmp(cmd, "RUN_GAME")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   407
            conn->onRunGameCb(conn->onRunGameCtx);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   408
        } else if (!strcmp(cmd, "ASKPASSWORD")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   409
            conn->onPasswordRequestCb(conn->onPasswordRequestCtx, conn->playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   410
        } else if (!strcmp(cmd, "NOTICE")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   411
            if(netmsg->partCount < 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   412
                flib_log_w("Net: Bad NOTICE message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   413
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   414
                errno = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   415
                long n = strtol(netmsg->parts[1], NULL, 10);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   416
                if(errno) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   417
                    flib_log_w("Net: Bad NOTICE message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   418
                } else if(n==0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   419
                    conn->onNickTakenCb(conn->onNickTakenCtx, conn->playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   420
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   421
                    flib_log_w("Net: Unknown NOTICE message: %l", n);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   422
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   423
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   424
        } else if (!strcmp(cmd, "TEAM_ACCEPTED")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   425
            if (netmsg->partCount != 2 || conn->netconnState!=NETCONN_STATE_ROOM) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   426
                flib_log_w("Net: Bad TEAM_ACCEPTED message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   427
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   428
                flib_team *team = flib_team_copy(flib_teamlist_find(&conn->pendingTeamlist, netmsg->parts[1]));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   429
                if(team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   430
                    flib_teamlist_insert(&conn->teamlist, team, conn->teamlist.teamCount);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   431
                    flib_teamlist_delete(&conn->pendingTeamlist, netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   432
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   433
                    flib_log_e("Team accepted that was not requested: %s", netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   434
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   435
                conn->onTeamAcceptedCb(conn->onTeamAcceptedCtx, netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   436
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   437
        } else if (!strcmp(cmd, "CFG")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   438
            if(netmsg->partCount < 3 || conn->netconnState!=NETCONN_STATE_ROOM) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   439
                flib_log_w("Net: Bad CFG message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   440
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   441
                const char *subcmd = netmsg->parts[1];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   442
                if(!strcmp(subcmd, "SCHEME") && netmsg->partCount == flib_meta.modCount + flib_meta.settingCount + 3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   443
                    flib_scheme *cfg = flib_scheme_from_netmsg(netmsg->parts+2);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   444
                    if(cfg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   445
                        flib_scheme_destroy(conn->scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   446
                        conn->scheme = cfg;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   447
                        conn->onSchemeChangedCb(conn->onSchemeChangedCtx, cfg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   448
                    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   449
                        flib_log_e("Error processing CFG SCHEME message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   450
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   451
                } else if(!strcmp(subcmd, "FULLMAPCONFIG") && netmsg->partCount == 7) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   452
                    flib_map *map = flib_map_from_netmsg(netmsg->parts+2);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   453
                    if(map) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   454
                        flib_map_destroy(conn->map);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   455
                        conn->map = map;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   456
                        conn->onMapChangedCb(conn->onMapChangedCtx, conn->map, NETCONN_MAPCHANGE_FULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   457
                    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   458
                        flib_log_e("Error processing CFG FULLMAPCONFIG message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   459
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   460
                } else if(!strcmp(subcmd, "MAP") && netmsg->partCount == 3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   461
                    char *mapname = flib_strdupnull(netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   462
                    if(mapname) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   463
                        free(conn->map->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   464
                        conn->map->name = mapname;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   465
                        conn->onMapChangedCb(conn->onMapChangedCtx, conn->map, NETCONN_MAPCHANGE_MAP);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   466
                    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   467
                        flib_log_e("Error processing CFG MAP message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   468
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   469
                } else if(!strcmp(subcmd, "THEME") && netmsg->partCount == 3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   470
                    char *themename = flib_strdupnull(netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   471
                    if(themename) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   472
                        free(conn->map->theme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   473
                        conn->map->theme = themename;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   474
                        conn->onMapChangedCb(conn->onMapChangedCtx, conn->map, NETCONN_MAPCHANGE_THEME);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   475
                    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   476
                        flib_log_e("Error processing CFG THEME message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   477
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   478
                } else if(!strcmp(subcmd, "SEED") && netmsg->partCount == 3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   479
                    char *seed = flib_strdupnull(netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   480
                    if(seed) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   481
                        free(conn->map->seed);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   482
                        conn->map->seed = seed;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   483
                        conn->onMapChangedCb(conn->onMapChangedCtx, conn->map, NETCONN_MAPCHANGE_SEED);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   484
                    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   485
                        flib_log_e("Error processing CFG SEED message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   486
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   487
                } else if(!strcmp(subcmd, "TEMPLATE") && netmsg->partCount == 3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   488
                    conn->map->templateFilter = atoi(netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   489
                    conn->onMapChangedCb(conn->onMapChangedCtx, conn->map, NETCONN_MAPCHANGE_TEMPLATE);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   490
                } else if(!strcmp(subcmd, "MAPGEN") && netmsg->partCount == 3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   491
                    conn->map->mapgen = atoi(netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   492
                    conn->onMapChangedCb(conn->onMapChangedCtx, conn->map, NETCONN_MAPCHANGE_MAPGEN);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   493
                } else if(!strcmp(subcmd, "MAZE_SIZE") && netmsg->partCount == 3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   494
                    conn->map->mazeSize = atoi(netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   495
                    conn->onMapChangedCb(conn->onMapChangedCtx, conn->map, NETCONN_MAPCHANGE_MAZE_SIZE);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   496
                } else if(!strcmp(subcmd, "DRAWNMAP") && netmsg->partCount == 3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   497
                    size_t drawnMapSize = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   498
                    uint8_t *drawnMapData = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   499
                    if(!flib_drawnmapdata_from_netmsg(netmsg->parts[2], &drawnMapData, &drawnMapSize)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   500
                        free(conn->map->drawData);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   501
                        conn->map->drawData = drawnMapData;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   502
                        conn->map->drawDataSize = drawnMapSize;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   503
                        conn->onMapChangedCb(conn->onMapChangedCtx, conn->map, NETCONN_MAPCHANGE_DRAWNMAP);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   504
                    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   505
                        flib_log_e("Error processing CFG DRAWNMAP message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   506
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   507
                } else if(!strcmp(subcmd, "SCRIPT") && netmsg->partCount == 3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   508
                    netconn_setScript(conn, netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   509
                    conn->onScriptChangedCb(conn->onScriptChangedCtx, netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   510
                } else if(!strcmp(subcmd, "AMMO") && netmsg->partCount == 4) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   511
                    flib_weaponset *weapons = flib_weaponset_from_ammostring(netmsg->parts[2], netmsg->parts[3]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   512
                    if(weapons) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   513
                        flib_weaponset_destroy(conn->weaponset);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   514
                        conn->weaponset = weapons;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   515
                        conn->onWeaponsetChangedCb(conn->onWeaponsetChangedCtx, weapons);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   516
                    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   517
                        flib_log_e("Error processing CFG AMMO message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   518
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   519
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   520
                    flib_log_w("Net: Unknown or malformed CFG subcommand: %s", subcmd);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   521
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   522
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   523
        } else if (!strcmp(cmd, "HH_NUM")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   524
            if (netmsg->partCount != 3 || conn->netconnState!=NETCONN_STATE_ROOM) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   525
                flib_log_w("Net: Bad HH_NUM message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   526
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   527
                int hogs = atoi(netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   528
                if(hogs<=0 || hogs>HEDGEHOGS_PER_TEAM) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   529
                    flib_log_w("Net: Bad HH_NUM message: %s hogs", netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   530
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   531
                    flib_team *team = flib_teamlist_find(&conn->teamlist, netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   532
                    if(team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   533
                        team->hogsInGame = hogs;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   534
                    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   535
                        flib_log_e("HH_NUM message for unknown team %s", netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   536
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   537
                    conn->onHogCountChangedCb(conn->onHogCountChangedCtx, netmsg->parts[1], hogs);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   538
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   539
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   540
        } else if (!strcmp(cmd, "TEAM_COLOR")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   541
            if (netmsg->partCount != 3 || conn->netconnState!=NETCONN_STATE_ROOM) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   542
                flib_log_w("Net: Bad TEAM_COLOR message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   543
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   544
                long color;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   545
                if(sscanf(netmsg->parts[2], "%lu", &color) && color>=0 && color<flib_teamcolor_count) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   546
                    flib_team *team = flib_teamlist_find(&conn->teamlist, netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   547
                    if(team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   548
                        team->colorIndex = color;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   549
                    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   550
                        flib_log_e("TEAM_COLOR message for unknown team %s", netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   551
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   552
                    conn->onTeamColorChangedCb(conn->onTeamColorChangedCtx, netmsg->parts[1], color);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   553
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   554
                    flib_log_w("Net: Bad TEAM_COLOR message: Color %s", netmsg->parts[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   555
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   556
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   557
        } else if (!strcmp(cmd, "EM")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   558
            if(netmsg->partCount < 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   559
                flib_log_w("Net: Bad EM message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   560
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   561
                for(int i = 1; i < netmsg->partCount; ++i) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   562
                    char *out = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   563
                    size_t outlen;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   564
                    bool ok = base64_decode_alloc(netmsg->parts[i], strlen(netmsg->parts[i]), &out, &outlen);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   565
                    if(ok && outlen) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   566
                        conn->onEngineMessageCb(conn->onEngineMessageCtx, (uint8_t*)out, outlen);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   567
                    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   568
                        flib_log_e("Net: Malformed engine message: %s", netmsg->parts[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   569
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   570
                    free(out);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   571
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   572
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   573
        } else if (!strcmp(cmd, "BYE")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   574
            if (netmsg->partCount < 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   575
                flib_log_w("Net: Bad BYE message");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   576
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   577
                conn->netconnState = NETCONN_STATE_DISCONNECTED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   578
                if (!strcmp(netmsg->parts[1], "Authentication failed")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   579
                    conn->onDisconnectedCb(conn->onDisconnectedCtx, NETCONN_DISCONNECT_AUTH_FAILED, netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   580
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   581
                    conn->onDisconnectedCb(conn->onDisconnectedCtx, NETCONN_DISCONNECT_NORMAL, netmsg->parts[1]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   582
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   583
                exit = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   584
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   585
        } else if (!strcmp(cmd, "ADMIN_ACCESS")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   586
            // deprecated
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   587
        } else if (!strcmp(cmd, "ROOM_CONTROL_ACCESS")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   588
            // deprecated
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   589
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   590
            flib_log_w("Unknown server command: %s", cmd);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   591
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   592
        flib_netmsg_destroy(netmsg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   593
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   594
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   595
    if(!exit && !conn->destroyRequested && !flib_netbase_connected(net)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   596
        conn->netconnState = NETCONN_STATE_DISCONNECTED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   597
        conn->onDisconnectedCb(conn->onDisconnectedCtx, NETCONN_DISCONNECT_CONNLOST, "Connection lost");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   598
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   599
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   600
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   601
void flib_netconn_tick(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   602
    if(!log_badargs_if(conn==NULL)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   603
            && !log_w_if(conn->running, "Call to flib_netconn_tick from a callback")
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   604
            && !log_w_if(conn->netconnState == NETCONN_STATE_DISCONNECTED, "We are already done.")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   605
        conn->running = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   606
        flib_netconn_wrappedtick(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   607
        conn->running = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   608
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   609
        if(conn->destroyRequested) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   610
            flib_netconn_destroy(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   611
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   612
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   613
}