project_files/frontlib/ipc/gameconn.c
author Medo <smaxein@googlemail.com>
Fri, 15 Jun 2012 19:57:25 +0200
changeset 7230 240620f46dd7
parent 7224 5143861c83bd
child 7234 613998625a3c
permissions -rw-r--r--
Changed frontlib to use the existing ini file formats of the QtFrontend
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "gameconn.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
#include "ipcconn.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
#include "ipcprotocol.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
#include "../util/logging.h"
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     5
#include "../util/util.h"
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
#include "../hwconsts.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
#include <stdbool.h>
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
#include <stdlib.h>
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     9
#include <string.h>
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
typedef enum {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
	AWAIT_CONNECTION,
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
	CONNECTED,
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
	FINISHED
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
} gameconn_state;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
struct _flib_gameconn {
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    18
	flib_ipcconn *connection;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    19
	flib_vector *configBuffer;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    20
	flib_vector *demoBuffer;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    21
	char *playerName;
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
	gameconn_state state;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
	bool netgame;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
	void (*onConnectCb)(void* context);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
	void *onConnectCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
	void (*onDisconnectCb)(void* context, int reason);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
	void *onDisconnectCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
	void (*onErrorMessageCb)(void* context, const char *msg);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
	void *onErrorMessageCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
	void (*onChatCb)(void* context, const char *msg, bool teamchat);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
	void *onChatCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
	void (*onGameRecordedCb)(void *context, const uint8_t *record, int size, bool isSavegame);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
	void *onGameRecordedCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	void (*onNetMessageCb)(void *context, const uint8_t *em, int size);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	void *onNetMessageCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
	bool running;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
	bool destroyRequested;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
};
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
static void defaultCallback_onConnect(void* context) {}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
static void defaultCallback_onDisconnect(void* context, int reason) {}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
static void defaultCallback_onErrorMessage(void* context, const char *msg) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
	flib_log_w("Error from engine (no callback set): %s", msg);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
static void defaultCallback_onChat(void* context, const char *msg, bool teamchat) {}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
static void defaultCallback_onGameRecorded(void *context, const uint8_t *record, int size, bool isSavegame) {}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
static void defaultCallback_onNetMessage(void *context, const uint8_t *em, int size) {}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
static void clearCallbacks(flib_gameconn *conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
	conn->onConnectCb = &defaultCallback_onConnect;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
	conn->onDisconnectCb = &defaultCallback_onDisconnect;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
	conn->onErrorMessageCb = &defaultCallback_onErrorMessage;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
	conn->onChatCb = &defaultCallback_onChat;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
	conn->onGameRecordedCb = &defaultCallback_onGameRecorded;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
	conn->onNetMessageCb = &defaultCallback_onNetMessage;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
static flib_gameconn *flib_gameconn_create_partial(bool record, const char *playerName, bool netGame) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
	flib_gameconn *result = NULL;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    68
	flib_gameconn *tempConn = flib_calloc(1, sizeof(flib_gameconn));
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
	if(tempConn) {
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    70
		tempConn->connection = flib_ipcconn_create();
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
		tempConn->configBuffer = flib_vector_create();
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    72
		tempConn->playerName = flib_strdupnull(playerName);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    73
		if(tempConn->connection && tempConn->configBuffer && tempConn->playerName) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    74
			if(record) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    75
				tempConn->demoBuffer = flib_vector_create();
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    76
			}
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
			tempConn->state = AWAIT_CONNECTION;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
			tempConn->netgame = netGame;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
			clearCallbacks(tempConn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
			result = tempConn;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
			tempConn = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
	flib_gameconn_destroy(tempConn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
	return result;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    88
flib_gameconn *flib_gameconn_create(const char *playerName, flib_gamesetup *setup, bool netgame) {
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
	flib_gameconn *result = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
	flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, netgame);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
	if(tempConn) {
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    92
		if(!flib_ipc_append_fullconfig(tempConn->configBuffer, setup, netgame)) {
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
			result = tempConn;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
			tempConn = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    96
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    97
	flib_gameconn_destroy(tempConn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
	return result;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   101
flib_gameconn *flib_gameconn_create_playdemo(const uint8_t *demo, int size) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
	flib_gameconn *result = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   103
	flib_gameconn *tempConn = flib_gameconn_create_partial(false, "Player", false);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
	if(tempConn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   105
		if(flib_vector_append(tempConn->configBuffer, demo, size) == size) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   106
			result = tempConn;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   107
			tempConn = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   109
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   110
	flib_gameconn_destroy(tempConn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   111
	return result;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   113
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   114
flib_gameconn *flib_gameconn_create_loadgame(const char *playerName, const uint8_t *save, int size) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   115
	flib_gameconn *result = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   116
	flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, false);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   117
	if(tempConn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   118
		if(flib_vector_append(tempConn->configBuffer, save, size) == size) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
			result = tempConn;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   120
			tempConn = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   122
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   123
	flib_gameconn_destroy(tempConn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   124
	return result;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   125
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   126
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   127
void flib_gameconn_destroy(flib_gameconn *conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   128
	if(conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
		if(conn->running) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   130
			/*
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   131
			 * The function was called from a callback, so the tick function is still running
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   132
			 * and we delay the actual destruction. We ensure no further callbacks will be
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   133
			 * sent to prevent surprises.
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   134
			 */
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   135
			clearCallbacks(conn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   136
			conn->destroyRequested = true;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   137
		} else {
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   138
			flib_ipcconn_destroy(conn->connection);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   139
			flib_vector_destroy(conn->configBuffer);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   140
			flib_vector_destroy(conn->demoBuffer);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   141
			free(conn->playerName);
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   142
			free(conn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   143
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   144
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   145
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   146
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   147
int flib_gameconn_getport(flib_gameconn *conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   148
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   149
		flib_log_e("null parameter in flib_gameconn_getport");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   150
		return 0;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   151
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   152
		return flib_ipcconn_port(conn->connection);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   153
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   154
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   155
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   156
static void demo_append(flib_gameconn *conn, const void *data, size_t len) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   157
	if(conn->demoBuffer) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   158
		if(flib_vector_append(conn->demoBuffer, data, len) < len) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   159
			flib_log_e("Error recording demo: Out of memory.");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   160
			flib_vector_destroy(conn->demoBuffer);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   161
			conn->demoBuffer = NULL;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   162
		}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   163
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   164
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   165
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   166
static int format_chatmessage(uint8_t buffer[257], const char *playerName, const char *message) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   167
	size_t msglen = strlen(message);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   168
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   169
	// If the message starts with /me, it will be displayed differently.
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   170
	bool meMessage = msglen >= 4 && !memcmp(message, "/me ", 4);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   171
	const char *template = meMessage ? "s\x02* %s %s  " : "s\x01%s: %s  ";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   172
	int size = snprintf((char*)buffer+1, 256, template, playerName, meMessage ? message+4 : message);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   173
	if(size>0) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   174
		buffer[0] = size>255 ? 255 : size;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   175
		return 0;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   176
	} else {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   177
		return -1;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   178
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   179
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   180
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   181
static void demo_append_chatmessage(flib_gameconn *conn, const char *message) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   182
	// Chat messages are reformatted to make them look as if they were received, not sent.
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   183
	uint8_t converted[257];
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   184
	if(!format_chatmessage(converted, conn->playerName, message)) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   185
		demo_append(conn, converted, converted[0]+1);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   186
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   187
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   188
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   189
static void demo_replace_gamemode(flib_buffer buf, char gamemode) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   190
	size_t msgStart = 0;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   191
	uint8_t *data = (uint8_t*)buf.data;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   192
	while(msgStart+2 < buf.size) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   193
		if(!memcmp(data+msgStart, "\x02T", 2)) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   194
			data[msgStart+2] = gamemode;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   195
		}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   196
		msgStart += (uint8_t)data[msgStart]+1;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   197
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   198
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   199
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   200
int flib_gameconn_send_enginemsg(flib_gameconn *conn, uint8_t *data, int len) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   201
	int result = -1;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   202
	if(!conn || (!data && len>0)) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   203
		flib_log_e("null parameter in flib_gameconn_send_enginemsg");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   204
	} else if(!flib_ipcconn_send_raw(conn->connection, data, len)) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   205
		demo_append(conn, data, len);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   206
		result = 0;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   207
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   208
	return result;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   209
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   210
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   211
int flib_gameconn_send_textmsg(flib_gameconn *conn, int msgtype, const char *msg) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   212
	int result = -1;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   213
	if(!conn || !msg) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   214
		flib_log_e("null parameter in flib_gameconn_send_textmsg");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   215
	} else {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   216
		uint8_t converted[257];
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   217
		int size = snprintf((char*)converted+1, 256, "s%c%s", (char)msgtype, msg);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   218
		if(size>0) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   219
			converted[0] = size>255 ? 255 : size;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   220
			if(!flib_ipcconn_send_raw(conn->connection, converted, converted[0]+1)) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   221
				demo_append(conn, converted, converted[0]+1);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   222
				result = 0;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   223
			}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   224
		}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   225
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   226
	return result;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   227
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   228
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   229
int flib_gameconn_send_chatmsg(flib_gameconn *conn, const char *playername, const char *msg) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   230
	int result = -1;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   231
	uint8_t converted[257];
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   232
	if(!conn || !playername || !msg) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   233
		flib_log_e("null parameter in flib_gameconn_send_chatmsg");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   234
	} else if(format_chatmessage(converted, playername, msg)) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   235
		flib_log_e("Error formatting message in flib_gameconn_send_chatmsg");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   236
	} else if(!flib_ipcconn_send_raw(conn->connection, converted, converted[0]+1)) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   237
		demo_append(conn, converted, converted[0]+1);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   238
		result = 0;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   239
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   240
	return result;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   241
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   242
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   243
void flib_gameconn_onConnect(flib_gameconn *conn, void (*callback)(void* context), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   244
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   245
		flib_log_e("null parameter in flib_gameconn_onConnect");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   246
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   247
		conn->onConnectCb = callback ? callback : &defaultCallback_onConnect;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   248
		conn->onConnectCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   249
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   250
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   251
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   252
void flib_gameconn_onDisconnect(flib_gameconn *conn, void (*callback)(void* context, int reason), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   253
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   254
		flib_log_e("null parameter in flib_gameconn_onDisconnect");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   255
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   256
		conn->onDisconnectCb = callback ? callback : &defaultCallback_onDisconnect;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   257
		conn->onDisconnectCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   258
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   259
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   260
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   261
void flib_gameconn_onErrorMessage(flib_gameconn *conn, void (*callback)(void* context, const char *msg), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   262
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   263
		flib_log_e("null parameter in flib_gameconn_onErrorMessage");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   264
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   265
		conn->onErrorMessageCb = callback ? callback : &defaultCallback_onErrorMessage;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   266
		conn->onErrorMessageCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   267
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   268
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   269
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   270
void flib_gameconn_onChat(flib_gameconn *conn, void (*callback)(void* context, const char *msg, bool teamchat), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   271
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   272
		flib_log_e("null parameter in flib_gameconn_onChat");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   273
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   274
		conn->onChatCb = callback ? callback : &defaultCallback_onChat;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   275
		conn->onChatCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   276
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   277
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   278
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   279
void flib_gameconn_onGameRecorded(flib_gameconn *conn, void (*callback)(void *context, const uint8_t *record, int size, bool isSavegame), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   280
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   281
		flib_log_e("null parameter in flib_gameconn_onGameRecorded");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   282
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   283
		conn->onGameRecordedCb = callback ? callback : &defaultCallback_onGameRecorded;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   284
		conn->onGameRecordedCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   285
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   286
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   287
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   288
void flib_gameconn_onNetMessage(flib_gameconn *conn, void (*callback)(void *context, const uint8_t *em, int size), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   289
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   290
		flib_log_e("null parameter in flib_gameconn_onNetMessage");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   291
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   292
		conn->onNetMessageCb = callback ? callback : &defaultCallback_onNetMessage;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   293
		conn->onNetMessageCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   294
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   295
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   296
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   297
static void flib_gameconn_wrappedtick(flib_gameconn *conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   298
	if(conn->state == AWAIT_CONNECTION) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   299
		flib_ipcconn_accept(conn->connection);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   300
		switch(flib_ipcconn_state(conn->connection)) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   301
		case IPC_CONNECTED:
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   302
			{
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   303
				flib_constbuffer configBuffer = flib_vector_as_constbuffer(conn->configBuffer);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   304
				if(flib_ipcconn_send_raw(conn->connection, configBuffer.data, configBuffer.size)) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   305
					conn->state = FINISHED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   306
					conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   307
					return;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   308
				} else {
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   309
					demo_append(conn, configBuffer.data, configBuffer.size);
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   310
					conn->state = CONNECTED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   311
					conn->onConnectCb(conn->onConnectCtx);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   312
					if(conn->destroyRequested) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   313
						return;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   314
					}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   315
				}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   316
			}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   317
			break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   318
		case IPC_NOT_CONNECTED:
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   319
			conn->state = FINISHED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   320
			conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   321
			return;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   322
		default:
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   323
			break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   324
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   325
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   326
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   327
	if(conn->state == CONNECTED) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   328
		uint8_t msgbuffer[257];
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   329
		int len;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   330
		while(!conn->destroyRequested && (len = flib_ipcconn_recv_message(conn->connection, msgbuffer))>=0) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   331
			if(len<2) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   332
				flib_log_w("Received short message from IPC (<2 bytes)");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   333
				continue;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   334
			}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   335
			switch(msgbuffer[1]) {
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   336
			case '?':	// Ping
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   337
				// The pong is already part of the config message
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   338
				break;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   339
			case 'C':	// Config query
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   340
				// And we already send the config message on connecting.
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   341
				break;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   342
			case 'E':	// Error message
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   343
				if(len>=3) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   344
					msgbuffer[len-2] = 0;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   345
					conn->onErrorMessageCb(conn->onErrorMessageCtx, (char*)msgbuffer+2);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   346
				}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   347
				break;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   348
			case 'i':	// Statistics
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   349
				// TODO stats
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   350
				break;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   351
			case 'Q':	// Game interrupted
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   352
			case 'H':	// Game halted
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   353
			case 'q':	// game finished
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   354
				{
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   355
					int reason = msgbuffer[1]=='Q' ? GAME_END_INTERRUPTED : msgbuffer[1]=='H' ? GAME_END_HALTED : GAME_END_FINISHED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   356
					bool savegame = (reason != GAME_END_FINISHED) && !conn->netgame;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   357
					if(conn->demoBuffer) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   358
						flib_buffer demoBuffer = flib_vector_as_buffer(conn->demoBuffer);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   359
						demo_replace_gamemode(demoBuffer, savegame ? 'S' : 'D');
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   360
						conn->onGameRecordedCb(conn->onGameRecordedCtx, demoBuffer.data, demoBuffer.size, savegame);
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   361
						if(conn->destroyRequested) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   362
							return;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   363
						}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   364
					}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   365
					conn->state = FINISHED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   366
					conn->onDisconnectCb(conn->onDisconnectCtx, reason);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   367
					return;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   368
				}
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   369
			case 's':	// Chat message
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   370
				if(len>=3) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   371
					msgbuffer[len-2] = 0;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   372
					demo_append_chatmessage(conn, (char*)msgbuffer+2);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   373
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   374
					conn->onChatCb(conn->onChatCtx, (char*)msgbuffer+2, false);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   375
				}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   376
				break;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   377
			case 'b':	// Teamchat message
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   378
				if(len>=3) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   379
					msgbuffer[len-2] = 0;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   380
					conn->onChatCb(conn->onChatCtx, (char*)msgbuffer+2, true);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   381
				}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   382
				break;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   383
			default:	// Engine message
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   384
				demo_append(conn, msgbuffer, len);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   385
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   386
				conn->onNetMessageCb(conn->onNetMessageCtx, msgbuffer, len);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   387
				break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   388
			}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   389
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   390
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   391
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   392
	if(flib_ipcconn_state(conn->connection) == IPC_NOT_CONNECTED) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   393
		conn->state = FINISHED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   394
		conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   395
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   396
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   397
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   398
void flib_gameconn_tick(flib_gameconn *conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   399
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   400
		flib_log_e("null parameter in flib_gameconn_tick");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   401
	} else if(conn->running) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   402
		flib_log_w("Call to flib_gameconn_tick from a callback");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   403
	} else if(conn->state == FINISHED) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   404
		flib_log_w("Call to flib_gameconn_tick, but we are already done.");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   405
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   406
		conn->running = true;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   407
		flib_gameconn_wrappedtick(conn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   408
		conn->running = false;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   409
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   410
		if(conn->destroyRequested) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   411
			flib_gameconn_destroy(conn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   412
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   413
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   414
}