mirror of
https://github.com/worldforge/cyphesis
synced 2026-08-13 12:26:04 -04:00
This uses the same importing code as Ember, and allows for import of both entities, minds and types. The code in EntityImporterBase.* should be kept synchronized with the Ember version. This is provided as a new command, instead of added to the existing cycmd executable. This is by design, to make it easier for users. Export and import of entities are one of the most basic functions, especially for any new user, and should therefore be provided as easy as possible.
142 lines
3.7 KiB
C++
142 lines
3.7 KiB
C++
//
|
|
// Copyright (C) 2009 Alistair Riddoch
|
|
// Copyright (C) 2013 Erik Ogenvik
|
|
//
|
|
// This program is free software; you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program; if not, write to the Free Software Foundation,
|
|
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
#include "EntityImporter.h"
|
|
|
|
#include "common/serialno.h"
|
|
#include "common/log.h"
|
|
#include "common/compose.hpp"
|
|
|
|
#include <Atlas/Objects/Operation.h>
|
|
#include <Atlas/Objects/Entity.h>
|
|
#include <Atlas/Objects/Decoder.h>
|
|
#include <Atlas/Codecs/XML.h>
|
|
|
|
#include <fstream>
|
|
|
|
using Atlas::Objects::Root;
|
|
using Atlas::Objects::smart_dynamic_cast;
|
|
using Atlas::Objects::Entity::Anonymous;
|
|
using Atlas::Objects::Entity::RootEntity;
|
|
using Atlas::Objects::Operation::Create;
|
|
using Atlas::Objects::Operation::Get;
|
|
using Atlas::Objects::Operation::Look;
|
|
using Atlas::Objects::Operation::Set;
|
|
using Atlas::Message::Element;
|
|
|
|
class ObjectDecoder : public Atlas::Objects::ObjectsDecoder {
|
|
private:
|
|
virtual void objectArrived(const Atlas::Objects::Root & obj) {
|
|
m_check = true;
|
|
m_obj = obj;
|
|
}
|
|
|
|
bool m_check;
|
|
Atlas::Objects::Root m_obj;
|
|
public:
|
|
ObjectDecoder() : m_check (false) {
|
|
}
|
|
|
|
bool check() const {
|
|
return m_check;
|
|
}
|
|
|
|
const Atlas::Objects::Root & get() {
|
|
m_check = false;
|
|
return m_obj;
|
|
}
|
|
};
|
|
|
|
EntityImporter::EntityImporter(const std::string & accountId,
|
|
const std::string & avatarId) :
|
|
EntityImporterBase(accountId, avatarId)
|
|
{
|
|
EventCompleted.connect(sigc::mem_fun(*this, &EntityImporter::completed));
|
|
}
|
|
|
|
EntityImporter::~EntityImporter()
|
|
{
|
|
}
|
|
|
|
void EntityImporter::setup(const std::string & arg, OpVector & ret)
|
|
{
|
|
mCurrentRes = &ret;
|
|
start(arg);
|
|
|
|
mCurrentRes = nullptr;
|
|
}
|
|
|
|
void EntityImporter::operation(const Operation & op, OpVector & res)
|
|
{
|
|
mCurrentRes = &res;
|
|
|
|
if (!op->isDefaultRefno()) {
|
|
auto I = mCallbacks.find(op->getRefno());
|
|
if (I != mCallbacks.end()) {
|
|
auto callback = I->second;
|
|
mCallbacks.erase(I);
|
|
callback(op);
|
|
}
|
|
} else {
|
|
if (op->getClassNo() == Atlas::Objects::Operation::ERROR_NO) {
|
|
std::string message =
|
|
op->getArgs().front()->getAttr("message").asString();
|
|
log(ERROR, String::compose("Got error. Message: %1", message));
|
|
}
|
|
}
|
|
|
|
mCurrentRes = nullptr;
|
|
}
|
|
|
|
void EntityImporter::completed()
|
|
{
|
|
m_complete = true;
|
|
}
|
|
|
|
long int EntityImporter::newSerialNumber()
|
|
{
|
|
return newSerialNo();
|
|
}
|
|
|
|
void EntityImporter::send(const Atlas::Objects::Operation::RootOperation& op)
|
|
{
|
|
if (mCurrentRes) {
|
|
mCurrentRes->push_back(op);
|
|
}
|
|
}
|
|
|
|
void EntityImporter::sendAndAwaitResponse(
|
|
const Atlas::Objects::Operation::RootOperation& op,
|
|
CallbackFunction& callback)
|
|
{
|
|
if (mCurrentRes) {
|
|
mCallbacks.insert(std::make_pair(op->getSerialno(), callback));
|
|
mCurrentRes->push_back(op);
|
|
}
|
|
}
|
|
|
|
Atlas::Objects::Root EntityImporter::loadFromFile(const std::string& filename)
|
|
{
|
|
std::fstream fileStream(filename, std::ios::in);
|
|
ObjectDecoder atlasLoader;
|
|
|
|
Atlas::Codecs::XML codec(fileStream, atlasLoader);
|
|
codec.poll(true);
|
|
|
|
return atlasLoader.get();
|
|
}
|