mapindex.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. struct indexes {
  12. char name[MAP_NAME_LENGTH]; //Stores map name
  13. char exists; //Set to 1 if index exists
  14. } indexes[MAX_MAPINDEX];
  15. static unsigned short max_index = 0;
  16. char mapindex_cfgfile[80] = "db/map_index.txt";
  17. // Removes the extension from a map name
  18. char *mapindex_normalize_name(char *mapname)
  19. {
  20. char *ptr, *ptr2;
  21. ptr = strchr(mapname, '.');
  22. if (ptr) { //Check and remove extension.
  23. while (ptr[1] && (ptr2 = strchr(ptr+1, '.')))
  24. ptr = ptr2; //Skip to the last dot.
  25. if(stricmp(ptr,".gat") == 0)
  26. *ptr = '\0'; //Remove extension.
  27. }
  28. return mapname;
  29. }
  30. /// Adds a map to the specified index
  31. /// Returns 1 if successful, 0 oherwise
  32. int mapindex_addmap(int index, const char *name)
  33. {
  34. char map_name[MAP_NAME_LENGTH_EXT];
  35. if (index < 0 || index >= MAX_MAPINDEX) {
  36. ShowError("(mapindex_add) Map index (%d) for \"%s\" out of range (max is %d)\n", index, name, MAX_MAPINDEX);
  37. return 0;
  38. }
  39. snprintf(map_name, MAP_NAME_LENGTH_EXT, "%s", name);
  40. mapindex_normalize_name(map_name);
  41. if (strlen(map_name) > MAP_NAME_LENGTH-1) {
  42. ShowError("(mapindex_add) Map name %s is too long. Maps are limited to %d characters.\n", map_name, MAP_NAME_LENGTH);
  43. return 0;
  44. }
  45. if (indexes[index].exists)
  46. ShowWarning("(mapindex_add) Overriding index %d: map \"%s\" -> \"%s\"\n", index, indexes[index].name, map_name);
  47. snprintf(indexes[index].name, MAP_NAME_LENGTH, "%s", map_name);
  48. indexes[index].exists = 1;
  49. if (max_index <= index)
  50. max_index = index+1;
  51. return 1;
  52. }
  53. unsigned short mapindex_name2id(const char* name)
  54. {
  55. //TODO: Perhaps use a db to speed this up? [Skotlex]
  56. int i;
  57. char map_name[MAP_NAME_LENGTH_EXT];
  58. snprintf(map_name, MAP_NAME_LENGTH_EXT, "%s", name);
  59. mapindex_normalize_name(map_name);
  60. for (i = 1; i < max_index; i++)
  61. {
  62. if (strcmp(indexes[i].name,map_name)==0)
  63. return i;
  64. }
  65. #ifdef MAPINDEX_AUTOADD
  66. if( mapindex_addmap(i,map_name) )
  67. {
  68. ShowDebug("mapindex_name2id: Auto-added map \"%s\" to position %d\n", indexes[i], i);
  69. return i;
  70. }
  71. ShowWarning("mapindex_name2id: Failed to auto-add map \"%s\" to position %d!\n", name, i);
  72. return 0;
  73. #else
  74. ShowDebug("mapindex_name2id: Map \"%s\" not found in index list!\n", name);
  75. return 0;
  76. #endif
  77. }
  78. const char* mapindex_id2name(unsigned short id)
  79. {
  80. if (id > MAX_MAPINDEX || !indexes[id].exists) {
  81. ShowDebug("mapindex_id2name: Requested name for non-existant map index [%d] in cache.\n", id);
  82. return indexes[0].name; //Theorically this should never happen, hence we return this string to prevent null pointer crashes.
  83. }
  84. return indexes[id].name;
  85. }
  86. void mapindex_init(void)
  87. {
  88. FILE *fp;
  89. char line[1024];
  90. int last_index = -1;
  91. int index;
  92. char map_name[1024]; // only MAP_NAME_LENGTH(_EXT) under safe conditions
  93. memset (&indexes, 0, sizeof (indexes));
  94. fp=fopen(mapindex_cfgfile,"r");
  95. if(fp==NULL){
  96. ShowFatalError("Unable to read mapindex config file %s!\n", mapindex_cfgfile);
  97. exit(1); //Server can't really run without this file.
  98. }
  99. while(fgets(line, sizeof(line), fp))
  100. {
  101. if(line[0] == '/' && line[1] == '/')
  102. continue;
  103. switch (sscanf(line,"%s\t%d",map_name,&index)) {
  104. case 1: //Map with no ID given, auto-assign
  105. index = last_index+1;
  106. case 2: //Map with ID given
  107. mapindex_addmap(index,map_name);
  108. break;
  109. default:
  110. continue;
  111. }
  112. last_index = index;
  113. }
  114. fclose(fp);
  115. }
  116. void mapindex_final(void)
  117. {
  118. }