summaryrefslogtreecommitdiff
path: root/src/char
diff options
context:
space:
mode:
authorgepard1984 <gepard1984@54d463be-8e91-2dee-dedb-b68131a5f0ec>2012-03-13 14:46:28 +0000
committergepard1984 <gepard1984@54d463be-8e91-2dee-dedb-b68131a5f0ec>2012-03-13 14:46:28 +0000
commitdc5babf8c3fb943a22c440f5afd6957b121926c5 (patch)
treefcf7000486b4172803ff2929c0b746013d0fb6a9 /src/char
parentd0932858a07e7df3159003c0b920c50e2e6a1b76 (diff)
downloadhercules-dc5babf8c3fb943a22c440f5afd6957b121926c5.tar.gz
hercules-dc5babf8c3fb943a22c440f5afd6957b121926c5.tar.bz2
hercules-dc5babf8c3fb943a22c440f5afd6957b121926c5.tar.xz
hercules-dc5babf8c3fb943a22c440f5afd6957b121926c5.zip
Enhanced `DBMap` implementation to allow storing integer type data in addition to void pointers.
- Added enum for data: `int`, `unsigned int` and `void*` - Replaced generic `void*` data with `DBData` struct to hold `int`, `unsigned int` or `void*` (member of `DBNode`) - Added `db_i2data`, `db_ui2data` and `db_ptr2data` functions to cast from `int`/`uint`/`void*` to `DBData` (used in `DBMap::put()`) - Added `db_data2i`, `db_data2ui` and `db_data2ptr` functions to get `int`/`uint`/`void*` from `DBData` - Enabled statistics for new functions in `db_stats` struct - `DBCreateData` functions (used in `DBMap::ensure()`) now return `DBData` instead of `void*` - `DBApply` functions (used in `DBMap::foreach()` and `DBMap::destroy()`) now take `DBData*` as a parameter instead of `void*` - `DBMatcher` functions (used in `DBMap::getall()`) now take `DBData` as a parameter instead of `void*` - `DBReleaser` functions now take `DBData` as parameter instead of `void*` - Default releasers release data if it is `void*` (`DB_DATA_PTR`) type - `DBIterator` functions: `first()`, `last()`, `next()` and `prev()` now return `DBData*` instead of `void*` - `DBIterator::remove()` now returns `int` (1 if node was found and removed, 0 otherwise) instead of `void*` and takes an extra `DBData*` parameter for the data of removed entry - `DBMap::get()` and `DBMap::ensure()` now return `DBData*` instead of `void*` - `DBMap::remove()` and `DBMap::put()` now return `int` (1 if node already existed, 0 otherwise) instead of `void*` and take an extra `DBData*` parameter for the data of removed entry - `DBMap::put()` now takes `DBData` as parameter instead of `void*` - `DBMap::getall()` now puts data into `DBData**` buffer instead of `void**` buffer - Updated macros: - (`i`/`ui`/`str`)`db_get` and (`i`/`ui`/`str`)`db_ensure` were wrapped with `db_data2ptr` to extract data from `DBData*` that is now returned by `DBMap::get()` - added (`i`/`ui`/`str`)`db_iget` and (`i`/`ui`/`str`)`db_uiget` that get `DBData` from `DBMap` and extract `int`/`unsigned int` from it (with `db_data2i`/`db_data2ui`) - `db_put`, `idb_put`, `uidb_put` and `strdb_put` data params were wrapped with `db_ptr2data` to match new signature of `DBMap::put()` (`DBData` instead of `void*`) - added (`i`/`ui`/`str`)`db_iput` and (`i`/`ui`/`str`)`db_uiput` that put `int`/`unsigned int` into `DBMap` (first wrapping it with `DBData`) - added `NULL` in place of extra parameter for removed data in `db_remove` macros - `dbi_first`, `dbi_last`, `dbi_next` and `dbi_prev` were wrapped with `db_data2ptr` to extract data from `DBData*` that is now returned by these `DBIterator` functions - Updated `DBMap` documentation, and fixed a dozen of typos in it. - Updated rest of rA code to reflect `DBMap` changes (mostly required signature changes of `DBCreateData` and `DBApply` functions). - Fixed a bug where `DBMap::put()` would return data of a deleted entry. - Removed some unnecessary pointer casts. - Increased `showmsg.c` static buffer size to fit entire DBMap stats report. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@15682 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/char')
-rw-r--r--src/char/char.c58
-rw-r--r--src/char/int_guild.c9
-rw-r--r--src/char/inter.c9
3 files changed, 50 insertions, 26 deletions
diff --git a/src/char/char.c b/src/char/char.c
index fb413be82..dccadc9fe 100644
--- a/src/char/char.c
+++ b/src/char/char.c
@@ -195,7 +195,10 @@ static DBMap* online_char_db; // int account_id -> struct online_char_data*
static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data);
int delete_char_sql(int char_id);
-static void* create_online_char_data(DBKey key, va_list args)
+/**
+ * @see DBCreateData
+ */
+static DBData create_online_char_data(DBKey key, va_list args)
{
struct online_char_data* character;
CREATE(character, struct online_char_data, 1);
@@ -204,14 +207,14 @@ static void* create_online_char_data(DBKey key, va_list args)
character->server = -1;
character->fd = -1;
character->waiting_disconnect = INVALID_TIMER;
- return character;
+ return db_ptr2data(character);
}
void set_char_charselect(int account_id)
{
struct online_char_data* character;
- character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
+ character = idb_ensure(online_char_db, account_id, create_online_char_data);
if( character->server > -1 )
if( server[character->server].users > 0 ) // Prevent this value from going negative.
@@ -245,7 +248,7 @@ void set_char_online(int map_id, int char_id, int account_id)
Sql_ShowDebug(sql_handle);
//Check to see for online conflicts
- character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
+ character = idb_ensure(online_char_db, account_id, create_online_char_data);
if( character->char_id != -1 && character->server > -1 && character->server != map_id )
{
ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
@@ -330,9 +333,12 @@ void set_char_offline(int char_id, int account_id)
}
}
-static int char_db_setoffline(DBKey key, void* data, va_list ap)
+/**
+ * @see DBApply
+ */
+static int char_db_setoffline(DBKey key, DBData *data, va_list ap)
{
- struct online_char_data* character = (struct online_char_data*)data;
+ struct online_char_data* character = db_data2ptr(data);
int server = va_arg(ap, int);
if (server == -1) {
character->char_id = -1;
@@ -346,15 +352,18 @@ static int char_db_setoffline(DBKey key, void* data, va_list ap)
return 0;
}
-static int char_db_kickoffline(DBKey key, void* data, va_list ap)
+/**
+ * @see DBApply
+ */
+static int char_db_kickoffline(DBKey key, DBData *data, va_list ap)
{
- struct online_char_data* character = (struct online_char_data*)data;
+ struct online_char_data* character = db_data2ptr(data);
int server_id = va_arg(ap, int);
if (server_id > -1 && character->server != server_id)
return 0;
- //Kick out any connected characters, and set them offline as appropiate.
+ //Kick out any connected characters, and set them offline as appropriate.
if (character->server > -1)
mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 1);
else if (character->waiting_disconnect == INVALID_TIMER)
@@ -392,12 +401,15 @@ void set_all_offline_sql(void)
Sql_ShowDebug(sql_handle);
}
-static void* create_charstatus(DBKey key, va_list args)
+/**
+ * @see DBCreateData
+ */
+static DBData create_charstatus(DBKey key, va_list args)
{
struct mmo_charstatus *cp;
cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
cp->char_id = key.i;
- return cp;
+ return db_ptr2data(cp);
}
@@ -413,7 +425,7 @@ int mmo_char_tosql(int char_id, struct mmo_charstatus* p)
if (char_id!=p->char_id) return 0;
- cp = (struct mmo_charstatus*)idb_ensure(char_db_, char_id, create_charstatus);
+ cp = idb_ensure(char_db_, char_id, create_charstatus);
StringBuf_Init(&buf);
memset(save_status, 0, sizeof(save_status));
@@ -1184,7 +1196,7 @@ int mmo_char_fromsql(int char_id, struct mmo_charstatus* p, bool load_everything
SqlStmt_Free(stmt);
StringBuf_Destroy(&buf);
- cp = (struct mmo_charstatus*)idb_ensure(char_db_, char_id, create_charstatus);
+ cp = idb_ensure(char_db_, char_id, create_charstatus);
memcpy(cp, p, sizeof(struct mmo_charstatus));
return 1;
}
@@ -2609,7 +2621,7 @@ int parse_frommap(int fd)
for(i = 0; i < server[id].users; i++) {
aid = RFIFOL(fd,6+i*8);
cid = RFIFOL(fd,6+i*8+4);
- character = (struct online_char_data*)idb_ensure(online_char_db, aid, create_online_char_data);
+ character = idb_ensure(online_char_db, aid, create_online_char_data);
if( character->server > -1 && character->server != id )
{
ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n",
@@ -2753,7 +2765,7 @@ int parse_frommap(int fd)
node->changing_mapservers = 1;
idb_put(auth_db, RFIFOL(fd,2), node);
- data = (struct online_char_data*)idb_ensure(online_char_db, RFIFOL(fd,2), create_online_char_data);
+ data = idb_ensure(online_char_db, RFIFOL(fd,2), create_online_char_data);
data->char_id = char_data->char_id;
data->server = map_id; //Update server where char is.
@@ -4060,10 +4072,13 @@ int broadcast_user_count(int tid, unsigned int tick, int id, intptr_t data)
return 0;
}
-/// load this char's account id into the 'online accounts' packet
-static int send_accounts_tologin_sub(DBKey key, void* data, va_list ap)
+/**
+ * Load this character's account id into the 'online accounts' packet
+ * @see DBApply
+ */
+static int send_accounts_tologin_sub(DBKey key, DBData *data, va_list ap)
{
- struct online_char_data* character = (struct online_char_data*)data;
+ struct online_char_data* character = db_data2ptr(data);
int* i = va_arg(ap, int*);
if(character->server > -1)
@@ -4152,9 +4167,12 @@ static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_
return 0;
}
-static int online_data_cleanup_sub(DBKey key, void *data, va_list ap)
+/**
+ * @see DBApply
+ */
+static int online_data_cleanup_sub(DBKey key, DBData *data, va_list ap)
{
- struct online_char_data *character= (struct online_char_data*)data;
+ struct online_char_data *character= db_data2ptr(data);
if (character->fd != -1)
return 0; //Character still connected
if (character->server == -2) //Unknown server.. set them offline
diff --git a/src/char/int_guild.c b/src/char/int_guild.c
index d2e74ee1d..a4d91ecd6 100644
--- a/src/char/int_guild.c
+++ b/src/char/int_guild.c
@@ -56,7 +56,7 @@ static int guild_save_timer(int tid, unsigned int tick, int id, intptr_t data)
if( last_id == 0 ) //Save the first guild in the list.
state = 1;
- for( g = (struct guild*)iter->first(iter,&key); iter->exists(iter); g = (struct guild*)iter->next(iter,&key) )
+ for( g = db_data2ptr(iter->first(iter, &key)); dbi_exists(iter); g = db_data2ptr(iter->next(iter, &key)) )
{
if( state == 0 && g->guild_id == last_id )
state++; //Save next guild in the list.
@@ -746,9 +746,12 @@ int inter_guild_sql_init(void)
return 0;
}
-static int guild_db_final(DBKey key, void *data, va_list ap)
+/**
+ * @see DBApply
+ */
+static int guild_db_final(DBKey key, DBData *data, va_list ap)
{
- struct guild *g = (struct guild*)data;
+ struct guild *g = db_data2ptr(data);
if (g->save_flag&GS_MASK) {
inter_guild_tosql(g, g->save_flag&GS_MASK);
return 1;
diff --git a/src/char/inter.c b/src/char/inter.c
index dbff1993f..c72c64108 100644
--- a/src/char/inter.c
+++ b/src/char/inter.c
@@ -421,11 +421,14 @@ int mapif_disconnectplayer(int fd, int account_id, int char_id, int reason)
//--------------------------------------------------------
-// Existence check of WISP data
-int check_ttl_wisdata_sub(DBKey key, void *data, va_list ap)
+/**
+ * Existence check of WISP data
+ * @see DBApply
+ */
+int check_ttl_wisdata_sub(DBKey key, DBData *data, va_list ap)
{
unsigned long tick;
- struct WisData *wd = (struct WisData *)data;
+ struct WisData *wd = db_data2ptr(data);
tick = va_arg(ap, unsigned long);
if (DIFF_TICK(tick, wd->tick) > WISDATA_TTL && wis_delnum < WISDELLIST_MAX)