From ad049a15b43b7ddba3fe7d0a898652fc8022629d Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Tue, 22 Apr 2014 11:46:23 -0700 Subject: Use strict ID types Possibly some missing for the far side of the network. AccountId and BlockId are still terribly entangled. --- Makefile.in | 2 +- src/admin/fwd.hpp | 26 +++ src/admin/ladmin.cpp | 127 +++++----- src/char/char.cpp | 307 +++++++++++++----------- src/char/char.hpp | 4 +- src/char/fwd.hpp | 26 +++ src/char/int_party.cpp | 131 ++++++----- src/char/int_party.hpp | 6 +- src/char/int_storage.cpp | 23 +- src/char/int_storage.hpp | 8 +- src/char/inter.cpp | 5 +- src/compat/alg.hpp | 2 +- src/compat/fwd.hpp | 26 +++ src/generic/enum.hpp | 2 +- src/generic/fwd.hpp | 26 +++ src/ints/cmp.hpp | 2 +- src/ints/fwd.hpp | 26 +++ src/ints/udl.hpp | 12 +- src/ints/udl_test.cpp | 7 +- src/ints/wrap.cpp | 21 ++ src/ints/wrap.hpp | 109 +++++++++ src/io/cxxstdio.hpp | 14 +- src/io/write.hpp | 2 +- src/login/fwd.hpp | 26 +++ src/login/login.cpp | 294 +++++++++++------------ src/map/atcommand.cpp | 261 ++++++++++----------- src/map/atcommand.hpp | 4 +- src/map/battle.cpp | 48 ++-- src/map/battle.hpp | 6 +- src/map/battle.t.hpp | 2 +- src/map/chrif.cpp | 103 ++++----- src/map/chrif.hpp | 10 +- src/map/clif.cpp | 332 +++++++++++++------------- src/map/clif.hpp | 28 +-- src/map/fwd.hpp | 27 +++ src/map/intif.cpp | 94 ++++---- src/map/intif.hpp | 18 +- src/map/itemdb.cpp | 27 +-- src/map/itemdb.hpp | 25 +- src/map/magic-expr.cpp | 16 +- src/map/magic-interpreter-base.cpp | 14 +- src/map/magic-interpreter.hpp | 14 +- src/map/magic-stmt.cpp | 70 +++--- src/map/magic-v2.cpp | 8 +- src/map/magic.hpp | 6 +- src/map/map.cpp | 112 +++++---- src/map/map.hpp | 81 ++++--- src/map/map.t.hpp | 9 +- src/map/mob.cpp | 462 +++++++++++++++++++------------------ src/map/mob.hpp | 48 ++-- src/map/npc.cpp | 89 +++---- src/map/npc.hpp | 20 +- src/map/party.cpp | 78 +++---- src/map/party.hpp | 30 +-- src/map/pc.cpp | 215 ++++++++--------- src/map/pc.hpp | 24 +- src/map/script.cpp | 149 ++++++------ src/map/script.hpp | 8 +- src/map/skill.cpp | 17 +- src/map/skill.hpp | 4 +- src/map/storage.cpp | 20 +- src/map/storage.hpp | 10 +- src/map/tmw.cpp | 4 +- src/map/trade.cpp | 36 +-- src/map/trade.hpp | 4 +- src/mmo/extract.cpp | 25 +- src/mmo/extract.hpp | 45 ++-- src/mmo/extract_test.cpp | 2 + src/mmo/fwd.hpp | 37 +++ src/mmo/ids.cpp | 21 ++ src/mmo/ids.hpp | 109 +++++++++ src/mmo/mmo.hpp | 32 +-- src/mmo/socket.cpp | 3 + src/mmo/socket.hpp | 3 +- src/mmo/utils.hpp | 2 +- src/mmo/version.hpp | 2 +- src/range/fwd.hpp | 26 +++ src/sexpr/fwd.hpp | 26 +++ src/sexpr/lexer.hpp | 2 +- src/sexpr/parser.hpp | 2 +- src/strings/astring.hpp | 2 +- src/strings/literal.hpp | 2 +- 82 files changed, 2340 insertions(+), 1768 deletions(-) create mode 100644 src/admin/fwd.hpp create mode 100644 src/char/fwd.hpp create mode 100644 src/compat/fwd.hpp create mode 100644 src/generic/fwd.hpp create mode 100644 src/ints/fwd.hpp create mode 100644 src/ints/wrap.cpp create mode 100644 src/ints/wrap.hpp create mode 100644 src/login/fwd.hpp create mode 100644 src/map/fwd.hpp create mode 100644 src/mmo/fwd.hpp create mode 100644 src/mmo/ids.cpp create mode 100644 src/mmo/ids.hpp create mode 100644 src/range/fwd.hpp create mode 100644 src/sexpr/fwd.hpp diff --git a/Makefile.in b/Makefile.in index a532bfe..7ac2cbb 100644 --- a/Makefile.in +++ b/Makefile.in @@ -424,7 +424,7 @@ obj/%.hpp.formatted: src/%.hpp tools/indenter $(MKDIR_FIRST) apply-filter 'indenter -cpp' $< fgrep -q Copyright $< - fgrep -q ../sanity.hpp $< + if [[ $< == *fwd* ]]; then fgrep -q ../sanity.hpp $<; else fgrep -q '"fwd.hpp"' $<; fi touch $@ obj/%.tcc.formatted: src/%.tcc tools/indenter $(MKDIR_FIRST) diff --git a/src/admin/fwd.hpp b/src/admin/fwd.hpp new file mode 100644 index 0000000..f987625 --- /dev/null +++ b/src/admin/fwd.hpp @@ -0,0 +1,26 @@ +#ifndef TMWA_ADMIN_FWD_HPP +#define TMWA_ADMIN_FWD_HPP +// admin/fwd.hpp - list of type names for admin client +// +// Copyright © 2014 Ben Longbons +// +// This file is part of The Mana World (Athena server) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +# include "../sanity.hpp" + +// meh, add more when I feel like it + +#endif // TMWA_ADMIN_FWD_HPP diff --git a/src/admin/ladmin.cpp b/src/admin/ladmin.cpp index 307e13b..a4c2464 100644 --- a/src/admin/ladmin.cpp +++ b/src/admin/ladmin.cpp @@ -259,7 +259,8 @@ static TString parameters; // needs to be global since it's passed to the parse function // really should be added to session data static -int list_first, list_last, list_type, list_count; // parameter to display a list of accounts +AccountId list_first, list_last; +int list_type, list_count; // parameter to display a list of accounts static int already_exit_function = 0; // sometimes, the exit function is called twice... so, don't log twice the message @@ -1174,19 +1175,12 @@ void idaccount(ZString param) // Sub-function: Asking to displaying information about an account (by its id) //---------------------------------------------------------------------------- static -void infoaccount(int account_id) +void infoaccount(AccountId account_id) { - if (account_id < 0) - { - PRINTF("Please input a positive value for the id.\n"_fmt); - LADMIN_LOG("Negative value was given to found the account.\n"_fmt); - return; - } - LADMIN_LOG("Request to login-server to obtain information about an account (by its id).\n"_fmt); WFIFOW(login_session, 0) = 0x7954; - WFIFOL(login_session, 2) = account_id; + WFIFOL(login_session, 2) = unwrap(account_id); WFIFOSET(login_session, 6); bytes_to_read = 1; } @@ -1227,8 +1221,8 @@ void listaccount(ZString param, int type) list_type = type; // set default values - list_first = 0; - list_last = 0; + list_first = AccountId(); + list_last = AccountId(); if (list_type == 1) { // if listgm @@ -1250,18 +1244,16 @@ void listaccount(ZString param, int type) { // if list (list_type == 0) extract(param, record<' '>(&list_first, &list_last)); - if (list_first < 0) - list_first = 0; - if (list_last < list_first || list_last < 0) - list_last = 0; + if (list_last < list_first) + list_last = AccountId(); } LADMIN_LOG("Request to login-server to obtain the list of accounts from %d to %d.\n"_fmt, list_first, list_last); WFIFOW(login_session, 0) = 0x7920; - WFIFOL(login_session, 2) = list_first; - WFIFOL(login_session, 6) = list_last; + WFIFOL(login_session, 2) = unwrap(list_first); + WFIFOL(login_session, 6) = unwrap(list_last); WFIFOSET(login_session, 10); bytes_to_read = 1; @@ -1277,7 +1269,7 @@ void listaccount(ZString param, int type) static int itemfrob(ZString param) { - int source_id, dest_id; + ItemNameId source_id, dest_id; if (!extract(param, record<' '>(&source_id, &dest_id))) { @@ -1286,8 +1278,8 @@ int itemfrob(ZString param) } WFIFOW(login_session, 0) = 0x7924; - WFIFOL(login_session, 2) = source_id; - WFIFOL(login_session, 6) = dest_id; + WFIFOL(login_session, 2) = unwrap(source_id); + WFIFOL(login_session, 6) = unwrap(dest_id); WFIFOSET(login_session, 10); bytes_to_read = 1; // all logging is done to the three main servers @@ -1339,19 +1331,12 @@ void changememo(ZString param) // Sub-function: Asking to obtain an account name //----------------------------------------------- static -void nameaccount(int id) +void nameaccount(AccountId id) { - if (id < 0) - { - PRINTF("Please input a positive value for the id.\n"_fmt); - LADMIN_LOG("Negativ id given to search an account name ('name' command).\n"_fmt); - return; - } - LADMIN_LOG("Request to login-server to know an account name.\n"_fmt); WFIFOW(login_session, 0) = 0x7946; - WFIFOL(login_session, 2) = id; + WFIFOL(login_session, 2) = unwrap(id); WFIFOSET(login_session, 6); bytes_to_read = 1; } @@ -1892,7 +1877,7 @@ void prompt(void) else if (command == "id"_s) idaccount(parameters); else if (command == "info"_s) - infoaccount(atoi(parameters.c_str())); + infoaccount(wrap(static_cast(atoi(parameters.c_str())))); else if (command == "kami"_s) sendbroadcast(parameters); // flag for normal else if (command == "itemfrob"_s) @@ -1908,7 +1893,7 @@ void prompt(void) else if (command == "memo"_s) changememo(parameters); else if (command == "name"_s) - nameaccount(atoi(parameters.c_str())); + nameaccount(wrap(static_cast(atoi(parameters.c_str())))); else if (command == "password"_s) changepasswd(parameters); else if (command == "reloadgm"_s) @@ -2031,7 +2016,8 @@ void parse_fromlogin(Session *s) { AccountName userid = stringish(RFIFO_STRING<24>(s, i + 5)); VString<23> lower_userid = userid.to_lower(); - list_first = RFIFOL(s, i) + 1; + // what? + list_first = next(wrap(RFIFOL(s, i))); // here are checks... if (list_type == 0 || (list_type == 1 && RFIFOB(s, i + 4) > 0) @@ -2098,8 +2084,8 @@ void parse_fromlogin(Session *s) LADMIN_LOG("Request to login-server to obtain the list of accounts from %d to %d (complement).\n"_fmt, list_first, list_last); WFIFOW(login_session, 0) = 0x7920; - WFIFOL(login_session, 2) = list_first; - WFIFOL(login_session, 6) = list_last; + WFIFOL(login_session, 2) = unwrap(list_first); + WFIFOL(login_session, 6) = unwrap(list_last); WFIFOSET(login_session, 10); bytes_to_read = 1; } @@ -2110,9 +2096,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 30) return; { - int accid = RFIFOL(s, 2); + AccountId accid = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (accid == -1) + if (!accid) { PRINTF("Account [%s] creation failed. Same account already exists.\n"_fmt, name); @@ -2135,9 +2121,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 30) return; { - int accid = RFIFOL(s, 2); + AccountId accid = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (accid == -1) + if (!accid) { PRINTF("Account [%s] deletion failed. Account doesn't exist.\n"_fmt, name); @@ -2160,9 +2146,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 30) return; { - int accid = RFIFOL(s, 2); + AccountId accid = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (accid == -1) + if (!accid) { PRINTF("Account [%s] password changing failed.\n"_fmt, name); @@ -2187,10 +2173,10 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 34) return; { - int accid = RFIFOL(s, 2); + AccountId accid = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); int state = RFIFOL(s, 30); - if (accid == -1) + if (!accid) { PRINTF("Account [%s] state changing failed. Account doesn't exist.\n"_fmt, name); @@ -2280,9 +2266,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 30) return; { - int account_id = RFIFOL(s, 2); + AccountId account_id = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (account_id == -1) + if (!account_id) { PRINTF("The account [%s] doesn't exist or the password is incorrect.\n"_fmt, name); @@ -2305,9 +2291,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 30) return; { - int account_id = RFIFOL(s, 2); + AccountId account_id = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (account_id == -1) + if (!account_id) { PRINTF("Account [%s] sex changing failed.\n"_fmt, name); @@ -2332,9 +2318,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 30) return; { - int account_id = RFIFOL(s, 2); + AccountId account_id = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (account_id == -1) + if (!account_id) { PRINTF("Account [%s] GM level changing failed.\n"_fmt, name); @@ -2360,9 +2346,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 30) return; { - int account_id = RFIFOL(s, 2); + AccountId account_id = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (account_id == -1) + if (!account_id) { PRINTF("Account [%s] e-mail changing failed.\n"_fmt, name); @@ -2387,9 +2373,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 30) return; { - int account_id = RFIFOL(s, 2); + AccountId account_id = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (account_id == -1) + if (!account_id) { PRINTF("Account [%s] memo changing failed. Account doesn't exist.\n"_fmt, name); @@ -2412,9 +2398,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 30) return; { - int account_id = RFIFOL(s, 2); + AccountId account_id = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (account_id == -1) + if (!account_id) { PRINTF("Unable to find the account [%s] id. Account doesn't exist.\n"_fmt, name); @@ -2437,7 +2423,7 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 30) return; { - int account_id = RFIFOL(s, 2); + AccountId account_id = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); if (!name) { @@ -2462,7 +2448,7 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 34) return; { - int account_id = RFIFOL(s, 2); + AccountId account_id = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); if (RFIFOL(s, 2) == -1) { @@ -2501,9 +2487,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 34) return; { - int account_id = RFIFOL(s, 2); + AccountId account_id = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (account_id == -1) + if (!account_id) { PRINTF("Account [%s] final date of banishment changing failed. Account doesn't exist.\n"_fmt, name); @@ -2540,9 +2526,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 34) return; { - int account_id = RFIFOL(s, 2); + AccountId account_id = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (account_id == -1) + if (!account_id) { PRINTF("Account [%s] final date of banishment changing failed. Account doesn't exist.\n"_fmt, name); @@ -2597,9 +2583,9 @@ void parse_fromlogin(Session *s) if (RFIFOREST(s) < 34) return; { - int account_id = RFIFOL(s, 2); + AccountId account_id = wrap(RFIFOL(s, 2)); AccountName name = stringish(RFIFO_STRING<24>(s, 6)); - if (account_id == -1) + if (!account_id) { PRINTF("Account [%s] validity limit changing failed. Account doesn't exist.\n"_fmt, name); @@ -2640,10 +2626,11 @@ void parse_fromlogin(Session *s) || RFIFOREST(s) < (150 + RFIFOW(s, 148))) return; { - int account_id = RFIFOL(s, 2); - uint8_t gm = RFIFOB(s, 6); + AccountId account_id = wrap(RFIFOL(s, 2)); + // TODO fix size (there's a lot of other stuff wrong with this packet too + GmLevel gm = GmLevel::from(static_cast(RFIFOB(s, 6))); AccountName userid = stringish(RFIFO_STRING<24>(s, 7)); - uint8_t sex = RFIFOB(s, 31); + SEX sex = static_cast(RFIFOB(s, 31)); int connections = RFIFOL(s, 32); int state = RFIFOL(s, 36); timestamp_seconds_buffer error_message = stringish(RFIFO_STRING<20>(s, 40)); @@ -2653,7 +2640,7 @@ void parse_fromlogin(Session *s) TimeT connect_until_time = static_cast(RFIFOL(s, 140)); TimeT ban_until_time = static_cast(RFIFOL(s, 144)); AString memo = RFIFO_STRING(s, 150, RFIFOW(s, 148)); - if (account_id == -1) + if (!account_id) { PRINTF("Unabled to find the account [%s]. Account doesn't exist.\n"_fmt, userid); @@ -2681,11 +2668,11 @@ void parse_fromlogin(Session *s) account_id, gm); } PRINTF(" Name: '%s'\n"_fmt, userid); - if (sex == 0) + if (sex == SEX::FEMALE) PRINTF(" Sex: Female\n"_fmt); - else if (sex == 1) + else if (sex == SEX::MALE) PRINTF(" Sex: Male\n"_fmt); - else + else // doesn't happen anymore PRINTF(" Sex: Server\n"_fmt); PRINTF(" E-mail: %s\n"_fmt, email); switch (state) diff --git a/src/char/char.cpp b/src/char/char.cpp index 87e48cb..f714dda 100644 --- a/src/char/char.cpp +++ b/src/char/char.cpp @@ -42,6 +42,7 @@ #include "../compat/alg.hpp" #include "../ints/cmp.hpp" +#include "../ints/udl.hpp" #include "../strings/mstring.hpp" #include "../strings/astring.hpp" @@ -117,10 +118,14 @@ static int char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor] static std::bitset<256> char_name_letters; // list of letters/symbols authorised (or not) in a character name. by [Yor] +static constexpr +GmLevel default_gm_level = GmLevel::from(0_u32); + struct char_session_data : SessionData { - int account_id, login_id1, login_id2; + AccountId account_id; + int login_id1, login_id2; SEX sex; unsigned short packet_tmw_version; AccountEmail email; @@ -134,8 +139,8 @@ void SessionDeleter::operator()(SessionData *sd) struct AuthFifoEntry { - int account_id; - int char_id; + AccountId account_id; + CharId char_id; int login_id1, login_id2; IP4Address ip; int delflag; @@ -152,7 +157,7 @@ static int check_ip_flag = 1; // It's to check IP of a player between char-server and other servers (part of anti-hacking system) static -int char_id_count = 150000; +CharId char_id_count = wrap(150000); static std::vector char_keys; static @@ -177,7 +182,7 @@ int online_sorting_option = 0; // sorting option to display online players in on static int online_refresh_html = 20; // refresh time (in sec) of the html file in the explorer static -int online_gm_display_min_level = 20; // minimum GM level to display 'GM' when we want to display it +GmLevel online_gm_display_min_level = GmLevel::from(20_u32); // minimum GM level to display 'GM' when we want to display it static std::vector online_chars; // same size of char_keys, and id value of current server (or -1) @@ -235,12 +240,12 @@ void char_log(XString line) // and returns its level (or 0 if it isn't a GM account or if not found) //---------------------------------------------------------------------- static -int isGM(int account_id) +GmLevel isGM(AccountId account_id) { for (GM_Account& gma : gm_accounts) if (gma.account_id == account_id) return gma.level; - return 0; + return default_gm_level; } //---------------------------------------------- @@ -266,7 +271,7 @@ const CharPair *search_character(CharName character_name) return nullptr; } -const CharPair *search_character_id(int char_id) +const CharPair *search_character_id(CharId char_id) { for (const CharPair& cd : char_keys) { @@ -342,7 +347,7 @@ AString mmo_char_tostr(struct CharPair *cp) if (p->inventory[i].nameid) { str_p += STRPRINTF("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d "_fmt, - p->inventory[i].id, + 0 /*id*/, p->inventory[i].nameid, p->inventory[i].amount, p->inventory[i].equip, @@ -450,7 +455,7 @@ bool extract(XString str, CharPair *cp) return false; // TODO replace *every* lookup with a map lookup - static std::set seen_ids; + static std::set seen_ids; static std::set seen_names; // we don't have to worry about deleted characters, // this is only called during startup @@ -518,8 +523,8 @@ int mmo_char_init(void) continue; { - int i, j = 0; - if (SSCANF(line, "%d\t%%newid%%%n"_fmt, &i, &j) == 1 && j > 0) + CharId i; + if (extract(line, record<'\t'>(&i, "%newid%"_s))) { if (char_id_count < i) char_id_count = i; @@ -533,8 +538,8 @@ int mmo_char_init(void) CHAR_LOG("Char skipped\n%s"_fmt, line); continue; } - if (cd.key.char_id >= char_id_count) - char_id_count = cd.key.char_id + 1; + if (char_id_count < next(cd.key.char_id)) + char_id_count = next(cd.key.char_id); char_keys.push_back(std::move(cd)); online_chars.push_back(nullptr); } @@ -740,11 +745,11 @@ CharPair *make_new_char(Session *s, CharName name, const uint8_t (&stats)[6], ui CharKey& ck = cp.key; CharData& cd = *cp.data; - ck.char_id = char_id_count++; + ck.char_id = char_id_count; char_id_count = next(char_id_count); ck.account_id = sd->account_id; ck.char_num = slot; ck.name = name; - cd.species = 0; + cd.species = Species(); cd.base_level = 1; cd.job_level = 1; cd.base_exp = 0; @@ -765,17 +770,17 @@ CharPair *make_new_char(Session *s, CharName name, const uint8_t (&stats)[6], ui cd.option = static_cast