/* load saved recipe names from file */ static void load_recipe_names(void) { char fname[128]; FILE *fp; char line [128]; size_t recipe_no; recipe_names_changed = 0; safe_snprintf(fname, sizeof(fname), "recipes_%s.names",username_str); my_tolower(fname); fp = open_file_config(fname,"r"); if(fp == NULL) { LOG_ERROR("%s() %s \"%s\" [%s]\n", __FUNCTION__, cant_open_file, fname, strerror(errno)); return; } recipe_no = 0; while (fgets(line, sizeof(line), fp) != NULL) { size_t len = strlen(line); while ((len > 0) && ((line[len-1] == '\r') || (line[len-1] == '\n') || (line[len-1] == ' '))) { line[len-1] = '\0'; len--; } if (len > 0) new_recipe_name(recipe_no, line); recipe_no++; } fclose(fp); }
/* save the recipe names, done on exit and after each change */ void save_recipes(){ char fname[128]; FILE *fp; size_t i; if (!recipes_loaded) return; save_recipe_names(); safe_snprintf(fname, sizeof(fname), "recipes_%s.dat",get_lowercase_username()); fp=open_file_config(fname,"wb"); if(fp == NULL){ LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, fname, strerror(errno)); return; } for (i=0; i<num_recipe_entries+1; i++) { item *store = (i<num_recipe_entries) ?recipes_store[i].items :manu_recipe.items; if (fwrite (store,sizeof(item)*NUM_MIX_SLOTS,1, fp) != 1) { LOG_ERROR("%s() fail during write of file [%s] : %s\n", __FUNCTION__, fname, strerror(errno)); break; } } fclose(fp); }
void load_server_markings(){ char fname[128]; FILE *fp; server_mark sm; int rf; init_server_markers(); //open server markings file safe_snprintf(fname, sizeof(fname), "servermarks_%s.dat",get_lowercase_username()); /* sliently ignore non existing file */ if (file_exists_config(fname)!=1) return; fp = open_file_config(fname,"r"); if(fp == NULL){ LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, fname, strerror(errno)); return; } while((rf=fscanf(fp,"%d %d %d %s %[^\n]s\n",&sm.id,&sm.x,&sm.y,sm.map_name,sm.text))==5){ server_mark *nm = calloc(1,sizeof(server_mark)); memcpy(nm,&sm,sizeof(server_mark)); hash_add(server_marks,(NULL+sm.id),(void*) nm); } fclose (fp); LOG_DEBUG("Read server markings from file '%s'", fname); add_server_markers(); }
void save_server_markings(){ char fname[128]; FILE *fp; server_mark *sm; hash_entry *he; if(!server_marks) return; //open server markings file safe_snprintf(fname, sizeof(fname), "servermarks_%s.dat",get_lowercase_username()); fp = open_file_config(fname,"w"); if(fp == NULL){ LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, fname, strerror(errno)); return; } hash_start_iterator(server_marks); while((he=hash_get_next(server_marks))){ sm = (server_mark *) he->item; fprintf(fp,"%d %d %d %s %s\n",sm->id, sm->x, sm->y, sm->map_name, sm->text); } fclose (fp); LOG_DEBUG("Wrote server markings to file '%s'", fname); }
void load_recipes (){ char fname[128]; FILE *fp; init_recipe_names(); if (recipes_loaded) { /* * save existing recipes instead of loading them if we are already logged in * this will take place when relogging after disconnection */ save_recipes(); return; } recipes_loaded=1; memset (recipes, 0, sizeof (recipes)); safe_snprintf(fname, sizeof(fname), "recipes_%s.dat",username_str); my_tolower(fname); fp = open_file_config(fname,"rb"); if(fp == NULL){ LOG_ERROR("%s: %s \"%s\"\n", reg_error_str, cant_open_file, fname); return; } if (fread (recipes,sizeof(recipes),1, fp) != 1) LOG_ERROR("%s() read failed for file [%s]\n", __FUNCTION__, fname); fclose (fp); load_recipe_names(); }
void load_exploration_map () { FILE *fp = NULL; char exploration_map_filename[256]; if(!minimap_texture) return; my_strcp (exploration_map_filename, map_file_name); exploration_map_filename[strlen(exploration_map_filename)-4] = 0; strcat (exploration_map_filename, ".xm"); safe_strncpy (current_exploration_map_filename, exploration_map_filename, sizeof (current_exploration_map_filename)); fp = open_file_config (exploration_map_filename, "rb"); if(fp) { if (fread(exploration_map, sizeof(GLubyte), 256 * 256, fp) != 256 * 256) { memset(exploration_map, 0, 256 * 256 * sizeof(GLubyte)); LOG_ERROR("%s() read failed for file [%s]\n", __FUNCTION__, exploration_map_filename); } fclose(fp); } else { memset(exploration_map, 0, 256 * 256 * sizeof(GLubyte)); } if(poor_man) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); } else if(use_mipmaps) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE); } else { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } if(have_extension(arb_texture_compression)) { if(have_extension(ext_texture_compression_s3tc)) glTexImage2D(GL_TEXTURE_2D,0,GL_COMPRESSED_RGB_S3TC_DXT1_EXT, minimap_size, minimap_size,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,&exploration_map); else glTexImage2D(GL_TEXTURE_2D,0,GL_COMPRESSED_LUMINANCE, minimap_size, minimap_size,0,GL_ALPHA,GL_UNSIGNED_BYTE,&exploration_map); } else glTexImage2D(GL_TEXTURE_2D,0,GL_LUMINANCE, minimap_size, minimap_size,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,&exploration_map); CHECK_GL_ERRORS(); }
void load_filters_list (const char *file_name, char local) { int f_size; FILE *f = NULL; char *filter_list_mem; int istart, iend; char name[128]; size_t ret; f = open_file_config (file_name, "rb"); if (f == NULL) return; fseek (f, 0, SEEK_END); f_size = ftell (f); if (f_size <= 0) { fclose(f); return; } //ok, allocate memory for it filter_list_mem = (char *) calloc (f_size, 1); fseek (f, 0, SEEK_SET); ret = fread (filter_list_mem, 1, f_size, f); fclose (f); if (ret != f_size) { free (filter_list_mem); LOG_ERROR("%s read failed for file [%s]\n", __FUNCTION__, file_name); return; } istart = 0; while (istart < f_size) { // find end of the line for (iend = istart; iend < f_size; iend++) { if (filter_list_mem[iend] == '\n' || filter_list_mem[iend] == '\r') break; } // copy the line and process it if (iend > istart) { safe_strncpy2 (name, filter_list_mem+istart, sizeof (name), iend-istart); if (add_to_filter_list (name, local, 0) == -2) // -1 == already exists, -2 == list full { free (filter_list_mem); return; // filter list full } } // move to next line istart = iend+1; } free (filter_list_mem); }
void open_chat_log() { char starttime[200], sttime[200]; struct tm *l_time; time_t c_time; char chat_log_file[100]; char srv_log_file[100]; time(&c_time); l_time = localtime(&c_time); if (get_rotate_chat_log()) { char logsuffix[7]; strftime(logsuffix, sizeof(logsuffix), "%Y%m", l_time); safe_snprintf (chat_log_file, sizeof (chat_log_file), "chat_log_%s.txt", logsuffix); safe_snprintf (srv_log_file, sizeof (srv_log_file), "srv_log_%s.txt", logsuffix); } else { safe_strncpy(chat_log_file, "chat_log.txt", sizeof(chat_log_file)); safe_strncpy(srv_log_file, "srv_log.txt", sizeof(srv_log_file)); } chat_log = open_file_config (chat_log_file, "a"); if (log_chat == LOG_SERVER || log_chat == LOG_SERVER_SEPERATE) srv_log = open_file_config (srv_log_file, "a"); if (chat_log == NULL) { LOG_TO_CONSOLE(c_red3, "Unable to open log file to write. We will NOT be recording anything."); log_chat = LOG_NONE; return; } else if ((log_chat == LOG_SERVER || log_chat == LOG_SERVER_SEPERATE) && srv_log == NULL) { LOG_TO_CONSOLE(c_red3, "Unable to open server log file to write. We will fall back to recording everything in chat_log.txt."); log_chat = LOG_CHAT; return; } strftime(sttime, sizeof(sttime), "\n\nLog started at %Y-%m-%d %H:%M:%S localtime", l_time); safe_snprintf(starttime, sizeof(starttime), "%s (%s)\n\n", sttime, tzname[l_time->tm_isdst>0]); fwrite (starttime, strlen(starttime), 1, chat_log); }
void load_ignores_list(char * file_name) { int f_size; FILE *f = NULL; char * ignore_list_mem; int i,j; char name[64]; Uint8 ch; size_t ret; f = open_file_config(file_name, "rb"); if(f == NULL){return;} fseek(f,0,SEEK_END); f_size = ftell(f); if (f_size <= 0) { fclose(f); return; } //ok, allocate memory for it ignore_list_mem=(char *)calloc(f_size, 1); fseek (f, 0, SEEK_SET); ret = fread (ignore_list_mem, 1, f_size, f); fclose (f); if (ret != f_size) { free (ignore_list_mem); LOG_ERROR("%s() read failed for file [%s]\n", __FUNCTION__, file_name); return; } j=0; i=0; while (i<f_size) { ch = ignore_list_mem[i]; if (ch == '\n' || ch == '\r') { if (j > 0 && add_to_ignore_list(name, 0) == -1) // ignore list full break; j = 0; i++; } else { name[j++] = ch; name[j] = '\0'; i++; } } free(ignore_list_mem); }
void load_marks_to_buffer(char* mapname, marking* buffer, int* max) { FILE * fp = NULL; char marks_file[256] = {0}, text[600] = {0}; if(mapname == NULL) { //Oops return; } safe_snprintf (marks_file, sizeof (marks_file), "%s.txt", mapname); //LOG_TO_CONSOLE(c_red2, marks_file); fp = open_file_config(marks_file, "r"); *max = 0; if (fp == NULL) return; //load user markers while ( fgets(text, 600,fp) ) { if (strlen (text) > 1) { int r,g,b; sscanf (text, "%d %d", &buffer[*max].x, &buffer[*max].y); //scanning mark color. It can be optional -> default=green if(sscanf(text,"%*d %*d|%d,%d,%d|",&r,&g,&b)<3) { //NO SPACES in RGB format string! r=b=0; g=255; } buffer[*max].server_side=0; buffer[*max].server_side_id=-1; text[strlen(text)-1] = '\0'; //remove the newline if ((strstr(text, " ") == NULL) || (strstr(strstr(text, " ")+1, " ") == NULL)) { LOG_ERROR("Bad map mark file=[%s] text=[%s]", marks_file, text); } else { safe_strncpy(buffer[*max].text, strstr(strstr(text, " ")+1, " ") + 1, sizeof(buffer[*max].text)); buffer[*max].r=r; buffer[*max].g=g; buffer[*max].b=b; *max = *max + 1; if ( *max >= MAX_USER_MARKS ) break; } } } fclose(fp); LOG_DEBUG("Read map markings from file '%s'", marks_file); }
void save_exploration_map() { FILE *fp = NULL; if(!minimap_texture) return; fp = open_file_config (current_exploration_map_filename, "wb"); if (fp) { fwrite(exploration_map, sizeof(GLubyte), 256 * 256, fp); fclose(fp); } else { //log error and quit } }
//returns -1 if the name is already ignored, 1 on sucess, -2 if no more ignore slots int add_to_ignore_list(char *name, char save_name) { int i; // never ignore uobeyuok, the rule bot if(!strcasecmp(name, "uobeyuok")){ return(-1); } //see if this name is already on the list for(i=0;i<MAX_IGNORES;i++) { if(ignore_list[i].used) if(my_strcompare(ignore_list[i].name,name))return -1;//already in the list } //ok, find a free spot for(i=0;i<MAX_IGNORES;i++) { if(!ignore_list[i].used) { //excellent, a free spot my_strcp(ignore_list[i].name,name); //add to the global ignore file, if the case if(save_name) { FILE * f=open_file_config("local_ignores.txt", "a"); if (f == NULL){ LOG_ERROR("%s: %s \"local_ignores.txt\": %s\n", reg_error_str, cant_open_file, strerror(errno)); } else { fwrite(name, strlen(name), 1, f); fwrite("\n", 1, 1, f); fclose(f); } } ignore_list[i].used=1;//mark as used ignored_so_far++; return 1; } } return -2;//if we are here, it means the ignores list is full }
void save_recipes(){ char fname[128]; FILE *fp; if (!recipes_loaded) return; save_recipe_names(); safe_snprintf(fname, sizeof(fname), "recipes_%s.dat",username_str); my_tolower(fname); fp=open_file_config(fname,"wb"); if(fp == NULL){ LOG_ERROR("%s: %s \"%s\"\n", reg_error_str, cant_open_file, fname); return; } fwrite(recipes, sizeof(recipes), 1, fp); fclose(fp); }
/* save recipe names to file if any have changed */ static void save_recipe_names(void) { char fname[128]; FILE *fp; size_t i; int errorflag = 0; if (!recipe_names_changed) return; safe_snprintf(fname, sizeof(fname), "recipes_%s.names",username_str); my_tolower(fname); fp = open_file_config(fname,"w"); if(fp == NULL) { LOG_ERROR("%s() %s \"%s\" [%s]\n", __FUNCTION__, cant_open_file, fname, strerror(errno)); return; } for (i=0; i<SHOW_MAX_RECIPE; i++) { if (recipe_name[i] != NULL) { if (fputs(recipe_name[i], fp) < 0) { errorflag = 1; break; } } if (fputc('\n', fp) != '\n') { errorflag = 1; break; } } if (errorflag) LOG_ERROR("%s() %s \"%s\" [%s]\n", __FUNCTION__, cant_open_file, fname, strerror(errno)); fclose(fp); }
/* save recipe names to file if any have changed */ static void save_recipe_names(void) { char fname[128]; FILE *fp; size_t i; int errorflag = 0; if (!recipe_names_changed) return; safe_snprintf(fname, sizeof(fname), "recipes_%s.names",get_lowercase_username()); fp = open_file_config(fname,"w"); if(fp == NULL) { LOG_ERROR("%s() %s \"%s\": %s\n", __FUNCTION__, cant_open_file, fname, strerror(errno)); return; } for (i=0; i<num_recipe_entries; i++) { if (recipes_store[i].name != NULL) { if (fputs(recipes_store[i].name, fp) < 0) { errorflag = 1; break; } } if (fputc('\n', fp) != '\n') { errorflag = 1; break; } } if (errorflag) LOG_ERROR("%s() %s \"%s\": %s\n", __FUNCTION__, cant_open_file, fname, strerror(errno)); fclose(fp); }
//returns -1 if the name is not filtered, 1 on sucess int remove_from_filter_list (const char *name) { int i; int local = 0; FILE *f = NULL; //see if this name is on the list for (i = 0; i < MAX_FILTERS; i++) { if (filter_list[i].len > 0) { if (my_strcompare (filter_list[i].name, name)) { local = filter_list[i].local; filter_list[i].len = 0; filtered_so_far--; break; } } } if (local) { f = open_file_config ("local_filters.txt", "w"); if (f == NULL){ LOG_ERROR("%s: %s \"local_filters.txt\": %s\n", reg_error_str, cant_open_file, strerror(errno)); } else { for (i = 0; i < MAX_FILTERS; i++) { if (filter_list[i].len > 0 && filter_list[i].local) fprintf (f, "%s = %s\n", filter_list[i].name, filter_list[i].replacement); } fclose(f); } return 1; } return -1; }
void save_markings() { FILE * fp; char marks_file[256]; int i; safe_snprintf (marks_file, sizeof (marks_file), "%s.txt", map_file_name); //LOG_TO_CONSOLE(c_red2, marks_file); fp = open_file_config(marks_file,"w"); if ( fp == NULL ){ LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, marks_file, strerror(errno)); } else { for ( i = 0 ; i < max_mark ; i ++){ if ( marks[i].x > 0 && !marks[i].server_side){ fprintf(fp,"%d %d|%d,%d,%d| %s\n",marks[i].x,marks[i].y,marks[i].r,marks[i].g,marks[i].b,marks[i].text); } } fclose(fp); } LOG_DEBUG("Wrote map markings to file '%s'", marks_file); }
void add_to_download(const char *filename, const Uint8 *md5) { // lock the mutex CHECK_AND_LOCK_MUTEX(download_mutex); if(download_queue_size < MAX_UPDATE_QUEUE_SIZE){ // add the file to the list, and increase the count download_queue[download_queue_size]= strdup(filename); download_MD5s[download_queue_size]= calloc(1, 16); memcpy(download_MD5s[download_queue_size], md5, 16); download_queue_size++; // start a thread if one isn't running if(!download_cur_file){ char buffer[1024]; FILE *fp; safe_snprintf(download_temp_file, sizeof(download_temp_file), "tmp/temp%03d.dat", ++temp_counter); buffer[sizeof(buffer)-1]= '\0'; fp = open_file_config(download_temp_file, "wb+"); if(fp == NULL){ LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, download_temp_file, strerror(errno)); } else { // build the proper URL to download download_cur_file= download_queue[--download_queue_size]; download_cur_md5= download_MD5s[download_queue_size]; if(is_this_files_lst){ safe_snprintf(buffer, sizeof(buffer), "http://%s/updates%d%d%d/%s", update_server, VER_MAJOR, VER_MINOR, VER_RELEASE, download_cur_file); } else { safe_snprintf(buffer, sizeof(buffer), "http://%s/updates/%s", update_server, download_cur_file); } buffer[sizeof(buffer)-1]= '\0'; LOG_DEBUG("@@ %s %s",update_server,buffer); http_threaded_get_file(update_server, buffer, fp, download_cur_md5, EVENT_DOWNLOAD_COMPLETE); } } } // unlock the mutex CHECK_AND_UNLOCK_MUTEX(download_mutex); }
/* load saved recipe names from file */ static void load_recipe_names(void) { char fname[128]; FILE *fp; char line [128]; size_t recipe_no; recipe_names_changed = 0; safe_snprintf(fname, sizeof(fname), "recipes_%s.names",get_lowercase_username()); /* sliently ignore non existing file */ if (file_exists_config(fname)!=1) return; fp = open_file_config(fname,"r"); if(fp == NULL) { LOG_ERROR("%s() %s \"%s\": %s\n", __FUNCTION__, cant_open_file, fname, strerror(errno)); return; } recipe_no = 0; while (fgets(line, sizeof(line), fp) != NULL) { size_t len = strlen(line); while ((len > 0) && ((line[len-1] == '\r') || (line[len-1] == '\n') || (line[len-1] == ' '))) { line[len-1] = '\0'; len--; } if (len > 0) new_recipe_name(recipe_no, line); recipe_no++; } fclose(fp); }
//returns -1 if the name is already ignored, 1 on sucess int remove_from_ignore_list(char *name) { int i; int found = 0; FILE *f = NULL; //see if this name is on the list for(i=0;i<MAX_IGNORES;i++) { if(!found && ignore_list[i].used) if(my_strcompare(ignore_list[i].name,name)) { ignore_list[i].used=0; found = 1; ignored_so_far--; } } if(found) { f=open_file_config("local_ignores.txt", "w"); if (f == NULL){ LOG_ERROR("%s: %s \"local_ignores.txt\": %s\n", reg_error_str, cant_open_file, strerror(errno)); } else { for(i=0;i<MAX_IGNORES;i++) { if(ignore_list[i].used) { fwrite(ignore_list[i].name, strlen(ignore_list[i].name), 1, f); fwrite("\n", 1, 1, f); } } fclose(f); } return 1; } else return -1; }
/* load recipes, done just after login when we have the player name */ void load_recipes (){ char fname[128]; FILE *fp; size_t i; int logged = 0; off_t file_size; const size_t recipe_size = sizeof(item)*NUM_MIX_SLOTS; if (recipes_loaded) { /* * save existing recipes instead of loading them if we are already logged in * this will take place when relogging after disconnection */ save_recipes(); save_recipe_names(); return; } safe_snprintf(fname, sizeof(fname), "recipes_%s.dat",get_lowercase_username()); /* get file length, if a valid length adjust the number of recipe slots if required */ file_size = get_file_size_config(fname); if ((file_size > 0) && (file_size % recipe_size == 0)) { int num_recipes_in_file = file_size / recipe_size - 1; // -1 as last is current in pipline if ((num_recipes_in_file > wanted_num_recipe_entries) && (num_recipes_in_file < max_num_recipe_entries)) { wanted_num_recipe_entries = num_recipes_in_file; set_var_OPT_INT("wanted_num_recipe_entries", wanted_num_recipe_entries); } } /* allocate and initialise the recipe store */ num_recipe_entries = max_prev_num_recipe_entries = wanted_num_recipe_entries; recipes_store = (recipe_entry *)calloc(num_recipe_entries, sizeof(recipe_entry)); if (recipes_store == NULL) { max_prev_num_recipe_entries = num_recipe_entries = 0; return; } recipes_loaded=1; init_recipe_names(); /* if the file exists but is not a valid size, don't use it */ if ((file_size > 0) && (file_size % recipe_size != 0)) { LOG_ERROR("%s: Invalid format (size mismatch) \"%s\"\n", reg_error_str, fname); return; } /* sliently ignore non existing file */ if (file_exists_config(fname)!=1) return; fp = open_file_config(fname,"rb"); if(fp == NULL){ LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, fname, strerror(errno)); return; } /* attempt to read all the recipies we're expecting */ for (i=0; !feof(fp) && i<num_recipe_entries; i++) { if (fread (recipes_store[i].items,recipe_size,1, fp) != 1) { if (!logged) { LOG_ERROR("%s() fail during read of file [%s] : %s\n", __FUNCTION__, fname, strerror(errno)); logged = 1; } memset(recipes_store[i].items, 0, recipe_size); break; } fix_recipe_uids(recipes_store[i].items); } /* if there is another, use it as the current recipe in the manufacturing pipeline */ if (!feof(fp)) { if (fread (manu_recipe.items,recipe_size,1, fp) != 1) memset(manu_recipe.items, 0, recipe_size); else fix_recipe_uids(manu_recipe.items); } fclose (fp); load_recipe_names(); }
//returns -1 if the name is already filtered, 1 on sucess, -2 if no more filter slots int add_to_filter_list (const char *name, char local, char save_name) { int i, j; char left[256]; char right[256]; int t, tp; int l=0; //ok, find a free spot for (i = 0; i < MAX_FILTERS; i++) { if (filter_list[i].len <= 0) { // excellent, a free spot safe_strncpy (left, name, sizeof(left)); for (t = 0; ; t++) { if (left[t] == '\0') { safe_strncpy (right, "smeg", sizeof(right)); break; } if(left[t]=='=') { left[t] = '\0'; tp = t - 1; for (tp = t-1; tp >= 0 && isspace (left[tp]); tp--) { left[tp] = '\0'; } for (tp = t + 1; left[tp] != '\0' && !(left[tp]&0x80) && isspace(left[tp]); tp++) ; safe_strncpy (right, &left[tp], sizeof(right)); break; } } // See if this name is already on the list for (j = 0; j < MAX_FILTERS; j++) { if (filter_list[j].len > 0) { if (my_strcompare (filter_list[j].name, left)) return -1; // Already in the list } } // add to the local filter file, if the case if (save_name) { FILE *f = open_file_config("local_filters.txt", "a"); if (f == NULL){ LOG_ERROR("%s: %s \"local_filters.txt\": %s\n", reg_error_str, cant_open_file, strerror(errno)); } else { fprintf (f, "%s = %s\n", left, right); fclose(f); } } left[sizeof(filter_list[i].name)-1] = '\0'; right[sizeof(filter_list[i].replacement)-1] = '\0'; filter_list[i].wildcard_type = 0; l = strlen (left) - 1; if (left[0] == '*' && left[l] != '*') filter_list[i].wildcard_type = 1; if (left[0] != '*' && left[l] == '*') filter_list[i].wildcard_type = 2; if (left[0] == '*' && left[l] == '*') filter_list[i].wildcard_type = 3; my_strcp (filter_list[i].name, left); my_strcp (filter_list[i].replacement, right); filter_list[i].len = strlen(filter_list[i].name);//memorize the length filter_list[i].rlen = strlen(filter_list[i].replacement);//memorize the length filter_list[i].local = local; filtered_so_far++; return 1; } } return -2;//if we are here, it means the filters list is full }
void load_server_list(const char *filename) { int f_size; FILE * f = NULL; char * server_list_mem; int istart, iend, i, section; char string[128]; int len; f = open_file_config(filename, "rb"); if (f == NULL) { // Error, this is a problem! const char *err_message = "Fatal error: %s file missing!\n"; LOG_ERROR(err_message, filename); fprintf(stderr, err_message, filename); exit(1); } // Ok, allocate memory for it and read it in fseek(f, 0, SEEK_END); f_size = ftell(f); if (f_size <= 0) { const char *err_message = "Fatal error: %s is empty!\n"; LOG_ERROR(err_message, filename); fprintf(stderr, err_message, filename); fclose(f); exit(1); } server_list_mem = (char *) calloc (f_size, 1); fseek(f, 0, SEEK_SET); if (fread(server_list_mem, 1, f_size, f) != f_size) { const char *err_message = "Fatal error: %s read failed!\n"; LOG_ERROR(err_message, filename); fprintf(stderr, err_message, filename); free(server_list_mem); fclose(f); exit(1); } fclose(f); istart = 0; num_servers = 0; while (istart < f_size) { // Find end of the line for (iend = istart; iend < f_size; iend++) { if (server_list_mem[iend] == '\n' || server_list_mem[iend] == '\r') break; } // Parse this line if (iend > istart) { section = 0; len = 0; for (i = istart; i < iend; i++) { if (server_list_mem[i] == '#') break; // This is a comment so ignore the rest of the line else if (section < 4 && (server_list_mem[i] == ' ' || server_list_mem[i] == '\t' || i == iend)) { // This is the end of a section so store it (except the description) // as we include whitespace in the description string[len] = '\0'; switch(section) { case 0: // Server ID safe_strncpy(servers[num_servers].id, string, sizeof(servers[num_servers].id)); break; case 1: // Config dir safe_strncpy(servers[num_servers].dir, string, sizeof(servers[num_servers].dir)); break; case 2: // Server address safe_strncpy((char *)servers[num_servers].address, string, sizeof(servers[num_servers].address)); break; case 3: // Server port servers[num_servers].port = atoi(string); break; } section++; // Reset the length to start the string again len = 0; // Skip any more spaces while (i < iend) { if (server_list_mem[i+1] != ' ' && server_list_mem[i+1] != '\t') break; i++; } } else //if (server_list_mem[i] == ) // Valid char!!) { string[len] = server_list_mem[i]; len++; } } if (i > istart) { // Anything left should be the description so store it now string[len] = '\0'; safe_strncpy(servers[num_servers].desc, string, sizeof(servers[num_servers].desc)); // Check the line was valid if (!strcmp(servers[num_servers].id, "") || !strcmp(servers[num_servers].dir, "") || !strcmp((char *)servers[num_servers].address, "") || servers[num_servers].port == 0 || !strcmp(servers[num_servers].desc, "")) { LOG_ERROR("%s: Invalid server details specified in %s - (%d) %s", "Servers list error", filename, num_servers, servers[num_servers].id); break; // Bail, but do the free first } // we added a valid line num_servers++; } } // Move to next line istart = iend + 1; } free(server_list_mem); }
// handle the update file event static void do_handle_update_download(struct http_get_struct *get) { static int mkdir_res= -1; // flag as not tried int sts; // try to make sure the directory is there if(mkdir_res < 0){ mkdir_res = mkdir_config("tmp"); } if(get != NULL){ // did we finish properly? if(get->status == 0){ // release the memory if(get->fp){ fclose(get->fp); } free(get); // yes, lets start using the new file sts = move_file_to_updates("tmp/temp000.dat", files_lst, doing_custom); // trigger processing this file if(!sts){ do_updates(); } else { LOG_ERROR("Unable to finish %s processing (%d)", files_lst, errno); } // and go back to normal processing return; } // wait for the just completed thread so we free it's resources assert(get->thread_index<MAX_THREADS); SDL_WaitThread(thread_list[get->thread_index], NULL); thread_list[get->thread_index] = NULL; //no, we need to free the memory and try again if(get->fp){ fclose(get->fp); } free(get); } // we need to download the update file if we get here if(update_attempt_count++ < 3){ char filename[1024]; FILE *fp; // select a server if(num_update_servers > 1){ int num; srand( (unsigned)time( NULL ) ); num= rand()%num_update_servers; if(!strcmp(update_server, update_servers[num])){ // oops, the same server twice in a row, try to avoid num= rand()%num_update_servers; if(!strcmp(update_server, update_servers[num])){ // oops, the same server twice in a row, try to avoid num= rand()%num_update_servers; if(!strcmp(update_server, update_servers[num])){ // oops, the same server twice in a row, try to avoid num= rand()%num_update_servers; } } } safe_strncpy(update_server, update_servers[num], sizeof(update_server)); update_server[127]= '\0'; LOG_DEBUG("downloading from mirror %d of %d %s", num+1, num_update_servers, update_server); } else { safe_strncpy(update_server, update_servers[0], sizeof(update_server)); } ++temp_counter; fp = open_file_config("tmp/temp000.dat", "wb+"); if(fp == NULL){ LOG_ERROR("%s: %s \"tmp/temp000.dat\": %s\n", reg_error_str, cant_open_file, strerror(errno)); } else { if(is_this_files_lst) //files.lst { safe_snprintf(filename, sizeof(filename), "http://%s/updates%d%d%d/%s", update_server, VER_MAJOR, VER_MINOR, VER_RELEASE, files_lst); } else { //custom_files.lst safe_snprintf(filename, sizeof(filename), "http://%s/updates/%s", update_server, files_lst); } LOG_DEBUG("* server %s filename %s", update_server, filename); http_threaded_get_file(update_server, filename, fp, NULL, EVENT_UPDATES_DOWNLOADED); } // and keep running until we get a response return; } // total failure, error and clear the busy flag LOG_DEBUG("Failed to download (%s) 3 times. Giving up.", files_lst); update_busy= 0; }
// finish up on one file that just downloaded void handle_file_download(struct http_get_struct *get) { int sts; if(!get){ // huh? what are you doing? return; } // lock the mutex CHECK_AND_LOCK_MUTEX(download_mutex); if(get->status == 0){ // replace the current file (creates all required directories) sts = move_file_to_updates(download_temp_file, download_cur_file, doing_custom); LOG_DEBUG("Moved \"%s\" to \"%s\"", download_temp_file, download_cur_file); // check for errors if(!sts){ // TODO: make the restart more intelligent if(allow_restart){ if(strstr(download_cur_file, "2dobjects/")==NULL && strstr(download_cur_file, "3dobjects/")==NULL && strstr(download_cur_file, "maps/")==NULL && strstr(download_cur_file, "music/")==NULL && strstr(download_cur_file, "textures/")==NULL && strstr(download_cur_file, ".menu")==NULL) restart_required++; if(strstr(download_cur_file, ".menu") > 0) reload_user_menus(); if(restart_required == 1) { LOG_TO_CONSOLE(c_red1, "Downloading Updates..."); } } } else { LOG_ERROR("Unable to finish processing of %s (%d)", download_cur_file, errno); // the final renamed failed, no restart permitted allow_restart= 0; restart_required= 0; } } else { // and make sure we can't restart since we had a total failure allow_restart= 0; restart_required= 0; } // wait for the just completed thread so we free it's resources assert(get->thread_index<MAX_THREADS); SDL_WaitThread(thread_list[get->thread_index], NULL); thread_list[get->thread_index] = NULL; // release the filename free(download_cur_file); free(download_cur_md5); download_cur_file= NULL; // unlock mutex CHECK_AND_UNLOCK_MUTEX(download_mutex); // now, release everything free(get); // lock the mutex CHECK_AND_LOCK_MUTEX(download_mutex); if(download_queue_size > 0 && !download_cur_file){ // start a thread if a file is waiting to download and no download active char buffer[512]; FILE *fp; safe_snprintf(download_temp_file, sizeof(download_temp_file), "tmp/temp%03d.dat", ++temp_counter); fp = open_file_config(download_temp_file, "wb+"); if(fp == NULL){ LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, download_temp_file, strerror(errno)); } else { // build the proper URL to download download_cur_file= download_queue[--download_queue_size]; download_cur_md5= download_MD5s[download_queue_size]; if(is_this_files_lst) { safe_snprintf(buffer, sizeof(buffer), "http://%s/updates%d%d%d/%s", update_server, VER_MAJOR, VER_MINOR, VER_RELEASE, download_cur_file); } else { safe_snprintf(buffer, sizeof(buffer), "http://%s/updates/%s", update_server, download_cur_file); } buffer[sizeof(buffer)-1]= '\0'; http_threaded_get_file(update_server, buffer, fp, download_cur_md5, EVENT_DOWNLOAD_COMPLETE); } } // check to see if this was the last file && a restart is required if(!update_busy && restart_required && allow_restart && download_queue_size <= 0 && !download_cur_file){ // yes, now trigger a restart LOG_INFO("Restart required because of update"); // Display something on the screen for a little bit before restarting if(autoupdate_delay >= 0) { int i; for (i=0; i < windows_list.num_windows;i++) { if(get_show_window(i)) break; } create_update_root_window (window_width, window_height, autoupdate_delay, i); show_window (update_root_win); } else { LOG_TO_CONSOLE(c_red2, "You need to restart the client to activate the updates!"); } } // unlock mutex CHECK_AND_UNLOCK_MUTEX(download_mutex); }