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.
This commit is contained in:
Aleos 2022-05-26 09:55:46 -04:00 committed by GitHub
parent cc93494cf9
commit 9bf78ee5a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 34 deletions

BIN
db/map_cache.dat Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

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

View File

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