neteja de temporals al acabar

This commit is contained in:
2025-08-19 16:29:52 +02:00
parent 7b193605e6
commit 5aca95f3d2
3 changed files with 32 additions and 6 deletions

View File

@@ -5,9 +5,10 @@
#include <algorithm> // Para find_if, max, find
#include <array> // Para array
#include <cstdlib> // Para exit, getenv
#include <filesystem> // Para filesystem::remove, filesystem::exists
#include <fstream> // Para ofstream
#include <stdexcept> // Para runtime_error
#include <utility> // Para move
#include <fstream> // Para ofstream
#include "asset.h" // Para Asset
#include "color.h" // Para Color
@@ -25,7 +26,7 @@ struct JA_Sound_t; // lines 12-12
// Helper para cargar archivos de audio desde pack o filesystem
namespace {
std::string createTempAudioFile(const std::string& file_path) {
std::string createTempAudioFile(const std::string& file_path, std::vector<std::string>& temp_files_tracker) {
auto resource_data = ResourceHelper::loadFile(file_path);
if (!resource_data.empty()) {
// Crear archivo temporal
@@ -43,6 +44,10 @@ namespace {
}
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
@@ -83,6 +88,7 @@ Resource::Resource(LoadingMode mode)
// Destructor
Resource::~Resource() {
cleanupTempAudioFiles();
clear();
}
@@ -321,7 +327,7 @@ 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);
std::string audio_path = createTempAudioFile(file, Resource::get()->temp_audio_files_);
return JA_LoadSound(audio_path.c_str());
}
}
@@ -335,7 +341,7 @@ 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);
std::string audio_path = createTempAudioFile(file, Resource::get()->temp_audio_files_);
return JA_LoadMusic(audio_path.c_str());
}
}
@@ -478,7 +484,7 @@ void Resource::loadSounds() {
auto name = getFileName(l);
updateLoadingProgress(name);
#ifndef NO_AUDIO
std::string audio_path = createTempAudioFile(l);
std::string audio_path = createTempAudioFile(l, temp_audio_files_);
sounds_.emplace_back(name, JA_LoadSound(audio_path.c_str()));
#else
sounds_.emplace_back(name, nullptr);
@@ -497,7 +503,7 @@ void Resource::loadMusics() {
auto name = getFileName(l);
updateLoadingProgress(name);
#ifndef NO_AUDIO
std::string audio_path = createTempAudioFile(l);
std::string audio_path = createTempAudioFile(l, temp_audio_files_);
musics_.emplace_back(name, JA_LoadMusic(audio_path.c_str()));
#else
musics_.emplace_back(name, nullptr);
@@ -865,3 +871,18 @@ 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();
}