#include "char.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include "../strings/mstring.hpp" #include "../strings/fstring.hpp" #include "../strings/zstring.hpp" #include "../strings/xstring.hpp" #include "../common/core.hpp" #include "../common/cxxstdio.hpp" #include "../common/db.hpp" #include "../common/extract.hpp" #include "../common/human_time_diff.hpp" #include "../common/io.hpp" #include "../common/lock.hpp" #include "../common/socket.hpp" #include "../common/timer.hpp" #include "../common/version.hpp" #include "inter.hpp" #include "int_party.hpp" #include "int_storage.hpp" #include "../poison.hpp" static struct mmo_map_server server[MAX_MAP_SERVERS]; static int server_fd[MAX_MAP_SERVERS]; static int server_freezeflag[MAX_MAP_SERVERS]; // Map-server anti-freeze system. Counter. 5 ok, 4...0 freezed static int anti_freeze_enable = 0; static std::chrono::seconds anti_freeze_interval = std::chrono::seconds(6); constexpr std::chrono::milliseconds DEFAULT_AUTOSAVE_INTERVAL = std::chrono::minutes(5); // TODO replace all string forms of IP addresses with class instances static int login_fd, char_fd; static AccountName userid; static AccountPass passwd; static ServerName server_name; static CharName wisp_server_name = stringish("Server"); static IP4Address login_ip; static int login_port = 6900; static IP4Address char_ip; static int char_port = 6121; static FString char_txt; static CharName unknown_char_name = stringish("Unknown"); static FString char_log_filename = "log/char.log"; //Added for lan support static IP4Address lan_map_ip; static IP4Mask lan_subnet; 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] struct char_session_data : SessionData { int account_id, login_id1, login_id2, sex; unsigned short packet_tmw_version; AccountEmail email; TimeT connect_until_time; }; void SessionDeleter::operator()(SessionData *sd) { really_delete1 static_cast(sd); } struct AuthFifoEntry { int account_id; int char_id; int login_id1, login_id2; IP4Address ip; int delflag; int sex; unsigned short packet_tmw_version; TimeT connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited) }; static std::array auth_fifo; static auto auth_fifo_iter = auth_fifo.begin(); 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; static std::vector char_data; static int max_connect_user = 0; static std::chrono::milliseconds autosave_time = DEFAULT_AUTOSAVE_INTERVAL; // Initial position (it's possible to set it in conf file) static struct point start_point = { {"001-1.gat"}, 273, 354 }; static std::vector gm_accounts; // online players by [Yor] static FString online_txt_filename = "online.txt"; static FString online_html_filename = "online.html"; static int online_sorting_option = 0; // sorting option to display online players in online files 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 static std::vector online_chars; // same size of char_data, and id value of current server (or -1) static TimeT update_online; // to update online files when we receiving information from a server (not less than 8 seconds) static pid_t pid = 0; // For forked DB writes //------------------------------ // Writing function of logs file //------------------------------ void char_log(XString line) { FILE *logfp = fopen(char_log_filename.c_str(), "a"); if (!logfp) return; log_with_timestamp(logfp, line); fclose(logfp); } //---------------------------------------------------------------------- // Determine if an account (id) is a GM account // and returns its level (or 0 if it isn't a GM account or if not found) //---------------------------------------------------------------------- static int isGM(int account_id) { for (GM_Account& gma : gm_accounts) if (gma.account_id == account_id) return gma.level; return 0; } //---------------------------------------------- // Search an character id // (return character pointer or nullptr (if not found)) // If exact character name is not found, // the function checks without case sensitive // and returns index if only 1 character is found // and similar to the searched name. //---------------------------------------------- const mmo_charstatus *search_character(CharName character_name) { for (const mmo_charstatus& cd : char_data) { { // Strict comparison (if found, we finish the function immediatly with correct value) if (cd.name == character_name) return &cd; } } // Exact character name is not found and 0 or more than 1 similar characters have been found ==> we say not found return nullptr; } //------------------------------------------------- // Function to create the character line (for save) //------------------------------------------------- static FString mmo_char_tostr(struct mmo_charstatus *p) { // on multi-map server, sometimes it's posssible that last_point become void. (reason???) We check that to not lost character at restart. if (!p->last_point.map_) { p->last_point = start_point; } MString str_p; str_p += STRPRINTF( "%d\t" "%d,%d\t" "%s\t" "%d,%d,%d\t" "%d,%d,%d\t" "%d,%d,%d,%d\t" "%d,%d,%d,%d,%d,%d\t" "%d,%d\t" "%d,%d,%d\t" "%d,%d,%d\t" "%d,%d,%d\t" "%d,%d,%d,%d,%d\t" "%s,%d,%d\t" "%s,%d,%d,%d\t", p->char_id, p->account_id, p->char_num, p->name, p->species, p->base_level, p->job_level, p->base_exp, p->job_exp, p->zeny, p->hp, p->max_hp, p->sp, p->max_sp, p->attrs[ATTR::STR], p->attrs[ATTR::AGI], p->attrs[ATTR::VIT], p->attrs[ATTR::INT], p->attrs[ATTR::DEX], p->attrs[ATTR::LUK], p->status_point, p->skill_point, p->option, p->karma, p->manner, p->party_id, 0/*guild_id*/, 0/*pet_id*/, p->hair, p->hair_color, p->clothes_color, p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom, p->last_point.map_, p->last_point.x, p->last_point.y, p->save_point.map_, p->save_point.x, p->save_point.y, p->partner_id); for (int i = 0; i < 10; i++) if (p->memo_point[i].map_) { str_p += STRPRINTF("%s,%d,%d ", p->memo_point[i].map_, p->memo_point[i].x, p->memo_point[i].y); } str_p += '\t'; for (int i = 0; i < MAX_INVENTORY; i++) if (p->inventory[i].nameid) { str_p += STRPRINTF("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d ", p->inventory[i].id, p->inventory[i].nameid, p->inventory[i].amount, p->inventory[i].equip, p->inventory[i].identify, p->inventory[i].refine, p->inventory[i].attribute, p->inventory[i].card[0], p->inventory[i].card[1], p->inventory[i].card[2], p->inventory[i].card[3], p->inventory[i].broken); } str_p += '\t'; for (int i = 0; i < MAX_CART; i++) if (p->cart[i].nameid) { str_p += STRPRINTF("%d,%d,%d,%hhu,%d,%hd,%hhu,%d,%d,%d,%d,%d ", p->cart[i].id, p->cart[i].nameid, p->cart[i].amount, p->cart[i].equip, p->cart[i].identify, p->cart[i].refine, p->cart[i].attribute, p->cart[i].card[0], p->cart[i].card[1], p->cart[i].card[2], p->cart[i].card[3], p->cart[i].broken); } str_p += '\t'; for (SkillID i : erange(SkillID(), MAX_SKILL)) if (p->skill[i].lv) { str_p += STRPRINTF("%d,%d ", i, p->skill[i].lv | (uint16_t(p->skill[i].flags) << 16)); } str_p += '\t'; for (int i = 0; i < p->global_reg_num; i++) if (p->global_reg[i].str) str_p += STRPRINTF("%s,%d ", p->global_reg[i].str, p->global_reg[i].value); str_p += '\t'; return FString(str_p); } static bool extract(XString str, struct point *p) { return extract(str, record<','>(&p->map_, &p->x, &p->y)); } struct skill_loader { SkillID id; uint16_t level; SkillFlags flags; }; static bool extract(XString str, struct skill_loader *s) { uint32_t flags_and_level; if (!extract(str, record<','>(&s->id, &flags_and_level))) return false; s->level = flags_and_level & 0xffff; s->flags = SkillFlags(flags_and_level >> 16); return true; } //------------------------------------------------------------------------- // Function to set the character from the line (at read of characters file) //------------------------------------------------------------------------- static bool extract(XString str, struct mmo_charstatus *p) { // initilialise character *p = mmo_charstatus{}; uint32_t unused_guild_id, unused_pet_id; std::vector memos; std::vector inventory, cart; std::vector skills; std::vector vars; if (!extract(str, record<'\t'>( &p->char_id, record<','>(&p->account_id, &p->char_num), &p->name, record<','>(&p->species, &p->base_level, &p->job_level), record<','>(&p->base_exp, &p->job_exp, &p->zeny), record<','>(&p->hp, &p->max_hp, &p->sp, &p->max_sp), record<','>(&p->attrs[ATTR::STR], &p->attrs[ATTR::AGI], &p->attrs[ATTR::VIT], &p->attrs[ATTR::INT], &p->attrs[ATTR::DEX], &p->attrs[ATTR::LUK]), record<','>(&p->status_point, &p->skill_point), record<','>(&p->option, &p->karma, &p->manner), record<','>(&p->party_id, &unused_guild_id, &unused_pet_id), record<','>(&p->hair, &p->hair_color, &p->clothes_color), record<','>(&p->weapon, &p->shield, &p->head_top, &p->head_mid, &p->head_bottom), &p->last_point, // somebody was silly and stuck partner id as a field // of this, instead of adding a new \t // or putting it elsewhere, like by pet/guild record<','>(&p->save_point.map_, &p->save_point.x, &p->save_point.y, &p->partner_id), vrec<' '>(&memos), vrec<' '>(&inventory), vrec<' '>(&cart), vrec<' '>(&skills), vrec<' '>(&vars)))) return false; if (wisp_server_name == p->name) return false; // TODO replace *every* lookup with a map lookup 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 auto _seen_id = seen_ids.insert(p->char_id); if (!_seen_id.second) return false; auto _seen_name = seen_names.insert(p->name); if (!_seen_name.second) { seen_ids.erase(_seen_id.first); return false; } if (memos.size() > 10) return false; std::copy(memos.begin(), memos.end(), p->memo_point); // number of memo points is not saved - it just detects map name '\0' if (inventory.size() > MAX_INVENTORY) return false; std::copy(inventory.begin(), inventory.end(), p->inventory); // number of inventory items is not saved - it just detects nameid 0 if (cart.size() > MAX_CART) return false; std::copy(cart.begin(), cart.end(), p->cart); // number of cart items is not saved - it just detects nameid 0 for (struct skill_loader& sk : skills) { if (sk.id > MAX_SKILL) return false; p->skill[sk.id].lv = sk.level; p->skill[sk.id].flags = sk.flags; } if (vars.size() > GLOBAL_REG_NUM) return false; std::copy(vars.begin(), vars.end(), p->global_reg); p->global_reg_num = vars.size(); return true; } //--------------------------------- // Function to read characters file //--------------------------------- static int mmo_char_init(void) { char_data.clear(); online_chars.clear(); std::ifstream in(char_txt.c_str()); if (!in.is_open()) { PRINTF("Characters file not found: %s.\n", char_txt); CHAR_LOG("Characters file not found: %s.\n", char_txt); CHAR_LOG("Id for the next created character: %d.\n", char_id_count); return 0; } int line_count = 0; FString line; while (io::getline(in, line)) { line_count++; if (line.startswith("//")) continue; { int i, j = 0; if (SSCANF(line, "%d\t%%newid%%%n", &i, &j) == 1 && j > 0) { if (char_id_count < i) char_id_count = i; continue; } } mmo_charstatus cd {}; if (!extract(line, &cd)) { CHAR_LOG("Char skipped\n%s", line); continue; } if (cd.char_id >= char_id_count) char_id_count = cd.char_id + 1; char_data.push_back(std::move(cd)); online_chars.push_back(-1); } PRINTF("mmo_char_init: %zu characters read in %s.\n", char_data.size(), char_txt); CHAR_LOG("mmo_char_init: %zu characters read in %s.\n", char_data.size(), char_txt); CHAR_LOG("Id for the next created character: %d.\n", char_id_count); return 0; } //--------------------------------------------------------- // Function to save characters in files (speed up by [Yor]) //--------------------------------------------------------- static void mmo_char_sync(void) { int lock; FILE *fp = lock_fopen(char_txt, &lock); if (fp == NULL) { PRINTF("WARNING: Server can't not save characters.\n"); CHAR_LOG("WARNING: Server can't not save characters.\n"); return; } { // yes, we need a mutable reference to do the saves ... for (mmo_charstatus& cd : char_data) { FString line = mmo_char_tostr(&cd); fwrite(line.data(), 1, line.size(), fp); fputc('\n', fp); } FPRINTF(fp, "%d\t%%newid%%\n", char_id_count); lock_fclose(fp, char_txt, &lock); } } //---------------------------------------------------- // Function to save (in a periodic way) datas in files //---------------------------------------------------- static void mmo_char_sync_timer(TimerData *, tick_t) { if (pid != 0) { int status; pid_t temp = waitpid(pid, &status, WNOHANG); // Need to check status too? if (temp == 0) { return; } } // This can take a lot of time. Fork a child to handle the work and return at once // If we're unable to fork just continue running the function normally if ((pid = fork()) > 0) return; mmo_char_sync(); inter_save(); // If we're a child we should suicide now. if (pid == 0) _exit(0); } //----------------------------------- // Function to create a new character //----------------------------------- static mmo_charstatus *make_new_char(int fd, CharName name, const uint8_t (&stats)[6], uint8_t slot, uint16_t hair_color, uint16_t hair_style) { // ugh char_session_data *sd = static_cast(session[fd]->session_data.get()); // remove control characters from the name if (!name.to__actual().is_print()) { CHAR_LOG("Make new char error (control char received in the name): (connection #%d, account: %d).\n", fd, sd->account_id); return nullptr; } // Eliminate whitespace if (name.to__actual() != name.to__actual().strip()) { CHAR_LOG("Make new char error (leading/trailing whitespace): (connection #%d, account: %d, name: '%s'.\n", fd, sd->account_id, name); return nullptr; } // check lenght of character name if (name.to__actual().size() < 4) { CHAR_LOG("Make new char error (character name too small): (connection #%d, account: %d, name: '%s').\n", fd, sd->account_id, name); return nullptr; } // Check Authorised letters/symbols in the name of the character if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised for (uint8_t c : name.to__actual()) if (!char_name_letters[c]) { CHAR_LOG("Make new char error (invalid letter in the name): (connection #%d, account: %d), name: %s, invalid letter: %c.\n", fd, sd->account_id, name, c); return nullptr; } } else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden for (uint8_t c : name.to__actual()) if (char_name_letters[c]) { CHAR_LOG("Make new char error (invalid letter in the name): (connection #%d, account: %d), name: %s, invalid letter: %c.\n", fd, sd->account_id, name, c); return nullptr; } } // else, all letters/symbols are authorised (except control char removed before) // this is why it needs to be unsigned if (stats[0] + stats[1] + stats[2] + stats[3] + stats[4] + stats[5] != 5 * 6 || // stats slot >= 9 || hair_style >= 20 || hair_color >= 12) { CHAR_LOG("Make new char error (invalid values): (connection #%d, account: %d) slot %d, name: %s, stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d\n", fd, sd->account_id, slot, name, stats[0], stats[1], stats[2], stats[3], stats[4], stats[5], stats[0] + stats[1] + stats[2] + stats[3] + stats[4] + stats[5], hair_style, hair_color); return nullptr; } // check individual stat value for (int i = 0; i < 6; i++) { if (stats[i] < 1 || stats[i] > 9) { CHAR_LOG("Make new char error (invalid stat value: not between 1 to 9): (connection #%d, account: %d) slot %d, name: %s, stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d\n", fd, sd->account_id, slot, name, stats[0], stats[1], stats[2], stats[3], stats[4], stats[5], stats[0] + stats[1] + stats[2] + stats[3] + stats[4] + stats[5], hair_style, hair_color); return nullptr; } } for (const mmo_charstatus& cd : char_data) { if (cd.name == name) { CHAR_LOG("Make new char error (name already exists): (connection #%d, account: %d) slot %d, name: %s (actual name of other char: %s), stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d.\n", fd, sd->account_id, slot, name, cd.name, stats[0], stats[1], stats[2], stats[3], stats[4], stats[5], stats[0] + stats[1] + stats[2] + stats[3] + stats[4] + stats[5], hair_style, hair_color); return nullptr; } if (cd.account_id == sd->account_id && cd.char_num == slot) { CHAR_LOG("Make new char error (slot already used): (connection #%d, account: %d) slot %d, name: %s (actual name of other char: %s), stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d.\n", fd, sd->account_id, slot, name, cd.name, stats[0], stats[1], stats[2], stats[3], stats[4], stats[5], stats[0] + stats[1] + stats[2] + stats[3] + stats[4] + stats[5], hair_style, hair_color); return nullptr; } } if (wisp_server_name == name) { CHAR_LOG("Make new char error (name used is wisp name for server): (connection #%d, account: %d) slot %d, name: %s (actual name whisper server: %s), stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d.\n", fd, sd->account_id, slot, name, wisp_server_name, stats[0], stats[1], stats[2], stats[3], stats[4], stats[5], stats[0] + stats[1] + stats[2] + stats[3] + stats[4] + stats[5], hair_style, hair_color); return nullptr; } IP4Address ip = session[fd]->client_ip; CHAR_LOG("Creation of New Character: (connection #%d, account: %d) slot %d, character Name: %s, stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d. [%s]\n", fd, sd->account_id, slot, name, stats[0], stats[1], stats[2], stats[3], stats[4], stats[5], stats[0] + stats[1] + stats[2] + stats[3] + stats[4] + stats[5], hair_style, hair_color, ip); mmo_charstatus cd {}; cd.char_id = char_id_count++; cd.account_id = sd->account_id; cd.char_num = slot; cd.name = name; cd.species = 0; cd.base_level = 1; cd.job_level = 1; cd.base_exp = 0; cd.job_exp = 0; cd.zeny = 0; cd.attrs[ATTR::STR] = stats[0]; cd.attrs[ATTR::AGI] = stats[1]; cd.attrs[ATTR::VIT] = stats[2]; cd.attrs[ATTR::INT] = stats[3]; cd.attrs[ATTR::DEX] = stats[4]; cd.attrs[ATTR::LUK] = stats[5]; cd.max_hp = 40 * (100 + cd.attrs[ATTR::VIT]) / 100; cd.max_sp = 11 * (100 + cd.attrs[ATTR::INT]) / 100; cd.hp = cd.max_hp; cd.sp = cd.max_sp; cd.status_point = 0; cd.skill_point = 0; cd.option = static_cast