2015-08-12 20:21:10 +00:00
|
|
|
#include "DefinitionNode.h"
|
2013-10-30 11:46:18 +00:00
|
|
|
|
2015-08-13 21:46:50 +00:00
|
|
|
#include "Logs.h"
|
2013-11-03 12:00:31 +00:00
|
|
|
#include "PackStream.h"
|
2015-08-17 20:55:30 +00:00
|
|
|
#include "DefinitionWatcher.h"
|
2015-08-16 21:01:56 +00:00
|
|
|
#include "DefinitionDiff.h"
|
2015-08-16 22:29:54 +00:00
|
|
|
#include "DiffManager.h"
|
|
|
|
|
|
|
|
#include <cassert>
|
2013-11-03 12:00:31 +00:00
|
|
|
|
2015-08-16 21:01:56 +00:00
|
|
|
DefinitionNode::DefinitionNode(DefinitionNode* parent, const std::string &name, const std::string &type_name):
|
|
|
|
parent(parent), type_name(type_name), name(name)
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
2013-10-30 14:39:56 +00:00
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
root = parent->root;
|
2015-08-12 15:02:27 +00:00
|
|
|
parent->addChild(this);
|
2015-08-16 22:29:54 +00:00
|
|
|
diffs = NULL;
|
2013-10-30 14:39:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
root = this;
|
2015-08-16 22:29:54 +00:00
|
|
|
diffs = new DiffManager(this);
|
2013-10-30 14:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
DefinitionNode::~DefinitionNode()
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2015-08-12 15:02:27 +00:00
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
parent->removeChild(this);
|
|
|
|
parent = NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-16 22:29:54 +00:00
|
|
|
if (diffs)
|
|
|
|
{
|
|
|
|
delete diffs;
|
|
|
|
diffs = NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-12 17:29:28 +00:00
|
|
|
// Work on a copy, because the child destructor will modify the array by removing itself using removeChild
|
2015-08-12 20:21:10 +00:00
|
|
|
std::vector<DefinitionNode*> children_copy = children;
|
2015-08-12 17:29:28 +00:00
|
|
|
for (auto child:children_copy)
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2015-08-12 15:02:27 +00:00
|
|
|
if (child->getParent() == this)
|
|
|
|
{
|
|
|
|
delete child;
|
|
|
|
}
|
2013-10-30 14:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
void DefinitionNode::setName(const std::string &name)
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2013-10-31 16:59:18 +00:00
|
|
|
this->name = name;
|
2013-10-30 14:39:56 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 20:15:19 +00:00
|
|
|
const Scenery* DefinitionNode::getScenery() const
|
2013-11-17 21:36:18 +00:00
|
|
|
{
|
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
return parent->getScenery();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
std::string DefinitionNode::toString(int indent) const
|
2015-08-12 15:02:27 +00:00
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
for (int i = 0; i < indent; i++)
|
|
|
|
{
|
|
|
|
result += " ";
|
|
|
|
}
|
|
|
|
result += name;
|
|
|
|
if (not children.empty())
|
|
|
|
{
|
|
|
|
for (auto &child: children)
|
|
|
|
{
|
|
|
|
result += "\n" + child->toString(indent + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-16 22:29:54 +00:00
|
|
|
std::string DefinitionNode::getPath() const
|
|
|
|
{
|
|
|
|
if (parent == root)
|
|
|
|
{
|
|
|
|
return parent->getPath() + name;
|
|
|
|
}
|
|
|
|
else if (parent)
|
|
|
|
{
|
|
|
|
return parent->getPath() + "/" + name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "/";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DefinitionNode *DefinitionNode::findByPath(const std::string &path) const
|
|
|
|
{
|
|
|
|
if (path.empty())
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (path[0] == '/')
|
|
|
|
{
|
|
|
|
if (path.length() == 1)
|
|
|
|
{
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
else if (root == this)
|
|
|
|
{
|
|
|
|
return findByPath(path.substr(1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return root->findByPath(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t seppos = path.find("/");
|
|
|
|
std::string child_name = (seppos == std::string::npos) ? path : path.substr(0, seppos);
|
|
|
|
DefinitionNode *child = ((DefinitionNode *)this)->findChildByName(child_name); // FIXME findChildByName should be const
|
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
if (seppos == std::string::npos)
|
|
|
|
{
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return child->findByPath(path.substr(seppos + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 21:01:56 +00:00
|
|
|
bool DefinitionNode::applyDiff(const DefinitionDiff *diff, bool)
|
|
|
|
{
|
|
|
|
// Only do type check, subclasses will do the rest
|
|
|
|
if (diff->getTypeName() == type_name)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logs::error() << "Can't apply " << diff->getTypeName() << " diff to " << getName() << " " << type_name << " node" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:55:30 +00:00
|
|
|
void DefinitionNode::generateInitDiffs(std::vector<const DefinitionDiff *> *) const
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefinitionNode::addWatcher(DefinitionWatcher *watcher, bool init_diff)
|
2015-08-16 22:29:54 +00:00
|
|
|
{
|
|
|
|
if (root && root->diffs)
|
|
|
|
{
|
2015-08-17 20:55:30 +00:00
|
|
|
if (init_diff)
|
|
|
|
{
|
|
|
|
std::vector<const DefinitionDiff *> diffs;
|
|
|
|
generateInitDiffs(&diffs);
|
|
|
|
|
|
|
|
for (auto diff: diffs)
|
|
|
|
{
|
|
|
|
watcher->nodeChanged(this, diff);
|
|
|
|
delete diff;
|
|
|
|
}
|
|
|
|
}
|
2015-08-16 22:29:54 +00:00
|
|
|
root->diffs->addWatcher(this, watcher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 21:10:43 +00:00
|
|
|
int DefinitionNode::getWatcherCount() const
|
|
|
|
{
|
|
|
|
if (root && root->diffs)
|
|
|
|
{
|
|
|
|
return root->diffs->getWatcherCount(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
void DefinitionNode::save(PackStream* stream) const
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
int children_count = (int)children.size();
|
|
|
|
stream->write(&children_count);
|
|
|
|
|
2013-12-10 21:32:58 +00:00
|
|
|
for (auto child: children)
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
stream->write(child->name);
|
|
|
|
|
|
|
|
int child_size = child->getStreamSize();
|
|
|
|
if (child_size >= 0)
|
|
|
|
{
|
|
|
|
stream->write(&child_size);
|
|
|
|
child->save(stream);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Child size not known, write it to a temporary stream to know it
|
|
|
|
Logs::debug() << "Unknown size for child " << child->name << ", unefficient writing to temporary stream" << std::endl;
|
|
|
|
PackStream substream;
|
|
|
|
child->save(&substream);
|
|
|
|
stream->writeFromBuffer(substream, true);
|
|
|
|
}
|
2013-10-30 14:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
void DefinitionNode::load(PackStream* stream)
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
int children_count;
|
|
|
|
|
|
|
|
stream->read(&children_count);
|
|
|
|
|
|
|
|
for (int i = 0; i < children_count; i++)
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
std::string child_name = stream->readString();
|
|
|
|
|
|
|
|
int child_size;
|
|
|
|
stream->read(&child_size);
|
|
|
|
|
|
|
|
DefinitionNode *child = findChildByName(child_name);
|
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
// TODO type check
|
|
|
|
child->load(stream);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO Ask subclass if it can instanciate a child
|
|
|
|
// Else skip length of unknown child
|
|
|
|
stream->skipBytes(child_size);
|
|
|
|
Logs::warning() << "Skipped unknown child '" << child_name << "'" << std::endl;
|
|
|
|
}
|
2013-10-30 14:39:56 +00:00
|
|
|
}
|
2013-10-30 11:46:18 +00:00
|
|
|
}
|
2013-10-31 16:59:18 +00:00
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
void DefinitionNode::copy(DefinitionNode* destination) const
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2015-08-16 21:01:56 +00:00
|
|
|
if (destination->getTypeName() == getTypeName())
|
|
|
|
{
|
|
|
|
destination->setName(name);
|
2015-08-23 23:23:05 +00:00
|
|
|
for (auto &child: children)
|
|
|
|
{
|
|
|
|
DefinitionNode *dest_child = destination->findChildByName(child->name);
|
|
|
|
if (dest_child)
|
|
|
|
{
|
|
|
|
child->copy(dest_child);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logs::warning() << "Can't copy to child " << child->name << " of " << destination->getTypeName() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2015-08-16 21:01:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logs::error() << "Can't copy from " << getTypeName() << " to " << destination->getTypeName() << std::endl;
|
|
|
|
}
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
void DefinitionNode::validate()
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
for (auto child: children)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
child->validate();
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
void DefinitionNode::addChild(DefinitionNode* child)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-15 14:33:54 +00:00
|
|
|
if (std::find(children.begin(), children.end(), child) == children.end())
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
children.push_back(child);
|
2013-10-31 16:59:18 +00:00
|
|
|
child->parent = this;
|
|
|
|
child->root = this->root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
void DefinitionNode::removeChild(DefinitionNode* child)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2015-08-12 20:21:10 +00:00
|
|
|
std::vector<DefinitionNode*>::iterator it = std::find(children.begin(), children.end(), child);
|
2013-12-10 21:32:58 +00:00
|
|
|
if (it != children.end())
|
|
|
|
{
|
2015-08-12 15:02:27 +00:00
|
|
|
child->parent = NULL;
|
2013-12-10 21:32:58 +00:00
|
|
|
children.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
Logs::warning() << "Trying to remove not found child '" << child->name << "' from '" << name << "'" << std::endl;
|
2013-12-10 21:32:58 +00:00
|
|
|
}
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
2015-08-13 21:46:50 +00:00
|
|
|
|
|
|
|
DefinitionNode *DefinitionNode::findChildByName(const std::string name)
|
|
|
|
{
|
|
|
|
for (auto child: children)
|
|
|
|
{
|
|
|
|
if (child->name == name)
|
|
|
|
{
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DefinitionNode::getStreamSize() const
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2015-08-16 22:29:54 +00:00
|
|
|
|
|
|
|
void DefinitionNode::addDiff(const DefinitionDiff *diff)
|
|
|
|
{
|
|
|
|
assert(diff->getTypeName() == type_name);
|
|
|
|
|
|
|
|
if (root && root->diffs)
|
|
|
|
{
|
|
|
|
root->diffs->addDiff(this, diff);
|
|
|
|
}
|
|
|
|
}
|