diff options
author | shennetsind <ind@henn.et> | 2013-11-13 21:26:21 -0200 |
---|---|---|
committer | shennetsind <ind@henn.et> | 2013-11-13 21:26:21 -0200 |
commit | 8fda38dcdabbb9d252b0e11fb07b2ad37f9e659f (patch) | |
tree | 4b08262c9ef18f9a2dd39d56ae2509320ab26e8c /src/common | |
parent | b4d197c667353f417069c406b85a696e3e2e5b6c (diff) | |
download | hercules-8fda38dcdabbb9d252b0e11fb07b2ad37f9e659f.tar.gz hercules-8fda38dcdabbb9d252b0e11fb07b2ad37f9e659f.tar.bz2 hercules-8fda38dcdabbb9d252b0e11fb07b2ad37f9e659f.tar.xz hercules-8fda38dcdabbb9d252b0e11fb07b2ad37f9e659f.zip |
HPM Custom Data Struct Makeover!
- Modified how the core handles it, making it easier to add new points.
- Modified how plugins call it, calls were made shorter, e.g. 'HPMi->getFromSession(session[fd],HPMi->pid,0)' => 'getFromSession(session[fd],0)' -- check src/common/HPMi.h #defines for all the options
- Added support for npc_data (getFromNPCD and so on) as requested in http://hercules.ws/board/topic/2923-hpm-custom-struct-npcs/
Signed-off-by: shennetsind <ind@henn.et>
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/HPM.c | 142 | ||||
-rw-r--r-- | src/common/HPM.h | 8 | ||||
-rw-r--r-- | src/common/HPMi.h | 31 | ||||
-rw-r--r-- | src/common/socket.c | 2 |
4 files changed, 135 insertions, 48 deletions
diff --git a/src/common/HPM.c b/src/common/HPM.c index 5d08821e6..81acb3415 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -207,9 +207,9 @@ struct hplugin *hplugin_load(const char* filename) { /* core */ plugin->hpi->addCPCommand = HPM->import_symbol("addCPCommand",plugin->idx); plugin->hpi->addPacket = HPM->import_symbol("addPacket",plugin->idx); - plugin->hpi->addToSession = HPM->import_symbol("addToSession",plugin->idx); - plugin->hpi->getFromSession = HPM->import_symbol("getFromSession",plugin->idx); - plugin->hpi->removeFromSession = HPM->import_symbol("removeFromSession",plugin->idx); + plugin->hpi->addToHPData = HPM->import_symbol("addToHPData",plugin->idx); + plugin->hpi->getFromHPData = HPM->import_symbol("getFromHPData",plugin->idx); + plugin->hpi->removeFromHPData = HPM->import_symbol("removeFromHPData",plugin->idx); plugin->hpi->AddHook = HPM->import_symbol("AddHook",plugin->idx); plugin->hpi->HookStop = HPM->import_symbol("HookStop",plugin->idx); plugin->hpi->HookStopped = HPM->import_symbol("HookStopped",plugin->idx); @@ -311,67 +311,129 @@ CPCMD(plugins) { } } } +void hplugins_grabHPData(struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr) { + /* record address */ + switch( type ) { + case HPDT_SESSION: + ret->HPDataSRCPtr = (void**)(&((struct socket_data *)ptr)->hdata); + ret->hdatac = &((struct socket_data *)ptr)->hdatac; + break; + /* goes to sub */ + case HPDT_MSD: + case HPDT_NPCD: + if( HPM->grabHPDataSub ) + HPM->grabHPDataSub(ret,type,ptr); + else + ShowError("HPM:grabHPData failed, type %d needs sub-handler!\n",type); + break; + default: + ret->HPDataSRCPtr = NULL; + ret->hdatac = NULL; + return; + } +} +void hplugins_addToHPData(enum HPluginDataTypes type, unsigned int pluginID, void *ptr, void *data, unsigned int index, bool autofree) { + struct HPluginData *HPData, **HPDataSRC; + struct HPDataOperationStorage action; + unsigned int i, max; + + HPM->grabHPData(&action,type,ptr); -void hplugins_addToSession(struct socket_data *sess, void *data, unsigned int id, unsigned int type, bool autofree) { - struct HPluginData *HPData; - unsigned int i; + if( action.hdatac == NULL ) { /* 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); - for(i = 0; i < sess->hdatac; i++) { - if( sess->hdata[i]->pluginID == id && sess->hdata[i]->type == type ) { - ShowError("HPM->addToSession:%s: error! attempting to insert duplicate struct of id %u and type %u\n",HPM->pid2name(id),id,type); + /* duplicate check */ + for(i = 0; i < max; i++) { + if( HPDataSRC[i]->pluginID == pluginID && HPDataSRC[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 + /* 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); - HPData->pluginID = id; - HPData->type = type; + /* input */ + HPData->pluginID = pluginID; + HPData->type = index; HPData->flag.free = autofree ? 1 : 0; HPData->data = data; - RECREATE(sess->hdata,struct HPluginData *,++sess->hdatac); - sess->hdata[sess->hdatac - 1] = HPData; + /* resize */ + *(action.hdatac) += 1; + RECREATE(*(action.HPDataSRCPtr),struct HPluginData *,*(action.hdatac)); + + /* RECREATE modified the addresss */ + HPDataSRC = *(action.HPDataSRCPtr); + HPDataSRC[*(action.hdatac) - 1] = HPData; } -void *hplugins_getFromSession(struct socket_data *sess, unsigned int id, unsigned int type) { - unsigned int i; + +void *hplugins_getFromHPData(enum HPluginDataTypes type, unsigned int pluginID, void *ptr, unsigned int index) { + struct HPDataOperationStorage action; + struct HPluginData **HPDataSRC; + unsigned int i, max; - for(i = 0; i < sess->hdatac; i++) { - if( sess->hdata[i]->pluginID == id && sess->hdata[i]->type == type ) { - break; - } + HPM->grabHPData(&action,type,ptr); + + if( action.hdatac == NULL ) { /* woo it failed! */ + ShowError("HPM:getFromHPData:%s: failed, type %d (%u|%u)\n",HPM->pid2name(pluginID),type,pluginID,index); + return NULL; } - if( i != sess->hdatac ) - return sess->hdata[i]->data; + /* 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; + } + return NULL; } -void hplugins_removeFromSession(struct socket_data *sess, unsigned int id, unsigned int type) { - unsigned int i; + +void hplugins_removeFromHPData(enum HPluginDataTypes type, unsigned int pluginID, void *ptr, unsigned int index) { + struct HPDataOperationStorage action; + struct HPluginData **HPDataSRC; + unsigned int i, max; + + HPM->grabHPData(&action,type,ptr); - for(i = 0; i < sess->hdatac; i++) { - if( sess->hdata[i]->pluginID == id && sess->hdata[i]->type == type ) { + if( action.hdatac == NULL ) { /* woo it failed! */ + ShowError("HPM:removeFromHPData:%s: failed, type %d (%u|%u)\n",HPM->pid2name(pluginID),type,pluginID,index); + return; + } + + /* flag */ + HPDataSRC = *(action.HPDataSRCPtr); + max = *(action.hdatac); + + for(i = 0; i < max; i++) { + if( HPDataSRC[i]->pluginID == pluginID && HPDataSRC[i]->type == index ) break; - } } - if( i != sess->hdatac ) { + if( i != max ) { unsigned int cursor; - - aFree(sess->hdata[i]->data); - aFree(sess->hdata[i]); - sess->hdata[i] = NULL; - for(i = 0, cursor = 0; i < sess->hdatac; i++) { - if( sess->hdata[i] == NULL ) + aFree(HPDataSRC[i]->data);/* when its removed we delete it regardless of autofree */ + aFree(HPDataSRC[i]); + HPDataSRC[i] = NULL; + + for(i = 0, cursor = 0; i < max; i++) { + if( HPDataSRC[i] == NULL ) continue; if( i != cursor ) - sess->hdata[cursor] = sess->hdata[i]; + HPDataSRC[cursor] = HPDataSRC[i]; cursor++; } - sess->hdatac = cursor; + *(action.hdatac) = cursor; } } @@ -547,9 +609,9 @@ void hplugins_share_defaults(void) { #endif /* our own */ HPM->share(hplugins_addpacket,"addPacket"); - HPM->share(hplugins_addToSession,"addToSession"); - HPM->share(hplugins_getFromSession,"getFromSession"); - HPM->share(hplugins_removeFromSession,"removeFromSession"); + HPM->share(hplugins_addToHPData,"addToHPData"); + HPM->share(hplugins_getFromHPData,"getFromHPData"); + HPM->share(hplugins_removeFromHPData,"removeFromHPData"); HPM->share(HPM_AddHook,"AddHook"); HPM->share(HPM_HookStop,"HookStop"); HPM->share(HPM_HookStopped,"HookStopped"); @@ -698,4 +760,6 @@ void hpm_defaults(void) { HPM->arg_db_clear_sub = hpm_arg_db_clear_sub; HPM->parse_arg = hpm_parse_arg; HPM->arg_help = hpm_arg_help; + HPM->grabHPData = hplugins_grabHPData; + HPM->grabHPDataSub = NULL; } diff --git a/src/common/HPM.h b/src/common/HPM.h index e685b3068..9a6bda713 100644 --- a/src/common/HPM.h +++ b/src/common/HPM.h @@ -82,6 +82,11 @@ struct HPMArgData { bool has_param;/* because of the weird "--arg<space>param" instead of the "--arg=param" */ }; +struct HPDataOperationStorage { + void **HPDataSRCPtr; + unsigned int *hdatac; +}; + /* Hercules Plugin Manager Interface */ struct HPM_interface { /* vars */ @@ -125,6 +130,9 @@ struct HPM_interface { bool (*parse_arg) (const char *arg, int* index, char *argv[], bool param); void (*arg_help) (void); int (*arg_db_clear_sub) (DBKey key, DBData *data, va_list args); + void (*grabHPData) (struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr); + /* for server-specific HPData e.g. map_session_data */ + void (*grabHPDataSub) (struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr); } HPM_s; struct HPM_interface *HPM; diff --git a/src/common/HPMi.h b/src/common/HPMi.h index f071bf09e..940782dce 100644 --- a/src/common/HPMi.h +++ b/src/common/HPMi.h @@ -77,6 +77,12 @@ enum HPluginHookType { HOOK_TYPE_POST, }; +enum HPluginDataTypes { + HPDT_SESSION, + HPDT_MSD, + HPDT_NPCD, +}; + #define addHookPre(tname,hook) HPMi->AddHook(HOOK_TYPE_PRE,tname,hook,HPMi->pid) #define addHookPost(tname,hook) HPMi->AddHook(HOOK_TYPE_POST,tname,hook,HPMi->pid) /* need better names ;/ */ @@ -85,6 +91,19 @@ enum HPluginHookType { #define hookStopped() HPMi->HookStopped() #define addArg(name,param,func,help) HPMi->addArg(HPMi->pid,name,param,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) +/* 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) +/* 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) /* Hercules Plugin Mananger Include Interface */ HPExport struct HPMi_interface { @@ -95,14 +114,10 @@ HPExport struct HPMi_interface { bool (*addCommand) (char *name, bool (*func)(const int fd, struct map_session_data* sd, const char* command, const char* message,struct AtCommandInfo *info)); bool (*addScript) (char *name, char *args, bool (*func)(struct script_state *st)); void (*addCPCommand) (char *name, CParseFunc func); - /* map_session_data */ - void (*addToMSD) (struct map_session_data *sd, void *data, unsigned int id, unsigned int type, bool autofree); - void *(*getFromMSD) (struct map_session_data *sd, unsigned int id, unsigned int type); - void (*removeFromMSD) (struct map_session_data *sd, unsigned int id, unsigned int type); - /* session[] */ - void (*addToSession) (struct socket_data *sess, void *data, unsigned int id, unsigned int type, bool autofree); - void *(*getFromSession) (struct socket_data *sess, unsigned int id, unsigned int type); - void (*removeFromSession) (struct socket_data *sess, unsigned int id, unsigned int type); + /* 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); /* 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/socket.c b/src/common/socket.c index c66153550..6e877d9be 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -613,8 +613,8 @@ static void delete_session(int fd) for(i = 0; i < session[fd]->hdatac; i++) { if( session[fd]->hdata[i]->flag.free ) { aFree(session[fd]->hdata[i]->data); - aFree(session[fd]->hdata[i]); } + aFree(session[fd]->hdata[i]); } if( session[fd]->hdata ) aFree(session[fd]->hdata); |