소스 검색

Updated mapcache tool (#3406)

* Fixes #3046.
* Mapcache tool now reads the import directory.
Thanks to @secretdataz suggestion!
Aleos 6 년 전
부모
커밋
a5af358bf7
1개의 변경된 파일75개의 추가작업 그리고 72개의 파일을 삭제
  1. 75 72
      src/tool/mapcache.cpp

+ 75 - 72
src/tool/mapcache.cpp

@@ -4,11 +4,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-
+#include <string>
 #ifndef _WIN32
 #include <unistd.h>
 #endif
+#include <vector>
 
+#include "../common/core.hpp"
 #include "../common/grfio.hpp"
 #include "../common/malloc.hpp"
 #include "../common/mmo.hpp"
@@ -17,9 +19,9 @@
 
 #define NO_WATER 1000000
 
-char grf_list_file[256] = "conf/grf-files.txt";
-char map_list_file[256] = "db/map_index.txt";
-char map_cache_file[256];
+std::string grf_list_file = "conf/grf-files.txt";
+std::string map_list_file = "map_index.txt";
+std::string map_cache_file;
 int rebuild = 0;
 
 FILE *map_cache_fp;
@@ -174,18 +176,16 @@ char *remove_extension(char *mapname)
 // Processes command-line arguments
 void process_args(int argc, char *argv[])
 {
-	int i;
-
-	for(i = 0; i < argc; i++) {
+	for(int i = 0; i < argc; i++) {
 		if(strcmp(argv[i], "-grf") == 0) {
 			if(++i < argc)
-				strcpy(grf_list_file, argv[i]);
+				grf_list_file = argv[i];
 		} else if(strcmp(argv[i], "-list") == 0) {
 			if(++i < argc)
-				strcpy(map_list_file, argv[i]);
+				map_list_file = argv[i];
 		} else if(strcmp(argv[i], "-cache") == 0) {
 			if(++i < argc)
-				strcpy(map_cache_file, argv[i]);
+				map_cache_file = argv[i];
 		} else if(strcmp(argv[i], "-rebuild") == 0)
 			rebuild = 1;
 	}
@@ -194,30 +194,19 @@ void process_args(int argc, char *argv[])
 
 int do_init(int argc, char** argv)
 {
-	FILE *list;
-	char line[1024];
-	struct map_data map;
-	char name[MAP_NAME_LENGTH_EXT];
-
 	/* setup pre-defined, #define-dependant */
-	sprintf(map_cache_file,"db/%s/map_cache.dat",
-#ifdef RENEWAL
-			"re"
-#else
-			"pre-re"
-#endif
-			);
+	map_cache_file = std::string(db_path) + "/" + std::string(DBPATH) + "map_cache.dat";
 
 	// Process the command-line arguments
 	process_args(argc, argv);
 
-	ShowStatus("Initializing grfio with %s\n", grf_list_file);
-	grfio_init(grf_list_file);
+	ShowStatus("Initializing grfio with %s\n", grf_list_file.c_str());
+	grfio_init(grf_list_file.c_str());
 
 	// Attempt to open the map cache file and force rebuild if not found
-	ShowStatus("Opening map cache: %s\n", map_cache_file);
+	ShowStatus("Opening map cache: %s\n", map_cache_file.c_str());
 	if(!rebuild) {
-		map_cache_fp = fopen(map_cache_file, "rb");
+		map_cache_fp = fopen(map_cache_file.c_str(), "rb");
 		if(map_cache_fp == NULL) {
 			ShowNotice("Existing map cache not found, forcing rebuild mode\n");
 			rebuild = 1;
@@ -225,61 +214,75 @@ int do_init(int argc, char** argv)
 			fclose(map_cache_fp);
 	}
 	if(rebuild)
-		map_cache_fp = fopen(map_cache_file, "w+b");
+		map_cache_fp = fopen(map_cache_file.c_str(), "w+b");
 	else
-		map_cache_fp = fopen(map_cache_file, "r+b");
+		map_cache_fp = fopen(map_cache_file.c_str(), "r+b");
 	if(map_cache_fp == NULL) {
-		ShowError("Failure when opening map cache file %s\n", map_cache_file);
+		ShowError("Failure when opening map cache file %s\n", map_cache_file.c_str());
 		exit(EXIT_FAILURE);
 	}
 
 	// Open the map list
-	ShowStatus("Opening map list: %s\n", map_list_file);
-	list = fopen(map_list_file, "r");
-	if(list == NULL) {
-		ShowError("Failure when opening maps list file %s\n", map_list_file);
-		exit(EXIT_FAILURE);
-	}
-
-	// Initialize the main header
-	if(rebuild) {
-		header.file_size = sizeof(struct main_header);
-		header.map_count = 0;
-	} else {
-		if(fread(&header, sizeof(struct main_header), 1, map_cache_fp) != 1){ printf("An error as occured while reading map_cache_fp \n"); }
-		header.file_size = GetULong((unsigned char *)&(header.file_size));
-		header.map_count = GetUShort((unsigned char *)&(header.map_count));
-	}
-
-	// Read and process the map list
-	while(fgets(line, sizeof(line), list))
-	{
-		if(line[0] == '/' && line[1] == '/')
-			continue;
-
-		if(sscanf(line, "%15s", name) < 1)
-			continue;
-
-		if(strcmp("map:", name) == 0 && sscanf(line, "%*s %15s", name) < 1)
-			continue;
-
-		name[MAP_NAME_LENGTH_EXT-1] = '\0';
-		remove_extension(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
-			ShowError("Map '" CL_WHITE "%s" CL_RESET "' not found!\n", name);
-
+	FILE *list;
+	std::vector<std::string> directories = { std::string(db_path) + "/",  std::string(db_path) + "/" + std::string(DBIMPORT) + "/" };
+
+	for (const auto &directory : directories) {
+		std::string filename = directory + map_list_file;
+
+		ShowStatus("Opening map list: %s\n", filename.c_str());
+		list = fopen(filename.c_str(), "r");
+		if (list == NULL) {
+			ShowError("Failure when opening maps list file %s\n", filename.c_str());
+			exit(EXIT_FAILURE);
+		}
+
+		// Initialize the main header
+		if (rebuild) {
+			header.file_size = sizeof(struct main_header);
+			header.map_count = 0;
+		} else {
+			if (fread(&header, sizeof(struct main_header), 1, map_cache_fp) != 1) { printf("An error as occured while reading map_cache_fp \n"); }
+			header.file_size = GetULong((unsigned char *)&(header.file_size));
+			header.map_count = GetUShort((unsigned char *)&(header.map_count));
+		}
+
+		// Read and process the map list
+		char line[1024];
+
+		while (fgets(line, sizeof(line), list))
+		{
+			if (line[0] == '/' && line[1] == '/')
+				continue;
+
+			char name[MAP_NAME_LENGTH_EXT];
+
+			if (sscanf(line, "%15s", name) < 1)
+				continue;
+
+			if (strcmp("map:", name) == 0 && sscanf(line, "%*s %15s", name) < 1)
+				continue;
+
+			struct map_data map;
+
+			name[MAP_NAME_LENGTH_EXT - 1] = '\0';
+			remove_extension(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
+				ShowError("Map '" CL_WHITE "%s" CL_RESET "' not found!\n", name);
+
+		}
+
+		ShowStatus("Closing map list: %s\n", filename.c_str());
+		fclose(list);
 	}
 
-	ShowStatus("Closing map list: %s\n", map_list_file);
-	fclose(list);
-
 	// Write the main header and close the map cache
-	ShowStatus("Closing map cache: %s\n", map_cache_file);
+	ShowStatus("Closing map cache: %s\n", map_cache_file.c_str());
 	fseek(map_cache_fp, 0, SEEK_SET);
 	fwrite(&header, sizeof(struct main_header), 1, map_cache_fp);
 	fclose(map_cache_fp);