integrat jail_audio en la carrega de resources.pack

This commit is contained in:
2025-10-24 13:59:03 +02:00
parent fd4136a882
commit 6190b35349
2 changed files with 38 additions and 53 deletions

View File

@@ -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<std::string>& temp_files_tracker) -> std::string {
struct AudioData {
std::vector<uint8_t> 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<std::string>{}(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<const char*>(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();
}