瀏覽代碼

Added support for import map_cache building
* Fixes #4234.
* Added maps defined in the import map_index to be saved into the import map_cache.
* Expanded detail of warning messages.
Thanks to @llchrisll!

aleos89 5 年之前
父節點
當前提交
61ffb1ce3d
共有 1 個文件被更改,包括 42 次插入42 次删除
  1. 42 42
      src/tool/mapcache.cpp

+ 42 - 42
src/tool/mapcache.cpp

@@ -149,7 +149,8 @@ int find_map(char *name)
 	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(fread(&info, sizeof(info), 1, map_cache_fp) != 1)
+			printf("An error has occured in fread while reading \"%s\"\n", map_cache_fp);
 		if(strcmp(name, info.name) == 0) // Map found
 			return 1;
 		else // Map not found, jump to the beginning of the next map info header
@@ -194,39 +195,39 @@ void process_args(int argc, char *argv[])
 
 int do_init(int argc, char** argv)
 {
-	/* setup pre-defined, #define-dependant */
-	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.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.c_str());
-	if(!rebuild) {
-		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;
-		} else
-			fclose(map_cache_fp);
-	}
-	if(rebuild)
-		map_cache_fp = fopen(map_cache_file.c_str(), "w+b");
-	else
-		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.c_str());
-		exit(EXIT_FAILURE);
-	}
-
-	// Open the map list
-	FILE *list;
 	std::vector<std::string> directories = { std::string(db_path) + "/",  std::string(db_path) + "/" + std::string(DBIMPORT) + "/" };
 
 	for (const auto &directory : directories) {
+		/* setup pre-defined, #define-dependant */
+		map_cache_file = directory + "map_cache.dat";
+
+		// Process the command-line arguments
+		process_args(argc, argv);
+
+		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.c_str());
+		if (!rebuild) {
+			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;
+			} else
+				fclose(map_cache_fp);
+		}
+		if (rebuild)
+			map_cache_fp = fopen(map_cache_file.c_str(), "w+b");
+		else
+			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.c_str());
+			exit(EXIT_FAILURE);
+		}
+
+		// Open the map list
+		FILE *list;
 		std::string filename = directory + map_list_file;
 
 		ShowStatus("Opening map list: %s\n", filename.c_str());
@@ -241,7 +242,8 @@ int do_init(int argc, char** argv)
 			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"); }
+			if (fread(&header, sizeof(struct main_header), 1, map_cache_fp) != 1)
+				printf("An error has occured while reading \"%s\"\n", map_cache_fp);
 			header.file_size = GetULong((unsigned char *)&(header.file_size));
 			header.map_count = GetUShort((unsigned char *)&(header.map_count));
 		}
@@ -249,8 +251,7 @@ int do_init(int argc, char** argv)
 		// Read and process the map list
 		char line[1024];
 
-		while (fgets(line, sizeof(line), list))
-		{
+		while (fgets(line, sizeof(line), list)) {
 			if (line[0] == '/' && line[1] == '/')
 				continue;
 
@@ -271,21 +272,20 @@ int do_init(int argc, char** argv)
 			else if (read_map(name, &map)) {
 				cache_map(name, &map);
 				ShowInfo("Map '" CL_WHITE "%s" CL_RESET "' successfully cached.\n", name);
-			}
-			else
+			} else
 				ShowError("Map '" CL_WHITE "%s" CL_RESET "' not found!\n", name);
 
 		}
 
 		ShowStatus("Closing map list: %s\n", filename.c_str());
 		fclose(list);
-	}
 
-	// Write the main header and close the map cache
-	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);
+		// Write the main header and close the map cache
+		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);
+	}
 
 	ShowStatus("Finalizing grfio\n");
 	grfio_final();