QByteArray FSTReader::writeMapping(const QVariantHash& mapping) { static const QStringList PREFERED_ORDER = QStringList() << NAME_FIELD << TYPE_FIELD << SCALE_FIELD << FILENAME_FIELD << TEXDIR_FIELD << JOINT_FIELD << FREE_JOINT_FIELD << BLENDSHAPE_FIELD << JOINT_INDEX_FIELD; QBuffer buffer; buffer.open(QIODevice::WriteOnly); for (auto key : PREFERED_ORDER) { auto it = mapping.find(key); if (it != mapping.constEnd()) { if (key == FREE_JOINT_FIELD) { // writeVariant does not handle strings added using insertMulti. for (auto multi : mapping.values(key)) { buffer.write(key.toUtf8()); buffer.write(" = "); buffer.write(multi.toByteArray()); buffer.write("\n"); } } else { writeVariant(buffer, it); } } } for (auto it = mapping.constBegin(); it != mapping.constEnd(); it++) { if (!PREFERED_ORDER.contains(it.key())) { writeVariant(buffer, it); } } return buffer.data(); }
void GeometryReader::run() { DependencyManager::get<StatTracker>()->decrementStat("PendingProcessing"); CounterStat counter("Processing"); PROFILE_RANGE_EX(resource_parse_geometry, "GeometryReader::run", 0xFF00FF00, 0, { { "url", _url.toString() } }); auto originalPriority = QThread::currentThread()->priority(); if (originalPriority == QThread::InheritPriority) { originalPriority = QThread::NormalPriority; } QThread::currentThread()->setPriority(QThread::LowPriority); Finally setPriorityBackToNormal([originalPriority]() { QThread::currentThread()->setPriority(originalPriority); }); if (!_resource.data()) { return; } try { if (_data.isEmpty()) { throw QString("reply is NULL"); } // Ensure the resource has not been deleted auto resource = _resource.toStrongRef(); if (!resource) { qCWarning(modelnetworking) << "Abandoning load of" << _url << "; could not get strong ref"; return; } if (_url.path().isEmpty()) { throw QString("url is invalid"); } HFMModel::Pointer hfmModel; QVariantHash serializerMapping = _mapping.second; serializerMapping["combineParts"] = _combineParts; serializerMapping["deduplicateIndices"] = true; if (_url.path().toLower().endsWith(".gz")) { QByteArray uncompressedData; if (!gunzip(_data, uncompressedData)) { throw QString("failed to decompress .gz model"); } // Strip the compression extension from the path, so the loader can infer the file type from what remains. // This is okay because we don't expect the serializer to be able to read the contents of a compressed model file. auto strippedUrl = _url; strippedUrl.setPath(_url.path().left(_url.path().size() - 3)); hfmModel = _modelLoader.load(uncompressedData, serializerMapping, strippedUrl, ""); } else { hfmModel = _modelLoader.load(_data, serializerMapping, _url, _webMediaType.toStdString()); } if (!hfmModel) { throw QString("unsupported format"); } if (hfmModel->meshes.empty() || hfmModel->joints.empty()) { throw QString("empty geometry, possibly due to an unsupported model version"); } // Add scripts to hfmModel if (!serializerMapping.value(SCRIPT_FIELD).isNull()) { QVariantList scripts = serializerMapping.values(SCRIPT_FIELD); for (auto &script : scripts) { hfmModel->scripts.push_back(script.toString()); } } // Do processing on the model baker::Baker modelBaker(hfmModel, _mapping.second, _mapping.first); modelBaker.run(); auto processedHFMModel = modelBaker.getHFMModel(); auto materialMapping = modelBaker.getMaterialMapping(); QMetaObject::invokeMethod(resource.data(), "setGeometryDefinition", Q_ARG(HFMModel::Pointer, processedHFMModel), Q_ARG(MaterialMapping, materialMapping)); } catch (const std::exception&) { auto resource = _resource.toStrongRef(); if (resource) { QMetaObject::invokeMethod(resource.data(), "finishedLoading", Q_ARG(bool, false)); } } catch (QString& e) { qCWarning(modelnetworking) << "Exception while loading model --" << e; auto resource = _resource.toStrongRef(); if (resource) { QMetaObject::invokeMethod(resource.data(), "finishedLoading", Q_ARG(bool, false)); } } }