From 1e77f5dc8d95bbf912205c85274d294a80ea65c9 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Sat, 23 Feb 2013 14:28:21 -0800 Subject: Replace struct dbt with typesafe std::map wrappers Also fix broken save/accreg.txt reading. --- src/char/int_party.cpp | 166 +++++++++++++++++++++-------------------------- src/char/int_party.hpp | 2 +- src/char/int_storage.cpp | 40 ++++-------- src/char/int_storage.hpp | 2 +- src/char/inter.cpp | 93 ++++++++++++-------------- 5 files changed, 129 insertions(+), 174 deletions(-) (limited to 'src/char') diff --git a/src/char/int_party.cpp b/src/char/int_party.cpp index 065398a..c64b208 100644 --- a/src/char/int_party.cpp +++ b/src/char/int_party.cpp @@ -17,7 +17,7 @@ char party_txt[1024] = "save/party.txt"; static -struct dbt *party_db; +Map party_db; static int party_newid = 100; @@ -26,7 +26,7 @@ int mapif_party_broken(int party_id, int flag); static int party_check_empty(struct party *p); static -int mapif_parse_PartyLeave(int fd, int party_id, int account_id); +void mapif_parse_PartyLeave(int fd, int party_id, int account_id); // パーティデータの文字列への変換 static @@ -94,13 +94,10 @@ int inter_party_fromstr(char *str, struct party *p) int inter_party_init(void) { char line[8192]; - struct party *p; FILE *fp; int c = 0; int i, j; - party_db = numdb_init(); - if ((fp = fopen_(party_txt, "r")) == NULL) return 1; @@ -115,19 +112,18 @@ int inter_party_init(void) continue; } - CREATE(p, struct party, 1); - if (inter_party_fromstr(line, p) == 0 && p->party_id > 0) + struct party p {}; + if (inter_party_fromstr(line, &p) == 0 && p.party_id > 0) { - if (p->party_id >= party_newid) - party_newid = p->party_id + 1; - numdb_insert(party_db, p->party_id, p); - party_check_empty(p); + if (p.party_id >= party_newid) + party_newid = p.party_id + 1; + party_db.insert(p.party_id, p); + party_check_empty(&p); } else { PRINTF("int_party: broken data [%s] line %d\n", party_txt, c + 1); - free(p); } c++; } @@ -139,9 +135,9 @@ int inter_party_init(void) // パーティーデータのセーブ用 static -void inter_party_save_sub(db_key_t, db_val_t data, FILE *fp) +void inter_party_save_sub(struct party *data, FILE *fp) { - std::string line = inter_party_tostr((struct party *) data); + std::string line = inter_party_tostr(data); FPRINTF(fp, "%s\n", line); } @@ -157,7 +153,8 @@ int inter_party_save(void) party_txt); return 1; } - numdb_foreach(party_db, std::bind(inter_party_save_sub, ph::_1, ph::_2, fp)); + for (auto& pair : party_db) + inter_party_save_sub(&pair.second, fp); // FPRINTF(fp, "%d\t%%newid%%\n", party_newid); lock_fclose(fp, party_txt, &lock); // PRINTF("int_party: %s saved.\n", party_txt); @@ -167,10 +164,8 @@ int inter_party_save(void) // パーティ名検索用 static -void search_partyname_sub(db_key_t, db_val_t data, const char *str, struct party **dst) +void search_partyname_sub(struct party *p, const char *str, struct party **dst) { - struct party *p = (struct party *) data; - if (strcasecmp(p->name, str) == 0) *dst = p; } @@ -180,7 +175,8 @@ static struct party *search_partyname(const char *str) { struct party *p = NULL; - numdb_foreach(party_db, std::bind(search_partyname_sub, ph::_1, ph::_2, str, &p)); + for (auto& pair : party_db) + search_partyname_sub(&pair.second, str, &p); return p; } @@ -223,18 +219,16 @@ int party_check_empty(struct party *p) } // 誰もいないので解散 mapif_party_broken(p->party_id, 0); - numdb_erase(party_db, p->party_id); - free(p); + party_db.erase(p->party_id); return 1; } // キャラの競合がないかチェック用 static -void party_check_conflict_sub(db_key_t, db_val_t data, +void party_check_conflict_sub(struct party *p, int party_id, int account_id, const char *nick) { - struct party *p = (struct party *) data; int i; if (p->party_id == party_id) // 本来の所属なので問題なし @@ -257,9 +251,9 @@ void party_check_conflict_sub(db_key_t, db_val_t data, static int party_check_conflict(int party_id, int account_id, const char *nick) { - numdb_foreach(party_db, - std::bind(party_check_conflict_sub, ph::_1, ph::_2, - party_id, account_id, nick)); + for (auto& pair : party_db) + party_check_conflict_sub(&pair.second, + party_id, account_id, nick); return 0; } @@ -428,7 +422,6 @@ static int mapif_parse_CreateParty(int fd, int account_id, const char *name, const char *nick, const char *map, int lv) { - struct party *p; int i; for (i = 0; i < 24 && name[i]; i++) @@ -441,28 +434,28 @@ int mapif_parse_CreateParty(int fd, int account_id, const char *name, const char } } - if ((p = search_partyname(name)) != NULL) + if (search_partyname(name) != NULL) { PRINTF("int_party: same name party exists [%s]\n", name); mapif_party_created(fd, account_id, NULL); return 0; } - CREATE(p, struct party, 1); - p->party_id = party_newid++; - memcpy(p->name, name, 24); - p->exp = 0; - p->item = 0; - p->member[0].account_id = account_id; - memcpy(p->member[0].name, nick, 24); - memcpy(p->member[0].map, map, 16); - p->member[0].leader = 1; - p->member[0].online = 1; - p->member[0].lv = lv; - - numdb_insert(party_db, p->party_id, p); - - mapif_party_created(fd, account_id, p); - mapif_party_info(fd, p); + struct party p {}; + p.party_id = party_newid++; + memcpy(p.name, name, 24); + p.exp = 0; + p.item = 0; + p.member[0].account_id = account_id; + memcpy(p.member[0].name, nick, 24); + memcpy(p.member[0].map, map, 16); + p.member[0].leader = 1; + p.member[0].online = 1; + p.member[0].lv = lv; + + party_db.insert(p.party_id, p); + + mapif_party_created(fd, account_id, &p); + mapif_party_info(fd, &p); return 0; } @@ -471,7 +464,7 @@ int mapif_parse_CreateParty(int fd, int account_id, const char *name, const char static int mapif_parse_PartyInfo(int fd, int party_id) { - struct party *p = (struct party *)numdb_search(party_db, party_id); + struct party *p = party_db.search(party_id); if (p != NULL) mapif_party_info(fd, p); else @@ -485,7 +478,7 @@ static int mapif_parse_PartyAddMember(int fd, int party_id, int account_id, const char *nick, const char *map, int lv) { - struct party *p = (struct party *)numdb_search(party_db, party_id); + struct party *p = party_db.search(party_id); if (p == NULL) { mapif_party_memberadded(fd, party_id, account_id, 1); @@ -527,7 +520,7 @@ static int mapif_parse_PartyChangeOption(int fd, int party_id, int account_id, int exp, int item) { - struct party *p = (struct party *)numdb_search(party_db, party_id); + struct party *p = party_db.search(party_id); if (p == NULL) return 0; @@ -546,74 +539,65 @@ int mapif_parse_PartyChangeOption(int fd, int party_id, int account_id, } // パーティ脱退要求 -int mapif_parse_PartyLeave(int, int party_id, int account_id) +void mapif_parse_PartyLeave(int, int party_id, int account_id) { - struct party *p = (struct party *)numdb_search(party_db, party_id); - if (p != NULL) + struct party *p = party_db.search(party_id); + if (!p) + return; + for (int i = 0; i < MAX_PARTY; i++) { - for (int i = 0; i < MAX_PARTY; i++) - { - if (p->member[i].account_id == account_id) - { - mapif_party_leaved(party_id, account_id, p->member[i].name); + if (p->member[i].account_id != account_id) + continue; + mapif_party_leaved(party_id, account_id, p->member[i].name); - memset(&p->member[i], 0, sizeof(struct party_member)); - if (party_check_empty(p) == 0) - mapif_party_info(-1, p); // まだ人がいるのでデータ送信 - return 0; - } - } + memset(&p->member[i], 0, sizeof(struct party_member)); + if (party_check_empty(p) == 0) + mapif_party_info(-1, p); // まだ人がいるのでデータ送信 + return; } - - return 0; } // パーティマップ更新要求 static -int mapif_parse_PartyChangeMap(int fd, int party_id, int account_id, +void mapif_parse_PartyChangeMap(int fd, int party_id, int account_id, const char *map, int online, int lv) { - struct party *p = (struct party *)numdb_search(party_db, party_id); + struct party *p = party_db.search(party_id); if (p == NULL) - return 0; + return; for (int i = 0; i < MAX_PARTY; i++) { - if (p->member[i].account_id == account_id) - { - int flag = 0; + if (p->member[i].account_id != account_id) + continue; + int flag = 0; - memcpy(p->member[i].map, map, 16); - p->member[i].online = online; - p->member[i].lv = lv; - mapif_party_membermoved(p, i); + memcpy(p->member[i].map, map, 16); + p->member[i].online = online; + p->member[i].lv = lv; + mapif_party_membermoved(p, i); - if (p->exp > 0 && !party_check_exp_share(p)) - { - p->exp = 0; - flag = 1; - } - if (flag) - mapif_party_optionchanged(fd, p, 0, 0); - break; + if (p->exp > 0 && !party_check_exp_share(p)) + { + p->exp = 0; + flag = 1; } + if (flag) + mapif_party_optionchanged(fd, p, 0, 0); + return; } - - return 0; } // パーティ解散要求 static -int mapif_parse_BreakParty(int fd, int party_id) +void mapif_parse_BreakParty(int fd, int party_id) { - struct party *p = (struct party *)numdb_search(party_db, party_id); + struct party *p = party_db.search(party_id); if (p == NULL) - return 0; + return; - numdb_erase(party_db, party_id); + party_db.erase(party_id); mapif_party_broken(fd, party_id); - - return 0; } // パーティメッセージ送信 @@ -684,7 +668,7 @@ int inter_party_parse_frommap(int fd) } // サーバーから脱退要求(キャラ削除用) -int inter_party_leave(int party_id, int account_id) +void inter_party_leave(int party_id, int account_id) { - return mapif_parse_PartyLeave(-1, party_id, account_id); + mapif_parse_PartyLeave(-1, party_id, account_id); } diff --git a/src/char/int_party.hpp b/src/char/int_party.hpp index eca5893..8a59b49 100644 --- a/src/char/int_party.hpp +++ b/src/char/int_party.hpp @@ -6,7 +6,7 @@ int inter_party_save(void); int inter_party_parse_frommap(int fd); -int inter_party_leave(int party_id, int account_id); +void inter_party_leave(int party_id, int account_id); extern char party_txt[1024]; diff --git a/src/char/int_storage.cpp b/src/char/int_storage.cpp index 9ca841f..b7feae1 100644 --- a/src/char/int_storage.cpp +++ b/src/char/int_storage.cpp @@ -20,7 +20,7 @@ char storage_txt[1024] = "save/storage.txt"; static -struct dbt *storage_db; +Map storage_db; // 倉庫データを文字列に変換 static @@ -83,14 +83,11 @@ bool extract(const_string str, struct storage *p) // アカウントから倉庫データインデックスを得る(新規倉庫追加可能) struct storage *account2storage(int account_id) { - struct storage *s; - s = (struct storage *) numdb_search(storage_db, account_id); + struct storage *s = storage_db.search(account_id); if (s == NULL) { - CREATE(s, struct storage, 1); - memset(s, 0, sizeof(struct storage)); + s = storage_db.init(account_id); s->account_id = account_id; - numdb_insert(storage_db, s->account_id, s); } return s; } @@ -101,8 +98,6 @@ int inter_storage_init(void) { int c = 0; - storage_db = numdb_init(); - std::ifstream in(storage_txt); if (!in.is_open()) { @@ -113,17 +108,15 @@ int inter_storage_init(void) std::string line; while (std::getline(in, line)) { - struct storage *s; - CREATE(s, struct storage, 1); - if (extract(line, s)) + struct storage s {}; + if (extract(line, &s)) { - numdb_insert(storage_db, s->account_id, s); + storage_db.insert(s.account_id, s); } else { PRINTF("int_storage: broken data [%s] line %d\n", storage_txt, c); - free(s); } c++; } @@ -132,9 +125,9 @@ int inter_storage_init(void) } static -void inter_storage_save_sub(db_key_t, db_val_t data, FILE *fp) +void inter_storage_save_sub(struct storage *data, FILE *fp) { - std::string line = storage_tostr((struct storage *) data); + std::string line = storage_tostr(data); if (!line.empty()) FPRINTF(fp, "%s\n", line); } @@ -146,32 +139,23 @@ int inter_storage_save(void) FILE *fp; int lock; - if (!storage_db) - return 1; - if ((fp = lock_fopen(storage_txt, &lock)) == NULL) { PRINTF("int_storage: cant write [%s] !!! data is lost !!!\n", storage_txt); return 1; } - numdb_foreach(storage_db, std::bind(inter_storage_save_sub, ph::_1, ph::_2, fp)); + for (auto& pair : storage_db) + inter_storage_save_sub(&pair.second, fp); lock_fclose(fp, storage_txt, &lock); // PRINTF("int_storage: %s saved.\n",storage_txt); return 0; } // 倉庫データ削除 -int inter_storage_delete(int account_id) +void inter_storage_delete(int account_id) { - struct storage *s = - (struct storage *) numdb_search(storage_db, account_id); - if (s) - { - numdb_erase(storage_db, account_id); - free(s); - } - return 0; + storage_db.erase(account_id); } //--------------------------------------------------------- diff --git a/src/char/int_storage.hpp b/src/char/int_storage.hpp index 286c5a6..691f16d 100644 --- a/src/char/int_storage.hpp +++ b/src/char/int_storage.hpp @@ -3,7 +3,7 @@ int inter_storage_init(void); int inter_storage_save(void); -int inter_storage_delete(int account_id); +void inter_storage_delete(int account_id); struct storage *account2storage(int account_id); int inter_storage_parse_frommap(int fd); diff --git a/src/char/inter.cpp b/src/char/inter.cpp index 3ca70e9..9efb28c 100644 --- a/src/char/inter.cpp +++ b/src/char/inter.cpp @@ -1,5 +1,6 @@ #include "inter.hpp" +#include #include #include @@ -30,14 +31,14 @@ char inter_log_filename[1024] = "log/inter.log"; static char accreg_txt[1024] = "save/accreg.txt"; -static -struct dbt *accreg_db = NULL; struct accreg { int account_id, reg_num; struct global_reg reg[ACCOUNT_REG_NUM]; }; +static +Map accreg_db; int party_share_level = 10; @@ -57,12 +58,13 @@ int inter_recv_packet_length[] = { struct WisData { - int id, fd, count, len; + int id, fd, count; tick_t tick; - unsigned char src[24], dst[24], msg[1024]; + unsigned char src[24], dst[24]; + std::string msg; }; static -struct dbt *wis_db = NULL; +Map wis_db; static int wis_dellist[WISDELLIST_MAX], wis_delnum; @@ -104,25 +106,21 @@ int inter_accreg_init(void) { int c = 0; - accreg_db = numdb_init(); - std::ifstream in(accreg_txt); if (!in.is_open()) return 1; std::string line; while (std::getline(in, line)) { - struct accreg *reg; - CREATE(reg, struct accreg, 1); - if (!extract(line, reg)) + struct accreg reg {}; + if (extract(line, ®)) { - numdb_insert(accreg_db, reg->account_id, reg); + accreg_db.insert(reg.account_id, reg); } else { PRINTF("inter: accreg: broken data [%s] line %d\n", accreg_txt, c); - free(reg); } c++; } @@ -132,10 +130,8 @@ int inter_accreg_init(void) // アカウント変数のセーブ用 static -void inter_accreg_save_sub(db_key_t, db_val_t data, FILE *fp) +void inter_accreg_save_sub(struct accreg *reg, FILE *fp) { - struct accreg *reg = (struct accreg *) data; - if (reg->reg_num > 0) { std::string line = inter_accreg_tostr(reg); @@ -157,7 +153,8 @@ int inter_accreg_save(void) accreg_txt); return 1; } - numdb_foreach(accreg_db, std::bind(inter_accreg_save_sub, ph::_1, ph::_2, fp)); + for (auto& pair : accreg_db) + inter_accreg_save_sub(&pair.second, fp); lock_fclose(fp, accreg_txt, &lock); return 0; @@ -236,8 +233,6 @@ int inter_init(const char *file) { inter_config_read(file); - wis_db = numdb_init(); - inter_party_init(); inter_storage_init(); inter_accreg_init(); @@ -264,14 +259,14 @@ void mapif_GMmessage(const uint8_t *mes, int len) static int mapif_wis_message(struct WisData *wd) { - unsigned char buf[56 + wd->len]; + unsigned char buf[56 + wd->msg.size()]; WBUFW(buf, 0) = 0x3801; - WBUFW(buf, 2) = 56 + wd->len; + WBUFW(buf, 2) = 56 + wd->msg.size(); WBUFL(buf, 4) = wd->id; memcpy(WBUFP(buf, 8), wd->src, 24); memcpy(WBUFP(buf, 32), wd->dst, 24); - memcpy(WBUFP(buf, 56), wd->msg, wd->len); + memcpy(WBUFP(buf, 56), wd->msg.data(), wd->msg.size()); wd->count = mapif_sendall(buf, WBUFW(buf, 2)); return 0; @@ -308,7 +303,7 @@ int mapif_account_reg(int fd, const uint8_t *src) static int mapif_account_reg_reply(int fd, int account_id) { - struct accreg *reg = (struct accreg *)numdb_search(accreg_db, account_id); + struct accreg *reg = accreg_db.search(account_id); WFIFOW(fd, 0) = 0x3804; WFIFOL(fd, 4) = account_id; @@ -335,10 +330,8 @@ int mapif_account_reg_reply(int fd, int account_id) // Existence check of WISP data static -void check_ttl_wisdata_sub(db_key_t, db_val_t data, tick_t tick) +void check_ttl_wisdata_sub(struct WisData *wd, tick_t tick) { - struct WisData *wd = (struct WisData *) data; - if (tick > wd->tick + WISDATA_TTL && wis_delnum < WISDELLIST_MAX) wis_dellist[wis_delnum++] = wd->id; @@ -353,16 +346,17 @@ int check_ttl_wisdata(void) do { wis_delnum = 0; - numdb_foreach(wis_db, std::bind(check_ttl_wisdata_sub, ph::_1, ph::_2, tick)); + for (auto& pair : wis_db) + check_ttl_wisdata_sub(&pair.second, tick); for (i = 0; i < wis_delnum; i++) { - struct WisData *wd = (struct WisData *)numdb_search(wis_db, wis_dellist[i]); + struct WisData *wd = wis_db.search(wis_dellist[i]); + assert (wd); PRINTF("inter: wis data id=%d time out : from %s to %s\n", wd->id, wd->src, wd->dst); // removed. not send information after a timeout. Just no answer for the player //mapif_wis_end(wd, 1); // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target - numdb_erase(wis_db, wd->id); - free(wd); + wis_db.erase(wd->id); } } while (wis_delnum >= WISDELLIST_MAX); @@ -386,16 +380,10 @@ int mapif_parse_GMmessage(int fd) static int mapif_parse_WisRequest(int fd) { - struct WisData *wd; static int wisid = 0; int index; - if (RFIFOW(fd, 2) - 52 >= sizeof(wd->msg)) - { - PRINTF("inter: Wis message size too long.\n"); - return 0; - } - else if (RFIFOW(fd, 2) - 52 <= 0) + if (RFIFOW(fd, 2) - 52 <= 0) { // normaly, impossible, but who knows... PRINTF("inter: Wis message doesn't exist.\n"); return 0; @@ -426,20 +414,20 @@ int mapif_parse_WisRequest(int fd) } else { - CREATE(wd, struct WisData, 1); + struct WisData wd; // Whether the failure of previous wisp/page transmission (timeout) check_ttl_wisdata(); - wd->id = ++wisid; - wd->fd = fd; - wd->len = RFIFOW(fd, 2) - 52; - memcpy(wd->src, RFIFOP(fd, 4), 24); - memcpy(wd->dst, RFIFOP(fd, 28), 24); - memcpy(wd->msg, RFIFOP(fd, 52), wd->len); - wd->tick = gettick(); - numdb_insert(wis_db, wd->id, wd); - mapif_wis_message(wd); + wd.id = ++wisid; + wd.fd = fd; + size_t len = RFIFOW(fd, 2) - 52; + memcpy(wd.src, RFIFOP(fd, 4), 24); + memcpy(wd.dst, RFIFOP(fd, 28), 24); + wd.msg = std::string(static_cast(RFIFOP(fd, 52)), len); + wd.tick = gettick(); + wis_db.insert(wd.id, wd); + mapif_wis_message(&wd); } } @@ -451,7 +439,7 @@ static int mapif_parse_WisReply(int fd) { int id = RFIFOL(fd, 2), flag = RFIFOB(fd, 6); - struct WisData *wd = (struct WisData *)numdb_search(wis_db, id); + struct WisData *wd = wis_db.search(id); if (wd == NULL) return 0; // This wisp was probably suppress before, because it was timeout of because of target was found on another map-server @@ -459,8 +447,7 @@ int mapif_parse_WisReply(int fd) if ((--wd->count) <= 0 || flag != 1) { mapif_wis_end(wd, flag); // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target - numdb_erase(wis_db, id); - free(wd); + wis_db.erase(id); } return 0; @@ -484,13 +471,13 @@ static int mapif_parse_AccReg(int fd) { int j, p; - struct accreg *reg = (struct accreg*)numdb_search(accreg_db, (numdb_key_t)RFIFOL(fd, 4)); + struct accreg *reg = accreg_db.search(RFIFOL(fd, 4)); if (reg == NULL) { - CREATE(reg, struct accreg, 1); - reg->account_id = RFIFOL(fd, 4); - numdb_insert(accreg_db, (numdb_key_t)RFIFOL(fd, 4), reg); + int account_id = RFIFOL(fd, 4); + reg = accreg_db.init(account_id); + reg->account_id = account_id; } for (j = 0, p = 8; j < ACCOUNT_REG_NUM && p < RFIFOW(fd, 2); -- cgit v1.2.3-60-g2f50