rewrite some parts of otbm saving and uncomment it for testing later

This commit is contained in:
niczkx 2012-08-05 05:26:27 -06:00
parent a01534e6a2
commit 5abf8622e5
9 changed files with 105 additions and 149 deletions

View File

@ -151,15 +151,12 @@ uint64 BinaryTree::getU64()
return v;
}
std::string BinaryTree::getString()
{
uint16 len = getU16();
return getString(len);
}
std::string BinaryTree::getString(uint16 len)
{
unserialize();
if(len == 0)
len = getU16();
if(m_pos+len > m_buffer.size())
stdext::throw_exception("BinaryTree: getString failed: string length exceeded buffer size.");
@ -185,73 +182,31 @@ Point BinaryTree::getPoint()
return ret;
}
BinaryTreePtr BinaryTree::makeChild(uint8 type)
/// BinaryWriteTree
BinaryWriteTree::BinaryWriteTree(const FileStreamPtr& fin) :
m_fin(fin)
{
BinaryTreePtr child(new BinaryTree(m_fin));
child->setType(type);
//children.append(child);
return child;
}
void BinaryTree::setType(uint8 type)
BinaryWriteTree::~BinaryWriteTree()
{
writeU8(0xFE);
m_fin->close();
m_fin = nullptr;
}
void BinaryWriteTree::startNode(uint8 type)
{
writeU8(BINARYTREE_NODE_START);
writeU8(type);
}
void BinaryTree::writeU8(uint8 u8)
void BinaryWriteTree::endNode()
{
m_buffer.add(u8);
writeU8(BINARYTREE_NODE_END);
}
void BinaryTree::writeU16(uint16 u16)
{
stdext::writeLE16(m_buffer.data(), u16);
}
void BinaryWriteTree::writeU8(uint8 u8) { m_fin->addU8(u8); }
void BinaryWriteTree::writeU16(uint16 u16) { m_fin->addU16(u16); }
void BinaryWriteTree::writeU32(uint32 u32) { m_fin->addU32(u32); }
void BinaryWriteTree::writeString(const std::string& str) { m_fin->addString(str); }
void BinaryTree::writeU32(uint32 u32)
{
stdext::writeLE32(m_buffer.data(), u32);
}
void BinaryTree::writeString(const std::string& s)
{
size_t len = s.length();
writeU16(len);
m_buffer.grow(m_pos + len);
memcpy(&m_buffer[m_pos], s.c_str(), len);
m_pos += len;
}
void BinaryTree::writePos(const Position& p)
{
if(!p.isValid())
stdext::throw_exception("invalid position passed to BinaryTree::writePos");
writeU16(p.x);
writeU16(p.y);
writeU8(p.z);
}
void BinaryTree::writePoint(const Point& p)
{
if(p.isNull())
stdext::throw_exception("invalid point passed to BinaryTree::writePoint");
writeU8(p.x);
writeU8(p.y);
}
void BinaryTree::writeToFile()
{
if(!m_fin)
stdext::throw_exception("attempt to write binary node to closed file");
/// first write self data
m_fin->write(&m_buffer[0], m_buffer.size());
#if 0
/// write children data
for(const BinaryTreePtr& child : m_children)
child->writeToFile();
#endif
}

View File

@ -26,6 +26,7 @@
#include "declarations.h"
#include <framework/util/databuffer.h>
#include <otclient/position.h>
#include <boost/concept_check.hpp>
enum {
BINARYTREE_ESCAPE_CHAR = 0xFD,
@ -48,21 +49,10 @@ public:
uint16 getU16();
uint32 getU32();
uint64 getU64();
std::string getString();
std::string getString(uint16 len);
std::string getString(uint16 len = 0);
Position getPosition();
Point getPoint();
void setType(uint8 type);
void writeU8(uint8 u8);
void writeU16(uint16 u16);
void writeU32(uint32 u32);
void writeString(const std::string& s);
void writePos(const Position& p);
void writePoint(const Point& p);
BinaryTreePtr makeChild(uint8 type);
void writeToFile();
BinaryTreeVec getChildren();
bool canRead() { unserialize(); return m_pos < m_buffer.size(); }
@ -76,4 +66,25 @@ private:
uint m_startPos;
};
class BinaryWriteTree : public stdext::shared_object
{
public:
BinaryWriteTree(const FileStreamPtr& fin);
~BinaryWriteTree();
void startNode(uint8 type);
void endNode();
void writeU8(uint8 u8);
void writeU16(uint16 u16);
void writeU32(uint32 u32);
void writeString(const std::string& str);
void writePoint(const Point& p) { writeU8(p.x); writeU8(p.y); }
void writePos(const Position& p) { writeU16(p.x); writeU16(p.y); writeU8(p.z); }
private:
FileStreamPtr m_fin;
};
#endif

View File

@ -32,12 +32,14 @@ class Event;
class ScheduledEvent;
class FileStream;
class BinaryTree;
class BinaryWriteTree;
typedef stdext::shared_object_ptr<Module> ModulePtr;
typedef stdext::shared_object_ptr<Event> EventPtr;
typedef stdext::shared_object_ptr<ScheduledEvent> ScheduledEventPtr;
typedef stdext::shared_object_ptr<FileStream> FileStreamPtr;
typedef stdext::shared_object_ptr<BinaryTree> BinaryTreePtr;
typedef stdext::shared_object_ptr<BinaryWriteTree> BinaryWriteTreePtr;
typedef std::vector<BinaryTreePtr> BinaryTreeVec;

View File

@ -267,13 +267,6 @@ BinaryTreePtr FileStream::getBinaryTree()
return BinaryTreePtr(new BinaryTree(asFileStream()));
}
BinaryTreePtr FileStream::makeTree()
{
BinaryTreePtr root(new BinaryTree(asFileStream()));
root->setType(0);
return root;
}
void FileStream::addU8(uint8 v)
{
if(!m_caching) {

View File

@ -60,7 +60,6 @@ public:
void addU32(uint32 v);
void addU64(uint64 v);
void addString(const std::string& v);
BinaryTreePtr makeTree();
FileStreamPtr asFileStream() { return static_self_cast<FileStream>(); }

View File

@ -268,7 +268,7 @@ void Item::unserializeItem(const BinaryTreePtr &in)
}
}
void Item::serializeItem(const BinaryTreePtr& out)
void Item::serializeItem(const BinaryWriteTreePtr& out)
{
out->writeU8(ATTR_COUNT);
out->writeU8(getCount());
@ -287,7 +287,8 @@ void Item::serializeItem(const BinaryTreePtr& out)
out->writeU16(getDepotId());
}
uint16 aid = m_attribs.get<uint16>(ATTR_ACTION_ID), uid = m_attribs.get<uint16>(ATTR_UNIQUE_ID);
uint16 aid = m_attribs.get<uint16>(ATTR_ACTION_ID);
uint16 uid = m_attribs.get<uint16>(ATTR_UNIQUE_ID);
if(aid) {
out->writeU8(ATTR_ACTION_ID);
out->writeU16(aid);

View File

@ -98,7 +98,7 @@ public:
ItemPtr clone();
void unserializeItem(const BinaryTreePtr& in);
void serializeItem(const BinaryTreePtr& out);
void serializeItem(const BinaryWriteTreePtr& out);
void setDepotId(uint16 depotId) { m_attribs.set(ATTR_DEPOT_ID, depotId); }
uint16 getDepotId() { return m_attribs.get<uint16>(ATTR_DEPOT_ID); }

View File

@ -163,6 +163,8 @@ public:
uint getTileIndex(const Position& pos) { return ((pos.y % BLOCK_SIZE) * BLOCK_SIZE) + (pos.x % BLOCK_SIZE); }
const std::array<TilePtr, BLOCK_SIZE*BLOCK_SIZE> getTiles() const { return m_tiles; }
private:
std::array<TilePtr, BLOCK_SIZE*BLOCK_SIZE> m_tiles;
};

View File

@ -23,6 +23,8 @@
#include "map.h"
#include "tile.h"
#include <framework/core/application.h>
#include <framework/core/eventdispatcher.h>
#include <framework/core/resourcemanager.h>
#include <framework/core/filestream.h>
@ -215,8 +217,6 @@ void Map::loadOtbm(const std::string& fileName)
void Map::saveOtbm(const std::string &fileName)
{
#if 0
/// FIXME: Untested code
FileStreamPtr fin = g_resources.appendFile(fileName);
if(!fin)
stdext::throw_exception(stdext::format("failed to open file '%s' for write", fileName));
@ -237,7 +237,8 @@ void Map::saveOtbm(const std::string &fileName)
/// Usually when a map has empty house/spawn file it means the map is new.
/// TODO: Ask the user for a map name instead of those ugly uses of substr
std::string houseFile = getHouseFile(), spawnFile = getSpawnFile();
std::string houseFile = getHouseFile();
std::string spawnFile = getSpawnFile();
if(houseFile.empty() && version > 1)
houseFile = fileName.substr(fileName.find_last_of('/')) + "-houses.xml";
if(spawnFile.empty())
@ -250,9 +251,8 @@ void Map::saveOtbm(const std::string &fileName)
saveSpawns(dir + "/" + spawnFile);
#endif
time_t start = time(0);
fin->addU32(0); // file version
BinaryTreePtr root = fin->makeTree();
BinaryWriteTreePtr root(new BinaryWriteTree(fin));
{
root->writeU32(version);
@ -263,93 +263,88 @@ void Map::saveOtbm(const std::string &fileName)
root->writeU32(g_things.getOtbMajorVersion());
root->writeU32(g_things.getOtbMinorVersion());
BinaryTreePtr mapData = root->makeChild(OTBM_MAP_DATA);
root->startNode(OTBM_MAP_DATA);
{
// own description.
for(const auto& desc : getDescriptions()) {
mapData->writeU8(OTBM_ATTR_DESCRIPTION);
mapData->writeString(desc);
root->writeU8(OTBM_ATTR_DESCRIPTION);
root->writeString(desc);
}
// special one
mapData->writeU8(OTBM_ATTR_DESCRIPTION);
mapData->writeString(stdext::format("Saved with %s v%d", g_app.getName(), stdext::unsafe_cast<int>(g_app.getVersion())));
root->writeU8(OTBM_ATTR_DESCRIPTION);
root->writeString(stdext::format("Saved with %s v%d", g_app.getName(), stdext::unsafe_cast<int>(g_app.getVersion())));
// spawn file.
mapData->writeU8(OTBM_ATTR_SPAWN_FILE);
mapData->writeString(spawnFile);
root->writeU8(OTBM_ATTR_SPAWN_FILE);
root->writeString(spawnFile);
// house file.
if(version > 1) {
mapData->writeU8(OTBM_ATTR_HOUSE_FILE);
mapData->writeString(houseFile);
root->writeU8(OTBM_ATTR_HOUSE_FILE);
root->writeString(houseFile);
}
/// write tiles first
BinaryTreePtr tileArea = mapData->makeChild(OTBM_TILE_AREA);
root->startNode(OTBM_TILE_AREA);
Position base(-1, -1, -1);
for(const auto& pair : m_tiles) {
Position pos = pair.first;
TilePtr tile = pair.second;
/// base position
if(pos.x < base.x || pos.x >= base.x + 256 || pos.y < base.y|| pos.y >= base.y + 256 ||
pos.z != base.z) {
tileArea->writePos(base = pos & 0xFF00);
}
for(uint8_t z = 0; z < Otc::MAX_Z + 1; ++z) {
for(const auto& it : m_tileBlocks[z]) {
const TileBlock& block = it.second;
for(const TilePtr& tile : block.getTiles()) {
const Position& pos = tile->getPosition();
if(!pos.isValid())
continue;
BinaryTreePtr tileNode(nullptr);
uint32 flags = tile->getFlags();
if((flags & TILESTATE_HOUSE) == TILESTATE_HOUSE)
tileNode = tileArea->makeChild(OTBM_HOUSETILE);
else
tileNode = tileArea->makeChild(OTBM_TILE);
/// base position
if(pos.x < base.x || pos.x >= base.x + 256 || pos.y < base.y|| pos.y >= base.y + 256 ||
pos.z != base.z)
root->writePos(base = pos & 0xFF00);
tileNode->writePoint(Point(pos.x, pos.y));
// if(tileNode->getType() == OTBM_HOUSETILE)
// tileNode->writeU32(tile->getHouseId());
uint32 flags = tile->getFlags();
if((flags & TILESTATE_HOUSE) == TILESTATE_HOUSE)
root->startNode(OTBM_HOUSETILE);
else
root->startNode(OTBM_TILE);
/// Tile flags
if(flags != 0) {
tileNode->writeU8(OTBM_ATTR_TILE_FLAGS);
tileNode->writeU32(flags);
}
root->writePoint(Point(pos.x, pos.y));
// if(tileNode->getType() == OTBM_HOUSETILE)
// tileNode->writeU32(tile->getHouseId());
/// start writing tile items
for(const ItemPtr& item : tile->getItems()) {
BinaryTreePtr itemNode = tileNode->makeChild(OTBM_ATTR_ITEM);
item->serializeItem(itemNode);
/// Tile flags
if(flags) {
root->writeU8(OTBM_ATTR_TILE_FLAGS);
root->writeU32(flags);
}
/// start writing tile items
for(const ItemPtr& item : tile->getItems()) {
root->startNode(OTBM_ATTR_ITEM);
item->serializeItem(root);
}
}
}
}
/// write towns
BinaryTreePtr townNode = mapData->makeChild(OTBM_TOWNS);
root->startNode(OTBM_TOWNS);
for(const TownPtr& town : m_towns.getTowns()) {
BinaryTreePtr newTown = townNode->makeChild(OTBM_TOWN);
{
newTown->writeU32(town->getId());
newTown->writeString(town->getName());
newTown->writePos(town->getPos());
}
root->writeU32(town->getId());
root->writeString(town->getName());
root->writePos(town->getPos());
}
/// write waypoints
if(version > 1) {
BinaryTreePtr waypointNode = mapData->makeChild(OTBM_WAYPOINTS);
root->startNode(OTBM_WAYPOINTS);
for(const auto& it : m_waypoints) {
BinaryTreePtr newWaypoint = waypointNode->makeChild(OTBM_WAYPOINT);
{
newWaypoint->writeString(it.second);
newWaypoint->writePos(it.first);
}
root->writeString(it.second);
root->writePos(it.first);
}
}
}
}
root->writeToFile();
g_logger.debug(stdext::format("OTBM saving took %ld", time(0) - start));
#endif
}
void Map::loadSpawns(const std::string &fileName)
@ -391,8 +386,6 @@ void Map::loadSpawns(const std::string &fileName)
addThing(creature, centerPos + cNode->readPoint());
}
}
doc.Clear();
}
bool Map::loadOtcm(const std::string& fileName)
@ -539,4 +532,4 @@ void Map::saveOtcm(const std::string& fileName)
g_logger.error(stdext::format("failed to save OTCM map: %s", e.what()));
}
#endif
}
}