bool StaticMapTree::CanLoadMap(const std::string& vmapPath, uint32 mapID, uint32 tileX, uint32 tileY) { std::string basePath = vmapPath; if (basePath.length() > 0 && (basePath[basePath.length() - 1] != '/' || basePath[basePath.length() - 1] != '\\')) { basePath.append("/"); } std::string fullname = basePath + VMapManager2::getMapFileName(mapID); bool success = true; FILE* rf = fopen(fullname.c_str(), "rb"); if (!rf) { return false; } // TODO: check magic number when implemented... char tiled; char chunk[8]; if (!readChunk(rf, chunk, VMAP_MAGIC, 8) || fread(&tiled, sizeof(char), 1, rf) != 1) { fclose(rf); return false; } if (tiled) { std::string tilefile = basePath + getTileFileName(mapID, tileX, tileY); FILE* tf = fopen(tilefile.c_str(), "rb"); if (!tf) { success = false; } else { if (!readChunk(tf, chunk, VMAP_MAGIC, 8)) { success = false; } fclose(tf); } } fclose(rf); return success; }
StaticMapTree::TileFileOpenResult StaticMapTree::OpenMapTileFile(std::string const& basePath, uint32 mapID, uint32 tileX, uint32 tileY, VMapManager2* vm) { TileFileOpenResult result; result.Name = basePath + getTileFileName(mapID, tileX, tileY); result.File = fopen(result.Name.c_str(), "rb"); if (!result.File) { int32 parentMapId = vm->getParentMapId(mapID); if (parentMapId != -1) { result.Name = basePath + getTileFileName(parentMapId, tileX, tileY); result.File = fopen(result.Name.c_str(), "rb"); } } return result; }
void StaticMapTree::UnloadMapTile(uint32 tileX, uint32 tileY, VMapManager2* vm) { uint32 tileID = packTileID(tileX, tileY); loadedTileMap::iterator tile = iLoadedTiles.find(tileID); if (tile == iLoadedTiles.end()) { VMAP_ERROR_LOG("misc", "StaticMapTree::UnloadMapTile() : trying to unload non-loaded tile - Map:%u X:%u Y:%u", iMapID, tileX, tileY); return; } if (tile->second) // file associated with tile { std::string tilefile = iBasePath + getTileFileName(iMapID, tileX, tileY); FILE* tf = fopen(tilefile.c_str(), "rb"); if (tf) { bool result=true; char chunk[8]; if (!readChunk(tf, chunk, VMAP_MAGIC, 8)) result = false; uint32 numSpawns; if (fread(&numSpawns, sizeof(uint32), 1, tf) != 1) result = false; for (uint32 i=0; i<numSpawns && result; ++i) { // read model spawns ModelSpawn spawn; result = ModelSpawn::readFromFile(tf, spawn); if (result) { // release model instance vm->releaseModelInstance(spawn.name); // update tree uint32 referencedNode; if (fread(&referencedNode, sizeof(uint32), 1, tf) != 1) result = false; else { if (!iLoadedSpawns.count(referencedNode)) VMAP_ERROR_LOG("misc", "StaticMapTree::UnloadMapTile() : trying to unload non-referenced model '%s' (ID:%u)", spawn.name.c_str(), spawn.ID); else if (--iLoadedSpawns[referencedNode] == 0) { iTreeValues[referencedNode].setUnloaded(); iLoadedSpawns.erase(referencedNode); } } } } fclose(tf); } } iLoadedTiles.erase(tile); TC_METRIC_EVENT("map_events", "UnloadMapTile", "Map: " + std::to_string(iMapID) + " TileX: " + std::to_string(tileX) + " TileY: " + std::to_string(tileY)); }
void StaticMapTree::unloadMap(uint32 tileX, uint32 tileY, VMapManager2 *vm) { if (!iIsTiled) return; uint32 tileID = packTileID(tileX, tileY); loadedTileMap::iterator tile = iLoadedTiles.find(tileID); if(tile == iLoadedTiles.end()) { std::cout << "WARNING: trying to unload non-loaded tile. Map:" << iMapID << " X:" << tileX << " Y:" << tileY << std::endl; return; } if(tile->second) // file associated with tile { std::string tilefile = iBasePath + getTileFileName(iMapID, tileX, tileY); FILE* tf = fopen(tilefile.c_str(), "rb"); if(tf) { bool result=true; while(result) { // read model spawns ModelSpawn spawn; result = ModelSpawn::readFromFile(tf, spawn); if(result) { // std::cout << "unloading '" << spawn.name << "'\n"; // release model instance vm->releaseModelInstance(spawn.name); // update tree uint32 nNodeVal=0, referencedNode; fread(&nNodeVal, sizeof(uint32), 1, tf); for(uint32 i=0; i<nNodeVal; ++i) { fread(&referencedNode, sizeof(uint32), 1, tf); if(!iLoadedSpawns.count(spawn.ID)) { std::cout << "error! trying to unload non-referenced model '" << spawn.name << "' (ID:" << spawn.ID << ")\n"; } else if(--iLoadedSpawns[spawn.ID] == 0) { //std::cout << "MapTree: removing '" << spawn.name << "' from tree\n"; iTreeValues[referencedNode].setUnloaded(); iLoadedSpawns.erase(spawn.ID); } } } } fclose(tf); } } iLoadedTiles.erase(tile); }
//========================================================= LoadResult StaticMapTree::CanLoadMap(const std::string &vmapPath, uint32 mapID, uint32 tileX, uint32 tileY, VMapManager2* vm) { std::string basePath = vmapPath; if (basePath.length() > 0 && basePath[basePath.length()-1] != '/' && basePath[basePath.length()-1] != '\\') basePath.push_back('/'); std::string fullname = basePath + VMapManager2::getMapFileName(mapID); LoadResult result = LoadResult::Success; FILE* rf = fopen(fullname.c_str(), "rb"); if (!rf) return LoadResult::FileNotFound; char chunk[8]; if (!readChunk(rf, chunk, VMAP_MAGIC, 8)) { fclose(rf); return LoadResult::VersionMismatch; } FILE* tf = OpenMapTileFile(basePath, mapID, tileX, tileY, vm).File; if (!tf) return LoadResult::FileNotFound; else { std::string tilefile = basePath + getTileFileName(mapID, tileX, tileY); FILE* tf = fopen(tilefile.c_str(), "rb"); if (!tf) result = LoadResult::FileNotFound; else { if (!readChunk(tf, chunk, VMAP_MAGIC, 8)) result = LoadResult::VersionMismatch; fclose(tf); } } fclose(rf); return result; }
bool StaticMapTree::LoadMapTile(uint32 tileX, uint32 tileY, VMapManager2* vm) { if (!iIsTiled) { // currently, core creates grids for all maps, whether it has terrain tiles or not // so we need "fake" tile loads to know when we can unload map geometry iLoadedTiles[packTileID(tileX, tileY)] = false; return true; } if (!iTreeValues) { ERROR_LOG("StaticMapTree::LoadMapTile(): Tree has not been initialized! [%u,%u]", tileX, tileY); return false; } bool result = true; std::string tilefile = iBasePath + getTileFileName(iMapID, tileX, tileY); FILE* tf = fopen(tilefile.c_str(), "rb"); if (tf) { char chunk[8]; if (!readChunk(tf, chunk, VMAP_MAGIC, 8)) { result = false; } uint32 numSpawns; if (result && fread(&numSpawns, sizeof(uint32), 1, tf) != 1) { result = false; } for (uint32 i = 0; i < numSpawns && result; ++i) { // read model spawns ModelSpawn spawn; result = ModelSpawn::readFromFile(tf, spawn); if (result) { // acquire model instance WorldModel* model = vm->acquireModelInstance(iBasePath, spawn.name); if (!model) { ERROR_LOG("StaticMapTree::LoadMapTile() could not acquire WorldModel pointer for '%s'!", spawn.name.c_str()); } // update tree uint32 referencedVal; size_t fileRead = fread(&referencedVal, sizeof(uint32), 1, tf); if (!iLoadedSpawns.count(referencedVal) || fileRead <= 0) { #ifdef VMAP_DEBUG if (referencedVal > iNTreeValues) { DEBUG_LOG("invalid tree element! (%u/%u)", referencedVal, iNTreeValues); continue; } #endif iTreeValues[referencedVal] = ModelInstance(spawn, model); iLoadedSpawns[referencedVal] = 1; } else { ++iLoadedSpawns[referencedVal]; #ifdef VMAP_DEBUG if (iTreeValues[referencedVal].ID != spawn.ID) { DEBUG_LOG("Error: trying to load wrong spawn in node!"); } else if (iTreeValues[referencedVal].name != spawn.name) { DEBUG_LOG("Error: name mismatch on GUID=%u", spawn.ID); } #endif } } } iLoadedTiles[packTileID(tileX, tileY)] = true; fclose(tf); } else { iLoadedTiles[packTileID(tileX, tileY)] = false; } return result; }
bool StaticMapTree::LoadMapTile(uint32 tileX, uint32 tileY, VMapManager2* vm) { if (!iIsTiled) { // currently, core creates grids for all maps, whether it has terrain tiles or not // so we need "fake" tile loads to know when we can unload map geometry iLoadedTiles[packTileID(tileX, tileY)] = false; return true; } if (!iTreeValues) { sLog->outError(LOG_FILTER_GENERAL, "StaticMapTree::LoadMapTile() : tree has not been initialized [%u, %u]", tileX, tileY); return false; } bool result = true; std::string tilefile = iBasePath + getTileFileName(iMapID, tileX, tileY); FILE* tf = fopen(tilefile.c_str(), "rb"); if (tf) { char chunk[8]; if (!readChunk(tf, chunk, VMAP_MAGIC, 8)) result = false; uint32 numSpawns = 0; if (result && fread(&numSpawns, sizeof(uint32), 1, tf) != 1) result = false; for (uint32 i=0; i<numSpawns && result; ++i) { // read model spawns ModelSpawn spawn; result = ModelSpawn::readFromFile(tf, spawn); if (result) { // acquire model instance WorldModel* model = vm->acquireModelInstance(iBasePath, spawn.name); if (!model) sLog->outError(LOG_FILTER_GENERAL, "StaticMapTree::LoadMapTile() : could not acquire WorldModel pointer [%u, %u]", tileX, tileY); // update tree uint32 referencedVal; if (fread(&referencedVal, sizeof(uint32), 1, tf) == 1) { if (!iLoadedSpawns.count(referencedVal)) { #ifdef VMAP_DEBUG if (referencedVal > iNTreeValues) { sLog->outDebug(LOG_FILTER_MAPS, "StaticMapTree::LoadMapTile() : invalid tree element (%u/%u)", referencedVal, iNTreeValues); continue; } #endif iTreeValues[referencedVal] = ModelInstance(spawn, model); iLoadedSpawns[referencedVal] = 1; } else { ++iLoadedSpawns[referencedVal]; #ifdef VMAP_DEBUG if (iTreeValues[referencedVal].ID != spawn.ID) sLog->outDebug(LOG_FILTER_MAPS, "StaticMapTree::LoadMapTile() : trying to load wrong spawn in node"); else if (iTreeValues[referencedVal].name != spawn.name) sLog->outDebug(LOG_FILTER_MAPS, "StaticMapTree::LoadMapTile() : name collision on GUID=%u", spawn.ID); #endif } } else result = false; } } iLoadedTiles[packTileID(tileX, tileY)] = true; fclose(tf); } else iLoadedTiles[packTileID(tileX, tileY)] = false; return result; }
bool StaticMapTree::loadMap(uint32 tileX, uint32 tileY, VMapManager2 *vm) { if (!iIsTiled) return true; if (!iTreeValues) { std::cout << "Tree has not been initialized!\n"; return false; } bool result = true; std::string tilefile = iBasePath + getTileFileName(iMapID, tileX, tileY); FILE* tf = fopen(tilefile.c_str(), "rb"); if(tf) { while(result) { // read model spawns ModelSpawn spawn; result = ModelSpawn::readFromFile(tf, spawn); if(result) { // acquire model instance WorldModel *model = vm->acquireModelInstance(iBasePath, spawn.name); if(!model) std::cout << "error: could not acquire WorldModel pointer!\n"; // update tree uint32 nNodeVal=0, referencedVal; fread(&nNodeVal, sizeof(uint32), 1, tf); #ifdef VMAP_DEBUG if(nNodeVal != 1) std::cout << "unexpected amount of affected NodeVals! (" << nNodeVal << ")\n"; #endif for (uint32 i=0; i<nNodeVal; ++i) { fread(&referencedVal, sizeof(uint32), 1, tf); if (!iLoadedSpawns.count(spawn.ID)) { #ifdef VMAP_DEBUG if (referencedVal > iNTreeValues) { std::cout << "invalid tree element! (" << referencedVal << "/" << iNTreeValues << ")\n"; continue; } #endif iTreeValues[referencedVal] = ModelInstance(spawn, model); iLoadedSpawns[spawn.ID] = 1; } else { ++iLoadedSpawns[spawn.ID]; #ifdef VMAP_DEBUG if (iTreeValues[referencedVal].ID != spawn.ID) std::cout << "error: trying to load wrong spawn in node!\n"; else if (iTreeValues[referencedVal].name != spawn.name) std::cout << "error: name collision on GUID="<< spawn.ID << "\n"; #endif } } } } iLoadedTiles[packTileID(tileX, tileY)] = true; fclose(tf); } else iLoadedTiles[packTileID(tileX, tileY)] = false; return result; }