Kaynağa Gözat

Clean up to how map cache is loaded by mode (#6984)

* The db/(pre-)re/map_cache.dat now contain maps that are only different between the modes.
* Moves all general maps into db/map_cache.dat for loading across both modes.
* Adds support for the new RSW water height.
* Adds an error message when loading GRFs if the file size is over 2GB.
Aleos 3 yıl önce
ebeveyn
işleme
9bf78ee5a3
5 değiştirilmiş dosya ile 28 ekleme ve 34 silme
  1. BIN
      db/map_cache.dat
  2. BIN
      db/pre-re/map_cache.dat
  3. BIN
      db/re/map_cache.dat
  4. 5 2
      src/common/grfio.cpp
  5. 23 32
      src/map/map.cpp

BIN
db/map_cache.dat


BIN
db/pre-re/map_cache.dat


BIN
db/re/map_cache.dat


+ 5 - 2
src/common/grfio.cpp

@@ -471,7 +471,7 @@ int32 grfio_read_rsw_water_level( const char* fname ){
 
 	uint16 version = ( rsw[4] << 8 ) | rsw[5];
 
-	if( version < 0x104 || version > 0x202 ){
+	if( version < 0x104 || version > 0x205 ){
 		ShowError( "grfio_read_rsw_water_level: Unsupported RSW version 0x%04x in file %s\n", version, fname );
 		aFree( rsw );
 		return RSW_NO_WATER;
@@ -479,7 +479,9 @@ int32 grfio_read_rsw_water_level( const char* fname ){
 
 	int32 level;
 
-	if( version >= 0x202 ){
+	if( version >= 0x205 ){
+		level = (int32)*(float*)( rsw + 171 );
+	} else if( version >= 0x202 ){
 		level = (int32)*(float*)( rsw + 167 );
 	}else{
 		level = (int32)*(float*)( rsw + 166 );
@@ -543,6 +545,7 @@ static int grfio_entryread(const char* grfname, int gentry)
 	if( strcmp((const char*)grf_header,"Master of Magic") != 0 || fseek(fp,getlong(grf_header+0x1e),SEEK_CUR) != 0 ) {
 		fclose(fp);
 		ShowError("GRF %s read error\n", grfname);
+		ShowError("GRF possibly over 2GB in size.\n");
 		return 2;	// 2:file format error
 	}
 

+ 23 - 32
src/map/map.cpp

@@ -3742,40 +3742,33 @@ void map_removemapdb(struct map_data *m)
  *--------------------------------------*/
 int map_readallmaps (void)
 {
-	FILE* fp=NULL;
+	FILE* fp;
 	// Has the uncompressed gat data of all maps, so just one allocation has to be made
-	char *map_cache_buffer[2] = {
-		NULL,
-		NULL
-	};
-	char map_cache_decode_buffer[MAX_MAP_SIZE];
+	std::vector<char *> map_cache_buffer = {};
 
 	if( enable_grf )
 		ShowStatus("Loading maps (using GRF files)...\n");
 	else {
-		const char* mapcachefilepath[] = {
+		// Load the map cache files in reverse order to account for import
+		const std::vector<std::string> mapcachefilepath = {
+			"db/" DBIMPORT "/map_cache.dat",
 			"db/" DBPATH "map_cache.dat",
-			"db/" DBIMPORT "/map_cache.dat"
+			"db/map_cache.dat",
 		};
 
-		for( int i = 0; i < 2; i++ ){
-			ShowStatus( "Loading maps (using %s as map cache)...\n", mapcachefilepath[i] );
+		for(const auto &mapdat : mapcachefilepath) {
+			ShowStatus( "Loading maps (using %s as map cache)...\n", mapdat.c_str() );
 
-			if( ( fp = fopen(mapcachefilepath[i], "rb") ) == NULL ){
-				if( i == 0 ){
-					ShowFatalError( "Unable to open map cache file " CL_WHITE "%s" CL_RESET "\n", mapcachefilepath[i] );
-					exit(EXIT_FAILURE); //No use launching server if maps can't be read.
-				}else{
-					ShowWarning( "Unable to open map cache file " CL_WHITE "%s" CL_RESET "\n", mapcachefilepath[i] );
-					break;
-				}
+			if( ( fp = fopen(mapdat.c_str(), "rb")) == nullptr) {
+				ShowFatalError( "Unable to open map cache file " CL_WHITE "%s" CL_RESET "\n", mapdat.c_str());
+				continue;
 			}
 
 			// Init mapcache data. [Shinryo]
-			map_cache_buffer[i] = map_init_mapcache(fp);
+			map_cache_buffer.push_back(map_init_mapcache(fp));
 
-			if( !map_cache_buffer[i] ) {
-				ShowFatalError( "Failed to initialize mapcache data (%s)..\n", mapcachefilepath[i] );
+			if( !map_cache_buffer.back() ) {
+				ShowFatalError( "Failed to initialize mapcache data (%s)..\n", mapdat.c_str());
 				exit(EXIT_FAILURE);
 			}
 
@@ -3790,6 +3783,7 @@ int map_readallmaps (void)
 		bool success = false;
 		unsigned short idx = 0;
 		struct map_data *mapdata = &map[i];
+		char map_cache_decode_buffer[MAX_MAP_SIZE];
 
 		// show progress
 		ShowStatus("Loading maps [%i/%i]: %s" CL_CLL "\r", i, map_num, mapdata->name);
@@ -3799,14 +3793,9 @@ int map_readallmaps (void)
 			success = map_readgat(mapdata) != 0;
 		}else{
 			// try to load the map
-			// Read from import first, in case of override
-			if( map_cache_buffer[1] != NULL ){
-				success = map_readfromcache( mapdata, map_cache_buffer[1], map_cache_decode_buffer ) != 0;
-			}
-
-			// Nothing was found in import - try to find it in the main file
-			if( !success ){
-				success = map_readfromcache( mapdata, map_cache_buffer[0], map_cache_decode_buffer ) != 0;
+			for (const auto &cache : map_cache_buffer) {
+				if ((success = map_readfromcache(mapdata, cache, map_cache_decode_buffer)) != 0)
+					break;
 			}
 		}
 
@@ -3855,10 +3844,12 @@ int map_readallmaps (void)
 
 	if( !enable_grf ) {
 		// The cache isn't needed anymore, so free it. [Shinryo]
-		if( map_cache_buffer[1] != NULL ){
-			aFree(map_cache_buffer[1]);
+		auto it = map_cache_buffer.begin();
+
+		while (it != map_cache_buffer.end()) {
+			aFree(*it);
+			it = map_cache_buffer.erase(it);
 		}
-		aFree(map_cache_buffer[0]);
 	}
 
 	if (maps_removed)