| 60 | } |
| 61 | |
| 62 | Result<Ref<AssetCatalog>> AssetCatalog::parse(const Byte* data, size_t length) { |
| 63 | ValdiArchive archive(data, data + length); |
| 64 | |
| 65 | auto entries = archive.getEntries(); |
| 66 | if (!entries) { |
| 67 | return entries.moveError(); |
| 68 | } |
| 69 | |
| 70 | std::vector<AssetSpecs> assets; |
| 71 | |
| 72 | for (const auto& entry : entries.value()) { |
| 73 | auto parser = Parser(entry.data, entry.data + entry.dataLength); |
| 74 | |
| 75 | AssetData assetData; |
| 76 | if (entry.dataLength >= sizeof(AssetData)) { |
| 77 | auto result = parser.parseStruct<AssetData>(); |
| 78 | if (!result) { |
| 79 | return result.moveError(); |
| 80 | } |
| 81 | std::memcpy(&assetData, reinterpret_cast<const void*>(result.value()), sizeof(AssetData)); |
| 82 | } else { |
| 83 | auto result = parser.parseStruct<AssetDataLegacy>(); |
| 84 | if (!result) { |
| 85 | return result.moveError(); |
| 86 | } |
| 87 | std::memcpy(&assetData, reinterpret_cast<const void*>(result.value()), sizeof(AssetDataLegacy)); |
| 88 | assetData.type = kAssetTypeImage; |
| 89 | } |
| 90 | |
| 91 | auto type = AssetSpecsType::UNKNOWN; |
| 92 | if (assetData.type == kAssetTypeImage) { |
| 93 | type = AssetSpecsType::IMAGE; |
| 94 | } else if (assetData.type == kAssetTypeFont) { |
| 95 | type = AssetSpecsType::FONT; |
| 96 | } |
| 97 | |
| 98 | assets.emplace_back( |
| 99 | entry.filePath, static_cast<int>(assetData.width), static_cast<int>(assetData.height), type); |
| 100 | } |
| 101 | |
| 102 | return Valdi::makeShared<AssetCatalog>(std::move(assets)); |
| 103 | } |
| 104 | |
| 105 | const std::vector<AssetSpecs>& AssetCatalog::getAssets() const { |
| 106 | return _assets; |
nothing calls this directly
no test coverage detected