mapindex.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "mapindex.h"
  7. #include "../common/mmo.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/malloc.h"
  10. #define MAX_MAPINDEX 2000
  11. //Leave an extra char of space to hold the terminator, in case for the strncpy(mapindex_id2name()) calls.
  12. struct indexes {
  13. char name[MAP_NAME_LENGTH+1]; //Stores map name
  14. int length; //Stores string length WITHOUT the extension for quick lookup.
  15. } indexes[MAX_MAPINDEX];
  16. static unsigned short max_index = 0;
  17. char mapindex_cfgfile[80] = "db/map_list.txt";
  18. /// Adds a map to the specified index
  19. /// Returns 1 if successful, 0 oherwise
  20. int mapindex_addmap(int index, const char *name)
  21. {
  22. char map_name[1024];
  23. char *ext;
  24. int length;
  25. if (index < 0 || index >= MAX_MAPINDEX) {
  26. ShowError("(mapindex_add) Map index (%d) for \"%s\" out of range (max is %d)\n", index, name, MAX_MAPINDEX);
  27. return 0;
  28. }
  29. snprintf(map_name, 1024, "%s", name);
  30. map_name[1023] = 0;
  31. length = strlen(map_name);
  32. if (length > MAP_NAME_LENGTH) {
  33. ShowError("(mapindex_add) Map name %s is too long. Maps are limited to %d characters.\n", map_name, MAP_NAME_LENGTH);
  34. return 0;
  35. }
  36. if ((ext = strstr(map_name, ".")) != NULL) { // Remove extension
  37. length = ext-map_name;
  38. sprintf(ext, "\0");
  39. }
  40. if (indexes[index].length)
  41. ShowWarning("(mapindex_add) Overriding index %d: map \"%s\" -> \"%s\"\n", index, indexes[index].name, map_name);
  42. strncpy(indexes[index].name, map_name, MAP_NAME_LENGTH);
  43. indexes[index].length = length;
  44. if (max_index <= index)
  45. max_index = index+1;
  46. return 1;
  47. }
  48. unsigned short mapindex_name2id(const char* name) {
  49. //TODO: Perhaps use a db to speed this up? [Skotlex]
  50. int i;
  51. int length = strlen(name);
  52. char *ext = strstr(name, ".");
  53. if (ext)
  54. length = ext-name; //Base map-name length without the extension.
  55. for (i = 1; i < max_index; i++)
  56. {
  57. if (strncmp(indexes[i].name,name,length)==0)
  58. return i;
  59. }
  60. #ifdef MAPINDEX_AUTOADD
  61. if( mapindex_addmap(i,name) )
  62. {
  63. ShowDebug("mapindex_name2id: Auto-added map \"%s\" to position %d\n", indexes[i], i);
  64. return i;
  65. }
  66. ShowWarning("mapindex_name2id: Failed to auto-add map \"%s\" to position %d!\n", name, i);
  67. return 0;
  68. #else
  69. ShowDebug("mapindex_name2id: Map \"%s\" not found in index list!\n", name);
  70. return 0;
  71. #endif
  72. }
  73. const char* mapindex_id2name(unsigned short id) {
  74. if (id > MAX_MAPINDEX || !indexes[id].length) {
  75. ShowDebug("mapindex_id2name: Requested name for non-existant map index [%d] in cache.\n", id);
  76. return indexes[0].name; //Theorically this should never happen, hence we return this string to prevent null pointer crashes.
  77. }
  78. return indexes[id].name;
  79. }
  80. void mapindex_init(void) {
  81. FILE *fp;
  82. char line[1024];
  83. int last_index = -1;
  84. int index;
  85. char map_name[1024];
  86. memset (&indexes, 0, sizeof (indexes));
  87. fp=fopen(mapindex_cfgfile,"r");
  88. if(fp==NULL){
  89. ShowFatalError("Unable to read mapindex config file %s!\n", mapindex_cfgfile);
  90. exit(1); //Server can't really run without this file.
  91. }
  92. while(fgets(line,1020,fp)){
  93. if(line[0] == '/' && line[1] == '/')
  94. continue;
  95. switch (sscanf(line,"%1000s\t%d",map_name,&index)) {
  96. case 1: //Map with no ID given, auto-assign
  97. index = last_index+1;
  98. case 2: //Map with ID given
  99. mapindex_addmap(index,map_name);
  100. break;
  101. default:
  102. continue;
  103. }
  104. last_index = index;
  105. }
  106. fclose(fp);
  107. }
  108. void mapindex_final(void) {
  109. }