| 13 | #include "Engine/Content/Upgraders/BinaryAssetUpgrader.h" |
| 14 | |
| 15 | bool BinaryAssetFactoryBase::Init(BinaryAsset* asset) |
| 16 | { |
| 17 | ASSERT(asset && asset->Storage); |
| 18 | auto storage = asset->Storage; |
| 19 | |
| 20 | // Load serialized asset data |
| 21 | AssetInitData initData; |
| 22 | if (storage->LoadAssetHeader(asset->GetID(), initData)) |
| 23 | { |
| 24 | LOG(Error, "Cannot load asset header.\nInfo: {0}", AssetInfo(asset->GetID(), asset->GetTypeName(), storage->GetPath()).ToString()); |
| 25 | return true; |
| 26 | } |
| 27 | |
| 28 | #if USE_EDITOR |
| 29 | // Check if need to perform data conversion to the newer version (only in Editor) |
| 30 | const auto upgrader = GetUpgrader(); |
| 31 | if (!storage->IsReadOnly() && upgrader && upgrader->ShouldUpgrade(initData.SerializedVersion)) |
| 32 | { |
| 33 | const auto startTime = DateTime::NowUTC(); |
| 34 | const AssetInfo info(asset->GetID(), asset->GetTypeName(), storage->GetPath()); |
| 35 | LOG(Info, "Starting asset \'{0}\' conversion", info.Path); |
| 36 | |
| 37 | // Backup source file (in case of conversion failure) |
| 38 | String backupPath; |
| 39 | FileSystem::GetTempFilePath(backupPath); |
| 40 | if (FileSystem::CopyFile(backupPath, info.Path)) |
| 41 | { |
| 42 | LOG(Warning, "Failed to create backup file \'{0}\'. Cannot copy file.", backupPath); |
| 43 | } |
| 44 | |
| 45 | // Create asset version migration context |
| 46 | AssetMigrationContext context; |
| 47 | context.Input = initData; |
| 48 | |
| 49 | // Perform conversion |
| 50 | bool conversionFailed = UpgradeAsset(info, storage, context); |
| 51 | |
| 52 | // Process result |
| 53 | if (conversionFailed) |
| 54 | { |
| 55 | LOG(Error, "Asset \'{0}\' conversion failed! Restoring backup file.", info.ToString()); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | // Peek the results |
| 60 | initData = context.Output; |
| 61 | |
| 62 | auto msg = String::Format(TEXT("Asset \'{0}\' upgraded to version {1} successfully ({2} ms)"), |
| 63 | info.ToString(), |
| 64 | initData.SerializedVersion, |
| 65 | Math::FloorToInt((float)(DateTime::NowUTC() - startTime).GetTotalMilliseconds())); |
| 66 | LOG_STR(Info, msg); |
| 67 | } |
| 68 | |
| 69 | // Remove or restore backup file |
| 70 | if (FileSystem::FileExists(backupPath)) |
| 71 | { |
| 72 | if (conversionFailed) |
no test coverage detected