project_files/frontlib/ipc/ipcprotocol.c
changeset 7314 6171f0bad318
parent 7275 15f722e0b96f
child 7316 f7b49b2c5d84
equal deleted inserted replaced
7312:d1db8aaa8edc 7314:6171f0bad318
       
     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 
     1 #include "ipcprotocol.h"
    20 #include "ipcprotocol.h"
     2 #include "../util/util.h"
    21 #include "../util/util.h"
     3 #include "../util/logging.h"
    22 #include "../util/logging.h"
       
    23 #include "../md5/md5.h"
     4 
    24 
     5 #include <stdio.h>
    25 #include <stdio.h>
     6 #include <stdbool.h>
    26 #include <stdbool.h>
     7 #include <string.h>
    27 #include <string.h>
     8 #include <inttypes.h>
    28 #include <inttypes.h>
   159 		|| flib_ipc_append_message(vec, "eammprob %s", set->crateprob)
   179 		|| flib_ipc_append_message(vec, "eammprob %s", set->crateprob)
   160 		|| flib_ipc_append_message(vec, "eammdelay %s", set->delay)
   180 		|| flib_ipc_append_message(vec, "eammdelay %s", set->delay)
   161 		|| flib_ipc_append_message(vec, "eammreinf %s", set->crateammo);
   181 		|| flib_ipc_append_message(vec, "eammreinf %s", set->crateammo);
   162 }
   182 }
   163 
   183 
       
   184 static void calculateMd5Hex(const char *in, char out[33]) {
       
   185 	if(!log_badparams_if(!in)) {
       
   186 		md5_state_t md5state;
       
   187 		uint8_t md5bytes[16];
       
   188 		md5_init(&md5state);
       
   189 		md5_append(&md5state, (unsigned char*)in, strlen(in));
       
   190 		md5_finish(&md5state, md5bytes);
       
   191 		for(int i=0;i<sizeof(md5bytes); i++) {
       
   192 			snprintf(out+i*2, 3, "%02x", (unsigned)md5bytes[i]);
       
   193 		}
       
   194 	}
       
   195 }
       
   196 
   164 int flib_ipc_append_addteam(flib_vector *vec, const flib_team *team, bool perHogAmmo, bool noAmmoStore) {
   197 int flib_ipc_append_addteam(flib_vector *vec, const flib_team *team, bool perHogAmmo, bool noAmmoStore) {
   165 	int result = -1;
   198 	int result = -1;
   166 	flib_vector *tempvector = flib_vector_create();
   199 	flib_vector *tempvector = flib_vector_create();
   167 	if(!log_badparams_if(!vec || !team) && tempvector) {
   200 	if(!log_badparams_if(!vec || !team) && tempvector) {
   168 		bool error = false;
   201 		bool error = false;
   171 			error = error
   204 			error = error
   172 					|| appendWeaponSet(tempvector, team->hogs[0].weaponset)
   205 					|| appendWeaponSet(tempvector, team->hogs[0].weaponset)
   173 					|| flib_ipc_append_message(tempvector, "eammstore");
   206 					|| flib_ipc_append_message(tempvector, "eammstore");
   174 		}
   207 		}
   175 
   208 
   176 		// TODO
   209 		char md5Hex[33];
   177 		char *hash = team->ownerName ? team->ownerName : "00000000000000000000000000000000";
   210 		calculateMd5Hex(team->ownerName ? team->ownerName : "", md5Hex);
   178 		if(team->colorIndex<0 || team->colorIndex>=flib_teamcolor_defaults_len) {
   211 		if(team->colorIndex<0 || team->colorIndex>=flib_teamcolor_defaults_len) {
   179 			flib_log_e("Color index out of bounds for team %s: %i", team->name, team->colorIndex);
   212 			flib_log_e("Color index out of bounds for team %s: %i", team->name, team->colorIndex);
   180 			error = true;
   213 			error = true;
   181 		} else {
   214 		} else {
   182 			error |= flib_ipc_append_message(tempvector, "eaddteam %s %"PRIu32" %s", hash, flib_teamcolor_defaults[team->colorIndex], team->name);
   215 			error |= flib_ipc_append_message(tempvector, "eaddteam %s %"PRIu32" %s", md5Hex, flib_teamcolor_defaults[team->colorIndex], team->name);
   183 		}
   216 		}
   184 
   217 
   185 		if(team->remoteDriven) {
   218 		if(team->remoteDriven) {
   186 			error |= flib_ipc_append_message(tempvector, "erdriven");
   219 			error |= flib_ipc_append_message(tempvector, "erdriven");
   187 		}
   220 		}
   213 	}
   246 	}
   214 	flib_vector_destroy(tempvector);
   247 	flib_vector_destroy(tempvector);
   215 	return result;
   248 	return result;
   216 }
   249 }
   217 
   250 
   218 static bool getGameMod(const flib_cfg *conf, const char *name) {
       
   219 	for(int i=0; i<conf->meta->modCount; i++) {
       
   220 		if(!strcmp(conf->meta->mods[i].name, name)) {
       
   221 			return conf->mods[i];
       
   222 		}
       
   223 	}
       
   224 	flib_log_e("Unable to find game mod %s", name);
       
   225 	return false;
       
   226 }
       
   227 
       
   228 int flib_ipc_append_fullconfig(flib_vector *vec, const flib_gamesetup *setup, bool netgame) {
   251 int flib_ipc_append_fullconfig(flib_vector *vec, const flib_gamesetup *setup, bool netgame) {
   229 	int result = -1;
   252 	int result = -1;
   230 	flib_vector *tempvector = flib_vector_create();
   253 	flib_vector *tempvector = flib_vector_create();
   231 	if(!log_badparams_if(!vec || !setup) && tempvector) {
   254 	if(!log_badparams_if(!vec || !setup) && tempvector) {
   232 		bool error = false;
   255 		bool error = false;
   240 		if(setup->script) {
   263 		if(setup->script) {
   241 			error |= flib_ipc_append_script(tempvector, setup->script);
   264 			error |= flib_ipc_append_script(tempvector, setup->script);
   242 		}
   265 		}
   243 		if(setup->gamescheme) {
   266 		if(setup->gamescheme) {
   244 			error |= flib_ipc_append_gamescheme(tempvector, setup->gamescheme);
   267 			error |= flib_ipc_append_gamescheme(tempvector, setup->gamescheme);
   245 			sharedAmmo = getGameMod(setup->gamescheme, "sharedammo");
   268 			sharedAmmo = flib_cfg_get_mod(setup->gamescheme, "sharedammo");
   246 			// Shared ammo has priority over per-hog ammo
   269 			// Shared ammo has priority over per-hog ammo
   247 			perHogAmmo = !sharedAmmo && getGameMod(setup->gamescheme, "perhogammo");
   270 			perHogAmmo = !sharedAmmo && flib_cfg_get_mod(setup->gamescheme, "perhogammo");
   248 		}
   271 		}
   249 		if(setup->teamlist->teams && setup->teamlist->teamCount>0) {
   272 		if(setup->teamlist->teams && setup->teamlist->teamCount>0) {
   250 			int *clanColors = flib_calloc(setup->teamlist->teamCount, sizeof(int));
   273 			int *clanColors = flib_calloc(setup->teamlist->teamCount, sizeof(int));
   251 			if(!clanColors) {
   274 			if(!clanColors) {
   252 				error = true;
   275 				error = true;