From 5a9d6c05dba6aa00590b475f848964cd888c0a93 Mon Sep 17 00:00:00 2001 From: Haru Date: Tue, 15 Sep 2015 12:39:47 +0200 Subject: Replaced HPM->plugins with a VECTOR Signed-off-by: Haru --- src/common/HPM.c | 146 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 91 insertions(+), 55 deletions(-) (limited to 'src/common/HPM.c') diff --git a/src/common/HPM.c b/src/common/HPM.c index 0316f9494..645784b01 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -43,11 +43,18 @@ DBMap *datacheck_db; int datacheck_version; const struct s_HPMDataCheck *datacheck_data; -void hplugin_trigger_event(enum hp_event_types type) { - unsigned int i; - for( i = 0; i < HPM->plugin_count; i++ ) { - if( HPM->plugins[i]->hpi->event[type] != NULL ) - (HPM->plugins[i]->hpi->event[type])(); +/** + * Executes an event on all loaded plugins. + * + * @param type The event type to trigger. + */ +void hplugin_trigger_event(enum hp_event_types type) +{ + int i; + for (i = 0; i < VECTOR_LENGTH(HPM->plugins); i++) { + struct hplugin *plugin = VECTOR_INDEX(HPM->plugins, i); + if (plugin->hpi->event[type] != NULL) + plugin->hpi->event[type](); } } @@ -81,20 +88,37 @@ bool hplugin_iscompatible(char* version) { return ( req_major == HPM->version[0] && req_minor <= HPM->version[1] ) ? true : false; } -bool hplugin_exists(const char *filename) { - unsigned int i; - for(i = 0; i < HPM->plugin_count; i++) { - if( strcmpi(HPM->plugins[i]->filename,filename) == 0 ) +/** + * Checks whether a plugin is currently loaded + * + * @param filename The plugin filename. + * @retval true if the plugin exists and is currently loaded. + * @retval false otherwise. + */ +bool hplugin_exists(const char *filename) +{ + int i; + for (i = 0; i < VECTOR_LENGTH(HPM->plugins); i++) { + if (strcmpi(VECTOR_INDEX(HPM->plugins, i)->filename,filename) == 0) return true; } return false; } -struct hplugin *hplugin_create(void) { - RECREATE(HPM->plugins, struct hplugin *, ++HPM->plugin_count); - CREATE(HPM->plugins[HPM->plugin_count - 1], struct hplugin, 1); - HPM->plugins[HPM->plugin_count - 1]->idx = HPM->plugin_count - 1; - HPM->plugins[HPM->plugin_count - 1]->filename = NULL; - return HPM->plugins[HPM->plugin_count - 1]; + +/** + * Initializes the data structure for a new plugin and registers it. + * + * @return A (retained) pointer to the initialized data. + */ +struct hplugin *hplugin_create(void) +{ + struct hplugin *plugin = NULL; + CREATE(plugin, struct hplugin, 1); + plugin->idx = (int)VECTOR_LENGTH(HPM->plugins); + plugin->filename = NULL; + VECTOR_ENSURE(HPM->plugins, 1, 1); + VECTOR_PUSH(HPM->plugins, plugin); + return plugin; } bool hplugins_addpacket(unsigned short cmd, unsigned short length, void (*receive) (int fd), unsigned int point,unsigned int pluginID) { @@ -477,30 +501,23 @@ struct hplugin *hplugin_load(const char* filename) { return plugin; } +/** + * Unloads and unregisters a plugin. + * + * @param plugin The plugin data. + */ void hplugin_unload(struct hplugin* plugin) { + int i; if (plugin->filename) aFree(plugin->filename); if (plugin->dll) plugin_close(plugin->dll); /* TODO: for manual packet unload */ /* - Go through known packets and unlink any belonging to the plugin being removed */ - if (!HPM->off) { - unsigned int i, cursor; - for (cursor = 0; cursor < HPM->plugin_count; cursor++) { - if (HPM->plugins[cursor]->idx != plugin->idx) - continue; - HPM->plugins[cursor] = NULL; - break; - } - for(i = cursor + 1; i < HPM->plugin_count; i++) { - HPM->plugins[cursor] = HPM->plugins[i]; - cursor++; - } - if (!(HPM->plugin_count = cursor)) { - aFree(HPM->plugins); - HPM->plugins = NULL; - } + ARR_FIND(0, VECTOR_LENGTH(HPM->plugins), i, VECTOR_INDEX(HPM->plugins, i)->idx == plugin->idx); + if (i != VECTOR_LENGTH(HPM->plugins)) { + VECTOR_ERASE(HPM->plugins, i); } aFree(plugin); } @@ -584,20 +601,31 @@ void hplugins_config_read(void) { } libconfig->destroy(&plugins_conf); - if( HPM->plugin_count ) - ShowStatus("HPM: There are '"CL_WHITE"%d"CL_RESET"' plugins loaded, type '"CL_WHITE"plugins"CL_RESET"' to list them\n", HPM->plugin_count); + if (VECTOR_LENGTH(HPM->plugins)) + ShowStatus("HPM: There are '"CL_WHITE"%"PRIuS""CL_RESET"' plugins loaded, type '"CL_WHITE"plugins"CL_RESET"' to list them\n", VECTOR_LENGTH(HPM->plugins)); } -CPCMD(plugins) { - if( HPM->plugin_count == 0 ) { + +/** + * Console command: plugins + * + * Shows a list of loaded plugins. + * + * @see CPCMD() + */ +CPCMD(plugins) +{ + int i; + + if (VECTOR_LENGTH(HPM->plugins) == 0) { ShowInfo("HPC: there are no plugins loaded\n"); - } else { - unsigned int i; + return; + } - ShowInfo("HPC: There are '"CL_WHITE"%d"CL_RESET"' plugins loaded\n",HPM->plugin_count); + ShowInfo("HPC: There are '"CL_WHITE"%"PRIuS""CL_RESET"' plugins loaded\n", VECTOR_LENGTH(HPM->plugins)); - for(i = 0; i < HPM->plugin_count; i++) { - ShowInfo("HPC: - '"CL_WHITE"%s"CL_RESET"' (%s)\n",HPM->plugins[i]->info->name,HPM->plugins[i]->filename); - } + for(i = 0; i < VECTOR_LENGTH(HPM->plugins); i++) { + struct hplugin *plugin = VECTOR_INDEX(HPM->plugins, i); + ShowInfo("HPC: - '"CL_WHITE"%s"CL_RESET"' (%s)\n", plugin->info->name, plugin->filename); } } @@ -631,15 +659,25 @@ unsigned char hplugins_parse_packets(int fd, enum HPluginPacketHookingPoints poi return 0; } -char *hplugins_id2name (unsigned int pid) { - unsigned int i; +/** + * Retrieves a plugin name by ID. + * + * @param pid The plugin identifier. + * @return The plugin name. + * @retval "core" if the plugin ID belongs to the Hercules core. + * @retval "UnknownPlugin" if the plugin wasn't found. + */ +char *hplugins_id2name(unsigned int pid) +{ + int i; if (pid == HPM_PID_CORE) return "core"; - for( i = 0; i < HPM->plugin_count; i++ ) { - if( HPM->plugins[i]->idx == pid ) - return HPM->plugins[i]->info->name; + for (i = 0; i < VECTOR_LENGTH(HPM->plugins); i++) { + struct hplugin *plugin = VECTOR_INDEX(HPM->plugins, i); + if (plugin->idx == pid) + return plugin->info->name; } return "UnknownPlugin"; @@ -753,8 +791,8 @@ void hpm_init(void) { datacheck_version = 0; HPM->symbols = NULL; - HPM->plugins = NULL; - HPM->plugin_count = HPM->symbol_count = 0; + VECTOR_INIT(HPM->plugins); + HPM->symbol_count = 0; HPM->off = false; memcpy(&iMalloc_HPM, iMalloc, sizeof(struct malloc_interface)); @@ -794,18 +832,16 @@ void hpm_memdown(void) free(HPM->fnames); } } -void hpm_final(void) { +void hpm_final(void) +{ unsigned int i; HPM->off = true; - if( HPM->plugins ) - { - for( i = 0; i < HPM->plugin_count; i++ ) { - HPM->unload(HPM->plugins[i]); - } - aFree(HPM->plugins); + while (VECTOR_LENGTH(HPM->plugins)) { + HPM->unload(VECTOR_LAST(HPM->plugins)); } + VECTOR_CLEAR(HPM->plugins); if( HPM->symbols ) { -- cgit v1.2.3-70-g09d2 From f17add758aa067f3b643e008dc42ec918b358528 Mon Sep 17 00:00:00 2001 From: Haru Date: Tue, 15 Sep 2015 12:52:08 +0200 Subject: Changed HPM->symbols to a VECTOR Signed-off-by: Haru --- src/common/HPM.c | 52 +++++++++++++++++++++++++++++++++------------------- src/common/HPM.h | 12 +++++++----- 2 files changed, 40 insertions(+), 24 deletions(-) (limited to 'src/common/HPM.c') diff --git a/src/common/HPM.c b/src/common/HPM.c index 645784b01..12b879357 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -58,20 +58,37 @@ void hplugin_trigger_event(enum hp_event_types type) } } -void hplugin_export_symbol(void *var, char *name) { - RECREATE(HPM->symbols, struct hpm_symbol *, ++HPM->symbol_count); - CREATE(HPM->symbols[HPM->symbol_count - 1] ,struct hpm_symbol, 1); - HPM->symbols[HPM->symbol_count - 1]->name = name; - HPM->symbols[HPM->symbol_count - 1]->ptr = var; +/** + * Exports a symbol to the shared symbols list. + * + * @param value The symbol value. + * @param name The symbol name. + */ +void hplugin_export_symbol(void *value, const char *name) +{ + struct hpm_symbol *symbol = NULL; + CREATE(symbol ,struct hpm_symbol, 1); + symbol->name = name; + symbol->ptr = value; + VECTOR_ENSURE(HPM->symbols, 1, 1); + VECTOR_PUSH(HPM->symbols, symbol); } -void *hplugin_import_symbol(char *name, unsigned int pID) { - unsigned int i; +/** + * Imports a shared symbol. + * + * @param name The symbol name. + * @param pID The requesting plugin ID. + * @return The symbol value. + * @retval NULL if the symbol wasn't found. + */ +void *hplugin_import_symbol(char *name, unsigned int pID) +{ + int i; + ARR_FIND(0, VECTOR_LENGTH(HPM->symbols), i, strcmp(VECTOR_INDEX(HPM->symbols, i)->name, name) == 0); - for( i = 0; i < HPM->symbol_count; i++ ) { - if( strcmp(HPM->symbols[i]->name,name) == 0 ) - return HPM->symbols[i]->ptr; - } + if (i != VECTOR_LENGTH(HPM->symbols)) + return VECTOR_INDEX(HPM->symbols, i)->ptr; ShowError("HPM:get_symbol:%s: '"CL_WHITE"%s"CL_RESET"' not found!\n",HPM->pid2name(pID),name); return NULL; @@ -790,9 +807,9 @@ void hpm_init(void) { datacheck_data = NULL; datacheck_version = 0; - HPM->symbols = NULL; VECTOR_INIT(HPM->plugins); - HPM->symbol_count = 0; + VECTOR_INIT(HPM->symbols); + HPM->off = false; memcpy(&iMalloc_HPM, iMalloc, sizeof(struct malloc_interface)); @@ -843,13 +860,10 @@ void hpm_final(void) } VECTOR_CLEAR(HPM->plugins); - if( HPM->symbols ) - { - for( i = 0; i < HPM->symbol_count; i++ ) { - aFree(HPM->symbols[i]); - } - aFree(HPM->symbols); + while (VECTOR_LENGTH(HPM->symbols)) { + aFree(VECTOR_POP(HPM->symbols)); } + VECTOR_CLEAR(HPM->symbols); for( i = 0; i < hpPHP_MAX; i++ ) { if( HPM->packets[i] ) diff --git a/src/common/HPM.h b/src/common/HPM.h index 5579dc2b1..6d44cd474 100644 --- a/src/common/HPM.h +++ b/src/common/HPM.h @@ -57,9 +57,12 @@ struct hplugin { struct HPMi_interface *hpi; }; +/** + * Symbols shared between core and plugins. + */ struct hpm_symbol { - char *name; - void *ptr; + const char *name; ///< The symbol name + void *ptr; ///< The symbol value }; struct HPluginData { @@ -104,8 +107,7 @@ struct HPM_interface { bool force_return; /* data */ VECTOR_DECL(struct hplugin *) plugins; - struct hpm_symbol **symbols; - unsigned int symbol_count; + VECTOR_DECL(struct hpm_symbol *) symbols; /* packet hooking points */ struct HPluginPacket *packets[hpPHP_MAX]; unsigned int packetsc[hpPHP_MAX]; @@ -128,7 +130,7 @@ struct HPM_interface { bool (*iscompatible) (char* version); void (*event) (enum hp_event_types type); void *(*import_symbol) (char *name, unsigned int pID); - void (*share) (void *, char *); + void (*share) (void *value, const char *name); void (*config_read) (void); char *(*pid2name) (unsigned int pid); unsigned char (*parse_packets) (int fd, enum HPluginPacketHookingPoints point); -- cgit v1.2.3-70-g09d2 From 437dc70ac6a19fe9a32c095ada8205dce173beb3 Mon Sep 17 00:00:00 2001 From: Haru Date: Tue, 15 Sep 2015 13:23:34 +0200 Subject: Changed HPM->packets[] into an array of VECTOR Signed-off-by: Haru --- src/char/char.c | 32 +++++++++++---------- src/common/HPM.c | 84 +++++++++++++++++++++++++++---------------------------- src/common/HPM.h | 3 +- src/login/login.c | 25 +++++++++-------- src/map/chrif.c | 22 +++++++-------- src/map/clif.c | 12 ++++---- 6 files changed, 91 insertions(+), 87 deletions(-) (limited to 'src/common/HPM.c') diff --git a/src/char/char.c b/src/char/char.c index 3cc8853a4..1226a03cd 100644 --- a/src/char/char.c +++ b/src/char/char.c @@ -2626,10 +2626,12 @@ int char_parse_fromlogin(int fd) { while(RFIFOREST(fd) >= 2) { uint16 command = RFIFOW(fd,0); - if( HPM->packetsc[hpParse_FromLogin] ) { - int success = HPM->parse_packets(fd,hpParse_FromLogin); - if( success == 1 ) continue; - else if( success == 2 ) return 0; + if (VECTOR_LENGTH(HPM->packets[hpParse_FromLogin]) > 0) { + int result = HPM->parse_packets(fd,hpParse_FromLogin); + if (result == 1) + continue; + if (result == 2) + return 0; } switch (command) { @@ -3896,7 +3898,6 @@ void char_parse_frommap_scdata_delete(int fd) int char_parse_frommap(int fd) { - int i; int id; ARR_FIND( 0, ARRAYLENGTH(chr->server), id, chr->server[id].fd == fd ); @@ -3913,11 +3914,12 @@ int char_parse_frommap(int fd) } while(RFIFOREST(fd) >= 2) { - if( HPM->packetsc[hpParse_FromMap] ) { - if( (i = HPM->parse_packets(fd,hpParse_FromMap)) ) { - if( i == 1 ) continue; - if( i == 2 ) return 0; - } + if (VECTOR_LENGTH(HPM->packets[hpParse_FromMap]) > 0) { + int result = HPM->parse_packets(fd,hpParse_FromMap); + if (result == 1) + continue; + if (result == 2) + return 0; } switch(RFIFOW(fd,0)) { @@ -5095,10 +5097,12 @@ int char_parse_char(int fd) //For use in packets that depend on an sd being present [Skotlex] #define FIFOSD_CHECK(rest) do { if(RFIFOREST(fd) < (rest)) return 0; if (sd==NULL || !sd->auth) { RFIFOSKIP(fd,(rest)); return 0; } } while (0) - if( HPM->packetsc[hpParse_Char] ) { - int success = HPM->parse_packets(fd,hpParse_Char); - if( success == 1 ) continue; - else if( success == 2 ) return 0; + if (VECTOR_LENGTH(HPM->packets[hpParse_Char]) > 0) { + int result = HPM->parse_packets(fd,hpParse_Char); + if (result == 1) + continue; + if (result == 2) + return 0; } cmd = RFIFOW(fd,0); diff --git a/src/common/HPM.c b/src/common/HPM.c index 12b879357..0bbfbbb49 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -138,24 +138,27 @@ struct hplugin *hplugin_create(void) return plugin; } -bool hplugins_addpacket(unsigned short cmd, unsigned short length, void (*receive) (int fd), unsigned int point,unsigned int pluginID) { +bool hplugins_addpacket(unsigned short cmd, unsigned short length, void (*receive) (int fd), unsigned int point, unsigned int pluginID) +{ struct HPluginPacket *packet; - unsigned int i; + int i; - if( point >= hpPHP_MAX ) { + if (point >= hpPHP_MAX) { ShowError("HPM->addPacket:%s: unknown point '%u' specified for packet 0x%04x (len %d)\n",HPM->pid2name(pluginID),point,cmd,length); return false; } - for(i = 0; i < HPM->packetsc[point]; i++) { - if( HPM->packets[point][i].cmd == cmd ) { - ShowError("HPM->addPacket:%s: can't add packet 0x%04x, already in use by '%s'!",HPM->pid2name(pluginID),cmd,HPM->pid2name(HPM->packets[point][i].pluginID)); + for (i = 0; i < VECTOR_LENGTH(HPM->packets[point]); i++) { + if (VECTOR_INDEX(HPM->packets[point], i).cmd == cmd ) { + ShowError("HPM->addPacket:%s: can't add packet 0x%04x, already in use by '%s'!", + HPM->pid2name(pluginID), cmd, HPM->pid2name(VECTOR_INDEX(HPM->packets[point], i).pluginID)); return false; } } - RECREATE(HPM->packets[point], struct HPluginPacket, ++HPM->packetsc[point]); - packet = &HPM->packets[point][HPM->packetsc[point] - 1]; + VECTOR_ENSURE(HPM->packets[point], 1, 1); + VECTOR_PUSHZEROED(HPM->packets[point]); + packet = &VECTOR_LAST(HPM->packets[point]); packet->pluginID = pluginID; packet->cmd = cmd; @@ -646,34 +649,37 @@ CPCMD(plugins) } } -/* - 0 = unknown - 1 = OK - 2 = incomplete +/** + * Parses a packet through the registered plugin. + * + * @param fd The connection fd. + * @param point The packet hooking point. + * @retval 0 unknown packet + * @retval 1 OK + * @retval 2 incomplete packet */ -unsigned char hplugins_parse_packets(int fd, enum HPluginPacketHookingPoints point) { - unsigned int i; +unsigned char hplugins_parse_packets(int fd, enum HPluginPacketHookingPoints point) +{ + struct HPluginPacket *packet = NULL; + int i; + int16 length; - for(i = 0; i < HPM->packetsc[point]; i++) { - if( HPM->packets[point][i].cmd == RFIFOW(fd,0) ) - break; - } + ARR_FIND(0, VECTOR_LENGTH(HPM->packets[point]), i, VECTOR_INDEX(HPM->packets[point], i).cmd == RFIFOW(fd,0)); - if( i != HPM->packetsc[point] ) { - struct HPluginPacket *packet = &HPM->packets[point][i]; - short length; + if (i == VECTOR_LENGTH(HPM->packets[point])) + return 0; - if( (length = packet->len) == -1 ) { - if( (length = RFIFOW(fd, 2)) > (int)RFIFOREST(fd) ) - return 2; - } + packet = &VECTOR_INDEX(HPM->packets[point], i); + length = packet->len; + if (length == -1) + length = RFIFOW(fd, 2); - packet->receive(fd); - RFIFOSKIP(fd, length); - return 1; - } + if (length > (int)RFIFOREST(fd)) + return 2; - return 0; + packet->receive(fd); + RFIFOSKIP(fd, length); + return 1; } /** @@ -802,7 +808,7 @@ void HPM_datacheck_final(void) { } void hpm_init(void) { - unsigned int i; + int i; datacheck_db = NULL; datacheck_data = NULL; datacheck_version = 0; @@ -827,9 +833,8 @@ void hpm_init(void) { return; } - for(i = 0; i < hpPHP_MAX; i++) { - HPM->packets[i] = NULL; - HPM->packetsc[i] = 0; + for (i = 0; i < hpPHP_MAX; i++) { + VECTOR_INIT(HPM->packets[i]); } #ifdef CONSOLE_INPUT @@ -851,7 +856,7 @@ void hpm_memdown(void) } void hpm_final(void) { - unsigned int i; + int i; HPM->off = true; @@ -865,9 +870,8 @@ void hpm_final(void) } VECTOR_CLEAR(HPM->symbols); - for( i = 0; i < hpPHP_MAX; i++ ) { - if( HPM->packets[i] ) - aFree(HPM->packets[i]); + for (i = 0; i < hpPHP_MAX; i++) { + VECTOR_CLEAR(HPM->packets[i]); } for( i = 0; i < HPCT_MAX; i++ ) { @@ -899,10 +903,6 @@ void hpm_defaults(void) { /* */ HPM->fnames = NULL; HPM->fnamec = 0; - for(i = 0; i < hpPHP_MAX; i++) { - HPM->packets[i] = NULL; - HPM->packetsc[i] = 0; - } for(i = 0; i < HPCT_MAX; i++) { HPM->confs[i] = NULL; HPM->confsc[i] = 0; diff --git a/src/common/HPM.h b/src/common/HPM.h index 6d44cd474..adbba5eda 100644 --- a/src/common/HPM.h +++ b/src/common/HPM.h @@ -109,8 +109,7 @@ struct HPM_interface { VECTOR_DECL(struct hplugin *) plugins; VECTOR_DECL(struct hpm_symbol *) symbols; /* packet hooking points */ - struct HPluginPacket *packets[hpPHP_MAX]; - unsigned int packetsc[hpPHP_MAX]; + VECTOR_DECL(struct HPluginPacket) packets[hpPHP_MAX]; /* plugin file ptr caching */ struct HPMFileNameCache *fnames; unsigned int fnamec; diff --git a/src/login/login.c b/src/login/login.c index 572bd594f..1d8ef489a 100644 --- a/src/login/login.c +++ b/src/login/login.c @@ -768,7 +768,7 @@ void login_fromchar_parse_accinfo(int fd) //-------------------------------- int login_parse_fromchar(int fd) { - int j, id; + int id; uint32 ipl; char ip[16]; @@ -795,11 +795,12 @@ int login_parse_fromchar(int fd) while( RFIFOREST(fd) >= 2 ) { uint16 command = RFIFOW(fd,0); - if( HPM->packetsc[hpParse_FromChar] ) { - if( (j = HPM->parse_packets(fd,hpParse_FromChar)) ) { - if( j == 1 ) continue; - if( j == 2 ) return 0; - } + if (VECTOR_LENGTH(HPM->packets[hpParse_FromChar]) > 0) { + int result = HPM->parse_packets(fd,hpParse_FromChar); + if (result == 1) + continue; + if (result == 2) + return 0; } switch( command ) { @@ -1574,7 +1575,6 @@ void login_parse_request_connection(int fd, struct login_session_data* sd, const int login_parse_login(int fd) { struct login_session_data* sd = (struct login_session_data*)sockt->session[fd]->session_data; - int result; char ip[16]; uint32 ipl = sockt->session[fd]->client_addr; @@ -1608,11 +1608,12 @@ int login_parse_login(int fd) while( RFIFOREST(fd) >= 2 ) { uint16 command = RFIFOW(fd,0); - if( HPM->packetsc[hpParse_Login] ) { - if( (result = HPM->parse_packets(fd,hpParse_Login)) ) { - if( result == 1 ) continue; - if( result == 2 ) return 0; - } + if (VECTOR_LENGTH(HPM->packets[hpParse_Login]) > 0) { + int result = HPM->parse_packets(fd,hpParse_Login); + if (result == 1) + continue; + if (result == 2) + return 0; } switch( command ) { diff --git a/src/map/chrif.c b/src/map/chrif.c index 1e376e3bc..a27038ff7 100644 --- a/src/map/chrif.c +++ b/src/map/chrif.c @@ -1351,7 +1351,7 @@ void chrif_skillid2idx(int fd) { * *------------------------------------------*/ int chrif_parse(int fd) { - int packet_len, cmd, r; + int packet_len, cmd; // only process data from the char-server if ( fd != chrif->fd ) { @@ -1375,22 +1375,22 @@ int chrif_parse(int fd) { } } - while ( RFIFOREST(fd) >= 2 ) { - - if( HPM->packetsc[hpChrif_Parse] ) { - if( (r = HPM->parse_packets(fd,hpChrif_Parse)) ) { - if( r == 1 ) continue; - if( r == 2 ) return 0; - } + while (RFIFOREST(fd) >= 2) { + if (VECTOR_LENGTH(HPM->packets[hpChrif_Parse]) > 0) { + int result = HPM->parse_packets(fd,hpChrif_Parse); + if (result == 1) + continue; + if (result == 2) + return 0; } cmd = RFIFOW(fd,0); if (cmd < 0x2af8 || cmd >= 0x2af8 + ARRAYLENGTH(chrif->packet_len_table) || chrif->packet_len_table[cmd-0x2af8] == 0) { - r = intif->parse(fd); // Passed on to the intif + int result = intif->parse(fd); // Passed on to the intif - if (r == 1) continue; // Treated in intif - if (r == 2) return 0; // Didn't have enough data (len==-1) + if (result == 1) continue; // Treated in intif + if (result == 2) return 0; // Didn't have enough data (len==-1) ShowWarning("chrif_parse: session #%d, intif->parse failed (unrecognized command 0x%.4x).\n", fd, cmd); sockt->eof(fd); diff --git a/src/map/clif.c b/src/map/clif.c index 59c8a7197..9c7d327e6 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -18578,12 +18578,12 @@ int clif_parse(int fd) { if (RFIFOREST(fd) < 2) return 0; - if( HPM->packetsc[hpClif_Parse] ) { - int r; - if( (r = HPM->parse_packets(fd,hpClif_Parse)) ) { - if( r == 1 ) continue; - if( r == 2 ) return 0; - } + if (VECTOR_LENGTH(HPM->packets[hpClif_Parse]) > 0) { + int result = HPM->parse_packets(fd,hpClif_Parse); + if (result == 1) + continue; + if (result == 2) + return 0; } if( sd ) -- cgit v1.2.3-70-g09d2 From 542a86978485e3671745aecfd94fa15fc3b1c73b Mon Sep 17 00:00:00 2001 From: Haru Date: Tue, 15 Sep 2015 15:04:50 +0200 Subject: Changed HPM->fnames to a vector type, renamed to HPM->filenames - This is a generic vector. It doesn't make use of the VECTOR type because it needs to outlive the memory manager. Signed-off-by: Haru --- src/common/HPM.c | 58 +++++++++++++++++++++++++++++++++++--------------------- src/common/HPM.h | 7 +++++-- 2 files changed, 41 insertions(+), 24 deletions(-) (limited to 'src/common/HPM.c') diff --git a/src/common/HPM.c b/src/common/HPM.c index 0bbfbbb49..5f3aec8ab 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -705,23 +705,33 @@ char *hplugins_id2name(unsigned int pid) return "UnknownPlugin"; } -char* HPM_file2ptr(const char *file) { - unsigned int i; - for(i = 0; i < HPM->fnamec; i++) { - if( HPM->fnames[i].addr == file ) - return HPM->fnames[i].name; - } +/** + * Returns a retained permanent pointer to a source filename, for memory-manager reporting use. + * + * The returned pointer is safe to be used as filename in the memory manager + * functions, and it will be available during and after the memory manager + * shutdown (for memory leak reporting purposes). + * + * @param file The string/filename to retain + * @return A retained copy of the source string. + */ +const char *HPM_file2ptr(const char *file) +{ + int i; - i = HPM->fnamec; + ARR_FIND(0, HPM->filenames.count, i, HPM->filenames.data[i].addr == file); + if (i != HPM->filenames.count) { + return HPM->filenames.data[i].name; + } /* we handle this memory outside of the server's memory manager because we need it to exist after the memory manager goes down */ - HPM->fnames = realloc(HPM->fnames,(++HPM->fnamec)*sizeof(struct HPMFileNameCache)); + HPM->filenames.data = realloc(HPM->filenames.data, (++HPM->filenames.count)*sizeof(struct HPMFileNameCache)); - HPM->fnames[i].addr = file; - HPM->fnames[i].name = strdup(file); + HPM->filenames.data[i].addr = file; + HPM->filenames.data[i].name = strdup(file); - return HPM->fnames[i].name; + return HPM->filenames.data[i].name; } void* HPM_mmalloc(size_t size, const char *file, int line, const char *func) { return iMalloc->malloc(size,HPM_file2ptr(file),line,func); @@ -842,18 +852,25 @@ void hpm_init(void) { #endif return; } + +/** + * Releases the retained filenames cache. + */ void hpm_memdown(void) { - /* this memory is handled outside of the server's memory manager and thus cleared after memory manager goes down */ - - if (HPM->fnames) { - unsigned int i; - for (i = 0; i < HPM->fnamec; i++) { - free(HPM->fnames[i].name); + /* this memory is handled outside of the server's memory manager and + * thus cleared after memory manager goes down */ + if (HPM->filenames.count) { + int i; + for (i = 0; i < HPM->filenames.count; i++) { + free(HPM->filenames.data[i].name); } - free(HPM->fnames); + free(HPM->filenames.data); + HPM->filenames.data = NULL; + HPM->filenames.count = 0; } } + void hpm_final(void) { int i; @@ -896,13 +913,10 @@ void hpm_defaults(void) { unsigned int i; HPM = &HPM_s; - HPM->fnames = NULL; - HPM->fnamec = 0; + memset(&HPM->filenames, 0, sizeof(HPM->filenames)); HPM->force_return = false; HPM->hooking = false; /* */ - HPM->fnames = NULL; - HPM->fnamec = 0; for(i = 0; i < HPCT_MAX; i++) { HPM->confs[i] = NULL; HPM->confsc[i] = 0; diff --git a/src/common/HPM.h b/src/common/HPM.h index adbba5eda..e8cc02f6f 100644 --- a/src/common/HPM.h +++ b/src/common/HPM.h @@ -111,8 +111,11 @@ struct HPM_interface { /* packet hooking points */ VECTOR_DECL(struct HPluginPacket) packets[hpPHP_MAX]; /* plugin file ptr caching */ - struct HPMFileNameCache *fnames; - unsigned int fnamec; + struct { + // This doesn't use a VECTOR because it needs to exist after the memory manager goes down. + int count; + struct HPMFileNameCache *data; + } filenames; /* config listen */ struct HPConfListenStorage *confs[HPCT_MAX]; unsigned int confsc[HPCT_MAX]; -- cgit v1.2.3-70-g09d2 From 9e2c59b0191e93effb7af82bb3e8e7e41bd75e67 Mon Sep 17 00:00:00 2001 From: Haru Date: Tue, 15 Sep 2015 15:21:19 +0200 Subject: Changed HPM->confs to a VECTOR and renamed to HPM->config_listeners Signed-off-by: Haru --- src/common/HPM.c | 78 ++++++++++++++++++++++++++++++++----------------------- src/common/HPM.h | 3 +-- src/common/HPMi.h | 14 +++++----- 3 files changed, 53 insertions(+), 42 deletions(-) (limited to 'src/common/HPM.c') diff --git a/src/common/HPM.c b/src/common/HPM.c index 5f3aec8ab..3fc0ab478 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -357,25 +357,36 @@ bool hpm_add_arg(unsigned int pluginID, char *name, bool has_param, CmdlineExecF return cmdline->arg_add(pluginID, name, '\0', func, help, has_param ? CMDLINE_OPT_PARAM : CMDLINE_OPT_NORMAL); } +/** + * Adds a configuration listener for a plugin. + * + * @param pluginID The plugin identifier. + * @param type The configuration type to listen for. + * @param name The configuration entry name. + * @param func The callback function. + * @retval true if the listener was added successfully. + * @retval false in case of error. + */ bool hplugins_addconf(unsigned int pluginID, enum HPluginConfType type, char *name, void (*func) (const char *val)) { struct HPConfListenStorage *conf; - unsigned int i; + int i; if (type >= HPCT_MAX) { ShowError("HPM->addConf:%s: unknown point '%u' specified for config '%s'\n",HPM->pid2name(pluginID),type,name); return false; } - for (i = 0; i < HPM->confsc[type]; i++) { - if (!strcmpi(name,HPM->confs[type][i].key)) { - ShowError("HPM->addConf:%s: duplicate '%s', already in use by '%s'!",HPM->pid2name(pluginID),name,HPM->pid2name(HPM->confs[type][i].pluginID)); - return false; - } + ARR_FIND(0, VECTOR_LENGTH(HPM->config_listeners[type]), i, strcmpi(name, VECTOR_INDEX(HPM->config_listeners[type], i).key) == 0); + if (i != VECTOR_LENGTH(HPM->config_listeners[type])) { + ShowError("HPM->addConf:%s: duplicate '%s', already in use by '%s'!", + HPM->pid2name(pluginID), name, HPM->pid2name(VECTOR_INDEX(HPM->config_listeners[type], i).pluginID)); + return false; } - RECREATE(HPM->confs[type], struct HPConfListenStorage, ++HPM->confsc[type]); - conf = &HPM->confs[type][HPM->confsc[type] - 1]; + VECTOR_ENSURE(HPM->config_listeners[type], 1, 1); + VECTOR_PUSHZEROED(HPM->config_listeners[type]); + conf = &VECTOR_LAST(HPM->config_listeners[type]); conf->pluginID = pluginID; safestrncpy(conf->key, name, HPM_ADDCONF_LENGTH); @@ -749,22 +760,24 @@ char* HPM_astrdup(const char *p, const char *file, int line, const char *func) { return iMalloc->astrdup(p,HPM_file2ptr(file),line,func); } -bool hplugins_parse_conf(const char *w1, const char *w2, enum HPluginConfType point) { - unsigned int i; - - /* exists? */ - for(i = 0; i < HPM->confsc[point]; i++) { - if( !strcmpi(w1,HPM->confs[point][i].key) ) - break; - } - - /* trigger and we're set! */ - if( i != HPM->confsc[point] ) { - HPM->confs[point][i].func(w2); - return true; - } +/** + * Parses a configuration entry through the registered plugins. + * + * @param w1 The configuration entry name. + * @param w2 The configuration entry value. + * @param point The type of configuration file. + * @retval true if a registered plugin was found to handle the entry. + * @retval false if no registered plugins could be found. + */ +bool hplugins_parse_conf(const char *w1, const char *w2, enum HPluginConfType point) +{ + int i; + ARR_FIND(0, VECTOR_LENGTH(HPM->config_listeners[point]), i, strcmpi(w1, VECTOR_INDEX(HPM->config_listeners[point], i).key) == 0); + if (i == VECTOR_LENGTH(HPM->config_listeners[point])) + return false; - return false; + VECTOR_INDEX(HPM->config_listeners[point], i).func(w2); + return true; } /** @@ -847,6 +860,10 @@ void hpm_init(void) { VECTOR_INIT(HPM->packets[i]); } + for (i = 0; i < HPCT_MAX; i++) { + VECTOR_INIT(HPM->config_listeners[i]); + } + #ifdef CONSOLE_INPUT console->input->addCommand("plugins",CPCMD_A(plugins)); #endif @@ -891,10 +908,10 @@ void hpm_final(void) VECTOR_CLEAR(HPM->packets[i]); } - for( i = 0; i < HPCT_MAX; i++ ) { - if( HPM->confsc[i] ) - aFree(HPM->confs[i]); + for (i = 0; i < HPCT_MAX; i++) { + VECTOR_CLEAR(HPM->config_listeners[i]); } + if (HPM->cmdline_plugins) { int j; for (j = 0; j < HPM->cmdline_plugins_count; j++) @@ -909,18 +926,13 @@ void hpm_final(void) return; } -void hpm_defaults(void) { - unsigned int i; +void hpm_defaults(void) +{ HPM = &HPM_s; memset(&HPM->filenames, 0, sizeof(HPM->filenames)); HPM->force_return = false; HPM->hooking = false; - /* */ - for(i = 0; i < HPCT_MAX; i++) { - HPM->confs[i] = NULL; - HPM->confsc[i] = 0; - } HPM->cmdline_plugins = NULL; HPM->cmdline_plugins_count = 0; /* */ diff --git a/src/common/HPM.h b/src/common/HPM.h index e8cc02f6f..c3284527a 100644 --- a/src/common/HPM.h +++ b/src/common/HPM.h @@ -117,8 +117,7 @@ struct HPM_interface { struct HPMFileNameCache *data; } filenames; /* config listen */ - struct HPConfListenStorage *confs[HPCT_MAX]; - unsigned int confsc[HPCT_MAX]; + VECTOR_DECL(struct HPConfListenStorage) config_listeners[HPCT_MAX]; /** Plugins requested through the command line */ char **cmdline_plugins; int cmdline_plugins_count; diff --git a/src/common/HPMi.h b/src/common/HPMi.h index e03f52e3b..eee713fd9 100644 --- a/src/common/HPMi.h +++ b/src/common/HPMi.h @@ -76,13 +76,13 @@ enum HPluginDataTypes { /* used in macros and conf storage */ enum HPluginConfType { - HPCT_BATTLE, /* battle-conf (map-server) */ - HPCT_LOGIN, /* login-server.conf (login-server) */ - HPCT_CHAR, /* char-server.conf (char-server) */ - HPCT_CHAR_INTER, /* inter-server.conf (char-server) */ - HPCT_MAP_INTER, /* inter-server.conf (map-server) */ - HPCT_LOG, /* logs.conf (map-server) */ - HPCT_SCRIPT, /* script.conf (map-server) */ + HPCT_BATTLE, ///< battle-conf (map-server) + HPCT_LOGIN, ///< login-server.conf (login-server) + HPCT_CHAR, ///< char-server.conf (char-server) + HPCT_CHAR_INTER, ///< inter-server.conf (char-server) + HPCT_MAP_INTER, ///< inter-server.conf (map-server) + HPCT_LOG, ///< logs.conf (map-server) + HPCT_SCRIPT, ///< script.conf (map-server) HPCT_MAX, }; -- cgit v1.2.3-70-g09d2 From 4d806399aa529431f6c2a347930d91177d8a2bfa Mon Sep 17 00:00:00 2001 From: Haru Date: Tue, 15 Sep 2015 15:33:58 +0200 Subject: Changed HPM->cmdline_plugins to a VECTOR and renamed to HPM->cmdline_load_plugins Signed-off-by: Haru --- src/common/HPM.c | 21 ++++++++------------- src/common/HPM.h | 3 +-- 2 files changed, 9 insertions(+), 15 deletions(-) (limited to 'src/common/HPM.c') diff --git a/src/common/HPM.c b/src/common/HPM.c index 3fc0ab478..454da9fbb 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -558,8 +558,8 @@ void hplugin_unload(struct hplugin* plugin) */ CMDLINEARG(loadplugin) { - RECREATE(HPM->cmdline_plugins, char *, ++HPM->cmdline_plugins_count); - HPM->cmdline_plugins[HPM->cmdline_plugins_count-1] = aStrdup(params); + VECTOR_ENSURE(HPM->cmdline_load_plugins, 1, 1); + VECTOR_PUSH(HPM->cmdline_load_plugins, aStrdup(params)); return true; } @@ -583,9 +583,9 @@ void hplugins_config_read(void) { return; plist = libconfig->lookup(&plugins_conf, "plugins_list"); - for (i = 0; i < HPM->cmdline_plugins_count; i++) { + for (i = 0; i < VECTOR_LENGTH(HPM->cmdline_load_plugins); i++) { config_setting_t *entry = libconfig->setting_add(plist, NULL, CONFIG_TYPE_STRING); - config_setting_set_string(entry, HPM->cmdline_plugins[i]); + config_setting_set_string(entry, VECTOR_INDEX(HPM->cmdline_load_plugins, i)); } if (plist != NULL) { @@ -912,14 +912,10 @@ void hpm_final(void) VECTOR_CLEAR(HPM->config_listeners[i]); } - if (HPM->cmdline_plugins) { - int j; - for (j = 0; j < HPM->cmdline_plugins_count; j++) - aFree(HPM->cmdline_plugins[j]); - aFree(HPM->cmdline_plugins); - HPM->cmdline_plugins = NULL; - HPM->cmdline_plugins_count = 0; + while (VECTOR_LENGTH(HPM->cmdline_load_plugins)) { + aFree(VECTOR_POP(HPM->cmdline_load_plugins)); } + VECTOR_CLEAR(HPM->cmdline_load_plugins); /* HPM->fnames is cleared after the memory manager goes down */ iMalloc->post_shutdown = hpm_memdown; @@ -931,10 +927,9 @@ void hpm_defaults(void) HPM = &HPM_s; memset(&HPM->filenames, 0, sizeof(HPM->filenames)); + VECTOR_INIT(HPM->cmdline_load_plugins); HPM->force_return = false; HPM->hooking = false; - HPM->cmdline_plugins = NULL; - HPM->cmdline_plugins_count = 0; /* */ HPM->init = hpm_init; HPM->final = hpm_final; diff --git a/src/common/HPM.h b/src/common/HPM.h index c3284527a..444829419 100644 --- a/src/common/HPM.h +++ b/src/common/HPM.h @@ -119,8 +119,7 @@ struct HPM_interface { /* config listen */ VECTOR_DECL(struct HPConfListenStorage) config_listeners[HPCT_MAX]; /** Plugins requested through the command line */ - char **cmdline_plugins; - int cmdline_plugins_count; + VECTOR_DECL(char *) cmdline_load_plugins; /* funcs */ void (*init) (void); void (*final) (void); -- cgit v1.2.3-70-g09d2 From 502703d2b632125b35a2b8d341a90a13d7445f02 Mon Sep 17 00:00:00 2001 From: Haru Date: Wed, 16 Sep 2015 17:48:09 +0200 Subject: Changed cmdline->args_data to a VECTOR Signed-off-by: Haru --- src/common/HPM.c | 4 ++-- src/common/core.c | 35 ++++++++++++++++++----------------- src/common/core.h | 4 ++-- 3 files changed, 22 insertions(+), 21 deletions(-) (limited to 'src/common/HPM.c') diff --git a/src/common/HPM.c b/src/common/HPM.c index 454da9fbb..77c7e3fa2 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -347,9 +347,9 @@ bool hpm_add_arg(unsigned int pluginID, char *name, bool has_param, CmdlineExecF return false; } - ARR_FIND(0, cmdline->args_data_count, i, strcmp(cmdline->args_data[i].name, name) == 0); + ARR_FIND(0, VECTOR_LENGTH(cmdline->args_data), i, strcmp(VECTOR_INDEX(cmdline->args_data, i).name, name) == 0); - if (i < cmdline->args_data_count) { + if (i != VECTOR_LENGTH(cmdline->args_data)) { ShowError("HPM:add_arg:%s duplicate! (from %s)\n",name,HPM->pid2name(pluginID)); return false; } diff --git a/src/common/core.c b/src/common/core.c index dcc96fa41..8564b8acc 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -207,8 +207,9 @@ const char *cmdline_arg_source(struct CmdlineArgData *arg) { bool cmdline_arg_add(unsigned int pluginID, const char *name, char shortname, CmdlineExecFunc func, const char *help, unsigned int options) { struct CmdlineArgData *data = NULL; - RECREATE(cmdline->args_data, struct CmdlineArgData, ++cmdline->args_data_count); - data = &cmdline->args_data[cmdline->args_data_count-1]; + VECTOR_ENSURE(cmdline->args_data, 1, 1); + VECTOR_PUSHZEROED(cmdline->args_data); + data = &VECTOR_LAST(cmdline->args_data); data->pluginID = pluginID; data->name = aStrdup(name); data->shortname = shortname; @@ -228,8 +229,8 @@ static CMDLINEARG(help) ShowInfo("\n"); ShowInfo("Options:\n"); - for (i = 0; i < cmdline->args_data_count; i++) { - struct CmdlineArgData *data = &cmdline->args_data[i]; + for (i = 0; i < VECTOR_LENGTH(cmdline->args_data); i++) { + struct CmdlineArgData *data = &VECTOR_INDEX(cmdline->args_data, i); char altname[16], paramnames[256]; if (data->shortname) { snprintf(altname, sizeof(altname), " [-%c]", data->shortname); @@ -288,8 +289,9 @@ bool cmdline_arg_next_value(const char *name, int current_arg, int argc) */ int cmdline_exec(int argc, char **argv, unsigned int options) { - int count = 0, i, j; + int count = 0, i; for (i = 1; i < argc; i++) { + int j; struct CmdlineArgData *data = NULL; const char *arg = argv[i]; if (arg[0] != '-') { // All arguments must begin with '-' @@ -297,17 +299,17 @@ int cmdline_exec(int argc, char **argv, unsigned int options) exit(EXIT_FAILURE); } if (arg[1] != '-' && strlen(arg) == 2) { - ARR_FIND(0, cmdline->args_data_count, j, cmdline->args_data[j].shortname == arg[1]); + ARR_FIND(0, VECTOR_LENGTH(cmdline->args_data), j, VECTOR_INDEX(cmdline->args_data, j).shortname == arg[1]); } else { - ARR_FIND(0, cmdline->args_data_count, j, strcmpi(cmdline->args_data[j].name, arg) == 0); + ARR_FIND(0, VECTOR_LENGTH(cmdline->args_data), j, strcmpi(VECTOR_INDEX(cmdline->args_data, j).name, arg) == 0); } - if (j == cmdline->args_data_count) { + if (j == VECTOR_LENGTH(cmdline->args_data)) { if (options&(CMDLINE_OPT_SILENT|CMDLINE_OPT_PREINIT)) continue; ShowError("Unknown option '%s'.\n", arg); exit(EXIT_FAILURE); } - data = &cmdline->args_data[j]; + data = &VECTOR_INDEX(cmdline->args_data, j); if (data->options&CMDLINE_OPT_PARAM) { if (!cmdline->arg_next_value(arg, i, argc)) exit(EXIT_FAILURE); @@ -346,15 +348,15 @@ void cmdline_init(void) #endif // !MINICORE cmdline_args_init_local(); } + void cmdline_final(void) { - int i; - for (i = 0; i < cmdline->args_data_count; i++) { - aFree(cmdline->args_data[i].name); - aFree(cmdline->args_data[i].help); + while (VECTOR_LENGTH(cmdline->args_data) > 0) { + struct CmdlineArgData *data = &VECTOR_POP(cmdline->args_data); + aFree(data->name); + aFree(data->help); } - if (cmdline->args_data) - aFree(cmdline->args_data); + VECTOR_CLEAR(cmdline->args_data); } struct cmdline_interface cmdline_s; @@ -364,8 +366,7 @@ void cmdline_defaults(void) { cmdline = &cmdline_s; - cmdline->args_data = NULL; - cmdline->args_data_count = 0; + VECTOR_INIT(cmdline->args_data); cmdline->init = cmdline_init; cmdline->final = cmdline_final; diff --git a/src/common/core.h b/src/common/core.h index c92bf4fa6..f8e748db4 100644 --- a/src/common/core.h +++ b/src/common/core.h @@ -6,6 +6,7 @@ #define COMMON_CORE_H #include "common/hercules.h" +#include "common/db.h" /* so that developers with --enable-debug can raise signals from any section of the code they'd like */ #ifdef DEBUG @@ -46,8 +47,7 @@ struct CmdlineArgData { }; struct cmdline_interface { - struct CmdlineArgData *args_data; - int args_data_count; + VECTOR_DECL(struct CmdlineArgData) args_data; void (*init) (void); void (*final) (void); -- cgit v1.2.3-70-g09d2 From 70265291d62280c525adc317158e9f531e0147ff Mon Sep 17 00:00:00 2001 From: Haru Date: Thu, 17 Sep 2015 01:21:28 +0200 Subject: Cleanup of the HPluginData implementation (First part) - Several explicit casts are removed, to have a slightly better type-checking at compile time. - A destructor function is provided, to remove code duplication. Signed-off-by: Haru --- src/char/HPMchar.c | 17 +++-- src/char/HPMchar.h | 2 +- src/common/HPM.c | 168 ++++++++++++++++++++++++++++--------------------- src/common/HPM.h | 20 +++--- src/common/HPMi.h | 79 +++++++++++------------ src/common/mmo.h | 5 +- src/common/socket.c | 13 +--- src/common/socket.h | 5 +- src/login/HPMlogin.c | 17 +++-- src/login/HPMlogin.h | 2 +- src/map/HPMmap.c | 54 +++++----------- src/map/HPMmap.h | 2 +- src/map/battleground.c | 15 ++--- src/map/battleground.h | 5 +- src/map/guild.c | 25 ++------ src/map/instance.c | 13 +--- src/map/instance.h | 5 +- src/map/itemdb.c | 13 +--- src/map/itemdb.h | 4 +- src/map/map.c | 12 +--- src/map/map.h | 4 +- src/map/mob.c | 12 +--- src/map/mob.h | 9 ++- src/map/npc.c | 12 +--- src/map/npc.h | 5 +- src/map/party.c | 25 ++------ src/map/party.h | 5 +- src/map/pc.c | 22 ++----- src/map/pc.h | 6 +- src/map/unit.c | 27 ++------ 30 files changed, 246 insertions(+), 357 deletions(-) (limited to 'src/common/HPM.c') diff --git a/src/char/HPMchar.c b/src/char/HPMchar.c index a67f017c1..14d7be1a3 100644 --- a/src/char/HPMchar.c +++ b/src/char/HPMchar.c @@ -47,13 +47,18 @@ // HPMDataCheck comes after all the other includes #include "common/HPMDataCheck.h" -bool HPM_char_grabHPData(struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr) { - /* record address */ - switch( type ) { +/** + * HPM plugin data store validator sub-handler (char-server) + * + * @see HPM_interface::data_store_validate + */ +bool HPM_char_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) +{ + switch (type) { default: - return false; + break; } - return true; + return false; } void HPM_char_plugin_load_sub(struct hplugin *plugin) { @@ -61,6 +66,8 @@ void HPM_char_plugin_load_sub(struct hplugin *plugin) { } void HPM_char_do_init(void) { + HPM->load_sub = HPM_char_plugin_load_sub; + HPM->data_store_validate_sub = HPM_char_data_store_validate; HPM->datacheck_init(HPMDataCheck, HPMDataCheckLen, HPMDataCheckVer); HPM_shared_symbols(SERVER_TYPE_CHAR); } diff --git a/src/char/HPMchar.h b/src/char/HPMchar.h index e3e000ef3..fd07f45ed 100644 --- a/src/char/HPMchar.h +++ b/src/char/HPMchar.h @@ -13,7 +13,7 @@ struct hplugin; -bool HPM_char_grabHPData(struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr); +bool HPM_char_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store); void HPM_char_plugin_load_sub(struct hplugin *plugin); diff --git a/src/common/HPM.c b/src/common/HPM.c index 77c7e3fa2..ce3d769ca 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -168,57 +168,54 @@ bool hplugins_addpacket(unsigned short cmd, unsigned short length, void (*receiv return true; } -void hplugins_grabHPData(struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr) +bool hplugin_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) { - /* record address */ + nullpo_retr(false, store); + switch (type) { /* core-handled */ case HPDT_SESSION: - ret->HPDataSRCPtr = (void**)(&((struct socket_data *)ptr)->hdata); - ret->hdatac = &((struct socket_data *)ptr)->hdatac; - break; + if (!*store) { + *store = HPM->data_store_create(); + } + return true; /* goes to sub */ default: - if (HPM->grabHPDataSub) { - if (HPM->grabHPDataSub(ret,type,ptr)) - return; - ShowError("HPM:HPM:grabHPData failed, unknown type %d!\n",type); - } else { - ShowError("HPM:grabHPData failed, type %d needs sub-handler!\n",type); - } - ret->HPDataSRCPtr = NULL; - ret->hdatac = NULL; - return; + break; } + if (HPM->data_store_validate_sub) { + if (HPM->data_store_validate_sub(type, store)) + return true; + ShowError("HPM:HPM:validateHPData failed, unknown type %d!\n",type); + } else { + ShowError("HPM:validateHPData failed, type %d needs sub-handler!\n",type); + } + return false; } -void hplugins_addToHPData(enum HPluginDataTypes type, unsigned int pluginID, void *ptr, void *data, unsigned int index, bool autofree) +void hplugins_addToHPData(enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, void *data, unsigned int index, bool autofree) { - struct HPluginData *HPData, **HPDataSRC; - struct HPDataOperationStorage action; - unsigned int i, max; - - HPM->grabHPData(&action,type,ptr); + struct hplugin_data_entry *HPData; + unsigned int i; + struct hplugin_data_store *store; - if (action.hdatac == NULL) { /* woo it failed! */ + if (!HPM->data_store_validate(type, storeptr)) { + /* woo it failed! */ ShowError("HPM:addToHPData:%s: failed, type %d (%u|%u)\n",HPM->pid2name(pluginID),type,pluginID,index); return; } - - /* flag */ - HPDataSRC = *(action.HPDataSRCPtr); - max = *(action.hdatac); + store = *storeptr; /* duplicate check */ - for (i = 0; i < max; i++) { - if (HPDataSRC[i]->pluginID == pluginID && HPDataSRC[i]->type == index) { + for (i = 0; i < store->count; i++) { + if (store->array[i]->pluginID == pluginID && store->array[i]->type == index) { ShowError("HPM:addToHPData:%s: error! attempting to insert duplicate struct of id %u and index %u\n",HPM->pid2name(pluginID),pluginID,index); return; } } - /* HPluginData is always same size, probably better to use the ERS (with reasonable chunk size e.g. 10/25/50) */ - CREATE(HPData, struct HPluginData, 1); + /* hplugin_data_entry is always same size, probably better to use the ERS (with reasonable chunk size e.g. 10/25/50) */ + CREATE(HPData, struct hplugin_data_entry, 1); /* input */ HPData->pluginID = pluginID; @@ -227,76 +224,63 @@ void hplugins_addToHPData(enum HPluginDataTypes type, unsigned int pluginID, voi HPData->data = data; /* resize */ - *(action.hdatac) += 1; - RECREATE(*(action.HPDataSRCPtr),struct HPluginData *,*(action.hdatac)); + RECREATE(store->array, struct hplugin_data_entry *, ++store->count); - /* RECREATE modified the address */ - HPDataSRC = *(action.HPDataSRCPtr); - HPDataSRC[*(action.hdatac) - 1] = HPData; + store->array[store->count - 1] = HPData; } -void *hplugins_getFromHPData(enum HPluginDataTypes type, unsigned int pluginID, void *ptr, unsigned int index) +void *hplugins_getFromHPData(enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index) { - struct HPDataOperationStorage action; - struct HPluginData **HPDataSRC; - unsigned int i, max; - - HPM->grabHPData(&action,type,ptr); + unsigned int i; + struct hplugin_data_store *store; - if (action.hdatac == NULL) { /* woo it failed! */ + if (!HPM->data_store_validate(type, storeptr)) { + /* woo it failed! */ ShowError("HPM:getFromHPData:%s: failed, type %d (%u|%u)\n",HPM->pid2name(pluginID),type,pluginID,index); return NULL; } + store = *storeptr; - /* flag */ - HPDataSRC = *(action.HPDataSRCPtr); - max = *(action.hdatac); - - for (i = 0; i < max; i++) { - if (HPDataSRC[i]->pluginID == pluginID && HPDataSRC[i]->type == index) - return HPDataSRC[i]->data; + for (i = 0; i < store->count; i++) { + if (store->array[i]->pluginID == pluginID && store->array[i]->type == index) + return store->array[i]->data; } return NULL; } -void hplugins_removeFromHPData(enum HPluginDataTypes type, unsigned int pluginID, void *ptr, unsigned int index) +void hplugins_removeFromHPData(enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index) { - struct HPDataOperationStorage action; - struct HPluginData **HPDataSRC; - unsigned int i, max; - - HPM->grabHPData(&action,type,ptr); + unsigned int i; + struct hplugin_data_store *store; - if (action.hdatac == NULL) { /* woo it failed! */ + if (!HPM->data_store_validate(type, storeptr)) { + /* woo it failed! */ ShowError("HPM:removeFromHPData:%s: failed, type %d (%u|%u)\n",HPM->pid2name(pluginID),type,pluginID,index); return; } + store = *storeptr; - /* flag */ - HPDataSRC = *(action.HPDataSRCPtr); - max = *(action.hdatac); - - for (i = 0; i < max; i++) { - if (HPDataSRC[i]->pluginID == pluginID && HPDataSRC[i]->type == index) + for (i = 0; i < store->count; i++) { + if (store->array[i]->pluginID == pluginID && store->array[i]->type == index) break; } - if (i != max) { + if (i != store->count) { unsigned int cursor; - aFree(HPDataSRC[i]->data);/* when its removed we delete it regardless of autofree */ - aFree(HPDataSRC[i]); - HPDataSRC[i] = NULL; + aFree(store->array[i]->data);/* when its removed we delete it regardless of autofree */ + aFree(store->array[i]); + store->array[i] = NULL; - for (i = 0, cursor = 0; i < max; i++) { - if (HPDataSRC[i] == NULL) + for (i = 0, cursor = 0; i < store->count; i++) { + if (store->array[i] == NULL) continue; if (i != cursor) - HPDataSRC[cursor] = HPDataSRC[i]; + store->array[cursor] = store->array[i]; cursor++; } - *(action.hdatac) = cursor; + store->count = cursor; } } @@ -780,6 +764,43 @@ bool hplugins_parse_conf(const char *w1, const char *w2, enum HPluginConfType po return true; } +/** + * Helper to destroy and release an interface's hplugin_data store. + * + * @param hdata The hplugin_data store. The pointer will be freed. + */ +void hplugin_data_store_destroy(struct hplugin_data_store *store) +{ + unsigned int i; + + if (!store) + return; + + for (i = 0; i < store->count; i++) { + if (store->array[i]->flag.free) { + aFree(store->array[i]->data); + } + aFree(store->array[i]); + } + aFree(store->array); + aFree(store); +} + +/** + * Helper to create and initialize an interface's hplugin_data store. + * + * The store is owned by the caller, and it should be eventually destroyed by + * \c hdata_destroy. + * + * @return An initialized hplugin_data store. + */ +struct hplugin_data_store *hplugin_data_store_create(void) +{ + struct hplugin_data_store *store; + CREATE(store, struct hplugin_data_store, 1); + return store; +} + /** * Called by HPM->DataCheck on a plugins incoming data, ensures data structs in use are matching! **/ @@ -947,10 +968,13 @@ void hpm_defaults(void) HPM->parse_packets = hplugins_parse_packets; HPM->load_sub = NULL; HPM->addhook_sub = NULL; - HPM->grabHPData = hplugins_grabHPData; - HPM->grabHPDataSub = NULL; HPM->parseConf = hplugins_parse_conf; HPM->DataCheck = HPM_DataCheck; HPM->datacheck_init = HPM_datacheck_init; HPM->datacheck_final = HPM_datacheck_final; + + HPM->data_store_destroy = hplugin_data_store_destroy; + HPM->data_store_create = hplugin_data_store_create; + HPM->data_store_validate = hplugin_data_store_validate; + HPM->data_store_validate_sub = NULL; } diff --git a/src/common/HPM.h b/src/common/HPM.h index 444829419..1d13a178f 100644 --- a/src/common/HPM.h +++ b/src/common/HPM.h @@ -65,7 +65,7 @@ struct hpm_symbol { void *ptr; ///< The symbol value }; -struct HPluginData { +struct hplugin_data_entry { unsigned int pluginID; unsigned int type; struct { @@ -74,6 +74,11 @@ struct HPluginData { void *data; }; +struct hplugin_data_store { + struct hplugin_data_entry **array; + unsigned int count; +}; + struct HPluginPacket { unsigned int pluginID; unsigned short cmd; @@ -86,10 +91,6 @@ struct HPMFileNameCache { char *name; }; -struct HPDataOperationStorage { - void **HPDataSRCPtr; - unsigned int *hdatac; -}; /* */ struct HPConfListenStorage { unsigned int pluginID; @@ -136,15 +137,18 @@ struct HPM_interface { unsigned char (*parse_packets) (int fd, enum HPluginPacketHookingPoints point); void (*load_sub) (struct hplugin *plugin); bool (*addhook_sub) (enum HPluginHookType type, const char *target, void *hook, unsigned int pID); - void (*grabHPData) (struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr); - /* for server-specific HPData e.g. map_session_data */ - bool (*grabHPDataSub) (struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr); /* for custom config parsing */ bool (*parseConf) (const char *w1, const char *w2, enum HPluginConfType point); /* validates plugin data */ bool (*DataCheck) (struct s_HPMDataCheck *src, unsigned int size, int version, char *name); void (*datacheck_init) (const struct s_HPMDataCheck *src, unsigned int length, int version); void (*datacheck_final) (void); + + struct hplugin_data_store *(*data_store_create) (void); + void (*data_store_destroy) (struct hplugin_data_store *store); + bool (*data_store_validate) (enum HPluginDataTypes type, struct hplugin_data_store **store); + /* for server-specific HPData e.g. map_session_data */ + bool (*data_store_validate_sub) (enum HPluginDataTypes type, struct hplugin_data_store **store); }; CMDLINEARG(loadplugin); diff --git a/src/common/HPMi.h b/src/common/HPMi.h index eee713fd9..d92503a91 100644 --- a/src/common/HPMi.h +++ b/src/common/HPMi.h @@ -14,6 +14,7 @@ struct script_state; struct AtCommandInfo; struct socket_data; struct map_session_data; +struct hplugin_data_store; #define HPM_VERSION "1.1" #define HPM_ADDCONF_LENGTH 40 @@ -96,53 +97,53 @@ enum HPluginConfType { #define addArg(name, param,func,help) (HPMi->addArg(HPMi->pid,(name),(param),(cmdline_arg_ ## func),(help))) /* HPData handy redirects */ /* session[] */ -#define addToSession(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_SESSION,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromSession(ptr,index) (HPMi->getFromHPData(HPDT_SESSION,HPMi->pid,(ptr),(index))) -#define removeFromSession(ptr,index) (HPMi->removeFromHPData(HPDT_SESSION,HPMi->pid,(ptr),(index))) +#define addToSession(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_SESSION,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromSession(ptr,index) (HPMi->getFromHPData(HPDT_SESSION,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromSession(ptr,index) (HPMi->removeFromHPData(HPDT_SESSION,HPMi->pid,&(ptr)->hdata,(index))) /* map_session_data */ -#define addToMSD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MSD,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromMSD(ptr,index) (HPMi->getFromHPData(HPDT_MSD,HPMi->pid,(ptr),(index))) -#define removeFromMSD(ptr,index) (HPMi->removeFromHPData(HPDT_MSD,HPMi->pid,(ptr),(index))) +#define addToMSD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MSD,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromMSD(ptr,index) (HPMi->getFromHPData(HPDT_MSD,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromMSD(ptr,index) (HPMi->removeFromHPData(HPDT_MSD,HPMi->pid,&(ptr)->hdata,(index))) /* npc_data */ -#define addToNPCD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_NPCD,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromNPCD(ptr,index) (HPMi->getFromHPData(HPDT_NPCD,HPMi->pid,(ptr),(index))) -#define removeFromNPCD(ptr,index) (HPMi->removeFromHPData(HPDT_NPCD,HPMi->pid,(ptr),(index))) +#define addToNPCD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_NPCD,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromNPCD(ptr,index) (HPMi->getFromHPData(HPDT_NPCD,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromNPCD(ptr,index) (HPMi->removeFromHPData(HPDT_NPCD,HPMi->pid,&(ptr)->hdata,(index))) /* map_data */ -#define addToMAPD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MAP,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromMAPD(ptr,index) (HPMi->getFromHPData(HPDT_MAP,HPMi->pid,(ptr),(index))) -#define removeFromMAPD(ptr,index) (HPMi->removeFromHPData(HPDT_MAP,HPMi->pid,(ptr),(index))) +#define addToMAPD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MAP,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromMAPD(ptr,index) (HPMi->getFromHPData(HPDT_MAP,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromMAPD(ptr,index) (HPMi->removeFromHPData(HPDT_MAP,HPMi->pid,&(ptr)->hdata,(index))) /* party_data */ -#define addToPAD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_PARTY,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromPAD(ptr,index) (HPMi->getFromHPData(HPDT_PARTY,HPMi->pid,(ptr),(index))) -#define removeFromPAD(ptr,index) (HPMi->removeFromHPData(HPDT_PARTY,HPMi->pid,(ptr),(index))) +#define addToPAD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_PARTY,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromPAD(ptr,index) (HPMi->getFromHPData(HPDT_PARTY,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromPAD(ptr,index) (HPMi->removeFromHPData(HPDT_PARTY,HPMi->pid,&(ptr)->hdata,(index))) /* guild */ -#define addToGLD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_GUILD,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromGLD(ptr,index) (HPMi->getFromHPData(HPDT_GUILD,HPMi->pid,(ptr),(index))) -#define removeFromGLD(ptr,index) (HPMi->removeFromHPData(HPDT_GUILD,HPMi->pid,(ptr),(index))) +#define addToGLD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_GUILD,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromGLD(ptr,index) (HPMi->getFromHPData(HPDT_GUILD,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromGLD(ptr,index) (HPMi->removeFromHPData(HPDT_GUILD,HPMi->pid,&(ptr)->hdata,(index))) /* instance_data */ -#define addToINSTD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_INSTANCE,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromINSTD(ptr,index) (HPMi->getFromHPData(HPDT_INSTANCE,HPMi->pid,(ptr),(index))) -#define removeFromINSTD(ptr,index) (HPMi->removeFromHPData(HPDT_INSTANCE,HPMi->pid,(ptr),(index))) +#define addToINSTD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_INSTANCE,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromINSTD(ptr,index) (HPMi->getFromHPData(HPDT_INSTANCE,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromINSTD(ptr,index) (HPMi->removeFromHPData(HPDT_INSTANCE,HPMi->pid,&(ptr)->hdata,(index))) /* mob_db */ -#define addToMOBDB(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MOBDB,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromMOBDB(ptr,index) (HPMi->getFromHPData(HPDT_MOBDB,HPMi->pid,(ptr),(index))) -#define removeFromMOBDB(ptr,index) (HPMi->removeFromHPData(HPDT_MOBDB,HPMi->pid,(ptr),(index))) +#define addToMOBDB(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MOBDB,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromMOBDB(ptr,index) (HPMi->getFromHPData(HPDT_MOBDB,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromMOBDB(ptr,index) (HPMi->removeFromHPData(HPDT_MOBDB,HPMi->pid,&(ptr)->hdata,(index))) /* mob_data */ -#define addToMOBDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MOBDATA,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromMOBDATA(ptr,index) (HPMi->getFromHPData(HPDT_MOBDATA,HPMi->pid,(ptr),(index))) -#define removeFromMOBDATA(ptr,index) (HPMi->removeFromHPData(HPDT_MOBDATA,HPMi->pid,(ptr),(index))) +#define addToMOBDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MOBDATA,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromMOBDATA(ptr,index) (HPMi->getFromHPData(HPDT_MOBDATA,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromMOBDATA(ptr,index) (HPMi->removeFromHPData(HPDT_MOBDATA,HPMi->pid,&(ptr)->hdata,(index))) /* item_data */ -#define addToITEMDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_ITEMDATA,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromITEMDATA(ptr,index) (HPMi->getFromHPData(HPDT_ITEMDATA,HPMi->pid,(ptr),(index))) -#define removeFromITEMDATA(ptr,index) (HPMi->removeFromHPData(HPDT_ITEMDATA,HPMi->pid,(ptr),(index))) +#define addToITEMDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_ITEMDATA,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromITEMDATA(ptr,index) (HPMi->getFromHPData(HPDT_ITEMDATA,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromITEMDATA(ptr,index) (HPMi->removeFromHPData(HPDT_ITEMDATA,HPMi->pid,&(ptr)->hdata,(index))) /* battleground_data */ -#define addToBGDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_BGDATA,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromBGDATA(ptr,index) (HPMi->getFromHPData(HPDT_BGDATA,HPMi->pid,(ptr),(index))) -#define removeFromBGDATA(ptr,index) (HPMi->removeFromHPData(HPDT_BGDATA,HPMi->pid,(ptr),(index))) +#define addToBGDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_BGDATA,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromBGDATA(ptr,index) (HPMi->getFromHPData(HPDT_BGDATA,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromBGDATA(ptr,index) (HPMi->removeFromHPData(HPDT_BGDATA,HPMi->pid,&(ptr)->hdata,(index))) /* autotrade_vending */ -#define addToATVEND(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,(ptr),(data),(index),(autofree))) -#define getFromATVEND(ptr,index) (HPMi->getFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,(ptr),(index))) -#define removeFromATVEND(ptr,index) (HPMi->removeFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,(ptr),(index))) +#define addToATVEND(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) +#define getFromATVEND(ptr,index) (HPMi->getFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,&(ptr)->hdata,(index))) +#define removeFromATVEND(ptr,index) (HPMi->removeFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,&(ptr)->hdata,(index))) /// HPMi->addCommand #define addAtcommand(cname,funcname) do { \ @@ -205,9 +206,9 @@ struct HPMi_interface { bool (*addScript) (char *name, char *args, bool (*func)(struct script_state *st), bool isDeprecated); void (*addCPCommand) (char *name, CParseFunc func); /* HPM Custom Data */ - void (*addToHPData) (enum HPluginDataTypes type, unsigned int pluginID, void *ptr, void *data, unsigned int index, bool autofree); - void *(*getFromHPData) (enum HPluginDataTypes type, unsigned int pluginID, void *ptr, unsigned int index); - void (*removeFromHPData) (enum HPluginDataTypes type, unsigned int pluginID, void *ptr, unsigned int index); + void (*addToHPData) (enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, void *data, unsigned int index, bool autofree); + void *(*getFromHPData) (enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index); + void (*removeFromHPData) (enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index); /* packet */ bool (*addPacket) (unsigned short cmd, unsigned short length, void (*receive)(int fd), unsigned int point, unsigned int pluginID); /* Hooking */ diff --git a/src/common/mmo.h b/src/common/mmo.h index 012eec935..cbda0e91b 100644 --- a/src/common/mmo.h +++ b/src/common/mmo.h @@ -198,7 +198,7 @@ #define JOBL_BABY 0x2000 //8192 #define JOBL_THIRD 0x4000 //16384 -struct HPluginData; +struct hplugin_data_store; enum item_types { IT_HEALING = 0, @@ -663,8 +663,7 @@ struct guild { struct channel_data *channel; /* HPM Custom Struct */ - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; struct guild_castle { diff --git a/src/common/socket.c b/src/common/socket.c index de8ca4682..842f9ba87 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -623,7 +623,6 @@ static int create_session(int fd, RecvFunc func_recv, SendFunc func_send, ParseF sockt->session[fd]->rdata_tick = sockt->last_tick; sockt->session[fd]->session_data = NULL; sockt->session[fd]->hdata = NULL; - sockt->session[fd]->hdatac = 0; return 0; } @@ -638,16 +637,8 @@ static void delete_session(int fd) aFree(sockt->session[fd]->wdata); if( sockt->session[fd]->session_data ) aFree(sockt->session[fd]->session_data); - if (sockt->session[fd]->hdata) { - unsigned int i; - for(i = 0; i < sockt->session[fd]->hdatac; i++) { - if( sockt->session[fd]->hdata[i]->flag.free ) { - aFree(sockt->session[fd]->hdata[i]->data); - } - aFree(sockt->session[fd]->hdata[i]); - } - aFree(sockt->session[fd]->hdata); - } + HPM->data_store_destroy(sockt->session[fd]->hdata); + sockt->session[fd]->hdata = NULL; aFree(sockt->session[fd]); sockt->session[fd] = NULL; } diff --git a/src/common/socket.h b/src/common/socket.h index a995bffc8..510eac575 100644 --- a/src/common/socket.h +++ b/src/common/socket.h @@ -17,7 +17,7 @@ # include #endif -struct HPluginData; +struct hplugin_data_store; #define FIFOSIZE_SERVERLINK 256*1024 @@ -105,8 +105,7 @@ struct socket_data { void* session_data; // stores application-specific data related to the session - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; struct hSockOpt { diff --git a/src/login/HPMlogin.c b/src/login/HPMlogin.c index 895cbad16..ce88728fe 100644 --- a/src/login/HPMlogin.c +++ b/src/login/HPMlogin.c @@ -32,13 +32,18 @@ // HPMDataCheck comes after all the other includes #include "common/HPMDataCheck.h" -bool HPM_login_grabHPData(struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr) { - /* record address */ - switch( type ) { +/** + * HPM plugin data store validator sub-handler (login-server) + * + * @see HPM_interface::data_store_validate + */ +bool HPM_login_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) +{ + switch (type) { default: - return false; + break; } - return true; + return false; } void HPM_login_plugin_load_sub(struct hplugin *plugin) { @@ -46,6 +51,8 @@ void HPM_login_plugin_load_sub(struct hplugin *plugin) { } void HPM_login_do_init(void) { + HPM->load_sub = HPM_login_plugin_load_sub; + HPM->data_store_validate_sub = HPM_login_data_store_validate; HPM->datacheck_init(HPMDataCheck, HPMDataCheckLen, HPMDataCheckVer); HPM_shared_symbols(SERVER_TYPE_LOGIN); } diff --git a/src/login/HPMlogin.h b/src/login/HPMlogin.h index 2a4d5c538..1f08a8666 100644 --- a/src/login/HPMlogin.h +++ b/src/login/HPMlogin.h @@ -13,7 +13,7 @@ struct hplugin; -bool HPM_login_grabHPData(struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr); +bool HPM_login_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store); void HPM_login_plugin_load_sub(struct hplugin *plugin); diff --git a/src/map/HPMmap.c b/src/map/HPMmap.c index ac78e8c84..ddffb5519 100644 --- a/src/map/HPMmap.c +++ b/src/map/HPMmap.c @@ -85,57 +85,33 @@ struct HPM_atcommand_list { struct HPM_atcommand_list *atcommand_list = NULL; unsigned int atcommand_list_items = 0; -bool HPM_map_grabHPData(struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr) { - /* record address */ - switch( type ) { +/** + * HPM plugin data store validator sub-handler (map-server) + * + * @see HPM_interface::data_store_validate + */ +bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) +{ + switch (type) { case HPDT_MSD: - ret->HPDataSRCPtr = (void**)(&((struct map_session_data *)ptr)->hdata); - ret->hdatac = &((struct map_session_data *)ptr)->hdatac; - break; case HPDT_NPCD: - ret->HPDataSRCPtr = (void**)(&((struct npc_data *)ptr)->hdata); - ret->hdatac = &((struct npc_data *)ptr)->hdatac; - break; case HPDT_MAP: - ret->HPDataSRCPtr = (void**)(&((struct map_data *)ptr)->hdata); - ret->hdatac = &((struct map_data *)ptr)->hdatac; - break; case HPDT_PARTY: - ret->HPDataSRCPtr = (void**)(&((struct party_data *)ptr)->hdata); - ret->hdatac = &((struct party_data *)ptr)->hdatac; - break; case HPDT_GUILD: - ret->HPDataSRCPtr = (void**)(&((struct guild *)ptr)->hdata); - ret->hdatac = &((struct guild *)ptr)->hdatac; - break; case HPDT_INSTANCE: - ret->HPDataSRCPtr = (void**)(&((struct instance_data *)ptr)->hdata); - ret->hdatac = &((struct instance_data *)ptr)->hdatac; - break; case HPDT_MOBDB: - ret->HPDataSRCPtr = (void**)(&((struct mob_db *)ptr)->hdata); - ret->hdatac = &((struct mob_db *)ptr)->hdatac; - break; case HPDT_MOBDATA: - ret->HPDataSRCPtr = (void**)(&((struct mob_data *)ptr)->hdata); - ret->hdatac = &((struct mob_data *)ptr)->hdatac; - break; case HPDT_ITEMDATA: - ret->HPDataSRCPtr = (void**)(&((struct item_data *)ptr)->hdata); - ret->hdatac = &((struct item_data *)ptr)->hdatac; - break; case HPDT_BGDATA: - ret->HPDataSRCPtr = (void**)(&((struct battleground_data *)ptr)->hdata); - ret->hdatac = &((struct battleground_data *)ptr)->hdatac; - break; case HPDT_AUTOTRADE_VEND: - ret->HPDataSRCPtr = (void**)(&((struct autotrade_vending *)ptr)->hdata); - ret->hdatac = &((struct autotrade_vending *)ptr)->hdatac; - break; + if (!*store) { + *store = HPM->data_store_create(); + } + return true; default: - return false; + break; } - return true; + return false; } void HPM_map_plugin_load_sub(struct hplugin *plugin) { @@ -188,7 +164,7 @@ void HPM_map_add_group_permission(unsigned int pluginID, char *name, unsigned in void HPM_map_do_init(void) { HPM->load_sub = HPM_map_plugin_load_sub; - HPM->grabHPDataSub = HPM_map_grabHPData; + HPM->data_store_validate_sub = HPM_map_data_store_validate; HPM->datacheck_init(HPMDataCheck, HPMDataCheckLen, HPMDataCheckVer); HPM_shared_symbols(SERVER_TYPE_MAP); } diff --git a/src/map/HPMmap.h b/src/map/HPMmap.h index 00a8f43c3..10bdf0ba7 100644 --- a/src/map/HPMmap.h +++ b/src/map/HPMmap.h @@ -15,7 +15,7 @@ struct hplugin; struct map_session_data; -bool HPM_map_grabHPData(struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr); +bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store); bool HPM_map_add_atcommand(char *name, AtCommandFunc func); void HPM_map_atcommands(void); diff --git a/src/map/battleground.c b/src/map/battleground.c index 5df05d301..46d695272 100644 --- a/src/map/battleground.c +++ b/src/map/battleground.c @@ -880,17 +880,10 @@ void do_init_battleground(bool minimal) { */ int bg_team_db_final(DBKey key, DBData *data, va_list ap) { struct battleground_data* bgd = DB->data2ptr(data); - int i; - nullpo_ret(bgd); - for(i = 0; i < bgd->hdatac; i++ ) { - if( bgd->hdata[i]->flag.free ) { - aFree(bgd->hdata[i]->data); - } - aFree(bgd->hdata[i]); - } - if( bgd->hdata ) - aFree(bgd->hdata); - + + HPM->data_store_destroy(bgd->hdata); + bgd->hdata = NULL; + return 0; } diff --git a/src/map/battleground.h b/src/map/battleground.h index 094037f43..0280407a2 100644 --- a/src/map/battleground.h +++ b/src/map/battleground.h @@ -10,7 +10,7 @@ #include "common/db.h" #include "common/mmo.h" // struct party -struct HPluginData; +struct hplugin_data_store; struct block_list; struct map_session_data; @@ -54,8 +54,7 @@ struct battleground_data { char logout_event[EVENT_NAME_LENGTH]; char die_event[EVENT_NAME_LENGTH]; /* HPM Custom Struct */ - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; struct bg_arena { diff --git a/src/map/guild.c b/src/map/guild.c index 7a187b625..2dca97502 100644 --- a/src/map/guild.c +++ b/src/map/guild.c @@ -1763,16 +1763,8 @@ int guild_broken(int guild_id,int flag) if( g->instance ) aFree(g->instance); - if( g->hdata ) - { - for( i = 0; i < g->hdatac; i++ ) { - if( g->hdata[i]->flag.free ) { - aFree(g->hdata[i]->data); - } - aFree(g->hdata[i]); - } - aFree(g->hdata); - } + HPM->data_store_destroy(g->hdata); + g->hdata = NULL; idb_remove(guild->db,guild_id); return 0; @@ -2250,7 +2242,6 @@ void do_init_guild(bool minimal) { void do_final_guild(void) { DBIterator *iter = db_iterator(guild->db); struct guild *g; - int i; for( g = dbi_first(iter); dbi_exists(iter); g = dbi_next(iter) ) { if( g->channel != NULL ) @@ -2259,16 +2250,8 @@ void do_final_guild(void) { aFree(g->instance); g->instance = NULL; } - if( g->hdata ) - { - for( i = 0; i < g->hdatac; i++ ) { - if( g->hdata[i]->flag.free ) { - aFree(g->hdata[i]->data); - } - aFree(g->hdata[i]); - } - aFree(g->hdata); - } + HPM->data_store_destroy(g->hdata); + g->hdata = NULL; } dbi_destroy(iter); diff --git a/src/map/instance.c b/src/map/instance.c index 300247fe7..7213c43a1 100644 --- a/src/map/instance.c +++ b/src/map/instance.c @@ -588,19 +588,8 @@ void instance_destroy(int instance_id) { instance->list[instance_id].state = INSTANCE_FREE; instance->list[instance_id].num_map = 0; - if (instance->list[instance_id].hdata) - { - for( j = 0; j < instance->list[instance_id].hdatac; j++ ) { - if( instance->list[instance_id].hdata[j]->flag.free ) { - aFree(instance->list[instance_id].hdata[j]->data); - } - aFree(instance->list[instance_id].hdata[j]); - } - aFree(instance->list[instance_id].hdata); - } - + HPM->data_store_destroy(instance->list[instance_id].hdata); instance->list[instance_id].hdata = NULL; - instance->list[instance_id].hdatac = 0; } /*-------------------------------------- diff --git a/src/map/instance.h b/src/map/instance.h index 589e1a511..f879195ef 100644 --- a/src/map/instance.h +++ b/src/map/instance.h @@ -9,7 +9,7 @@ #include "common/hercules.h" #include "common/mmo.h" // struct point -struct HPluginData; +struct hplugin_data_store; struct block_list; struct map_session_data; @@ -54,8 +54,7 @@ struct instance_data { struct point respawn; ///< reload spawn /** HPM Custom Struct */ - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; struct instance_interface { diff --git a/src/map/itemdb.c b/src/map/itemdb.c index ccedee72a..c531a4d7a 100644 --- a/src/map/itemdb.c +++ b/src/map/itemdb.c @@ -2100,17 +2100,8 @@ void destroy_item_data(struct item_data* self, int free_self) script->free_code(self->unequip_script); if( self->combos ) aFree(self->combos); - if (self->hdata) - { - int i; - for (i = 0; i < self->hdatac; i++ ) { - if (self->hdata[i]->flag.free ) { - aFree(self->hdata[i]->data); - } - aFree(self->hdata[i]); - } - aFree(self->hdata); - } + HPM->data_store_destroy(self->hdata); + self->hdata = NULL; #if defined(DEBUG) // trash item memset(self, 0xDD, sizeof(struct item_data)); diff --git a/src/map/itemdb.h b/src/map/itemdb.h index a3edd451e..de9770aab 100644 --- a/src/map/itemdb.h +++ b/src/map/itemdb.h @@ -13,6 +13,7 @@ #include "common/sql.h" struct script_code; +struct hplugin_data_store; /** * Defines @@ -490,8 +491,7 @@ struct item_data { struct item_package *package; /* HPM Custom Struct */ - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; #define itemdb_name(n) (itemdb->search(n)->name) diff --git a/src/map/map.c b/src/map/map.c index e01a25366..0ed7072d2 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -3239,16 +3239,8 @@ void do_final_maps(void) { if( map->list[i].qi_data ) aFree(map->list[i].qi_data); - if( map->list[i].hdata ) - { - for( v = 0; v < map->list[i].hdatac; v++ ) { - if( map->list[i].hdata[v]->flag.free ) { - aFree(map->list[i].hdata[v]->data); - } - aFree(map->list[i].hdata[v]); - } - aFree(map->list[i].hdata); - } + HPM->data_store_destroy(map->list[i].hdata); + map->list[i].hdata = NULL; } map->zone_db_clear(); diff --git a/src/map/map.h b/src/map/map.h index 3ac39e54f..c3e426bb6 100644 --- a/src/map/map.h +++ b/src/map/map.h @@ -19,6 +19,7 @@ struct mob_data; struct npc_data; struct channel_data; +struct hplugin_data_store; enum E_MAPSERVER_ST { MAPSERVER_ST_RUNNING = CORE_ST_LAST, @@ -738,8 +739,7 @@ struct map_data { unsigned short hpmeter_visible; /* HPM Custom Struct */ - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; /// Stores information about a remote map (for multi-mapserver setups). diff --git a/src/map/mob.c b/src/map/mob.c index 2fe9fe8fb..174d2e49f 100644 --- a/src/map/mob.c +++ b/src/map/mob.c @@ -4708,16 +4708,8 @@ int do_init_mob(bool minimal) { void mob_destroy_mob_db(int index) { struct mob_db *data = mob->db_data[index]; - if (data->hdata) { - int i; - for (i = 0; i < data->hdatac; i++) { - if (data->hdata[i]->flag.free ) { - aFree(data->hdata[i]->data); - } - aFree(data->hdata[i]); - } - aFree(data->hdata); - } + HPM->data_store_destroy(data->hdata); + data->hdata = NULL; aFree(data); mob->db_data[index] = NULL; } diff --git a/src/map/mob.h b/src/map/mob.h index 4b8a054b5..1142502f4 100644 --- a/src/map/mob.h +++ b/src/map/mob.h @@ -11,6 +11,8 @@ #include "common/hercules.h" #include "common/mmo.h" // struct item +struct hplugin_data_store; + #define MAX_RANDOMMONSTER 5 // Change this to increase the table size in your mob_db to accommodate a larger mob database. @@ -140,8 +142,7 @@ struct mob_db { struct spawn_info spawn[10]; /* HPM Custom Struct */ - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; struct mob_data { @@ -210,12 +211,10 @@ struct mob_data { int tomb_nid; /* HPM Custom Struct */ - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; - enum { MST_TARGET = 0, MST_RANDOM, //Random Target! diff --git a/src/map/npc.c b/src/map/npc.c index a0c14a058..85de5301b 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -2321,16 +2321,8 @@ int npc_unload(struct npc_data* nd, bool single) nd->ud = NULL; } - if (nd->hdata) { - unsigned int i; - for (i = 0; i < nd->hdatac; i++) { - if (nd->hdata[i]->flag.free) { - aFree(nd->hdata[i]->data); - } - aFree(nd->hdata[i]); - } - aFree(nd->hdata); - } + HPM->data_store_destroy(nd->hdata); + nd->hdata = NULL; aFree(nd); diff --git a/src/map/npc.h b/src/map/npc.h index 14b89d128..06aee3f93 100644 --- a/src/map/npc.h +++ b/src/map/npc.h @@ -11,7 +11,7 @@ #include "common/hercules.h" #include "common/db.h" -struct HPluginData; +struct hplugin_data_store; struct view_data; enum npc_parse_options { @@ -103,8 +103,7 @@ struct npc_data { } tomb; } u; /* HPData Support for npc_data */ - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; diff --git a/src/map/party.c b/src/map/party.c index db285a4b4..4735dfc42 100644 --- a/src/map/party.c +++ b/src/map/party.c @@ -103,16 +103,8 @@ int party_db_final(DBKey key, DBData *data, va_list ap) { if (p->instance) aFree(p->instance); - if (p->hdata) { - int i; - for (i = 0; i < p->hdatac; i++) { - if (p->hdata[i]->flag.free) { - aFree(p->hdata[i]->data); - } - aFree(p->hdata[i]); - } - aFree(p->hdata); - } + HPM->data_store_destroy(p->hdata); + p->hdata = NULL; } return 0; } @@ -608,16 +600,9 @@ int party_broken(int party_id) if( p->instance ) aFree(p->instance); - if( p->hdata ) - { - for( j = 0; j < p->hdatac; j++ ) { - if( p->hdata[j]->flag.free ) { - aFree(p->hdata[j]->data); - } - aFree(p->hdata[j]); - } - aFree(p->hdata); - } + HPM->data_store_destroy(p->hdata); + p->hdata = NULL; + idb_remove(party->db,party_id); return 0; } diff --git a/src/map/party.h b/src/map/party.h index c7893add2..960e6da08 100644 --- a/src/map/party.h +++ b/src/map/party.h @@ -15,7 +15,7 @@ #define PARTY_BOOKING_JOBS 6 #define PARTY_BOOKING_RESULTS 10 -struct HPluginData; +struct hplugin_data_store; struct party_member_data { struct map_session_data *sd; @@ -37,8 +37,7 @@ struct party_data { } state; /* HPM Custom Struct */ - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; #define PB_NOTICE_LENGTH (36 + 1) diff --git a/src/map/pc.c b/src/map/pc.c index dc7014701..fee41deff 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -11332,14 +11332,8 @@ void pc_autotrade_populate(struct map_session_data *sd) { pc->autotrade_update(sd,PAUC_START); - for(i = 0; i < data->hdatac; i++ ) { - if( data->hdata[i]->flag.free ) { - aFree(data->hdata[i]->data); - } - aFree(data->hdata[i]); - } - if( data->hdata ) - aFree(data->hdata); + HPM->data_store_destroy(data->hdata); + data->hdata = NULL; idb_remove(pc->at_db, sd->status.char_id); } @@ -11349,16 +11343,8 @@ void pc_autotrade_populate(struct map_session_data *sd) { */ int pc_autotrade_final(DBKey key, DBData *data, va_list ap) { struct autotrade_vending* at_v = DB->data2ptr(data); - int i; - for(i = 0; i < at_v->hdatac; i++ ) { - if( at_v->hdata[i]->flag.free ) { - aFree(at_v->hdata[i]->data); - } - aFree(at_v->hdata[i]); - } - if( at_v->hdata ) - aFree(at_v->hdata); - + HPM->data_store_destroy(at_v->hdata); + at_v->hdata = NULL; return 0; } diff --git a/src/map/pc.h b/src/map/pc.h index e6e95978d..a74bc6c80 100644 --- a/src/map/pc.h +++ b/src/map/pc.h @@ -541,8 +541,7 @@ END_ZEROED_BLOCK; unsigned char delayed_damage;//ref. counter bugreport:7307 [Ind/Hercules] /* HPM Custom Struct */ - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; /* expiration_time timer id */ int expiration_tid; @@ -758,8 +757,7 @@ struct autotrade_vending { struct s_vending vending[MAX_VENDING]; unsigned char vend_num; /* HPM Custom Struct */ - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; /*===================================== diff --git a/src/map/unit.c b/src/map/unit.c index 04a8befc2..0f8a9d7e6 100644 --- a/src/map/unit.c +++ b/src/map/unit.c @@ -2655,16 +2655,9 @@ int unit_free(struct block_list *bl, clr_type clrtype) { sd->num_quests = sd->avail_quests = 0; } - if (sd->hdata) { - unsigned int k; - for( k = 0; k < sd->hdatac; k++ ) { - if( sd->hdata[k]->flag.free ) { - aFree(sd->hdata[k]->data); - } - aFree(sd->hdata[k]); - } - aFree(sd->hdata); - } + HPM->data_store_destroy(sd->hdata); + sd->hdata = NULL; + break; } case BL_PET: @@ -2775,17 +2768,9 @@ int unit_free(struct block_list *bl, clr_type clrtype) { if( md->tomb_nid ) mob->mvptomb_destroy(md); - if (md->hdata) - { - unsigned int k; - for (k = 0; k < md->hdatac; k++) { - if( md->hdata[k]->flag.free ) { - aFree(md->hdata[k]->data); - } - aFree(md->hdata[k]); - } - aFree(md->hdata); - } + HPM->data_store_destroy(md->hdata); + md->hdata = NULL; + break; } case BL_HOM: -- cgit v1.2.3-70-g09d2 From 657e7f9c07bc1445ce785cd11772664a1848ea5a Mon Sep 17 00:00:00 2001 From: Haru Date: Sat, 26 Sep 2015 04:11:41 +0200 Subject: Cleanup of the HPluginData implementation (second part) - Changed the hplugin_data_store's array into a VECTOR. Signed-off-by: Haru --- src/char/HPMchar.c | 3 +- src/char/HPMchar.h | 2 +- src/common/HPM.c | 219 ++++++++++++++++++++++++++++++------------------- src/common/HPM.h | 26 +++--- src/common/HPMi.h | 106 ++++++++++++------------ src/common/mmo.h | 4 +- src/common/socket.c | 3 +- src/common/socket.h | 3 +- src/login/HPMlogin.c | 3 +- src/login/HPMlogin.h | 2 +- src/map/HPMmap.c | 6 +- src/map/HPMmap.h | 2 +- src/map/battleground.c | 3 +- src/map/battleground.h | 3 +- src/map/guild.c | 6 +- src/map/instance.c | 3 +- src/map/instance.h | 4 +- src/map/itemdb.c | 3 +- src/map/itemdb.h | 4 +- src/map/map.c | 3 +- src/map/map.h | 4 +- src/map/mob.c | 3 +- src/map/mob.h | 8 +- src/map/npc.c | 3 +- src/map/npc.h | 3 +- src/map/party.c | 6 +- src/map/party.h | 4 +- src/map/pc.c | 6 +- src/map/pc.h | 7 +- src/map/unit.c | 9 +- 30 files changed, 242 insertions(+), 219 deletions(-) (limited to 'src/common/HPM.c') diff --git a/src/char/HPMchar.c b/src/char/HPMchar.c index 14d7be1a3..d3150bc11 100644 --- a/src/char/HPMchar.c +++ b/src/char/HPMchar.c @@ -52,9 +52,10 @@ * * @see HPM_interface::data_store_validate */ -bool HPM_char_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) +bool HPM_char_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize) { switch (type) { + // No supported types at the moment. default: break; } diff --git a/src/char/HPMchar.h b/src/char/HPMchar.h index fd07f45ed..431017b7a 100644 --- a/src/char/HPMchar.h +++ b/src/char/HPMchar.h @@ -13,7 +13,7 @@ struct hplugin; -bool HPM_char_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store); +bool HPM_char_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize); void HPM_char_plugin_load_sub(struct hplugin *plugin); diff --git a/src/common/HPM.c b/src/common/HPM.c index ce3d769ca..578e90bbc 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -168,120 +168,158 @@ bool hplugins_addpacket(unsigned short cmd, unsigned short length, void (*receiv return true; } -bool hplugin_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) +/** + * Validates and if necessary initializes a plugin data store. + * + * @param type[in] The data store type. + * @param storeptr[in,out] A pointer to the store. + * @param initialize Whether the store should be initialized in case it isn't. + * @retval false if the store is an invalid or mismatching type. + * @retval true if the store is a valid type. + * + * @remark + * If \c storeptr is a pointer to a NULL pointer (\c storeptr itself can't + * be NULL), two things may happen, depending on the \c initialize value: + * if false, then \c storeptr isn't changed; if true, then \c storeptr is + * initialized through \c HPM->data_store_create() and ownership is passed + * to the caller. + */ +bool hplugin_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize) { - nullpo_retr(false, store); + struct hplugin_data_store *store; + nullpo_retr(false, storeptr); + store = *storeptr; + if (!initialize && store == NULL) + return true; switch (type) { /* core-handled */ case HPDT_SESSION: - if (!*store) { - *store = HPM->data_store_create(); - } - return true; - /* goes to sub */ + break; default: + if (HPM->data_store_validate_sub == NULL) { + ShowError("HPM:validateHPData failed, type %d needs sub-handler!\n",type); + return false; + } + if (!HPM->data_store_validate_sub(type, storeptr, initialize)) { + ShowError("HPM:HPM:validateHPData failed, unknown type %d!\n",type); + return false; + } break; } - if (HPM->data_store_validate_sub) { - if (HPM->data_store_validate_sub(type, store)) - return true; - ShowError("HPM:HPM:validateHPData failed, unknown type %d!\n",type); - } else { - ShowError("HPM:validateHPData failed, type %d needs sub-handler!\n",type); + if (initialize && (!store || store->type == HPDT_UNKNOWN)) { + HPM->data_store_create(storeptr, type); + store = *storeptr; } - return false; + if (store->type != type) { + ShowError("HPM:HPM:validateHPData failed, store type mismatch %d != %d.\n",store->type, type); + return false; + } + return true; } -void hplugins_addToHPData(enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, void *data, unsigned int index, bool autofree) +/** + * Adds an entry to a plugin data store. + * + * @param type[in] The store type. + * @param pluginID[in] The plugin identifier. + * @param storeptr[in,out] A pointer to the store. The store will be initialized if necessary. + * @param data[in] The data entry to add. + * @param classid[in] The entry class identifier. + * @param autofree[in] Whether the entry should be automatically freed when removed. + */ +void hplugins_addToHPData(enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store **storeptr, void *data, uint32 classid, bool autofree) { - struct hplugin_data_entry *HPData; - unsigned int i; struct hplugin_data_store *store; + struct hplugin_data_entry *entry; + int i; + nullpo_retv(storeptr); - if (!HPM->data_store_validate(type, storeptr)) { + if (!HPM->data_store_validate(type, storeptr, true)) { /* woo it failed! */ - ShowError("HPM:addToHPData:%s: failed, type %d (%u|%u)\n",HPM->pid2name(pluginID),type,pluginID,index); + ShowError("HPM:addToHPData:%s: failed, type %d (%u|%u)\n", HPM->pid2name(pluginID), type, pluginID, classid); return; } store = *storeptr; /* duplicate check */ - for (i = 0; i < store->count; i++) { - if (store->array[i]->pluginID == pluginID && store->array[i]->type == index) { - ShowError("HPM:addToHPData:%s: error! attempting to insert duplicate struct of id %u and index %u\n",HPM->pid2name(pluginID),pluginID,index); - return; - } + ARR_FIND(0, VECTOR_LENGTH(store->entries), i, VECTOR_INDEX(store->entries, i)->pluginID == pluginID && VECTOR_INDEX(store->entries, i)->classid == classid); + if (i != VECTOR_LENGTH(store->entries)) { + ShowError("HPM:addToHPData:%s: error! attempting to insert duplicate struct of id %u and classid %u\n", HPM->pid2name(pluginID), pluginID, classid); + return; } /* hplugin_data_entry is always same size, probably better to use the ERS (with reasonable chunk size e.g. 10/25/50) */ - CREATE(HPData, struct hplugin_data_entry, 1); + CREATE(entry, struct hplugin_data_entry, 1); /* input */ - HPData->pluginID = pluginID; - HPData->type = index; - HPData->flag.free = autofree ? 1 : 0; - HPData->data = data; - - /* resize */ - RECREATE(store->array, struct hplugin_data_entry *, ++store->count); + entry->pluginID = pluginID; + entry->classid = classid; + entry->flag.free = autofree ? 1 : 0; + entry->data = data; - store->array[store->count - 1] = HPData; + VECTOR_ENSURE(store->entries, 1, 1); + VECTOR_PUSH(store->entries, entry); } -void *hplugins_getFromHPData(enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index) +/** + * Retrieves an entry from a plugin data store. + * + * @param type[in] The store type. + * @param pluginID[in] The plugin identifier. + * @param store[in] The store. + * @param classid[in] The entry class identifier. + * + * @return The retrieved entry, or NULL. + */ +void *hplugins_getFromHPData(enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store *store, uint32 classid) { - unsigned int i; - struct hplugin_data_store *store; + int i; - if (!HPM->data_store_validate(type, storeptr)) { + if (!HPM->data_store_validate(type, &store, false)) { /* woo it failed! */ - ShowError("HPM:getFromHPData:%s: failed, type %d (%u|%u)\n",HPM->pid2name(pluginID),type,pluginID,index); + ShowError("HPM:getFromHPData:%s: failed, type %d (%u|%u)\n", HPM->pid2name(pluginID), type, pluginID, classid); return NULL; } - store = *storeptr; + if (!store) + return NULL; - for (i = 0; i < store->count; i++) { - if (store->array[i]->pluginID == pluginID && store->array[i]->type == index) - return store->array[i]->data; - } + ARR_FIND(0, VECTOR_LENGTH(store->entries), i, VECTOR_INDEX(store->entries, i)->pluginID == pluginID && VECTOR_INDEX(store->entries, i)->classid == classid); + if (i != VECTOR_LENGTH(store->entries)) + return VECTOR_INDEX(store->entries, i)->data; return NULL; } -void hplugins_removeFromHPData(enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index) +/** + * Removes an entry from a plugin data store. + * + * @param type[in] The store type. + * @param pluginID[in] The plugin identifier. + * @param store[in] The store. + * @param classid[in] The entry class identifier. + */ +void hplugins_removeFromHPData(enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store *store, uint32 classid) { - unsigned int i; - struct hplugin_data_store *store; + struct hplugin_data_entry *entry; + int i; - if (!HPM->data_store_validate(type, storeptr)) { + if (!HPM->data_store_validate(type, &store, false)) { /* woo it failed! */ - ShowError("HPM:removeFromHPData:%s: failed, type %d (%u|%u)\n",HPM->pid2name(pluginID),type,pluginID,index); + ShowError("HPM:removeFromHPData:%s: failed, type %d (%u|%u)\n", HPM->pid2name(pluginID), type, pluginID, classid); return; } - store = *storeptr; - - for (i = 0; i < store->count; i++) { - if (store->array[i]->pluginID == pluginID && store->array[i]->type == index) - break; - } - - if (i != store->count) { - unsigned int cursor; + if (!store) + return; - aFree(store->array[i]->data);/* when its removed we delete it regardless of autofree */ - aFree(store->array[i]); - store->array[i] = NULL; + ARR_FIND(0, VECTOR_LENGTH(store->entries), i, VECTOR_INDEX(store->entries, i)->pluginID == pluginID && VECTOR_INDEX(store->entries, i)->classid == classid); + if (i == VECTOR_LENGTH(store->entries)) + return; - for (i = 0, cursor = 0; i < store->count; i++) { - if (store->array[i] == NULL) - continue; - if (i != cursor) - store->array[cursor] = store->array[i]; - cursor++; - } - store->count = cursor; - } + entry = VECTOR_INDEX(store->entries, i); + VECTOR_ERASE(store->entries, i); // Erase and compact + aFree(entry->data); // when it's removed we delete it regardless of autofree + aFree(entry); } /* TODO: add ability for tracking using pID for the upcoming runtime load/unload support. */ @@ -765,25 +803,30 @@ bool hplugins_parse_conf(const char *w1, const char *w2, enum HPluginConfType po } /** - * Helper to destroy and release an interface's hplugin_data store. + * Helper to destroy an interface's hplugin_data store and release any owned memory. * - * @param hdata The hplugin_data store. The pointer will be freed. + * The pointer will be cleared. + * + * @param storeptr[in,out] A pointer to the plugin data store. */ -void hplugin_data_store_destroy(struct hplugin_data_store *store) +void hplugin_data_store_destroy(struct hplugin_data_store **storeptr) { - unsigned int i; - - if (!store) + struct hplugin_data_store *store; + nullpo_retv(storeptr); + store = *storeptr; + if (store == NULL) return; - for (i = 0; i < store->count; i++) { - if (store->array[i]->flag.free) { - aFree(store->array[i]->data); + while (VECTOR_LENGTH(store->entries) > 0) { + struct hplugin_data_entry *entry = VECTOR_POP(store->entries); + if (entry->flag.free) { + aFree(entry->data); } - aFree(store->array[i]); + aFree(entry); } - aFree(store->array); + VECTOR_CLEAR(store->entries); aFree(store); + *storeptr = NULL; } /** @@ -792,13 +835,21 @@ void hplugin_data_store_destroy(struct hplugin_data_store *store) * The store is owned by the caller, and it should be eventually destroyed by * \c hdata_destroy. * - * @return An initialized hplugin_data store. + * @param storeptr[in,out] A pointer to the data store to initialize. + * @param type[in] The store type. */ -struct hplugin_data_store *hplugin_data_store_create(void) +void hplugin_data_store_create(struct hplugin_data_store **storeptr, enum HPluginDataTypes type) { struct hplugin_data_store *store; - CREATE(store, struct hplugin_data_store, 1); - return store; + nullpo_retv(storeptr); + + if (*storeptr == NULL) { + CREATE(*storeptr, struct hplugin_data_store, 1); + } + store = *storeptr; + + store->type = type; + VECTOR_INIT(store->entries); } /** diff --git a/src/common/HPM.h b/src/common/HPM.h index 1d13a178f..5420e5300 100644 --- a/src/common/HPM.h +++ b/src/common/HPM.h @@ -65,18 +65,24 @@ struct hpm_symbol { void *ptr; ///< The symbol value }; +/** + * A plugin custom data, to be injected in various interfaces and objects. + */ struct hplugin_data_entry { - unsigned int pluginID; - unsigned int type; + uint32 pluginID; ///< The owner plugin identifier. + uint32 classid; ///< The entry's object type, managed by the plugin (for plugins that need more than one entry). struct { - unsigned int free : 1; + unsigned int free : 1; ///< Whether the entry data should be automatically cleared by the HPM. } flag; - void *data; + void *data; ///< The entry data. }; +/** + * A store for plugin custom data entries. + */ struct hplugin_data_store { - struct hplugin_data_entry **array; - unsigned int count; + enum HPluginDataTypes type; ///< The store type. + VECTOR_DECL(struct hplugin_data_entry *) entries; ///< The store entries. }; struct HPluginPacket { @@ -144,11 +150,11 @@ struct HPM_interface { void (*datacheck_init) (const struct s_HPMDataCheck *src, unsigned int length, int version); void (*datacheck_final) (void); - struct hplugin_data_store *(*data_store_create) (void); - void (*data_store_destroy) (struct hplugin_data_store *store); - bool (*data_store_validate) (enum HPluginDataTypes type, struct hplugin_data_store **store); + void (*data_store_create) (struct hplugin_data_store **storeptr, enum HPluginDataTypes type); + void (*data_store_destroy) (struct hplugin_data_store **storeptr); + bool (*data_store_validate) (enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize); /* for server-specific HPData e.g. map_session_data */ - bool (*data_store_validate_sub) (enum HPluginDataTypes type, struct hplugin_data_store **store); + bool (*data_store_validate_sub) (enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize); }; CMDLINEARG(loadplugin); diff --git a/src/common/HPMi.h b/src/common/HPMi.h index d92503a91..9a61dd256 100644 --- a/src/common/HPMi.h +++ b/src/common/HPMi.h @@ -60,19 +60,23 @@ enum HPluginHookType { HOOK_TYPE_POST, }; +/** + * Data types for plugin custom data. + */ enum HPluginDataTypes { - HPDT_SESSION, - HPDT_MSD, - HPDT_NPCD, - HPDT_MAP, - HPDT_INSTANCE, - HPDT_GUILD, - HPDT_PARTY, - HPDT_MOBDB, - HPDT_MOBDATA, - HPDT_ITEMDATA, - HPDT_BGDATA, - HPDT_AUTOTRADE_VEND, + HPDT_UNKNOWN, ///< Unknown type (such as an uninitialized store). + HPDT_SESSION, ///< For struct socket_data. + HPDT_MSD, ///< For struct map_session_data. + HPDT_NPCD, ///< For struct npc_data. + HPDT_MAP, ///< For struct map_data. + HPDT_INSTANCE, ///< For struct instance_data. + HPDT_GUILD, ///< For struct guild. + HPDT_PARTY, ///< For struct party_data. + HPDT_MOBDB, ///< For struct mob_db. + HPDT_MOBDATA, ///< For struct mob_data. + HPDT_ITEMDATA, ///< For struct item_data. + HPDT_BGDATA, ///< For struct battleground_data. + HPDT_AUTOTRADE_VEND, ///< For struct autotrade_vending. }; /* used in macros and conf storage */ @@ -97,53 +101,53 @@ enum HPluginConfType { #define addArg(name, param,func,help) (HPMi->addArg(HPMi->pid,(name),(param),(cmdline_arg_ ## func),(help))) /* HPData handy redirects */ /* session[] */ -#define addToSession(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_SESSION,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromSession(ptr,index) (HPMi->getFromHPData(HPDT_SESSION,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromSession(ptr,index) (HPMi->removeFromHPData(HPDT_SESSION,HPMi->pid,&(ptr)->hdata,(index))) +#define addToSession(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_SESSION,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromSession(ptr,classid) (HPMi->getFromHPData(HPDT_SESSION,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromSession(ptr,classid) (HPMi->removeFromHPData(HPDT_SESSION,HPMi->pid,(ptr)->hdata,(classid))) /* map_session_data */ -#define addToMSD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MSD,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromMSD(ptr,index) (HPMi->getFromHPData(HPDT_MSD,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromMSD(ptr,index) (HPMi->removeFromHPData(HPDT_MSD,HPMi->pid,&(ptr)->hdata,(index))) +#define addToMSD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_MSD,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromMSD(ptr,classid) (HPMi->getFromHPData(HPDT_MSD,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromMSD(ptr,classid) (HPMi->removeFromHPData(HPDT_MSD,HPMi->pid,(ptr)->hdata,(classid))) /* npc_data */ -#define addToNPCD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_NPCD,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromNPCD(ptr,index) (HPMi->getFromHPData(HPDT_NPCD,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromNPCD(ptr,index) (HPMi->removeFromHPData(HPDT_NPCD,HPMi->pid,&(ptr)->hdata,(index))) +#define addToNPCD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_NPCD,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromNPCD(ptr,classid) (HPMi->getFromHPData(HPDT_NPCD,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromNPCD(ptr,classid) (HPMi->removeFromHPData(HPDT_NPCD,HPMi->pid,(ptr)->hdata,(classid))) /* map_data */ -#define addToMAPD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MAP,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromMAPD(ptr,index) (HPMi->getFromHPData(HPDT_MAP,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromMAPD(ptr,index) (HPMi->removeFromHPData(HPDT_MAP,HPMi->pid,&(ptr)->hdata,(index))) +#define addToMAPD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_MAP,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromMAPD(ptr,classid) (HPMi->getFromHPData(HPDT_MAP,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromMAPD(ptr,classid) (HPMi->removeFromHPData(HPDT_MAP,HPMi->pid,(ptr)->hdata,(classid))) /* party_data */ -#define addToPAD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_PARTY,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromPAD(ptr,index) (HPMi->getFromHPData(HPDT_PARTY,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromPAD(ptr,index) (HPMi->removeFromHPData(HPDT_PARTY,HPMi->pid,&(ptr)->hdata,(index))) +#define addToPAD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_PARTY,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromPAD(ptr,classid) (HPMi->getFromHPData(HPDT_PARTY,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromPAD(ptr,classid) (HPMi->removeFromHPData(HPDT_PARTY,HPMi->pid,(ptr)->hdata,(classid))) /* guild */ -#define addToGLD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_GUILD,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromGLD(ptr,index) (HPMi->getFromHPData(HPDT_GUILD,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromGLD(ptr,index) (HPMi->removeFromHPData(HPDT_GUILD,HPMi->pid,&(ptr)->hdata,(index))) +#define addToGLD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_GUILD,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromGLD(ptr,classid) (HPMi->getFromHPData(HPDT_GUILD,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromGLD(ptr,classid) (HPMi->removeFromHPData(HPDT_GUILD,HPMi->pid,(ptr)->hdata,(classid))) /* instance_data */ -#define addToINSTD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_INSTANCE,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromINSTD(ptr,index) (HPMi->getFromHPData(HPDT_INSTANCE,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromINSTD(ptr,index) (HPMi->removeFromHPData(HPDT_INSTANCE,HPMi->pid,&(ptr)->hdata,(index))) +#define addToINSTD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_INSTANCE,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromINSTD(ptr,classid) (HPMi->getFromHPData(HPDT_INSTANCE,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromINSTD(ptr,classid) (HPMi->removeFromHPData(HPDT_INSTANCE,HPMi->pid,(ptr)->hdata,(classid))) /* mob_db */ -#define addToMOBDB(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MOBDB,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromMOBDB(ptr,index) (HPMi->getFromHPData(HPDT_MOBDB,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromMOBDB(ptr,index) (HPMi->removeFromHPData(HPDT_MOBDB,HPMi->pid,&(ptr)->hdata,(index))) +#define addToMOBDB(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_MOBDB,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromMOBDB(ptr,classid) (HPMi->getFromHPData(HPDT_MOBDB,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromMOBDB(ptr,classid) (HPMi->removeFromHPData(HPDT_MOBDB,HPMi->pid,(ptr)->hdata,(classid))) /* mob_data */ -#define addToMOBDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MOBDATA,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromMOBDATA(ptr,index) (HPMi->getFromHPData(HPDT_MOBDATA,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromMOBDATA(ptr,index) (HPMi->removeFromHPData(HPDT_MOBDATA,HPMi->pid,&(ptr)->hdata,(index))) +#define addToMOBDATA(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_MOBDATA,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromMOBDATA(ptr,classid) (HPMi->getFromHPData(HPDT_MOBDATA,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromMOBDATA(ptr,classid) (HPMi->removeFromHPData(HPDT_MOBDATA,HPMi->pid,(ptr)->hdata,(classid))) /* item_data */ -#define addToITEMDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_ITEMDATA,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromITEMDATA(ptr,index) (HPMi->getFromHPData(HPDT_ITEMDATA,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromITEMDATA(ptr,index) (HPMi->removeFromHPData(HPDT_ITEMDATA,HPMi->pid,&(ptr)->hdata,(index))) +#define addToITEMDATA(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_ITEMDATA,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromITEMDATA(ptr,classid) (HPMi->getFromHPData(HPDT_ITEMDATA,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromITEMDATA(ptr,classid) (HPMi->removeFromHPData(HPDT_ITEMDATA,HPMi->pid,(ptr)->hdata,(classid))) /* battleground_data */ -#define addToBGDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_BGDATA,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromBGDATA(ptr,index) (HPMi->getFromHPData(HPDT_BGDATA,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromBGDATA(ptr,index) (HPMi->removeFromHPData(HPDT_BGDATA,HPMi->pid,&(ptr)->hdata,(index))) +#define addToBGDATA(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_BGDATA,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromBGDATA(ptr,classid) (HPMi->getFromHPData(HPDT_BGDATA,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromBGDATA(ptr,classid) (HPMi->removeFromHPData(HPDT_BGDATA,HPMi->pid,(ptr)->hdata,(classid))) /* autotrade_vending */ -#define addToATVEND(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromATVEND(ptr,index) (HPMi->getFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromATVEND(ptr,index) (HPMi->removeFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,&(ptr)->hdata,(index))) +#define addToATVEND(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromATVEND(ptr,classid) (HPMi->getFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromATVEND(ptr,classid) (HPMi->removeFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,(ptr)->hdata,(classid))) /// HPMi->addCommand #define addAtcommand(cname,funcname) do { \ @@ -206,9 +210,9 @@ struct HPMi_interface { bool (*addScript) (char *name, char *args, bool (*func)(struct script_state *st), bool isDeprecated); void (*addCPCommand) (char *name, CParseFunc func); /* HPM Custom Data */ - void (*addToHPData) (enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, void *data, unsigned int index, bool autofree); - void *(*getFromHPData) (enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index); - void (*removeFromHPData) (enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index); + void (*addToHPData) (enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store **storeptr, void *data, uint32 classid, bool autofree); + void *(*getFromHPData) (enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store *store, uint32 classid); + void (*removeFromHPData) (enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store *store, uint32 classid); /* packet */ bool (*addPacket) (unsigned short cmd, unsigned short length, void (*receive)(int fd), unsigned int point, unsigned int pluginID); /* Hooking */ diff --git a/src/common/mmo.h b/src/common/mmo.h index cbda0e91b..73d4510a1 100644 --- a/src/common/mmo.h +++ b/src/common/mmo.h @@ -661,9 +661,7 @@ struct guild { unsigned short instances; struct channel_data *channel; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; struct guild_castle { diff --git a/src/common/socket.c b/src/common/socket.c index 842f9ba87..a26873b67 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -637,8 +637,7 @@ static void delete_session(int fd) aFree(sockt->session[fd]->wdata); if( sockt->session[fd]->session_data ) aFree(sockt->session[fd]->session_data); - HPM->data_store_destroy(sockt->session[fd]->hdata); - sockt->session[fd]->hdata = NULL; + HPM->data_store_destroy(&sockt->session[fd]->hdata); aFree(sockt->session[fd]); sockt->session[fd] = NULL; } diff --git a/src/common/socket.h b/src/common/socket.h index 510eac575..a2dea8b74 100644 --- a/src/common/socket.h +++ b/src/common/socket.h @@ -104,8 +104,7 @@ struct socket_data { ParseFunc func_parse; void* session_data; // stores application-specific data related to the session - - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store. }; struct hSockOpt { diff --git a/src/login/HPMlogin.c b/src/login/HPMlogin.c index ce88728fe..b900f28fe 100644 --- a/src/login/HPMlogin.c +++ b/src/login/HPMlogin.c @@ -37,9 +37,10 @@ * * @see HPM_interface::data_store_validate */ -bool HPM_login_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) +bool HPM_login_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize) { switch (type) { + // No supported types at the moment default: break; } diff --git a/src/login/HPMlogin.h b/src/login/HPMlogin.h index 1f08a8666..35d4bd01c 100644 --- a/src/login/HPMlogin.h +++ b/src/login/HPMlogin.h @@ -13,7 +13,7 @@ struct hplugin; -bool HPM_login_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store); +bool HPM_login_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize); void HPM_login_plugin_load_sub(struct hplugin *plugin); diff --git a/src/map/HPMmap.c b/src/map/HPMmap.c index ddffb5519..edbe74039 100644 --- a/src/map/HPMmap.c +++ b/src/map/HPMmap.c @@ -90,7 +90,7 @@ unsigned int atcommand_list_items = 0; * * @see HPM_interface::data_store_validate */ -bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) +bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize) { switch (type) { case HPDT_MSD: @@ -104,9 +104,7 @@ bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data case HPDT_ITEMDATA: case HPDT_BGDATA: case HPDT_AUTOTRADE_VEND: - if (!*store) { - *store = HPM->data_store_create(); - } + // Initialized by the caller. return true; default: break; diff --git a/src/map/HPMmap.h b/src/map/HPMmap.h index 10bdf0ba7..b1957b139 100644 --- a/src/map/HPMmap.h +++ b/src/map/HPMmap.h @@ -15,7 +15,7 @@ struct hplugin; struct map_session_data; -bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store); +bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize); bool HPM_map_add_atcommand(char *name, AtCommandFunc func); void HPM_map_atcommands(void); diff --git a/src/map/battleground.c b/src/map/battleground.c index 46d695272..be6b7a863 100644 --- a/src/map/battleground.c +++ b/src/map/battleground.c @@ -881,8 +881,7 @@ void do_init_battleground(bool minimal) { int bg_team_db_final(DBKey key, DBData *data, va_list ap) { struct battleground_data* bgd = DB->data2ptr(data); - HPM->data_store_destroy(bgd->hdata); - bgd->hdata = NULL; + HPM->data_store_destroy(&bgd->hdata); return 0; } diff --git a/src/map/battleground.h b/src/map/battleground.h index 0280407a2..dcf92d6d8 100644 --- a/src/map/battleground.h +++ b/src/map/battleground.h @@ -53,8 +53,7 @@ struct battleground_data { // Logout Event char logout_event[EVENT_NAME_LENGTH]; char die_event[EVENT_NAME_LENGTH]; - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; struct bg_arena { diff --git a/src/map/guild.c b/src/map/guild.c index 2dca97502..f8798f821 100644 --- a/src/map/guild.c +++ b/src/map/guild.c @@ -1763,8 +1763,7 @@ int guild_broken(int guild_id,int flag) if( g->instance ) aFree(g->instance); - HPM->data_store_destroy(g->hdata); - g->hdata = NULL; + HPM->data_store_destroy(&g->hdata); idb_remove(guild->db,guild_id); return 0; @@ -2250,8 +2249,7 @@ void do_final_guild(void) { aFree(g->instance); g->instance = NULL; } - HPM->data_store_destroy(g->hdata); - g->hdata = NULL; + HPM->data_store_destroy(&g->hdata); } dbi_destroy(iter); diff --git a/src/map/instance.c b/src/map/instance.c index 7213c43a1..0936a5ace 100644 --- a/src/map/instance.c +++ b/src/map/instance.c @@ -588,8 +588,7 @@ void instance_destroy(int instance_id) { instance->list[instance_id].state = INSTANCE_FREE; instance->list[instance_id].num_map = 0; - HPM->data_store_destroy(instance->list[instance_id].hdata); - instance->list[instance_id].hdata = NULL; + HPM->data_store_destroy(&instance->list[instance_id].hdata); } /*-------------------------------------- diff --git a/src/map/instance.h b/src/map/instance.h index f879195ef..058cd2c3d 100644 --- a/src/map/instance.h +++ b/src/map/instance.h @@ -52,9 +52,7 @@ struct instance_data { unsigned int original_progress_timeout; struct point respawn; ///< reload spawn - - /** HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; struct instance_interface { diff --git a/src/map/itemdb.c b/src/map/itemdb.c index c531a4d7a..ccfff2246 100644 --- a/src/map/itemdb.c +++ b/src/map/itemdb.c @@ -2100,8 +2100,7 @@ void destroy_item_data(struct item_data* self, int free_self) script->free_code(self->unequip_script); if( self->combos ) aFree(self->combos); - HPM->data_store_destroy(self->hdata); - self->hdata = NULL; + HPM->data_store_destroy(&self->hdata); #if defined(DEBUG) // trash item memset(self, 0xDD, sizeof(struct item_data)); diff --git a/src/map/itemdb.h b/src/map/itemdb.h index de9770aab..64b800b87 100644 --- a/src/map/itemdb.h +++ b/src/map/itemdb.h @@ -489,9 +489,7 @@ struct item_data { /* TODO add a pointer to some sort of (struct extra) and gather all the not-common vals into it to save memory */ struct item_group *group; struct item_package *package; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; #define itemdb_name(n) (itemdb->search(n)->name) diff --git a/src/map/map.c b/src/map/map.c index 0ed7072d2..b142ed1f3 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -3239,8 +3239,7 @@ void do_final_maps(void) { if( map->list[i].qi_data ) aFree(map->list[i].qi_data); - HPM->data_store_destroy(map->list[i].hdata); - map->list[i].hdata = NULL; + HPM->data_store_destroy(&map->list[i].hdata); } map->zone_db_clear(); diff --git a/src/map/map.h b/src/map/map.h index c3e426bb6..961a45793 100644 --- a/src/map/map.h +++ b/src/map/map.h @@ -737,9 +737,7 @@ struct map_data { /* speeds up clif_updatestatus processing by causing hpmeter to run only when someone with the permission can view it */ unsigned short hpmeter_visible; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; /// Stores information about a remote map (for multi-mapserver setups). diff --git a/src/map/mob.c b/src/map/mob.c index 174d2e49f..01f585b6f 100644 --- a/src/map/mob.c +++ b/src/map/mob.c @@ -4708,8 +4708,7 @@ int do_init_mob(bool minimal) { void mob_destroy_mob_db(int index) { struct mob_db *data = mob->db_data[index]; - HPM->data_store_destroy(data->hdata); - data->hdata = NULL; + HPM->data_store_destroy(&data->hdata); aFree(data); mob->db_data[index] = NULL; } diff --git a/src/map/mob.h b/src/map/mob.h index 1142502f4..bb26258c6 100644 --- a/src/map/mob.h +++ b/src/map/mob.h @@ -140,9 +140,7 @@ struct mob_db { int maxskill; struct mob_skill skill[MAX_MOBSKILL]; struct spawn_info spawn[10]; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; struct mob_data { @@ -209,9 +207,7 @@ struct mob_data { * MvP Tombstone NPC ID **/ int tomb_nid; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; diff --git a/src/map/npc.c b/src/map/npc.c index 85de5301b..24b22beb3 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -2321,8 +2321,7 @@ int npc_unload(struct npc_data* nd, bool single) nd->ud = NULL; } - HPM->data_store_destroy(nd->hdata); - nd->hdata = NULL; + HPM->data_store_destroy(&nd->hdata); aFree(nd); diff --git a/src/map/npc.h b/src/map/npc.h index 06aee3f93..bf3d1494d 100644 --- a/src/map/npc.h +++ b/src/map/npc.h @@ -102,8 +102,7 @@ struct npc_data { char killer_name[NAME_LENGTH]; } tomb; } u; - /* HPData Support for npc_data */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; diff --git a/src/map/party.c b/src/map/party.c index 4735dfc42..f7e7d431c 100644 --- a/src/map/party.c +++ b/src/map/party.c @@ -103,8 +103,7 @@ int party_db_final(DBKey key, DBData *data, va_list ap) { if (p->instance) aFree(p->instance); - HPM->data_store_destroy(p->hdata); - p->hdata = NULL; + HPM->data_store_destroy(&p->hdata); } return 0; } @@ -600,8 +599,7 @@ int party_broken(int party_id) if( p->instance ) aFree(p->instance); - HPM->data_store_destroy(p->hdata); - p->hdata = NULL; + HPM->data_store_destroy(&p->hdata); idb_remove(party->db,party_id); return 0; diff --git a/src/map/party.h b/src/map/party.h index 960e6da08..df7c03f05 100644 --- a/src/map/party.h +++ b/src/map/party.h @@ -35,9 +35,7 @@ struct party_data { unsigned snovice :1; ///< There's a Super Novice unsigned tk : 1; ///< There's a taekwon } state; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; #define PB_NOTICE_LENGTH (36 + 1) diff --git a/src/map/pc.c b/src/map/pc.c index fee41deff..99f5b867e 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -11332,8 +11332,7 @@ void pc_autotrade_populate(struct map_session_data *sd) { pc->autotrade_update(sd,PAUC_START); - HPM->data_store_destroy(data->hdata); - data->hdata = NULL; + HPM->data_store_destroy(&data->hdata); idb_remove(pc->at_db, sd->status.char_id); } @@ -11343,8 +11342,7 @@ void pc_autotrade_populate(struct map_session_data *sd) { */ int pc_autotrade_final(DBKey key, DBData *data, va_list ap) { struct autotrade_vending* at_v = DB->data2ptr(data); - HPM->data_store_destroy(at_v->hdata); - at_v->hdata = NULL; + HPM->data_store_destroy(&at_v->hdata); return 0; } diff --git a/src/map/pc.h b/src/map/pc.h index a74bc6c80..2c8b24acf 100644 --- a/src/map/pc.h +++ b/src/map/pc.h @@ -539,9 +539,7 @@ END_ZEROED_BLOCK; unsigned short (*parse_cmd_func)(int fd, struct map_session_data *sd); ///< parse_cmd_func used by this player unsigned char delayed_damage;//ref. counter bugreport:7307 [Ind/Hercules] - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store /* expiration_time timer id */ int expiration_tid; @@ -756,8 +754,7 @@ struct autotrade_vending { struct item list[MAX_VENDING]; struct s_vending vending[MAX_VENDING]; unsigned char vend_num; - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; /*===================================== diff --git a/src/map/unit.c b/src/map/unit.c index 0f8a9d7e6..dcb5ec900 100644 --- a/src/map/unit.c +++ b/src/map/unit.c @@ -2654,10 +2654,7 @@ int unit_free(struct block_list *bl, clr_type clrtype) { sd->quest_log = NULL; sd->num_quests = sd->avail_quests = 0; } - - HPM->data_store_destroy(sd->hdata); - sd->hdata = NULL; - + HPM->data_store_destroy(&sd->hdata); break; } case BL_PET: @@ -2768,9 +2765,7 @@ int unit_free(struct block_list *bl, clr_type clrtype) { if( md->tomb_nid ) mob->mvptomb_destroy(md); - HPM->data_store_destroy(md->hdata); - md->hdata = NULL; - + HPM->data_store_destroy(&md->hdata); break; } case BL_HOM: -- cgit v1.2.3-70-g09d2