本文整理汇总了C++中FileStreamPtr::close方法的典型用法代码示例。如果您正苦于以下问题:C++ FileStreamPtr::close方法的具体用法?C++ FileStreamPtr::close怎么用?C++ FileStreamPtr::close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileStreamPtr
的用法示例。
在下文中一共展示了FileStreamPtr::close方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: saveDat
void ThingTypeManager::saveDat(std::string fileName)
{
if(!m_datLoaded)
stdext::throw_exception("failed to save, dat is not loaded");
try {
FileStreamPtr fin = g_resources.createFile(fileName);
if(!fin)
stdext::throw_exception(stdext::format("failed to open file '%s' for write", fileName));
fin->cache();
fin->addU32(m_datSignature);
for(int category = 0; category < ThingLastCategory; ++category)
fin->addU16(m_thingTypes[category].size() - 1);
for(int category = 0; category < ThingLastCategory; ++category) {
uint16 firstId = 1;
if(category == ThingCategoryItem)
firstId = 100;
for(uint16 id = firstId; id < m_thingTypes[category].size(); ++id)
m_thingTypes[category][id]->serialize(fin);
}
fin->flush();
fin->close();
} catch(std::exception& e) {
g_logger.error(stdext::format("Failed to save '%s': %s", fileName, e.what()));
}
}
示例2: saveSpr
void SpriteManager::saveSpr(std::string fileName)
{
if(!m_loaded)
stdext::throw_exception("failed to save, spr is not loaded");
try {
FileStreamPtr fin = g_resources.createFile(fileName);
if(!fin)
stdext::throw_exception(stdext::format("failed to open file '%s' for write", fileName));
fin->cache();
fin->addU32(m_signature);
if(g_game.getFeature(Otc::GameSpritesU32))
fin->addU32(m_spritesCount);
else
fin->addU16(m_spritesCount);
uint32 offset = fin->tell();
uint32 spriteAddress = offset + 4 * m_spritesCount;
for(int i = 1; i <= m_spritesCount; i++)
fin->addU32(0);
for(int i = 1; i <= m_spritesCount; i++) {
m_spritesFile->seek((i - 1) * 4 + m_spritesOffset);
uint32 fromAdress = m_spritesFile->getU32();
if(fromAdress != 0) {
fin->seek(offset + (i - 1) * 4);
fin->addU32(spriteAddress);
fin->seek(spriteAddress);
m_spritesFile->seek(fromAdress);
fin->addU8(m_spritesFile->getU8());
fin->addU8(m_spritesFile->getU8());
fin->addU8(m_spritesFile->getU8());
uint16 dataSize = m_spritesFile->getU16();
fin->addU16(dataSize);
char spriteData[SPRITE_DATA_SIZE];
m_spritesFile->read(spriteData, dataSize);
fin->write(spriteData, dataSize);
spriteAddress = fin->tell();
}
//TODO: Check for overwritten sprites.
}
fin->flush();
fin->close();
} catch(std::exception& e) {
g_logger.error(stdext::format("Failed to save '%s': %s", fileName, e.what()));
}
}
示例3: savePNG
void Image::savePNG(const std::string& fileName)
{
FileStreamPtr fin = g_resources.createFile(fileName);
if(!fin)
stdext::throw_exception(stdext::format("failed to open file '%s' for write", fileName));
fin->cache();
std::stringstream data;
save_png(data, m_size.width(), m_size.height(), 4, (unsigned char*)getPixelData());
fin->write(data.str().c_str(), data.str().length());
fin->flush();
fin->close();
}
示例4: save
bool CSceneData::save(const std::string& file, f32 version)
{
if (m_objectList.empty())
{
LOG_ERROR("CSceneData::save: Object list empty, nothing save");
return false;
}
LOG_DEBUG("CSceneData::save: Serialize model to memory stream");
MemoryStreamPtr stream = CStreamManager::createMemoryStream();
stream->seekBeg(0);
stream->write(version);
stream->write(m_id);
stream->write(m_name);
if (!CSceneData::saveMaterial(stream))
{
LOG_ERROR("CSceneData::save: Material serialize failed");
return false;
}
if (!CSceneData::saveGeometry(stream))
{
LOG_ERROR("CSceneData::save: Geometry serialize failed");
return false;
}
LOG_DEBUG("CSceneData::save: Save memory stream to file stream [%s]. Size [%d]", file.c_str(), stream->size());
FileStreamPtr fileStream = CStreamManager::createFileStream(file, FileStream::e_create);
fileStream->write(stream->getData(), sizeof(u8), stream->size());
fileStream->close();
return true;
}
示例5: saveOtcm
void Map::saveOtcm(const std::string& fileName)
{
#if 0
try {
g_clock.update();
FileStreamPtr fin = g_resources.createFile(fileName);
fin->cache();
//TODO: compression flag with zlib
uint32 flags = 0;
// header
fin->addU32(OTCM_SIGNATURE);
fin->addU16(0); // data start, will be overwritten later
fin->addU16(OTCM_VERSION);
fin->addU32(flags);
// version 1 header
fin->addString("OTCM 1.0"); // map description
fin->addU32(g_things.getDatSignature());
fin->addU16(g_game.getClientVersion());
fin->addString(g_game.getWorldName());
// go back and rewrite where the map data starts
uint32 start = fin->tell();
fin->seek(4);
fin->addU16(start);
fin->seek(start);
for(auto& pair : m_tiles) {
const TilePtr& tile = pair.second;
if(!tile || tile->isEmpty())
continue;
Position pos = pair.first;
fin->addU16(pos.x);
fin->addU16(pos.y);
fin->addU8(pos.z);
const auto& list = tile->getThings();
auto first = std::find_if(list.begin(), list.end(), [](const ThingPtr& thing) { return thing->isItem(); });
for(auto it = first, end = list.end(); it != end; ++it) {
const ThingPtr& thing = *it;
if(thing->isItem()) {
ItemPtr item = thing->static_self_cast<Item>();
fin->addU16(item->getId());
fin->addU8(item->getCountOrSubType());
}
}
// end of tile
fin->addU16(0xFFFF);
}
// end of file
Position invalidPos;
fin->addU16(invalidPos.x);
fin->addU16(invalidPos.y);
fin->addU8(invalidPos.z);
fin->flush();
fin->close();
} catch(stdext::exception& e) {
g_logger.error(stdext::format("failed to save OTCM map: %s", e.what()));
}
#endif
}
示例6: loadOtcm
bool Map::loadOtcm(const std::string& fileName)
{
try {
FileStreamPtr fin = g_resources.openFile(fileName);
if(!fin)
stdext::throw_exception("unable to open file");
stdext::timer loadTimer;
fin->cache();
uint32 signature = fin->getU32();
if(signature != OTCM_SIGNATURE)
stdext::throw_exception("invalid otcm file");
uint16 start = fin->getU16();
uint16 version = fin->getU16();
fin->getU32(); // flags
switch(version) {
case 1: {
fin->getString(); // description
uint32 datSignature = fin->getU32();
fin->getU16(); // protocol version
fin->getString(); // world name
if(datSignature != g_things.getDatSignature())
g_logger.warning("otcm map loaded was created with a different dat signature");
break;
}
default:
stdext::throw_exception("otcm version not supported");
}
fin->seek(start);
while(true) {
Position pos;
pos.x = fin->getU16();
pos.y = fin->getU16();
pos.z = fin->getU8();
// end of file
if(!pos.isValid())
break;
const TilePtr& tile = g_map.createTile(pos);
int stackPos = 0;
while(true) {
int id = fin->getU16();
// end of tile
if(id == 0xFFFF)
break;
int countOrSubType = fin->getU8();
ItemPtr item = Item::create(id);
item->setCountOrSubType(countOrSubType);
if(item->isValid())
tile->addThing(item, stackPos++);
}
}
fin->close();
g_logger.debug(stdext::format("Otcm load time: %.2f seconds", loadTimer.elapsed_seconds()));
return true;
} catch(stdext::exception& e) {
g_logger.error(stdext::format("failed to load OTCM map: %s", e.what()));
return false;
}
}
示例7: loadOtbm
//.........这里部分代码省略.........
if(!(house = m_houses.getHouse(hId))) {
house = HousePtr(new House(hId));
m_houses.addHouse(house);
}
house->setTile(tile);
}
while(nodeTile->canRead()) {
uint8 tileAttr = nodeTile->getU8();
switch (tileAttr) {
case OTBM_ATTR_TILE_FLAGS: {
uint32 _flags = nodeTile->getU32();
if((_flags & TILESTATE_PROTECTIONZONE) == TILESTATE_PROTECTIONZONE)
flags |= TILESTATE_PROTECTIONZONE;
else if((_flags & TILESTATE_OPTIONALZONE) == TILESTATE_OPTIONALZONE)
flags |= TILESTATE_OPTIONALZONE;
else if((_flags & TILESTATE_HARDCOREZONE) == TILESTATE_HARDCOREZONE)
flags |= TILESTATE_HARDCOREZONE;
if((_flags & TILESTATE_NOLOGOUT) == TILESTATE_NOLOGOUT)
flags |= TILESTATE_NOLOGOUT;
if((_flags & TILESTATE_REFRESH) == TILESTATE_REFRESH)
flags |= TILESTATE_REFRESH;
break;
}
case OTBM_ATTR_ITEM: {
addThing(Item::createFromOtb(nodeTile->getU16()), pos);
break;
}
default: {
stdext::throw_exception(stdext::format("invalid tile attribute %d at pos %s", (int)tileAttr, stdext::to_string(pos)));
}
}
}
for(const BinaryTreePtr &nodeItem : nodeTile->getChildren()) {
if(nodeItem->getU8() != OTBM_ITEM)
stdext::throw_exception("invalid item node");
ItemPtr item = Item::createFromOtb(nodeItem->getU16());
item->unserializeItem(nodeItem);
if(item->isContainer()) {
for(const BinaryTreePtr& containerItem : nodeItem->getChildren()) {
if(containerItem->getU8() != OTBM_ITEM)
stdext::throw_exception("invalid container item node");
ItemPtr cItem = Item::createFromOtb(containerItem->getU16());
cItem->unserializeItem(containerItem);
//item->addContainerItem(cItem);
}
}
if(house && item->isMoveable()) {
g_logger.warning(stdext::format("Movable item found in house: %d at pos %s - escaping...", item->getId(), stdext::to_string(pos)));
item.reset();
}
addThing(item, pos);
}
if(const TilePtr& tile = getTile(pos))
tile->setFlags((tileflags_t)flags);
}
} else if(mapDataType == OTBM_TOWNS) {
TownPtr town = nullptr;
for(const BinaryTreePtr &nodeTown : nodeMapData->getChildren()) {
if(nodeTown->getU8() != OTBM_TOWN)
stdext::throw_exception("invalid town node.");
uint32 townId = nodeTown->getU32();
std::string townName = nodeTown->getString();
Position townCoords = nodeTown->getPosition();
if(!(town = m_towns.getTown(townId))) {
town = TownPtr(new Town(townId, townName, townCoords));
m_towns.addTown(town);
}
}
} else if(mapDataType == OTBM_WAYPOINTS && headerVersion > 1) {
for(const BinaryTreePtr &nodeWaypoint : nodeMapData->getChildren()) {
if(nodeWaypoint->getU8() != OTBM_WAYPOINT)
stdext::throw_exception("invalid waypoint node.");
std::string name = nodeWaypoint->getString();
Position waypointPos = nodeWaypoint->getPosition();
if(waypointPos.isValid() && !name.empty() && m_waypoints.find(waypointPos) == m_waypoints.end())
m_waypoints.insert(std::make_pair(waypointPos, name));
}
} else
stdext::throw_exception(stdext::format("Unknown map data node %d", (int)mapDataType));
}
g_logger.debug("OTBM read successfully.");
fin->close();
loadSpawns(getSpawnFile());
// m_houses.load(getHouseFile());
}
示例8: saveOtcm
void Map::saveOtcm(const std::string& fileName)
{
try {
stdext::timer saveTimer;
FileStreamPtr fin = g_resources.createFile(fileName);
fin->cache();
//TODO: compression flag with zlib
uint32 flags = 0;
// header
fin->addU32(OTCM_SIGNATURE);
fin->addU16(0); // data start, will be overwritten later
fin->addU16(OTCM_VERSION);
fin->addU32(flags);
// version 1 header
fin->addString("OTCM 1.0"); // map description
fin->addU32(g_things.getDatSignature());
fin->addU16(g_game.getProtocolVersion());
fin->addString(g_game.getWorldName());
// go back and rewrite where the map data starts
uint32 start = fin->tell();
fin->seek(4);
fin->addU16(start);
fin->seek(start);
for(uint8_t z = 0; z <= Otc::MAX_Z; ++z) {
for(const auto& it : m_tileBlocks[z]) {
const TileBlock& block = it.second;
for(const TilePtr& tile : block.getTiles()) {
if(!tile || tile->isEmpty())
continue;
Position pos = tile->getPosition();
fin->addU16(pos.x);
fin->addU16(pos.y);
fin->addU8(pos.z);
for(const ThingPtr& thing : tile->getThings()) {
if(thing->isItem()) {
ItemPtr item = thing->static_self_cast<Item>();
fin->addU16(item->getId());
fin->addU8(item->getCountOrSubType());
}
}
// end of tile
fin->addU16(0xFFFF);
}
}
}
// end of file
Position invalidPos;
fin->addU16(invalidPos.x);
fin->addU16(invalidPos.y);
fin->addU8(invalidPos.z);
fin->flush();
fin->close();
} catch(stdext::exception& e) {
g_logger.error(stdext::format("failed to save OTCM map: %s", e.what()));
}
}
示例9: saveOtbm
//.........这里部分代码省略.........
int px = -1, py = -1, pz =-1;
bool firstNode = true;
for(uint8_t z = 0; z <= Otc::MAX_Z; ++z) {
for(const auto& it : m_tileBlocks[z]) {
const TileBlock& block = it.second;
for(const TilePtr& tile : block.getTiles()) {
if(unlikely(!tile || tile->isEmpty()))
continue;
const Position& pos = tile->getPosition();
if(unlikely(!pos.isValid()))
continue;
if(pos.x < px || pos.x >= px + 256
|| pos.y < py || pos.y >= py + 256
|| pos.z != pz) {
if(!firstNode)
root->endNode(); /// OTBM_TILE_AREA
firstNode = false;
root->startNode(OTBM_TILE_AREA);
px = pos.x & 0xFF00;
py = pos.y & 0xFF00;
pz = pos.z;
root->addPos(px, py, pz);
}
root->startNode(tile->isHouseTile() ? OTBM_HOUSETILE : OTBM_TILE);
root->addPoint(Point(pos.x, pos.y) & 0xFF);
if(tile->isHouseTile())
root->addU32(tile->getHouseId());
if(tile->getFlags()) {
root->addU8(OTBM_ATTR_TILE_FLAGS);
root->addU32(tile->getFlags());
}
const auto& itemList = tile->getItems();
const ItemPtr& ground = tile->getGround();
if(ground) {
// Those types are called "complex" needs other stuff to be written.
// For containers, there is container items, for depot, depot it and so on.
if(!ground->isContainer() && !ground->isDepot()
&& !ground->isDoor() && !ground->isTeleport()) {
root->addU8(OTBM_ATTR_ITEM);
root->addU16(ground->getServerId());
} else
ground->serializeItem(root);
}
for(const ItemPtr& item : itemList)
if(!item->isGround())
item->serializeItem(root);
root->endNode(); // OTBM_TILE
}
}
}
if(!firstNode)
root->endNode(); // OTBM_TILE_AREA
root->startNode(OTBM_TOWNS);
for(const TownPtr& town : g_towns.getTowns()) {
root->startNode(OTBM_TOWN);
root->addU32(town->getId());
root->addString(town->getName());
Position townPos = town->getPos();
root->addPos(townPos.x, townPos.y, townPos.z);
root->endNode();
}
root->endNode();
if(version > 1) {
root->startNode(OTBM_WAYPOINTS);
for(const auto& it : m_waypoints) {
root->startNode(OTBM_WAYPOINT);
root->addString(it.second);
Position pos = it.first;
root->addPos(pos.x, pos.y, pos.z);
root->endNode();
}
root->endNode();
}
}
root->endNode(); // OTBM_MAP_DATA
}
root->endNode();
fin->flush();
fin->close();
} catch(std::exception& e) {
g_logger.error(stdext::format("Failed to save '%s': %s", fileName, e.what()));
}
}