Skip to content

Commit

Permalink
add m3u support
Browse files Browse the repository at this point in the history
  • Loading branch information
i30817 committed May 21, 2023
1 parent 0b690a2 commit ebcae54
Showing 1 changed file with 63 additions and 4 deletions.
67 changes: 63 additions & 4 deletions libretro/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <libretro.h>
#include <string>
#include <thread>
#include <fstream>

#include "options.h"

Expand Down Expand Up @@ -358,11 +359,60 @@ void retro_get_system_info(retro_system_info* info)
#endif

info->library_name = "pcsx2";
info->valid_extensions = "elf|iso|ciso|cue|bin|gz";
info->valid_extensions = "elf|iso|ciso|cue|bin|gz|m3u";
info->need_fullpath = true;
info->block_extract = true;
}


static std::vector<std::string>
read_m3u_file(const std::string& m3u_file)
{
log_cb(RETRO_LOG_DEBUG, "Reading M3U file");
std::vector<std::string> result;
std::ifstream m3u_data(m3u_file);
if (!m3u_data.is_open())
{
log_cb(RETRO_LOG_ERROR, "M3U file \"%s\" cannot be read", m3u_file.c_str());
return result;
}
// This is the UTF-8 representation of U+FEFF.
const std::string utf8_bom = "\xEF\xBB\xBF";
std::string line;
getline(m3u_data, line);
if (line.rfind(utf8_bom,0) == 0)
{
log_cb(RETRO_LOG_WARN, "M3U file \"%s\" contains UTF-8 BOM", m3u_file.c_str());
line.erase(0, utf8_bom.length());
}
std::string m3u_parent = Path::Canonicalize(Path::GetDirectory(m3u_file));
for (line; !m3u_data.eof(); getline(m3u_data, line))
{
if (!line.rfind('#', 0) == 0) // Comments start with #
{
std::string m3u_path = line;
if (!Path::IsAbsolute(line))
{
m3u_path = Path::Combine(m3u_parent, line);
}
std::ifstream discFile(m3u_path);
if (discFile.is_open())
{
log_cb(RETRO_LOG_DEBUG, "Found disc image in M3U file, %s", m3u_path.c_str());
result.push_back(m3u_path);
}
else
{
log_cb(RETRO_LOG_WARN, "File specified in the M3U file \"%s\" was not found", m3u_path.c_str());
}
}
}
if (result.empty())
log_cb(RETRO_LOG_ERROR, "No paths found in the M3U file \"%s\"", m3u_file.c_str());

return result;
}

void retro_get_system_av_info(retro_system_av_info* info)
{
if (Options::renderer == "Software" || Options::renderer == "Null")
Expand Down Expand Up @@ -713,13 +763,22 @@ bool retro_load_game(const struct retro_game_info* game)

VMManager::ApplySettings();

image_index = 0;
disk_images.clear();
VMBootParameters boot_params;
if(game && game->path)
{
disk_images.push_back(game->path);
boot_params.filename = game->path;
std::vector<std::string> game_paths;
if (Path::GetExtension(game->path) == "m3u")
{
game_paths = read_m3u_file(game->path);
log_cb(RETRO_LOG_DEBUG, "Found %u game images in M3U file.", game_paths.size());
}
else
{
game_paths.push_back(game->path);
}
disk_images.assign(game_paths.begin(), game_paths.end());
boot_params.filename = disk_images[get_image_index()];
}

cpu_thread = std::thread(cpu_thread_entry, boot_params);
Expand Down

0 comments on commit ebcae54

Please sign in to comment.