------------------------------------------------------------------------------
| 112 | |
| 113 | //------------------------------------------------------------------------------ |
| 114 | bool vtkGLTFDocumentLoaderInternals::LoadFileMetaData(nlohmann::json& gltfRoot) |
| 115 | { |
| 116 | try |
| 117 | { |
| 118 | const auto& stream = this->Self->GetInternalModel()->Stream; |
| 119 | stream->Seek(this->Self->GetGLBStart(), vtkResourceStream::SeekDirection::Begin); |
| 120 | |
| 121 | // Determine the format |
| 122 | std::string magic; |
| 123 | magic.resize(4); |
| 124 | stream->Read(magic.data(), magic.size()); |
| 125 | |
| 126 | if (magic == "glTF") |
| 127 | { |
| 128 | std::uint32_t version; |
| 129 | std::uint32_t fileLength; |
| 130 | std::vector<vtkGLTFUtils::ChunkInfoType> chunkInfo; |
| 131 | if (vtkGLTFUtils::ExtractGLBFileInformation( |
| 132 | stream, version, fileLength, this->Self->GetGLBStart(), chunkInfo)) |
| 133 | { |
| 134 | if (!vtkGLTFUtils::ValidateGLBFile(magic, version, fileLength, chunkInfo)) |
| 135 | { |
| 136 | vtkErrorWithObjectMacro(this->Self, "Invalid binary glTF file"); |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | // Get JSON chunk's information (we know it exists and it's the first chunk) |
| 141 | vtkGLTFUtils::ChunkInfoType& JSONChunkInfo = chunkInfo[0]; |
| 142 | |
| 143 | // Jump to chunk data start |
| 144 | stream->Seek(this->Self->GetGLBStart() + vtkGLTFUtils::GLBHeaderSize + |
| 145 | vtkGLTFUtils::GLBChunkHeaderSize, |
| 146 | vtkResourceStream::SeekDirection::Begin); |
| 147 | // Read chunk data |
| 148 | std::vector<char> JSONDataBuffer; |
| 149 | JSONDataBuffer.resize(JSONChunkInfo.second); |
| 150 | if (stream->Read(JSONDataBuffer.data(), JSONChunkInfo.second) != JSONChunkInfo.second) |
| 151 | { |
| 152 | vtkErrorWithObjectMacro(this->Self, "Failed to read chunk 0."); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | gltfRoot = nlohmann::json::parse(JSONDataBuffer); |
| 157 | } |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | // Text gltf |
| 162 | stream->Seek(0, vtkResourceStream::SeekDirection::End); |
| 163 | const auto fileSize = static_cast<std::size_t>(stream->Tell()) - this->Self->GetGLBStart(); |
| 164 | stream->Seek(this->Self->GetGLBStart(), vtkResourceStream::SeekDirection::Begin); |
| 165 | |
| 166 | std::vector<char> fileData; |
| 167 | fileData.resize(fileSize); |
| 168 | if (stream->Read(fileData.data(), fileData.size()) != fileData.size()) |
| 169 | { |
| 170 | vtkErrorWithObjectMacro(this->Self, "Failed to read GLTF file"); |
| 171 | return false; |
no test coverage detected