MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / SaveToAsset

Method SaveToAsset

Source/Engine/Content/BinaryAsset.cpp:321–399  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

319}
320
321bool BinaryAsset::SaveToAsset(const StringView& path, AssetInitData& data, bool silentMode)
322{
323 PROFILE_CPU();
324
325 // Ensure path is in a valid format
326 String pathNorm(path);
327 ContentStorageManager::FormatPath(pathNorm);
328 const StringView filePath = pathNorm;
329
330 // Find target storage container and the asset
331 auto storage = ContentStorageManager::TryGetStorage(filePath);
332 auto asset = Content::GetAsset(filePath);
333 auto binaryAsset = dynamic_cast<BinaryAsset*>(asset);
334 if (asset && !binaryAsset)
335 {
336 LOG(Warning, "Cannot write to the non-binary asset location.");
337 return true;
338 }
339 if (!binaryAsset && !storage && FileSystem::FileExists(filePath))
340 {
341 // Force-resolve storage (asset at that path could be not yet loaded into registry)
342 storage = ContentStorageManager::GetStorage(filePath);
343 }
344 if (storage && storage->IsReadOnly())
345 {
346 LOG(Warning, "Cannot write to the asset storage container.");
347 return true;
348 }
349
350 // Initialize data container
351 ASSERT(data.SerializedVersion > 0);
352 if (binaryAsset)
353 {
354 // Use the same asset ID
355 data.Header.ID = binaryAsset->GetID();
356 }
357 else if (storage && storage->GetEntriesCount())
358 {
359 // Use the same file ID
360 data.Header.ID = storage->GetEntry(0).ID;
361 }
362 else
363 {
364 // Randomize ID
365 data.Header.ID = Guid::New();
366 }
367
368 // Save (set flag to lock reloads on storage modified)
369 if (binaryAsset)
370 binaryAsset->_isSaving = true;
371 bool result;
372 if (storage)
373 {
374 // HACK: file is locked by some tasks (e.g material asset loaded some data and is updating the asset)
375 // Let's hide these locks just for the saving
376 const auto locks = Platform::AtomicRead(&storage->_chunksLock);
377 Platform::AtomicStore(&storage->_chunksLock, 0);
378 result = storage->Save(data, silentMode);

Callers

nothing calls this directly

Calls 12

GetAssetFunction · 0.85
GetIDMethod · 0.80
GetEntriesCountMethod · 0.80
NewFunction · 0.50
AtomicReadFunction · 0.50
AtomicStoreFunction · 0.50
InterlockedAddFunction · 0.50
CreateFunction · 0.50
IsReadOnlyMethod · 0.45
GetEntryMethod · 0.45
SaveMethod · 0.45
OnDependencyModifiedMethod · 0.45

Tested by

no test coverage detected