Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix O2DS crash on sram save #5

Merged
merged 3 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions source/3dsfiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ void file3dsGoToParentDirectory(void)
// Checks if file exists.
//----------------------------------------------------------------------
bool IsFileExists(const char * filename) {
if (filename == nullptr || filename[0] == '\0') {
return false;
}

if (FILE * file = fopen(filename, "r")) {
fclose(file);
return true;
Expand Down Expand Up @@ -475,6 +479,11 @@ std::string file3dsGetThumbnailFilenameByBasename(const std::string& basename, c

// get the associated filename of the current game (e.g. savestate, config, border, etc.)
std::string file3dsGetAssociatedFilename(const char* filename, const char* ext, const char* targetDir, bool trimmed) {
if (filename == nullptr || filename[0] == '\0') {
return "";
}

std::string associatedFilename;
std::string basename = trimmed ? file3dsGetTrimmedFileBasename(filename, false) : file3dsGetFileBasename(filename, false);

if (strcmp(ext, ".png") == 0 || strcmp(ext, ".chx") == 0 || strcmp(ext, ".cht") == 0) {
Expand All @@ -489,20 +498,25 @@ std::string file3dsGetAssociatedFilename(const char* filename, const char* ext,

std::string extension = ext != nullptr ? std::string(ext) : "";

if (targetDir == "thumbnails") {
return file3dsGetThumbnailFilenameByBasename(basename, ext);
if (targetDir == "thumbnails") {
associatedFilename = file3dsGetThumbnailFilenameByBasename(basename, ext);
return associatedFilename;
}

if (targetDir != nullptr) {
return std::string(settings3DS.RootDir) + "/" + targetDir + "/" + basename + extension;
associatedFilename = std::string(settings3DS.RootDir) + "/" + std::string(targetDir) + "/" + basename + extension;

return associatedFilename;
}

// if targetDir is undefined, use current game directory
std::string dir = std::string(filename);
size_t lastSlashPos = dir.find_last_of('/');
if (lastSlashPos != std::string::npos) {
return dir.substr(0, lastSlashPos) + "/" + basename + extension;
associatedFilename = dir.substr(0, lastSlashPos) + "/" + basename + extension;
} else {
associatedFilename = basename + extension;
}

return basename + extension;
return associatedFilename;
}
30 changes: 22 additions & 8 deletions source/3dsimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ void impl3dsSetBorderImage() {
} else {
borderFilename = file3dsGetAssociatedFilename(Memory.ROMFilename, ".png", "borders", true);
}

if (borderFilename.empty()) {
return;
}

float borderAlpha = (float)(settings3DS.GameBorderOpacity) / OPACITY_STEPS;

Expand Down Expand Up @@ -468,7 +472,10 @@ bool impl3dsLoadROM(char *romFilePath)

if(loaded) {
std::string path = file3dsGetAssociatedFilename(romFilePath, ".srm", "saves");
Memory.LoadSRAM (path.c_str());

if (!path.empty()) {
Memory.LoadSRAM (path.c_str());
}

// ensure controller is always set to player 1 when rom has loaded
Settings.SwapJoypads = 0;
Expand Down Expand Up @@ -693,6 +700,10 @@ bool impl3dsSaveStateAuto()

bool impl3dsSaveState(const char* filename)
{
if (filename == nullptr || filename[0] == '\0') {
return false;
}

return Snapshot(filename);
}

Expand Down Expand Up @@ -728,6 +739,10 @@ bool impl3dsLoadStateAuto()

bool impl3dsLoadState(const char* filename)
{
if (filename == nullptr || filename[0] == '\0') {
return false;
}

bool success = S9xLoadSnapshot(filename);
if (success)
{
Expand Down Expand Up @@ -858,9 +873,10 @@ bool impl3dsTakeScreenshot(const char*& path, bool menuOpen) {

while (i <= 99) {
ext = "." + std::to_string(i) + ".png";
snprintf(tmp, _MAX_PATH - 1, "%s", file3dsGetAssociatedFilename(Memory.ROMFilename, ext.c_str(), "screenshots").c_str());
std::string filename = file3dsGetAssociatedFilename(Memory.ROMFilename, ext.c_str(), "screenshots");
snprintf(tmp, _MAX_PATH - 1, "%s", filename.c_str());

if (!IsFileExists(tmp)) {
if (!filename.empty() && !IsFileExists(tmp)) {
path = tmp;
break;
}
Expand Down Expand Up @@ -978,17 +994,15 @@ void S9xAutoSaveSRAM (void)
//CPU.AccumulatedAutoSaveTimer = 0;
CPU.SRAMModified = false;

if (settings3DS.SecondScreenContent == CONTENT_INFO) {
menu3dsSetSecondScreenContent("Saving SRAM to SD card...", DIALOGCOLOR_CYAN);
}

// Bug fix: Instead of stopping CSND, we generate silence
// like we did prior to v0.61
//
snd3DS.generateSilence = true;
std::string path = file3dsGetAssociatedFilename(Memory.ROMFilename, ".srm", "saves");

Memory.SaveSRAM (path.c_str());
if (!path.empty()) {
Memory.SaveSRAM (path.c_str());
}

// Bug fix: Instead of starting CSND, we continue to mix
// like we did prior to v0.61
Expand Down
17 changes: 14 additions & 3 deletions source/3dsmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ size_t cacheThumbnails(std::vector<DirectoryEntry>& romFileNames, unsigned short

if (romFileNames[j].Type == FileEntryType::File) {
std::string thumbnailFilename = file3dsGetAssociatedFilename(romFileNames[j].Filename.c_str(), ".png", "thumbnails", true);
file3dsAddFileBufferToMemory(romFileNames[j].Filename, thumbnailFilename);

if (!thumbnailFilename.empty()) {
file3dsAddFileBufferToMemory(romFileNames[j].Filename, thumbnailFilename);
}

menu3dsSetCurrentPercent(++currentCount, totalCount);
}

Expand Down Expand Up @@ -225,7 +229,10 @@ void initThumbnailThread() {
char lastSelectedGame[_MAX_PATH];
strncpy(lastSelectedGame, romFileNameLastSelected, _MAX_PATH);
std::string thumbnailFilename = file3dsGetAssociatedFilename(lastSelectedGame, ".png", "thumbnails", true);
StoredFile file = file3dsAddFileBufferToMemory(lastSelectedGame, thumbnailFilename);

if (!thumbnailFilename.empty()) {
file3dsAddFileBufferToMemory(lastSelectedGame, thumbnailFilename);
}
}

// values have been taken from thread-basic example of 3ds-examples
Expand Down Expand Up @@ -377,7 +384,7 @@ int resetConfigOptionSelected(int val) {

if (val > 1) {
std::string gameConfigFile = file3dsGetAssociatedFilename(Memory.ROMFilename, ".cfg", "configs");
if (std::remove(gameConfigFile.c_str()) != 0) {
if (!gameConfigFile.empty() && std::remove(gameConfigFile.c_str()) != 0) {
cfgRemovalfailed += 2;
}
}
Expand Down Expand Up @@ -1232,6 +1239,10 @@ bool settingsReadWriteFullListByGame(bool writeMode)
BufferedFileWriter stream;
std::string path = file3dsGetAssociatedFilename(Memory.ROMFilename, ".cfg", "configs");

if (path.empty()) {
return false;
}

if (writeMode) {
if (!stream.open(path.c_str(), "w"))
return false;
Expand Down
11 changes: 7 additions & 4 deletions source/3dsmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,12 +1319,15 @@ void menu3dsSetSecondScreenContent(const char *dialogMessage, int dialogBackgrou

if (settings3DS.SecondScreenContent == CONTENT_IMAGE) {
std::string coverFilename = file3dsGetAssociatedFilename(Memory.ROMFilename, ".png", "covers", true);
cover = file3dsAddFileBufferToMemory("gameCover", coverFilename);

// show fallback cover
if (cover.Buffer.empty() && settings3DS.RomFsLoaded) {
coverFilename = "romfs:/cover.png";
if (!coverFilename.empty()) {
cover = file3dsAddFileBufferToMemory("gameCover", coverFilename);

// show fallback cover
if (cover.Buffer.empty() && settings3DS.RomFsLoaded) {
coverFilename = "romfs:/cover.png";
cover = file3dsAddFileBufferToMemory("gameCover", coverFilename);
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions source/Snes9x/cheats2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ bool S9xCheatExists(uint32 addr)

bool8 S9xLoadCheatFile (const char *filename)
{
if (filename == nullptr || filename[0] == '\0') {
return false;
}

Cheat.num_cheats = 0;

FILE *fs = fopen (filename, "rb");
Expand Down Expand Up @@ -205,6 +209,10 @@ bool8 S9xLoadCheatFile (const char *filename)

bool8 S9xSaveCheatFile (const char *filename)
{
if (filename == nullptr || filename[0] == '\0') {
return false;
}

if (Cheat.text_format)
return false;

Expand Down Expand Up @@ -269,6 +277,10 @@ void S9xStripNewLine(char *s)
//
bool8 S9xSaveCheatTextFile (const char *filename)
{
if (filename == nullptr || filename[0] == '\0') {
return false;
}

if (!Cheat.text_format)
return false;

Expand Down Expand Up @@ -303,6 +315,10 @@ bool8 S9xSaveCheatTextFile (const char *filename)
//
bool8 S9xLoadCheatTextFile (const char *filename)
{
if (filename == nullptr || filename[0] == '\0') {
return false;
}

FILE *fp = fopen (filename, "r");
if (fp == NULL)
return false;
Expand Down
8 changes: 8 additions & 0 deletions source/Snes9x/spc7110.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2234,6 +2234,10 @@ bool8 S9xSaveSPC7110RTC (S7RTC *rtc_f9)
FILE* fp;
std::string path = file3dsGetAssociatedFilename(Memory.ROMFilename, ".rtc", NULL, false);

if (path.empty()) {
return (FALSE);
}

if((fp=fopen(path.c_str(), "wb"))==NULL)
return (FALSE);
int i=0;
Expand Down Expand Up @@ -2265,6 +2269,10 @@ bool8 S9xLoadSPC7110RTC (S7RTC *rtc_f9)
FILE* fp;
std::string path = file3dsGetAssociatedFilename(Memory.ROMFilename, ".rtc", NULL, false);

if (path.empty()) {
return (FALSE);
}

if((fp=fopen(path.c_str(), "rb"))==NULL)
return (FALSE);
for (int i=0; i<16;i++)
Expand Down