neteja de temporals al acabar
This commit is contained in:
@@ -5,9 +5,10 @@
|
|||||||
#include <algorithm> // Para find_if, max, find
|
#include <algorithm> // Para find_if, max, find
|
||||||
#include <array> // Para array
|
#include <array> // Para array
|
||||||
#include <cstdlib> // Para exit, getenv
|
#include <cstdlib> // Para exit, getenv
|
||||||
|
#include <filesystem> // Para filesystem::remove, filesystem::exists
|
||||||
|
#include <fstream> // Para ofstream
|
||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
#include <utility> // Para move
|
#include <utility> // Para move
|
||||||
#include <fstream> // Para ofstream
|
|
||||||
|
|
||||||
#include "asset.h" // Para Asset
|
#include "asset.h" // Para Asset
|
||||||
#include "color.h" // Para Color
|
#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
|
// Helper para cargar archivos de audio desde pack o filesystem
|
||||||
namespace {
|
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);
|
auto resource_data = ResourceHelper::loadFile(file_path);
|
||||||
if (!resource_data.empty()) {
|
if (!resource_data.empty()) {
|
||||||
// Crear archivo temporal
|
// Crear archivo temporal
|
||||||
@@ -43,6 +44,10 @@ namespace {
|
|||||||
}
|
}
|
||||||
temp_file.write(reinterpret_cast<const char*>(resource_data.data()), resource_data.size());
|
temp_file.write(reinterpret_cast<const char*>(resource_data.data()), resource_data.size());
|
||||||
temp_file.close();
|
temp_file.close();
|
||||||
|
|
||||||
|
// Agregar a la lista de archivos temporales para limpieza posterior
|
||||||
|
temp_files_tracker.push_back(temp_path);
|
||||||
|
|
||||||
return temp_path;
|
return temp_path;
|
||||||
}
|
}
|
||||||
return file_path; // Usar ruta original si no está en pack
|
return file_path; // Usar ruta original si no está en pack
|
||||||
@@ -83,6 +88,7 @@ Resource::Resource(LoadingMode mode)
|
|||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
Resource::~Resource() {
|
Resource::~Resource() {
|
||||||
|
cleanupTempAudioFiles();
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,7 +327,7 @@ auto Resource::loadSoundLazy(const std::string &name) -> JA_Sound_t * {
|
|||||||
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
|
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
|
||||||
for (const auto &file : sound_list) {
|
for (const auto &file : sound_list) {
|
||||||
if (getFileName(file) == name) {
|
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());
|
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);
|
auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
|
||||||
for (const auto &file : music_list) {
|
for (const auto &file : music_list) {
|
||||||
if (getFileName(file) == name) {
|
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());
|
return JA_LoadMusic(audio_path.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -478,7 +484,7 @@ void Resource::loadSounds() {
|
|||||||
auto name = getFileName(l);
|
auto name = getFileName(l);
|
||||||
updateLoadingProgress(name);
|
updateLoadingProgress(name);
|
||||||
#ifndef NO_AUDIO
|
#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()));
|
sounds_.emplace_back(name, JA_LoadSound(audio_path.c_str()));
|
||||||
#else
|
#else
|
||||||
sounds_.emplace_back(name, nullptr);
|
sounds_.emplace_back(name, nullptr);
|
||||||
@@ -497,7 +503,7 @@ void Resource::loadMusics() {
|
|||||||
auto name = getFileName(l);
|
auto name = getFileName(l);
|
||||||
updateLoadingProgress(name);
|
updateLoadingProgress(name);
|
||||||
#ifndef NO_AUDIO
|
#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()));
|
musics_.emplace_back(name, JA_LoadMusic(audio_path.c_str()));
|
||||||
#else
|
#else
|
||||||
musics_.emplace_back(name, nullptr);
|
musics_.emplace_back(name, nullptr);
|
||||||
@@ -865,3 +871,18 @@ void Resource::updateLoadingProgress(std::string name) {
|
|||||||
void Resource::updateProgressBar() {
|
void Resource::updateProgressBar() {
|
||||||
loading_full_rect_.w = loading_wired_rect_.w * loading_count_.getPercentage();
|
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();
|
||||||
|
}
|
||||||
|
|||||||
@@ -128,6 +128,9 @@ class Resource {
|
|||||||
std::string loading_resource_name_; // Nombre del recurso que se está cargando
|
std::string loading_resource_name_; // Nombre del recurso que se está cargando
|
||||||
SDL_FRect loading_wired_rect_;
|
SDL_FRect loading_wired_rect_;
|
||||||
SDL_FRect loading_full_rect_;
|
SDL_FRect loading_full_rect_;
|
||||||
|
|
||||||
|
// --- Archivos temporales ---
|
||||||
|
std::vector<std::string> temp_audio_files_; // Rutas de archivos temporales de audio para limpieza
|
||||||
|
|
||||||
// --- Métodos internos de carga y gestión ---
|
// --- Métodos internos de carga y gestión ---
|
||||||
void loadSounds(); // Carga los sonidos
|
void loadSounds(); // Carga los sonidos
|
||||||
@@ -147,6 +150,7 @@ class Resource {
|
|||||||
void load(); // Carga todos los recursos
|
void load(); // Carga todos los recursos
|
||||||
void clearSounds(); // Vacía el vector de sonidos
|
void clearSounds(); // Vacía el vector de sonidos
|
||||||
void clearMusics(); // Vacía el vector de músicas
|
void clearMusics(); // Vacía el vector de músicas
|
||||||
|
void cleanupTempAudioFiles(); // Limpia archivos temporales de audio
|
||||||
|
|
||||||
// --- Métodos para carga perezosa ---
|
// --- Métodos para carga perezosa ---
|
||||||
void initResourceLists(); // Inicializa las listas de recursos sin cargar el contenido
|
void initResourceLists(); // Inicializa las listas de recursos sin cargar el contenido
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <fstream> // Para basic_ifstream, basic_istream, basic_ostream, operator<<, endl, ifstream
|
#include <fstream> // Para basic_ifstream, basic_istream, basic_ostream, operator<<, endl, ifstream
|
||||||
#include <iostream> // Para cerr
|
#include <iostream> // Para cerr
|
||||||
|
#include <sstream> // Para istringstream
|
||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
#include <string_view> // Para string_view
|
#include <string_view> // Para string_view
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user