diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index 52a019aff6..59b9e9fc47 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -203,6 +203,10 @@ void AssetServer::handleAssetMappingOperation(QSharedPointer me handleDeleteMappingsOperation(*message, senderNode, *replyPacket); break; } + case AssetMappingOperationType::Rename: { + handleRenameMappingOperation(*message, senderNode, *replyPacket); + break; + } } auto nodeList = DependencyManager::get(); @@ -274,6 +278,21 @@ void AssetServer::handleDeleteMappingsOperation(ReceivedMessage& message, Shared } } +void AssetServer::handleRenameMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) { + if (senderNode->getCanRez()) { + QString oldPath = message.readString(); + QString newPath = message.readString(); + + if (renameMapping(oldPath, newPath)) { + replyPacket.writePrimitive(AssetServerError::NoError); + } else { + replyPacket.writePrimitive(AssetServerError::MappingOperationFailed); + } + } else { + replyPacket.writePrimitive(AssetServerError::PermissionDenied); + } +} + void AssetServer::handleAssetGetInfo(QSharedPointer message, SharedNodePointer senderNode) { QByteArray assetHash; MessageID messageID; @@ -468,7 +487,7 @@ bool AssetServer::writeMappingsToFile() { return false; } -bool AssetServer::setMapping(AssetPath path, AssetHash hash) { +bool AssetServer::setMapping(const AssetPath& path, const AssetHash& hash) { // remember what the old mapping was in case persistence fails auto oldMapping = _fileMappings.value(path).toString(); @@ -516,3 +535,25 @@ bool AssetServer::deleteMappings(const AssetPathList& paths) { return false; } } + +bool AssetServer::renameMapping(const AssetPath& oldPath, const AssetPath& newPath) { + // take the old hash to remove the old mapping + auto oldMapping = _fileMappings[oldPath].toString(); + + if (!oldMapping.isEmpty()) { + _fileMappings[newPath] = oldMapping; + + if (writeMappingsToFile()) { + // persisted the renamed mapping, return success + return true; + } else { + // we couldn't persist the renamed mapping, rollback and return failure + _fileMappings[oldPath] = oldMapping; + + return false; + } + } else { + // failed to find a mapping that was to be renamed, return failure + return false; + } +} diff --git a/assignment-client/src/assets/AssetServer.h b/assignment-client/src/assets/AssetServer.h index f81fcf46b3..f8597c3688 100644 --- a/assignment-client/src/assets/AssetServer.h +++ b/assignment-client/src/assets/AssetServer.h @@ -45,17 +45,21 @@ private: void handleGetAllMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket); void handleSetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket); void handleDeleteMappingsOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket); + void handleRenameMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket); // Mapping file operations must be called from main assignment thread only void loadMappingsFromFile(); bool writeMappingsToFile(); /// Set the mapping for path to hash - bool setMapping(AssetPath path, AssetHash hash); + bool setMapping(const AssetPath& path, const AssetHash& hash); /// Delete mapping `path`. Returns `true` if deletion of mappings succeeds, else `false`. bool deleteMappings(const AssetPathList& paths); + /// Rename mapping from `oldPath` to `newPath`. Returns true if successful + bool renameMapping(const AssetPath& oldPath, const AssetPath& newPath); + static void writeError(NLPacketList* packetList, AssetServerError error); void performMappingMigration();