From 3c73a02748ca50a32005a5b73933a582cd87b80f Mon Sep 17 00:00:00 2001 From: ultramage Date: Thu, 16 Aug 2007 14:14:11 +0000 Subject: * Fixed npc chatrooms not being deleted on npc unload/reload (crash) * Modified chat code a bit for more clarity - replaced double-indirection on 'owner' with a normal pointer - separated some common functionality to 'chat_createchat()' git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@11023 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/map/chat.c | 253 ++++++++++++++++++++++++++----------------------------- src/map/chat.h | 12 +-- src/map/clif.c | 35 +++++--- src/map/map.c | 2 +- src/map/map.h | 4 +- src/map/npc.c | 9 +- src/map/script.c | 7 +- 7 files changed, 159 insertions(+), 163 deletions(-) (limited to 'src/map') diff --git a/src/map/chat.c b/src/map/chat.c index b5b16f28c..3e8ebeb2c 100644 --- a/src/map/chat.c +++ b/src/map/chat.c @@ -16,78 +16,87 @@ #include #include -int chat_triggerevent(struct chat_data *cd); +int chat_triggerevent(struct chat_data *cd); // forward declaration + +/// Initializes a chatroom object (common functionality for both pc and npc chatrooms). +/// Returns a chatroom object on success, or NULL on failure. +static struct chat_data* chat_createchat(struct block_list* bl, const char* title, const char* pass, int limit, bool pub, int trigger, const char* ev) +{ + struct chat_data* cd; + nullpo_retr(NULL, bl); + + cd = (struct chat_data *) aMalloc(sizeof(struct chat_data)); + + safestrncpy(cd->title, title, sizeof(cd->title)); + safestrncpy(cd->pass, pass, sizeof(cd->pass)); + cd->pub = pub; + cd->users = 0; + cd->limit = limit; + cd->trigger = trigger; + memset(cd->usersd, 0, sizeof(cd->usersd)); + cd->owner = bl; + safestrncpy(cd->npc_event, ev, sizeof(cd->npc_event)); + + cd->bl.m = bl->m; + cd->bl.x = bl->x; + cd->bl.y = bl->y; + cd->bl.type = BL_CHAT; + cd->bl.next = cd->bl.prev = NULL; + cd->bl.id = map_addobject(&cd->bl); + + if( cd->bl.id == 0 ) { + aFree(cd); + cd = NULL; + } + + return cd; +} /*========================================== - * chatroom creation + * player chatroom creation *------------------------------------------*/ -int chat_createchat(struct map_session_data* sd, int limit, bool pub, char* pass, char* title, int titlelen) +int chat_createpcchat(struct map_session_data* sd, const char* title, const char* pass, int limit, bool pub) { - struct chat_data *cd; - + struct chat_data* cd; nullpo_retr(0, sd); if (sd->chatID) - return 0; //Prevent people abusing the chat system by creating multiple chats, as pointed out by End of Exam. [Skotlex] + return 0; //Prevent people abusing the chat system by creating multiple chats, as pointed out by End of Exam. [Skotlex] if (map[sd->bl.m].flag.nochat) { clif_displaymessage (sd->fd, msg_txt(281)); return 0; //Can't create chatrooms on this map. } pc_stop_walking(sd,1); - cd = (struct chat_data *) aMalloc(sizeof(struct chat_data)); - - cd->limit = limit; - cd->pub = pub; - cd->users = 1; - titlelen = cap_value(titlelen, 0, sizeof(cd->title)-1); // empty string achievable by using custom client - // the following two input strings aren't zero terminated and need to be handled carefully - safestrncpy(cd->title, title, min(titlelen+1,CHATROOM_TITLE_SIZE)); - safestrncpy(cd->pass, pass, CHATROOM_PASS_SIZE); - cd->owner = (struct block_list **)(&cd->usersd[0]); - cd->usersd[0] = sd; - cd->bl.m = sd->bl.m; - cd->bl.x = sd->bl.x; - cd->bl.y = sd->bl.y; - cd->bl.type = BL_CHAT; - cd->bl.next = cd->bl.prev = NULL; - cd->bl.id = map_addobject(&cd->bl); - if(cd->bl.id==0){ + cd = chat_createchat(&sd->bl, title, pass, limit, pub, 0, ""); + if (cd) { + cd->users = 1; + cd->usersd[0] = sd; + pc_setchatid(sd,cd->bl.id); + clif_createchat(sd,0); + clif_dispchat(cd,0); + } else clif_createchat(sd,1); - aFree(cd); - return 0; - } - pc_setchatid(sd,cd->bl.id); - - clif_createchat(sd,0); - clif_dispchat(cd,0); return 0; } /*========================================== - * 既存チャットルームに参加 + * join an existing chatroom *------------------------------------------*/ -int chat_joinchat(struct map_session_data* sd, int chatid, char* pass) +int chat_joinchat(struct map_session_data* sd, int chatid, const char* pass) { - struct chat_data *cd; + struct chat_data* cd; nullpo_retr(0, sd); cd = (struct chat_data*)map_id2bl(chatid); - //No need for a nullpo check. The chatid was sent by the client, if they lag or mess with the packet - //a wrong chat id can be received. [Skotlex] - if (cd == NULL) - return 1; - if (cd->bl.type != BL_CHAT || cd->bl.m != sd->bl.m || sd->vender_id || sd->chatID || cd->limit <= cd->users) { + if (cd == NULL || cd->bl.type != BL_CHAT || cd->bl.m != sd->bl.m || sd->vender_id || sd->chatID || cd->users >= cd->limit) { clif_joinchatfail(sd,0); return 0; } - //Allows Gm access to protected room with any password they want by valaris - if ((!cd->pub && strncmp(pass, (char *)cd->pass, 8) && (pc_isGM(sd) < battle_config.gm_join_chat || !battle_config.gm_join_chat)) || - chatid == (int)sd->chatID) //Double Chat fix by Alex14, thx CHaNGeTe - { + if (!cd->pub && strncmp(pass, cd->pass, sizeof(cd->pass)) != 0 && !(battle_config.gm_join_chat && pc_isGM(sd) >= battle_config.gm_join_chat)) { clif_joinchatfail(sd,1); return 0; } @@ -107,7 +116,10 @@ int chat_joinchat(struct map_session_data* sd, int chatid, char* pass) return 0; } -/// Removes the user from the chat room. + +/*========================================== + * leave a chatroom + *------------------------------------------*/ int chat_leavechat(struct map_session_data* sd) { struct chat_data* cd; @@ -123,109 +135,105 @@ int chat_leavechat(struct map_session_data* sd) return 1; } - for( i = 0, leavechar = -1; i < cd->users; i++ ) - { - if(cd->usersd[i] == sd){ - leavechar=i; - break; - } - } - if( leavechar < 0 ) + ARR_FIND( 0, cd->users, i, cd->usersd[i] == sd ); + if ( i == cd->users ) {// Not found in the chatroom? pc_setchatid(sd, 0); return -1; } - if( leavechar == 0 && cd->users > 1 && (*cd->owner)->type == BL_PC ) - {// Change ownership to the next user - clif_changechatowner(cd, cd->usersd[1]); - clif_clearchat(cd, 0); - } + leavechar = i; - // 抜けるPCにも送るのでusersを減らす前に実行 clif_leavechat(cd, sd); - cd->users--; + for( i = leavechar; i < cd->users; i++ ) + cd->usersd[i] = cd->usersd[i + 1]; + pc_setchatid(sd, 0); + cd->users--; - if( cd->users == 0 && (*cd->owner)->type == BL_PC ) + if( cd->users == 0 && cd->owner->type == BL_PC ) {// Delete empty chatroom clif_clearchat(cd, 0); map_delobject(cd->bl.id); return 1; } - for( i = leavechar; i < cd->users; i++ ) - cd->usersd[i] = cd->usersd[i + 1]; - if( leavechar == 0 && (*cd->owner)->type == BL_PC ) - { + if( leavechar == 0 && cd->owner->type == BL_PC ) + { // Set and announce new owner + cd->owner = (struct block_list*) cd->usersd[0]; + clif_changechatowner(cd, cd->usersd[0]); + clif_clearchat(cd, 0); + //Adjust Chat location after owner has been changed. map_delblock( &cd->bl ); cd->bl.x=cd->usersd[0]->bl.x; cd->bl.y=cd->usersd[0]->bl.y; map_addblock( &cd->bl ); + + clif_dispchat(cd,0); } - clif_dispchat(cd,0); + else + clif_dispchat(cd,0); // refresh chatroom return 0; } /*========================================== - * チャットルームの持ち主を譲る + * change a chatroom's owner *------------------------------------------*/ -int chat_changechatowner(struct map_session_data* sd, char* nextownername) +int chat_changechatowner(struct map_session_data* sd, const char* nextownername) { - struct chat_data *cd; - struct map_session_data *tmp_sd; + struct chat_data* cd; + struct map_session_data* tmpsd; int i, nextowner; nullpo_retr(1, sd); cd = (struct chat_data*)map_id2bl(sd->chatID); - if (cd == NULL || (struct block_list *)sd != (*cd->owner)) + if (cd == NULL || (struct block_list*) sd != cd->owner) return 1; - for(i = 1,nextowner=-1;i < cd->users;i++){ - if(strcmp(cd->usersd[i]->status.name,nextownername)==0){ - nextowner=i; - break; - } - } - if(nextowner<0) // そんな人は居ない - return -1; + ARR_FIND( 1, cd->users, i, strncmp(cd->usersd[i]->status.name, nextownername, NAME_LENGTH) == 0 ); + if (i == cd->users) + return -1; // name not found - clif_changechatowner(cd,cd->usersd[nextowner]); - // 一旦消す + // erase temporarily clif_clearchat(cd,0); - // userlistの順番変更 (0が所有者なので) - if( (tmp_sd = cd->usersd[0]) == NULL ) //FIXME: How is this even possible!? Invoking character should be owner, hence, it SHOULD be on sc->usersd[0]! - return 1; //ありえるのかな? - cd->usersd[0] = cd->usersd[nextowner]; - cd->usersd[nextowner] = tmp_sd; + // set new owner + nextowner = i; + cd->owner = (struct block_list*) cd->usersd[nextowner]; + clif_changechatowner(cd,cd->usersd[nextowner]); + // change the owner's position (placing him to 0) + tmpsd = cd->usersd[nextowner]; + cd->usersd[nextowner] = cd->usersd[0]; + cd->usersd[0] = tmpsd; + + // set the new chatroom position map_delblock( &cd->bl ); - cd->bl.x=cd->usersd[0]->bl.x; - cd->bl.y=cd->usersd[0]->bl.y; + cd->bl.x = cd->owner->x; + cd->bl.y = cd->owner->y; map_addblock( &cd->bl ); - // 再度表示 + // and display again clif_dispchat(cd,0); return 0; } /*========================================== - * チャットの状態(タイトル等)を変更 + * change a chatroom's status (title, etc) *------------------------------------------*/ -int chat_changechatstatus(struct map_session_data* sd, char* title, char* pass, int limit, bool pub) +int chat_changechatstatus(struct map_session_data* sd, const char* title, const char* pass, int limit, bool pub) { - struct chat_data *cd; + struct chat_data* cd; nullpo_retr(1, sd); cd=(struct chat_data*)map_id2bl(sd->chatID); - if(cd==NULL || (struct block_list *)sd != (*cd->owner)) + if(cd==NULL || (struct block_list *)sd != cd->owner) return 1; safestrncpy(cd->title, title, CHATROOM_TITLE_SIZE); @@ -240,66 +248,43 @@ int chat_changechatstatus(struct map_session_data* sd, char* title, char* pass, } /*========================================== - * チャットルームから蹴り出す + * kick an user from a chatroom *------------------------------------------*/ -int chat_kickchat(struct map_session_data* sd,char* kickusername) +int chat_kickchat(struct map_session_data* sd, const char* kickusername) { - struct chat_data *cd; + struct chat_data* cd; int i; nullpo_retr(1, sd); cd = (struct chat_data *)map_id2bl(sd->chatID); - if (!cd) return -1; + if (!cd) + return -1; - for(i = 0; i < cd->users; i++) { - if (strcmp(cd->usersd[i]->status.name, kickusername) == 0) { - if (battle_config.gm_kick_chat && pc_isGM(cd->usersd[i]) >= battle_config.gm_kick_chat) - //gm kick protection by valaris - return 0; + ARR_FIND( 0, cd->users, i, strncmp(cd->usersd[i]->status.name, kickusername, NAME_LENGTH) == 0 ); + if (i == cd->users) + return -1; - chat_leavechat(cd->usersd[i]); - return 0; - } - } + if (battle_config.gm_kick_chat && pc_isGM(cd->usersd[i]) >= battle_config.gm_kick_chat) + return 0; //gm kick protection [Valaris] - return -1; + chat_leavechat(cd->usersd[i]); + return 0; } /// Creates a chat room for the npc. -int chat_createnpcchat(struct npc_data* nd, int limit, bool pub, int trigger, const char* title, const char* ev) +int chat_createnpcchat(struct npc_data* nd, const char* title, int limit, bool pub, int trigger, const char* ev) { - struct chat_data *cd; - - nullpo_retr(1, nd); - - cd = (struct chat_data *) aMalloc(sizeof(struct chat_data)); - - cd->limit = cd->trigger = limit; - if( trigger > 0 ) - cd->trigger = trigger; - cd->pub = pub; - cd->users = 0; + struct chat_data* cd; + nullpo_retr(0, nd); - safestrncpy(cd->title, title, CHATROOM_TITLE_SIZE); - memset(cd->pass, '\0', CHATROOM_PASS_SIZE); - cd->bl.m = nd->bl.m; - cd->bl.x = nd->bl.x; - cd->bl.y = nd->bl.y; - cd->bl.type = BL_CHAT; - cd->bl.prev = cd->bl.next = NULL; - cd->owner = &(struct block_list *)nd; - safestrncpy(cd->npc_event, ev, ARRAYLENGTH(cd->npc_event)); - cd->bl.id = map_addobject(&cd->bl); - if( cd->bl.id == 0) + cd = chat_createchat(&nd->bl, title, "", limit, pub, trigger, ev); + if (cd) { - aFree(cd); - return 0; + nd->chat_id = cd->bl.id; + clif_dispchat(cd,0); } - nd->chat_id = cd->bl.id; - - clif_dispchat(cd, 0); return 0; } diff --git a/src/map/chat.h b/src/map/chat.h index e6a0c819e..aac356584 100644 --- a/src/map/chat.h +++ b/src/map/chat.h @@ -8,14 +8,14 @@ struct map_session_data; struct chat_data; -int chat_createchat(struct map_session_data* sd, int limit, bool pub, char* pass, char* title, int titlelen); -int chat_joinchat(struct map_session_data* sd, int chatid, char* pass); +int chat_createpcchat(struct map_session_data* sd, const char* title, const char* pass, int limit, bool pub); +int chat_joinchat(struct map_session_data* sd, int chatid, const char* pass); int chat_leavechat(struct map_session_data* sd); -int chat_changechatowner(struct map_session_data* sd, char* nextownername); -int chat_changechatstatus(struct map_session_data* sd, char* title, char* pass, int limit, bool pub); -int chat_kickchat(struct map_session_data* sd, char* kickusername); +int chat_changechatowner(struct map_session_data* sd, const char* nextownername); +int chat_changechatstatus(struct map_session_data* sd, const char* title, const char* pass, int limit, bool pub); +int chat_kickchat(struct map_session_data* sd, const char* kickusername); -int chat_createnpcchat(struct npc_data* nd, int limit, bool pub, int trigger, const char* title, const char* ev); +int chat_createnpcchat(struct npc_data* nd, const char* title, int limit, bool pub, int trigger, const char* ev); int chat_deletenpcchat(struct npc_data* nd); int chat_enableevent(struct chat_data* cd); int chat_disableevent(struct chat_data* cd); diff --git a/src/map/clif.c b/src/map/clif.c index 335d94ccf..d228ea6f7 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -3179,16 +3179,16 @@ int clif_createchat(struct map_session_data *sd,int fail) /*========================================== * *------------------------------------------*/ -int clif_dispchat(struct chat_data *cd,int fd) +int clif_dispchat(struct chat_data* cd, int fd) { unsigned char buf[128]; // 最大title(60バイト)+17 - if(cd==NULL || *cd->owner==NULL) + if(cd==NULL || cd->owner==NULL) return 1; WBUFW(buf,0)=0xd7; WBUFW(buf,2)=strlen((const char*)cd->title)+17; - WBUFL(buf,4)=(*cd->owner)->id; + WBUFL(buf,4)=cd->owner->id; WBUFL(buf,8)=cd->bl.id; WBUFW(buf,12)=cd->limit; WBUFW(buf,14)=cd->users; @@ -3199,7 +3199,7 @@ int clif_dispchat(struct chat_data *cd,int fd) memcpy(WFIFOP(fd,0),buf,WBUFW(buf,2)); WFIFOSET(fd,WBUFW(buf,2)); } else { - clif_send(buf,WBUFW(buf,2),*cd->owner,AREA_WOSC); + clif_send(buf,WBUFW(buf,2),cd->owner,AREA_WOSC); } return 0; @@ -3245,7 +3245,7 @@ int clif_clearchat(struct chat_data *cd,int fd) memcpy(WFIFOP(fd,0),buf,packet_len(0xd8)); WFIFOSET(fd,packet_len(0xd8)); } else { - clif_send(buf,packet_len(0xd8),*cd->owner,AREA_WOSC); + clif_send(buf,packet_len(0xd8),cd->owner,AREA_WOSC); } return 0; @@ -3289,7 +3289,7 @@ int clif_joinchatok(struct map_session_data *sd,struct chat_data* cd) WFIFOW(fd, 2) = 8 + (28*cd->users); WFIFOL(fd, 4) = cd->bl.id; for (i = 0; i < cd->users; i++) { - WFIFOL(fd, 8+i*28) = (i!=0) || ((*cd->owner)->type == BL_NPC); + WFIFOL(fd, 8+i*28) = (i != 0 || cd->owner->type == BL_NPC); memcpy(WFIFOP(fd, 8+i*28+4), cd->usersd[i]->status.name, NAME_LENGTH); } WFIFOSET(fd, WFIFOW(fd, 2)); @@ -3318,7 +3318,7 @@ int clif_addchat(struct chat_data* cd,struct map_session_data *sd) /*========================================== * *------------------------------------------*/ -int clif_changechatowner(struct chat_data* cd,struct map_session_data *sd) +int clif_changechatowner(struct chat_data* cd, struct map_session_data* sd) { unsigned char buf[64]; @@ -9250,14 +9250,27 @@ void clif_parse_NpcSellListSend(int fd,struct map_session_data *sd) /*========================================== * *------------------------------------------*/ -void clif_parse_CreateChatRoom(int fd,struct map_session_data *sd) +void clif_parse_CreateChatRoom(int fd, struct map_session_data* sd) { + int len = RFIFOW(fd,2)-15; + int limit = RFIFOW(fd,4); + bool public = (bool)RFIFOB(fd,6); + const char* password = (char*)RFIFOP(fd,7); //not zero-terminated + const char* title = (char*)RFIFOP(fd,15); // not zero-terminated + char s_title[CHATROOM_TITLE_SIZE]; + char s_password[CHATROOM_PASS_SIZE]; + if (sd->sc.data[SC_NOCHAT].timer!=-1 && sd->sc.data[SC_NOCHAT].val1&MANNER_NOROOM) return; - if(battle_config.basic_skill_check == 0 || pc_checkskill(sd,NV_BASIC) >= 4){ - chat_createchat(sd,RFIFOW(fd,4),RFIFOB(fd,6),(char*)RFIFOP(fd,7),(char*)RFIFOP(fd,15),RFIFOW(fd,2)-15); - } else + if(battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 4) { clif_skill_fail(sd,1,0,3); + return; + } + + safestrncpy(s_title, title, min(len+1,CHATROOM_TITLE_SIZE)); + safestrncpy(s_password, password, CHATROOM_PASS_SIZE); + + chat_createpcchat(sd, s_title, s_password, limit, public); } /*========================================== diff --git a/src/map/map.c b/src/map/map.c index 82a557109..971ffc9f0 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -1844,7 +1844,7 @@ struct map_session_data * map_nick2sd(const char *nick) struct block_list * map_id2bl(int id) { struct block_list *bl=NULL; - if(id >= 0 && id < sizeof(objects)/sizeof(objects[0])) + if(id >= 0 && id < ARRAYLENGTH(objects)) bl = objects[id]; else bl = idb_get(id_db,id); diff --git a/src/map/map.h b/src/map/map.h index 7d5340373..01af03789 100644 --- a/src/map/map.h +++ b/src/map/map.h @@ -1143,8 +1143,8 @@ struct chat_data { unsigned char users; // current users unsigned char limit; // join limit unsigned char trigger; // number of users needed to trigger event - struct map_session_data *usersd[20]; - struct block_list **owner; + struct map_session_data* usersd[20]; + struct block_list* owner; char npc_event[50]; }; diff --git a/src/map/npc.c b/src/map/npc.c index f991f14d1..2b3de118f 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -22,6 +22,7 @@ #include "skill.h" #include "unit.h" #include "npc.h" +#include "chat.h" #include #include @@ -1511,11 +1512,9 @@ int npc_unload(struct npc_data* nd) npc_remove_map(nd); map_deliddb(&nd->bl); - if (nd->chat_id) { - struct chat_data *cd = (struct chat_data*)map_id2bl(nd->chat_id); - if (cd) aFree (cd); - cd = NULL; - } + if (nd->chat_id) // remove npc chatroom object and kick users + chat_deletenpcchat(nd); + if (nd->bl.subtype == SCRIPT) { ev_db->foreach(ev_db,npc_unload_ev,nd->exname); //Clean up all events related. if (nd->u.scr.timerid != -1) { diff --git a/src/map/script.c b/src/map/script.c index d689463cf..472b4e545 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -8942,8 +8942,6 @@ BUILDIN_FUNC(waitingroom) title = script_getstr(st, 2); limit = script_getnum(st, 3); - if( limit == 0 ) - pub = 3; if( script_hasdata(st,5) ) { @@ -8957,15 +8955,16 @@ BUILDIN_FUNC(waitingroom) else {// ,"", ev = script_getstr(st, 4); - trigger=script_getnum(st,5); + trigger = script_getnum(st,5); } } else if( script_hasdata(st,4) ) {// ,"" ev = script_getstr(st, 4); + trigger = limit; } if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL ) - chat_createnpcchat(nd, limit, pub, trigger, title, ev); + chat_createnpcchat(nd, title, limit, pub, trigger, ev); return 0; } -- cgit v1.2.3-60-g2f50