Пример #1
0
/*==========================================
 * アイテムデータベースの読み込み
 *------------------------------------------
 */
static int itemdb_readdb(void)
{
	FILE *fp;
	char line[1024];
	int ln=0,lines=0;
	int nameid,j;
	char *str[32],*p,*np;
	struct item_data *id;
	int i=0;
	char *filename[]={ "item_db.txt","item_db2.txt" };

	for(i=0;i<2;i++){
		sprintf(line, "%s/%s", db_path, filename[i]);
		fp=fopen(line,"r");
		if(fp==NULL){
			if(i>0)
				continue;
			ShowFatalError("can't read %s\n",line);
			exit(1);
		}

		lines=0;
		while(fgets(line,1020,fp)){
			lines++;
			if(line[0]=='/' && line[1]=='/')
				continue;
			memset(str,0,sizeof(str));
			for(j=0,np=p=line;j<19 && p;j++){
				str[j]=p;
				p=strchr(p,',');
				if(p){ *p++=0; np=p; }
			}
			if(str[0]==NULL)
				continue;

			nameid=atoi(str[0]);
			if(nameid<=0)
				continue;
			if (j < 19)
			{	//Crash-fix on broken item lines. [Skotlex]
				ShowWarning("Reading %s: Insufficient fields for item with id %d, skipping.\n", filename[i], nameid);
				continue;
			}
			ln++;

			//ID,Name,Jname,Type,Price,Sell,Weight,ATK,DEF,Range,Slot,Job,Job Upper,Gender,Loc,wLV,eLV,refineable,View
			id=itemdb_load(nameid);
			strncpy(id->name, str[1], ITEM_NAME_LENGTH-1);
			strncpy(id->jname, str[2], ITEM_NAME_LENGTH-1);
			id->type=atoi(str[3]);
			if (id->type == IT_DELAYCONSUME)
			{	//Items that are consumed upon target confirmation
				//(yggdrasil leaf, spells & pet lures) [Skotlex]
				id->type = IT_USABLE;
				id->flag.delay_consume=1;
			}

			{
				int buy = atoi(str[4]), sell = atoi(str[5]);
				// if buying price > selling price * 2 consider it valid and don't change it [celest]
				if (buy && sell && buy > sell*2){
					id->value_buy = buy;
					id->value_sell = sell;
				} else {
					// buy≠sell*2 は item_value_db.txt で指定してください。
					if (sell) {		// sell値を優先とする
						id->value_buy = sell*2;
						id->value_sell = sell;
					} else {
						id->value_buy = buy;
						id->value_sell = buy/2;
					}
				}
				// check for bad prices that can possibly cause exploits
				if (id->value_buy*75/100 < id->value_sell*124/100) {
					ShowWarning ("Item %s [%d] buying:%d < selling:%d\n",
						id->name, id->nameid, id->value_buy*75/100, id->value_sell*124/100);
				}
			}
			id->weight=atoi(str[6]);
			id->atk=atoi(str[7]);
			id->def=atoi(str[8]);
			id->range=atoi(str[9]);
			id->slot=atoi(str[10]);
			if (id->slot > MAX_SLOTS)
			{
				ShowWarning("itemdb_readdb: Item %d (%s) specifies %d slots, but the server only supports up to %d\n", nameid, id->jname, id->slot, MAX_SLOTS);
				id->slot = MAX_SLOTS;
			}
			itemdb_jobid2mapid(id->class_base, (unsigned int)strtoul(str[11],NULL,0));
			id->class_upper = atoi(str[12]);
			id->sex	= atoi(str[13]);
			if(id->equip != atoi(str[14])){
				id->equip=atoi(str[14]);
			}
			if (!id->equip && itemdb_isequip2(id))
			{
				ShowWarning("Item %d (%s) is an equipment with no equip-field! Making it an etc item.\n", nameid, id->jname);
				id->type = 3;
			}
			id->wlv=atoi(str[15]);
			id->elv=atoi(str[16]);
			id->flag.no_refine = atoi(str[17])?0:1;	//If the refine column is 1, no_refine is 0
			id->look=atoi(str[18]);
			id->flag.available=1;
			id->flag.value_notdc=0;
			id->flag.value_notoc=0;
			id->view_id=0;
			id->sex = itemdb_gendercheck(id); //Apply gender filtering.

			if (id->script) {
				script_free_code(id->script);
				id->script=NULL;
			}
			if (id->equip_script) {
				script_free_code(id->equip_script);
				id->equip_script=NULL;
			}
			if (id->unequip_script) {
				script_free_code(id->unequip_script);
				id->unequip_script=NULL;
			}

			if((p=strchr(np,'{'))==NULL)
				continue;
			
			str[19] = p; //Script
			np = strchr(p,'}');

			while (np && np[1] && np[1] != ',')
				np = strchr(np+1,'}'); //Jump close brackets until the next field is found.
			if (!np || !np[1]) {
				//Couldn't find the end of the script field.
				id->script = parse_script(str[19],filename[i],lines,0);
				continue;
			}
			np[1] = '\0'; //Set end of script
			id->script = parse_script(str[19],filename[i],lines,0);
			np+=2; //Skip to next field

			if(!np || (p=strchr(np,'{'))==NULL)
				continue;

			str[20] = p; //Equip Script
			np = strchr(p,'}');

			while (np && np[1] && np[1] != ',')
				np = strchr(np+1,'}'); //Jump close brackets until the next field is found.
			if (!np || !np[1]) {
				//Couldn't find the end of the script field.
				id->equip_script = parse_script(str[20],filename[i],lines,0);
				continue;
			}

			np[1] = '\0'; //Set end of script
			id->equip_script = parse_script(str[20],filename[i],lines,0);
			np+=2; //Skip comma, to next field

			if(!np || (p=strchr(np,'{'))==NULL)
				continue;
			//Unequip script, last column.
			id->unequip_script = parse_script(p,filename[i],lines,0);
		}
		fclose(fp);
		if (ln > 0) {
			ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n",ln,filename[i]);
		}
		ln=0;	// reset to 0
	}
	return 0;
}
Пример #2
0
/*======================================
* SQL
*===================================
*/
static int itemdb_read_sqldb(void)
{
	unsigned short nameid;
	struct item_data *id;
	char script[65535 + 2 + 1]; // Maximum length of MySQL TEXT type (65535) + 2 bytes for curly brackets + 1 byte for terminator
	char *item_db_name[] = { item_db_db, item_db2_db };
	long unsigned int ln = 0;
	int i;	

	// ----------

	for (i = 0; i < 2; i++) {
		sprintf(tmp_sql, "SELECT * FROM `%s`", item_db_name[i]);

		// Execute the query; if the query execution succeeded...
		if (mysql_query(&mmysql_handle, tmp_sql) == 0) {
			sql_res = mysql_store_result(&mmysql_handle);

			// If the storage of the query result succeeded...
			if (sql_res) {
				// Parse each row in the query result into sql_row
				while ((sql_row = mysql_fetch_row(sql_res)))
				{	/*Table structure is:
					00  id
					01  name_english
					02  name_japanese
					03  type
					04  price_buy
					05  price_sell
					06  weight
					07  attack
					08  defence
					09  range
					10  slots
					11  equip_jobs
					12  equip_upper
					13  equip_genders
					14  equip_locations
					15  weapon_level
					16  equip_level
					17  refineable
					18  view
					19  script
					20  equip_script
					21  unequip_script
					*/
					nameid = atoi(sql_row[0]);

					// If the identifier is not within the valid range, process the next row
					if (nameid == 0)
						continue;

					ln++;

					// ----------
					id = itemdb_load(nameid);
					
					strncpy(id->name, sql_row[1], ITEM_NAME_LENGTH-1);
					strncpy(id->jname, sql_row[2], ITEM_NAME_LENGTH-1);

					id->type = atoi(sql_row[3]);
					if (id->type == IT_DELAYCONSUME)
					{	//Items that are consumed upon target confirmation
						//(yggdrasil leaf, spells & pet lures) [Skotlex]
						id->type = IT_USABLE;
						id->flag.delay_consume=1;
					} else //In case of an itemdb reload and the item type changed.
						id->flag.delay_consume=0;

					// If price_buy is not NULL and price_sell is not NULL...
					if ((sql_row[4] != NULL) && (sql_row[5] != NULL)) {
						id->value_buy = atoi(sql_row[4]);
						id->value_sell = atoi(sql_row[5]);
					}
					// If price_buy is not NULL and price_sell is NULL...
					else if ((sql_row[4] != NULL) && (sql_row[5] == NULL)) {
						id->value_buy = atoi(sql_row[4]);
						id->value_sell = atoi(sql_row[4]) / 2;
					}
					// If price_buy is NULL and price_sell is not NULL...
					else if ((sql_row[4] == NULL) && (sql_row[5] != NULL)) {
						id->value_buy = atoi(sql_row[5]) * 2;
						id->value_sell = atoi(sql_row[5]);
					}
					// If price_buy is NULL and price_sell is NULL...
					if ((sql_row[4] == NULL) && (sql_row[5] == NULL)) {
						id->value_buy = 0;
						id->value_sell = 0;
					}

					id->weight	= atoi(sql_row[6]);
					id->atk		= (sql_row[7] != NULL) ? atoi(sql_row[7]) : 0;
					id->def		= (sql_row[8] != NULL) ? atoi(sql_row[8]) : 0;
					id->range	= (sql_row[9] != NULL) ? atoi(sql_row[9]) : 0;
					id->slot	= (sql_row[10] != NULL) ? atoi(sql_row[10]) : 0;
					if (id->slot > MAX_SLOTS)
					{
						ShowWarning("itemdb_read_sqldb: Item %d (%s) specifies %d slots, but the server only supports up to %d\n", nameid, id->jname, id->slot, MAX_SLOTS);
						id->slot = MAX_SLOTS;
					}
					itemdb_jobid2mapid(id->class_base, (sql_row[11] != NULL) ? (unsigned int)strtoul(sql_row[11], NULL, 0) : 0);
					id->class_upper= (sql_row[12] != NULL) ? atoi(sql_row[12]) : 0;
					id->sex		= (sql_row[13] != NULL) ? atoi(sql_row[13]) : 0;
					id->equip	= (sql_row[14] != NULL) ? atoi(sql_row[14]) : 0;
					if (!id->equip && itemdb_isequip2(id))
					{
						ShowWarning("Item %d (%s) is an equipment with no equip-field! Making it an etc item.\n", nameid, id->jname);
						id->type = 3;
					}
					id->wlv		= (sql_row[15] != NULL) ? atoi(sql_row[15]) : 0;
					id->elv		= (sql_row[16] != NULL)	? atoi(sql_row[16]) : 0;
					id->flag.no_refine = (sql_row[17] == NULL || atoi(sql_row[17]) == 1)?0:1;
					id->look	= (sql_row[18] != NULL) ? atoi(sql_row[18]) : 0;
					id->view_id	= 0;
					id->sex = itemdb_gendercheck(id); //Apply gender filtering.

					// ----------
					
					if (id->script)
						script_free_code(id->script);
					if (sql_row[19] != NULL) {
						if (sql_row[19][0] == '{')
							id->script = parse_script(sql_row[19],item_db_name[i], ln, 0);
						else {
							sprintf(script, "{%s}", sql_row[19]);
							id->script = parse_script(script, item_db_name[i], ln, 0);
						}
					} else id->script = NULL;
	
					if (id->equip_script)
						script_free_code(id->equip_script);
					if (sql_row[20] != NULL) {
						if (sql_row[20][0] == '{')
							id->equip_script = parse_script(sql_row[20], item_db_name[i], ln, 0);
						else {
							sprintf(script, "{%s}", sql_row[20]);
							id->equip_script = parse_script(script, item_db_name[i], ln, 0);
						}
					} else id->equip_script = NULL;
	
					if (id->unequip_script)
						script_free_code(id->unequip_script);
					if (sql_row[21] != NULL) {
						if (sql_row[21][0] == '{')
							id->unequip_script = parse_script(sql_row[21],item_db_name[i], ln, 0);
						else {
							sprintf(script, "{%s}", sql_row[21]);
							id->unequip_script = parse_script(script, item_db_name[i], ln, 0);
						}
					} else id->unequip_script = NULL;
				
					// ----------

					id->flag.available	= 1;
					id->flag.value_notdc	= 0;
					id->flag.value_notoc	= 0;
				}

				ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", ln, item_db_name[i]);
				ln = 0;
			} else {
				ShowSQL("DB error (%s) - %s\n",item_db_name[i], mysql_error(&mmysql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
			}

			// Free the query result
			mysql_free_result(sql_res);
		} else {
			ShowSQL("DB error (%s) - %s\n",item_db_name[i], mysql_error(&mmysql_handle));
			ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
		}
	}

	return 0;
}
Пример #3
0
/*==========================================
 * processes one itemdb entry
 *------------------------------------------*/
static bool itemdb_parse_dbrow(char** str, const char* source, int line, int scriptopt)
{
	/*
		+----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+-------------+---------------+-----------------+--------------+-------------+------------+------+--------+--------------+----------------+
		| 00 |      01      |       02      |  03  |     04    |     05     |   06   |   07   |    08   |   09  |   10  |     11     |      12     |       13      |        14       |      15      |      16     |     17     |  18  |   19   |      20      |        21      |
		+----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+-------------+---------------+-----------------+--------------+-------------+------------+------+--------+--------------+----------------+
		| id | name_english | name_japanese | type | price_buy | price_sell | weight | attack | defence | range | slots | equip_jobs | equip_upper | equip_genders | equip_locations | weapon_level | equip_level | refineable | view | script | equip_script | unequip_script |
		+----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+-------------+---------------+-----------------+--------------+-------------+------------+------+--------+--------------+----------------+
	*/
	int nameid;
	struct item_data* id;
	
	nameid = atoi(str[0]);
	if( nameid <= 0 )
	{
		ShowWarning("itemdb_parse_dbrow: Invalid id %d in line %d of \"%s\", skipping.\n", nameid, line, source);
		return false;
	}

	//ID,Name,Jname,Type,Price,Sell,Weight,ATK,DEF,Range,Slot,Job,Job Upper,Gender,Loc,wLV,eLV,refineable,View
	id = itemdb_load(nameid);
	safestrncpy(id->name, str[1], sizeof(id->name));
	safestrncpy(id->jname, str[2], sizeof(id->jname));

	id->type = atoi(str[3]);

	if( id->type < 0 || id->type == IT_UNKNOWN || id->type == IT_UNKNOWN2 || ( id->type > IT_DELAYCONSUME && id->type < IT_THROWWEAPON ) || id->type >= IT_MAX )
	{// catch invalid item types
		ShowWarning("itemdb_parse_dbrow: Invalid item type %d for item %d. IT_ETC will be used.\n", id->type, nameid);
		id->type = IT_ETC;
	}

	if (id->type == IT_DELAYCONSUME)
	{	//Items that are consumed only after target confirmation
		id->type = IT_USABLE;
		id->flag.delay_consume = 1;
	} else //In case of an itemdb reload and the item type changed.
		id->flag.delay_consume = 0;

	//When a particular price is not given, we should base it off the other one
	//(it is important to make a distinction between 'no price' and 0z)
	if ( str[4][0] )
		id->value_buy = atoi(str[4]);
	else
		id->value_buy = atoi(str[5]) * 2;

	if ( str[5][0] )
		id->value_sell = atoi(str[5]);
	else
		id->value_sell = id->value_buy / 2;
	/* 
	if ( !str[4][0] && !str[5][0])
	{  
		ShowWarning("itemdb_parse_dbrow: No buying/selling price defined for item %d (%s), using 20/10z\n",       nameid, id->jname);
		id->value_buy = 20;
		id->value_sell = 10;
	} else
	*/
	if (id->value_buy/124. < id->value_sell/75.)
		ShowWarning("itemdb_parse_dbrow: Buying/Selling [%d/%d] price of item %d (%s) allows Zeny making exploit  through buying/selling at discounted/overcharged prices!\n",
			id->value_buy, id->value_sell, nameid, id->jname);

	id->weight = atoi(str[6]);
#if REMODE
	itemdb_re_split_atoi(str[7],&id->atk,&id->matk);
#else
	id->atk = atoi(str[7]);
#endif
	id->def = atoi(str[8]);
	id->range = atoi(str[9]);
	id->slot = atoi(str[10]);

	if (id->slot > MAX_SLOTS)
	{
		ShowWarning("itemdb_parse_dbrow: Item %d (%s) specifies %d slots, but the server only supports up to %d. Using %d slots.\n", nameid, id->jname, id->slot, MAX_SLOTS, MAX_SLOTS);
		id->slot = MAX_SLOTS;
	}

	itemdb_jobid2mapid(id->class_base, (unsigned int)strtoul(str[11],NULL,0));
	id->class_upper = atoi(str[12]);
	id->sex	= atoi(str[13]);
	id->equip = atoi(str[14]);

	if (!id->equip && itemdb_isequip2(id))
	{
		ShowWarning("Item %d (%s) is an equipment with no equip-field! Making it an etc item.\n", nameid, id->jname);
		id->type = IT_ETC;
	}

	id->wlv = atoi(str[15]);
	id->elv = atoi(str[16]);
	id->flag.no_refine = atoi(str[17]) ? 0 : 1; //FIXME: verify this
	id->look = atoi(str[18]);

	id->flag.available = 1;
	id->view_id = 0;
	id->sex = itemdb_gendercheck(id); //Apply gender filtering.

	if (id->script)
	{
		script_free_code(id->script);
		id->script = NULL;
	}
	if (id->equip_script)
	{
		script_free_code(id->equip_script);
		id->equip_script = NULL;
	}
	if (id->unequip_script)
	{
		script_free_code(id->unequip_script);
		id->unequip_script = NULL;
	}

	if (*str[19])
		id->script = parse_script(str[19], source, line, scriptopt);
	if (*str[20])
		id->equip_script = parse_script(str[20], source, line, scriptopt);
	if (*str[21])
		id->unequip_script = parse_script(str[21], source, line, scriptopt);

	return true;
}
Пример #4
0
/*==========================================
 * アイテムデータベースの読み込み
 *------------------------------------------
 */
static int itemdb_readdb(void)
{
	FILE *fp;
	char line[1024];
	int ln, lines = 0;
	int nameid, j;
	char *str[32], *p, *np;
	struct item_data *id;
	int i = 0;
	int buy_price, sell_price;
	char *filename[] = { "db/item_db.txt", "db/item_db2.txt" };
	#define ITEM_DB_SQL_NAME "item_db_map.sql"

#ifdef USE_SQL
	FILE *item_db_fp = NULL; // need to be initialized to avoid 'false' warning
	// code to create SQL item db
	if (create_item_db_script) {
		// if file exists (doesn't exist, was renamed!... unnormal that continue to exist)
		if (access(ITEM_DB_SQL_NAME, 0) == 0) // 0: file exist or not, 0 = success
			remove(ITEM_DB_SQL_NAME); // delete the file. return value = 0 (success), return value = -1 (denied access or not found file)
		if ((item_db_fp = fopen(ITEM_DB_SQL_NAME, "a")) != NULL) {
			printf("Generating the '" CL_WHITE ITEM_DB_SQL_NAME CL_RESET "' file.\n");
			fprintf(item_db_fp, "# You can regenerate this file with an option in inter_athena.conf" RETCODE);
			fprintf(item_db_fp, RETCODE);
			fprintf(item_db_fp, "DROP TABLE IF EXISTS `%s`;" RETCODE, item_db_db);
			fprintf(item_db_fp, "CREATE TABLE `%s` (" RETCODE, item_db_db);
			fprintf(item_db_fp, "  `id` smallint(5) unsigned NOT NULL default '0'," RETCODE);
			fprintf(item_db_fp, "  `name_english` varchar(50) NOT NULL default ''," RETCODE);
			fprintf(item_db_fp, "  `name_japanese` varchar(50) NOT NULL default ''," RETCODE);
			fprintf(item_db_fp, "  `type` tinyint(2) unsigned NOT NULL default '0'," RETCODE);
			fprintf(item_db_fp, "  `price_buy` meduimint(10) unsigned default NULL," RETCODE);
			fprintf(item_db_fp, "  `price_sell` mediumint(10) unsigned default NULL," RETCODE);
			fprintf(item_db_fp, "  `weight` smallint(5) unsigned NOT NULL default '0'," RETCODE);
			fprintf(item_db_fp, "  `attack` tinyint(4) unsigned default NULL," RETCODE);
			fprintf(item_db_fp, "  `defence` tinyint(4) unsigned default NULL," RETCODE);
			fprintf(item_db_fp, "  `range` tinyint(2) unsigned default NULL," RETCODE);
			fprintf(item_db_fp, "  `slots` tinyint(2) unsigned default NULL," RETCODE); // max 5, but set to tinyint(2) to avoid possible BOOL replacement
			fprintf(item_db_fp, "  `equip_jobs` int(12) unsigned default NULL," RETCODE);
			fprintf(item_db_fp, "  `equip_upper` tinyint(8) unsigned default NULL," RETCODE);
			fprintf(item_db_fp, "  `equip_genders` tinyint(2) unsigned default NULL," RETCODE); // max 3 (1+2), but set to tinyint(2) to avoid possible BOOL replacement
			fprintf(item_db_fp, "  `equip_locations` smallint(4) unsigned default NULL," RETCODE);
			fprintf(item_db_fp, "  `weapon_level` tinyint(2) unsigned default NULL," RETCODE); // max 4, but set to tinyint(2) to avoid possible BOOL replacement
			fprintf(item_db_fp, "  `equip_level` tinyint(3) unsigned default NULL," RETCODE);
			fprintf(item_db_fp, "  `refineable` tinyint(1) unsigned default NULL," RETCODE);
			fprintf(item_db_fp, "  `view` tinyint(3) unsigned default NULL," RETCODE);
			fprintf(item_db_fp, "  `script` text," RETCODE);
			fprintf(item_db_fp, "  `equip_script` text," RETCODE);
			fprintf(item_db_fp, "  `unequip_script` text," RETCODE);
			fprintf(item_db_fp, "  PRIMARY KEY (`id`)" RETCODE);
			fprintf(item_db_fp, ") TYPE=MyISAM;" RETCODE);
			fprintf(item_db_fp, RETCODE);
		} else {
			printf(CL_RED "Can not generate the '" ITEM_DB_SQL_NAME "' file." CL_RESET" \n");
			create_item_db_script = 0; // not continue to try to create file after
		}
	}
#endif /* USE_SQL */

	for(i = 0; i < 2; i++) {

		ln = 0;
		fp = fopen(filename[i], "r");
		if (fp == NULL) {
			if (i > 0)
				continue;
			printf("can't read %s\n", filename[i]);
#ifdef USE_SQL
			if (create_item_db_script && item_db_fp != NULL) {
				fclose(item_db_fp);
			}
#endif /* USE_SQL */
			exit(1);
		}

		lines = 0;
		while(fgets(line, sizeof(line), fp)) { // fgets reads until maximum one less than size and add '\0' -> so, it's not necessary to add -1
			lines++;
			if ((line[0] == '/' && line[1] == '/') || line[0] == '\0' || line[0] == '\n' || line[0] == '\r') {
#ifdef USE_SQL
				// code to create SQL item db
				if (create_item_db_script && item_db_fp != NULL) {
					/* remove carriage return if exist */
					while(line[0] != '\0' && (line[(j = strlen(line) - 1)] == '\n' || line[j] == '\r'))
						line[j] = '\0';
					// add comments in the sql script
					if ((line[0] == '/' && line[1] == '/' && strlen(line) > 2)) {
						fprintf(item_db_fp, "#%s" RETCODE, line + 2);
					} else {
						fprintf(item_db_fp, RETCODE);
					}
				}
#endif /* USE_SQL */
				continue;
			}

			/* remove carriage return if exist */
			while(line[0] != '\0' && (line[(j = strlen(line) - 1)] == '\n' || line[j] == '\r'))
				line[j] = '\0';

			memset(str, 0, sizeof(str));
			for(j = 0, np = p = line; j < 19 && p; j++) {
				str[j] = p;
				p = strchr(p, ',');
				if (p) { *p++ = 0; np = p; }
			}
			if (str[0] == NULL)
				continue;

			nameid = atoi(str[0]);
			if (nameid <= 0 || nameid >= 20000)
				continue;
#ifdef __DEBUG
			if (battle_config.etc_log) {
				if (strlen(str[1]) > ITEM_NAME_LENGTH)
					printf(CL_YELLOW "WARNING: Invalid item name" CL_RESET" (id: %d) - Name too long (> ITEM_NAME_LENGTH char.) -> only ITEM_NAME_LENGTH first characters are used.\n", nameid);
				if (strlen(str[2]) > ITEM_NAME_LENGTH)
					printf(CL_YELLOW "WARNING: Invalid item jname" CL_RESET" (id: %d) - Name too long (> ITEM_NAME_LENGTH char.) -> only ITEM_NAME_LENGTH first characters are used.\n", nameid);
			}
#endif

			ln++;
			if (ln % 20 == 19) {
				printf("Reading item #%d (id: %d)...\r", ln, nameid);
				fflush(stdout);
			}

			id = itemdb_search(nameid);
			memset(id->name, 0, sizeof(id->name));
			strncpy(id->name, str[1], ITEM_NAME_LENGTH);
			memset(id->jname, 0, sizeof(id->jname));
			strncpy(id->jname, str[2], ITEM_NAME_LENGTH);
			id->type = atoi(str[3]);
			if (id->type == 11) {
				id->type = 2;
				id->flag.delay_consume = 1;
			}

			buy_price = atoi(str[4]);
			sell_price = atoi(str[5]);
			// If price_buy is not 0 and price_sell is not 0...
			if (buy_price > 0 && sell_price > 0) {
				id->value_buy = buy_price;
				id->value_sell = sell_price;
			// If price_buy is not 0 and price_sell is 0...
			} else if (buy_price > 0 && sell_price == 0) {
				id->value_buy = buy_price;
				id->value_sell = buy_price / 2;
			// If price_buy is 0 and price_sell is not 0...
			} else if (buy_price == 0 && sell_price > 0) {
				id->value_buy = sell_price * 2;
				id->value_sell = sell_price;
			// If price_buy is 0 and price_sell is 0...
			} else {
				id->value_buy = 0;
				id->value_sell = 0;
			}
			// check for bad prices that can possibly cause exploits
			if (((double)id->value_buy * (double)75) / 100 < ((double)id->value_sell * (double)124) / 100) {
				printf("Item %s [%d]: prices: buy %d / sell %d (" CL_YELLOW "WARNING" CL_RESET ").\n", id->name, id->nameid, id->value_buy, id->value_sell);
				printf("for merchant: buying: %d < selling:%d (" CL_YELLOW "possible exploit OC/DC" CL_RESET ").\n", id->value_buy * 75 / 100, id->value_sell * 124 / 100);
				id->value_buy = ((double)id->value_sell * (double)124) / (double)75 + 1;
				printf("->set to: buy %d, sell %d (change in database please).\n", id->value_buy, id->value_sell);
			}

			id->weight = atoi(str[6]);
			id->atk = atoi(str[7]);
			id->def = atoi(str[8]);
			id->range = atoi(str[9]);
			id->slot = atoi(str[10]);
			itemdb_jobid2mapid(id->class_base, (unsigned int)strtoul(str[11],NULL,0));
			id->class_upper = atoi(str[12]);
			id->sex = atoi(str[13]);
			if (id->equip != atoi(str[14])) {
				id->equip = atoi(str[14]);
			}
			id->wlv = atoi(str[15]);
			id->elv = atoi(str[16]);
			id->flag.no_refine = atoi(str[17]) ? 0 : 1;
			id->look = atoi(str[18]);
			id->flag.available = 1;
			id->flag.value_notdc = 0;
			id->flag.value_notoc = 0;
			id->view_id = 0;

			id->script = NULL;
			id->equip_script = NULL;
			id->unequip_script = NULL;

			if ((p = strchr(np, '{')) != NULL) {
				id->script = parse_script((unsigned char *)p, lines);
				if ((p = strchr(p + 1, '{')) != NULL)
					id->equip_script = parse_script((unsigned char *)p, lines);
				if ((p = strchr(p + 1, '{')) != NULL)
					id->unequip_script = parse_script((unsigned char *)p, lines);
			}

#ifdef USE_SQL
/*			// code to create SQL item db
			if (create_item_db_script && item_db_fp != NULL) {
				char item_name[50]; // 24 * 2 + 1 + NULL
				char item_jname[50]; // 24 * 2 + 1 + NULL
				char script1[1024], script2[1024], script3[1024], temp_script[1024], comment_script[1024];
				char *p1, *p2, *p3;
				struct item_data *actual_item;

				actual_item = id;
				// escape item names
				memset(item_name, 0, sizeof(item_name));
				db_sql_escape_string(item_name, actual_item->name, strlen(actual_item->name));
				memset(item_jname, 0, sizeof(item_jname));
				db_sql_escape_string(item_jname, actual_item->jname, strlen(actual_item->jname));
				// extract script 1, 2 and 3
				memset(script1, 0, sizeof(script1));
				memset(script2, 0, sizeof(script2));
				memset(script3, 0, sizeof(script3));
				if ((p1 = strchr(np, '{')) != NULL && (p2 = strchr(p1, '}')) != NULL && (p3 = strchr(p3, '}')) != NULL)) {
					while(*p1 == ' ' || *p1 == '{')
						p1++;
					while((*p2 == ' ' || *p2 == '}') && p2 > p1)
						p2--;
					while((*p3 == ' ' || *p3 == '}') && p3 > p2)
						p3--;
					if (p2 > p1) {
						memset(temp_script, 0, sizeof(temp_script));
						strncpy(temp_script, p1, p2 - p1 + 1);
						memset(script1, 0, sizeof(script1));
						script1[0] = '\'';
						db_sql_escape_string(script1 + 1, temp_script, strlen(temp_script));
						strcat(script1, "'");
					}
					// search second script
					if ((p1 = strchr(p2, '{')) != NULL && (p2 = strchr(p1, '}')) != NULL) {
						while(*p1 == ' ' || *p1 == '{')
							p1++;
						while((*p2 == ' ' || *p2 == '}') && p2 > p1)
							p2--;
						if (p2 > p1) {
							memset(temp_script, 0, sizeof(temp_script));
							strncpy(temp_script, p1, p2 - p1 + 1);
							memset(script2, 0, sizeof(script2));
							script2[0] = '\'';
							db_sql_escape_string(script2 + 1, temp_script, strlen(temp_script));
							strcat(script2, "'");
						}
					}
					if ((p1 = strchr(p2, '{')) != NULL && (p2 = strchr(p1, '}')) != NULL) {
						while(*p1 == ' ' || *p1 == '{')
							p1++;
						while((*p2 == ' ' || *p2 == '}') && p2 > p1)
							p2--;
						if (p2 > p1) {
							memset(temp_script, 0, sizeof(temp_script));
							strncpy(temp_script, p1, p2 - p1 + 1);
							memset(script3, 0, sizeof(script3));
							script3[0] = '\'';
							db_sql_escape_string(script3 + 1, temp_script, strlen(temp_script));
							strcat(script3, "'");
						}
					}
				}

				memset(comment_script, 0 , sizeof(comment_script));
				if ((p1 = strchr(np, '/')) != NULL && p1[1] == '/') {
					comment_script[0] = ' ';
					comment_script[1] = '#';
					strcpy(comment_script + 2, p1 + 2);
				}
				// create request
				fprintf(item_db_fp, "INSERT INTO `%s` VALUES (%d, '%s', '%s', %d,"
				                                             " %d, %d,"
				                                             " %d, %d, %d, %d,"
				                                             " %d, '%s', %d, %d, %d,"
				                                             " %d, %d, %d, %d,"
				                                             " '%s', '%s', '%s');%s" RETCODE,
				                    item_db_db, nameid, item_name, item_jname, actual_item->type,
				                    atoi(str[4]), atoi(str[5]), // id->value_buy, id->value_sell: not modified
				                    actual_item->weight, actual_item->atk, actual_item->def, actual_item->range,
				                    actual_item->slot, str[11], actual_item->class_upper, actual_item->sex, actual_item->equip,
				                    actual_item->wlv, actual_item->elv, actual_item->flag.no_refine, actual_item->look,
				                    (script1[0] == 0) ? "NULL" : script1, (script2[0] == 0) ? "NULL" : script2, (script3[0] == 0) ? "NULL" : script3, comment_script);

			}
*/
#endif /* USE_SQL */
		}
		fclose(fp);
		printf("DB '" CL_WHITE "%s" CL_RESET "' readed ('" CL_WHITE "%d" CL_RESET "' %s).\n", filename[i], ln, (ln > 1) ? "entries" : "entry");
	}

#ifdef USE_SQL
		// code to create SQL item db
		if (create_item_db_script && item_db_fp != NULL) {
			fclose(item_db_fp);
		}
#endif /* USE_SQL */

	return 0;
}
Пример #5
0
/*==================================
* SQL
*===================================
*/
static int itemdb_read_sqldb(void) {
	unsigned short nameid;
	unsigned long ln = 0;
	struct item_data *id;
	int buy_price, sell_price;
	char script[65535 + 2 + 1]; // Maximum length of MySQL TEXT type (65535) + 2 bytes for curly brackets + 1 byte for terminator

	// ----------

	sql_request("SELECT * FROM `%s`", item_db_db);

	while (sql_get_row()) {
		/* +----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+-------------+---------------+-----------------+--------------+-------------+------------+------+--------+--------------+----------------+
		   |  0 |            1 |             2 |    3 |         4 |          5 |      6 |      7 |       8 |     9 |    10 |         11 |          12 |            13 |              14 |           15 |          16 |         17 |   18 |     19 |           20 |             21 |
		   +----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+-------------+---------------+-----------------+--------------+-------------+------------+------+--------+--------------+----------------+
		   | id | name_english | name_japanese | type | price_buy | price_sell | weight | attack | defence | range | slots | equip_jobs | equip_upper | equip_genders | equip_locations | weapon_level | equip_level | refineable | view | script | equip_script | unequip_script |
		   +----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+-------------+---------------+-----------------+--------------+-------------+------------+------+--------+--------------+----------------+ */

		nameid = sql_get_integer(0);

		// If the identifier is not within the valid range, process the next row
		if (nameid == 0 || nameid >= 20000)
			continue;
#ifdef __DEBUG
		if (battle_config.etc_log) {
			if (strlen(sql_get_string(1)) > 24)
				printf(CL_YELLOW "WARNING: Invalid item name" CL_RESET" (id: %d) - Name too long (> 24 char.) -> only 24 first characters are used.\n", nameid);
			if (strlen(sql_get_string(2)) > 24)
				printf(CL_YELLOW "WARNING: Invalid item jname" CL_RESET" (id: %d) - Name too long (> 24 char.) -> only 24 first characters are used.\n", nameid);
		}
#endif

		ln++;
		if (ln % 22 == 21) {
			printf("Reading item #%ld (id: %d)...\r", ln, nameid);
			fflush(stdout);
		}

		// Insert a new row into the item database

		// ----------
		id = itemdb_search(nameid);

		memset(id->name, 0, sizeof(id->name));
		strncpy(id->name, sql_get_string(1), ITEM_NAME_LENGTH);
		memset(id->jname, 0, sizeof(id->jname));
		strncpy(id->jname, sql_get_string(2), ITEM_NAME_LENGTH);

		id->type = sql_get_integer(3);
		if (id->type == 11) {
			id->type = 2;
			id->flag.delay_consume=1;
		}

		// fix NULL for buy/sell prices
		if (sql_get_string(4) == NULL)
			buy_price = 0;
		else
			buy_price = sql_get_integer(4);
		if (sql_get_string(5) == NULL)
			sell_price = 0;
		else
			sell_price = sql_get_integer(5);
		// If price_buy is not 0 and price_sell is not 0...
		if (buy_price > 0 && sell_price > 0) {
			id->value_buy = buy_price;
			id->value_sell = sell_price;
		// If price_buy is not 0 and price_sell is 0...
		} else if (buy_price > 0 && sell_price == 0) {
			id->value_buy = buy_price;
			id->value_sell = buy_price / 2;
		// If price_buy is 0 and price_sell is not 0...
		} else if (buy_price == 0 && sell_price > 0) {
			id->value_buy = sell_price * 2;
			id->value_sell = sell_price;
		// If price_buy is 0 and price_sell is 0...
		} else {
			id->value_buy = 0;
			id->value_sell = 0;
		}
		// check for bad prices that can possibly cause exploits
		if (((double)id->value_buy * (double)75) / 100 < ((double)id->value_sell * (double)124) / 100) {
			printf("Item %s [%d]: prices: buy %d / sell %d (" CL_YELLOW "WARNING" CL_RESET ").\n", id->name, id->nameid, id->value_buy, id->value_sell);
			printf("for merchant: buying: %d < selling:%d (" CL_YELLOW "possible exploit OC/DC" CL_RESET ").\n", id->value_buy * 75 / 100, id->value_sell * 124 / 100);
			id->value_buy = ((double)id->value_sell * (double)124) / (double)75 + 1;
			printf("->set to: buy %d, sell %d (change in database please).\n", id->value_buy, id->value_sell);
		}

		id->weight	= sql_get_integer( 6);

		id->atk   = (sql_get_string( 7) != NULL) ? sql_get_integer( 7) : 0;
		id->def   = (sql_get_string( 8) != NULL) ? sql_get_integer( 8) : 0;
		id->range = (sql_get_string( 9) != NULL) ? sql_get_integer( 9) : 0;
		id->slot  = (sql_get_string(10) != NULL) ? sql_get_integer(10) : 0;
		itemdb_jobid2mapid(id->class_base, (sql_get_string(11) != NULL) ? (unsigned int)strtoul(sql_get_string(11), NULL, 0) : 0);
		id->class_upper = (sql_get_string(12) != NULL) ? sql_get_integer(12) : 0;
		id->sex   = (sql_get_string(13) != NULL) ? sql_get_integer(13) : 0;
		id->equip = (sql_get_string(14) != NULL) ? sql_get_integer(14) : 0;
		id->wlv   = (sql_get_string(15) != NULL) ? sql_get_integer(15) : 0;
		id->elv   = (sql_get_string(16) != NULL) ? sql_get_integer(16) : 0;
		id->flag.no_refine = (sql_get_integer(17) == 0 || sql_get_integer(17) == 1)?0:1;
		id->look  = (sql_get_string(18) != NULL) ? sql_get_integer(18) : 0;

		id->view_id = 0;

		// ----------

		if (sql_get_string(19) != NULL) {
			if (sql_get_string(19)[0] == '{')
				id->script = parse_script((unsigned char *)sql_get_string(19), 0);
			else {
				sprintf(script, "{%s}", sql_get_string(19));
				id->script = parse_script((unsigned char *)script, 0);
			}
		} else {
			id->script = NULL;
		}

		if (sql_get_string(20) != NULL) {
			if (sql_get_string(20)[0] == '{')
				id->equip_script = parse_script((unsigned char *)sql_get_string(20), 0);
			else {
				sprintf(script, "{%s}", sql_get_string(20));
				id->equip_script = parse_script((unsigned char *)script, 0);
			}
		} else {
			id->equip_script = NULL;
		}

		if (sql_get_string(21) != NULL) {
			if (sql_get_string(21)[0] == '{')
				id->unequip_script = parse_script((unsigned char *)sql_get_string(21), 0);
			else {
				sprintf(script, "{%s}", sql_get_string(21));
				id->unequip_script = parse_script((unsigned char *)script, 0);
			}
		} else {
			id->equip_script = NULL;
		}

		// ----------

		id->flag.available   = 1;
		id->flag.value_notdc = 0;
		id->flag.value_notoc = 0;
	}

	printf("DB '" CL_WHITE "%s" CL_RESET "' readed ('" CL_WHITE "%ld" CL_RESET "' entrie%s).\n", item_db_db, ln, (ln > 1) ? "s" : "");

	return 0;
}
Пример #6
0
/*======================================
* SQL
*===================================
*/
static int itemdb_read_sqldb(void)
{
	unsigned short nameid;
	struct item_data *id;
	char script[65535 + 2 + 1]; // Maximum length of MySQL TEXT type (65535) + 2 bytes for curly brackets + 1 byte for terminator
	char *item_db_name[] = { item_db_db, item_db2_db };
	long unsigned int ln = 0;
	int i;	

	// ----------

	for (i = 0; i < 2; i++) {
		sprintf(tmp_sql, "SELECT * FROM `%s`", item_db_name[i]);

		// Execute the query; if the query execution succeeded...
		if (mysql_query(&mmysql_handle, tmp_sql) == 0) {
			sql_res = mysql_store_result(&mmysql_handle);

			// If the storage of the query result succeeded...
			if (sql_res) {
				// Parse each row in the query result into sql_row
				while ((sql_row = mysql_fetch_row(sql_res)))
				{
					/* +----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+---------------+-----------------+--------------+-------------+------+------------+--------------+
					   |  0 |            1 |             2 |    3 |         4 |          5 |      6 |      7 |       8 |     9 |    10 |         11 |          12 |            13 |              14 |           15 |   16        |         17 |   18 |     19 |
					   +----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+---------------+-----------------+--------------+-------------+------+------------+--------------+
					   | id | name_english | name_japanese | type | price_buy | price_sell | weight | attack | defence | range | slots | equip_jobs | equip_upper | equip_genders | equip_locations | weapon_level | equip_level | refineable | view | script |
					   +----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+---------------+-----------------+--------------+-------------+------+------------+--------------+ */

					nameid = atoi(sql_row[0]);

					// If the identifier is not within the valid range, process the next row
					if (nameid == 0 || nameid >= 20000)
						continue;

					ln++;

					// ----------
					id = itemdb_search(nameid);
					
					memcpy(id->name, sql_row[1], ITEM_NAME_LENGTH-1);
					memcpy(id->jname, sql_row[2], ITEM_NAME_LENGTH-1);

					id->type = atoi(sql_row[3]);
					if (id->type == 11)
					{	//Items that are consumed upon target confirmation
						//(yggdrasil leaf, spells & pet lures) [Skotlex]
						id->type = 2;
						id->flag.delay_consume=1;
					}

					// If price_buy is not NULL and price_sell is not NULL...
					if ((sql_row[4] != NULL) && (sql_row[5] != NULL)) {
						id->value_buy = atoi(sql_row[4]);
						id->value_sell = atoi(sql_row[5]);
					}
					// If price_buy is not NULL and price_sell is NULL...
					else if ((sql_row[4] != NULL) && (sql_row[5] == NULL)) {
						id->value_buy = atoi(sql_row[4]);
						id->value_sell = atoi(sql_row[4]) / 2;
					}
					// If price_buy is NULL and price_sell is not NULL...
					else if ((sql_row[4] == NULL) && (sql_row[5] != NULL)) {
						id->value_buy = atoi(sql_row[5]) * 2;
						id->value_sell = atoi(sql_row[5]);
					}
					// If price_buy is NULL and price_sell is NULL...
					if ((sql_row[4] == NULL) && (sql_row[5] == NULL)) {
						id->value_buy = 0;
						id->value_sell = 0;
					}

					id->weight	= atoi(sql_row[6]);
					id->atk		= (sql_row[7] != NULL) ? atoi(sql_row[7]) : 0;
					id->def		= (sql_row[8] != NULL) ? atoi(sql_row[8]) : 0;
					id->range	= (sql_row[9] != NULL) ? atoi(sql_row[9]) : 0;
					id->slot	= (sql_row[10] != NULL) ? atoi(sql_row[10]) : 0;
					if (id->slot > MAX_SLOTS)
					{
						ShowWarning("itemdb_read_sqldb: Item %d (%s) specifies %d slots, but the server only supports up to %d\n", nameid, id->jname, id->slot, MAX_SLOTS);
						id->slot = MAX_SLOTS;
					}
					itemdb_jobid2mapid(id->class_base, (sql_row[11] != NULL) ? atoi(sql_row[11]) : 0);
					id->class_upper= (sql_row[12] != NULL) ? atoi(sql_row[12]) : 0;
					id->sex		= (sql_row[13] != NULL) ? atoi(sql_row[13]) : 0;
					id->equip	= (sql_row[14] != NULL) ? atoi(sql_row[14]) : 0;
					id->wlv		= (sql_row[15] != NULL) ? atoi(sql_row[15]) : 0;
					id->elv		= (sql_row[16] != NULL)	? atoi(sql_row[16]) : 0;
					id->flag.no_refine = (sql_row[17] == NULL || atoi(sql_row[17]) == 1)?0:1;
					id->look	= (sql_row[18] != NULL) ? atoi(sql_row[18]) : 0;
					id->view_id	= 0;
					id->sex = itemdb_gendercheck(id); //Apply gender filtering.

					// ----------
					
					if (id->script)
						aFree(id->script);
					if (sql_row[19] != NULL) {
						if (sql_row[19][0] == '{')
							id->script = parse_script((unsigned char *) sql_row[19], 0);
						else {
							sprintf(script, "{%s}", sql_row[19]);
							id->script = parse_script((unsigned char *) script, 0);
						}
					} else id->script = NULL;

					// ----------

					id->flag.available	= 1;
					id->flag.value_notdc	= 0;
					id->flag.value_notoc	= 0;
				}

				ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", ln, item_db_name[i]);
				ln = 0;
			} else {
				ShowSQL("DB error (%s) - %s\n",item_db_name[i], mysql_error(&mmysql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
			}

			// Free the query result
			mysql_free_result(sql_res);
		} else {
			ShowSQL("DB error (%s) - %s\n",item_db_name[i], mysql_error(&mmysql_handle));
			ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
		}
	}

	return 0;
}