int flib_ini_save(flib_ini *ini, const char *filename) { int result = INI_ERROR_OTHER; if(!log_badargs_if2(ini==NULL, filename==NULL)) { FILE *file = fopen(filename, "wb"); if(!file) { flib_log_e("Error opening file \"%s\" for writing.", filename); } else { iniparser_dump_ini(ini->inidict, file); if(fclose(file)) { flib_log_e("Write error on ini file \"%s\"", filename); } else { result = 0; } } } return result; }
int flib_init() { flib_log_d("Initializing frontlib"); if(SDLNet_Init()==-1) { flib_log_e("Error in SDLNet_Init: %s", SDLNet_GetError()); return -1; } return 0; }
int flib_drawnmapdata_from_netmsg(char *netmsg, uint8_t** outbuf, size_t *outlen) { int result = -1; // First step: base64 decoding char *base64decout = NULL; size_t base64declen; bool ok = base64_decode_alloc(netmsg, strlen(netmsg), &base64decout, &base64declen); if(ok && base64declen>3) { // Second step: unzip with the QCompress header. That header is just a big-endian // uint32 indicating the length of the uncompressed data. uint8_t *ubyteBuf = (uint8_t*)base64decout; uint32_t unzipLen = (((uint32_t)ubyteBuf[0])<<24) + (((uint32_t)ubyteBuf[1])<<16) + (((uint32_t)ubyteBuf[2])<<8) + ubyteBuf[3]; if(unzipLen==0) { *outbuf = NULL; *outlen = 0; result = 0; } else { uint8_t *out = flib_malloc(unzipLen); if(out) { uLongf actualUnzipLen = unzipLen; int resultcode = uncompress(out, &actualUnzipLen, (Bytef*)(base64decout+4), base64declen-4); if(resultcode == Z_OK) { *outbuf = out; *outlen = actualUnzipLen; out = NULL; result = 0; } else { flib_log_e("Uncompressing drawn map failed. Code: %i", resultcode); } } free(out); } } else { flib_log_e("base64 decoding of drawn map failed."); } free(base64decout); return result; }
flib_weaponsetlist *flib_weaponsetlist_from_ini(const char *filename) { flib_weaponsetlist *result = NULL; if(!log_badargs_if(filename==NULL)) { flib_ini *ini = flib_ini_load(filename); if(!ini) { flib_log_e("Missing file %s.", filename); } else if(flib_ini_enter_section(ini, "General")) { flib_log_e("Missing section \"General\" in file %s.", filename); } else { flib_weaponsetlist *tmpList = flib_weaponsetlist_create(); if(tmpList && !fillWeaponsetsFromIni(tmpList, ini)) { result = tmpList; tmpList = NULL; } flib_weaponsetlist_destroy(tmpList); } flib_ini_destroy(ini); } return result; }
int flib_scheme_get_setting(const flib_scheme *scheme, const char *name, int def) { if(!log_badargs_if2(scheme==NULL, name==NULL)) { for(int i=0; i<flib_meta.settingCount; i++) { if(!strcmp(flib_meta.settings[i].name, name)) { return scheme->settings[i]; } } flib_log_e("Unable to find game setting %s", name); } return def; }
bool flib_scheme_get_mod(const flib_scheme *scheme, const char *name) { if(!log_badargs_if2(scheme==NULL, name==NULL)) { for(int i=0; i<flib_meta.modCount; i++) { if(!strcmp(flib_meta.mods[i].name, name)) { return scheme->mods[i]; } } flib_log_e("Unable to find game mod %s", name); } return false; }
static int flib_ipc_append_addteam(flib_vector *vec, const flib_team *team, bool perHogAmmo, bool noAmmoStore) { int result = -1; flib_vector *tempvector = flib_vector_create(); if(!log_badargs_if2(vec==NULL, team==NULL) && tempvector) { bool error = false; if(!perHogAmmo && !noAmmoStore) { error = error || appendWeaponSet(tempvector, team->hogs[0].weaponset) || flib_ipc_append_message(tempvector, "eammstore"); } char md5Hex[33]; calculateMd5Hex(team->ownerName ? team->ownerName : "", md5Hex); if(team->colorIndex<0 || team->colorIndex>=flib_teamcolor_count) { flib_log_e("Color index out of bounds for team %s: %i", team->name, team->colorIndex); error = true; } else { error |= flib_ipc_append_message(tempvector, "eaddteam %s %"PRIu32" %s", md5Hex, flib_teamcolors[team->colorIndex], team->name); } if(team->remoteDriven) { error |= flib_ipc_append_message(tempvector, "erdriven"); } error |= flib_ipc_append_message(tempvector, "egrave %s", team->grave); error |= flib_ipc_append_message(tempvector, "efort %s", team->fort); error |= flib_ipc_append_message(tempvector, "evoicepack %s", team->voicepack); error |= flib_ipc_append_message(tempvector, "eflag %s", team->flag); for(int i=0; i<team->bindingCount; i++) { error |= flib_ipc_append_message(tempvector, "ebind %s %s", team->bindings[i].binding, team->bindings[i].action); } for(int i=0; i<team->hogsInGame; i++) { if(perHogAmmo && !noAmmoStore) { error |= appendWeaponSet(tempvector, team->hogs[i].weaponset); } error |= flib_ipc_append_message(tempvector, "eaddhh %i %i %s", team->hogs[i].difficulty, team->hogs[i].initialHealth, team->hogs[i].name); error |= flib_ipc_append_message(tempvector, "ehat %s", team->hogs[i].hat); } if(!error) { // Message created, now we can copy everything. flib_constbuffer constbuf = flib_vector_as_constbuffer(tempvector); if(!flib_vector_append(vec, constbuf.data, constbuf.size)) { result = 0; } } } flib_vector_destroy(tempvector); return result; }
flib_team *flib_team_from_netmsg(char **parts) { flib_team *result = NULL; flib_team *tmpTeam = flib_calloc(1, sizeof(flib_team)); if(tmpTeam) { if(!fillTeamFromMsg(tmpTeam, parts)) { result = tmpTeam; tmpTeam = NULL; } else { flib_log_e("Error parsing team from net."); } } flib_team_destroy(tmpTeam); return result; }
int flib_ini_set_str(flib_ini *ini, const char *key, const char *value) { int result = INI_ERROR_OTHER; if(!log_badargs_if4(ini==NULL, ini->currentSection==NULL, key==NULL, value==NULL)) { char *dictKey = createDictKey(ini->currentSection, key); if(dictKey) { result = iniparser_set(ini->inidict, dictKey, value); if(result) { flib_log_e("Error setting ini entry %s to %s", dictKey, value); } } free(dictKey); } return result; }
int flib_ini_create_section(flib_ini *ini, const char *section) { int result = INI_ERROR_OTHER; if(!log_badargs_if2(ini==NULL, section==NULL)) { result = flib_ini_enter_section(ini, section); if(result == INI_ERROR_NOTFOUND) { if(iniparser_set(ini->inidict, section, NULL)) { flib_log_e("Error creating ini section %s", section); result = INI_ERROR_OTHER; } else { result = flib_ini_enter_section(ini, section); } } } return result; }
flib_team *flib_team_from_ini(const char *filename) { if(log_badargs_if(filename==NULL)) { return NULL; } flib_team *result = flib_calloc(1, sizeof(flib_team)); flib_ini *ini = NULL; if(!result) { return from_ini_handleError(result, ini); } ini = flib_ini_load(filename); if(!ini) { flib_log_e("Error loading team file %s", filename); return from_ini_handleError(result, ini); } if(flib_ini_enter_section(ini, "team")) { flib_log_e("Missing section \"Team\" in team file %s", filename); return from_ini_handleError(result, ini); } bool error = false; error |= flib_ini_get_str(ini, &result->name, "name"); error |= flib_ini_get_str(ini, &result->grave, "grave"); error |= flib_ini_get_str(ini, &result->fort, "fort"); error |= flib_ini_get_str(ini, &result->voicepack, "voicepack"); error |= flib_ini_get_str(ini, &result->flag, "flag"); error |= flib_ini_get_int(ini, &result->rounds, "rounds"); error |= flib_ini_get_int(ini, &result->wins, "wins"); error |= flib_ini_get_int(ini, &result->campaignProgress, "campaignprogress"); int difficulty = 0; error |= flib_ini_get_int(ini, &difficulty, "difficulty"); if(error) { flib_log_e("Missing or malformed entry in section \"Team\" in file %s", filename); return from_ini_handleError(result, ini); } for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { char sectionName[32]; if(snprintf(sectionName, sizeof(sectionName), "hedgehog%i", i) <= 0) { return from_ini_handleError(result, ini); } if(flib_ini_enter_section(ini, sectionName)) { flib_log_e("Missing section \"%s\" in team file %s", sectionName, filename); return from_ini_handleError(result, ini); } flib_hog *hog = &result->hogs[i]; error |= flib_ini_get_str(ini, &hog->name, "name"); error |= flib_ini_get_str(ini, &hog->hat, "hat"); error |= flib_ini_get_int(ini, &hog->rounds, "rounds"); error |= flib_ini_get_int(ini, &hog->kills, "kills"); error |= flib_ini_get_int(ini, &hog->deaths, "deaths"); error |= flib_ini_get_int(ini, &hog->suicides, "suicides"); result->hogs[i].difficulty = difficulty; result->hogs[i].initialHealth = TEAM_DEFAULT_HEALTH; if(error) { flib_log_e("Missing or malformed entry in section \"%s\" in file %s", sectionName, filename); return from_ini_handleError(result, ini); } } if(!flib_ini_enter_section(ini, "binds")) { result->bindingCount = flib_ini_get_keycount(ini); if(result->bindingCount<0) { flib_log_e("Error reading bindings from file %s", filename); result->bindingCount = 0; } result->bindings = flib_calloc(result->bindingCount, sizeof(flib_binding)); if(!result->bindings) { return from_ini_handleError(result, ini); } for(int i=0; i<result->bindingCount; i++) { char *keyname = flib_ini_get_keyname(ini, i); if(!keyname) { error = true; } else { result->bindings[i].action = flib_urldecode(keyname); error |= !result->bindings[i].action; error |= flib_ini_get_str(ini, &result->bindings[i].binding, keyname); } free(keyname); } } if(error) { flib_log_e("Error reading team file %s", filename); return from_ini_handleError(result, ini); } flib_ini_destroy(ini); return result; }