project_files/frontlib/net/netconn_send.c
author Wuzzy <Wuzzy2@mail.ru>
Mon, 07 Oct 2019 23:07:41 +0200
changeset 15450 88770c206c31
parent 10017 de822cd3df3a
permissions -rw-r--r--
Bump protocol version to 59
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    20
#include "netconn_internal.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    21
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    22
#include "../util/logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    23
#include "../util/util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    24
#include "../util/buffer.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    25
#include "../md5/md5.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    26
#include "../base64/base64.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    27
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    28
#include <zlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    29
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    30
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    31
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    32
#include <limits.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    33
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    34
// cmdname is always given as literal from functions in this file, so it is never null.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    35
static int sendVoid(flib_netconn *conn, const char *cmdname) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    36
    if(log_e_if(!conn, "Invalid parameter sending %s command", cmdname)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    37
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    38
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    39
    return flib_netbase_sendf(conn->netBase, "%s\n\n", cmdname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    40
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    41
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    42
// Testing for !*str prevents sending 0-length parameters (they trip up the protocol)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    43
static int sendStr(flib_netconn *conn, const char *cmdname, const char *str) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    44
    if(log_e_if(!conn || flib_strempty(str), "Invalid parameter sending %s command", cmdname)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    45
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    46
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    47
    return flib_netbase_sendf(conn->netBase, "%s\n%s\n\n", cmdname, str);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    48
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    49
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    50
static int sendInt(flib_netconn *conn, const char *cmdname, int param) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    51
    if(log_e_if(!conn, "Invalid parameter sending %s command", cmdname)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    52
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    53
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    54
    return flib_netbase_sendf(conn->netBase, "%s\n%i\n\n", cmdname, param);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    55
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    56
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    57
int flib_netconn_send_nick(flib_netconn *conn, const char *nick) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    58
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    59
    if(!log_badargs_if2(conn==NULL, flib_strempty(nick))) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    60
        char *tmpName = flib_strdupnull(nick);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    61
        if(tmpName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    62
            if(!flib_netbase_sendf(conn->netBase, "%s\n%s\n\n", "NICK", nick)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    63
                free(conn->playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    64
                conn->playerName = tmpName;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    65
                tmpName = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    66
                result = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    67
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    68
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    69
        free(tmpName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    70
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    71
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    72
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    73
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    74
int flib_netconn_send_password(flib_netconn *conn, const char *passwd) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    75
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    76
    if(!log_badargs_if2(conn==NULL, passwd==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    77
        md5_state_t md5state;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    78
        uint8_t md5bytes[16];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    79
        char md5hex[33];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    80
        md5_init(&md5state);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    81
        md5_append(&md5state, (unsigned char*)passwd, strlen(passwd));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    82
        md5_finish(&md5state, md5bytes);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    83
        for(int i=0;i<sizeof(md5bytes); i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    84
            // Needs to be lowercase - server checks case sensitive
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    85
            snprintf(md5hex+i*2, 3, "%02x", (unsigned)md5bytes[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    86
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    87
        result = flib_netbase_sendf(conn->netBase, "%s\n%s\n\n", "PASSWORD", md5hex);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    88
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    89
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    90
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    91
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    92
int flib_netconn_send_quit(flib_netconn *conn, const char *quitmsg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    93
    return sendStr(conn, "QUIT", (quitmsg && *quitmsg) ? quitmsg : "User quit");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    94
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    95
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    96
int flib_netconn_send_chat(flib_netconn *conn, const char *chat) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    97
    if(!flib_strempty(chat)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    98
        return sendStr(conn, "CHAT", chat);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    99
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   100
    return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   101
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   102
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   103
int flib_netconn_send_kick(flib_netconn *conn, const char *playerName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   104
    return sendStr(conn, "KICK", playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   105
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   106
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   107
int flib_netconn_send_playerInfo(flib_netconn *conn, const char *playerName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   108
    return sendStr(conn, "INFO", playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   109
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   110
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   111
int flib_netconn_send_request_roomlist(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   112
    return sendVoid(conn, "LIST");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   113
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   114
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   115
int flib_netconn_send_joinRoom(flib_netconn *conn, const char *room) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   116
    if(!sendStr(conn, "JOIN_ROOM", room)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   117
        conn->isChief = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   118
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   119
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   120
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   121
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   122
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   123
int flib_netconn_send_playerFollow(flib_netconn *conn, const char *playerName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   124
    return sendStr(conn, "FOLLOW", playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   125
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   126
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   127
int flib_netconn_send_createRoom(flib_netconn *conn, const char *room) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   128
    if(!sendStr(conn, "CREATE_ROOM", room)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   129
        conn->isChief = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   130
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   131
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   132
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   133
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   134
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   135
int flib_netconn_send_ban(flib_netconn *conn, const char *playerName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   136
    return sendStr(conn, "BAN", playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   137
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   138
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   139
int flib_netconn_send_clearAccountsCache(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   140
    return sendVoid(conn, "CLEAR_ACCOUNTS_CACHE");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   141
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   142
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   143
int flib_netconn_send_setServerVar(flib_netconn *conn, const char *name, const char *value) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   144
    if(log_badargs_if3(conn==NULL, flib_strempty(name), flib_strempty(value))) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   145
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   146
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   147
    return flib_netbase_sendf(conn->netBase, "%s\n%s\n%s\n\n", "SET_SERVER_VAR", name, value);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   148
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   149
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   150
int flib_netconn_send_getServerVars(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   151
    return sendVoid(conn, "GET_SERVER_VAR");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   152
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   153
int flib_netconn_send_leaveRoom(flib_netconn *conn, const char *str) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   154
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   155
    if(conn->netconnState==NETCONN_STATE_ROOM) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   156
        result = (str && *str) ? sendStr(conn, "PART", str) : sendVoid(conn, "PART");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   157
        if(!result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   158
            netconn_leaveRoom(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   159
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   160
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   161
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   162
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   163
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   164
int flib_netconn_send_toggleReady(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   165
    return sendVoid(conn, "TOGGLE_READY");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   166
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   167
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   168
static void addTeamToPendingList(flib_netconn *conn, const flib_team *team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   169
    flib_team *teamcopy = flib_team_copy(team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   170
    if(teamcopy) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   171
        teamcopy->remoteDriven = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   172
        free(teamcopy->ownerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   173
        teamcopy->ownerName = flib_strdupnull(conn->playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   174
        if(teamcopy->ownerName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   175
            flib_teamlist_delete(&conn->pendingTeamlist, team->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   176
            if(!flib_teamlist_insert(&conn->pendingTeamlist, teamcopy, 0)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   177
                teamcopy = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   178
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   179
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   180
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   181
    flib_team_destroy(teamcopy);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   182
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   183
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   184
int flib_netconn_send_addTeam(flib_netconn *conn, const flib_team *team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   185
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   186
    if(!log_badargs_if2(conn==NULL, team==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   187
        bool missingInfo = flib_strempty(team->name) || flib_strempty(team->grave) || flib_strempty(team->fort) || flib_strempty(team->voicepack) || flib_strempty(team->flag);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   188
        for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   189
            missingInfo |= flib_strempty(team->hogs[i].name) || flib_strempty(team->hogs[i].hat);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   190
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   191
        if(!log_e_if(missingInfo, "Incomplete team definition")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   192
            flib_vector *vec = flib_vector_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   193
            if(vec) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   194
                bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   195
                error |= flib_vector_appendf(vec, "ADD_TEAM\n%s\n%i\n%s\n%s\n%s\n%s\n%i\n", team->name, team->colorIndex, team->grave, team->fort, team->voicepack, team->flag, team->hogs[0].difficulty);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   196
                for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   197
                    error |= flib_vector_appendf(vec, "%s\n%s\n", team->hogs[i].name, team->hogs[i].hat);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   198
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   199
                error |= flib_vector_appendf(vec, "\n");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   200
                if(!error && !flib_netbase_send_raw(conn->netBase, flib_vector_data(vec), flib_vector_size(vec))) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   201
                    addTeamToPendingList(conn, team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   202
                    result = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   203
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   204
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   205
            flib_vector_destroy(vec);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   206
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   207
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   208
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   209
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   210
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   211
int flib_netconn_send_removeTeam(flib_netconn *conn, const char *teamname) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   212
    flib_team *team = flib_teamlist_find(&conn->teamlist, teamname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   213
    if(team && !team->remoteDriven && !sendStr(conn, "REMOVE_TEAM", teamname)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   214
        flib_teamlist_delete(&conn->teamlist, teamname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   215
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   216
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   217
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   218
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   219
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   220
int flib_netconn_send_renameRoom(flib_netconn *conn, const char *roomName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   221
    return sendStr(conn, "ROOM_NAME", roomName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   222
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   223
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   224
int flib_netconn_send_teamHogCount(flib_netconn *conn, const char *teamname, int hogcount) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   225
    if(!log_badargs_if5(conn==NULL, flib_strempty(teamname), hogcount<1, hogcount>HEDGEHOGS_PER_TEAM, !conn->isChief)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   226
            && !flib_netbase_sendf(conn->netBase, "HH_NUM\n%s\n%i\n\n", teamname, hogcount)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   227
        flib_team *team = flib_teamlist_find(&conn->teamlist, teamname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   228
        if(team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   229
            team->hogsInGame = hogcount;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   230
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   231
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   232
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   233
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   234
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   235
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   236
int flib_netconn_send_teamColor(flib_netconn *conn, const char *teamname, int colorIndex) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   237
    if(!log_badargs_if3(conn==NULL, flib_strempty(teamname), !conn->isChief)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   238
            && !flib_netbase_sendf(conn->netBase, "TEAM_COLOR\n%s\n%i\n\n", teamname, colorIndex)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   239
        flib_team *team = flib_teamlist_find(&conn->teamlist, teamname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   240
        if(team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   241
            team->colorIndex = colorIndex;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   242
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   243
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   244
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   245
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   246
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   247
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   248
int flib_netconn_send_weaponset(flib_netconn *conn, const flib_weaponset *weaponset) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   249
    if(!log_badargs_if3(conn==NULL, weaponset==NULL, flib_strempty(weaponset->name))) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   250
        char ammostring[WEAPONS_COUNT*4+1];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   251
        strcpy(ammostring, weaponset->loadout);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   252
        strcat(ammostring, weaponset->crateprob);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   253
        strcat(ammostring, weaponset->delay);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   254
        strcat(ammostring, weaponset->crateammo);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   255
        if(conn->isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   256
            if(!flib_netbase_sendf(conn->netBase, "CFG\nAMMO\n%s\n%s\n\n", weaponset->name, ammostring)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   257
                netconn_setWeaponset(conn, weaponset);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   258
                return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   259
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   260
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   261
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   262
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   263
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   264
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   265
int flib_netconn_send_map(flib_netconn *conn, const flib_map *map) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   266
    if(log_badargs_if2(conn==NULL, map==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   267
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   268
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   269
    bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   270
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   271
    if(map->seed) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   272
        error |= flib_netconn_send_mapSeed(conn, map->seed);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   273
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   274
    error |= flib_netconn_send_mapTemplate(conn, map->templateFilter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   275
    if(map->theme) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   276
        error |= flib_netconn_send_mapTheme(conn, map->theme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   277
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   278
    error |= flib_netconn_send_mapGen(conn, map->mapgen);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   279
    error |= flib_netconn_send_mapMazeSize(conn, map->mazeSize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   280
    if(map->drawData && map->drawDataSize>0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   281
        error |= flib_netconn_send_mapDrawdata(conn, map->drawData, map->drawDataSize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   282
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   283
    // Name is sent last, because the QtFrontend uses this to update its preview, and to show/hide
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   284
    // certain fields
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   285
    if(map->name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   286
        error |= flib_netconn_send_mapName(conn, map->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   287
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   288
    return error;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   289
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   290
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   291
int flib_netconn_send_mapName(flib_netconn *conn, const char *mapName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   292
    if(log_badargs_if2(conn==NULL, mapName==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   293
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   294
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   295
    if(conn->isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   296
        if(!sendStr(conn, "CFG\nMAP", mapName)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   297
            char *copy = flib_strdupnull(mapName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   298
            if(copy) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   299
                free(conn->map->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   300
                conn->map->name = copy;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   301
                return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   302
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   303
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   304
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   305
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   306
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   307
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   308
int flib_netconn_send_mapGen(flib_netconn *conn, int mapGen) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   309
    if(log_badargs_if(conn==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   310
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   311
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   312
    if(conn->isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   313
        if(!sendInt(conn, "CFG\nMAPGEN", mapGen)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   314
            conn->map->mapgen = mapGen;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   315
            return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   316
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   317
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   318
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   319
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   320
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   321
int flib_netconn_send_mapTemplate(flib_netconn *conn, int templateFilter) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   322
    if(log_badargs_if(conn==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   323
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   324
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   325
    if(conn->isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   326
        if(!sendInt(conn, "CFG\nTEMPLATE", templateFilter)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   327
            conn->map->templateFilter = templateFilter;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   328
            return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   329
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   330
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   331
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   332
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   333
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   334
int flib_netconn_send_mapMazeSize(flib_netconn *conn, int mazeSize) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   335
    if(log_badargs_if(conn==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   336
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   337
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   338
    if(conn->isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   339
        if(!sendInt(conn, "CFG\nMAZE_SIZE", mazeSize)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   340
            conn->map->mazeSize = mazeSize;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   341
            return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   342
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   343
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   344
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   345
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   346
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   347
int flib_netconn_send_mapSeed(flib_netconn *conn, const char *seed) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   348
    if(log_badargs_if2(conn==NULL, seed==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   349
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   350
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   351
    if(conn->isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   352
        if(!sendStr(conn, "CFG\nSEED", seed)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   353
            char *copy = flib_strdupnull(seed);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   354
            if(copy) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   355
                free(conn->map->seed);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   356
                conn->map->seed = copy;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   357
                return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   358
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   359
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   360
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   361
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   362
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   363
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   364
int flib_netconn_send_mapTheme(flib_netconn *conn, const char *theme) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   365
    if(log_badargs_if2(conn==NULL, theme==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   366
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   367
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   368
    if(conn->isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   369
        if(!sendStr(conn, "CFG\nTHEME", theme)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   370
            char *copy = flib_strdupnull(theme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   371
            if(copy) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   372
                free(conn->map->theme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   373
                conn->map->theme = copy;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   374
                return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   375
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   376
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   377
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   378
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   379
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   380
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   381
int flib_netconn_send_mapDrawdata(flib_netconn *conn, const uint8_t *drawData, size_t size) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   382
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   383
    if(!log_badargs_if3(conn==NULL, drawData==NULL && size>0, size>SIZE_MAX/2) && conn->isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   384
        uLongf zippedSize = compressBound(size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   385
        uint8_t *zipped = flib_malloc(zippedSize+4); // 4 extra bytes for header
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   386
        if(zipped) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   387
            // Create the QCompress size header (uint32 big endian)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   388
            zipped[0] = (size>>24) & 0xff;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   389
            zipped[1] = (size>>16) & 0xff;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   390
            zipped[2] = (size>>8) & 0xff;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   391
            zipped[3] = (size) & 0xff;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   392
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   393
            if(compress(zipped+4, &zippedSize, drawData, size) != Z_OK) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   394
                flib_log_e("Error compressing drawn map data.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   395
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   396
                char *base64encout = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   397
                base64_encode_alloc((const char*)zipped, zippedSize+4, &base64encout);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   398
                if(!base64encout) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   399
                    flib_log_e("Error base64-encoding drawn map data.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   400
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   401
                    result = flib_netbase_sendf(conn->netBase, "CFG\nDRAWNMAP\n%s\n\n", base64encout);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   402
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   403
                free(base64encout);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   404
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   405
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   406
        free(zipped);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   407
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   408
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   409
    if(!result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   410
        uint8_t *copy = flib_bufdupnull(drawData, size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   411
        if(copy) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   412
            free(conn->map->drawData);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   413
            conn->map->drawData = copy;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   414
            conn->map->drawDataSize = size;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   415
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   416
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   417
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   418
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   419
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   420
int flib_netconn_send_script(flib_netconn *conn, const char *scriptName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   421
    if(log_badargs_if2(conn==NULL, scriptName==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   422
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   423
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   424
    if(conn->isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   425
        if(!sendStr(conn, "CFG\nSCRIPT", scriptName)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   426
            netconn_setScript(conn, scriptName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   427
            return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   428
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   429
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   430
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   431
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   432
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   433
int flib_netconn_send_scheme(flib_netconn *conn, const flib_scheme *scheme) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   434
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   435
    if(!log_badargs_if3(conn==NULL, scheme==NULL, flib_strempty(scheme->name)) && conn->isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   436
        flib_vector *vec = flib_vector_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   437
        if(vec) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   438
            bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   439
            error |= flib_vector_appendf(vec, "CFG\nSCHEME\n%s\n", scheme->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   440
            for(int i=0; i<flib_meta.modCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   441
                error |= flib_vector_appendf(vec, "%s\n", scheme->mods[i] ? "true" : "false");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   442
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   443
            for(int i=0; i<flib_meta.settingCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   444
                error |= flib_vector_appendf(vec, "%i\n", scheme->settings[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   445
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   446
            error |= flib_vector_appendf(vec, "\n");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   447
            if(!error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   448
                result = flib_netbase_send_raw(conn->netBase, flib_vector_data(vec), flib_vector_size(vec));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   449
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   450
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   451
        flib_vector_destroy(vec);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   452
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   453
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   454
    if(!result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   455
        netconn_setScheme(conn, scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   456
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   457
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   458
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   459
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   460
int flib_netconn_send_startGame(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   461
    return sendVoid(conn, "START_GAME");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   462
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   463
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   464
int flib_netconn_send_toggleRestrictJoins(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   465
    return sendVoid(conn, "TOGGLE_RESTRICT_JOINS");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   466
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   467
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   468
int flib_netconn_send_toggleRestrictTeams(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   469
    return sendVoid(conn, "TOGGLE_RESTRICT_TEAMS");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   470
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   471
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   472
int flib_netconn_send_teamchat(flib_netconn *conn, const char *chat) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   473
    if(!flib_strempty(chat)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   474
        return sendStr(conn, "TEAMCHAT", chat);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   475
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   476
    return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   477
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   478
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   479
int flib_netconn_send_engineMessage(flib_netconn *conn, const uint8_t *message, size_t size) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   480
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   481
    if(!log_badargs_if2(conn==NULL, message==NULL && size>0)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   482
        char *base64encout = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   483
        base64_encode_alloc((const char*)message, size, &base64encout);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   484
        if(base64encout) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   485
            result = flib_netbase_sendf(conn->netBase, "EM\n%s\n\n", base64encout);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   486
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   487
        free(base64encout);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   488
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   489
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   490
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   491
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   492
int flib_netconn_send_roundfinished(flib_netconn *conn, bool withoutError) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   493
    return sendInt(conn, "ROUNDFINISHED", withoutError ? 1 : 0);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   494
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   495