1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
#include "Import.h"
#include <map>
#include <limits>
#include <algorithm>
#include "i18n.h"
#include "imap.h"
#include "icomparablenode.h"
#include "imapformat.h"
#include "inamespace.h"
#include "iselectiongroup.h"
#include "ientity.h"
#include "iscenegraph.h"
#include "scene/BasicRootNode.h"
#include "scene/ChildPrimitives.h"
#include "map/Map.h"
#include "scenelib.h"
#include "entitylib.h"
#include "command/ExecutionFailure.h"
#include "string/join.h"
namespace map
{
namespace algorithm
{
// Will rewrite the group memberships of visited nodes to not be
// in conflict with any of the groups present in the target scene
class SelectionGroupRemapper :
public scene::NodeVisitor
{
private:
// The group manager of the target scene
selection::ISelectionGroupManager& _targetGroupManager;
// Maps old group IDs to new selection groups
std::map<std::size_t, selection::ISelectionGroupPtr> _groupMap;
std::size_t _nextNewGroupId;
public:
SelectionGroupRemapper(selection::ISelectionGroupManager& targetGroupManager) :
_targetGroupManager(targetGroupManager),
_nextNewGroupId(0)
{}
bool pre(const scene::INodePtr& node) override
{
// Check if this node is a group selectable in the first place
auto groupSelectable = std::dynamic_pointer_cast<IGroupSelectable>(node);
if (groupSelectable)
{
assert(node->getRootNode());
auto& sourceGroupManager = node->getRootNode()->getSelectionGroupManager();
// Copy the current set of group IDs locally
IGroupSelectable::GroupIds oldGroupIds = groupSelectable->getGroupIds();
// Remove the node from all its groups in the source space before continuing
for (auto id : oldGroupIds)
{
auto group = sourceGroupManager.getSelectionGroup(id);
group->removeNode(node);
}
// Assign the new set of groups for this node
for (auto id : oldGroupIds)
{
// Only do a remap if the group ID is in use in the target space
auto group = _targetGroupManager.getSelectionGroup(id) ?
getMappedGroup(id, sourceGroupManager) : sourceGroupManager.getSelectionGroup(id);
group->addNode(node);
}
#ifndef NDEBUG
rMessage() << "Node " << node->name() << " had the groups " << string::join(oldGroupIds, "|");
rMessage() << " remapped to " << string::join(groupSelectable->getGroupIds(), "|") << std::endl;
#endif
}
return true;
}
private:
const selection::ISelectionGroupPtr& getMappedGroup(std::size_t idToRemap, selection::ISelectionGroupManager& sourceGroupManager)
{
auto found = _groupMap.emplace(idToRemap, selection::ISelectionGroupPtr());
if (!found.second)
{
// We already covered this ID, return the mapped group
return found.first->second;
}
// Insertion was successful, so we didn't cover this ID yet
// Find a new ID that is not in known to the target manager
auto newGroupId = generateNonConflictingGroupId();
// Create this group in the source space
found.first->second = sourceGroupManager.findOrCreateSelectionGroup(newGroupId);
return found.first->second;
}
std::size_t generateNonConflictingGroupId()
{
while (++_nextNewGroupId < std::numeric_limits<std::size_t>::max())
{
if (!_targetGroupManager.getSelectionGroup(_nextNewGroupId))
{
return _nextNewGroupId;
}
}
throw std::runtime_error("Out of group IDs.");
}
};
class PrimitiveMerger :
public scene::PrimitiveReparentor
{
public:
PrimitiveMerger(const scene::INodePtr& newParent) :
PrimitiveReparentor(newParent)
{}
void post(const scene::INodePtr& node) override
{
// Base class is doing the reparenting
PrimitiveReparentor::post(node);
// After reparenting, highlight the imported node
Node_setSelected(node, true);
}
};
class EntityMerger :
public scene::NodeVisitor
{
private:
// The target path
mutable scene::Path _path;
public:
EntityMerger(const scene::INodePtr& root) :
_path(scene::Path(root))
{}
bool pre(const scene::INodePtr& originalNode) override
{
// The removeChildNode below might destroy the instance - push the refcount
scene::INodePtr node = originalNode;
// greebo: Check if the visited node is the worldspawn of the other map
if (Node_isWorldspawn(node))
{
// Find the worldspawn of the target map
const scene::INodePtr& worldSpawn = GlobalMap().getWorldspawn();
if (!worldSpawn)
{
// Set the worldspawn to the new node
GlobalMap().setWorldspawn(node);
// greebo: Un-register the node from its previous parent first to be clean
scene::INodePtr oldParent = node->getParent();
if (oldParent)
{
oldParent->removeChildNode(node);
}
// Insert the visited node at the target path
_path.top()->addChildNode(node);
_path.push(node);
// Select all the children of the visited node (these are primitives)
node->foreachNode([](const scene::INodePtr& child)->bool
{
Node_setSelected(child, true);
return true;
});
}
else
{
// The target map already has a worldspawn
_path.push(worldSpawn);
// Move all children of this node to the target worldspawn
PrimitiveMerger visitor(worldSpawn);
node->traverseChildren(visitor);
}
}
else
{
// This is an ordinary entity, not worldspawn
// greebo: Un-register the entity from its previous root node first to be clean
scene::INodePtr oldParent = node->getParent();
if (oldParent)
{
oldParent->removeChildNode(node);
}
// Insert this node at the target path
_path.top()->addChildNode(node);
_path.push(node);
// Select the visited node
Node_setSelected(node, true);
}
// Only traverse top-level entities, don't traverse the children
return false;
}
void post(const scene::INodePtr& node) override
{
_path.pop();
}
};
void importMap(const scene::INodePtr& node)
{
if (!GlobalSceneGraph().root())
{
rError() << "Cannot merge map, no scenegraph root present." << std::endl;
return;
}
// Discard all layer information found in the data to be merged
// We move everything into the active layer
{
scene::LayerList layers;
layers.insert(GlobalSceneGraph().root()->getLayerManager().getActiveLayer());
scene::AssignNodeToLayersWalker walker(layers);
node->traverse(walker);
}
// Rewrite the incoming group IDs to not be in conflict with the target scene
SelectionGroupRemapper remapper(GlobalSceneGraph().root()->getSelectionGroupManager());
node->traverseChildren(remapper);
// Now move everything into the target map
EntityMerger merger(GlobalSceneGraph().root());
node->traverseChildren(merger);
}
void prepareNamesForImport(const scene::IMapRootNodePtr& targetRoot, const scene::INodePtr& foreignRoot)
{
const auto& nspace = targetRoot->getNamespace();
if (nspace)
{
// Prepare all names, but do not import them into the namespace. This
// will happen when nodes are added to the target root later by the caller.
nspace->ensureNoConflicts(foreignRoot);
}
}
MapFormatPtr determineMapFormat(std::istream& stream, const std::string& type)
{
// Get all registered map formats matching the extension
auto availableFormats = type.empty() ?
GlobalMapFormatManager().getAllMapFormats() :
GlobalMapFormatManager().getMapFormatList(type);
MapFormatPtr format;
for (const auto& candidate : availableFormats)
{
// Rewind the stream before passing it to the format for testing
// Map format valid, rewind the stream
stream.seekg(0, std::ios_base::beg);
if (candidate->canLoad(stream))
{
format = candidate;
break;
}
}
// Rewind the stream when we're done
stream.seekg(0, std::ios_base::beg);
return format;
}
MapFormatPtr determineMapFormat(std::istream& stream)
{
return determineMapFormat(stream, std::string());
}
class SimpleMapImportFilter :
public IMapImportFilter
{
private:
scene::IMapRootNodePtr _root;
public:
SimpleMapImportFilter() :
_root(std::make_shared<scene::BasicRootNode>())
{}
const scene::IMapRootNodePtr& getRootNode() const
{
return _root;
}
bool addEntity(const scene::INodePtr& entityNode)
{
_root->addChildNode(entityNode);
return true;
}
bool addPrimitiveToEntity(const scene::INodePtr& primitive, const scene::INodePtr& entity)
{
if (Node_getEntity(entity)->isContainer())
{
entity->addChildNode(primitive);
return true;
}
return false;
}
};
void importFromStream(std::istream& stream)
{
GlobalSelectionSystem().setSelectedAll(false);
// Instantiate the default import filter
SimpleMapImportFilter importFilter;
try
{
auto format = determineMapFormat(stream);
if (!format)
{
throw IMapReader::FailureException(_("Unknown map format"));
}
auto reader = format->getMapReader(importFilter);
// Start parsing
reader->readFromStream(stream);
// Prepare child primitives
scene::addOriginToChildPrimitives(importFilter.getRootNode());
// Adjust all new names to fit into the existing map namespace
prepareNamesForImport(GlobalMap().getRoot(), importFilter.getRootNode());
importMap(importFilter.getRootNode());
}
catch (IMapReader::FailureException& ex)
{
// Clear out the root node, otherwise we end up with half a map
scene::NodeRemover remover;
importFilter.getRootNode()->traverseChildren(remover);
throw cmd::ExecutionFailure(fmt::format(_("Failure reading map from clipboard:\n{0}"), ex.what()));
}
}
}
}
|