Skip to content

Commit

Permalink
Added support for missing or empty categories in cashshop_db.conf
Browse files Browse the repository at this point in the history
- If a category in cashshop_db.conf is empty or missing, it will no
  longer show an error and/or insert an apple (dummy item).
- Corrected a mapserver startup crash in case cashshop_db is missing
  elements.
- Special thanks to Ind for providing official server/client info to
  handle the case of empty categories.

Signed-off-by: Haru <haru@dotalux.com>
  • Loading branch information
MishimaHaruna committed Oct 19, 2013
1 parent 4139f24 commit 448dce8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 47 deletions.
4 changes: 4 additions & 0 deletions db/cashshop_db.conf
Expand Up @@ -14,6 +14,10 @@
// add in any amount of items you like within each category.
// Please keep in mind that the Cashshop does not work
// with ragexere clients.
// Categories can be empty or even missing, but, if
// present, their names must be kept as cat_<n>, where
// <n> is a valid tab index, as descripbed in 'enum
// CASH_SHOP_TABS' in clif.c (normally 0 through 7)
//====================================================

cash_shop: (
Expand Down
83 changes: 36 additions & 47 deletions src/map/clif.c
Expand Up @@ -17247,9 +17247,9 @@ void clif_parse_MoveItem(int fd, struct map_session_data *sd) {
/* [Ind/Hercules] */
void clif_cashshop_db(void) {
config_t cashshop_conf;
config_setting_t *cashshop = NULL;
config_setting_t *cashshop = NULL, *cats = NULL;
const char *config_filename = "db/cashshop_db.conf"; // FIXME hardcoded name
int i;
int i, item_count_t = 0;
for( i = 0; i < CASHSHOP_TAB_MAX; i++ ) {
CREATE(clif->cs.data[i], struct hCSData *, 1);
clif->cs.item_count[i] = 0;
Expand All @@ -17262,67 +17262,53 @@ void clif_cashshop_db(void) {

cashshop = config_lookup(&cashshop_conf, "cash_shop");

if (cashshop != NULL) {
config_setting_t *cats = config_setting_get_elem(cashshop, 0);
config_setting_t *cat;
int k, item_count_t = 0;

if( cashshop != NULL && (cats = config_setting_get_elem(cashshop, 0)) != NULL ) {
for(i = 0; i < CASHSHOP_TAB_MAX; i++) {
config_setting_t *cat;
char entry_name[10];

sprintf(entry_name,"cat_%d",i);

if( (cat = config_setting_get_member(cats, entry_name)) != NULL ) {
int item_count = config_setting_length(cat);
int k, item_count = config_setting_length(cat);

if( item_count == 0 ) {
ShowWarning("cashshop_db: category '%s' is empty! adding dull apple!\n", entry_name);
RECREATE(clif->cs.data[i], struct hCSData *, ++clif->cs.item_count[i]);
CREATE(clif->cs.data[i][ clif->cs.item_count[i] - 1 ], struct hCSData , 1);

clif->cs.data[i][ clif->cs.item_count[i] - 1 ]->id = UNKNOWN_ITEM_ID;
clif->cs.data[i][ clif->cs.item_count[i] - 1 ]->price = 999;
} else {
for(k = 0; k < item_count; k++) {
config_setting_t *entry = config_setting_get_elem(cat,k);
const char *name = config_setting_name(entry);
int price = config_setting_get_int(entry);
struct item_data * data = NULL;

if( price < 1 ) {
ShowWarning("cashshop_db: unsupported price '%d' for entry named '%s' in category '%s'\n", price, name, entry_name);
for(k = 0; k < item_count; k++) {
config_setting_t *entry = config_setting_get_elem(cat,k);
const char *name = config_setting_name(entry);
int price = config_setting_get_int(entry);
struct item_data * data = NULL;

if( price < 1 ) {
ShowWarning("cashshop_db: unsupported price '%d' for entry named '%s' in category '%s'\n", price, name, entry_name);
continue;
}

if( name[0] == 'I' && name[1] == 'D' && strlen(name) <= 7 ) {
if( !( data = itemdb->exists(atoi(name+2))) ) {
ShowWarning("cashshop_db: unknown item id '%s' in category '%s'\n", name+2, entry_name);
continue;
}

if( name[0] == 'I' && name[1] == 'D' && strlen(name) <= 7 ) {
if( !( data = itemdb->exists(atoi(name+2))) ) {
ShowWarning("cashshop_db: unknown item id '%s' in category '%s'\n", name+2, entry_name);
continue;
}
} else {
if( !( data = itemdb->search_name(name) ) ) {
ShowWarning("cashshop_db: unknown item name '%s' in category '%s'\n", name, entry_name);
continue;
}
} else {
if( !( data = itemdb->search_name(name) ) ) {
ShowWarning("cashshop_db: unknown item name '%s' in category '%s'\n", name, entry_name);
continue;
}


RECREATE(clif->cs.data[i], struct hCSData *, ++clif->cs.item_count[i]);
CREATE(clif->cs.data[i][ clif->cs.item_count[i] - 1 ], struct hCSData , 1);

clif->cs.data[i][ clif->cs.item_count[i] - 1 ]->id = data->nameid;
clif->cs.data[i][ clif->cs.item_count[i] - 1 ]->price = price;
item_count_t++;
}


RECREATE(clif->cs.data[i], struct hCSData *, ++clif->cs.item_count[i]);
CREATE(clif->cs.data[i][ clif->cs.item_count[i] - 1 ], struct hCSData , 1);

clif->cs.data[i][ clif->cs.item_count[i] - 1 ]->id = data->nameid;
clif->cs.data[i][ clif->cs.item_count[i] - 1 ]->price = price;
item_count_t++;
}
} else {
ShowError("cashshop_db: category '%s' (%d) not found!!\n",entry_name,i);
}
}

ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", item_count_t, config_filename);
config_destroy(&cashshop_conf);
}
ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", item_count_t, config_filename);
}
/// Items that are in favorite tab of inventory (ZC_ITEM_FAVORITE).
/// 0900 <index>.W <favorite>.B
Expand Down Expand Up @@ -17377,6 +17363,9 @@ void clif_parse_CashShopSchedule(int fd, struct map_session_data *sd) {
int i, j = 0;

for( i = 0; i < CASHSHOP_TAB_MAX; i++ ) {
if( clif->cs.item_count[i] == 0 )
continue; // Skip empty tabs, the client only expects filled ones

WFIFOHEAD(fd, 8 + ( clif->cs.item_count[i] * 6 ) );
WFIFOW(fd, 0) = 0x8ca;
WFIFOW(fd, 2) = 8 + ( clif->cs.item_count[i] * 6 );
Expand Down Expand Up @@ -17478,7 +17467,7 @@ void clif_parse_CashShopReqTab(int fd, struct map_session_data *sd) {
short tab = RFIFOW(fd, 2);
int j;

if( tab < 0 || tab > CASHSHOP_TAB_MAX )
if( tab < 0 || tab > CASHSHOP_TAB_MAX || clif->cs.item_count[tab] == 0 )
return;

WFIFOHEAD(fd, 10 + ( clif->cs.item_count[tab] * 6 ) );
Expand Down

0 comments on commit 448dce8

Please sign in to comment.