Skip to content

Commit

Permalink
strip paths from items in m3u when populating cd menu (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored May 15, 2021
1 parent 8ee51c7 commit 8be43b4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
51 changes: 27 additions & 24 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ bool Application::init(const char* title, int width, int height)
lastHardcore = hardcore();
cancelLoad = false;
updateMenu();
updateCDMenu(NULL, 0, true);
updateDiscMenu(true);
return true;

error:
Expand Down Expand Up @@ -1288,7 +1288,8 @@ bool Application::loadGame(const std::string& path)

if (!romLoaded(&_logger, _system, path, data, size))
{
updateCDMenu(NULL, 0, true);
_discPaths.clear();
updateDiscMenu(true);

_gameFileName.clear();

Expand Down Expand Up @@ -1316,13 +1317,13 @@ bool Application::loadGame(const std::string& path)

if (_core.getNumDiscs() == 0)
{
updateCDMenu(NULL, 0, true);
_discPaths.clear();
updateDiscMenu(true);
}
else
{
char names[10][128];
int count = cdrom_get_cd_names(path.c_str(), names, sizeof(names) / sizeof(names[0]), &_logger);
updateCDMenu(names, count, true);
cdrom_get_cd_names(path.c_str(), &_discPaths, &_logger);
updateDiscMenu(true);
}

_gamePath = path;
Expand Down Expand Up @@ -1654,7 +1655,7 @@ void Application::enableRecent()
}
}

void Application::updateCDMenu(const char names[][128], int count, bool updateLabels)
void Application::updateDiscMenu(bool updateLabels)
{
size_t i = 0;
size_t coreDiscCount = _core.getNumDiscs();
Expand Down Expand Up @@ -1701,9 +1702,17 @@ void Application::updateCDMenu(const char names[][128], int count, bool updateLa

if (updateLabels)
{
if ((int)i < count)
if ((int)i < _discPaths.size())
{
info.dwTypeData = (char*)names[i];
const std::string& path = _discPaths.at(i);
size_t index = path.find_last_of('\\');
if (index == std::string::npos)
index = path.find_last_of('/');

if (index != std::string::npos)
info.dwTypeData = (LPSTR)&path.at(index + 1);
else
info.dwTypeData = (LPSTR)&path.at(0);
}
else
{
Expand Down Expand Up @@ -1793,7 +1802,7 @@ void Application::loadState(const std::string& path)
{
if (_states.loadState(path))
{
updateCDMenu(NULL, 0, false);
updateDiscMenu(false);
}
}

Expand All @@ -1806,7 +1815,7 @@ void Application::loadState(unsigned ndx)

if (_states.loadState(ndx))
{
updateCDMenu(NULL, 0, false);
updateDiscMenu(false);
}
}

Expand Down Expand Up @@ -2184,7 +2193,7 @@ void Application::handle(const SDL_SysWMEvent* syswm)

case IDM_CD_OPEN_TRAY:
_core.setTrayOpen(!_core.getTrayOpen());
updateCDMenu(NULL, 0, false);
updateDiscMenu(false);
break;

case IDM_PAUSE_GAME:
Expand Down Expand Up @@ -2299,19 +2308,13 @@ void Application::handle(const SDL_SysWMEvent* syswm)
{
_core.setCurrentDiscIndex(newDiscIndex);

char buffer[128];
MENUITEMINFO info;
memset(&info, 0, sizeof(info));
info.cbSize = sizeof(info);
info.fMask = MIIM_TYPE | MIIM_DATA;
info.dwTypeData = buffer;
info.cch = sizeof(buffer);
GetMenuItemInfo(_menu, cmd, false, &info);

std::string path = util::replaceFileName(_gamePath, buffer);
romLoaded(&_logger, _system, path, NULL, 0);
if (newDiscIndex < _discPaths.size())
{
std::string path = util::replaceFileName(_gamePath, _discPaths.at(newDiscIndex).c_str());
romLoaded(&_logger, _system, path, NULL, 0);
}

updateCDMenu(NULL, 0, false);
updateDiscMenu(false);
}
}
else if (cmd >= IDM_SYSTEM_FIRST && cmd <= IDM_SYSTEM_LAST)
Expand Down
3 changes: 2 additions & 1 deletion src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Application
void enableItems(const UINT* items, size_t count, UINT enable);
void enableSlots();
void enableRecent();
void updateCDMenu(const char names[][128], int count, bool updateLabels);
void updateDiscMenu(bool updateLabels);
std::string getStatePath(unsigned ndx);
std::string getConfigPath();
std::string getCoreConfigPath(const std::string& coreName);
Expand Down Expand Up @@ -142,6 +142,7 @@ class Application

KeyBinds _keybinds;
std::vector<RecentItem> _recentList;
std::vector<std::string> _discPaths;

Allocator<256 * 1024> _allocator;

Expand Down
18 changes: 8 additions & 10 deletions src/CdRom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ along with RALibretro. If not, see <http://www.gnu.org/licenses/>.

#include <string.h>

static int cdrom_get_cd_names_m3u(const char* filename, char names[][128], int max_names, Logger* logger)
static int cdrom_get_cd_names_m3u(const char* filename, std::vector<std::string>* disc_paths, Logger* logger)
{
int count = 0;
char buffer[2048], *ptr, *start;
Expand All @@ -46,13 +46,9 @@ static int cdrom_get_cd_names_m3u(const char* filename, char names[][128], int m

if (ptr > start)
{
char* name = names[count];
memcpy(name, start, ptr - start);
name[ptr - start] = '\0';
std::string path(start, ptr - start);
disc_paths->push_back(path);
++count;

if (count == max_names)
break;
}

if (!*ptr)
Expand All @@ -65,13 +61,15 @@ static int cdrom_get_cd_names_m3u(const char* filename, char names[][128], int m
return count;
}

int cdrom_get_cd_names(const char* filename, char names[][128], int max_names, Logger* logger)
int cdrom_get_cd_names(const char* filename, std::vector<std::string>* disc_paths, Logger* logger)
{
disc_paths->clear();

int len = strlen(filename);
if (len > 4 && strcasecmp(&filename[len - 4], ".m3u") == 0)
return cdrom_get_cd_names_m3u(filename, names, max_names, logger);
return cdrom_get_cd_names_m3u(filename, disc_paths, logger);

std::string filename_path = util::fileNameWithExtension(filename);
strcpy(names[0], filename_path.c_str());
disc_paths->push_back(std::move(filename_path));
return 1;
}
4 changes: 2 additions & 2 deletions src/CdRom.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ along with RALibretro. If not, see <http://www.gnu.org/licenses/>.

#include "components/Logger.h"

#include <stdio.h>
#include <vector>

int cdrom_get_cd_names(const char* filename, char names[][128], int max_names, Logger* logger);
int cdrom_get_cd_names(const char* filename, std::vector<std::string>* disc_paths, Logger* logger);

0 comments on commit 8be43b4

Please sign in to comment.