From 39d1a2be6fab230de00e51b7518cc9eb1de764d0 Mon Sep 17 00:00:00 2001 From: sabrina-shanman Date: Fri, 30 Nov 2018 16:14:14 -0800 Subject: [PATCH] Simplify ModelLoader to utilize new model format registry --- .../src/model-networking/ModelLoader.cpp | 62 ++--------------- .../src/model-networking/ModelLoader.h | 67 ------------------- 2 files changed, 6 insertions(+), 123 deletions(-) diff --git a/libraries/model-networking/src/model-networking/ModelLoader.cpp b/libraries/model-networking/src/model-networking/ModelLoader.cpp index 04384acd39..ff32c3a999 100644 --- a/libraries/model-networking/src/model-networking/ModelLoader.cpp +++ b/libraries/model-networking/src/model-networking/ModelLoader.cpp @@ -11,64 +11,14 @@ #include "ModelLoader.h" -#include "FBXSerializer.h" -#include "OBJSerializer.h" -#include "GLTFSerializer.h" +#include +#include "ModelFormatRegistry.h" -ModelLoader::ModelLoader() { - // Add supported model formats - - MIMEType fbxMIMEType("fbx"); - fbxMIMEType.extensions.push_back("fbx"); - fbxMIMEType.fileSignatures.emplace_back("Kaydara FBX Binary \x00", 0); - addSupportedFormat(fbxMIMEType); - - MIMEType objMIMEType("obj"); - objMIMEType.extensions.push_back("obj"); - addSupportedFormat(objMIMEType); - - MIMEType gltfMIMEType("gltf"); - gltfMIMEType.extensions.push_back("gltf"); - gltfMIMEType.webMediaTypes.push_back("model/gltf+json"); - addSupportedFormat(gltfMIMEType); -} hfm::Model::Pointer ModelLoader::load(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const { - // Check file contents - for (auto& supportedFormat : supportedFormats) { - for (auto& fileSignature : supportedFormat.mimeType.fileSignatures) { - auto testBytes = data.mid(fileSignature.byteOffset, (int)fileSignature.bytes.size()).toStdString(); - if (testBytes == fileSignature.bytes) { - return supportedFormat.loader(data, mapping, url); - } - } + auto factory = DependencyManager::get()->getFactoryForMIMEType(data, mapping, url, webMediaType); + if (!factory) { + return hfm::Model::Pointer(); } - - // Check file extension - std::string urlString = url.path().toStdString(); - std::size_t extensionSeparator = urlString.rfind('.'); - if (extensionSeparator != std::string::npos) { - std::string detectedExtension = urlString.substr(extensionSeparator + 1); - for (auto& supportedFormat : supportedFormats) { - for (auto& extension : supportedFormat.mimeType.extensions) { - if (extension == detectedExtension) { - return supportedFormat.loader(data, mapping, url); - } - } - } - } - - // Check web media type - if (webMediaType != "") { - for (auto& supportedFormat : supportedFormats) { - for (auto& candidateWebMediaType : supportedFormat.mimeType.webMediaTypes) { - if (candidateWebMediaType == webMediaType) { - return supportedFormat.loader(data, mapping, url); - } - } - } - } - - // Supported file type not found. Abort loading. - return hfm::Model::Pointer(); + return factory->get()->read(data, mapping, url); } diff --git a/libraries/model-networking/src/model-networking/ModelLoader.h b/libraries/model-networking/src/model-networking/ModelLoader.h index 0deb339f15..5fbab4fb65 100644 --- a/libraries/model-networking/src/model-networking/ModelLoader.h +++ b/libraries/model-networking/src/model-networking/ModelLoader.h @@ -12,82 +12,15 @@ #ifndef hifi_ModelLoader_h #define hifi_ModelLoader_h -#include -#include -#include - #include #include -#include class ModelLoader { public: - - // A short sequence of bytes, typically at the beginning of the file, which identifies the file format - class FileSignature { - public: - FileSignature(const std::string& bytes, int byteOffset) : - bytes(bytes), - byteOffset(byteOffset) { - } - - std::string bytes; - int byteOffset; - }; - - // A named file extension with a list of known ways to positively identify the file type - class MIMEType { - public: - MIMEType(const std::string& name) : - name(name) { - } - - std::string name; - std::vector extensions; - std::vector webMediaTypes; - std::vector fileSignatures; - }; - - ModelLoader(); - - // T is a subclass of hfm::Serializer - template - void addSupportedFormat(const MIMEType& mimeType) { - supportedFormats.push_back(SupportedFormat(mimeType, SupportedFormat::getLoader())); - } - // Given the currently stored list of supported file formats, determine how to load a model from the given parameters. // If successful, return an owned reference to the newly loaded model. // If failed, return an empty reference. hfm::Model::Pointer load(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const; - -protected: - using Loader = std::function; - - class SupportedFormat { - public: - SupportedFormat(const MIMEType& mimeType, const Loader& loader) : - mimeType(mimeType), - loader(loader) { - } - - MIMEType mimeType; - Loader loader; - - template - static Loader getLoader() { - assert([](){ - T t; - return dynamic_cast(&t) != nullptr; - }()); - - return [](const hifi::ByteArray& bytes, const hifi::VariantHash& mapping, const hifi::URL& url) -> hfm::Model::Pointer { - return T().read(bytes, mapping, url); - }; - } - }; - - std::vector supportedFormats; }; #endif // hifi_ModelLoader_h