PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/plugins/HPMHooking.c

https://gitlab.com/evol/hercules
C | 246 lines | 190 code | 37 blank | 19 comment | 16 complexity | fcf204f423af163161506441bdfea5c6 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0
  1. /**
  2. * This file is part of Hercules.
  3. * http://herc.ws - http://github.com/HerculesWS/Hercules
  4. *
  5. * Copyright (C) 2013-2015 Hercules Dev Team
  6. *
  7. * Hercules is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "common/hercules.h"
  21. #include "common/db.h"
  22. #include "common/memmgr.h"
  23. #include "common/mmo.h"
  24. #include "common/socket.h"
  25. #if defined (HPMHOOKING_LOGIN)
  26. #define HPM_SERVER_TYPE SERVER_TYPE_LOGIN
  27. #define HPM_CORE_INCLUDE "HPMHooking/HPMHooking_login.HPMHooksCore.inc"
  28. #define HPM_HOOKS_INCLUDE "HPMHooking/HPMHooking_login.Hooks.inc"
  29. #define HPM_POINTS_INCLUDE "HPMHooking/HPMHooking_login.HookingPoints.inc"
  30. #define HPM_SOURCES_INCLUDE "HPMHooking/HPMHooking_login.sources.inc"
  31. #include "login/login.h"
  32. #elif defined (HPMHOOKING_CHAR)
  33. #define HPM_SERVER_TYPE SERVER_TYPE_CHAR
  34. #define HPM_CORE_INCLUDE "HPMHooking/HPMHooking_char.HPMHooksCore.inc"
  35. #define HPM_HOOKS_INCLUDE "HPMHooking/HPMHooking_char.Hooks.inc"
  36. #define HPM_POINTS_INCLUDE "HPMHooking/HPMHooking_char.HookingPoints.inc"
  37. #define HPM_SOURCES_INCLUDE "HPMHooking/HPMHooking_char.sources.inc"
  38. #include "char/char.h"
  39. #include "char/geoip.h"
  40. #include "char/int_auction.h"
  41. #include "char/int_elemental.h"
  42. #include "char/int_guild.h"
  43. #include "char/int_homun.h"
  44. #include "char/int_mail.h"
  45. #include "char/int_mercenary.h"
  46. #include "char/int_party.h"
  47. #include "char/int_pet.h"
  48. #include "char/int_quest.h"
  49. #include "char/int_storage.h"
  50. #include "char/inter.h"
  51. #include "char/loginif.h"
  52. #include "char/mapif.h"
  53. #include "char/pincode.h"
  54. #include "common/mapindex.h"
  55. #elif defined (HPMHOOKING_MAP)
  56. #define HPM_SERVER_TYPE SERVER_TYPE_MAP
  57. #define HPM_CORE_INCLUDE "HPMHooking/HPMHooking_map.HPMHooksCore.inc"
  58. #define HPM_HOOKS_INCLUDE "HPMHooking/HPMHooking_map.Hooks.inc"
  59. #define HPM_POINTS_INCLUDE "HPMHooking/HPMHooking_map.HookingPoints.inc"
  60. #define HPM_SOURCES_INCLUDE "HPMHooking/HPMHooking_map.sources.inc"
  61. #include "map/atcommand.h"
  62. #include "map/battle.h"
  63. #include "map/battleground.h"
  64. #include "map/channel.h"
  65. #include "map/chat.h"
  66. #include "map/chrif.h"
  67. #include "map/clif.h"
  68. #include "map/duel.h"
  69. #include "map/elemental.h"
  70. #include "map/guild.h"
  71. #include "map/homunculus.h"
  72. #include "map/instance.h"
  73. #include "map/intif.h"
  74. #include "map/irc-bot.h"
  75. #include "map/itemdb.h"
  76. #include "map/log.h"
  77. #include "map/mail.h"
  78. #include "map/map.h"
  79. #include "map/mapreg.h"
  80. #include "map/mercenary.h"
  81. #include "map/mob.h"
  82. #include "map/npc.h"
  83. #include "map/party.h"
  84. #include "map/path.h"
  85. #include "map/pc.h"
  86. #include "map/pet.h"
  87. #include "map/quest.h"
  88. #include "map/script.h"
  89. #include "map/skill.h"
  90. #include "map/status.h"
  91. #include "map/storage.h"
  92. #include "map/trade.h"
  93. #include "map/unit.h"
  94. #include "common/mapindex.h"
  95. #else
  96. #define HPM_SERVER_TYPE SERVER_TYPE_UNKNOWN
  97. #define HPM_CORE_INCLUDE "HPMHooking/HPMHooking.HPMHooksCore.inc"
  98. #define HPM_HOOKS_INCLUDE "HPMHooking/HPMHooking.Hooks.inc"
  99. #define HPM_POINTS_INCLUDE "HPMHooking/HPMHooking.HookingPoints.inc"
  100. #define HPM_SOURCES_INCLUDE "HPMHooking/HPMHooking.sources.inc"
  101. #error HPMHooking plugin needs to be compiled for a specific server type. Please make sure your Makefiles are up to date.
  102. #endif
  103. #include "common/conf.h"
  104. #include "common/console.h"
  105. #include "common/db.h"
  106. #include "common/memmgr.h"
  107. #include "common/nullpo.h"
  108. #include "common/showmsg.h"
  109. #include "common/socket.h"
  110. #include "common/sql.h"
  111. #include "common/strlib.h"
  112. #include "common/sysinfo.h"
  113. #include "common/timer.h"
  114. #include "common/utils.h"
  115. #include "common/HPMDataCheck.h"
  116. #include <stdio.h>
  117. #include <stdlib.h>
  118. #include <string.h>
  119. HPExport struct hplugin_info pinfo = {
  120. "HPMHooking", // Plugin name
  121. HPM_SERVER_TYPE,// Which server types this plugin works with?
  122. "0.2", // Plugin version
  123. HPM_VERSION, // HPM Version (don't change, macro is automatically updated)
  124. };
  125. #define HP_POP(x,y) #x , (void**)(&x) , (void*)y , 0
  126. DBMap *hp_db;/* hooking points db -- for quick lookup */
  127. struct HookingPointData {
  128. char* name;
  129. void **sref;
  130. void *tref;
  131. int idx;
  132. };
  133. struct HPMHookPoint {
  134. void *func;
  135. unsigned int pID;
  136. };
  137. struct HPMHooksCore {
  138. #include HPM_CORE_INCLUDE
  139. struct {
  140. int total;
  141. } data;
  142. } HPMHooks;
  143. bool *HPMforce_return;
  144. void HPM_HP_final(void);
  145. void HPM_HP_load(void);
  146. HPExport void server_post_final (void) {
  147. HPM_HP_final();
  148. }
  149. HPExport const char *Hooked (bool *fr) {
  150. const char *ret = HPM_shared_symbols(HPM_SERVER_TYPE);
  151. if (ret)
  152. return ret;
  153. HPMforce_return = fr;
  154. HPM_HP_load();
  155. return NULL;
  156. }
  157. HPExport bool HPM_Plugin_AddHook(enum HPluginHookType type, const char *target, void *hook, unsigned int pID) {
  158. struct HookingPointData *hpd;
  159. if (hp_db && (hpd = strdb_get(hp_db,target)) != NULL) {
  160. struct HPMHookPoint **hp = NULL;
  161. int *count = NULL;
  162. if( type == HOOK_TYPE_PRE ) {
  163. hp = (struct HPMHookPoint **)((char *)&HPMHooks.list + (sizeof(struct HPMHookPoint *)*hpd->idx));
  164. count = (int *)((char *)&HPMHooks.count + (sizeof(int)*hpd->idx));
  165. } else {
  166. hp = (struct HPMHookPoint **)((char *)&HPMHooks.list + (sizeof(struct HPMHookPoint *)*(hpd->idx+1)));
  167. count = (int *)((char *)&HPMHooks.count + (sizeof(int)*(hpd->idx+1)));
  168. }
  169. if( hp ) {
  170. *count += 1;
  171. RECREATE(*hp, struct HPMHookPoint, *count);
  172. (*hp)[*count - 1].func = hook;
  173. (*hp)[*count - 1].pID = pID;
  174. *(hpd->sref) = hpd->tref;
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. #include HPM_HOOKS_INCLUDE
  181. void HPM_HP_final(void) {
  182. int i, len = HPMHooks.data.total * 2;
  183. if( hp_db )
  184. db_destroy(hp_db);
  185. for(i = 0; i < len; i++) {
  186. int *count = (int *)((char *)&HPMHooks.count + (sizeof(int)*(i)));
  187. if( count && *count ) {
  188. struct HPMHookPoint **hp = (struct HPMHookPoint **)((char *)&HPMHooks.list + (sizeof(struct HPMHookPoint *)*(i)));
  189. if( hp && *hp )
  190. aFree(*hp);
  191. }
  192. }
  193. }
  194. void HPM_HP_load(void) {
  195. #include HPM_POINTS_INCLUDE
  196. int i, len = ARRAYLENGTH(HookingPoints), idx = 0;
  197. memset(&HPMHooks,0,sizeof(struct HPMHooksCore));
  198. hp_db = strdb_alloc(DB_OPT_BASE|DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA, HookingPointsLenMax);
  199. for(i = 0; i < len; i++) {
  200. struct HookingPointData *hpd = NULL;
  201. CREATE(hpd, struct HookingPointData, 1);
  202. memcpy(hpd, &HookingPoints[i], sizeof(struct HookingPointData));
  203. hpd->idx = idx;
  204. idx += 2;
  205. strdb_put(hp_db, HookingPoints[i].name, hpd);
  206. HPMHooks.data.total++;
  207. }
  208. #include HPM_SOURCES_INCLUDE
  209. }