mapindex.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/mmo.h"
  4. #include "../common/showmsg.h"
  5. #include "../common/malloc.h"
  6. #include "../common/strlib.h"
  7. #include "../common/db.h"
  8. #include "mapindex.h"
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. DBMap *mapindex_db;
  13. struct _indexes {
  14. char name[MAP_NAME_LENGTH]; //Stores map name
  15. } indexes[MAX_MAPINDEX];
  16. int max_index = 0;
  17. char mapindex_cfgfile[80] = "db/map_index.txt";
  18. #define mapindex_exists(id) (indexes[id].name[0] != '\0')
  19. /// Retrieves the map name from 'string' (removing .gat extension if present).
  20. /// Result gets placed either into 'buf' or in a static local buffer.
  21. const char* mapindex_getmapname(const char* string, char* output) {
  22. static char buf[MAP_NAME_LENGTH];
  23. char* dest = (output != NULL) ? output : buf;
  24. size_t len = strnlen(string, MAP_NAME_LENGTH_EXT);
  25. if (len == MAP_NAME_LENGTH_EXT) {
  26. ShowWarning("(mapindex_normalize_name) Map name '%*s' is too long!\n", 2*MAP_NAME_LENGTH_EXT, string);
  27. len--;
  28. }
  29. if (len >= 4 && stricmp(&string[len-4], ".gat") == 0)
  30. len -= 4; // strip .gat extension
  31. len = min(len, MAP_NAME_LENGTH-1);
  32. safestrncpy(dest, string, len+1);
  33. memset(&dest[len], '\0', MAP_NAME_LENGTH-len);
  34. return dest;
  35. }
  36. /// Retrieves the map name from 'string' (adding .gat extension if not already present).
  37. /// Result gets placed either into 'buf' or in a static local buffer.
  38. const char* mapindex_getmapname_ext(const char* string, char* output) {
  39. static char buf[MAP_NAME_LENGTH_EXT];
  40. char* dest = (output != NULL) ? output : buf;
  41. size_t len;
  42. strcpy(buf,string);
  43. sscanf(string,"%*[^#]%*[#]%15s",buf);
  44. len = safestrnlen(buf, MAP_NAME_LENGTH);
  45. if (len == MAP_NAME_LENGTH) {
  46. ShowWarning("(mapindex_normalize_name) Map name '%*s' is too long!\n", 2*MAP_NAME_LENGTH, buf);
  47. len--;
  48. }
  49. safestrncpy(dest, buf, len+1);
  50. if (len < 4 || stricmp(&dest[len-4], ".gat") != 0) {
  51. strcpy(&dest[len], ".gat");
  52. len += 4; // add .gat extension
  53. }
  54. memset(&dest[len], '\0', MAP_NAME_LENGTH_EXT-len);
  55. return dest;
  56. }
  57. /// Adds a map to the specified index
  58. /// Returns 1 if successful, 0 oherwise
  59. int mapindex_addmap(int index, const char* name) {
  60. char map_name[MAP_NAME_LENGTH];
  61. if (index == -1){ //autogive index
  62. ARR_FIND(1,max_index,index,(indexes[index].name[0] == '\0'));
  63. }
  64. if (index < 0 || index >= MAX_MAPINDEX) {
  65. ShowError("(mapindex_add) Map index (%d) for \"%s\" out of range (max is %d)\n", index, name, MAX_MAPINDEX);
  66. return 0;
  67. }
  68. mapindex_getmapname(name, map_name);
  69. if (map_name[0] == '\0') {
  70. ShowError("(mapindex_add) Cannot add maps with no name.\n");
  71. return 0;
  72. }
  73. if (strlen(map_name) >= MAP_NAME_LENGTH) {
  74. ShowError("(mapindex_add) Map name %s is too long. Maps are limited to %d characters.\n", map_name, MAP_NAME_LENGTH);
  75. return 0;
  76. }
  77. if (mapindex_exists(index)) {
  78. ShowWarning("(mapindex_add) Overriding index %d: map \"%s\" -> \"%s\"\n", index, indexes[index].name, map_name);
  79. strdb_remove(mapindex_db, indexes[index].name);
  80. }
  81. safestrncpy(indexes[index].name, map_name, MAP_NAME_LENGTH);
  82. strdb_iput(mapindex_db, map_name, index);
  83. if (max_index <= index)
  84. max_index = index+1;
  85. return index;
  86. }
  87. unsigned short mapindex_name2id(const char* name) {
  88. int i;
  89. char map_name[MAP_NAME_LENGTH];
  90. mapindex_getmapname(name, map_name);
  91. if( (i = strdb_iget(mapindex_db, map_name)) )
  92. return i;
  93. ShowDebug("mapindex_name2id: Map \"%s\" not found in index list!\n", map_name);
  94. return 0;
  95. }
  96. const char* mapindex_id2name(unsigned short id)
  97. {
  98. if (id > MAX_MAPINDEX || !mapindex_exists(id)) {
  99. ShowDebug("mapindex_id2name: Requested name for non-existant map index [%d] in cache.\n", id);
  100. return indexes[0].name; // dummy empty string so that the callee doesn't crash
  101. }
  102. return indexes[id].name;
  103. }
  104. void mapindex_init(void) {
  105. FILE *fp;
  106. char line[1024];
  107. int last_index = -1;
  108. int index;
  109. char map_name[MAP_NAME_LENGTH];
  110. if( ( fp = fopen(mapindex_cfgfile,"r") ) == NULL ){
  111. ShowFatalError("Unable to read mapindex config file %s!\n", mapindex_cfgfile);
  112. exit(EXIT_FAILURE); //Server can't really run without this file.
  113. }
  114. memset (&indexes, 0, sizeof (indexes));
  115. mapindex_db = strdb_alloc(DB_OPT_DUP_KEY, MAP_NAME_LENGTH);
  116. while(fgets(line, sizeof(line), fp)) {
  117. if(line[0] == '/' && line[1] == '/')
  118. continue;
  119. switch (sscanf(line, "%11s\t%d", map_name, &index)) {
  120. case 1: //Map with no ID given, auto-assign
  121. index = last_index+1;
  122. case 2: //Map with ID given
  123. mapindex_addmap(index,map_name);
  124. break;
  125. default:
  126. continue;
  127. }
  128. last_index = index;
  129. }
  130. fclose(fp);
  131. if( !strdb_iget(mapindex_db, MAP_DEFAULT) ) {
  132. ShowError("mapindex_init: MAP_DEFAULT '%s' not found in cache! Update MAP_DEFAULT in mapindex.h!\n",MAP_DEFAULT);
  133. }
  134. }
  135. int mapindex_removemap(int index){
  136. indexes[index].name[0] = '\0';
  137. return 0;
  138. }
  139. void mapindex_final(void) {
  140. db_destroy(mapindex_db);
  141. }