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