| 1132 | } |
| 1133 | |
| 1134 | shared_ptr<Assets::AssetData> Assets::loadAsset(AssetId const& id) const { |
| 1135 | if (auto asset = m_assetsCache.value(id)) |
| 1136 | return asset; |
| 1137 | |
| 1138 | if (m_queue.value(id, QueuePriority::None) == QueuePriority::Working) |
| 1139 | return {}; |
| 1140 | |
| 1141 | try { |
| 1142 | m_queue[id] = QueuePriority::Working; |
| 1143 | shared_ptr<AssetData> assetData; |
| 1144 | |
| 1145 | try { |
| 1146 | if (id.type == AssetType::Json) { |
| 1147 | assetData = loadJson(id.path); |
| 1148 | } else if (id.type == AssetType::Image) { |
| 1149 | assetData = loadImage(id.path); |
| 1150 | } else if (id.type == AssetType::Audio) { |
| 1151 | assetData = loadAudio(id.path); |
| 1152 | } else if (id.type == AssetType::Font) { |
| 1153 | assetData = loadFont(id.path); |
| 1154 | } else if (id.type == AssetType::Bytes) { |
| 1155 | assetData = loadBytes(id.path); |
| 1156 | } |
| 1157 | |
| 1158 | } catch (StarException const& e) { |
| 1159 | if (id.type == AssetType::Image && m_settings.missingImage) { |
| 1160 | Logger::error("Could not load image asset '{}', using placeholder default.\n{}", id.path, outputException(e, false)); |
| 1161 | assetData = loadImage({*m_settings.missingImage, {}, {}}); |
| 1162 | } else if (id.type == AssetType::Audio && m_settings.missingAudio) { |
| 1163 | Logger::error("Could not load audio asset '{}', using placeholder default.\n{}", id.path, outputException(e, false)); |
| 1164 | assetData = loadAudio({*m_settings.missingAudio, {}, {}}); |
| 1165 | } else { |
| 1166 | throw; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | if (assetData) { |
| 1171 | if (assetData->needsPostProcessing) |
| 1172 | m_queue[id] = QueuePriority::PostProcess; |
| 1173 | else |
| 1174 | m_queue.remove(id); |
| 1175 | m_assetsCache[id] = assetData; |
| 1176 | m_assetsDone.broadcast(); |
| 1177 | freshen(assetData); |
| 1178 | |
| 1179 | } else { |
| 1180 | // We have failed to load an asset because it depends on an asset |
| 1181 | // currently being worked on. Mark it as needing loading and move it to |
| 1182 | // the end of the queue. |
| 1183 | m_queue[id] = QueuePriority::Load; |
| 1184 | m_assetsQueued.signal(); |
| 1185 | m_queue.toBack(id); |
| 1186 | } |
| 1187 | |
| 1188 | return assetData; |
| 1189 | |
| 1190 | } catch (...) { |
| 1191 | m_queue.remove(id); |