56 std::ifstream inFile( aPath.ToStdString(), std::ios::binary );
61 std::vector<uint8_t> data( ( std::istreambuf_iterator<char>( inFile ) ),
62 std::istreambuf_iterator<char>() );
65 if( data.size() < 20 )
70 uint32_t jsonType = 0;
71 std::memcpy( &magic, &data[0], 4 );
72 std::memcpy( &jsonLen, &data[12], 4 );
73 std::memcpy( &jsonType, &data[16], 4 );
78 if( 20 + jsonLen > data.size() )
81 std::string jsonStr( data.begin() + 20, data.begin() + 20 + jsonLen );
87 gltf = nlohmann::json::parse( jsonStr );
89 catch(
const nlohmann::json::parse_error& )
96 if( !gltf.contains(
"meshes" ) || !gltf.contains(
"accessors" ) )
99 auto& accessors = gltf[
"accessors"];
101 for(
auto& mesh : gltf[
"meshes"] )
103 if( !mesh.contains(
"primitives" ) )
106 for(
auto& prim : mesh[
"primitives"] )
118 if( prim.contains(
"indices" ) )
120 int idx = prim[
"indices"].get<
int>();
122 if( idx >= 0 && idx <
static_cast<int>( accessors.size() ) )
124 int count = accessors[idx].value(
"count", 0 );
147static wxString
WriteTempGlb(
const nlohmann::json& aGltf,
const wxString& aPrefix )
149 std::string
json = aGltf.dump();
151 while(
json.size() % 4 != 0 )
152 json.push_back(
' ' );
154 uint32_t jsonLen =
static_cast<uint32_t
>(
json.size() );
155 uint32_t version = 2;
157 uint32_t binType = 0x004E4942;
158 uint32_t total = 12 + 8 + jsonLen + 8 + binLen;
160 std::vector<uint8_t> data;
162 auto append32 = [&]( uint32_t v )
165 std::memcpy( bytes, &v, 4 );
166 data.insert( data.end(), bytes, bytes + 4 );
174 data.insert( data.end(),
json.begin(),
json.end() );
178 wxString
path( wxFileName::CreateTempFileName( aPrefix ) );
179 std::ofstream out(
path.ToStdString(), std::ios::binary | std::ios::trunc );
180 out.write(
reinterpret_cast<const char*
>( data.data() ), data.size() );
220 wxString badPath =
GetTestGlbPath( wxS(
"issue21706 (BAD 9.0.4).glb" ) );
225 std::filesystem::path tempFile( wxFileName::CreateTempFileName( wxS(
"test_issue21706_fix" ) ).
ToStdString() );
226 std::filesystem::copy_file( badPath.ToStdString(), tempFile,
227 std::filesystem::copy_options::overwrite_existing );
229 wxString tempPath( tempFile.string() );
246 std::filesystem::remove( tempFile );
252 wxString goodPath =
GetTestGlbPath( wxS(
"issue21706 (GOOD 9.0.1).glb" ) );
256 std::filesystem::path tempFile( wxFileName::CreateTempFileName( wxS(
"test_issue21706_good" ) ).
ToStdString() );
257 std::filesystem::copy_file( goodPath.ToStdString(), tempFile,
258 std::filesystem::copy_options::overwrite_existing );
260 wxString tempPath( tempFile.string() );
263 auto origSize = std::filesystem::file_size( tempFile );
269 auto newSize = std::filesystem::file_size( tempFile );
277 std::filesystem::remove( tempFile );
358 gltf[
"accessors"] = nlohmann::json::array(
359 { { {
"count", 3 }, {
"componentType", 5123 }, {
"type",
"SCALAR" } } } );
360 gltf[
"meshes"] = nlohmann::json::array(
366 std::vector<uint8_t> data;
368 std::ifstream in(
path.ToStdString(), std::ios::binary );
369 data.assign( ( std::istreambuf_iterator<char>( in ) ), std::istreambuf_iterator<char>() );
372 uint32_t bogusTotal =
static_cast<uint32_t
>( data.size() ) + 1024;
373 std::memcpy( &data[8], &bogusTotal, 4 );
376 std::ofstream out(
path.ToStdString(), std::ios::binary | std::ios::trunc );
377 out.write(
reinterpret_cast<const char*
>( data.data() ), data.size() );
382 std::filesystem::remove(
path.ToStdString() );
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")