22
33// #include <optick.h>
44
5+ #include < SDL3/SDL_error.h>
56#include < SDL3/SDL_iostream.h>
67
78#include < cstring>
89#include < limits>
910#include < new>
1011
1112#include " Runtime/CDvdRequest.hpp"
13+ #include " Runtime/Logging.hpp"
1214#include " Runtime/CStopwatch.hpp"
1315
1416namespace metaforce {
@@ -99,10 +101,9 @@ class CFileDvdRequest : public IDvdRequest {
99101 ESeekOrigin m_whence;
100102 int m_offset;
101103
102-
103104 bool m_cancel = false ;
104105 bool m_complete = false ;
105-
106+
106107 std::function<void (u32 )> m_callback;
107108
108109public:
@@ -185,6 +186,7 @@ class CFileDvdRequest : public IDvdRequest {
185186
186187std::vector<std::shared_ptr<IDvdRequest>> CDvdFile::m_RequestQueue;
187188std::string CDvdFile::m_rootDirectory;
189+ std::string CDvdFile::m_lastError;
188190std::unique_ptr<u8 []> CDvdFile::m_dolBuf;
189191size_t CDvdFile::m_dolBufLen = 0 ;
190192
@@ -369,16 +371,21 @@ bool CDvdFile::LoadDolBuf() {
369371
370372bool CDvdFile::Initialize (const std::string_view& path) {
371373 Shutdown ();
374+ m_lastError.clear ();
372375
373376 std::string pathStr (path);
374377 SDL_IOStream* io = SDL_IOFromFile (pathStr.c_str (), " rb" );
375378 if (io == nullptr ) {
379+ m_lastError = std::string{" SDL_IOFromFile failed: " } + SDL_GetError ();
380+ spdlog::error (" {} (path: '{}')" , m_lastError, pathStr);
376381 return false ;
377382 }
378383
379384 auto * streamCtx = new (std::nothrow) SDLDiscStreamCtx{io};
380385 if (streamCtx == nullptr ) {
381386 SDL_CloseIO (io);
387+ m_lastError = " Failed to allocate SDL disc stream context" ;
388+ spdlog::error (" {} (path: '{}')" , m_lastError, pathStr);
382389 return false ;
383390 }
384391
@@ -394,7 +401,12 @@ bool CDvdFile::Initialize(const std::string_view& path) {
394401 };
395402 const NodResult discResult = nod_disc_open_stream (&stream, &discOpts, &discRaw);
396403 if (discResult != NOD_RESULT_OK || discRaw == nullptr ) {
397- sdlStreamClose (streamCtx);
404+ const char * nodError = nod_error_message ();
405+ m_lastError = fmt::format (" nod_disc_open_stream failed ({}){}" , int (discResult),
406+ nodError != nullptr && nodError[0 ] != ' \0 ' ? fmt::format (" : {}" , nodError) : " " );
407+ spdlog::error (" {} (path: '{}')" , m_lastError, pathStr);
408+ // Ownership of streamCtx is transferred to nod_disc_open_stream.
409+ // FfiDiscStream drops and invokes close() on failure paths.
398410 return false ;
399411 }
400412 m_DvdRoot = NodHandleUnique (discRaw, nod_free);
@@ -403,12 +415,24 @@ bool CDvdFile::Initialize(const std::string_view& path) {
403415 const NodResult partitionResult =
404416 nod_disc_open_partition_kind (m_DvdRoot.get (), NOD_PARTITION_KIND_DATA, nullptr , &partitionRaw);
405417 if (partitionResult != NOD_RESULT_OK || partitionRaw == nullptr ) {
418+ const char * nodError = nod_error_message ();
419+ m_lastError = fmt::format (" nod_disc_open_partition_kind(data) failed ({}){}" , int (partitionResult),
420+ nodError != nullptr && nodError[0 ] != ' \0 ' ? fmt::format (" : {}" , nodError) : " " );
421+ spdlog::error (" {} (path: '{}')" , m_lastError, pathStr);
406422 Shutdown ();
407423 return false ;
408424 }
409425 m_DataPartition = NodHandleUnique (partitionRaw, nod_free);
410426
411- if (!BuildFileEntries () || !LoadDolBuf ()) {
427+ if (!BuildFileEntries ()) {
428+ m_lastError = " Failed to read disc file-system table" ;
429+ spdlog::error (" {} (path: '{}')" , m_lastError, pathStr);
430+ Shutdown ();
431+ return false ;
432+ }
433+ if (!LoadDolBuf ()) {
434+ m_lastError = " Failed to load raw DOL data from disc" ;
435+ spdlog::error (" {} (path: '{}')" , m_lastError, pathStr);
412436 Shutdown ();
413437 return false ;
414438 }
0 commit comments