Skip to content

Commit

Permalink
Clean up to how map cache is loaded by mode (#6984)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
aleos89 committed May 26, 2022
1 parent cc93494 commit 9bf78ee
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
Binary file added db/map_cache.dat
Binary file not shown.
Binary file modified db/pre-re/map_cache.dat
Binary file not shown.
Binary file modified db/re/map_cache.dat
Binary file not shown.
7 changes: 5 additions & 2 deletions src/common/grfio.cpp
Expand Up @@ -471,15 +471,17 @@ 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;
}

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 );
Expand Down Expand Up @@ -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
}

Expand Down
55 changes: 23 additions & 32 deletions src/map/map.cpp
Expand Up @@ -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);
}

Expand All @@ -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);
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9bf78ee

Please sign in to comment.