Skip to content

Commit

Permalink
capture and report core messages when load state fails (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Nov 2, 2022
1 parent 9320955 commit dd52c1d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
20 changes: 16 additions & 4 deletions src/States.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void States::saveState(unsigned ndx)
}
}

bool States::loadRAState1(unsigned char* input, size_t size)
bool States::loadRAState1(unsigned char* input, size_t size, std::string& errorBuffer)
{
unsigned char* stop = input + size;
unsigned char* marker;
Expand All @@ -301,7 +301,7 @@ bool States::loadRAState1(unsigned char* input, size_t size)

if (memcmp(marker, RASTATE_MEM_BLOCK, 4) == 0)
{
ret = _core->unserialize(input, block_size);
ret = _core->unserialize(input, block_size, &errorBuffer);
}
else if (memcmp(marker, RASTATE_CHEEVOS_BLOCK, 4) == 0)
{
Expand Down Expand Up @@ -433,11 +433,12 @@ bool States::loadState(const std::string& path)
data = decompressed;
}

std::string errorBuffer;
bool ret = true;
if (memcmp(data, "RASTATE", 7) != 0)
{
/* old format is just core data, load it directly */
ret = _core->unserialize(data, size);
ret = _core->unserialize(data, size, &errorBuffer);
if (ret)
RA_OnLoadState(path.c_str());

Expand All @@ -458,7 +459,7 @@ bool States::loadState(const std::string& path)
switch (input[7]) /* version */
{
case 1:
ret = loadRAState1(input, size);
ret = loadRAState1(input, size, errorBuffer);
break;

default:
Expand All @@ -471,6 +472,17 @@ bool States::loadState(const std::string& path)
if (!ret)
{
_logger->error(TAG "Error loading savestate");

if (!errorBuffer.empty())
{
errorBuffer = "Failed to load state.\n\n" + errorBuffer;
MessageBox(g_mainWindow, errorBuffer.c_str(), "Core Error", MB_OK);
}
else
{
MessageBox(g_mainWindow, "Failed to load state.", "Core Error", MB_OK);
}

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/States.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ class States
void saveSRAM(void* sramData, size_t sramSize);
void restoreFrameBuffer(const void* pixels, unsigned image_width, unsigned image_height, unsigned pitch);

bool loadRAState1(unsigned char* input, size_t size);
bool loadRAState1(unsigned char* input, size_t size, std::string& errorBuffer);
};
14 changes: 11 additions & 3 deletions src/libretro/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class CoreErrorLogger : public libretro::LoggerComponent
{
_logger->log(level, line, length);

if (level == RETRO_LOG_ERROR && _errorBuffer)
if ((level == RETRO_LOG_ERROR || level == RETRO_LOG_WARN) && _errorBuffer)
{
if (!_errorBuffer->empty())
_errorBuffer->push_back('\n');
Expand Down Expand Up @@ -518,9 +518,17 @@ bool libretro::Core::serialize(void* data, size_t size)
return _core.serialize(data, size);
}

bool libretro::Core::unserialize(const void* data, size_t size)
bool libretro::Core::unserialize(const void* data, size_t size, std::string* errorBuffer)
{
return _core.unserialize(data, size);
bool result;

CoreErrorLogger errorLogger(_logger, errorBuffer);
libretro::LoggerComponent* oldLogger = _logger;
_logger = &errorLogger;
result = _core.unserialize(data, size);
_logger = oldLogger;

return result;
}

bool libretro::Core::initCore()
Expand Down
2 changes: 1 addition & 1 deletion src/libretro/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace libretro
void resetGame();
size_t serializeSize();
bool serialize(void* data, size_t size);
bool unserialize(const void* data, size_t size);
bool unserialize(const void* data, size_t size, std::string* errorBuffer);

inline unsigned getPerformanceLevel() const { return _performanceLevel; }
inline enum retro_pixel_format getPixelFormat() const { return _pixelFormat; }
Expand Down

0 comments on commit dd52c1d

Please sign in to comment.