From 061f084d9faf91123d9326dd4afbd186b2751642 Mon Sep 17 00:00:00 2001 From: David Kelly Date: Mon, 17 Jul 2017 15:08:30 -0700 Subject: [PATCH 1/2] Get marketplaceID into imported entities Rather than doing this on the backend, we can do it here at the time we import. Later we will have some support for getting an entities identity hash and querying the backend to get a marketplaceID for them as needed. However for now this is enough to get us moving forward. --- libraries/octree/src/Octree.cpp | 60 ++++++++++++++++++++++++++++++--- libraries/octree/src/Octree.h | 16 ++++----- 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index 2e93f3515f..c0d8ce33ab 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -29,8 +29,12 @@ #include #include #include +#include +#include #include #include +#include +#include #include #include @@ -1667,7 +1671,30 @@ bool Octree::readJSONFromGzippedFile(QString qFileName) { return readJSONFromStream(-1, jsonStream); } +// hack to get the marketplace id into the entities. We will create a way to get this from a hash of +// the entity later, but this helps us move things along for now +QString getMarketplaceID(const QString& urlString) { + // the url should be http://mpassets.highfidelity.com/-v1/.extension + // a regex for the this is a PITA as there are several valid versions of uuids, and so + // lets strip out the uuid (if any) and try to create a UUID from the string, relying on + // QT to parse it + static const QRegularExpression re("^http:\\/\\/mpassets.highfidelity.com\\/([0-9A-Fa-f\\-]+)v[\\d]+\\/.*"); + QRegularExpressionMatch match = re.match(urlString); + if (match.hasMatch()) { + QString matched = match.captured(1); + // strip the hyphen off the end because my regex is crap + matched.truncate(matched.size()-1); + if (QUuid() == QUuid(matched)) { + qDebug() << "invalid uuid for marketplaceID"; + } else { + return matched; + } + } + return QString(); +} + bool Octree::readFromURL(const QString& urlString) { + QString marketplaceID = getMarketplaceID(urlString); auto request = std::unique_ptr(DependencyManager::get()->createResourceRequest(this, urlString)); @@ -1686,11 +1713,11 @@ bool Octree::readFromURL(const QString& urlString) { auto data = request->getData(); QDataStream inputStream(data); - return readFromStream(data.size(), inputStream); + return readFromStream(data.size(), inputStream, marketplaceID); } -bool Octree::readFromStream(unsigned long streamLength, QDataStream& inputStream) { +bool Octree::readFromStream(unsigned long streamLength, QDataStream& inputStream, const QString& marketplaceID) { // decide if this is binary SVO or JSON-formatted SVO QIODevice *device = inputStream.device(); char firstChar; @@ -1702,7 +1729,7 @@ bool Octree::readFromStream(unsigned long streamLength, QDataStream& inputStream return readSVOFromStream(streamLength, inputStream); } else { qCDebug(octree) << "Reading from JSON SVO Stream length:" << streamLength; - return readJSONFromStream(streamLength, inputStream); + return readJSONFromStream(streamLength, inputStream, marketplaceID); } } @@ -1838,9 +1865,31 @@ bool Octree::readSVOFromStream(unsigned long streamLength, QDataStream& inputStr return fileOk; } +// hack to get the marketplace id into the entities. We will create a way to get this from a hash of +// the entity later, but this helps us move things along for now +QJsonDocument addMarketplaceIDToDocumentEntities(QJsonDocument& doc, const QString& marketplaceID) { + if (!marketplaceID.isEmpty()) { + QJsonDocument newDoc; + QJsonObject rootObj = doc.object(); + QJsonArray newEntitiesArray; + + // build a new entities array + auto entitiesArray = rootObj["Entities"].toArray(); + for(auto it = entitiesArray.begin(); it != entitiesArray.end(); it++) { + auto entity = (*it).toObject(); + entity["marketplaceID"] = marketplaceID; + newEntitiesArray.append(entity); + } + rootObj["Entities"] = newEntitiesArray; + newDoc.setObject(rootObj); + return newDoc; + } + return doc; +} + const int READ_JSON_BUFFER_SIZE = 2048; -bool Octree::readJSONFromStream(unsigned long streamLength, QDataStream& inputStream) { +bool Octree::readJSONFromStream(unsigned long streamLength, QDataStream& inputStream, const QString& marketplaceID /*=""*/) { // if the data is gzipped we may not have a useful bytesAvailable() result, so just keep reading until // we get an eof. Leave streamLength parameter for consistency. @@ -1860,6 +1909,9 @@ bool Octree::readJSONFromStream(unsigned long streamLength, QDataStream& inputSt } QJsonDocument asDocument = QJsonDocument::fromJson(jsonBuffer); + if (!marketplaceID.isEmpty()) { + asDocument = addMarketplaceIDToDocumentEntities(asDocument, marketplaceID); + } QVariant asVariant = asDocument.toVariant(); QVariantMap asMap = asVariant.toMap(); bool success = readFromMap(asMap); diff --git a/libraries/octree/src/Octree.h b/libraries/octree/src/Octree.h index 512a0ab64e..38454d20b5 100644 --- a/libraries/octree/src/Octree.h +++ b/libraries/octree/src/Octree.h @@ -212,14 +212,14 @@ public: virtual bool handlesEditPacketType(PacketType packetType) const { return false; } virtual int processEditPacketData(ReceivedMessage& message, const unsigned char* editData, int maxLength, const SharedNodePointer& sourceNode) { return 0; } - + virtual bool recurseChildrenWithData() const { return true; } virtual bool rootElementHasData() const { return false; } virtual int minimumRequiredRootDataBytes() const { return 0; } virtual bool suppressEmptySubtrees() const { return true; } virtual void releaseSceneEncodeData(OctreeElementExtraEncodeData* extraEncodeData) const { } virtual bool mustIncludeAllChildData() const { return true; } - + /// some versions of the SVO file will include breaks with buffer lengths between each buffer chunk in the SVO /// file. If the Octree subclass expects this for this particular version of the file, it should override this /// method and return true. @@ -236,15 +236,15 @@ public: void reaverageOctreeElements(OctreeElementPointer startElement = OctreeElementPointer()); void deleteOctreeElementAt(float x, float y, float z, float s); - + /// Find the voxel at position x,y,z,s /// \return pointer to the OctreeElement or NULL if none at x,y,z,s. OctreeElementPointer getOctreeElementAt(float x, float y, float z, float s) const; - + /// Find the voxel at position x,y,z,s /// \return pointer to the OctreeElement or to the smallest enclosing parent if none at x,y,z,s. OctreeElementPointer getOctreeEnclosingElementAt(float x, float y, float z, float s) const; - + OctreeElementPointer getOrCreateChildElementAt(float x, float y, float z, float s); OctreeElementPointer getOrCreateChildElementContaining(const AACube& box); @@ -261,7 +261,7 @@ public: int encodeTreeBitstream(const OctreeElementPointer& element, OctreePacketData* packetData, OctreeElementBag& bag, EncodeBitstreamParams& params) ; - + bool isDirty() const { return _isDirty; } void clearDirtyBit() { _isDirty = false; } void setDirtyBit() { _isDirty = true; } @@ -301,9 +301,9 @@ public: // Octree importers bool readFromFile(const char* filename); bool readFromURL(const QString& url); // will support file urls as well... - bool readFromStream(unsigned long streamLength, QDataStream& inputStream); + bool readFromStream(unsigned long streamLength, QDataStream& inputStream, const QString& marketplaceID=""); bool readSVOFromStream(unsigned long streamLength, QDataStream& inputStream); - bool readJSONFromStream(unsigned long streamLength, QDataStream& inputStream); + bool readJSONFromStream(unsigned long streamLength, QDataStream& inputStream, const QString& marketplaceID=""); bool readJSONFromGzippedFile(QString qFileName); virtual bool readFromMap(QVariantMap& entityDescription) = 0; From c4d5d16eb3de7065f49f55f2c90ccec624e07347 Mon Sep 17 00:00:00 2001 From: David Kelly Date: Tue, 18 Jul 2017 11:12:06 -0700 Subject: [PATCH 2/2] cr feedback --- libraries/octree/src/Octree.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index c0d8ce33ab..cceecaeed9 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -1678,13 +1678,11 @@ QString getMarketplaceID(const QString& urlString) { // a regex for the this is a PITA as there are several valid versions of uuids, and so // lets strip out the uuid (if any) and try to create a UUID from the string, relying on // QT to parse it - static const QRegularExpression re("^http:\\/\\/mpassets.highfidelity.com\\/([0-9A-Fa-f\\-]+)v[\\d]+\\/.*"); + static const QRegularExpression re("^http:\\/\\/mpassets.highfidelity.com\\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})-v[\\d]+\\/.*"); QRegularExpressionMatch match = re.match(urlString); if (match.hasMatch()) { QString matched = match.captured(1); - // strip the hyphen off the end because my regex is crap - matched.truncate(matched.size()-1); - if (QUuid() == QUuid(matched)) { + if (QUuid(matched).isNull()) { qDebug() << "invalid uuid for marketplaceID"; } else { return matched;