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

master
niczkx 12 years ago
parent a01534e6a2
commit 5abf8622e5

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

@ -26,6 +26,7 @@
#include "declarations.h" #include "declarations.h"
#include <framework/util/databuffer.h> #include <framework/util/databuffer.h>
#include <otclient/position.h> #include <otclient/position.h>
#include <boost/concept_check.hpp>
enum { enum {
BINARYTREE_ESCAPE_CHAR = 0xFD, BINARYTREE_ESCAPE_CHAR = 0xFD,
@ -48,21 +49,10 @@ public:
uint16 getU16(); uint16 getU16();
uint32 getU32(); uint32 getU32();
uint64 getU64(); uint64 getU64();
std::string getString(); std::string getString(uint16 len = 0);
std::string getString(uint16 len);
Position getPosition(); Position getPosition();
Point getPoint(); 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(); BinaryTreeVec getChildren();
bool canRead() { unserialize(); return m_pos < m_buffer.size(); } bool canRead() { unserialize(); return m_pos < m_buffer.size(); }
@ -76,4 +66,25 @@ private:
uint m_startPos; 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 #endif

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

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

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

@ -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(ATTR_COUNT);
out->writeU8(getCount()); out->writeU8(getCount());
@ -287,7 +287,8 @@ void Item::serializeItem(const BinaryTreePtr& out)
out->writeU16(getDepotId()); 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) { if(aid) {
out->writeU8(ATTR_ACTION_ID); out->writeU8(ATTR_ACTION_ID);
out->writeU16(aid); out->writeU16(aid);

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

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

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

Loading…
Cancel
Save