From 6190b35349786385222fac093219d847770b84e0 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 24 Oct 2025 13:59:03 +0200 Subject: [PATCH] integrat jail_audio en la carrega de resources.pack --- source/resource.cpp | 87 ++++++++++++++++++++------------------------- source/resource.hpp | 4 --- 2 files changed, 38 insertions(+), 53 deletions(-) diff --git a/source/resource.cpp b/source/resource.cpp index 3d7a05e..585b5e2 100644 --- a/source/resource.cpp +++ b/source/resource.cpp @@ -26,33 +26,16 @@ struct JA_Music_t; // lines 11-11 struct JA_Sound_t; // lines 12-12 -// Helper para cargar archivos de audio desde pack o filesystem +// Helper para cargar archivos de audio desde pack o filesystem en memoria namespace { -auto createTempAudioFile(const std::string& file_path, std::vector& temp_files_tracker) -> std::string { +struct AudioData { + std::vector data; + std::string filepath; +}; + +auto loadAudioData(const std::string& file_path) -> AudioData { auto resource_data = ResourceHelper::loadFile(file_path); - if (!resource_data.empty()) { - // Crear archivo temporal - std::string temp_dir; -#ifdef _WIN32 - temp_dir = std::getenv("TEMP") ? std::getenv("TEMP") : "C:\\temp"; -#else - temp_dir = "/tmp"; -#endif - std::string temp_path = temp_dir + "/ccae_audio_" + std::to_string(std::hash{}(file_path)); - std::ofstream temp_file(temp_path, std::ios::binary); - if (!temp_file) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Cannot create temp file %s", temp_path.c_str()); - return file_path; - } - temp_file.write(reinterpret_cast(resource_data.data()), resource_data.size()); - temp_file.close(); - - // Agregar a la lista de archivos temporales para limpieza posterior - temp_files_tracker.push_back(temp_path); - - return temp_path; - } - return file_path; // Usar ruta original si no está en pack + return AudioData{std::move(resource_data), file_path}; } } // namespace @@ -92,7 +75,6 @@ Resource::Resource(LoadingMode mode) // Destructor Resource::~Resource() { - cleanupTempAudioFiles(); clear(); } @@ -335,8 +317,12 @@ auto Resource::loadSoundLazy(const std::string& name) -> JA_Sound_t* { auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND); for (const auto& file : sound_list) { if (getFileName(file) == name) { - std::string audio_path = createTempAudioFile(file, Resource::get()->temp_audio_files_); - return JA_LoadSound(audio_path.c_str()); + auto audio_data = loadAudioData(file); + if (!audio_data.data.empty()) { + return JA_LoadSound(audio_data.data.data(), audio_data.data.size()); + } + // Fallback a cargar desde disco si no está en pack + return JA_LoadSound(file.c_str()); } } return nullptr; @@ -347,8 +333,12 @@ auto Resource::loadMusicLazy(const std::string& name) -> JA_Music_t* { auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC); for (const auto& file : music_list) { if (getFileName(file) == name) { - std::string audio_path = createTempAudioFile(file, Resource::get()->temp_audio_files_); - return JA_LoadMusic(audio_path.c_str()); + auto audio_data = loadAudioData(file); + if (!audio_data.data.empty()) { + return JA_LoadMusic(audio_data.data.data(), audio_data.data.size()); + } + // Fallback a cargar desde disco si no está en pack + return JA_LoadMusic(file.c_str()); } } return nullptr; @@ -484,8 +474,15 @@ void Resource::loadSounds() { for (const auto& l : list) { auto name = getFileName(l); updateLoadingProgress(name); - std::string audio_path = createTempAudioFile(l, temp_audio_files_); - sounds_.emplace_back(name, JA_LoadSound(audio_path.c_str())); + auto audio_data = loadAudioData(l); + JA_Sound_t* sound = nullptr; + if (!audio_data.data.empty()) { + sound = JA_LoadSound(audio_data.data.data(), audio_data.data.size()); + } else { + // Fallback a cargar desde disco si no está en pack + sound = JA_LoadSound(l.c_str()); + } + sounds_.emplace_back(name, sound); Logger::dots("Sound : ", name, "[ LOADED ]"); } } @@ -500,8 +497,15 @@ void Resource::loadMusics() { for (const auto& l : list) { auto name = getFileName(l); updateLoadingProgress(name); - std::string audio_path = createTempAudioFile(l, temp_audio_files_); - musics_.emplace_back(name, JA_LoadMusic(audio_path.c_str())); + auto audio_data = loadAudioData(l); + JA_Music_t* music = nullptr; + if (!audio_data.data.empty()) { + music = JA_LoadMusic(audio_data.data.data(), audio_data.data.size()); + } else { + // Fallback a cargar desde disco si no está en pack + music = JA_LoadMusic(l.c_str()); + } + musics_.emplace_back(name, music); Logger::dots("Music : ", name, "[ LOADED ]"); } } @@ -901,18 +905,3 @@ void Resource::updateLoadingProgress(std::string name) { void Resource::updateProgressBar() { loading_full_rect_.w = loading_wired_rect_.w * loading_count_.getPercentage(); } - -// Limpia archivos temporales de audio -void Resource::cleanupTempAudioFiles() { - for (const auto& temp_path : temp_audio_files_) { - try { - if (std::filesystem::exists(temp_path)) { - std::filesystem::remove(temp_path); - // SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Removed temp audio file: %s", temp_path.c_str()); - } - } catch (const std::exception& e) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to remove temp audio file %s: %s", temp_path.c_str(), e.what()); - } - } - temp_audio_files_.clear(); -} diff --git a/source/resource.hpp b/source/resource.hpp index 597b764..99b9920 100644 --- a/source/resource.hpp +++ b/source/resource.hpp @@ -146,9 +146,6 @@ class Resource { SDL_FRect loading_wired_rect_; SDL_FRect loading_full_rect_; - // --- Archivos temporales --- - std::vector temp_audio_files_; // Rutas de archivos temporales de audio para limpieza - // --- Métodos internos de carga y gestión --- void loadSounds(); // Carga los sonidos void loadMusics(); // Carga las músicas @@ -167,7 +164,6 @@ class Resource { void load(); // Carga todos los recursos void clearSounds(); // Vacía el vector de sonidos void clearMusics(); // Vacía el vector de músicas - void cleanupTempAudioFiles(); // Limpia archivos temporales de audio // --- Métodos para carga perezosa --- void initResourceLists(); // Inicializa las listas de recursos sin cargar el contenido