DataBuffer* GfxFeatureMapReplyPacket::getGfxFeatureMapData() { bool zipped = false; DataBuffer* maybeZippedBuf = getGfxFeatureMapData( zipped ); if ( zipped ) { // Zipped, means that we should unzip it. // Check unzipped length. int unzippedLength = GunzipUtil::origLength( maybeZippedBuf->getBufferAddress(), maybeZippedBuf->getBufferSize() ); // Unzip. DataBuffer* unzippedBuf = new DataBuffer( unzippedLength ); int retVal = GunzipUtil::gunzip( unzippedBuf->getBufferAddress(), unzippedBuf->getBufferSize(), maybeZippedBuf->getBufferAddress(), maybeZippedBuf->getBufferSize() ); delete maybeZippedBuf; if ( retVal < 0 ) { delete unzippedBuf; return NULL; } // Better read past the bytes just to make sure.. unzippedBuf->readPastBytes( unzippedLength ); return unzippedBuf; } else { // Was already unzipped. return maybeZippedBuf; } }
std::auto_ptr< BitBuffer> convertToBitBuffer( DataBuffer& buffer ) { std::auto_ptr< BitBuffer > retBuffer; // Copy the buffer if it is mmaped, which hopefully will not happen // to often. if ( buffer.isMMaped() ) { uint8* data = new uint8[ buffer.getBufferSize() ]; memcpy( data, buffer.getBufferAddress(), buffer.getBufferSize() ); retBuffer = std::auto_ptr< BitBuffer > ( new BitBuffer( data, buffer.getBufferSize() ) ); } else { // take ownership of the memory uint32 size = buffer.getBufferSize(); retBuffer = std::auto_ptr< BitBuffer > ( new BitBuffer( buffer.release(), size ) ); retBuffer->setSelfAlloc( true ); } return retBuffer; }
bool POIInfo::load( const MC2String& filename, uint32 offset ) { DataBuffer fileBuff; fileBuff.memMapFile( filename.c_str() ); DataBuffer offsetBuff( fileBuff.getBufferAddress() + offset, fileBuff.getBufferSize() - offset ); load( offsetBuff ); return true; }
DataBuffer* DataBufferCreator:: getDataBufferFromProgram(const char* command) { FILE* program = popen( command, "r" ); if ( program == NULL ) { return NULL; } vector<uint8> byteBuffer; // 10 megabytes of temporary buffer const int tmpBufSize = 4 * 4096; uint8* tmpBuf = new uint8[tmpBufSize]; while ( !feof(program) ) { int readSize = fread( tmpBuf, 1, tmpBufSize, program ); byteBuffer.insert( byteBuffer.end(), tmpBuf, tmpBuf + readSize ); } delete [] tmpBuf; int progRes = pclose(program); if ( progRes == 0 && byteBuffer.size() > 2 ) { DataBuffer* db = new VectorDataBuffer( byteBuffer ); mc2dbg << "[DataBufferCreator]: Read " << db->getBufferSize() << " bytes from " << command << endl; return db; } else { mc2log << error << "[DataBufferCreator]: " << command << " returned to few bytes" << endl; return NULL; } }
bool GenericMapHeader::internalLoad(DataBuffer& dataBuffer) { CLOCK_MAPLOAD(DebugClock loadClock); mc2dbg4 << "GenericMapHeader::internalLoad" << endl; // *************************************************************** // Read the general map information // *************************************************************** uint32 currentMapID = dataBuffer.readNextLong(); // do check without map set bits if (currentMapID != MapBits::getMapIDWithoutMapSet( m_mapID )) { mc2log << fatal << "GenericMapHeader::createFromDataBuffer, currentMapID: " << prettyMapIDFill(currentMapID) << " does not match filename, mapID: " << prettyMapIDFill(m_mapID) << ". EXITING!" << endl; exit(1); } m_totalMapSize = dataBuffer.readNextLong(); // WARNING: Not set in file!!! mc2dbg << "[GMH]: Total map size variable = " << m_totalMapSize << " and size of DataBuffer = " << dataBuffer.getBufferSize() << endl; // Read the creationtime of this map m_creationTime = dataBuffer.readNextLong(); // Read some data about all the items in this map m_country = StringTable::countryCode(dataBuffer.readNextLong()); byte flagByte = dataBuffer.readNextByte(); byte nbrNativeLanguages = dataBuffer.readNextByte(); byte nbrCurrencies = dataBuffer.readNextByte(); m_loadedVersion= dataBuffer.readNextByte(); if ( m_loadedVersion < 3 ) { m_driveOnRightSide = (flagByte != 0); } else { m_driveOnRightSide = BitUtility::getBit( flagByte, 0 ); m_groupsInLocationNameOrder = BitUtility::getBit( flagByte, 1 ); } m_nbrAllocators = dataBuffer.readNextLong(); mc2dbg << "Nbr allocators: " << m_nbrAllocators << endl; // The languages m_nativeLanguages.resize( nbrNativeLanguages ); for (NativeLanguageIndexes::size_type i=0; i<nbrNativeLanguages; i++) { m_nativeLanguages[ i ] = static_cast<NativeLanguageIndexes::value_type> ( dataBuffer.readNextLong() ); } // The currencies m_currency = new Vector(nbrCurrencies); for (uint32 i=0; i<nbrCurrencies; i++) { m_currency->addLast(dataBuffer.readNextLong()); } // Variable header: delete [] m_name; delete [] m_origin; delete [] m_mapCountryDir; switch ( m_loadedVersion ) { case ( 0 ) : m_name = StringUtility::newStrDup( "" ); m_origin = StringUtility::newStrDup( "" ); m_trueCreationTime = MAX_UINT32; m_waspTime = MAX_UINT32; m_dynamicExtradataTime = MAX_UINT32; m_utf8Strings = false; m_mapFiltered = false; m_mapGfxDataFiltered = false; m_mapCountryDir = StringUtility::newStrDup(""); break; case ( 1 ) : { uint32 offset = dataBuffer.getCurrentOffset(); uint32 variableHeaderSize = dataBuffer.readNextLong(); m_name = StringUtility::newStrDup( dataBuffer.readNextString() ); m_origin = StringUtility::newStrDup( "" ); m_trueCreationTime = MAX_UINT32; m_waspTime = MAX_UINT32; m_dynamicExtradataTime = MAX_UINT32; m_utf8Strings = false; m_mapFiltered = false; m_mapGfxDataFiltered = false; m_mapCountryDir = StringUtility::newStrDup(""); dataBuffer.readPastBytes( variableHeaderSize - ( dataBuffer.getCurrentOffset() - offset ) ); } break; case ( 2 ) : case ( 3 ) : { uint32 offset = dataBuffer.getCurrentOffset(); uint32 variableHeaderSize = dataBuffer.readNextLong(); m_name = StringUtility::newStrDup( dataBuffer.readNextString() ); m_origin = StringUtility::newStrDup( dataBuffer.readNextString() ); m_trueCreationTime = MAX_UINT32; m_waspTime = MAX_UINT32; m_dynamicExtradataTime = MAX_UINT32; m_utf8Strings = false; m_mapFiltered = false; m_mapGfxDataFiltered = false; m_mapCountryDir = StringUtility::newStrDup(""); dataBuffer.readPastBytes( variableHeaderSize - ( dataBuffer.getCurrentOffset() - offset ) ); } break; case ( 4 ) : { uint32 offset = dataBuffer.getCurrentOffset(); uint32 variableHeaderSize = dataBuffer.readNextLong(); m_name = StringUtility::newStrDup( dataBuffer.readNextString() ); m_origin = StringUtility::newStrDup( dataBuffer.readNextString() ); m_trueCreationTime = dataBuffer.readNextLong(); m_waspTime = dataBuffer.readNextLong(); m_dynamicExtradataTime = dataBuffer.readNextLong(); m_utf8Strings = false; m_mapFiltered = false; m_mapGfxDataFiltered = false; m_mapCountryDir = StringUtility::newStrDup(""); dataBuffer.readPastBytes( variableHeaderSize - ( dataBuffer.getCurrentOffset() - offset ) ); } break; case (5) : case (6) : // GenericMap::m_userRightsTable case (7) : // GenericMap::m_adminAreaCentres default : { uint32 offset = dataBuffer.getCurrentOffset(); uint32 variableHeaderSize = dataBuffer.readNextLong(); m_name = StringUtility::newStrDup( dataBuffer.readNextString() ); m_origin = StringUtility::newStrDup( dataBuffer.readNextString() ); m_trueCreationTime = dataBuffer.readNextLong(); m_waspTime = dataBuffer.readNextLong(); m_dynamicExtradataTime = dataBuffer.readNextLong(); m_utf8Strings = dataBuffer.readNextBool(); m_mapFiltered = dataBuffer.readNextBool(); m_mapGfxDataFiltered = dataBuffer.readNextBool(); m_mapCountryDir = StringUtility::newStrDup( dataBuffer.readNextString() ); dataBuffer.readPastBytes( variableHeaderSize - ( dataBuffer.getCurrentOffset() - offset ) ); } break; } // Read string with copyright-information for images of this map m_copyrightString = StringUtility::newStrDup(dataBuffer.readNextString()); CLOCK_MAPLOAD(mc2log << "[" << m_mapID << "] Header loaded in " << loadClock << ", m_loadedVersion=" << int(m_loadedVersion) << endl ); return true; }
bool OldCountryOverviewMap::internalLoad(DataBuffer& dataBuffer) { // Load the general map data bool retVal = OldGenericMap::internalLoad(dataBuffer); if (retVal) { // Read the number of countries uint32 nbrCountries = dataBuffer.readNextLong(); if (nbrCountries > 0) { CLOCK_MAPLOAD(uint32 startTime = TimeUtility::getCurrentMicroTime()); // Insert the mapID's into the m_mapsInCountry-array for (uint32 i=0; i<nbrCountries; i++) { struct mapnotice_t notice; notice.mapID = dataBuffer.readNextLong(); notice.creationTime = dataBuffer.readNextLong(); notice.maxLat = dataBuffer.readNextLong(); notice.minLon = dataBuffer.readNextLong(); notice.minLat = dataBuffer.readNextLong(); notice.maxLon = dataBuffer.readNextLong(); mc2dbg2 << "[COMap] Creation time(0x" << hex << notice.mapID << dec << "):" << notice.creationTime << endl; m_mapsInCountry.push_back(notice); mc2dbg2 << " Added map with ID=" << notice.mapID << endl; } CLOCK_MAPLOAD(mc2log << "[" << m_mapID << "] Countries read in " << (TimeUtility::getCurrentMicroTime()-startTime)/1000.0 << " ms" << endl; startTime = TimeUtility::getCurrentMicroTime()); // Read the number of original IDs uint32 nbrOrigIDs = dataBuffer.readNextLong(); // Read the original IDs of the items in this map mc2dbg2 << "To insert " << nbrOrigIDs << " original IDs" << endl; for (uint32 i=0; i<nbrOrigIDs; i++) { uint32 newID = dataBuffer.readNextLong(); struct originalIDs_t origIDs; origIDs.origMapID = dataBuffer.readNextLong(); origIDs.origItemID = dataBuffer.readNextLong(); mc2dbg4 << " ID: " << newID << " <==> " << origIDs.origMapID << "." << origIDs.origItemID << endl; m_originalIDs.insert(make_pair(newID, origIDs)); } CLOCK_MAPLOAD(mc2log << "[" << m_mapID << "] Orig IDs read in " << (TimeUtility::getCurrentMicroTime()-startTime)/1000.0 << " ms" << endl; startTime = TimeUtility::getCurrentMicroTime()); // Create the simplified representation of the country gfxdata. // If it exists on disk, then read from there, otherwise filter. if ( (dataBuffer.getBufferSize() - dataBuffer.getCurrentOffset()) > 0) { // Read the size.(not used now) dataBuffer.readNextLong(); // Ok, read from disk. // Nbr levels of stack. uint32 levelsOfStack = dataBuffer.readNextLong(); // Nbr polygons per level. m_nbrGfxPolygons = dataBuffer.readNextLong(); // Allocate stack matrix. m_simplifiedGfxStack = new Stack**[levelsOfStack]; // For each level. for (byte i = 0; i < levelsOfStack; i++) { // And each stack for that level m_simplifiedGfxStack[i] = new Stack*[m_nbrGfxPolygons]; for (uint32 j = 0; j < m_nbrGfxPolygons; j++) { Stack* curStack = new Stack; // The number of elements of this stack. uint32 nbrElems = dataBuffer.readNextLong(); // And read all elements for (uint32 k = 0; k < nbrElems; k++) { curStack->push(dataBuffer.readNextLong()); } m_simplifiedGfxStack[i][j] = curStack; } } } else { // Could not read from disk, filter now instead. mc2log << info << "OldCountryOverviewMap::internalLoad(), " << "Error reading simplified representation of " << "country gfxdata. Exits." << endl; //makeSimplifiedCountryGfx(); exit(1); } CLOCK_MAPLOAD(mc2log << "[" << m_mapID << "] Country GfxData " << "created/read in " << (TimeUtility::getCurrentMicroTime()-startTime)/1000.0 << " ms" << endl; startTime = TimeUtility::getCurrentMicroTime()); } else {
bool ModuleMap::saveCachedMap(DataBuffer& mapBuffer, const GenericMap* theMap, uint32 loadMapRequestType, uint32 mapVersion, uint32 generatorVersion, bool update, byte zoomlevel) { uint32 mapID = theMap->getMapID(); MC2String finalFileName( ModuleMap::getCacheFilename(mapID, loadMapRequestType, zoomlevel) ); if ( update && cacheIsUpToDate(theMap->getMapID(), theMap->getFilename(), loadMapRequestType, zoomlevel) ){ return true; } const char* fileName = finalFileName.c_str(); MC2String tmpDir = ModuleMap::getCachePath(); if ( tmpDir[0] == '\0' ) { mc2log << info << "[ModuleMap]: No cachepath set - will not save " << "map 0x" << hex << mapID << dec << endl; return false; } else { mc2log << info << "[ModuleMap]: Will try to save " << MC2CITE(fileName) << endl; } char tempTemplate[1024]; sprintf(tempTemplate, "%scachingXXXXXX", tmpDir.c_str()); int tmpDesc = mkstemp(tempTemplate); MC2String tempName = MC2String(tempTemplate); // Remove the old file if there is one ( to make space for new map ) mc2dbg << "[ModuleMap]: Removing " << MC2CITE(fileName) << " if it exists" << endl; unlink(fileName); int headerSize; byte* header = makeHeader( headerSize, mapID, mapVersion, generatorVersion); FILE* writeFile = NULL; if ( tmpDesc < 0 ) { mc2dbg << "[ModuleMap]: Could not make tempfile" << endl; writeFile = NULL; return false; } else { writeFile = fdopen(tmpDesc, "w"); if ( header ) { if ( fwrite(header, headerSize, 1, writeFile) != 1 ) { mc2dbg << "[ModuleMap]: Could not write header: " << strerror(errno) << endl; fclose(writeFile); unlink(tempName.c_str()); writeFile = NULL; return false; } } else { mc2log << "[ModuleMap]: Header is NULL" << endl; return false; } } delete [] header; MC2_ASSERT( writeFile != NULL ); // Now the header should be written. if ( fwrite(mapBuffer.getBufferAddress(), mapBuffer.getBufferSize(), 1, writeFile) != 1 ) { fclose(writeFile); unlink(tempName.c_str()); writeFile = NULL; mc2dbg << "[ModuleMap]: Could not write map data" << endl; return false; } else { rename(tempName.c_str(), fileName); chmod (fileName, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) ); fclose(writeFile); mc2log << info << "[ModuleMap]: Map 0x" << hex << mapID << dec << " saved." << endl; return true; } }