Merge pull request #10983 from davidkelly/dk/marketplaceIDHack

Get marketplaceID into imported entities
This commit is contained in:
David Kelly 2017-07-18 15:25:49 -07:00 committed by GitHub
commit 4d4d9421d3
2 changed files with 62 additions and 12 deletions

View file

@ -29,8 +29,12 @@
#include <QVector>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QFileInfo>
#include <QString>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <GeometryUtil.h>
#include <Gzip.h>
@ -1667,7 +1671,28 @@ 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/<uuid>-v1/<item name>.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-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);
if (QUuid(matched).isNull()) {
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<ResourceRequest>(DependencyManager::get<ResourceManager>()->createResourceRequest(this, urlString));
@ -1686,11 +1711,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 +1727,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 +1863,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 +1907,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);

View file

@ -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;