mapindex.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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,"%*[^#]%*[#]%s",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){
  62. for (index = 1; index < max_index; index++) {
  63. //if (strcmp(indexes[index].name,"#CLEARED#")==0)
  64. if (indexes[index].name[0] == '\0')
  65. break;
  66. }
  67. }
  68. if (index < 0 || index >= MAX_MAPINDEX) {
  69. ShowError("(mapindex_add) Map index (%d) for \"%s\" out of range (max is %d)\n", index, name, MAX_MAPINDEX);
  70. return 0;
  71. }
  72. mapindex_getmapname(name, map_name);
  73. if (map_name[0] == '\0') {
  74. ShowError("(mapindex_add) Cannot add maps with no name.\n");
  75. return 0;
  76. }
  77. if (strlen(map_name) >= MAP_NAME_LENGTH) {
  78. ShowError("(mapindex_add) Map name %s is too long. Maps are limited to %d characters.\n", map_name, MAP_NAME_LENGTH);
  79. return 0;
  80. }
  81. if (mapindex_exists(index)) {
  82. ShowWarning("(mapindex_add) Overriding index %d: map \"%s\" -> \"%s\"\n", index, indexes[index].name, map_name);
  83. strdb_remove(mapindex_db, indexes[index].name);
  84. }
  85. safestrncpy(indexes[index].name, map_name, MAP_NAME_LENGTH);
  86. strdb_iput(mapindex_db, map_name, index);
  87. if (max_index <= index)
  88. max_index = index+1;
  89. return index;
  90. }
  91. unsigned short mapindex_name2id(const char* name) {
  92. int i;
  93. char map_name[MAP_NAME_LENGTH];
  94. mapindex_getmapname(name, map_name);
  95. if( (i = strdb_iget(mapindex_db, map_name)) )
  96. return i;
  97. ShowDebug("mapindex_name2id: Map \"%s\" not found in index list!\n", map_name);
  98. return 0;
  99. }
  100. const char* mapindex_id2name(unsigned short id)
  101. {
  102. if (id > MAX_MAPINDEX || !mapindex_exists(id)) {
  103. ShowDebug("mapindex_id2name: Requested name for non-existant map index [%d] in cache.\n", id);
  104. return indexes[0].name; // dummy empty string so that the callee doesn't crash
  105. }
  106. return indexes[id].name;
  107. }
  108. void mapindex_init(void) {
  109. FILE *fp;
  110. char line[1024];
  111. int last_index = -1;
  112. int index;
  113. char map_name[MAP_NAME_LENGTH];
  114. if( ( fp = fopen(mapindex_cfgfile,"r") ) == NULL ){
  115. ShowFatalError("Unable to read mapindex config file %s!\n", mapindex_cfgfile);
  116. exit(EXIT_FAILURE); //Server can't really run without this file.
  117. }
  118. memset (&indexes, 0, sizeof (indexes));
  119. mapindex_db = strdb_alloc(DB_OPT_DUP_KEY, MAP_NAME_LENGTH);
  120. while(fgets(line, sizeof(line), fp)) {
  121. if(line[0] == '/' && line[1] == '/')
  122. continue;
  123. switch (sscanf(line, "%12s\t%d", map_name, &index)) {
  124. case 1: //Map with no ID given, auto-assign
  125. index = last_index+1;
  126. case 2: //Map with ID given
  127. mapindex_addmap(index,map_name);
  128. break;
  129. default:
  130. continue;
  131. }
  132. last_index = index;
  133. }
  134. fclose(fp);
  135. if( !strdb_iget(mapindex_db, MAP_DEFAULT) ) {
  136. ShowError("mapindex_init: MAP_DEFAULT '%s' not found in cache! update mapindex.h MAP_DEFAULT var!!!\n",MAP_DEFAULT);
  137. }
  138. }
  139. int mapindex_removemap(int index){
  140. indexes[index].name[0] = '\0';
  141. return 0;
  142. }
  143. void mapindex_final(void) {
  144. db_destroy(mapindex_db);
  145. }