plugins.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <server.h>
  6. #include <general.h>
  7. #include <ipc.h>
  8. static void *get_symbol(GModule *hdl, const char *name, boolean_t stop_error)
  9. {
  10. void *p = NULL;
  11. char *nm = strdup(name);
  12. if (!g_module_symbol(hdl, nm, &p))
  13. {
  14. nm[0] = toupper(nm[0]);
  15. if (!g_module_symbol(hdl, nm, &p) && stop_error)
  16. {
  17. fprintf(stderr, "Dynamic library error : %s : %s\n", name,
  18. g_module_error());
  19. return NULL;
  20. }
  21. }
  22. free(nm);
  23. return p;
  24. }
  25. player_t *new_player(const char *libname, const char *name, int id,
  26. conf_t *conf)
  27. {
  28. player_t *player;
  29. void (*caml)(char **);
  30. char *caml_par[2] = { "./server", NULL };
  31. printf("%s : %s ( %d )\n", name, libname, id);
  32. player = shm_alloc(sizeof(player_t), get_shm_plr_id() + id);
  33. if (!g_module_supported())
  34. {
  35. fprintf(stderr, "%s\n", "Fatal error : Dynamic library not supported!");
  36. abort();
  37. }
  38. player->team_id = id;
  39. player->name = name;
  40. player->score = 0;
  41. player->on = True;
  42. player->handle = g_module_open(libname, 0);
  43. player->time_turn = conf->turn_msec;
  44. if (player->handle == NULL)
  45. {
  46. fprintf(stderr, "Dynamic library error : %s : %s\n",
  47. libname, g_module_error());
  48. abort();
  49. }
  50. caml = get_symbol(player->handle, "caml_startup", False);
  51. if (caml)
  52. {
  53. caml(caml_par);
  54. }
  55. player->pipe_read = 0;
  56. player->pipe_write = 1;
  57. player->alive = False;
  58. player->init = get_symbol(player->handle, "player_init", True);
  59. player->new_turn = get_symbol(player->handle, "player_new_turn", True);
  60. player->turn_akx = get_symbol(player->handle, "player_akx_turn", True);
  61. player->turn_r4d2 = get_symbol(player->handle, "player_r4d2_turn", True);
  62. return player;
  63. }
  64. void destroy_player(player_t *player)
  65. {
  66. g_module_close(player->handle);
  67. free(player);
  68. }
  69. void player_init(player_t *p, int team_id)
  70. {
  71. if (p->init)
  72. {
  73. p->init(team_id, gl_config->nb_players);
  74. }
  75. else
  76. {
  77. printf("erreur de symbol : 'player_init'\n");
  78. }
  79. }
  80. void player_new_turn(player_t *p, int turn_num)
  81. {
  82. if (p->new_turn)
  83. {
  84. p->new_turn(turn_num);
  85. }
  86. else
  87. {
  88. printf("erreur de symbol : 'player_new_turn'\n");
  89. }
  90. }
  91. void player_turn_akx(player_t *p, int akx_id)
  92. {
  93. if (p->turn_akx)
  94. {
  95. p->turn_akx(akx_id);
  96. }
  97. else
  98. {
  99. printf("erreur de symbol : 'player_turn_akx'\n");
  100. }
  101. }
  102. void player_turn_r4d2(player_t *p, int r4d2_id)
  103. {
  104. if (p->turn_r4d2)
  105. {
  106. p->turn_r4d2(r4d2_id);
  107. }
  108. else
  109. {
  110. printf("erreur de symbol : 'player_turn_r4d2'\n");
  111. }
  112. }