Skip to content

Commit 6884a5a

Browse files
committed
QT: Add entries from .m3u files to change disc menu
1 parent 07ee0bd commit 6884a5a

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

pcsx2-qt/MainWindow.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
#include <QtCore/QDateTime>
5151
#include <QtCore/QDir>
52+
#include <QtCore/QFileInfo>
5253
#include <QtGui/QCloseEvent>
5354
#include <QtWidgets/QFileDialog>
5455
#include <QtWidgets/QInputDialog>
@@ -1687,11 +1688,37 @@ void MainWindow::onRemoveDiscActionTriggered()
16871688

16881689
void MainWindow::onChangeDiscMenuAboutToShow()
16891690
{
1690-
// TODO: This is where we would populate the playlist if there is one.
1691+
const std::vector<std::string>& playlist = VMManager::GetM3UPlaylistEntries();
1692+
if (playlist.empty())
1693+
return;
1694+
1695+
m_change_disc_playlist_actions.clear();
1696+
m_change_disc_playlist_actions.append(m_ui.menuChangeDisc->addSeparator());
1697+
1698+
const int active_index = VMManager::GetM3UPlaylistCurrentIndex();
1699+
for (int i = 0; i < static_cast<int>(playlist.size()); ++i)
1700+
{
1701+
const QString label = tr("%1: %2").arg(i + 1).arg(QFileInfo(QString::fromStdString(playlist[i])).fileName());
1702+
QAction* action = m_ui.menuChangeDisc->addAction(label);
1703+
action->setCheckable(true);
1704+
action->setChecked(i == active_index);
1705+
action->setToolTip(QString::fromStdString(playlist[i]));
1706+
const std::string target_path = playlist[i];
1707+
connect(action, &QAction::triggered, this, [target_path]() {
1708+
g_emu_thread->changeDisc(CDVD_SourceType::Iso, QString::fromStdString(target_path));
1709+
});
1710+
m_change_disc_playlist_actions.append(action);
1711+
}
16911712
}
16921713

16931714
void MainWindow::onChangeDiscMenuAboutToHide()
16941715
{
1716+
for (QAction* action : m_change_disc_playlist_actions)
1717+
{
1718+
m_ui.menuChangeDisc->removeAction(action);
1719+
action->deleteLater();
1720+
}
1721+
m_change_disc_playlist_actions.clear();
16951722
}
16961723

16971724
void MainWindow::onLoadStateMenuAboutToShow()

pcsx2-qt/MainWindow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ private Q_SLOTS:
306306
InputRecordingViewer* m_input_recording_viewer = nullptr;
307307
AutoUpdaterDialog* m_auto_updater_dialog = nullptr;
308308

309+
QList<QAction*> m_change_disc_playlist_actions;
310+
309311
QProgressBar* m_status_progress_widget = nullptr;
310312
QLabel* m_status_verbose_widget = nullptr;
311313
QLabel* m_status_renderer_widget = nullptr;

pcsx2/VMManager.cpp

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,37 @@ bool VMManager::HasBootedELF()
12361236
return s_current_crc != 0 && s_elf_executed;
12371237
}
12381238

1239+
static std::string s_current_m3u_playlist_source;
1240+
static std::vector<std::string> s_current_m3u_playlist_entries;
1241+
static int s_current_m3u_playlist_index = -1;
1242+
1243+
static void ClearM3UPlaylist()
1244+
{
1245+
s_current_m3u_playlist_source.clear();
1246+
s_current_m3u_playlist_entries.clear();
1247+
s_current_m3u_playlist_index = -1;
1248+
}
1249+
1250+
static void SetM3UPlaylist(const std::string& m3u_path, std::vector<std::string> entries, int current_index)
1251+
{
1252+
s_current_m3u_playlist_source = m3u_path;
1253+
s_current_m3u_playlist_entries = std::move(entries);
1254+
s_current_m3u_playlist_index = current_index;
1255+
}
1256+
1257+
static void UpdateM3UPlaylistCurrentIndex(const std::string& current_disc_path)
1258+
{
1259+
s_current_m3u_playlist_index = -1;
1260+
for (size_t i = 0; i < s_current_m3u_playlist_entries.size(); ++i)
1261+
{
1262+
if (s_current_m3u_playlist_entries[i] == current_disc_path)
1263+
{
1264+
s_current_m3u_playlist_index = static_cast<int>(i);
1265+
return;
1266+
}
1267+
}
1268+
}
1269+
12391270
static std::vector<std::string> ParseM3UPlaylist(const std::string& m3u_path)
12401271
{
12411272
std::vector<std::string> disc_paths;
@@ -1273,14 +1304,15 @@ static std::vector<std::string> ParseM3UPlaylist(const std::string& m3u_path)
12731304

12741305
bool VMManager::AutoDetectSource(const std::string& filename, Error* error)
12751306
{
1307+
ClearM3UPlaylist();
12761308
if (!filename.empty())
12771309
{
12781310
if (!FileSystem::FileExists(filename.c_str()))
12791311
{
12801312
Error::SetStringFmt(error, TRANSLATE_FS("VMManager", "Requested filename '{}' does not exist."), filename);
12811313
return false;
12821314
}
1283-
1315+
12841316
if (IsGSDumpFileName(filename))
12851317
{
12861318
CDVDsys_ChangeSource(CDVD_SourceType::NoDisc);
@@ -1313,8 +1345,8 @@ bool VMManager::AutoDetectSource(const std::string& filename, Error* error)
13131345
return false;
13141346
}
13151347

1316-
// For now, just load the first disc
1317-
CDVDsys_SetFile(CDVD_SourceType::Iso, disc_paths[0]);
1348+
SetM3UPlaylist(filename, disc_paths, 0);
1349+
CDVDsys_SetFile(CDVD_SourceType::Iso, s_current_m3u_playlist_entries[0]);
13181350
CDVDsys_ChangeSource(CDVD_SourceType::Iso);
13191351
return true;
13201352
}
@@ -2367,6 +2399,28 @@ bool VMManager::ChangeDisc(CDVD_SourceType source, std::string path)
23672399
const CDVD_SourceType old_type = CDVDsys_GetSourceType();
23682400
const std::string old_path(CDVDsys_GetFile(old_type));
23692401

2402+
if (source == CDVD_SourceType::Iso && StringUtil::EndsWithNoCase(path, ".m3u"))
2403+
{
2404+
const std::vector<std::string> disc_paths = ParseM3UPlaylist(path);
2405+
if (disc_paths.empty())
2406+
{
2407+
return false;
2408+
}
2409+
2410+
SetM3UPlaylist(path, disc_paths, 0);
2411+
path = s_current_m3u_playlist_entries[0];
2412+
}
2413+
else if (source != CDVD_SourceType::Iso)
2414+
{
2415+
ClearM3UPlaylist();
2416+
}
2417+
else if (!path.empty())
2418+
{
2419+
UpdateM3UPlaylistCurrentIndex(path);
2420+
if (s_current_m3u_playlist_index < 0)
2421+
ClearM3UPlaylist();
2422+
}
2423+
23702424
CDVDsys_ChangeSource(source);
23712425
if (!path.empty())
23722426
CDVDsys_SetFile(source, path);
@@ -2421,6 +2475,16 @@ bool VMManager::ChangeDisc(CDVD_SourceType source, std::string path)
24212475
return result;
24222476
}
24232477

2478+
const std::vector<std::string>& VMManager::GetM3UPlaylistEntries()
2479+
{
2480+
return s_current_m3u_playlist_entries;
2481+
}
2482+
2483+
int VMManager::GetM3UPlaylistCurrentIndex()
2484+
{
2485+
return s_current_m3u_playlist_index;
2486+
}
2487+
24242488
bool VMManager::SetELFOverride(std::string path)
24252489
{
24262490
if (!HasValidVM() || (!path.empty() && !FileSystem::FileExists(path.c_str())))

pcsx2/VMManager.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ namespace VMManager
8989
/// Returns the path of the disc currently running.
9090
std::string GetDiscPath();
9191

92+
/// Returns the list of disc paths from the currently loaded M3U playlist.
93+
const std::vector<std::string>& GetM3UPlaylistEntries();
94+
95+
/// Returns the current selected disc index for the loaded M3U playlist, or -1 if none.
96+
int GetM3UPlaylistCurrentIndex();
97+
9298
/// Returns the serial of the disc currently running.
9399
std::string GetDiscSerial();
94100

0 commit comments

Comments
 (0)