| 563 | } |
| 564 | |
| 565 | Expected<LoadedObject> deserializeObjectTreeFromFolder( const std::filesystem::path& folder, |
| 566 | const ProgressCallback& progressCb ) |
| 567 | { |
| 568 | MR_TIMER; |
| 569 | |
| 570 | std::error_code ec; |
| 571 | std::filesystem::path jsonFile; |
| 572 | for ( auto entry : Directory{ folder, ec } ) |
| 573 | { |
| 574 | // unlike extension() this works even if full file name is simply ".json" |
| 575 | if ( entry.path().u8string().ends_with( u8".json" ) ) |
| 576 | { |
| 577 | jsonFile = entry.path(); |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | auto readRes = deserializeJsonValue( jsonFile ); |
| 583 | if( !readRes.has_value() ) |
| 584 | { |
| 585 | return unexpected( readRes.error() ); |
| 586 | } |
| 587 | auto root = readRes.value(); |
| 588 | if ( auto formatVersion = root["FormatVersion"]; formatVersion.isNumeric() && formatVersion.asDouble() >= 2 ) |
| 589 | { |
| 590 | return unexpected( "Unsupported version of scene file. Please update your application." ); |
| 591 | } |
| 592 | |
| 593 | LoadedObject res; |
| 594 | if ( auto lengthUnits = root["LengthUnits"]; lengthUnits.isString() ) |
| 595 | { |
| 596 | auto lengthUnitsStr = lengthUnits.asString(); |
| 597 | for ( int i = 0; i < (int)LengthUnit::_count; ++i ) |
| 598 | if ( lengthUnitsStr == getUnitInfo( (LengthUnit)i ).prettyName ) |
| 599 | { |
| 600 | res.lengthUnit = (LengthUnit)i; |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | auto typeTreeSize = root["Type"].size(); |
| 606 | for (int i = typeTreeSize-1;i>=0;--i) |
| 607 | { |
| 608 | const auto& type = root["Type"][unsigned( i )]; |
| 609 | if ( type.isString() ) |
| 610 | res.obj = createObject( type.asString() ); |
| 611 | if ( res.obj ) |
| 612 | break; |
| 613 | } |
| 614 | if ( !res.obj ) |
| 615 | return unexpected( "Unknown root object type" ); |
| 616 | |
| 617 | int modelNumber{ 0 }; |
| 618 | int modelCounter{ 0 }; |
| 619 | auto cb = progressCb; |
| 620 | if ( progressCb ) |
| 621 | { |
| 622 | std::function<int( const Json::Value& )> calculateModelNum = [&calculateModelNum] ( const Json::Value& root ) |
no test coverage detected