From c1014ea864aeb550d89839df9caf6405faff1b07 Mon Sep 17 00:00:00 2001 From: CairoLee Date: Mon, 8 Feb 2016 15:33:39 +0800 Subject: [PATCH 1/3] Use sv_readdb to read CashShop Database --- src/map/cashshop.c | 141 ++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 91 deletions(-) diff --git a/src/map/cashshop.c b/src/map/cashshop.c index 407ef295cf..7fb256637a 100644 --- a/src/map/cashshop.c +++ b/src/map/cashshop.c @@ -23,112 +23,71 @@ extern char item_cash_db2_db[32]; * 0 = failure * 1 = success */ -static int cashshop_parse_dbrow( char** str, const char* source, int line ){ - unsigned short nameid = atoi( str[1] ); +static bool cashshop_parse_dbrow(char* fields[], int columns, int current) { + uint16 tab = atoi(fields[0]); + unsigned short nameid = atoi(fields[1]); + uint32 price = atoi(fields[2]); + int j; + struct cash_item_data* cid; - if( itemdb_exists( nameid ) ){ - uint16 tab = atoi( str[0] ); - uint32 price = atoi( str[2] ); - struct cash_item_data* cid; - int j; - - if( tab > CASHSHOP_TAB_SEARCH ){ - ShowWarning( "cashshop_parse_dbrow: Invalid tab %d in line %d of \"%s\", skipping...\n", tab, line, source ); - return 0; - }else if( price < 1 ){ - ShowWarning( "cashshop_parse_dbrow: Invalid price %d in line %d of \"%s\", skipping...\n", price, line, source ); - return 0; - } - - ARR_FIND( 0, cash_shop_items[tab].count, j, nameid == cash_shop_items[tab].item[j]->nameid ); - - if( j == cash_shop_items[tab].count ){ - RECREATE( cash_shop_items[tab].item, struct cash_item_data *, ++cash_shop_items[tab].count ); - CREATE( cash_shop_items[tab].item[ cash_shop_items[tab].count - 1], struct cash_item_data, 1 ); - cid = cash_shop_items[tab].item[ cash_shop_items[tab].count - 1]; - }else{ - cid = cash_shop_items[tab].item[j]; - } - - cid->nameid = nameid; - cid->price = price; - cash_shop_defined = true; - return 1; - }else{ - ShowWarning( "cashshop_parse_dbrow: Invalid ID %hu in line %d of \"%s\", skipping...\n", nameid, line, source ); + if( !itemdb_exists( nameid ) ){ + ShowWarning( "cashshop_parse_dbrow: Invalid ID %hu in line %d, skipping...\n", nameid, current ); + return 0; } - return 0; + if( tab > CASHSHOP_TAB_SEARCH ){ + ShowWarning( "cashshop_parse_dbrow: Invalid tab %d in line %d, skipping...\n", tab, current ); + return 0; + }else if( price < 1 ){ + ShowWarning( "cashshop_parse_dbrow: Invalid price %d in line %d, skipping...\n", price, current ); + return 0; + } + + ARR_FIND( 0, cash_shop_items[tab].count, j, nameid == cash_shop_items[tab].item[j]->nameid ); + + if( j == cash_shop_items[tab].count ){ + RECREATE( cash_shop_items[tab].item, struct cash_item_data *, ++cash_shop_items[tab].count ); + CREATE( cash_shop_items[tab].item[ cash_shop_items[tab].count - 1], struct cash_item_data, 1 ); + cid = cash_shop_items[tab].item[ cash_shop_items[tab].count - 1]; + }else{ + cid = cash_shop_items[tab].item[j]; + } + + cid->nameid = nameid; + cid->price = price; + cash_shop_defined = true; + + return 1; } /* * Reads database from TXT format, * parses lines and sends them to parse_dbrow. - * TODO: Change to sv_readdb */ static void cashshop_read_db_txt( void ){ - const char* filename[] = { DBPATH"item_cash_db.txt", DBIMPORT"/item_cash_db.txt" }; + const char* dbsubpath[] = { + "", + "/"DBIMPORT, + }; int fi; - for( fi = 0; fi < ARRAYLENGTH( filename ); ++fi ){ - uint32 lines = 0, count = 0; - char line[1024]; + for( fi = 0; fi < ARRAYLENGTH( dbsubpath ); ++fi ){ + uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[fi])+1); + uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[fi])+1); + char* dbsubpath1 = (char*)aMalloc(n1+1); + char* dbsubpath2 = (char*)aMalloc(n2+1); - char path[256]; - FILE* fp; - sprintf( path, "%s/%s", db_path, filename[fi] ); - fp = fopen( path, "r" ); - if( fp == NULL ) { - ShowWarning( "itemdb_readdb: File not found \"%s\", skipping.\n", path ); - continue; + if(fi==0) { + safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[fi]); + safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[fi]); + } + else { + safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[fi]); + safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[fi]); } - while( fgets( line, sizeof( line ), fp ) ){ - char *str[3], *p; - int i; - lines++; - - if( line[0] == '/' && line[1] == '/' ) - continue; - - memset( str, 0, sizeof( str ) ); - - p = line; - while( ISSPACE( *p ) ) - ++p; - if( *p == '\0' ) - continue; - - for( i = 0; i < 2; ++i ){ - str[i] = p; - p = strchr( p, ',' ); - - if( p == NULL ) - break; - - *p = '\0'; - ++p; - } - - str[2] = p; - while( !ISSPACE( *p ) && *p != '\0' && *p != '/' ) - ++p; - - if( p == NULL ){ - ShowError("cashshop_read_db_txt: Insufficient columns in line %d of \"%s\" (item with id %d), skipping.\n", lines, path, atoi( str[0] ) ); - continue; - } - - if( !cashshop_parse_dbrow( str, path, lines ) ) - continue; - - count++; - } - - fclose(fp); - - ShowStatus( "Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, path ); + sv_readdb(dbsubpath2, "item_cash_db.txt", ',', 3, 3, -1, &cashshop_parse_dbrow, fi); } } @@ -162,7 +121,7 @@ static int cashshop_read_db_sql( void ){ } } - if( !cashshop_parse_dbrow( str, cash_db_name[fi], lines ) ) + if( !cashshop_parse_dbrow( str, 3, lines ) ) continue; ++count; From b43853e17443061a840a735fae9f8e16557c10d1 Mon Sep 17 00:00:00 2001 From: CairoLee Date: Mon, 8 Feb 2016 19:45:31 +0800 Subject: [PATCH 2/3] Follow up to c1014ea Thanks to cydh --- src/map/cashshop.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/map/cashshop.c b/src/map/cashshop.c index 7fb256637a..6c89895f02 100644 --- a/src/map/cashshop.c +++ b/src/map/cashshop.c @@ -31,15 +31,15 @@ static bool cashshop_parse_dbrow(char* fields[], int columns, int current) { struct cash_item_data* cid; if( !itemdb_exists( nameid ) ){ - ShowWarning( "cashshop_parse_dbrow: Invalid ID %hu in line %d, skipping...\n", nameid, current ); + ShowWarning( "cashshop_parse_dbrow: Invalid ID %hu in line '%d', skipping...\n", nameid, current ); return 0; } if( tab > CASHSHOP_TAB_SEARCH ){ - ShowWarning( "cashshop_parse_dbrow: Invalid tab %d in line %d, skipping...\n", tab, current ); + ShowWarning( "cashshop_parse_dbrow: Invalid tab %d in line '%d', skipping...\n", tab, current ); return 0; }else if( price < 1 ){ - ShowWarning( "cashshop_parse_dbrow: Invalid price %d in line %d, skipping...\n", price, current ); + ShowWarning( "cashshop_parse_dbrow: Invalid price %d in line '%d', skipping...\n", price, current ); return 0; } @@ -121,8 +121,10 @@ static int cashshop_read_db_sql( void ){ } } - if( !cashshop_parse_dbrow( str, 3, lines ) ) + if( !cashshop_parse_dbrow( str, 3, lines ) ) { + ShowError("cashshop_read_db_sql: Cannot process table '%s' at line '%d', skipping...\n", cash_db_name[fi], lines); continue; + } ++count; } From b96fba7e3625e656ec4db1e37b9cd0345f110e81 Mon Sep 17 00:00:00 2001 From: CairoLee Date: Tue, 9 Feb 2016 12:55:26 +0800 Subject: [PATCH 3/3] Follow up to b43853e --- src/map/cashshop.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/map/cashshop.c b/src/map/cashshop.c index 6c89895f02..b3ff7dc7cf 100644 --- a/src/map/cashshop.c +++ b/src/map/cashshop.c @@ -77,7 +77,6 @@ static void cashshop_read_db_txt( void ){ char* dbsubpath1 = (char*)aMalloc(n1+1); char* dbsubpath2 = (char*)aMalloc(n2+1); - if(fi==0) { safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[fi]); safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[fi]); @@ -88,6 +87,9 @@ static void cashshop_read_db_txt( void ){ } sv_readdb(dbsubpath2, "item_cash_db.txt", ',', 3, 3, -1, &cashshop_parse_dbrow, fi); + + aFree(dbsubpath1); + aFree(dbsubpath2); } }