#include "core/resources/resource_cache.hpp" #include #include #include "core/audio/jail_audio.hpp" #include "core/resources/palette_loader.hpp" #include "core/resources/resource_helper.hpp" #include "utils/log.hpp" #include "utils/utils.hpp" // Deleters dels recursos d'àudio: necessiten veure el tipus complet de // Ja::Music / Ja::Sound (definits a jail_audio.hpp), per això van ací. void MusicDeleter::operator()(Ja::Music* music) const noexcept { if (music != nullptr) { Ja::deleteMusic(music); } } void SoundDeleter::operator()(Ja::Sound* sound) const noexcept { if (sound != nullptr) { Ja::deleteSound(sound); } } namespace Resource { auto Cache::get() -> Cache& { static Cache instance_; return instance_; } void Cache::loadSurface(const std::string& name, const std::string& file_path) { auto surface = std::make_shared(file_path); surfaces_.emplace_back(SurfaceResource{.name = name, .surface = std::move(surface)}); } void Cache::loadAnimation(const std::string& name, const std::string& file_path) { auto bytes = Helper::loadFile(file_path); if (bytes.empty()) { throw std::runtime_error("Cache: cannot load animation " + file_path); } animations_.emplace_back(AnimationResource{.name = name, .yaml_data = std::move(bytes)}); printStatus("Animation", getFileName(file_path), StatusKind::SUCCESS); } void Cache::loadPalette(const std::string& name, const std::string& file_path) { auto palette = PaletteLoader::loadFromFile(file_path); palettes_.emplace_back(PaletteResource{.name = name, .palette = palette}); printStatus("Palette", getFileName(file_path), StatusKind::SUCCESS); } auto Cache::getSurface(const std::string& name) -> std::shared_ptr { auto it = std::ranges::find_if(surfaces_, [&name](const auto& s) { return s.name == name; }); if (it == surfaces_.end()) { Log::error("Cache", "surface not found: {}", name); throw std::runtime_error("Surface not found: " + name); } return it->surface; } auto Cache::getAnimationData(const std::string& name) -> const AnimationResource& { auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; }); if (it == animations_.end()) { Log::error("Cache", "animation not found: {}", name); throw std::runtime_error("Animation not found: " + name); } return *it; } auto Cache::getPalette(const std::string& name) -> const Palette& { auto it = std::ranges::find_if(palettes_, [&name](const auto& p) { return p.name == name; }); if (it == palettes_.end()) { Log::error("Cache", "palette not found: {}", name); throw std::runtime_error("Palette not found: " + name); } return it->palette; } void Cache::loadMusic(const std::string& name, const std::string& file_path) { auto bytes = Helper::loadFile(file_path); if (bytes.empty()) { throw std::runtime_error("Cache: cannot load music " + file_path); } Ja::Music* raw = Ja::loadMusic(bytes.data(), static_cast(bytes.size()), file_path.c_str()); if (raw == nullptr) { throw std::runtime_error("Cache: Ja::loadMusic failed for " + file_path); } musics_.emplace_back(MusicResource{.name = name, .music = std::unique_ptr(raw)}); printStatus("Music", getFileName(file_path), StatusKind::SUCCESS); } void Cache::loadSound(const std::string& name, const std::string& file_path) { auto bytes = Helper::loadFile(file_path); if (bytes.empty()) { throw std::runtime_error("Cache: cannot load sound " + file_path); } Ja::Sound* raw = Ja::loadSound(bytes.data(), static_cast(bytes.size())); if (raw == nullptr) { throw std::runtime_error("Cache: Ja::loadSound failed for " + file_path); } sounds_.emplace_back(SoundResource{.name = name, .sound = std::unique_ptr(raw)}); printStatus("Sound", getFileName(file_path), StatusKind::SUCCESS); } auto Cache::getMusic(const std::string& name) -> Ja::Music* { auto it = std::ranges::find_if(musics_, [&name](const auto& m) { return m.name == name; }); if (it == musics_.end()) { Log::error("Cache", "music not found: {}", name); throw std::runtime_error("Music not found: " + name); } return it->music.get(); } auto Cache::getSound(const std::string& name) -> Ja::Sound* { auto it = std::ranges::find_if(sounds_, [&name](const auto& s) { return s.name == name; }); if (it == sounds_.end()) { Log::error("Cache", "sound not found: {}", name); throw std::runtime_error("Sound not found: " + name); } return it->sound.get(); } void Cache::clearAudio() { musics_.clear(); sounds_.clear(); } } // namespace Resource