10017
|
1 |
/*
|
|
2 |
* Hedgewars, a free turn based strategy game
|
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU General Public License
|
|
7 |
* as published by the Free Software Foundation; either version 2
|
|
8 |
* of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
18 |
*/
|
|
19 |
|
|
20 |
#include "gameconn.h"
|
|
21 |
#include "ipcbase.h"
|
|
22 |
#include "ipcprotocol.h"
|
|
23 |
#include "../util/logging.h"
|
|
24 |
#include "../util/util.h"
|
|
25 |
#include "../hwconsts.h"
|
|
26 |
#include <stdbool.h>
|
|
27 |
#include <stdlib.h>
|
|
28 |
#include <string.h>
|
|
29 |
|
|
30 |
typedef enum {
|
|
31 |
AWAIT_CONNECTION,
|
|
32 |
CONNECTED,
|
|
33 |
FINISHED
|
|
34 |
} gameconn_state;
|
|
35 |
|
|
36 |
struct _flib_gameconn {
|
|
37 |
flib_ipcbase *ipcBase;
|
|
38 |
flib_vector *configBuffer;
|
|
39 |
flib_vector *demoBuffer;
|
|
40 |
char *playerName;
|
|
41 |
|
|
42 |
gameconn_state state;
|
|
43 |
bool netgame;
|
|
44 |
int disconnectReason;
|
|
45 |
|
|
46 |
void (*onConnectCb)(void* context);
|
|
47 |
void *onConnectCtx;
|
|
48 |
|
|
49 |
void (*onDisconnectCb)(void* context, int reason);
|
|
50 |
void *onDisconnectCtx;
|
|
51 |
|
|
52 |
void (*onErrorMessageCb)(void* context, const char *msg);
|
|
53 |
void *onErrorMessageCtx;
|
|
54 |
|
|
55 |
void (*onChatCb)(void* context, const char *msg, bool teamchat);
|
|
56 |
void *onChatCtx;
|
|
57 |
|
|
58 |
void (*onGameRecordedCb)(void *context, const uint8_t *record, size_t size, bool isSavegame);
|
|
59 |
void *onGameRecordedCtx;
|
|
60 |
|
|
61 |
void (*onEngineMessageCb)(void *context, const uint8_t *em, size_t size);
|
|
62 |
void *onEngineMessageCtx;
|
|
63 |
|
|
64 |
bool running;
|
|
65 |
bool destroyRequested;
|
|
66 |
};
|
|
67 |
|
|
68 |
static void defaultCallback_onErrorMessage(void* context, const char *msg) {
|
|
69 |
flib_log_w("Error from engine (no callback set): %s", msg);
|
|
70 |
}
|
|
71 |
|
|
72 |
static void clearCallbacks(flib_gameconn *conn) {
|
|
73 |
flib_gameconn_onConnect(conn, NULL, NULL);
|
|
74 |
flib_gameconn_onDisconnect(conn, NULL, NULL);
|
|
75 |
flib_gameconn_onErrorMessage(conn, NULL, NULL);
|
|
76 |
flib_gameconn_onChat(conn, NULL, NULL);
|
|
77 |
flib_gameconn_onGameRecorded(conn, NULL, NULL);
|
|
78 |
flib_gameconn_onEngineMessage(conn, NULL, NULL);
|
|
79 |
}
|
|
80 |
|
|
81 |
static flib_gameconn *flib_gameconn_create_partial(bool record, const char *playerName, bool netGame) {
|
|
82 |
flib_gameconn *result = NULL;
|
|
83 |
flib_gameconn *tempConn = flib_calloc(1, sizeof(flib_gameconn));
|
|
84 |
if(tempConn) {
|
|
85 |
tempConn->ipcBase = flib_ipcbase_create();
|
|
86 |
tempConn->configBuffer = flib_vector_create();
|
|
87 |
tempConn->playerName = flib_strdupnull(playerName);
|
|
88 |
if(tempConn->ipcBase && tempConn->configBuffer && tempConn->playerName) {
|
|
89 |
if(record) {
|
|
90 |
tempConn->demoBuffer = flib_vector_create();
|
|
91 |
}
|
|
92 |
tempConn->state = AWAIT_CONNECTION;
|
|
93 |
tempConn->netgame = netGame;
|
|
94 |
tempConn->disconnectReason = GAME_END_ERROR;
|
|
95 |
clearCallbacks(tempConn);
|
|
96 |
result = tempConn;
|
|
97 |
tempConn = NULL;
|
|
98 |
}
|
|
99 |
}
|
|
100 |
flib_gameconn_destroy(tempConn);
|
|
101 |
return result;
|
|
102 |
}
|
|
103 |
|
|
104 |
flib_gameconn *flib_gameconn_create(const char *playerName, const flib_gamesetup *setup, bool netgame) {
|
|
105 |
if(log_badargs_if2(playerName==NULL, setup==NULL)) {
|
|
106 |
return NULL;
|
|
107 |
}
|
|
108 |
flib_gameconn *result = NULL;
|
|
109 |
flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, netgame);
|
|
110 |
if(tempConn) {
|
|
111 |
if(flib_ipc_append_fullconfig(tempConn->configBuffer, setup, netgame)) {
|
|
112 |
flib_log_e("Error generating full game configuration for the engine.");
|
|
113 |
} else {
|
|
114 |
result = tempConn;
|
|
115 |
tempConn = NULL;
|
|
116 |
}
|
|
117 |
}
|
|
118 |
flib_gameconn_destroy(tempConn);
|
|
119 |
return result;
|
|
120 |
}
|
|
121 |
|
|
122 |
flib_gameconn *flib_gameconn_create_playdemo(const uint8_t *demoFileContent, size_t size) {
|
|
123 |
if(log_badargs_if(demoFileContent==NULL && size>0)) {
|
|
124 |
return NULL;
|
|
125 |
}
|
|
126 |
flib_gameconn *result = NULL;
|
|
127 |
flib_gameconn *tempConn = flib_gameconn_create_partial(false, "Player", false);
|
|
128 |
if(tempConn) {
|
|
129 |
if(!flib_vector_append(tempConn->configBuffer, demoFileContent, size)) {
|
|
130 |
result = tempConn;
|
|
131 |
tempConn = NULL;
|
|
132 |
}
|
|
133 |
}
|
|
134 |
flib_gameconn_destroy(tempConn);
|
|
135 |
return result;
|
|
136 |
}
|
|
137 |
|
|
138 |
flib_gameconn *flib_gameconn_create_loadgame(const char *playerName, const uint8_t *saveFileContent, size_t size) {
|
|
139 |
if(log_badargs_if(saveFileContent==NULL && size>0)) {
|
|
140 |
return NULL;
|
|
141 |
}
|
|
142 |
flib_gameconn *result = NULL;
|
|
143 |
flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, false);
|
|
144 |
if(tempConn) {
|
|
145 |
if(!flib_vector_append(tempConn->configBuffer, saveFileContent, size)) {
|
|
146 |
result = tempConn;
|
|
147 |
tempConn = NULL;
|
|
148 |
}
|
|
149 |
}
|
|
150 |
flib_gameconn_destroy(tempConn);
|
|
151 |
return result;
|
|
152 |
}
|
|
153 |
|
|
154 |
flib_gameconn *flib_gameconn_create_campaign(const char *playerName, const char *seed, const char *script) {
|
|
155 |
if(log_badargs_if3(playerName==NULL, seed==NULL, script==NULL)) {
|
|
156 |
return NULL;
|
|
157 |
}
|
|
158 |
flib_gameconn *result = NULL;
|
|
159 |
flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, false);
|
|
160 |
if(tempConn) {
|
|
161 |
if(!flib_ipc_append_message(tempConn->configBuffer, "TL")
|
|
162 |
&& !flib_ipc_append_seed(tempConn->configBuffer, seed)
|
|
163 |
&& !flib_ipc_append_script(tempConn->configBuffer, script)
|
|
164 |
&& !flib_ipc_append_message(tempConn->configBuffer, "!")) {
|
|
165 |
result = tempConn;
|
|
166 |
tempConn = NULL;
|
|
167 |
}
|
|
168 |
}
|
|
169 |
flib_gameconn_destroy(tempConn);
|
|
170 |
return result;
|
|
171 |
}
|
|
172 |
|
|
173 |
void flib_gameconn_destroy(flib_gameconn *conn) {
|
|
174 |
if(conn) {
|
|
175 |
if(conn->running) {
|
|
176 |
/*
|
|
177 |
* The function was called from a callback, so the tick function is still running
|
|
178 |
* and we delay the actual destruction. We ensure no further callbacks will be
|
|
179 |
* sent to prevent surprises.
|
|
180 |
*/
|
|
181 |
clearCallbacks(conn);
|
|
182 |
conn->destroyRequested = true;
|
|
183 |
} else {
|
|
184 |
flib_ipcbase_destroy(conn->ipcBase);
|
|
185 |
flib_vector_destroy(conn->configBuffer);
|
|
186 |
flib_vector_destroy(conn->demoBuffer);
|
|
187 |
free(conn->playerName);
|
|
188 |
free(conn);
|
|
189 |
}
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
int flib_gameconn_getport(flib_gameconn *conn) {
|
|
194 |
if(log_badargs_if(conn==NULL)) {
|
|
195 |
return 0;
|
|
196 |
}
|
|
197 |
return flib_ipcbase_port(conn->ipcBase);
|
|
198 |
}
|
|
199 |
|
|
200 |
static void demo_append(flib_gameconn *conn, const void *data, size_t len) {
|
|
201 |
if(conn->demoBuffer) {
|
|
202 |
if(flib_vector_append(conn->demoBuffer, data, len)) {
|
|
203 |
flib_log_e("Error recording demo: Out of memory.");
|
|
204 |
flib_vector_destroy(conn->demoBuffer);
|
|
205 |
conn->demoBuffer = NULL;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
static int format_chatmessage(uint8_t buffer[257], const char *playerName, const char *message) {
|
|
211 |
size_t msglen = strlen(message);
|
|
212 |
|
|
213 |
// If the message starts with /me, it will be displayed differently.
|
|
214 |
bool meMessage = msglen >= 4 && !memcmp(message, "/me ", 4);
|
|
215 |
const char *template = meMessage ? "s\x02* %s %s " : "s\x01%s: %s ";
|
|
216 |
int size = snprintf((char*)buffer+1, 256, template, playerName, meMessage ? message+4 : message);
|
|
217 |
if(log_e_if(size<=0, "printf error")) {
|
|
218 |
return -1;
|
|
219 |
} else {
|
|
220 |
buffer[0] = size>255 ? 255 : size;
|
|
221 |
return 0;
|
|
222 |
}
|
|
223 |
}
|
|
224 |
|
|
225 |
static void demo_append_chatmessage(flib_gameconn *conn, const char *message) {
|
|
226 |
// Chat messages are reformatted to make them look as if they were received, not sent.
|
|
227 |
uint8_t converted[257];
|
|
228 |
if(!format_chatmessage(converted, conn->playerName, message)) {
|
|
229 |
demo_append(conn, converted, converted[0]+1);
|
|
230 |
}
|
|
231 |
}
|
|
232 |
|
|
233 |
static void demo_replace_gamemode(flib_buffer buf, char gamemode) {
|
|
234 |
size_t msgStart = 0;
|
|
235 |
uint8_t *data = (uint8_t*)buf.data;
|
|
236 |
while(msgStart+2 < buf.size) {
|
|
237 |
if(!memcmp(data+msgStart, "\x02T", 2)) {
|
|
238 |
data[msgStart+2] = gamemode;
|
|
239 |
}
|
|
240 |
msgStart += (uint8_t)data[msgStart]+1;
|
|
241 |
}
|
|
242 |
}
|
|
243 |
|
|
244 |
int flib_gameconn_send_enginemsg(flib_gameconn *conn, const uint8_t *data, size_t len) {
|
|
245 |
if(log_badargs_if2(conn==NULL, data==NULL && len>0)) {
|
|
246 |
return -1;
|
|
247 |
}
|
|
248 |
int result = flib_ipcbase_send_raw(conn->ipcBase, data, len);
|
|
249 |
if(!result) {
|
|
250 |
demo_append(conn, data, len);
|
|
251 |
}
|
|
252 |
return result;
|
|
253 |
}
|
|
254 |
|
|
255 |
int flib_gameconn_send_textmsg(flib_gameconn *conn, int msgtype, const char *msg) {
|
|
256 |
if(log_badargs_if2(conn==NULL, msg==NULL)) {
|
|
257 |
return -1;
|
|
258 |
}
|
|
259 |
int result = -1;
|
|
260 |
uint8_t converted[257];
|
|
261 |
int size = snprintf((char*)converted+1, 256, "s%c%s", (char)msgtype, msg);
|
|
262 |
if(size>0) {
|
|
263 |
converted[0] = size>255 ? 255 : size;
|
|
264 |
if(!flib_ipcbase_send_raw(conn->ipcBase, converted, converted[0]+1)) {
|
|
265 |
demo_append(conn, converted, converted[0]+1);
|
|
266 |
result = 0;
|
|
267 |
}
|
|
268 |
}
|
|
269 |
return result;
|
|
270 |
}
|
|
271 |
|
|
272 |
int flib_gameconn_send_chatmsg(flib_gameconn *conn, const char *playername, const char *msg) {
|
|
273 |
if(log_badargs_if3(conn==NULL, playername==NULL, msg==NULL)) {
|
|
274 |
return -1;
|
|
275 |
}
|
|
276 |
uint8_t converted[257];
|
|
277 |
if(!format_chatmessage(converted, playername, msg)
|
|
278 |
&& !flib_ipcbase_send_raw(conn->ipcBase, converted, converted[0]+1)) {
|
|
279 |
demo_append(conn, converted, converted[0]+1);
|
|
280 |
return 0;
|
|
281 |
}
|
|
282 |
return -1;
|
|
283 |
}
|
|
284 |
|
|
285 |
int flib_gameconn_send_quit(flib_gameconn *conn) {
|
|
286 |
return flib_gameconn_send_cmd(conn, "efinish");
|
|
287 |
}
|
|
288 |
|
|
289 |
int flib_gameconn_send_cmd(flib_gameconn *conn, const char *cmdString) {
|
|
290 |
if(log_badargs_if2(conn==NULL, cmdString==NULL)) {
|
|
291 |
return -1;
|
|
292 |
}
|
|
293 |
int result = -1;
|
|
294 |
uint8_t converted[256];
|
|
295 |
size_t msglen = strlen(cmdString);
|
|
296 |
if(!log_e_if(msglen>255, "Message too long: %s", cmdString)) {
|
|
297 |
strcpy((char*)converted+1, cmdString);
|
|
298 |
converted[0] = msglen;
|
|
299 |
if(!flib_ipcbase_send_raw(conn->ipcBase, converted, msglen+1)) {
|
|
300 |
demo_append(conn, converted, msglen+1);
|
|
301 |
result = 0;
|
|
302 |
}
|
|
303 |
}
|
|
304 |
return result;
|
|
305 |
}
|
|
306 |
|
|
307 |
/**
|
|
308 |
* This macro generates a callback setter function. It uses the name of the callback to
|
|
309 |
* automatically generate the function name and the fields to set, so a consistent naming
|
|
310 |
* convention needs to be enforced (not that that is a bad thing). If null is passed as
|
|
311 |
* callback to the generated function, the defaultCb will be set instead (with conn
|
|
312 |
* as the context).
|
|
313 |
*/
|
|
314 |
#define GENERATE_CB_SETTER(cbName, cbParameterTypes, defaultCb) \
|
|
315 |
void flib_gameconn_##cbName(flib_gameconn *conn, void (*callback)cbParameterTypes, void *context) { \
|
|
316 |
if(!log_badargs_if(conn==NULL)) { \
|
|
317 |
conn->cbName##Cb = callback ? callback : &defaultCb; \
|
|
318 |
conn->cbName##Ctx = callback ? context : conn; \
|
|
319 |
} \
|
|
320 |
}
|
|
321 |
|
|
322 |
/**
|
|
323 |
* Generate a callback setter function like GENERATE_CB_SETTER, and automatically generate a
|
|
324 |
* no-op callback function as well that is used as default.
|
|
325 |
*/
|
|
326 |
#define GENERATE_CB_SETTER_AND_DEFAULT(cbName, cbParameterTypes) \
|
|
327 |
static void _noop_callback_##cbName cbParameterTypes {} \
|
|
328 |
GENERATE_CB_SETTER(cbName, cbParameterTypes, _noop_callback_##cbName)
|
|
329 |
|
|
330 |
GENERATE_CB_SETTER_AND_DEFAULT(onConnect, (void *context));
|
|
331 |
GENERATE_CB_SETTER_AND_DEFAULT(onDisconnect, (void* context, int reason));
|
|
332 |
GENERATE_CB_SETTER(onErrorMessage, (void* context, const char *msg), defaultCallback_onErrorMessage);
|
|
333 |
GENERATE_CB_SETTER_AND_DEFAULT(onChat, (void* context, const char *msg, bool teamchat));
|
|
334 |
GENERATE_CB_SETTER_AND_DEFAULT(onGameRecorded, (void *context, const uint8_t *record, size_t size, bool isSavegame));
|
|
335 |
GENERATE_CB_SETTER_AND_DEFAULT(onEngineMessage, (void *context, const uint8_t *em, size_t size));
|
|
336 |
|
|
337 |
#undef GENERATE_CB_SETTER_AND_DEFAULT
|
|
338 |
#undef GENERATE_CB_SETTER
|
|
339 |
|
|
340 |
static void flib_gameconn_wrappedtick(flib_gameconn *conn) {
|
|
341 |
if(conn->state == AWAIT_CONNECTION) {
|
|
342 |
flib_ipcbase_accept(conn->ipcBase);
|
|
343 |
switch(flib_ipcbase_state(conn->ipcBase)) {
|
|
344 |
case IPC_CONNECTED:
|
|
345 |
{
|
|
346 |
flib_constbuffer configBuffer = flib_vector_as_constbuffer(conn->configBuffer);
|
|
347 |
if(flib_ipcbase_send_raw(conn->ipcBase, configBuffer.data, configBuffer.size)) {
|
|
348 |
conn->state = FINISHED;
|
|
349 |
conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
|
|
350 |
return;
|
|
351 |
} else {
|
|
352 |
demo_append(conn, configBuffer.data, configBuffer.size);
|
|
353 |
conn->state = CONNECTED;
|
|
354 |
conn->onConnectCb(conn->onConnectCtx);
|
|
355 |
if(conn->destroyRequested) {
|
|
356 |
return;
|
|
357 |
}
|
|
358 |
}
|
|
359 |
}
|
|
360 |
break;
|
|
361 |
case IPC_NOT_CONNECTED:
|
|
362 |
conn->state = FINISHED;
|
|
363 |
conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
|
|
364 |
return;
|
|
365 |
default:
|
|
366 |
break;
|
|
367 |
}
|
|
368 |
}
|
|
369 |
|
|
370 |
if(conn->state == CONNECTED) {
|
|
371 |
uint8_t msgbuffer[257];
|
|
372 |
int len;
|
|
373 |
while(!conn->destroyRequested && (len = flib_ipcbase_recv_message(conn->ipcBase, msgbuffer))>=0) {
|
|
374 |
if(len<2) {
|
|
375 |
flib_log_w("Received short message from IPC (<2 bytes)");
|
|
376 |
continue;
|
|
377 |
}
|
|
378 |
switch(msgbuffer[1]) {
|
|
379 |
case '?': // Ping
|
|
380 |
// The pong is already part of the config message
|
|
381 |
break;
|
|
382 |
case 'C': // Config query
|
|
383 |
// And we already send the config message on connecting.
|
|
384 |
break;
|
|
385 |
case 'E': // Error message
|
|
386 |
if(len>=3) {
|
|
387 |
msgbuffer[len-2] = 0;
|
|
388 |
conn->onErrorMessageCb(conn->onErrorMessageCtx, (char*)msgbuffer+2);
|
|
389 |
}
|
|
390 |
break;
|
|
391 |
case 'i': // Statistics
|
|
392 |
// TODO stats
|
|
393 |
break;
|
|
394 |
case 'Q': // Game interrupted
|
|
395 |
case 'H': // Game halted
|
|
396 |
case 'q': // game finished
|
|
397 |
{
|
|
398 |
int reason = msgbuffer[1]=='Q' ? GAME_END_INTERRUPTED : msgbuffer[1]=='H' ? GAME_END_HALTED : GAME_END_FINISHED;
|
|
399 |
conn->disconnectReason = reason;
|
|
400 |
bool savegame = (reason != GAME_END_FINISHED) && !conn->netgame;
|
|
401 |
if(conn->demoBuffer) {
|
|
402 |
flib_buffer demoBuffer = flib_vector_as_buffer(conn->demoBuffer);
|
|
403 |
demo_replace_gamemode(demoBuffer, savegame ? 'S' : 'D');
|
|
404 |
conn->onGameRecordedCb(conn->onGameRecordedCtx, demoBuffer.data, demoBuffer.size, savegame);
|
|
405 |
if(conn->destroyRequested) {
|
|
406 |
return;
|
|
407 |
}
|
|
408 |
}
|
|
409 |
return;
|
|
410 |
}
|
|
411 |
case 's': // Chat message
|
|
412 |
if(len>=3) {
|
|
413 |
msgbuffer[len-2] = 0;
|
|
414 |
demo_append_chatmessage(conn, (char*)msgbuffer+2);
|
|
415 |
|
|
416 |
conn->onChatCb(conn->onChatCtx, (char*)msgbuffer+2, false);
|
|
417 |
}
|
|
418 |
break;
|
|
419 |
case 'b': // Teamchat message
|
|
420 |
if(len>=3) {
|
|
421 |
msgbuffer[len-2] = 0;
|
|
422 |
conn->onChatCb(conn->onChatCtx, (char*)msgbuffer+2, true);
|
|
423 |
}
|
|
424 |
break;
|
|
425 |
default: // Engine message
|
|
426 |
demo_append(conn, msgbuffer, len);
|
|
427 |
|
|
428 |
conn->onEngineMessageCb(conn->onEngineMessageCtx, msgbuffer, len);
|
|
429 |
break;
|
|
430 |
}
|
|
431 |
}
|
|
432 |
}
|
|
433 |
|
|
434 |
if(flib_ipcbase_state(conn->ipcBase) == IPC_NOT_CONNECTED) {
|
|
435 |
conn->state = FINISHED;
|
|
436 |
conn->onDisconnectCb(conn->onDisconnectCtx, conn->disconnectReason);
|
|
437 |
}
|
|
438 |
}
|
|
439 |
|
|
440 |
void flib_gameconn_tick(flib_gameconn *conn) {
|
|
441 |
if(!log_badargs_if(conn == NULL)
|
|
442 |
&& !log_w_if(conn->running, "Call to flib_gameconn_tick from a callback")
|
|
443 |
&& !log_w_if(conn->state == FINISHED, "We are already done.")) {
|
|
444 |
conn->running = true;
|
|
445 |
flib_gameconn_wrappedtick(conn);
|
|
446 |
conn->running = false;
|
|
447 |
|
|
448 |
if(conn->destroyRequested) {
|
|
449 |
flib_gameconn_destroy(conn);
|
|
450 |
}
|
|
451 |
}
|
|
452 |
}
|