Пример #1
0
/* 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);
}
Пример #2
0
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);
}
Пример #3
0
/* 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();
}