Qt: Add accessors for UI thread view of current game

This commit is contained in:
Stenzek 2024-01-11 18:19:27 +10:00 committed by Connor McLaughlin
parent fa00069068
commit e908bbfae6
3 changed files with 51 additions and 32 deletions

View File

@ -90,6 +90,12 @@ static bool s_use_central_widget = false;
// UI thread VM validity. // UI thread VM validity.
static bool s_vm_valid = false; static bool s_vm_valid = false;
static bool s_vm_paused = false; static bool s_vm_paused = false;
static QString s_current_title;
static QString s_current_elf_override;
static QString s_current_disc_path;
static QString s_current_disc_serial;
static quint32 s_current_disc_crc;
static quint32 s_current_running_crc;
MainWindow::MainWindow() MainWindow::MainWindow()
{ {
@ -898,9 +904,9 @@ void MainWindow::updateWindowTitle()
{ {
QString suffix(QtHost::GetAppConfigSuffix()); QString suffix(QtHost::GetAppConfigSuffix());
QString main_title(QtHost::GetAppNameAndVersion() + suffix); QString main_title(QtHost::GetAppNameAndVersion() + suffix);
QString display_title(m_current_title + suffix); QString display_title(s_current_title + suffix);
if (!s_vm_valid || m_current_title.isEmpty()) if (!s_vm_valid || s_current_title.isEmpty())
display_title = main_title; display_title = main_title;
else if (isRenderingToMain()) else if (isRenderingToMain())
main_title = display_title; main_title = display_title;
@ -1108,7 +1114,7 @@ bool MainWindow::requestShutdown(bool allow_confirm, bool allow_save_to_state, b
return true; return true;
// If we don't have a crc, we can't save state. // If we don't have a crc, we can't save state.
allow_save_to_state &= (m_current_disc_crc != 0); allow_save_to_state &= (s_current_disc_crc != 0);
bool save_state = allow_save_to_state && default_save_to_state; bool save_state = allow_save_to_state && default_save_to_state;
VMLock lock(pauseAndLockVM()); VMLock lock(pauseAndLockVM());
@ -1407,13 +1413,13 @@ void MainWindow::onChangeDiscMenuAboutToHide()
void MainWindow::onLoadStateMenuAboutToShow() void MainWindow::onLoadStateMenuAboutToShow()
{ {
m_ui.menuLoadState->clear(); m_ui.menuLoadState->clear();
populateLoadStateMenu(m_ui.menuLoadState, m_current_disc_path, m_current_disc_serial, m_current_disc_crc); populateLoadStateMenu(m_ui.menuLoadState, s_current_disc_path, s_current_disc_serial, s_current_disc_crc);
} }
void MainWindow::onSaveStateMenuAboutToShow() void MainWindow::onSaveStateMenuAboutToShow()
{ {
m_ui.menuSaveState->clear(); m_ui.menuSaveState->clear();
populateSaveStateMenu(m_ui.menuSaveState, m_current_disc_serial, m_current_disc_crc); populateSaveStateMenu(m_ui.menuSaveState, s_current_disc_serial, s_current_disc_crc);
} }
void MainWindow::onStartFullscreenUITriggered() void MainWindow::onStartFullscreenUITriggered()
@ -1475,36 +1481,36 @@ void MainWindow::onViewGamePropertiesActionTriggered()
return; return;
// prefer to use a game list entry, if we have one, that way the summary is populated // prefer to use a game list entry, if we have one, that way the summary is populated
if (!m_current_disc_path.isEmpty() || !m_current_elf_override.isEmpty()) if (!s_current_disc_path.isEmpty() || !s_current_elf_override.isEmpty())
{ {
auto lock = GameList::GetLock(); auto lock = GameList::GetLock();
const QString& path = (m_current_elf_override.isEmpty() ? m_current_disc_path : m_current_elf_override); const QString& path = (s_current_elf_override.isEmpty() ? s_current_disc_path : s_current_elf_override);
const GameList::Entry* entry = GameList::GetEntryForPath(path.toUtf8().constData()); const GameList::Entry* entry = GameList::GetEntryForPath(path.toUtf8().constData());
if (entry) if (entry)
{ {
SettingsWindow::openGamePropertiesDialog( SettingsWindow::openGamePropertiesDialog(
entry, entry->title, m_current_elf_override.isEmpty() ? entry->serial : std::string(), entry->crc); entry, entry->title, s_current_elf_override.isEmpty() ? entry->serial : std::string(), entry->crc);
return; return;
} }
} }
// open properties for the current running file (isn't in the game list) // open properties for the current running file (isn't in the game list)
if (m_current_disc_crc == 0) if (s_current_disc_crc == 0)
{ {
QMessageBox::critical(this, tr("Game Properties"), tr("Game properties is unavailable for the current game.")); QMessageBox::critical(this, tr("Game Properties"), tr("Game properties is unavailable for the current game."));
return; return;
} }
// can't use serial for ELFs, because they might have a disc set // can't use serial for ELFs, because they might have a disc set
if (m_current_elf_override.isEmpty()) if (s_current_elf_override.isEmpty())
{ {
SettingsWindow::openGamePropertiesDialog( SettingsWindow::openGamePropertiesDialog(
nullptr, m_current_title.toStdString(), m_current_disc_serial.toStdString(), m_current_disc_crc); nullptr, s_current_title.toStdString(), s_current_disc_serial.toStdString(), s_current_disc_crc);
} }
else else
{ {
SettingsWindow::openGamePropertiesDialog( SettingsWindow::openGamePropertiesDialog(
nullptr, m_current_title.toStdString(), std::string(), m_current_disc_crc); nullptr, s_current_title.toStdString(), std::string(), s_current_disc_crc);
} }
} }
@ -1606,10 +1612,10 @@ void MainWindow::onToolsCoverDownloaderTriggered()
void MainWindow::onToolsEditCheatsPatchesTriggered(bool cheats) void MainWindow::onToolsEditCheatsPatchesTriggered(bool cheats)
{ {
if (m_current_disc_serial.isEmpty() || m_current_running_crc == 0) if (s_current_disc_serial.isEmpty() || s_current_running_crc == 0)
return; return;
const std::string path = Patch::GetPnachFilename(m_current_disc_serial.toStdString(), m_current_running_crc, cheats); const std::string path = Patch::GetPnachFilename(s_current_disc_serial.toStdString(), s_current_running_crc, cheats);
if (!FileSystem::FileExists(path.c_str())) if (!FileSystem::FileExists(path.c_str()))
{ {
if (QMessageBox::question(this, tr("Confirm File Creation"), if (QMessageBox::question(this, tr("Confirm File Creation"),
@ -1876,12 +1882,12 @@ void MainWindow::onVMStopped()
void MainWindow::onGameChanged(const QString& title, const QString& elf_override, const QString& disc_path, void MainWindow::onGameChanged(const QString& title, const QString& elf_override, const QString& disc_path,
const QString& serial, quint32 disc_crc, quint32 crc) const QString& serial, quint32 disc_crc, quint32 crc)
{ {
m_current_title = title; s_current_title = title;
m_current_elf_override = elf_override; s_current_elf_override = elf_override;
m_current_disc_path = disc_path; s_current_disc_path = disc_path;
m_current_disc_serial = serial; s_current_disc_serial = serial;
m_current_disc_crc = disc_crc; s_current_disc_crc = disc_crc;
m_current_running_crc = crc; s_current_running_crc = crc;
updateWindowTitle(); updateWindowTitle();
updateGameDependentActions(); updateGameDependentActions();
} }
@ -2645,8 +2651,8 @@ void MainWindow::loadSaveStateFile(const QString& filename, const QString& state
{ {
if (s_vm_valid) if (s_vm_valid)
{ {
if (!filename.isEmpty() && m_current_disc_path != filename) if (!filename.isEmpty() && s_current_disc_path != filename)
g_emu_thread->changeDisc(CDVD_SourceType::Iso, m_current_disc_path); g_emu_thread->changeDisc(CDVD_SourceType::Iso, s_current_disc_path);
g_emu_thread->loadState(state_filename); g_emu_thread->loadState(state_filename);
} }
else else
@ -2771,13 +2777,13 @@ void MainWindow::populateSaveStateMenu(QMenu* menu, const QString& serial, quint
void MainWindow::updateGameDependentActions() void MainWindow::updateGameDependentActions()
{ {
const bool valid_serial_and_crc = (s_vm_valid && !m_current_disc_serial.isEmpty() && m_current_disc_crc != 0); const bool valid_serial_and_crc = (s_vm_valid && !s_current_disc_serial.isEmpty() && s_current_disc_crc != 0);
m_ui.menuLoadState->setEnabled(valid_serial_and_crc); m_ui.menuLoadState->setEnabled(valid_serial_and_crc);
m_ui.actionToolbarLoadState->setEnabled(valid_serial_and_crc); m_ui.actionToolbarLoadState->setEnabled(valid_serial_and_crc);
m_ui.menuSaveState->setEnabled(valid_serial_and_crc); m_ui.menuSaveState->setEnabled(valid_serial_and_crc);
m_ui.actionToolbarSaveState->setEnabled(valid_serial_and_crc); m_ui.actionToolbarSaveState->setEnabled(valid_serial_and_crc);
const bool can_use_pnach = (s_vm_valid && !m_current_disc_serial.isEmpty() && m_current_running_crc != 0); const bool can_use_pnach = (s_vm_valid && !s_current_disc_serial.isEmpty() && s_current_running_crc != 0);
m_ui.actionEditCheats->setEnabled(can_use_pnach); m_ui.actionEditCheats->setEnabled(can_use_pnach);
m_ui.actionEditPatches->setEnabled(can_use_pnach); m_ui.actionEditPatches->setEnabled(can_use_pnach);
m_ui.actionReloadPatches->setEnabled(s_vm_valid); m_ui.actionReloadPatches->setEnabled(s_vm_valid);
@ -2840,7 +2846,7 @@ void MainWindow::doDiscChange(CDVD_SourceType source, const QString& path)
if (reset_system) if (reset_system)
{ {
// Clearing ELF override will reset the system. // Clearing ELF override will reset the system.
if (!m_current_elf_override.isEmpty()) if (!s_current_elf_override.isEmpty())
g_emu_thread->setELFOverride(QString()); g_emu_thread->setELFOverride(QString());
else else
g_emu_thread->resetVM(); g_emu_thread->resetVM();
@ -2959,3 +2965,18 @@ bool QtHost::IsVMPaused()
{ {
return s_vm_paused; return s_vm_paused;
} }
const QString& QtHost::GetCurrentGameTitle()
{
return s_current_title;
}
const QString& QtHost::GetCurrentGameSerial()
{
return s_current_disc_serial;
}
const QString& QtHost::GetCurrentGamePath()
{
return s_current_disc_path;
}

View File

@ -293,13 +293,6 @@ private:
QMenu* m_settings_toolbar_menu = nullptr; QMenu* m_settings_toolbar_menu = nullptr;
QString m_current_title;
QString m_current_elf_override;
QString m_current_disc_path;
QString m_current_disc_serial;
quint32 m_current_disc_crc;
quint32 m_current_running_crc;
bool m_display_created = false; bool m_display_created = false;
bool m_relative_mouse_mode = false; bool m_relative_mouse_mode = false;
bool m_hide_mouse_cursor = false; bool m_hide_mouse_cursor = false;

View File

@ -283,6 +283,11 @@ namespace QtHost
bool IsVMValid(); bool IsVMValid();
bool IsVMPaused(); bool IsVMPaused();
/// Accessors for game information.
const QString& GetCurrentGameTitle();
const QString& GetCurrentGameSerial();
const QString& GetCurrentGamePath();
/// Compare strings in the locale of the current UI language /// Compare strings in the locale of the current UI language
int LocaleSensitiveCompare(QStringView lhs, QStringView rhs); int LocaleSensitiveCompare(QStringView lhs, QStringView rhs);
} // namespace QtHost } // namespace QtHost