diff options
author | hemagx <ibrahem.h.basyone@gmail.com> | 2017-01-16 19:02:15 +0200 |
---|---|---|
committer | Haru <haru@dotalux.com> | 2018-02-17 15:10:12 +0100 |
commit | 400076f4e26a121746545cf9f6483ca8e93896b5 (patch) | |
tree | a962db573b5483965bcc5e74c127aec08ff350f7 /src | |
parent | 59f02f1d406a1238f90d072949ac07279a90e9ea (diff) | |
download | hercules-400076f4e26a121746545cf9f6483ca8e93896b5.tar.gz hercules-400076f4e26a121746545cf9f6483ca8e93896b5.tar.bz2 hercules-400076f4e26a121746545cf9f6483ca8e93896b5.tar.xz hercules-400076f4e26a121746545cf9f6483ca8e93896b5.zip |
New mapcache system
* Now each map is in separated file so now it will be easier to know
which map got updated or delete on updates
* Now there's md5 checksum check for each map
* Now the map cache is platform safe, the old format was not packed
which may result in undefined behavior
* The map cache tool got converted into hercules plugin
Signed-off-by: hemagx <ibrahem.h.basyone@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/map/map.c | 221 | ||||
-rw-r--r-- | src/map/map.h | 32 | ||||
-rw-r--r-- | src/plugins/mapcache.c | 370 |
3 files changed, 506 insertions, 117 deletions
diff --git a/src/map/map.c b/src/map/map.c index bb367a08d..780e6f535 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -66,6 +66,7 @@ #include "common/core.h" #include "common/ers.h" #include "common/grfio.h" +#include "common/md5calc.h" #include "common/memmgr.h" #include "common/nullpo.h" #include "common/random.h" @@ -2899,21 +2900,26 @@ int map_cell2gat(struct mapcell cell) { ShowWarning("map_cell2gat: cell has no matching gat type\n"); return 1; // default to 'wall' } -void map_cellfromcache(struct map_data *m) { - struct map_cache_map_info *info; +/** + * Extracts a map's cell data from its compressed mapcache. + * + * @param[in, out] m The target map. + */ +void map_cellfromcache(struct map_data *m) +{ nullpo_retv(m); - info = (struct map_cache_map_info *)m->cellPos; - if (info) { + if (m->cell_buf.data != NULL) { char decode_buffer[MAX_MAP_SIZE]; unsigned long size, xy; int i; - size = (unsigned long)info->xs*(unsigned long)info->ys; + size = (unsigned long)m->xs * (unsigned long)m->ys; // TO-DO: Maybe handle the scenario, if the decoded buffer isn't the same size as expected? [Shinryo] - grfio->decode_zip(decode_buffer, &size, m->cellPos+sizeof(struct map_cache_map_info), info->len); + grfio->decode_zip(decode_buffer, &size, m->cell_buf.data, m->cell_buf.len); + CREATE(m->cell, struct mapcell, size); // Set cell properties @@ -3253,97 +3259,125 @@ int map_eraseipport(unsigned short map_index, uint32 ip, uint16 port) { return 0; } -/*========================================== - * [Shinryo]: Init the mapcache - *------------------------------------------*/ -char *map_init_mapcache(FILE *fp) { - struct map_cache_main_header header; - size_t size = 0; - char *buffer; - - // No file open? Return.. - nullpo_ret(fp); +/** + * Reads a map's compressed cell data from its mapcache file. + * + * @param[in,out] m The target map. + * @return The loading success state. + * @retval false in case of errors. + */ +bool map_readfromcache(struct map_data *m) +{ + unsigned int file_size; + char file_path[256]; + FILE *fp = NULL; + bool retval = false; + int16 version; - // Get file size - fseek(fp, 0, SEEK_END); - size = ftell(fp); - fseek(fp, 0, SEEK_SET); + nullpo_retr(false, m); - // Allocate enough space - CREATE(buffer, char, size); + snprintf(file_path, sizeof(file_path), "%s%s%s.%s", "maps/", DBPATH, m->name, "mcache"); + fp = fopen(file_path, "rb"); - // No memory? Return.. - nullpo_ret(buffer); + if (fp == NULL) { + ShowWarning("map_readfromcache: Could not open the mapcache file for map '%s' at path '%s'.\n", m->name, file_path); + return false; + } - // Read file into buffer.. - if(fread(buffer, sizeof(char), size, fp) != size) { - ShowError("map_init_mapcache: Could not read entire mapcache file\n"); - aFree(buffer); - return NULL; + if (fread(&version, sizeof(version), 1, fp) < 1) { + ShowError("map_readfromcache: Could not read file version for map '%s'.\n", m->name); + fclose(fp); + return false; } - rewind(fp); + fseek(fp, 0, SEEK_END); + file_size = (unsigned int)ftell(fp); + fseek(fp, 0, SEEK_SET); // Rewind file pointer before passing it to the read function. - // Get main header to verify if data is corrupted - if( fread(&header, sizeof(header), 1, fp) != 1 ) { - ShowError("map_init_mapcache: Error obtaining main header!\n"); - aFree(buffer); - return NULL; - } - if( GetULong((unsigned char *)&(header.file_size)) != size ) { - ShowError("map_init_mapcache: Map cache is corrupted!\n"); - aFree(buffer); - return NULL; + switch(version) { + case 1: + retval = map->readfromcache_v1(fp, m, file_size); + break; + default: + ShowError("map_readfromcache: Mapcache file has unknown version '%d' for map '%s'.\n", version, m->name); + break; } - return buffer; + fclose(fp); + return retval; } -/*========================================== - * Map cache reading - * [Shinryo]: Optimized some behaviour to speed this up - *==========================================*/ -int map_readfromcache(struct map_data *m, char *buffer) { - int i; - struct map_cache_main_header *header = (struct map_cache_main_header *)buffer; - struct map_cache_map_info *info = NULL; - char *p = buffer + sizeof(struct map_cache_main_header); - - nullpo_ret(m); - nullpo_ret(buffer); - - for(i = 0; i < header->map_count; i++) { - info = (struct map_cache_map_info *)p; +/** + * Reads a map's compressed cell data from its mapcache file (file format + * version 1). + * + * @param[in] fp The file pointer to read from (opened and closed by + * the caller). + * @param[in,out] m The target map. + * @param[in] file_size The size of the file to load from. + * @return The loading success state. + * @retval false in case of errors. + */ +bool map_readfromcache_v1(FILE *fp, struct map_data *m, unsigned int file_size) +{ + struct map_cache_header mheader = { 0 }; + uint8 md5buf[16] = { 0 }; + int map_size; + nullpo_retr(false, fp); + nullpo_retr(false, m); + + if (file_size <= sizeof(mheader) || fread(&mheader, sizeof(mheader), 1, fp) < 1) { + ShowError("map_readfromcache: Failed to read cache header for map '%s'.\n", m->name); + return false; + } - if( strcmp(m->name, info->name) == 0 ) - break; // Map found + if (mheader.len <= 0) { + ShowError("map_readfromcache: A file with negative or zero compressed length passed '%d'.\n", mheader.len); + return false; + } - // Jump to next entry.. - p += sizeof(struct map_cache_map_info) + info->len; + if (file_size < sizeof(mheader) + mheader.len) { + ShowError("map_readfromcache: An incomplete file passed for map '%s'.\n", m->name); + return false; } - if( info && i < header->map_count ) { - unsigned long size; + if (mheader.ys <= 0 || mheader.xs <= 0) { + ShowError("map_readfromcache: A map with invalid size passed '%s' xs: '%d' ys: '%d'.\n", m->name, mheader.xs, mheader.ys); + return false; + } - if( info->xs <= 0 || info->ys <= 0 ) - return 0;// Invalid + m->xs = mheader.xs; + m->ys = mheader.ys; + map_size = (int)mheader.xs * (int)mheader.ys; - m->xs = info->xs; - m->ys = info->ys; - size = (unsigned long)info->xs*(unsigned long)info->ys; + if (map_size > MAX_MAP_SIZE) { + ShowWarning("map_readfromcache: %s exceeded MAX_MAP_SIZE of %d.\n", m->name, MAX_MAP_SIZE); + return false; + } - if(size > MAX_MAP_SIZE) { - ShowWarning("map_readfromcache: %s exceeded MAX_MAP_SIZE of %d\n", info->name, MAX_MAP_SIZE); - return 0; // Say not found to remove it from list.. [Shinryo] - } + CREATE(m->cell_buf.data, uint8, mheader.len); + m->cell_buf.len = mheader.len; + if (fread(m->cell_buf.data, mheader.len, 1, fp) < 1) { + ShowError("mapreadfromcache: Could not load the compressed cell data for map '%s'.\n", m->name); + aFree(m->cell_buf.data); + m->cell_buf.data = NULL; + m->cell_buf.len = 0; + return false; + } - m->cellPos = p; - m->cell = (struct mapcell *)0xdeadbeaf; + md5->binary(m->cell_buf.data, m->cell_buf.len, md5buf); - return 1; + if (memcmp(md5buf, mheader.md5_checksum, sizeof(md5buf)) != 0) { + ShowError("mapreadfromcache: md5 checksum check failed for map '%s'\n", m->name); + aFree(m->cell_buf.data); + m->cell_buf.data = NULL; + m->cell_buf.len = 0; + return false; } - return 0; // Not found + m->cell = (struct mapcell *)0xdeadbeaf; + + return true; } /** @@ -3747,26 +3781,12 @@ void map_removemapdb(struct map_data *m) { *--------------------------------------*/ int map_readallmaps (void) { int i; - FILE* fp=NULL; int maps_removed = 0; - if( map->enable_grf ) + if (map->enable_grf) { ShowStatus("Loading maps (using GRF files)...\n"); - else { - char mapcachefilepath[256]; - safesnprintf(mapcachefilepath, 256, "%s/%s%s", map->db_path, DBPATH, "map_cache.dat"); - ShowStatus("Loading maps (using %s as map cache)...\n", mapcachefilepath); - if( (fp = fopen(mapcachefilepath, "rb")) == NULL ) { - ShowFatalError("Unable to open map cache file "CL_WHITE"%s"CL_RESET"\n", mapcachefilepath); - exit(EXIT_FAILURE); //No use launching server if maps can't be read. - } - - // Init mapcache data.. [Shinryo] - map->cache_buffer = map->init_mapcache(fp); - if(!map->cache_buffer) { - ShowFatalError("Failed to initialize mapcache data (%s)..\n", mapcachefilepath); - exit(EXIT_FAILURE); - } + } else { + ShowStatus("Loading maps using map cache files...\n"); } for(i = 0; i < map->count; i++) { @@ -3780,7 +3800,7 @@ int map_readallmaps (void) { if( ! (map->enable_grf? map->readgat(&map->list[i]) - :map->readfromcache(&map->list[i], map->cache_buffer)) + :map->readfromcache(&map->list[i])) ) { map->delmapid(i); maps_removed++; @@ -3822,10 +3842,6 @@ int map_readallmaps (void) { // intialization and configuration-dependent adjustments of mapflags map->flags_init(); - if( !map->enable_grf ) { - fclose(fp); - } - // finished map loading ShowInfo("Successfully loaded '"CL_WHITE"%d"CL_RESET"' maps."CL_CLL"\n",map->count); instance->start_id = map->count; // Next Map Index will be instances @@ -6062,6 +6078,11 @@ int do_final(void) { ers_destroy(map->iterator_ers); ers_destroy(map->flooritem_ers); + for (i = 0; i < map->count; ++i) { + if (map->list[i].cell_buf.data != NULL) + aFree(map->list[i].cell_buf.data); + map->list[i].cell_buf.len = 0; + } aFree(map->list); if( map->block_free ) @@ -6069,9 +6090,6 @@ int do_final(void) { if( map->bl_list ) aFree(map->bl_list); - if( !map->enable_grf ) - aFree(map->cache_buffer); - aFree(map->MAP_CONF_NAME); aFree(map->BATTLE_CONF_FILENAME); aFree(map->ATCOMMAND_CONF_FILENAME); @@ -6687,7 +6705,6 @@ void map_defaults(void) { map->list = NULL; map->iterator_ers = NULL; - map->cache_buffer = NULL; map->flooritem_ers = NULL; /* */ @@ -6834,8 +6851,8 @@ void map_defaults(void) { map->iwall_nextxy = map_iwall_nextxy; map->create_map_data_other_server = create_map_data_other_server; map->eraseallipport_sub = map_eraseallipport_sub; - map->init_mapcache = map_init_mapcache; map->readfromcache = map_readfromcache; + map->readfromcache_v1 = map_readfromcache_v1; map->addmap = map_addmap; map->delmapid = map_delmapid; map->zone_db_clear = map_zone_db_clear; diff --git a/src/map/map.h b/src/map/map.h index facf1d921..5c4c6d59d 100644 --- a/src/map/map.h +++ b/src/map/map.h @@ -909,7 +909,10 @@ struct map_data { /* */ int (*getcellp)(struct map_data* m, const struct block_list *bl, int16 x, int16 y, cell_chk cellchk); void (*setcell) (int16 m, int16 x, int16 y, cell_t cell, bool flag); - char *cellPos; + struct { + uint8 *data; + int len; + } cell_buf; /* ShowEvent Data Cache */ struct questinfo *qi_data; @@ -1064,20 +1067,20 @@ struct charid2nick { struct charid_request* requests;// requests of notification on this nick }; -// This is the main header found at the very beginning of the map cache -struct map_cache_main_header { - uint32 file_size; - uint16 map_count; -}; - -// This is the header appended before every compressed map cells info in the map cache -struct map_cache_map_info { - char name[MAP_NAME_LENGTH]; +// New mcache file format header +#if !defined(sun) && (!defined(__NETBSD__) || __NetBSD_Version__ >= 600000000) // NetBSD 5 and Solaris don't like pragma pack but accept the packed attribute +#pragma pack(push, 1) +#endif // not NetBSD < 6 / Solaris +struct map_cache_header { + int16 version; + uint8 md5_checksum[16]; int16 xs; int16 ys; int32 len; -}; - +} __attribute__((packed)); +#if !defined(sun) && (!defined(__NETBSD__) || __NetBSD_Version__ >= 600000000) // NetBSD 5 and Solaris don't like pragma pack but accept the packed attribute +#pragma pack(pop) +#endif // not NetBSD < 6 / Solaris /*===================================== * Interface : map.h @@ -1167,7 +1170,6 @@ END_ZEROED_BLOCK; struct map_data *list; /* [Ind/Hercules] */ struct eri *iterator_ers; - char *cache_buffer; // Has the uncompressed gat data of all maps, so just one allocation has to be made /* */ struct eri *flooritem_ers; /* */ @@ -1317,8 +1319,8 @@ END_ZEROED_BLOCK; void (*iwall_nextxy) (int16 x, int16 y, int8 dir, int pos, int16 *x1, int16 *y1); struct DBData (*create_map_data_other_server) (union DBKey key, va_list args); int (*eraseallipport_sub) (union DBKey key, struct DBData *data, va_list va); - char* (*init_mapcache) (FILE *fp); - int (*readfromcache) (struct map_data *m, char *buffer); + bool (*readfromcache) (struct map_data *m); + bool (*readfromcache_v1) (FILE *fp, struct map_data *m, unsigned int file_size); int (*addmap) (const char *mapname); void (*delmapid) (int id); void (*zone_db_clear) (void); diff --git a/src/plugins/mapcache.c b/src/plugins/mapcache.c new file mode 100644 index 000000000..95e6ead04 --- /dev/null +++ b/src/plugins/mapcache.c @@ -0,0 +1,370 @@ +/** +* This file is part of Hercules. +* http://herc.ws - http://github.com/HerculesWS/Hercules +* +* Copyright (C) 2013-2015 Hercules Dev Team +* +* Hercules is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +/** + * Mapcache Plugin + * This Plugin is made to handle the creation and update the new format of mapcache + * it also handles the convertion from the old to the new mapcache format + **/ + +#include "common/hercules.h" /* Should always be the first Hercules file included! (if you don't make it first, you won't be able to use interfaces) */ + +#include "common/memmgr.h" +#include "common/md5calc.h" +#include "common/nullpo.h" +#include "common/grfio.h" +#include "common/utils.h" +#include "map/map.h" + +#include "common/HPMDataCheck.h" /* should always be the last Hercules file included! (if you don't make it last, it'll intentionally break compile time) */ + +#include <stdio.h> +#include <string.h> + +HPExport struct hplugin_info pinfo = { + "Mapcache", ///< Plugin name + SERVER_TYPE_MAP, ///< Which server types this plugin works with? + "1.0.0", ///< Plugin version + HPM_VERSION, ///< HPM Version (don't change, macro is automatically updated) +}; + +/** + * Yes.. old mapcache was never packed, and we loaded and wrote a compiler paded structs + * DON'T BLAME IF SOMETHING EXPLODED [hemagx] + **/ +// This is the main header found at the very beginning of the map cache +struct old_mapcache_main_header { + uint32 file_size; + uint16 map_count; +}; + +// This is the header appended before every compressed map cells info in the map cache +struct old_mapcache_map_info { + char name[MAP_NAME_LENGTH]; + int16 xs; + int16 ys; + int32 len; +}; + +/** + * + */ + +#define NO_WATER 1000000 + +VECTOR_DECL(char *) maplist; + + +/** + * code from utlis.c until it's interfaced + **/ + +#ifdef WIN32 +# ifndef F_OK +# define F_OK 0x0 +# endif /* F_OK */ +#else +# include <unistd.h> +#endif + + +// Reads an uint32 in little-endian from the buffer +uint32 GetULong(const unsigned char* buf) +{ + return (((uint32)(buf[0]))) + | (((uint32)(buf[1])) << 0x08) + | (((uint32)(buf[2])) << 0x10) + | (((uint32)(buf[3])) << 0x18); +} + +// Reads a float (32 bits) from the buffer +float GetFloat(const unsigned char* buf) +{ + uint32 val = GetULong(buf); + return *((float*)(void*)&val); +} + +bool write_mapcache(const uint8 *buf, int32 buf_len, bool is_compressed, const char *mapname, int16 xs, int16 ys) +{ + struct map_cache_header header = { 0 }; + char file_path[255]; + int mapname_len; + unsigned long compressed_buf_len; + uint8 *compressed_buf = NULL; + FILE *new_mapcache_fp; + + nullpo_retr(false, buf); + nullpo_retr(false, mapname); + + mapname_len = (int)strlen(mapname); + + if (mapname_len > MAP_NAME_LENGTH || mapname_len < 1) { + ShowError("write_mapcache: A map with invalid name length has beed passed '%s' size (%d)\n", mapname, mapname_len); + return false; + } + + if ((xs < 0 || ys < 0)) { + ShowError("write_mapcache: '%s' given with invalid coords xs = %d, ys = %d\n", mapname, xs, ys); + return false; + } + + if (((int)xs * ys) > MAX_MAP_SIZE) { + ShowError("write_mapcache: map '%s' exceeded MAX_MAP_SIZE of %d\n", mapname, MAX_MAP_SIZE); + return false; + } + + + + snprintf(file_path, sizeof(file_path), "%s%s%s.%s", "maps/", DBPATH, mapname, "mcache"); + new_mapcache_fp = fopen(file_path, "w+b"); + + if (new_mapcache_fp == NULL) { + ShowWarning("Could not open file '%s', map cache creating failed.\n", file_path); + return false; + } + + header.version = 0x1; + header.xs = xs; + header.ys = ys; + + if (is_compressed == false) { + compressed_buf_len = buf_len * 2; //Creating big enough buffer to ensure ability to hold compressed data + CREATE(compressed_buf, uint8, compressed_buf_len); + grfio->encode_zip(compressed_buf, &compressed_buf_len, buf, buf_len); + + header.len = (int)compressed_buf_len; + md5->binary(compressed_buf, (int)compressed_buf_len, header.md5_checksum); + } else { + header.len = buf_len; + md5->binary(buf, buf_len, header.md5_checksum); + } + + + fwrite(&header, sizeof(header), 1, new_mapcache_fp); + if (is_compressed == false) + fwrite(compressed_buf, compressed_buf_len, 1, new_mapcache_fp); + else + fwrite(buf, buf_len, 1, new_mapcache_fp); + + fclose(new_mapcache_fp); + if (compressed_buf != NULL) + aFree(compressed_buf); + + return true; +} + +bool convert_old_mapcache(void) +{ + const char *path = "db/"DBPATH"map_cache.dat"; + FILE *mapcache_fp = fopen(path, "rb"); + struct old_mapcache_main_header header = { 0 }; + uint8 *p, *cursor; + uint32 file_size; + int i; + + if (mapcache_fp == NULL) { + ShowError("Could not open mapcache file in the following path '%s' \n", path); + return false; + } + + if (fread(&header, sizeof(header), 1, mapcache_fp) != 1) { + ShowError("Failed to read mapcache header \n"); + fclose(mapcache_fp); + return false; + } + + fseek(mapcache_fp, 0, SEEK_END); + file_size = (int)ftell(mapcache_fp); + fseek(mapcache_fp, 0, SEEK_SET); + + if (file_size != header.file_size) { + ShowError("File size in mapcache header doesn't match actual mapcache file size \n"); + fclose(mapcache_fp); + return false; + } + + CREATE(p, uint8, header.file_size); + cursor = p + sizeof(header); + + if (fread(p, header.file_size, 1, mapcache_fp) != 1) { + ShowError("Could not load mapcache file into memory, fread failed.\n"); + aFree(p); + fclose(mapcache_fp); + return false; + } + + for (i = 0; i < header.map_count; ++i) { + struct old_mapcache_map_info *info = (struct old_mapcache_map_info *)cursor; + + ShowStatus("Creating mapcache: %s"CL_CLL"\n", info->name); + + if (write_mapcache((uint8 *)info + sizeof(*info), info->len, true, info->name, info->xs, info->ys) == false) { + ShowError("failed To convert map '%s'\n", info->name); + } + + cursor += sizeof(*info) + info->len; + } + + aFree(p); + fclose(mapcache_fp); + return true; +} + +bool mapcache_read_maplist(const char *filepath) +{ + char line[4096] = { 0 }; + FILE *fp; + + nullpo_retr(false, filepath); + + fp = fopen(filepath, "r"); + + if (fp == NULL) + return false; + + while (fgets(line, sizeof(line), fp)) { + char map_name[MAP_NAME_LENGTH]; + if (line[0] == '/' && line[1] == '/') + continue; + + if (sscanf(line, "%11s", map_name) == 1) { + VECTOR_ENSURE(maplist, 1, 1); + VECTOR_PUSH(maplist, aStrdup(map_name)); + } + } + + ShowStatus("%d map loaded from map_index.txt\n", VECTOR_LENGTH(maplist)); + fclose(fp); + return true; +} + +bool mapcache_cache_map(const char *mapname) +{ + char filepath[255] = { 0 }; + uint8 *gat, *rsw, *gat_cursor; + uint8 *cells; + int water_height, map_size, xy; + int16 xs, ys; + + nullpo_retr(false, mapname); + + snprintf(filepath, sizeof(filepath), "data\\%s.gat", mapname); + gat = grfio_read(filepath); + + if (gat == NULL) { + ShowError("mapcache_cache_map: Could not read %s, aborting caching map %s\n", filepath, mapname); + return false; + } + + snprintf(filepath, sizeof(filepath), "data\\%s.rsw", mapname); + + rsw = grfio_read(filepath); + + if (rsw == NULL) { + water_height = NO_WATER; + } else { + water_height = (int)GetFloat(rsw + 166); + aFree(rsw); + } + + xs = (int16)GetULong(gat + 6); + ys = (int16)GetULong(gat + 10); + + if (xs <= 0 || ys <= 0) { + ShowError("mapcache_cache_map: map '%s' doesn't have valid size xs = %d, ys = %d\n", mapname, xs, ys); + aFree(gat); + return false; + } + + map_size = xs * ys; + CREATE(cells, uint8, map_size); + + gat_cursor = gat; + for (xy = 0; xy < map_size; ++xy) { + float height = GetFloat(gat_cursor + 14); + uint32 type = GetULong(gat_cursor + 30); + gat_cursor += 20; + + if (type == 0 && water_height != NO_WATER && height > water_height) + type = 3; + + cells[xy] = (uint8)type; + } + + write_mapcache(cells, map_size, false, mapname, xs, ys); + + aFree(gat); + aFree(cells); + return true; +} + +bool mapcache_rebuild(void) +{ + int i; + char file_path[255]; + + if (mapcache_read_maplist("db/map_index.txt") == false) { + ShowError("mapcache_rebuild: Could not read maplist, aborting\n"); + return false; + } + + for (i = 0; i < VECTOR_LENGTH(maplist); ++i) { + snprintf(file_path, sizeof(file_path), "%s%s%s.%s", "maps/", DBPATH, VECTOR_INDEX(maplist, i), "mcache"); + if (access(file_path, F_OK) == 0 && remove(file_path) != 0) { + ShowWarning("mapcache_rebuild: Could not remove file '%s' \n", file_path); + } + } + + grfio->init("conf/grf-files.txt"); + + for (i = 0; i < VECTOR_LENGTH(maplist); ++i) { + ShowStatus("Creating mapcache: %s"CL_CLL"\r", VECTOR_INDEX(maplist, i)); + mapcache_cache_map(VECTOR_INDEX(maplist, i)); + } + + return true; +} + +CMDLINEARG(convertmapcache) +{ + map->minimal = true; + return convert_old_mapcache(); +} + +CMDLINEARG(rebuild) +{ + map->minimal = true; + return mapcache_rebuild(); +} + +HPExport void server_preinit(void) +{ + addArg("--convert-old-mapcache", false, convertmapcache, + "Converts an old db/"DBPATH"map_cache.dat file to the new format."); + addArg("--rebuild-mapcache", false, rebuild, + "Rebuilds the entire mapcache folder (maps/"DBPATH"), using db/map_index.txt as index."); + + VECTOR_INIT(maplist); +} + +HPExport void plugin_final(void) +{ + VECTOR_CLEAR(maplist); +} |