diff options
Diffstat (limited to 'src/tool')
-rw-r--r-- | src/tool/Makefile.in | 9 | ||||
-rw-r--r-- | src/tool/mapcache.c | 86 |
2 files changed, 56 insertions, 39 deletions
diff --git a/src/tool/Makefile.in b/src/tool/Makefile.in index 2d7699db0..b0c07b2fb 100644 --- a/src/tool/Makefile.in +++ b/src/tool/Makefile.in @@ -9,13 +9,16 @@ CONFIG_H = $(wildcard $(CONFIG_D)/*.h) $(wildcard $(CONFIG_D)/*/*.h) COMMON_D = ../common COMMON_H = $(wildcard $(COMMON_D)/*.h) SYSINFO_INC = $(COMMON_D)/sysinfo.inc +COMMON_INCLUDE = -I.. -LIBCONFIG_D = ../../3rdparty/libconfig +THIRDPARTY_D = ../../3rdparty +THIRDPARTY_INCLUDE = -I$(THIRDPARTY_D) + +LIBCONFIG_D = $(THIRDPARTY_D)/libconfig LIBCONFIG_OBJ = $(addprefix $(LIBCONFIG_D)/, libconfig.o grammar.o scanctx.o \ scanner.o strbuf.o) LIBCONFIG_H = $(addprefix $(LIBCONFIG_D)/, libconfig.h grammar.h parsectx.h \ scanctx.h scanner.h strbuf.h wincompat.h) -LIBCONFIG_INCLUDE = -I$(LIBCONFIG_D) MAPCACHE_OBJ = obj_all/mapcache.o MAPCACHE_C = mapcache.c @@ -70,7 +73,7 @@ obj_all: obj_all/%.o: %.c $(MAPCACHE_H) $(COMMON_H) $(CONFIG_H) $(LIBCONFIG_H) | obj_all @echo " CC $<" - @$(CC) @CFLAGS@ @DEFS@ $(LIBCONFIG_INCLUDE) @CPPFLAGS@ -c $(OUTPUT_OPTION) $< + @$(CC) @CFLAGS@ @DEFS@ $(COMMON_INCLUDE) $(THIRDPARTY_INCLUDE) @CPPFLAGS@ -c $(OUTPUT_OPTION) $< # missing common object files $(COMMON_D)/obj_all/common_mini.a: diff --git a/src/tool/mapcache.c b/src/tool/mapcache.c index 4b2b4bd49..72457b73a 100644 --- a/src/tool/mapcache.c +++ b/src/tool/mapcache.c @@ -3,21 +3,17 @@ #define HERCULES_CORE -#include "../config/core.h" // RENEWAL +#include "common/cbasetypes.h" +#include "common/core.h" +#include "common/grfio.h" +#include "common/malloc.h" +#include "common/mmo.h" +#include "common/showmsg.h" +#include "common/strlib.h" +#include "common/utils.h" #include <stdio.h> #include <stdlib.h> -#include <string.h> - -#include "../common/cbasetypes.h" -#include "../common/core.h" -#include "../common/grfio.h" -#include "../common/malloc.h" -#include "../common/mmo.h" -#include "../common/showmsg.h" -#include "../common/utils.h" -#include "../common/strlib.h" - #ifndef _WIN32 #include <unistd.h> #endif @@ -114,8 +110,14 @@ int read_map(char *name, struct map_data *m) return 1; } -// Adds a map to the cache -void cache_map(char *name, struct map_data *m) +/** + * Adds a map to the cache. + * + * @param name The map name. + * @param m Map data to cache. + * @retval true if the map was successfully added to the cache. + */ +bool cache_map(char *name, struct map_data *m) { struct map_info info; unsigned long len; @@ -130,14 +132,18 @@ void cache_map(char *name, struct map_data *m) // Fill the map header safestrncpy(info.name, name, MAP_NAME_LENGTH); if (strlen(name) > MAP_NAME_LENGTH) // It does not hurt to warn that there are maps with name longer than allowed. - ShowWarning("Map name '%s' (length %"PRIuS") is too long. Truncating to '%s' (lentgh %d).\n", + ShowWarning("Map name '%s' (length %"PRIuS") is too long. Truncating to '%s' (length %d).\n", name, strlen(name), info.name, MAP_NAME_LENGTH); info.xs = MakeShortLE(m->xs); info.ys = MakeShortLE(m->ys); info.len = MakeLongLE((uint32)len); // Append map header then compressed cells at the end of the file - fseek(map_cache_fp, header.file_size, SEEK_SET); + if (fseek(map_cache_fp, header.file_size, SEEK_SET) != 0) { + aFree(write_buf); + aFree(m->cells); + return false; + } fwrite(&info, sizeof(struct map_info), 1, map_cache_fp); fwrite(write_buf, 1, len, map_cache_fp); header.file_size += sizeof(struct map_info) + len; @@ -146,26 +152,34 @@ void cache_map(char *name, struct map_data *m) aFree(write_buf); aFree(m->cells); - return; + return true; } -// Checks whether a map is already is the cache -int find_map(char *name) +/** + * Checks whether a map is already is the cache. + * + * @param name The map name. + * @retval true if the map is already cached. + */ +bool find_map(char *name) { int i; struct map_info info; - fseek(map_cache_fp, sizeof(struct main_header), SEEK_SET); - - for(i = 0; i < header.map_count; i++) { - if(fread(&info, sizeof(info), 1, map_cache_fp) != 1) printf("An error as occured in fread while reading map_cache\n"); - if(strcmp(name, info.name) == 0) // Map found - return 1; - else // Map not found, jump to the beginning of the next map info header - fseek(map_cache_fp, GetLong((unsigned char *)&(info.len)), SEEK_CUR); + if (fseek(map_cache_fp, sizeof(struct main_header), SEEK_SET) != 0) + return false; + + for (i = 0; i < header.map_count; i++) { + if (fread(&info, sizeof(info), 1, map_cache_fp) != 1) + printf("An error as occured in fread while reading map_cache\n"); + if (strcmp(name, info.name) == 0) // Map found + return true; + // Map not found, jump to the beginning of the next map info header + if (fseek(map_cache_fp, GetLong((unsigned char *)&(info.len)), SEEK_CUR) != 0) + return false; } - return 0; + return false; } // Cuts the extension from a map name @@ -174,7 +188,7 @@ char *remove_extension(char *mapname) char *ptr, *ptr2; ptr = strchr(mapname, '.'); if (ptr) { //Check and remove extension. - while (ptr[1] && (ptr2 = strchr(ptr+1, '.'))) + while (ptr[1] && (ptr2 = strchr(ptr+1, '.')) != NULL) ptr = ptr2; //Skip to the last dot. if (strcmp(ptr,".gat") == 0) *ptr = '\0'; //Remove extension. @@ -242,7 +256,6 @@ void cmdline_args_init_local(void) CMDLINEARG_DEF2(map-list, maplist, "Alternative map list file", CMDLINE_OPT_NORMAL|CMDLINE_OPT_PARAM); CMDLINEARG_DEF2(map-cache, mapcache, "Alternative map cache file", CMDLINE_OPT_NORMAL|CMDLINE_OPT_PARAM); CMDLINEARG_DEF2(rebuild, rebuild, "Forces a rebuild of the map cache, rather than only adding missing maps", CMDLINE_OPT_NORMAL); - } int do_init(int argc, char** argv) @@ -314,14 +327,15 @@ int do_init(int argc, char** argv) name[MAP_NAME_LENGTH_EXT-1] = '\0'; remove_extension(name); - if(find_map(name)) + if (find_map(name)) { ShowInfo("Map '"CL_WHITE"%s"CL_RESET"' already in cache.\n", name); - else if(read_map(name, &map)) { - cache_map(name, &map); - ShowInfo("Map '"CL_WHITE"%s"CL_RESET"' successfully cached.\n", name); - } else + } else if(!read_map(name, &map)) { ShowError("Map '"CL_WHITE"%s"CL_RESET"' not found!\n", name); - + } else if (!cache_map(name, &map)) { + ShowError("Map '"CL_WHITE"%s"CL_RESET"' failed to cache (write error).\n", name); + } else { + ShowInfo("Map '"CL_WHITE"%s"CL_RESET"' successfully cached.\n", name); + } } ShowStatus("Closing map list: %s\n", map_list_file); |