| 227 | } |
| 228 | |
| 229 | void asyncFromAnySupportedFormat( const std::vector<std::filesystem::path>& files, |
| 230 | const SceneLoad::PostLoadCallback& postLoadCallback, const Settings& settings ) |
| 231 | { |
| 232 | auto ctx = std::make_shared<AsyncLoadContext>(); |
| 233 | ctx->paths = files; |
| 234 | std::erase_if( ctx->paths, [] ( auto&& path ) { return path.empty(); } ); |
| 235 | |
| 236 | const auto count = ctx->paths.size(); |
| 237 | ctx->results.resize( count, unexpected( "Uninitialized" ) ); |
| 238 | |
| 239 | size_t syncIndex = 0; |
| 240 | BitSet asyncBitSet( count ); |
| 241 | for ( auto index = 0ull; index < count; ++index ) |
| 242 | { |
| 243 | const auto& path = ctx->paths[index]; |
| 244 | if ( findAsyncObjectLoadFilter( path ) ) |
| 245 | { |
| 246 | asyncBitSet.set( index ); |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | spdlog::info( "Loading file {}", utf8string( path ) ); |
| 251 | ctx->results[index] = sLoadPath( path, settings.openFolder, subprogress( settings.progress, syncIndex++, count ) ); |
| 252 | } |
| 253 | } |
| 254 | assert( syncIndex + asyncBitSet.count() == count ); |
| 255 | |
| 256 | ctx->progressCallback = subprogress( settings.progress, (float)syncIndex / (float)count, 1.00f ); |
| 257 | ctx->initializeProgressMap( asyncBitSet ); |
| 258 | |
| 259 | auto postLoad = [ctx, count, postLoadCallback, targetUnit = settings.targetUnit] |
| 260 | { |
| 261 | SceneConstructor constructor( targetUnit ); |
| 262 | for ( auto index = 0ull; index < count; ++index ) |
| 263 | constructor.process( ctx->paths[index], ctx->results[index] ); |
| 264 | postLoadCallback( constructor.construct() ); |
| 265 | }; |
| 266 | |
| 267 | if ( asyncBitSet.none() ) |
| 268 | return postLoad(); |
| 269 | |
| 270 | ctx->asyncCount = asyncBitSet.count(); |
| 271 | for ( const auto index : asyncBitSet ) |
| 272 | { |
| 273 | const auto& path = ctx->paths[index]; |
| 274 | const auto asyncFilter = findAsyncObjectLoadFilter( path ); |
| 275 | assert( asyncFilter ); |
| 276 | const auto asyncLoader = AsyncObjectLoad::getObjectLoader( *asyncFilter ); |
| 277 | assert( asyncLoader ); |
| 278 | const auto callback = ctx->progressCallbackFor( index ); |
| 279 | spdlog::info( "Async loading file {}", utf8string( path ) ); |
| 280 | asyncLoader( path, [ctx, index, postLoad, callback] ( Expected<LoadedObjects> result ) |
| 281 | { |
| 282 | ctx->results[index] = std::move( result ); |
| 283 | reportProgress( callback, 1.00f ); |
| 284 | if ( ctx->asyncCount.fetch_sub( 1 ) == 1 ) |
| 285 | // that was the last file |
| 286 | postLoad(); |
no test coverage detected