integrat jail_audio en la carrega de resources.pack
This commit is contained in:
@@ -26,33 +26,16 @@
|
|||||||
struct JA_Music_t; // lines 11-11
|
struct JA_Music_t; // lines 11-11
|
||||||
struct JA_Sound_t; // lines 12-12
|
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 {
|
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);
|
auto resource_data = ResourceHelper::loadFile(file_path);
|
||||||
if (!resource_data.empty()) {
|
return AudioData{std::move(resource_data), file_path};
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
@@ -92,7 +75,6 @@ Resource::Resource(LoadingMode mode)
|
|||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
Resource::~Resource() {
|
Resource::~Resource() {
|
||||||
cleanupTempAudioFiles();
|
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,8 +317,12 @@ 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, Resource::get()->temp_audio_files_);
|
auto audio_data = loadAudioData(file);
|
||||||
return JA_LoadSound(audio_path.c_str());
|
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;
|
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);
|
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, Resource::get()->temp_audio_files_);
|
auto audio_data = loadAudioData(file);
|
||||||
return JA_LoadMusic(audio_path.c_str());
|
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;
|
return nullptr;
|
||||||
@@ -484,8 +474,15 @@ void Resource::loadSounds() {
|
|||||||
for (const auto& l : list) {
|
for (const auto& l : list) {
|
||||||
auto name = getFileName(l);
|
auto name = getFileName(l);
|
||||||
updateLoadingProgress(name);
|
updateLoadingProgress(name);
|
||||||
std::string audio_path = createTempAudioFile(l, temp_audio_files_);
|
auto audio_data = loadAudioData(l);
|
||||||
sounds_.emplace_back(name, JA_LoadSound(audio_path.c_str()));
|
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 ]");
|
Logger::dots("Sound : ", name, "[ LOADED ]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -500,8 +497,15 @@ void Resource::loadMusics() {
|
|||||||
for (const auto& l : list) {
|
for (const auto& l : list) {
|
||||||
auto name = getFileName(l);
|
auto name = getFileName(l);
|
||||||
updateLoadingProgress(name);
|
updateLoadingProgress(name);
|
||||||
std::string audio_path = createTempAudioFile(l, temp_audio_files_);
|
auto audio_data = loadAudioData(l);
|
||||||
musics_.emplace_back(name, JA_LoadMusic(audio_path.c_str()));
|
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 ]");
|
Logger::dots("Music : ", name, "[ LOADED ]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -901,18 +905,3 @@ 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();
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -146,9 +146,6 @@ class Resource {
|
|||||||
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
|
||||||
void loadMusics(); // Carga las músicas
|
void loadMusics(); // Carga las músicas
|
||||||
@@ -167,7 +164,6 @@ 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
|
||||||
|
|||||||
Reference in New Issue
Block a user