| 129 | } // namespace |
| 130 | |
| 131 | Future<ReadJsonResult<Subtree>> SubtreeFileReader::loadBinary( |
| 132 | const CesiumAsync::AsyncSystem& asyncSystem, |
| 133 | const std::shared_ptr<IAssetAccessor>& pAssetAccessor, |
| 134 | const std::string& url, |
| 135 | const std::vector<CesiumAsync::IAssetAccessor::THeader>& requestHeaders, |
| 136 | const std::span<const std::byte>& data) const noexcept { |
| 137 | if (data.size() < sizeof(SubtreeHeader)) { |
| 138 | CesiumJsonReader::ReadJsonResult<Subtree> result; |
| 139 | result.errors.emplace_back(fmt::format( |
| 140 | "The binary Subtree file is invalid because it is too small to include " |
| 141 | "a Subtree header.", |
| 142 | data.size())); |
| 143 | return asyncSystem.createResolvedFuture(std::move(result)); |
| 144 | } |
| 145 | |
| 146 | const SubtreeHeader* header = |
| 147 | reinterpret_cast<const SubtreeHeader*>(data.data()); |
| 148 | if (header->jsonByteLength > data.size() - sizeof(SubtreeHeader)) { |
| 149 | CesiumJsonReader::ReadJsonResult<Subtree> result; |
| 150 | result.errors.emplace_back(fmt::format( |
| 151 | "The binary Subtree file is invalid because it is too small to include " |
| 152 | "the jsonByteLength specified in its header.", |
| 153 | data.size())); |
| 154 | return asyncSystem.createResolvedFuture(std::move(result)); |
| 155 | } |
| 156 | |
| 157 | if (header->binaryByteLength > |
| 158 | data.size() - sizeof(SubtreeHeader) - header->jsonByteLength) { |
| 159 | CesiumJsonReader::ReadJsonResult<Subtree> result; |
| 160 | result.errors.emplace_back(fmt::format( |
| 161 | "The binary Subtree file is invalid because it is too small to include " |
| 162 | "the binaryByteLength specified in its header.", |
| 163 | data.size())); |
| 164 | return asyncSystem.createResolvedFuture(std::move(result)); |
| 165 | } |
| 166 | |
| 167 | ReadJsonResult<Subtree> result = this->_reader.readFromJson( |
| 168 | data.subspan(sizeof(SubtreeHeader), size_t(header->jsonByteLength))); |
| 169 | |
| 170 | if (result.value) { |
| 171 | std::span<const std::byte> binaryChunk = data.subspan( |
| 172 | sizeof(SubtreeHeader) + size_t(header->jsonByteLength), |
| 173 | size_t(header->binaryByteLength)); |
| 174 | |
| 175 | if (binaryChunk.size() > 0) { |
| 176 | if (result.value->buffers.empty()) { |
| 177 | result.errors.emplace_back( |
| 178 | "Subtree has a binary chunk but the JSON does " |
| 179 | "not define any buffers."); |
| 180 | return asyncSystem.createResolvedFuture(std::move(result)); |
| 181 | } |
| 182 | |
| 183 | Buffer& buffer = result.value->buffers[0]; |
| 184 | if (buffer.uri) { |
| 185 | result.errors.emplace_back( |
| 186 | "Subtree has a binary chunk but the first buffer " |
| 187 | "in the JSON chunk also has a 'uri'."); |
| 188 | return asyncSystem.createResolvedFuture(std::move(result)); |
no test coverage detected