project_files/frontlib/ipc/ipcprotocol.c
changeset 7316 f7b49b2c5d84
parent 7314 6171f0bad318
child 7320 e704706008d4
equal deleted inserted replaced
7314:6171f0bad318 7316:f7b49b2c5d84
    28 #include <inttypes.h>
    28 #include <inttypes.h>
    29 #include <stdlib.h>
    29 #include <stdlib.h>
    30 
    30 
    31 int flib_ipc_append_message(flib_vector *vec, const char *fmt, ...) {
    31 int flib_ipc_append_message(flib_vector *vec, const char *fmt, ...) {
    32 	int result = -1;
    32 	int result = -1;
    33 	if(!log_badparams_if(!vec || !fmt)) {
    33 	if(!log_badargs_if2(vec==NULL, fmt==NULL)) {
    34 		// 1 byte size prefix, 255 bytes max message length, 1 0-byte for vsnprintf
    34 		// 1 byte size prefix, 255 bytes max message length, 1 0-byte for vsnprintf
    35 		char msgbuffer[257];
    35 		char msgbuffer[257];
    36 
    36 
    37 		// Format the message, leaving one byte at the start for the length
    37 		// Format the message, leaving one byte at the start for the length
    38 		va_list argp;
    38 		va_list argp;
    53 }
    53 }
    54 
    54 
    55 int flib_ipc_append_mapconf(flib_vector *vec, const flib_map *map, bool mappreview) {
    55 int flib_ipc_append_mapconf(flib_vector *vec, const flib_map *map, bool mappreview) {
    56 	int result = -1;
    56 	int result = -1;
    57 	flib_vector *tempvector = flib_vector_create();
    57 	flib_vector *tempvector = flib_vector_create();
    58 	if(!log_badparams_if(!vec || !map)) {
    58 	if(!log_badargs_if2(vec==NULL, map==NULL)) {
    59 		bool error = false;
    59 		bool error = false;
    60 
    60 
    61 		if(map->mapgen == MAPGEN_NAMED) {
    61 		if(map->mapgen == MAPGEN_NAMED) {
    62 			error |= log_e_if(!map->name, "Missing map name")
    62 			error |= log_e_if(!map->name, "Missing map name")
    63 					|| flib_ipc_append_message(tempvector, "emap %s", map->name);
    63 					|| flib_ipc_append_message(tempvector, "emap %s", map->name);
   101 	flib_vector_destroy(tempvector);
   101 	flib_vector_destroy(tempvector);
   102 	return result;
   102 	return result;
   103 }
   103 }
   104 
   104 
   105 int flib_ipc_append_seed(flib_vector *vec, const char *seed) {
   105 int flib_ipc_append_seed(flib_vector *vec, const char *seed) {
   106 	if(!log_badparams_if(!vec || !seed)) {
   106 	if(log_badargs_if2(vec==NULL, seed==NULL)) {
   107 		return flib_ipc_append_message(vec, "eseed %s", seed);
   107 		return -1;
   108 	}
   108 	}
   109 	return -1;
   109 	return flib_ipc_append_message(vec, "eseed %s", seed);
   110 }
   110 }
   111 
   111 
   112 int flib_ipc_append_script(flib_vector *vec, const char *script) {
   112 int flib_ipc_append_script(flib_vector *vec, const char *script) {
   113 	int result = -1;
   113 	int result = -1;
   114 	char *copy = flib_strdupnull(script);
   114 	char *copy = flib_strdupnull(script);
   115 	if(!log_badparams_if(!vec) && copy) {
   115 	if(!log_badargs_if(vec==NULL) && copy) {
   116 		if(!strcmp("Normal", copy)) {
   116 		if(!strcmp("Normal", copy)) {
   117 			// "Normal" means no gametype script
   117 			// "Normal" means no gametype script
   118 			result = 0;
   118 			result = 0;
   119 		} else {
   119 		} else {
   120 			size_t len = strlen(copy);
   120 			size_t len = strlen(copy);
   129 	}
   129 	}
   130 	free(copy);
   130 	free(copy);
   131 	return result;
   131 	return result;
   132 }
   132 }
   133 
   133 
   134 uint32_t buildModFlags(const flib_cfg *scheme) {
   134 static uint32_t buildModFlags(const flib_cfg *scheme) {
   135 	uint32_t result = 0;
   135 	uint32_t result = 0;
   136 	for(int i=0; i<scheme->meta->modCount; i++) {
   136 	for(int i=0; i<scheme->meta->modCount; i++) {
   137 		if(scheme->mods[i]) {
   137 		if(scheme->mods[i]) {
   138 			int bitmaskIndex = scheme->meta->mods[i].bitmaskIndex;
   138 			int bitmaskIndex = scheme->meta->mods[i].bitmaskIndex;
   139 			result |= (UINT32_C(1) << bitmaskIndex);
   139 			result |= (UINT32_C(1) << bitmaskIndex);
   143 }
   143 }
   144 
   144 
   145 int flib_ipc_append_gamescheme(flib_vector *vec, const flib_cfg *scheme) {
   145 int flib_ipc_append_gamescheme(flib_vector *vec, const flib_cfg *scheme) {
   146 	int result = -1;
   146 	int result = -1;
   147 	flib_vector *tempvector = flib_vector_create();
   147 	flib_vector *tempvector = flib_vector_create();
   148 	if(!log_badparams_if(!vec || !scheme) && tempvector) {
   148 	if(!log_badargs_if2(vec==NULL, scheme==NULL) && tempvector) {
   149 		const flib_cfg_meta *meta = scheme->meta;
   149 		const flib_cfg_meta *meta = scheme->meta;
   150 		bool error = false;
   150 		bool error = false;
   151 		error |= flib_ipc_append_message(tempvector, "e$gmflags %"PRIu32, buildModFlags(scheme));
   151 		error |= flib_ipc_append_message(tempvector, "e$gmflags %"PRIu32, buildModFlags(scheme));
   152 		for(int i=0; i<meta->settingCount; i++) {
   152 		for(int i=0; i<meta->settingCount; i++) {
   153 			if(meta->settings[i].engineCommand) {
   153 			if(meta->settings[i].engineCommand) {
   180 		|| flib_ipc_append_message(vec, "eammdelay %s", set->delay)
   180 		|| flib_ipc_append_message(vec, "eammdelay %s", set->delay)
   181 		|| flib_ipc_append_message(vec, "eammreinf %s", set->crateammo);
   181 		|| flib_ipc_append_message(vec, "eammreinf %s", set->crateammo);
   182 }
   182 }
   183 
   183 
   184 static void calculateMd5Hex(const char *in, char out[33]) {
   184 static void calculateMd5Hex(const char *in, char out[33]) {
   185 	if(!log_badparams_if(!in)) {
   185 	md5_state_t md5state;
   186 		md5_state_t md5state;
   186 	uint8_t md5bytes[16];
   187 		uint8_t md5bytes[16];
   187 	md5_init(&md5state);
   188 		md5_init(&md5state);
   188 	md5_append(&md5state, (unsigned char*)in, strlen(in));
   189 		md5_append(&md5state, (unsigned char*)in, strlen(in));
   189 	md5_finish(&md5state, md5bytes);
   190 		md5_finish(&md5state, md5bytes);
   190 	for(int i=0;i<sizeof(md5bytes); i++) {
   191 		for(int i=0;i<sizeof(md5bytes); i++) {
   191 		snprintf(out+i*2, 3, "%02x", (unsigned)md5bytes[i]);
   192 			snprintf(out+i*2, 3, "%02x", (unsigned)md5bytes[i]);
       
   193 		}
       
   194 	}
   192 	}
   195 }
   193 }
   196 
   194 
   197 int flib_ipc_append_addteam(flib_vector *vec, const flib_team *team, bool perHogAmmo, bool noAmmoStore) {
   195 int flib_ipc_append_addteam(flib_vector *vec, const flib_team *team, bool perHogAmmo, bool noAmmoStore) {
   198 	int result = -1;
   196 	int result = -1;
   199 	flib_vector *tempvector = flib_vector_create();
   197 	flib_vector *tempvector = flib_vector_create();
   200 	if(!log_badparams_if(!vec || !team) && tempvector) {
   198 	if(!log_badargs_if2(vec==NULL, team==NULL) && tempvector) {
   201 		bool error = false;
   199 		bool error = false;
   202 
   200 
   203 		if(!perHogAmmo && !noAmmoStore) {
   201 		if(!perHogAmmo && !noAmmoStore) {
   204 			error = error
   202 			error = error
   205 					|| appendWeaponSet(tempvector, team->hogs[0].weaponset)
   203 					|| appendWeaponSet(tempvector, team->hogs[0].weaponset)
   249 }
   247 }
   250 
   248 
   251 int flib_ipc_append_fullconfig(flib_vector *vec, const flib_gamesetup *setup, bool netgame) {
   249 int flib_ipc_append_fullconfig(flib_vector *vec, const flib_gamesetup *setup, bool netgame) {
   252 	int result = -1;
   250 	int result = -1;
   253 	flib_vector *tempvector = flib_vector_create();
   251 	flib_vector *tempvector = flib_vector_create();
   254 	if(!log_badparams_if(!vec || !setup) && tempvector) {
   252 	if(!log_badargs_if2(vec==NULL, setup==NULL) && tempvector) {
   255 		bool error = false;
   253 		bool error = false;
   256 		bool perHogAmmo = false;
   254 		bool perHogAmmo = false;
   257 		bool sharedAmmo = false;
   255 		bool sharedAmmo = false;
   258 
   256 
   259 		error |= flib_ipc_append_message(vec, netgame ? "TN" : "TL");
   257 		error |= flib_ipc_append_message(vec, netgame ? "TN" : "TL");