From 2ae78f6e2fa3001ed9a04589753a8a596fbeb63f Mon Sep 17 00:00:00 2001 From: Stenzek Date: Wed, 28 Dec 2022 23:38:09 +1000 Subject: [PATCH] Misc: Clean up the last places exceptions were used --- pcsx2/CDVD/CDVDdiscReader.cpp | 39 +++++------------- pcsx2/CDVD/CDVDdiscReader.h | 5 ++- pcsx2/CDVD/CDVDdiscThread.cpp | 23 ++--------- pcsx2/CDVD/Darwin/IOCtlSrc.cpp | 6 +-- pcsx2/CDVD/Linux/IOCtlSrc.cpp | 6 +-- pcsx2/CDVD/Windows/IOCtlSrc.cpp | 2 - pcsx2/GameDatabase.cpp | 61 +++++++++++++--------------- pcsx2/MemoryCardFolder.cpp | 56 ++++++++++++------------- pcsx2/R5900.h | 2 +- pcsx2/R5900OpcodeImpl.cpp | 3 -- pcsx2/USB/usb-eyetoy/cam-windows.cpp | 36 +++++++++++----- pcsx2/USB/usb-eyetoy/cam-windows.h | 2 +- 12 files changed, 101 insertions(+), 140 deletions(-) diff --git a/pcsx2/CDVD/CDVDdiscReader.cpp b/pcsx2/CDVD/CDVDdiscReader.cpp index 44d42d0157..6b58e2ac70 100644 --- a/pcsx2/CDVD/CDVDdiscReader.cpp +++ b/pcsx2/CDVD/CDVDdiscReader.cpp @@ -156,14 +156,7 @@ bool StartKeepAliveThread() if (s_keepalive_is_open == false) { s_keepalive_is_open = true; - try - { - s_keepalive_thread = std::thread(keepAliveThread); - } - catch (std::system_error&) - { - s_keepalive_is_open = false; - } + s_keepalive_thread = std::thread(keepAliveThread); } return s_keepalive_is_open; @@ -190,21 +183,15 @@ s32 CALLBACK DISCopen(const char* pTitle) return -1; // open device file - try - { - src = std::unique_ptr(new IOCtlSrc(drive)); - } - catch (std::runtime_error&) - { - return -1; - } - - //setup threading manager - if (!cdvdStartThread()) + src = std::make_unique(std::move(drive)); + if (!src->Reopen()) { src.reset(); return -1; } + + //setup threading manager + cdvdStartThread(); StartKeepAliveThread(); return cdvdRefreshData(); @@ -312,16 +299,10 @@ s32 CALLBACK DISCgetTD(u8 Track, cdvdTD* Buffer) { if (src == nullptr) return -1; - try - { - Buffer->lsn = src->GetSectorCount(); - Buffer->type = 0; - return 0; - } - catch (...) - { - return -1; - } + + Buffer->lsn = src->GetSectorCount(); + Buffer->type = 0; + return 0; } if (Track < strack) diff --git a/pcsx2/CDVD/CDVDdiscReader.h b/pcsx2/CDVD/CDVDdiscReader.h index 622942b98d..9a11158e9a 100644 --- a/pcsx2/CDVD/CDVDdiscReader.h +++ b/pcsx2/CDVD/CDVDdiscReader.h @@ -64,12 +64,13 @@ class IOCtlSrc bool ReadDVDInfo(); bool ReadCDInfo(); - bool Reopen(); public: IOCtlSrc(std::string filename); ~IOCtlSrc(); + bool Reopen(); + u32 GetSectorCount() const; const std::vector& ReadTOC() const; bool ReadSectors2048(u32 sector, u32 count, u8* buffer) const; @@ -90,7 +91,7 @@ extern bool weAreInNewDiskCB; extern void (*newDiscCB)(); -bool cdvdStartThread(); +void cdvdStartThread(); void cdvdStopThread(); void cdvdRequestSector(u32 sector, s32 mode); u8* cdvdGetSector(u32 sector, s32 mode); diff --git a/pcsx2/CDVD/CDVDdiscThread.cpp b/pcsx2/CDVD/CDVDdiscThread.cpp index d46b83966c..03c298a57d 100644 --- a/pcsx2/CDVD/CDVDdiscThread.cpp +++ b/pcsx2/CDVD/CDVDdiscThread.cpp @@ -261,25 +261,15 @@ void cdvdThread() printf(" * CDVD: IO thread finished.\n"); } -bool cdvdStartThread() +void cdvdStartThread() { if (cdvd_is_open == false) { cdvd_is_open = true; - try - { - s_thread = std::thread(cdvdThread); - } - catch (std::system_error&) - { - cdvd_is_open = false; - return false; - } + s_thread = std::thread(cdvdThread); } cdvdCacheReset(); - - return true; } void cdvdStopThread() @@ -349,15 +339,8 @@ s32 cdvdDirectReadSector(u32 sector, s32 mode, u8* buffer) if (src == nullptr) return -1; - try - { - if (sector >= src->GetSectorCount()) - return -1; - } - catch (...) - { + if (sector >= src->GetSectorCount()) return -1; - } // Align to cache block u32 sector_block = sector & ~(sectors_per_read - 1); diff --git a/pcsx2/CDVD/Darwin/IOCtlSrc.cpp b/pcsx2/CDVD/Darwin/IOCtlSrc.cpp index f6d56e81f5..d8ef61dd27 100644 --- a/pcsx2/CDVD/Darwin/IOCtlSrc.cpp +++ b/pcsx2/CDVD/Darwin/IOCtlSrc.cpp @@ -29,11 +29,9 @@ #include #include -IOCtlSrc::IOCtlSrc(decltype(m_filename) filename) - : m_filename(filename) +IOCtlSrc::IOCtlSrc(std::string filename) + : m_filename(std::move(filename)) { - if (!Reopen()) - throw std::runtime_error(" * CDVD: Error opening source.\n"); } IOCtlSrc::~IOCtlSrc() diff --git a/pcsx2/CDVD/Linux/IOCtlSrc.cpp b/pcsx2/CDVD/Linux/IOCtlSrc.cpp index a424d05ceb..311ba1a1c6 100644 --- a/pcsx2/CDVD/Linux/IOCtlSrc.cpp +++ b/pcsx2/CDVD/Linux/IOCtlSrc.cpp @@ -26,11 +26,9 @@ #include #include -IOCtlSrc::IOCtlSrc(decltype(m_filename) filename) - : m_filename(filename) +IOCtlSrc::IOCtlSrc(std::string filename) + : m_filename(std::move(filename)) { - if (!Reopen()) - throw std::runtime_error(" * CDVD: Error opening source.\n"); } IOCtlSrc::~IOCtlSrc() diff --git a/pcsx2/CDVD/Windows/IOCtlSrc.cpp b/pcsx2/CDVD/Windows/IOCtlSrc.cpp index 36b288b15e..9917751e98 100644 --- a/pcsx2/CDVD/Windows/IOCtlSrc.cpp +++ b/pcsx2/CDVD/Windows/IOCtlSrc.cpp @@ -36,8 +36,6 @@ IOCtlSrc::IOCtlSrc(std::string filename) : m_filename(std::move(filename)) { - if (!Reopen()) - throw std::runtime_error(" * CDVD: Error opening source.\n"); } IOCtlSrc::~IOCtlSrc() diff --git a/pcsx2/GameDatabase.cpp b/pcsx2/GameDatabase.cpp index 7631fe08e0..b35fd9ee85 100644 --- a/pcsx2/GameDatabase.cpp +++ b/pcsx2/GameDatabase.cpp @@ -908,50 +908,45 @@ void GameDatabaseSchema::GameEntry::applyGSHardwareFixes(Pcsx2Config::GSOptions& void GameDatabase::initDatabase() { ryml::Callbacks rymlCallbacks = ryml::get_callbacks(); - rymlCallbacks.m_error = [](const char* msg, size_t msg_len, ryml::Location loc, void*) { - throw std::runtime_error(fmt::format("[YAML] Parsing error at {}:{} (bufpos={}): {}", - loc.line, loc.col, loc.offset, msg)); + rymlCallbacks.m_error = [](const char* msg, size_t msg_len, ryml::Location loc, void* userdata) { + Console.Error(fmt::format("[GameDB YAML] Parsing error at {}:{} (bufpos={}): {}", + loc.line, loc.col, loc.offset, std::string_view(msg, msg_len))); }; ryml::set_callbacks(rymlCallbacks); c4::set_error_callback([](const char* msg, size_t msg_size) { - throw std::runtime_error(fmt::format("[YAML] Internal Parsing error: {}", - msg)); + Console.Error(fmt::format("[GameDB YAML] Internal Parsing error: {}", std::string_view(msg, msg_size))); }); - try + + auto buf = Host::ReadResourceFileToString(GAMEDB_YAML_FILE_NAME); + if (!buf.has_value()) { - auto buf = Host::ReadResourceFileToString(GAMEDB_YAML_FILE_NAME); - if (!buf.has_value()) + Console.Error("[GameDB] Unable to open GameDB file, file does not exist."); + return; + } + + ryml::Tree tree = ryml::parse_in_arena(c4::to_csubstr(buf.value())); + ryml::NodeRef root = tree.rootref(); + + for (const ryml::NodeRef& n : root.children()) + { + auto serial = StringUtil::toLower(std::string(n.key().str, n.key().len)); + + // Serials and CRCs must be inserted as lower-case, as that is how they are retrieved + // this is because the application may pass a lowercase CRC or serial along + // + // However, YAML's keys are as expected case-sensitive, so we have to explicitly do our own duplicate checking + if (s_game_db.count(serial) == 1) { - Console.Error("[GameDB] Unable to open GameDB file, file does not exist."); - return; + Console.Error(fmt::format("[GameDB] Duplicate serial '{}' found in GameDB. Skipping, Serials are case-insensitive!", serial)); + continue; } - ryml::Tree tree = ryml::parse_in_arena(c4::to_csubstr(buf.value())); - ryml::NodeRef root = tree.rootref(); - - for (const auto& n : root.children()) + if (n.is_map()) { - auto serial = StringUtil::toLower(std::string(n.key().str, n.key().len)); - - // Serials and CRCs must be inserted as lower-case, as that is how they are retrieved - // this is because the application may pass a lowercase CRC or serial along - // - // However, YAML's keys are as expected case-sensitive, so we have to explicitly do our own duplicate checking - if (s_game_db.count(serial) == 1) - { - Console.Error(fmt::format("[GameDB] Duplicate serial '{}' found in GameDB. Skipping, Serials are case-insensitive!", serial)); - continue; - } - if (n.is_map()) - { - parseAndInsert(serial, n); - } + parseAndInsert(serial, n); } } - catch (const std::exception& e) - { - Console.Error(fmt::format("[GameDB] Error occured when initializing GameDB: {}", e.what())); - } + ryml::reset_callbacks(); } diff --git a/pcsx2/MemoryCardFolder.cpp b/pcsx2/MemoryCardFolder.cpp index 46eb978f70..914c184565 100644 --- a/pcsx2/MemoryCardFolder.cpp +++ b/pcsx2/MemoryCardFolder.cpp @@ -40,43 +40,37 @@ #include #include -static ryml::Tree parseYamlStr(const std::string& str) -{ - ryml::Callbacks rymlCallbacks = ryml::get_callbacks(); - rymlCallbacks.m_error = [](const char* msg, size_t msg_len, ryml::Location loc, void*) { - throw std::runtime_error(fmt::format("[YAML] Parsing error at {}:{} (bufpos={}): {}", - loc.line, loc.col, loc.offset, msg)); - }; - ryml::set_callbacks(rymlCallbacks); - c4::set_error_callback([](const char* msg, size_t msg_size) { - throw std::runtime_error(fmt::format("[YAML] Internal Parsing error: {}", - msg)); - }); - ryml::Tree tree = ryml::parse_in_arena(c4::to_csubstr(str)); - - ryml::reset_callbacks(); - return tree; -} - // A helper function to parse the YAML file static std::optional loadYamlFile(const char* filePath) { - try + std::optional buffer = FileSystem::ReadFileToString(filePath); + if (!buffer.has_value()) + return std::nullopt; + + static u32 errorCount; + errorCount = 0; + + ryml::Callbacks rymlCallbacks = ryml::get_callbacks(); + rymlCallbacks.m_error = [](const char* msg, size_t msg_len, ryml::Location loc, void* userdata) { + Console.Error(fmt::format("[YAML] Parsing error at {}:{} (bufpos={}): {}", + loc.line, loc.col, loc.offset, std::string_view(msg, msg_len))); + errorCount++; + }; + ryml::set_callbacks(rymlCallbacks); + c4::set_error_callback([](const char* msg, size_t msg_size) { + Console.Error(fmt::format("[YAML] Internal Parsing error: {}", std::string_view(msg, msg_size))); + errorCount++; + }); + + ryml::Tree tree = ryml::parse_in_arena(c4::to_csubstr(buffer.value())); + ryml::reset_callbacks(); + if (errorCount > 0) { - std::optional buffer = FileSystem::ReadFileToString(filePath); - if (!buffer.has_value()) - { - return std::nullopt; - } - ryml::Tree tree = parseYamlStr(buffer.value()); - return std::make_optional(tree); - } - catch (const std::exception& e) - { - Console.Error(fmt::format("[MemoryCard] Error occured when parsing folder memory card at path '{}': {}", filePath, e.what())); - ryml::reset_callbacks(); + Console.Error(fmt::format("[MemoryCard] Error occured when parsing folder memory card at path '{}'.", filePath)); return std::nullopt; } + + return tree; } /// A helper function to write a YAML file diff --git a/pcsx2/R5900.h b/pcsx2/R5900.h index 2fd5731e93..6eaa7986a6 100644 --- a/pcsx2/R5900.h +++ b/pcsx2/R5900.h @@ -366,7 +366,7 @@ extern void CPU_SET_DMASTALL(EE_EventType n, bool set); extern uint intcInterrupt(); extern uint dmacInterrupt(); -extern void cpuReset(); // can throw Exception::FileNotFound. +extern void cpuReset(); extern void cpuException(u32 code, u32 bd); extern void cpuTlbMissR(u32 addr, u32 bd); extern void cpuTlbMissW(u32 addr, u32 bd); diff --git a/pcsx2/R5900OpcodeImpl.cpp b/pcsx2/R5900OpcodeImpl.cpp index 5773cf5268..f187a933fa 100644 --- a/pcsx2/R5900OpcodeImpl.cpp +++ b/pcsx2/R5900OpcodeImpl.cpp @@ -1178,9 +1178,6 @@ void PREF() static void trap(u16 code=0) { - // unimplemented? - // throw R5900Exception::Trap(code); - cpuRegs.pc -= 4; Console.Warning("Trap exception at 0x%08x", cpuRegs.pc); cpuException(0x34, cpuRegs.branch); diff --git a/pcsx2/USB/usb-eyetoy/cam-windows.cpp b/pcsx2/USB/usb-eyetoy/cam-windows.cpp index 8b4af6878f..b10971b92c 100644 --- a/pcsx2/USB/usb-eyetoy/cam-windows.cpp +++ b/pcsx2/USB/usb-eyetoy/cam-windows.cpp @@ -339,34 +339,45 @@ namespace usb_eyetoy return 0; } - void DirectShow::Start() + bool DirectShow::Start() { HRESULT hr = nullrenderer->Run(0); if (FAILED(hr)) - throw hr; + { + Console.Error("nullrenderer->Run() failed: %08X", hr); + return false; + } hr = samplegrabberfilter->Run(0); if (FAILED(hr)) - throw hr; + { + Console.Error("samplegrabberfilter->Run() failed: %08X", hr); + return false; + } hr = sourcefilter->Run(0); if (FAILED(hr)) - throw hr; + { + Console.Error("sourcefilter->Run() failed: %08X", hr); + return false; + } + + return true; } void DirectShow::Stop() { HRESULT hr = sourcefilter->Stop(); if (FAILED(hr)) - throw hr; + Console.Error("sourcefilter->Stop() failed: %08X", hr); hr = samplegrabberfilter->Stop(); if (FAILED(hr)) - throw hr; + Console.Error("samplegrabberfilter->Stop() failed: %08X", hr); hr = nullrenderer->Stop(); if (FAILED(hr)) - throw hr; + Console.Error("nullrenderer->Stop() failed: %08X", hr); } void store_mpeg_frame(const unsigned char* data, const unsigned int len) @@ -551,9 +562,14 @@ namespace usb_eyetoy } pControl->Run(); - this->Stop(); - this->SetCallback(dshow_callback); - this->Start(); + Stop(); + SetCallback(dshow_callback); + if (!Start()) + { + Console.Error("Camera: Failed to start"); + Stop(); + return -1; + } return 0; }; diff --git a/pcsx2/USB/usb-eyetoy/cam-windows.h b/pcsx2/USB/usb-eyetoy/cam-windows.h index 399cc78d07..b03c057d94 100644 --- a/pcsx2/USB/usb-eyetoy/cam-windows.h +++ b/pcsx2/USB/usb-eyetoy/cam-windows.h @@ -90,7 +90,7 @@ namespace usb_eyetoy protected: void SetCallback(DShowVideoCaptureCallback cb) { callbackhandler->SetCallback(cb); } - void Start(); + bool Start(); void Stop(); int InitializeDevice(std::wstring selectedDevice);