From 407b01b39d85301fcd3642476c2fa826368f2034 Mon Sep 17 00:00:00 2001 From: SuuperW Date: Mon, 3 Sep 2018 06:02:01 -0500 Subject: [PATCH 01/10] (Windows) Set default path for exporting a save to path.BATTERY (same as importing a save) --- desmume/src/frontend/windows/importSave.cpp | 36 ++++++++++++++++++--- desmume/src/frontend/windows/importSave.h | 1 + desmume/src/frontend/windows/main.cpp | 21 +----------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/desmume/src/frontend/windows/importSave.cpp b/desmume/src/frontend/windows/importSave.cpp index 739d91473..f79db1d4a 100644 --- a/desmume/src/frontend/windows/importSave.cpp +++ b/desmume/src/frontend/windows/importSave.cpp @@ -26,7 +26,7 @@ #include "resource.h" -char ImportSavFName[MAX_PATH] = {0}; +char SavFName[MAX_PATH] = {0}; u32 fileSaveSize = 0; u32 fileSaveType = 0xFF; @@ -79,7 +79,7 @@ BOOL CALLBACK ImportSizeSelect_Proc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM l } SendDlgItemMessage(hDlg, IDC_IMP_MANUAL_SIZE, CB_SETCURSEL, fileInfo.type, 0); - fileSaveSize = MMU_new.backupDevice.importDataSize(ImportSavFName); + fileSaveSize = MMU_new.backupDevice.importDataSize(SavFName); if (fileSaveSize > 0) { @@ -174,7 +174,7 @@ bool importSave(HWND hwnd, HINSTANCE hAppInst) ofn.hwndOwner = hwnd; ofn.lpstrFilter = "All supported types\0*.sav;*.duc;*.dss\0Raw/No$GBA Save format (*.sav)\0*.sav\0Action Replay DS Save (*.duc,*.dss)\0*.duc;*.dss\0\0"; ofn.nFilterIndex = 1; - ofn.lpstrFile = ImportSavFName; + ofn.lpstrFile = SavFName; ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "sav"; ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST; @@ -190,7 +190,7 @@ bool importSave(HWND hwnd, HINSTANCE hAppInst) u32 res = DialogBoxW(hAppInst, MAKEINTRESOURCEW(IDD_IMPORT_SAVE_SIZE), hwnd, (DLGPROC)ImportSizeSelect_Proc); if (res < MAX_SAVE_TYPES) { - res = MMU_new.backupDevice.importData(ImportSavFName, save_types[res+1].size); + res = MMU_new.backupDevice.importData(SavFName, save_types[res+1].size); if (res) { printf("Save was successfully imported\n"); @@ -202,4 +202,30 @@ bool importSave(HWND hwnd, HINSTANCE hAppInst) } return (res == (MAX_SAVE_TYPES + 1)); -} \ No newline at end of file +} + +bool exportSave(HWND hwnd, HINSTANCE hAppInst) +{ + OPENFILENAME ofn; + ZeroMemory(&ofn, sizeof(ofn)); + ofn.lStructSize = sizeof(ofn); + ofn.hwndOwner = hwnd; + ofn.lpstrFilter = "Raw Save format (*.sav)\0*.sav\0No$GBA Save format (*.sav)\0*.sav\0\0"; + ofn.nFilterIndex = 0; + ofn.lpstrFile = SavFName; + ofn.nMaxFile = MAX_PATH; + ofn.lpstrDefExt = "sav"; + ofn.Flags = OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; + + char buffer[MAX_PATH] = { 0 }; + ZeroMemory(buffer, sizeof(buffer)); + path.getpath(path.BATTERY, buffer); + ofn.lpstrInitialDir = buffer; + + if (!GetSaveFileName(&ofn)) + return true; + + if (ofn.nFilterIndex == 2) strcat(SavFName, "*"); + + return !MMU_new.backupDevice.exportData(SavFName); +} diff --git a/desmume/src/frontend/windows/importSave.h b/desmume/src/frontend/windows/importSave.h index c9fb40c2b..d92605820 100644 --- a/desmume/src/frontend/windows/importSave.h +++ b/desmume/src/frontend/windows/importSave.h @@ -22,5 +22,6 @@ #include extern bool importSave(HWND hwnd, HINSTANCE hAppInst); +extern bool exportSave(HWND hwnd, HINSTANCE hAppInst); #endif \ No newline at end of file diff --git a/desmume/src/frontend/windows/main.cpp b/desmume/src/frontend/windows/main.cpp index e96a9358e..96e7818e7 100755 --- a/desmume/src/frontend/windows/main.cpp +++ b/desmume/src/frontend/windows/main.cpp @@ -4741,27 +4741,8 @@ DOKEYDOWN: } case IDM_EXPORTBACKUPMEMORY: { - OPENFILENAME ofn; NDS_Pause(); - ZeroMemory(&ofn, sizeof(ofn)); - ofn.lStructSize = sizeof(ofn); - ofn.hwndOwner = hwnd; - ofn.lpstrFilter = "Raw Save format (*.sav)\0*.sav\0No$GBA Save format (*.sav)\0*.sav\0\0"; - ofn.nFilterIndex = 0; - ofn.lpstrFile = ImportSavName; - ofn.nMaxFile = MAX_PATH; - ofn.lpstrDefExt = "sav"; - ofn.Flags = OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; - - if(!GetSaveFileName(&ofn)) - { - NDS_UnPause(); - return 0; - } - - if (ofn.nFilterIndex == 2) strcat(ImportSavName, "*"); - - if (!MMU_new.backupDevice.exportData(ImportSavName)) + if (!exportSave(hwnd, hAppInst)) MessageBox(hwnd,"Save was not successfully exported","Error",MB_OK); NDS_UnPause(); return 0; From c9c60498929c9319701852b55cb7580fc8c88e2d Mon Sep 17 00:00:00 2001 From: SuuperW Date: Tue, 4 Sep 2018 00:26:34 -0500 Subject: [PATCH 02/10] Move PathInfo method definitions to .cpp file. --- desmume/src/path.cpp | 254 +++++++++++++++++++++++++++++++++++++++++-- desmume/src/path.h | 238 +++------------------------------------- 2 files changed, 263 insertions(+), 229 deletions(-) diff --git a/desmume/src/path.cpp b/desmume/src/path.cpp index 009302013..2c3151278 100644 --- a/desmume/src/path.cpp +++ b/desmume/src/path.cpp @@ -199,6 +199,204 @@ void PathInfo::init(const char *filename) } +void PathInfo::LoadModulePath() +{ +#if defined(HOST_WINDOWS) + + char *p; + ZeroMemory(pathToModule, sizeof(pathToModule)); + + GetModuleFileName(NULL, pathToModule, sizeof(pathToModule)); + p = pathToModule + lstrlen(pathToModule); + while (p >= pathToModule && *p != DIRECTORY_DELIMITER_CHAR) p--; + if (++p >= pathToModule) *p = 0; + + extern char* _hack_alternateModulePath; + if (_hack_alternateModulePath) + { + strcpy(pathToModule, _hack_alternateModulePath); + } +#elif defined(DESMUME_COCOA) + std::string pathStr = Path::GetFileDirectoryPath(path); + + strncpy(pathToModule, pathStr.c_str(), MAX_PATH); +#else + char *cwd = g_build_filename(g_get_user_config_dir(), "desmume", NULL); + g_mkdir_with_parents(cwd, 0755); + strncpy(pathToModule, cwd, MAX_PATH); + g_free(cwd); +#endif +} + +void PathInfo::GetDefaultPath(char *pathToDefault, const char *key, int maxCount) +{ +#ifdef HOST_WINDOWS + std::string temp = (std::string)"." + DIRECTORY_DELIMITER_CHAR + pathToDefault; + strncpy(pathToDefault, temp.c_str(), maxCount); +#else + strncpy(pathToDefault, pathToModule, maxCount); +#endif +} + +void PathInfo::ReadKey(char *pathToRead, const char *key) +{ +#ifdef HOST_WINDOWS + GetPrivateProfileString(SECTION, key, key, pathToRead, MAX_PATH, IniName); + if (strcmp(pathToRead, key) == 0) { + //since the variables are all intialized in this file they all use MAX_PATH + GetDefaultPath(pathToRead, key, MAX_PATH); + } +#else + //since the variables are all intialized in this file they all use MAX_PATH + GetDefaultPath(pathToRead, key, MAX_PATH); +#endif +} + +void PathInfo::ReadPathSettings() +{ + if ((strcmp(pathToModule, "") == 0) || !pathToModule) + LoadModulePath(); + + ReadKey(pathToRoms, ROMKEY); + ReadKey(pathToBattery, BATTERYKEY); + ReadKey(pathToStates, STATEKEY); + ReadKey(pathToScreenshots, SCREENSHOTKEY); + ReadKey(pathToAviFiles, AVIKEY); + ReadKey(pathToCheats, CHEATKEY); + ReadKey(pathToSounds, SOUNDKEY); + ReadKey(pathToFirmware, FIRMWAREKEY); + ReadKey(pathToLua, LUAKEY); + ReadKey(pathToSlot1D, SLOT1DKEY); +#ifdef HOST_WINDOWS + GetPrivateProfileString(SECTION, FORMATKEY, "%f_%s_%r", screenshotFormat, MAX_FORMAT, IniName); + savelastromvisit = GetPrivateProfileBool(SECTION, LASTVISITKEY, true, IniName); + currentimageformat = (ImageFormat)GetPrivateProfileInt(SECTION, DEFAULTFORMATKEY, PNG, IniName); + r4Format = (R4Format)GetPrivateProfileInt(SECTION, R4FORMATKEY, R4_CHEAT_DAT, IniName); + if ((r4Format != R4_CHEAT_DAT) && (r4Format != R4_USRCHEAT_DAT)) + { + r4Format = R4_USRCHEAT_DAT; + WritePrivateProfileInt(SECTION, R4FORMATKEY, r4Format, IniName); + } +#endif + /* + needsSaving = GetPrivateProfileInt(SECTION, NEEDSSAVINGKEY, TRUE, IniName); + if(needsSaving) + { + needsSaving = FALSE; + WritePathSettings(); + }*/ +} + +void PathInfo::SwitchPath(Action action, KnownPath path, char *buffer) +{ + char *pathToCopy = 0; + switch (path) + { + case ROMS: + pathToCopy = pathToRoms; + break; + case BATTERY: + pathToCopy = pathToBattery; + break; + case STATES: + pathToCopy = pathToStates; + break; + case SCREENSHOTS: + pathToCopy = pathToScreenshots; + break; + case AVI_FILES: + pathToCopy = pathToAviFiles; + break; + case CHEATS: + pathToCopy = pathToCheats; + break; + case SOUNDS: + pathToCopy = pathToSounds; + break; + case FIRMWARE: + pathToCopy = pathToFirmware; + break; + case MODULE: + pathToCopy = pathToModule; + break; + case SLOT1D: + pathToCopy = pathToSlot1D; + break; + } + + if (action == GET) + { + std::string thePath = pathToCopy; + std::string relativePath = (std::string)"." + DIRECTORY_DELIMITER_CHAR; + + int len = (int)thePath.size() - 1; + + if (len == -1) + thePath = relativePath; + else + if (thePath[len] != DIRECTORY_DELIMITER_CHAR) + thePath += DIRECTORY_DELIMITER_CHAR; + + if (!Path::IsPathRooted(thePath)) + { + thePath = (std::string)pathToModule + thePath; + } + + strncpy(buffer, thePath.c_str(), MAX_PATH); +#ifdef HOST_WINDOWS + FCEUD_MakePathDirs(buffer); +#endif + } + else if (action == SET) + { + int len = strlen(buffer) - 1; + if (buffer[len] == DIRECTORY_DELIMITER_CHAR) + buffer[len] = '\0'; + + strncpy(pathToCopy, buffer, MAX_PATH); + } +} + +std::string PathInfo::getpath(KnownPath path) +{ + char temp[MAX_PATH]; + SwitchPath(GET, path, temp); + return temp; +} + +void PathInfo::getpath(KnownPath path, char *buffer) +{ + SwitchPath(GET, path, buffer); +} + +void PathInfo::setpath(KnownPath path, char *buffer) +{ + SwitchPath(SET, path, buffer); +} + +void PathInfo::getfilename(char *buffer, int maxCount) +{ + strcpy(buffer, noextension().c_str()); +} + +void PathInfo::getpathnoext(KnownPath path, char *buffer) +{ + getpath(path, buffer); + strcat(buffer, GetRomNameWithoutExtension().c_str()); +} + +std::string PathInfo::extension() +{ + return Path::GetFileExt(path); +} + +std::string PathInfo::noextension() +{ + std::string romNameWithPath = Path::GetFileDirectoryPath(path) + DIRECTORY_DELIMITER_CHAR + Path::GetFileNameWithoutExt(RomName); + + return romNameWithPath; +} + void PathInfo::formatname(char *output) { // Except 't' for tick and 'r' for random. @@ -211,10 +409,10 @@ void PathInfo::formatname(char *output) srand((unsigned)now); for (char* p = screenshotFormat, - *end = p + sizeof(screenshotFormat); p < end; p++) - { + *end = p + sizeof(screenshotFormat); p < end; p++) + { if (*p != '%') - { + { file.append(1, *p); } else @@ -228,18 +426,18 @@ void PathInfo::formatname(char *output) else if (*p == 'r') { file.append(stditoa(rand())); - } + } else if (*p == 't') - { + { file.append(stditoa(clock() >> 5)); - } + } else if (strchr(strftimeArgs, *p)) { char tmp[MAX_PATH]; char format[] = { '%', *p, '\0' }; strftime(tmp, MAX_PATH, format, time_struct); file.append(tmp); - } + } } } @@ -256,4 +454,44 @@ void PathInfo::formatname(char *output) #endif strncpy(output, file.c_str(), MAX_PATH); -} \ No newline at end of file +} + +PathInfo::ImageFormat PathInfo::imageformat() { + return currentimageformat; +} + +void PathInfo::SetRomName(const char *filename) +{ + std::string romPath = filename; + + RomName = Path::GetFileNameFromPath(romPath); + RomName = Path::ScrubInvalid(RomName); + RomDirectory = Path::GetFileDirectoryPath(romPath); +} + +const char *PathInfo::GetRomName() +{ + return RomName.c_str(); +} + +std::string PathInfo::GetRomNameWithoutExtension() +{ + if (RomName.c_str() == NULL) + return ""; + return Path::GetFileNameWithoutExt(RomName); +} + +bool PathInfo::isdsgba(std::string fileName) +{ + size_t i = fileName.find_last_of(FILE_EXT_DELIMITER_CHAR); + + if (i != std::string::npos) { + fileName = fileName.substr(i - 2); + } + + if (fileName == "ds.gba") { + return true; + } + + return false; +} diff --git a/desmume/src/path.h b/desmume/src/path.h index 6580fbcda..bea561432 100644 --- a/desmume/src/path.h +++ b/desmume/src/path.h @@ -118,34 +118,7 @@ public: void init(const char *filename); - void LoadModulePath() - { -#if defined(HOST_WINDOWS) - - char *p; - ZeroMemory(pathToModule, sizeof(pathToModule)); - - GetModuleFileName(NULL, pathToModule, sizeof(pathToModule)); - p = pathToModule + lstrlen(pathToModule); - while (p >= pathToModule && *p != DIRECTORY_DELIMITER_CHAR) p--; - if (++p >= pathToModule) *p = 0; - - extern char* _hack_alternateModulePath; - if(_hack_alternateModulePath) - { - strcpy(pathToModule,_hack_alternateModulePath); - } -#elif defined(DESMUME_COCOA) - std::string pathStr = Path::GetFileDirectoryPath(path); - - strncpy(pathToModule, pathStr.c_str(), MAX_PATH); -#else - char *cwd = g_build_filename(g_get_user_config_dir(), "desmume", NULL); - g_mkdir_with_parents(cwd, 0755); - strncpy(pathToModule, cwd, MAX_PATH); - g_free(cwd); -#endif - } + void LoadModulePath(); enum Action { @@ -153,174 +126,27 @@ public: SET }; - void GetDefaultPath(char *pathToDefault, const char *key, int maxCount) - { -#ifdef HOST_WINDOWS - std::string temp = (std::string)"." + DIRECTORY_DELIMITER_CHAR + pathToDefault; - strncpy(pathToDefault, temp.c_str(), maxCount); -#else - strncpy(pathToDefault, pathToModule, maxCount); -#endif - } + void GetDefaultPath(char *pathToDefault, const char *key, int maxCount); - void ReadKey(char *pathToRead, const char *key) - { -#ifdef HOST_WINDOWS - GetPrivateProfileString(SECTION, key, key, pathToRead, MAX_PATH, IniName); - if(strcmp(pathToRead, key) == 0) { - //since the variables are all intialized in this file they all use MAX_PATH - GetDefaultPath(pathToRead, key, MAX_PATH); - } -#else - //since the variables are all intialized in this file they all use MAX_PATH - GetDefaultPath(pathToRead, key, MAX_PATH); -#endif - } + void ReadKey(char *pathToRead, const char *key); - void ReadPathSettings() - { - if( ( strcmp(pathToModule, "") == 0) || !pathToModule) - LoadModulePath(); + void ReadPathSettings(); - ReadKey(pathToRoms, ROMKEY); - ReadKey(pathToBattery, BATTERYKEY); - ReadKey(pathToStates, STATEKEY); - ReadKey(pathToScreenshots, SCREENSHOTKEY); - ReadKey(pathToAviFiles, AVIKEY); - ReadKey(pathToCheats, CHEATKEY); - ReadKey(pathToSounds, SOUNDKEY); - ReadKey(pathToFirmware, FIRMWAREKEY); - ReadKey(pathToLua, LUAKEY); - ReadKey(pathToSlot1D, SLOT1DKEY); -#ifdef HOST_WINDOWS - GetPrivateProfileString(SECTION, FORMATKEY, "%f_%s_%r", screenshotFormat, MAX_FORMAT, IniName); - savelastromvisit = GetPrivateProfileBool(SECTION, LASTVISITKEY, true, IniName); - currentimageformat = (ImageFormat)GetPrivateProfileInt(SECTION, DEFAULTFORMATKEY, PNG, IniName); - r4Format = (R4Format)GetPrivateProfileInt(SECTION, R4FORMATKEY, R4_CHEAT_DAT, IniName); - if ((r4Format != R4_CHEAT_DAT) && (r4Format != R4_USRCHEAT_DAT)) - { - r4Format = R4_USRCHEAT_DAT; - WritePrivateProfileInt(SECTION, R4FORMATKEY, r4Format, IniName); - } -#endif - /* - needsSaving = GetPrivateProfileInt(SECTION, NEEDSSAVINGKEY, TRUE, IniName); - if(needsSaving) - { - needsSaving = FALSE; - WritePathSettings(); - }*/ - } + void SwitchPath(Action action, KnownPath path, char *buffer); - void SwitchPath(Action action, KnownPath path, char *buffer) - { - char *pathToCopy = 0; - switch(path) - { - case ROMS: - pathToCopy = pathToRoms; - break; - case BATTERY: - pathToCopy = pathToBattery; - break; - case STATES: - pathToCopy = pathToStates; - break; - case SCREENSHOTS: - pathToCopy = pathToScreenshots; - break; - case AVI_FILES: - pathToCopy = pathToAviFiles; - break; - case CHEATS: - pathToCopy = pathToCheats; - break; - case SOUNDS: - pathToCopy = pathToSounds; - break; - case FIRMWARE: - pathToCopy = pathToFirmware; - break; - case MODULE: - pathToCopy = pathToModule; - break; - case SLOT1D: - pathToCopy = pathToSlot1D; - break; - } + std::string getpath(KnownPath path); - if(action == GET) - { - std::string thePath = pathToCopy; - std::string relativePath = (std::string)"." + DIRECTORY_DELIMITER_CHAR; - - int len = (int)thePath.size()-1; + void getpath(KnownPath path, char *buffer); - if(len == -1) - thePath = relativePath; - else - if(thePath[len] != DIRECTORY_DELIMITER_CHAR) - thePath += DIRECTORY_DELIMITER_CHAR; - - if(!Path::IsPathRooted(thePath)) - { - thePath = (std::string)pathToModule + thePath; - } + void setpath(KnownPath path, char *buffer); - strncpy(buffer, thePath.c_str(), MAX_PATH); - #ifdef HOST_WINDOWS - FCEUD_MakePathDirs(buffer); - #endif - } - else if(action == SET) - { - int len = strlen(buffer)-1; - if(buffer[len] == DIRECTORY_DELIMITER_CHAR) - buffer[len] = '\0'; + void getfilename(char *buffer, int maxCount); - strncpy(pathToCopy, buffer, MAX_PATH); - } - } + void getpathnoext(KnownPath path, char *buffer); - std::string getpath(KnownPath path) - { - char temp[MAX_PATH]; - SwitchPath(GET, path, temp); - return temp; - } + std::string extension(); - void getpath(KnownPath path, char *buffer) - { - SwitchPath(GET, path, buffer); - } - - void setpath(KnownPath path, char *buffer) - { - SwitchPath(SET, path, buffer); - } - - void getfilename(char *buffer, int maxCount) - { - strcpy(buffer,noextension().c_str()); - } - - void getpathnoext(KnownPath path, char *buffer) - { - getpath(path, buffer); - strcat(buffer, GetRomNameWithoutExtension().c_str()); - } - - std::string extension() - { - return Path::GetFileExt(path); - } - - std::string noextension() - { - std::string romNameWithPath = Path::GetFileDirectoryPath(path) + DIRECTORY_DELIMITER_CHAR + Path::GetFileNameWithoutExt(RomName); - - return romNameWithPath; - } + std::string noextension(); void formatname(char *output); @@ -339,45 +165,15 @@ public: ImageFormat currentimageformat; - ImageFormat imageformat() { - return currentimageformat; - } + ImageFormat imageformat(); - void SetRomName(const char *filename) - { - std::string romPath = filename; + void SetRomName(const char *filename); - RomName = Path::GetFileNameFromPath(romPath); - RomName = Path::ScrubInvalid(RomName); - RomDirectory = Path::GetFileDirectoryPath(romPath); - } + const char *GetRomName(); - const char *GetRomName() - { - return RomName.c_str(); - } + std::string GetRomNameWithoutExtension(); - std::string GetRomNameWithoutExtension() - { - if (RomName.c_str() == NULL) - return ""; - return Path::GetFileNameWithoutExt(RomName); - } - - bool isdsgba(std::string fileName) - { - size_t i = fileName.find_last_of(FILE_EXT_DELIMITER_CHAR); - - if (i != std::string::npos) { - fileName = fileName.substr(i - 2); - } - - if(fileName == "ds.gba") { - return true; - } - - return false; - } + bool isdsgba(std::string fileName); }; extern PathInfo path; From 524997e406b40f7b7548e295e4d2a55a9a20b466 Mon Sep 17 00:00:00 2001 From: SuuperW Date: Tue, 4 Sep 2018 11:23:11 -0500 Subject: [PATCH 03/10] clean up Path::IsPathRooted --- desmume/src/path.cpp | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/desmume/src/path.cpp b/desmume/src/path.cpp index 2c3151278..b31117539 100644 --- a/desmume/src/path.cpp +++ b/desmume/src/path.cpp @@ -39,23 +39,17 @@ static const char InvalidPathChars[] = { //but it is sort of windows-specific. Does it work in linux? Maybe we'll have to make it smarter static const char VolumeSeparatorChar = ':'; -static const char AltDirectorySeparatorChar = '/'; static bool dirEqualsVolume = (DIRECTORY_DELIMITER_CHAR == VolumeSeparatorChar); - - -bool Path::IsPathRooted (const std::string &path) +bool Path::IsPathRooted(const std::string &path) { - if (path.empty()) { + if (path.empty()) return false; - } - if (path.find_first_of(InvalidPathChars) != std::string::npos) { + if (path.find_first_of(InvalidPathChars) != std::string::npos) return false; - } - char c = path[0]; - return (c == DIRECTORY_DELIMITER_CHAR || - c == AltDirectorySeparatorChar || + std::string delimiters = ALL_DIRECTORY_DELIMITER_STRING; + return (delimiters.find(path[0]) != std::string::npos || (!dirEqualsVolume && path.size() > 1 && path[1] == VolumeSeparatorChar)); } From e2d83f99fe51965f41a1863153fb89ec39046226 Mon Sep 17 00:00:00 2001 From: SuuperW Date: Tue, 4 Sep 2018 12:14:02 -0500 Subject: [PATCH 04/10] Support type string in setpath, use new method in several places. Make SwitchPath check for all directory delimiters when removing trailing delimiter, remove redundant trailing delimiter logic in CFIRMWARE::GetExternalFilePath(). --- desmume/src/firmware.cpp | 8 ++-- desmume/src/frontend/windows/importSave.cpp | 14 ++----- desmume/src/frontend/windows/main.cpp | 46 ++++++--------------- desmume/src/path.cpp | 7 +++- desmume/src/path.h | 2 +- 5 files changed, 26 insertions(+), 51 deletions(-) diff --git a/desmume/src/firmware.cpp b/desmume/src/firmware.cpp index 3fba948a1..94f3e9ca2 100644 --- a/desmume/src/firmware.cpp +++ b/desmume/src/firmware.cpp @@ -603,11 +603,9 @@ std::string CFIRMWARE::GetExternalFilePath() { std::string fwPath = CommonSettings.Firmware; std::string fwFileName = Path::GetFileNameFromPathWithoutExt(fwPath); - char configPath[MAX_PATH] = {0}; - path.getpath(path.BATTERY, configPath); - if (configPath[strlen(configPath)-1] == DIRECTORY_DELIMITER_CHAR) - configPath[strlen(configPath)-1] = 0; - std::string finalPath = std::string(configPath) + DIRECTORY_DELIMITER_CHAR + fwFileName + FILE_EXT_DELIMITER_CHAR + FW_CONFIG_FILE_EXT; + + std::string configPath = path.getpath(path.BATTERY); + std::string finalPath = configPath + DIRECTORY_DELIMITER_CHAR + fwFileName + FILE_EXT_DELIMITER_CHAR + FW_CONFIG_FILE_EXT; return finalPath; } diff --git a/desmume/src/frontend/windows/importSave.cpp b/desmume/src/frontend/windows/importSave.cpp index f79db1d4a..afd1ccc88 100644 --- a/desmume/src/frontend/windows/importSave.cpp +++ b/desmume/src/frontend/windows/importSave.cpp @@ -178,11 +178,8 @@ bool importSave(HWND hwnd, HINSTANCE hAppInst) ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "sav"; ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST; - - char buffer[MAX_PATH] = {0}; - ZeroMemory(buffer, sizeof(buffer)); - path.getpath(path.BATTERY, buffer); - ofn.lpstrInitialDir = buffer; + std::string dir = path.getpath(path.BATTERY); + ofn.lpstrInitialDir = dir.c_str(); if(!GetOpenFileName(&ofn)) return true; @@ -216,11 +213,8 @@ bool exportSave(HWND hwnd, HINSTANCE hAppInst) ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "sav"; ofn.Flags = OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; - - char buffer[MAX_PATH] = { 0 }; - ZeroMemory(buffer, sizeof(buffer)); - path.getpath(path.BATTERY, buffer); - ofn.lpstrInitialDir = buffer; + std::string dir = path.getpath(path.BATTERY); + ofn.lpstrInitialDir = dir.c_str(); if (!GetSaveFileName(&ofn)) return true; diff --git a/desmume/src/frontend/windows/main.cpp b/desmume/src/frontend/windows/main.cpp index 96e7818e7..c400d258b 100755 --- a/desmume/src/frontend/windows/main.cpp +++ b/desmume/src/frontend/windows/main.cpp @@ -3070,11 +3070,8 @@ LRESULT OpenFile() ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "nds"; ofn.Flags = OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST; - - char buffer[MAX_PATH]; - ZeroMemory(buffer, sizeof(buffer)); - path.getpath(path.ROMS, buffer); - ofn.lpstrInitialDir = buffer; + std::string dir = path.getpath(path.ROMS); + ofn.lpstrInitialDir = dir.c_str(); if (GetOpenFileName(&ofn) == NULL) { @@ -3085,14 +3082,9 @@ LRESULT OpenFile() { if(path.savelastromvisit) { - char *lchr, buffer[MAX_PATH]; - ZeroMemory(buffer, sizeof(buffer)); - - lchr = strrchr(filename, '\\'); - strncpy(buffer, filename, strlen(filename) - strlen(lchr)); - - path.setpath(path.ROMS, buffer); - WritePathSettings(); + std::string dir = Path::GetFileDirectoryPath(filename); + path.setpath(path.ROMS, dir); + WritePrivateProfileString(SECTION, ROMKEY, dir.c_str(), IniName); } } @@ -4660,11 +4652,8 @@ DOKEYDOWN: ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "dst"; ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST; - - char buffer[MAX_PATH]; - ZeroMemory(buffer, sizeof(buffer)); - path.getpath(path.STATES, buffer); - ofn.lpstrInitialDir = buffer; + std::string dir = path.getpath(path.STATES); + ofn.lpstrInitialDir = dir.c_str(); if(!GetOpenFileName(&ofn)) { @@ -4690,11 +4679,8 @@ DOKEYDOWN: ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "dst"; ofn.Flags = OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST; - - char buffer[MAX_PATH]; - ZeroMemory(buffer, sizeof(buffer)); - path.getpath(path.STATES, buffer); - ofn.lpstrInitialDir = buffer; + std::string dir = path.getpath(path.STATES); + ofn.lpstrInitialDir = dir.c_str(); if(GetSaveFileName(&ofn)) { @@ -5967,11 +5953,8 @@ LRESULT CALLBACK EmulationSettingsDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, L ofn.nMaxFile = 256; ofn.lpstrDefExt = "bin"; ofn.Flags = OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST; - - char buffer[MAX_PATH]; - ZeroMemory(buffer, sizeof(buffer)); - path.getpath(path.FIRMWARE, buffer); - ofn.lpstrInitialDir = buffer; + std::string dir = path.getpath(path.FIRMWARE); + ofn.lpstrInitialDir = dir.c_str(); if(GetOpenFileName(&ofn)) { @@ -6086,11 +6069,8 @@ LRESULT CALLBACK MicrophoneSettingsDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, ofn.nMaxFile = 256; ofn.lpstrDefExt = "wav"; ofn.Flags = OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST; - - char buffer[MAX_PATH]; - ZeroMemory(buffer, sizeof(buffer)); - path.getpath(path.SOUNDS, buffer); - ofn.lpstrInitialDir = buffer; + std::string dir = path.getpath(path.SOUNDS); + ofn.lpstrInitialDir = dir.c_str(); if(GetOpenFileName(&ofn)) { diff --git a/desmume/src/path.cpp b/desmume/src/path.cpp index b31117539..3beeee4e9 100644 --- a/desmume/src/path.cpp +++ b/desmume/src/path.cpp @@ -344,7 +344,7 @@ void PathInfo::SwitchPath(Action action, KnownPath path, char *buffer) else if (action == SET) { int len = strlen(buffer) - 1; - if (buffer[len] == DIRECTORY_DELIMITER_CHAR) + if (std::string(ALL_DIRECTORY_DELIMITER_STRING).find(buffer[len]) != std::string::npos) buffer[len] = '\0'; strncpy(pathToCopy, buffer, MAX_PATH); @@ -357,12 +357,15 @@ std::string PathInfo::getpath(KnownPath path) SwitchPath(GET, path, temp); return temp; } - void PathInfo::getpath(KnownPath path, char *buffer) { SwitchPath(GET, path, buffer); } +void PathInfo::setpath(KnownPath path, std::string value) +{ + SwitchPath(SET, path, (char*)value.c_str()); +} void PathInfo::setpath(KnownPath path, char *buffer) { SwitchPath(SET, path, buffer); diff --git a/desmume/src/path.h b/desmume/src/path.h index bea561432..275730028 100644 --- a/desmume/src/path.h +++ b/desmume/src/path.h @@ -135,9 +135,9 @@ public: void SwitchPath(Action action, KnownPath path, char *buffer); std::string getpath(KnownPath path); - void getpath(KnownPath path, char *buffer); + void setpath(KnownPath path, std::string value); void setpath(KnownPath path, char *buffer); void getfilename(char *buffer, int maxCount); From aaad5e63c5ee9917863398c19d3cf6fd7f9dba78 Mon Sep 17 00:00:00 2001 From: SuuperW Date: Wed, 5 Sep 2018 00:27:49 -0500 Subject: [PATCH 05/10] (Windows) Save and use all config paths. Add StateSlots path to avoid losing saved slot states when user manually selects a file to load elsewhere. --- desmume/src/frontend/windows/hotkey.cpp | 20 +++++++++--------- desmume/src/frontend/windows/importSave.cpp | 12 +++++++++-- desmume/src/frontend/windows/main.cpp | 21 +++++++++++++++++++ desmume/src/frontend/windows/pathsettings.cpp | 1 + desmume/src/path.cpp | 4 ++++ desmume/src/path.h | 5 ++++- desmume/src/saves.cpp | 6 +++--- 7 files changed, 53 insertions(+), 16 deletions(-) diff --git a/desmume/src/frontend/windows/hotkey.cpp b/desmume/src/frontend/windows/hotkey.cpp index dfad3e001..85849fb4d 100644 --- a/desmume/src/frontend/windows/hotkey.cpp +++ b/desmume/src/frontend/windows/hotkey.cpp @@ -236,7 +236,7 @@ void HK_PrintScreen(int param, bool justPressed) bool unpause = NDS_Pause(false); - char outFilename[MAX_PATH]; + char outFilename[MAX_PATH] = ""; OPENFILENAME ofn; ZeroMemory(&ofn,sizeof(ofn)); @@ -249,30 +249,30 @@ void HK_PrintScreen(int param, bool justPressed) ofn.lpstrDefExt = "png"; ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST; - std::string filename = path.getpath(path.SCREENSHOTS); - - char file[MAX_PATH]; - ZeroMemory(file, sizeof(file)); - path.formatname(file); - filename += file; + std::string dir = path.getpath(path.SCREENSHOTS); + ofn.lpstrInitialDir = dir.c_str(); + path.formatname(outFilename); if(path.imageformat() == PathInfo::PNG) { - filename += ".png"; + strcat(outFilename, ".png"); ofn.lpstrDefExt = "png"; ofn.nFilterIndex = 1; } else if(path.imageformat() == PathInfo::BMP) { - filename += ".bmp"; + strcat(outFilename, ".bmp"); ofn.lpstrDefExt = "bmp"; ofn.nFilterIndex = 2; } - strcpy(outFilename,filename.c_str()); if(GetSaveFileName(&ofn)) { DoScreenshot(outFilename); + + dir = Path::GetFileDirectoryPath(outFilename); + path.setpath(path.SCREENSHOTS, dir); + WritePrivateProfileString(SECTION, SCREENSHOTKEY, dir.c_str(), IniName); } if(unpause) NDS_UnPause(false); diff --git a/desmume/src/frontend/windows/importSave.cpp b/desmume/src/frontend/windows/importSave.cpp index afd1ccc88..b3f8a54dc 100644 --- a/desmume/src/frontend/windows/importSave.cpp +++ b/desmume/src/frontend/windows/importSave.cpp @@ -187,6 +187,10 @@ bool importSave(HWND hwnd, HINSTANCE hAppInst) u32 res = DialogBoxW(hAppInst, MAKEINTRESOURCEW(IDD_IMPORT_SAVE_SIZE), hwnd, (DLGPROC)ImportSizeSelect_Proc); if (res < MAX_SAVE_TYPES) { + std::string dir = Path::GetFileDirectoryPath(SavFName); + path.setpath(path.BATTERY, dir); + WritePrivateProfileString(SECTION, BATTERYKEY, dir.c_str(), IniName); + res = MMU_new.backupDevice.importData(SavFName, save_types[res+1].size); if (res) { @@ -197,8 +201,8 @@ bool importSave(HWND hwnd, HINSTANCE hAppInst) printf("Save was not successfully imported"); return res; } - - return (res == (MAX_SAVE_TYPES + 1)); + else // user canceled + return (res == (MAX_SAVE_TYPES + 1)); } bool exportSave(HWND hwnd, HINSTANCE hAppInst) @@ -219,6 +223,10 @@ bool exportSave(HWND hwnd, HINSTANCE hAppInst) if (!GetSaveFileName(&ofn)) return true; + dir = Path::GetFileDirectoryPath(SavFName); + path.setpath(path.BATTERY, dir); + WritePrivateProfileString(SECTION, BATTERYKEY, dir.c_str(), IniName); + if (ofn.nFilterIndex == 2) strcat(SavFName, "*"); return !MMU_new.backupDevice.exportData(SavFName); diff --git a/desmume/src/frontend/windows/main.cpp b/desmume/src/frontend/windows/main.cpp index c400d258b..72d9f477c 100755 --- a/desmume/src/frontend/windows/main.cpp +++ b/desmume/src/frontend/windows/main.cpp @@ -2896,6 +2896,10 @@ void AviRecordTo() LOG("AVI recording started."); driver->AddLine("AVI recording started."); } + + std::string dir = Path::GetFileDirectoryPath(folder); + path.setpath(path.AVI_FILES, dir); + WritePrivateProfileString(SECTION, AVIKEY, dir.c_str(), IniName); } NDS_UnPause(); @@ -4661,6 +4665,10 @@ DOKEYDOWN: return 0; } + dir = Path::GetFileDirectoryPath(SavName); + path.setpath(path.STATES, dir); + WritePrivateProfileString(SECTION, STATEKEY, dir.c_str(), IniName); + savestate_load(SavName); UpdateToolWindows(); NDS_UnPause(); @@ -4687,6 +4695,11 @@ DOKEYDOWN: savestate_save(SavName); LoadSaveStateInfo(); } + + dir = Path::GetFileDirectoryPath(SavName); + path.setpath(path.STATES, dir); + WritePrivateProfileString(SECTION, STATEKEY, dir.c_str(), IniName); + if(unpause) NDS_UnPause(); return 0; } @@ -5958,6 +5971,10 @@ LRESULT CALLBACK EmulationSettingsDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, L if(GetOpenFileName(&ofn)) { + std::string dir = Path::GetFileDirectoryPath(fileName); + path.setpath(path.FIRMWARE, dir); + WritePrivateProfileString(SECTION, FIRMWAREKEY, dir.c_str(), IniName); + HWND cur; switch(LOWORD(wParam)) @@ -6074,6 +6091,10 @@ LRESULT CALLBACK MicrophoneSettingsDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, if(GetOpenFileName(&ofn)) { + std::string dir = Path::GetFileDirectoryPath(fileName); + path.setpath(path.SOUNDS, dir); + WritePrivateProfileString(SECTION, SOUNDKEY, dir.c_str(), IniName); + HWND cur; switch(LOWORD(wParam)) diff --git a/desmume/src/frontend/windows/pathsettings.cpp b/desmume/src/frontend/windows/pathsettings.cpp index 23b69e5ed..4c4297eea 100644 --- a/desmume/src/frontend/windows/pathsettings.cpp +++ b/desmume/src/frontend/windows/pathsettings.cpp @@ -108,6 +108,7 @@ void WritePathSettings() WritePrivateProfileString(SECTION, ROMKEY, path.pathToRoms, IniName); WritePrivateProfileString(SECTION, BATTERYKEY, path.pathToBattery, IniName); WritePrivateProfileString(SECTION, STATEKEY, path.pathToStates, IniName); + WritePrivateProfileString(SECTION, STATESLOTKEY, path.pathToStateSlots, IniName); WritePrivateProfileString(SECTION, SCREENSHOTKEY, path.pathToScreenshots, IniName); WritePrivateProfileString(SECTION, AVIKEY, path.pathToAviFiles, IniName); WritePrivateProfileString(SECTION, CHEATKEY, path.pathToCheats, IniName); diff --git a/desmume/src/path.cpp b/desmume/src/path.cpp index 3beeee4e9..bd0aa00c8 100644 --- a/desmume/src/path.cpp +++ b/desmume/src/path.cpp @@ -254,6 +254,7 @@ void PathInfo::ReadPathSettings() ReadKey(pathToRoms, ROMKEY); ReadKey(pathToBattery, BATTERYKEY); ReadKey(pathToStates, STATEKEY); + ReadKey(pathToStateSlots, STATESLOTKEY); ReadKey(pathToScreenshots, SCREENSHOTKEY); ReadKey(pathToAviFiles, AVIKEY); ReadKey(pathToCheats, CHEATKEY); @@ -295,6 +296,9 @@ void PathInfo::SwitchPath(Action action, KnownPath path, char *buffer) case STATES: pathToCopy = pathToStates; break; + case STATE_SLOTS: + pathToCopy = pathToStateSlots; + break; case SCREENSHOTS: pathToCopy = pathToScreenshots; break; diff --git a/desmume/src/path.h b/desmume/src/path.h index 275730028..a06cc307b 100644 --- a/desmume/src/path.h +++ b/desmume/src/path.h @@ -73,6 +73,7 @@ public: #define ROMKEY "Roms" #define BATTERYKEY "Battery" #define STATEKEY "States" + #define STATESLOTKEY "StateSlots" #define SCREENSHOTKEY "Screenshots" #define AVIKEY "AviFiles" #define CHEATKEY "Cheats" @@ -93,7 +94,8 @@ public: FIRSTKNOWNPATH = 0, ROMS = 0, BATTERY, - STATES, + STATES, + STATE_SLOTS, SCREENSHOTS, AVI_FILES, CHEATS, @@ -107,6 +109,7 @@ public: char pathToRoms[MAX_PATH]; char pathToBattery[MAX_PATH]; char pathToStates[MAX_PATH]; + char pathToStateSlots[MAX_PATH]; char pathToScreenshots[MAX_PATH]; char pathToAviFiles[MAX_PATH]; char pathToCheats[MAX_PATH]; diff --git a/desmume/src/saves.cpp b/desmume/src/saves.cpp index b5d385819..7f1ae62ab 100644 --- a/desmume/src/saves.cpp +++ b/desmume/src/saves.cpp @@ -672,7 +672,7 @@ void scan_savestates() for(int i = 0; i < NB_STATES; i++ ) { - path.getpathnoext(path.STATES, filename); + path.getpathnoext(path.STATE_SLOTS, filename); if (strlen(filename) + strlen(".dst") + strlen("-2147483648") /* = biggest string for i */ >MAX_PATH) return ; sprintf(filename+strlen(filename), ".ds%d", i); @@ -692,7 +692,7 @@ void savestate_slot(int num) lastSaveState = num; //Set last savestate used - path.getpathnoext(path.STATES, filename); + path.getpathnoext(path.STATE_SLOTS, filename); if (strlen(filename) + strlen(".dsx") + strlen("-2147483648") /* = biggest string for num */ >MAX_PATH) return ; sprintf(filename+strlen(filename), ".ds%d", num); @@ -726,7 +726,7 @@ void loadstate_slot(int num) lastSaveState = num; //Set last savestate used - path.getpathnoext(path.STATES, filename); + path.getpathnoext(path.STATE_SLOTS, filename); if (strlen(filename) + strlen(".dsx") + strlen("-2147483648") /* = biggest string for num */ >MAX_PATH) return ; sprintf(filename+strlen(filename), ".ds%d", num); From 0cb3bbe87d30182d5dcc076dd3cfe85d7f8ff29d Mon Sep 17 00:00:00 2001 From: SuuperW Date: Wed, 5 Sep 2018 00:39:12 -0500 Subject: [PATCH 06/10] Format code. (mostly fixing indentation) --- desmume/src/frontend/windows/hotkey.cpp | 2 +- desmume/src/saves.cpp | 164 ++++++++++++------------ 2 files changed, 83 insertions(+), 83 deletions(-) diff --git a/desmume/src/frontend/windows/hotkey.cpp b/desmume/src/frontend/windows/hotkey.cpp index 85849fb4d..e40914281 100644 --- a/desmume/src/frontend/windows/hotkey.cpp +++ b/desmume/src/frontend/windows/hotkey.cpp @@ -232,7 +232,7 @@ void HK_QuickScreenShot(int param, bool justPressed) void HK_PrintScreen(int param, bool justPressed) { if(!justPressed) return; - if(!romloaded) return; + if(!romloaded) return; bool unpause = NDS_Pause(false); diff --git a/desmume/src/saves.cpp b/desmume/src/saves.cpp index 7f1ae62ab..72f312860 100644 --- a/desmume/src/saves.cpp +++ b/desmume/src/saves.cpp @@ -166,7 +166,7 @@ SFORMAT SF_MEM[]={ { "ITCM", 1, sizeof(MMU.ARM9_ITCM), MMU.ARM9_ITCM}, { "DTCM", 1, sizeof(MMU.ARM9_DTCM), MMU.ARM9_DTCM}, - //for legacy purposes, WRAX is a separate variable. shouldnt be a problem. + //for legacy purposes, WRAX is a separate variable. shouldnt be a problem. { "WRAM", 1, 0x400000, MMU.MAIN_MEM}, { "WRAX", 1, 0x400000, MMU.MAIN_MEM+0x400000}, @@ -414,17 +414,17 @@ static bool s_slot1_loadstate(EMUFILE &is, int size) u32 version = is.read_u32LE(); /* version 0: */ - u8 slotID = is.read_u32LE(); - slot1Type = NDS_SLOT1_RETAIL_AUTO; - if (version >= 1) - slot1_getTypeByID(slotID, slot1Type); + u8 slotID = is.read_u32LE(); + slot1Type = NDS_SLOT1_RETAIL_AUTO; + if (version >= 1) + slot1_getTypeByID(slotID, slot1Type); - slot1_Change(slot1Type); + slot1_Change(slot1Type); - EMUFILE_MEMORY temp; - is.read_MemoryStream(temp); - temp.fseek(0,SEEK_SET); - slot1_Loadstate(temp); + EMUFILE_MEMORY temp; + is.read_MemoryStream(temp); + temp.fseek(0, SEEK_SET); + slot1_Loadstate(temp); return true; } @@ -447,16 +447,16 @@ static bool s_slot2_loadstate(EMUFILE &is, int size) u32 version = is.read_u32LE(); /* version 0: */ - slot2Type = NDS_SLOT2_AUTO; - u8 slotID = is.read_u32LE(); - if (version == 0) - slot2_getTypeByID(slotID, slot2Type); - slot2_Change(slot2Type); + slot2Type = NDS_SLOT2_AUTO; + u8 slotID = is.read_u32LE(); + if (version == 0) + slot2_getTypeByID(slotID, slot2Type); + slot2_Change(slot2Type); - EMUFILE_MEMORY temp; - is.read_MemoryStream(temp); - temp.fseek(0,SEEK_SET); - slot2_Loadstate(temp); + EMUFILE_MEMORY temp; + is.read_MemoryStream(temp); + temp.fseek(0, SEEK_SET); + slot2_Loadstate(temp); return true; } @@ -646,100 +646,100 @@ static bool cp15_loadstate(EMUFILE &is, int size) /* Format time and convert to string */ static char * format_time(time_t cal_time) { - struct tm *time_struct; - static char str[64]; + struct tm *time_struct; + static char str[64]; - time_struct=localtime(&cal_time); - strftime(str, sizeof str, "%d-%b-%Y %H:%M:%S", time_struct); + time_struct = localtime(&cal_time); + strftime(str, sizeof str, "%d-%b-%Y %H:%M:%S", time_struct); - return(str); + return(str); } void clear_savestates() { - u8 i; - for( i = 0; i < NB_STATES; i++ ) - savestates[i].exists = FALSE; + u8 i; + for (i = 0; i < NB_STATES; i++) + savestates[i].exists = FALSE; } // Scan for existing savestates and update struct void scan_savestates() { - struct stat sbuf; - char filename[MAX_PATH+1]; + struct stat sbuf; + char filename[MAX_PATH + 1]; - clear_savestates(); + clear_savestates(); - for(int i = 0; i < NB_STATES; i++ ) - { - path.getpathnoext(path.STATE_SLOTS, filename); - - if (strlen(filename) + strlen(".dst") + strlen("-2147483648") /* = biggest string for i */ >MAX_PATH) return ; - sprintf(filename+strlen(filename), ".ds%d", i); - if( stat(filename,&sbuf) == -1 ) continue; - savestates[i].exists = TRUE; - strncpy(savestates[i].date, format_time(sbuf.st_mtime),40); - savestates[i].date[40-1] = '\0'; - } + for (int i = 0; i < NB_STATES; i++) + { + path.getpathnoext(path.STATE_SLOTS, filename); - return ; + if (strlen(filename) + strlen(".dst") + strlen("-2147483648") /* = biggest string for i */ > MAX_PATH) return; + sprintf(filename + strlen(filename), ".ds%d", i); + if (stat(filename, &sbuf) == -1) continue; + savestates[i].exists = TRUE; + strncpy(savestates[i].date, format_time(sbuf.st_mtime), 40); + savestates[i].date[40 - 1] = '\0'; + } + + return; } void savestate_slot(int num) { - struct stat sbuf; - char filename[MAX_PATH+1]; + struct stat sbuf; + char filename[MAX_PATH + 1]; lastSaveState = num; //Set last savestate used - path.getpathnoext(path.STATE_SLOTS, filename); + path.getpathnoext(path.STATE_SLOTS, filename); - if (strlen(filename) + strlen(".dsx") + strlen("-2147483648") /* = biggest string for num */ >MAX_PATH) return ; - sprintf(filename+strlen(filename), ".ds%d", num); + if (strlen(filename) + strlen(".dsx") + strlen("-2147483648") /* = biggest string for num */ > MAX_PATH) return; + sprintf(filename + strlen(filename), ".ds%d", num); - if (savestate_save(filename)) - { - driver->SetLineColor(255, 255, 255); - driver->AddLine("Saved to %i slot", num); - } - else - { - driver->SetLineColor(255, 0, 0); - driver->AddLine("Error saving %i slot", num); - return; - } + if (savestate_save(filename)) + { + driver->SetLineColor(255, 255, 255); + driver->AddLine("Saved to %i slot", num); + } + else + { + driver->SetLineColor(255, 0, 0); + driver->AddLine("Error saving %i slot", num); + return; + } - if (num >= 0 && num < NB_STATES) - { - if (stat(filename,&sbuf) != -1) - { - savestates[num].exists = TRUE; - strncpy(savestates[num].date, format_time(sbuf.st_mtime),40); - savestates[num].date[40-1] = '\0'; - } - } + if (num >= 0 && num < NB_STATES) + { + if (stat(filename, &sbuf) != -1) + { + savestates[num].exists = TRUE; + strncpy(savestates[num].date, format_time(sbuf.st_mtime), 40); + savestates[num].date[40 - 1] = '\0'; + } + } } void loadstate_slot(int num) { - char filename[MAX_PATH]; + char filename[MAX_PATH]; - lastSaveState = num; //Set last savestate used + lastSaveState = num; //Set last savestate used - path.getpathnoext(path.STATE_SLOTS, filename); + path.getpathnoext(path.STATE_SLOTS, filename); - if (strlen(filename) + strlen(".dsx") + strlen("-2147483648") /* = biggest string for num */ >MAX_PATH) return ; - sprintf(filename+strlen(filename), ".ds%d", num); - if (savestate_load(filename)) - { - driver->SetLineColor(255, 255, 255); - driver->AddLine("Loaded from %i slot", num); - } - else - { - driver->SetLineColor(255, 0, 0); - driver->AddLine("Error loading %i slot", num); - } + if (strlen(filename) + strlen(".dsx") + strlen("-2147483648") /* = biggest string for num */ > MAX_PATH) return; + sprintf(filename + strlen(filename), ".ds%d", num); + if (savestate_load(filename)) + { + driver->SetLineColor(255, 255, 255); + driver->AddLine("Loaded from %i slot", num); + } + else + { + driver->SetLineColor(255, 0, 0); + driver->AddLine("Error loading %i slot", num); + } } From 3b310082dc7b21d6abadb434d5e87f9173194fd3 Mon Sep 17 00:00:00 2001 From: SuuperW Date: Fri, 7 Sep 2018 16:08:27 -0500 Subject: [PATCH 07/10] (Windows) Properly set ofn.lpstrFile for import/export sav and for recording AVI/WAV --- desmume/src/frontend/windows/importSave.cpp | 4 +- desmume/src/frontend/windows/main.cpp | 43 +++++++++++---------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/desmume/src/frontend/windows/importSave.cpp b/desmume/src/frontend/windows/importSave.cpp index b3f8a54dc..42f701db3 100644 --- a/desmume/src/frontend/windows/importSave.cpp +++ b/desmume/src/frontend/windows/importSave.cpp @@ -174,7 +174,8 @@ bool importSave(HWND hwnd, HINSTANCE hAppInst) ofn.hwndOwner = hwnd; ofn.lpstrFilter = "All supported types\0*.sav;*.duc;*.dss\0Raw/No$GBA Save format (*.sav)\0*.sav\0Action Replay DS Save (*.duc,*.dss)\0*.duc;*.dss\0\0"; ofn.nFilterIndex = 1; - ofn.lpstrFile = SavFName; + ofn.lpstrFile = SavFName; + SavFName[0] = 0; // lpstrFile overrides lpstsrInitialDir if lpstrFile contains a path ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "sav"; ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST; @@ -214,6 +215,7 @@ bool exportSave(HWND hwnd, HINSTANCE hAppInst) ofn.lpstrFilter = "Raw Save format (*.sav)\0*.sav\0No$GBA Save format (*.sav)\0*.sav\0\0"; ofn.nFilterIndex = 0; ofn.lpstrFile = SavFName; + SavFName[0] = 0; // lpstrFile overrides lpstsrInitialDir if lpstrFile contains a path ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "sav"; ofn.Flags = OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; diff --git a/desmume/src/frontend/windows/main.cpp b/desmume/src/frontend/windows/main.cpp index 72d9f477c..bc27ff181 100755 --- a/desmume/src/frontend/windows/main.cpp +++ b/desmume/src/frontend/windows/main.cpp @@ -2854,6 +2854,7 @@ void AviRecordTo() //} // avi record file browser + char outFilename[MAX_PATH] = ""; memset(&ofn, 0, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = MainWindow->getHWnd(); @@ -2861,22 +2862,15 @@ void AviRecordTo() ofn.lpstrDefExt = "avi"; ofn.lpstrTitle = "Save AVI as"; - char folder[MAX_PATH]; - ZeroMemory(folder, sizeof(folder)); - path.getpath(path.AVI_FILES, folder); - - char file[MAX_PATH]; - ZeroMemory(file, sizeof(file)); - path.formatname(file); - - strcat(folder, file); - int len = strlen(folder); - if(len > MAX_PATH - 4) - folder[MAX_PATH - 4] = '\0'; - - strcat(folder, ".avi"); - ofn.lpstrFile = folder; + std::string dir = path.getpath(path.AVI_FILES); + ofn.lpstrInitialDir = dir.c_str(); + path.formatname(outFilename); + ofn.lpstrFile = outFilename; + int len = strlen(outFilename); + if(len + dir.length() > MAX_PATH - 4) + outFilename[MAX_PATH - dir.length() - 4] = '\0'; + strcat(outFilename, ".avi"); ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST; @@ -2890,14 +2884,14 @@ void AviRecordTo() driver->AddLine("AVI recording ended."); } - bool result = DRV_AviBegin(folder); + bool result = DRV_AviBegin(outFilename); if (result) { LOG("AVI recording started."); driver->AddLine("AVI recording started."); } - std::string dir = Path::GetFileDirectoryPath(folder); + dir = Path::GetFileDirectoryPath(outFilename); path.setpath(path.AVI_FILES, dir); WritePrivateProfileString(SECTION, AVIKEY, dir.c_str(), IniName); } @@ -2927,7 +2921,6 @@ void WavRecordTo(int wavmode) NDS_Pause(); OPENFILENAME ofn; - char szChoice[MAX_PATH] = {0}; ////if we are playing a movie, construct the filename from the current movie. ////else construct it from the filename. @@ -2951,20 +2944,30 @@ void WavRecordTo(int wavmode) //} // wav record file browser + char outFilename[MAX_PATH] = ""; memset(&ofn, 0, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = MainWindow->getHWnd(); ofn.lpstrFilter = "WAV Files (*.wav)\0*.wav\0\0"; - ofn.lpstrFile = szChoice; ofn.lpstrDefExt = "wav"; ofn.lpstrTitle = "Save WAV as"; + std::string dir = path.getpath(path.SOUNDS); + ofn.lpstrInitialDir = dir.c_str(); + path.formatname(outFilename); + ofn.lpstrFile = outFilename; + + int len = strlen(outFilename); + if (len + dir.length() > MAX_PATH - 4) + outFilename[MAX_PATH - dir.length() - 4] = '\0'; + strcat(outFilename, ".wav"); + ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST; if(GetSaveFileName(&ofn)) { - WAV_Begin(szChoice, (WAVMode)wavmode); + WAV_Begin(outFilename, (WAVMode)wavmode); } NDS_UnPause(); From 39f473c5909613f68cfd6ff474296002aeb1bd48 Mon Sep 17 00:00:00 2001 From: SuuperW Date: Fri, 7 Sep 2018 16:14:00 -0500 Subject: [PATCH 08/10] (Windows) Save selected WAV path --- desmume/src/frontend/windows/main.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/desmume/src/frontend/windows/main.cpp b/desmume/src/frontend/windows/main.cpp index bc27ff181..b05ac3d50 100755 --- a/desmume/src/frontend/windows/main.cpp +++ b/desmume/src/frontend/windows/main.cpp @@ -2968,6 +2968,10 @@ void WavRecordTo(int wavmode) if(GetSaveFileName(&ofn)) { WAV_Begin(outFilename, (WAVMode)wavmode); + + dir = Path::GetFileDirectoryPath(outFilename); + path.setpath(path.SOUNDS, dir); + WritePrivateProfileString(SECTION, SOUNDKEY, dir.c_str(), IniName); } NDS_UnPause(); From 910661f982384beb3cb31ac2da0c27b5564ffee8 Mon Sep 17 00:00:00 2001 From: SuuperW Date: Fri, 7 Sep 2018 16:24:02 -0500 Subject: [PATCH 09/10] (Windows) Switch Record WAV to use the same directory as Record AVI. --- desmume/src/frontend/windows/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/desmume/src/frontend/windows/main.cpp b/desmume/src/frontend/windows/main.cpp index b05ac3d50..e13549d64 100755 --- a/desmume/src/frontend/windows/main.cpp +++ b/desmume/src/frontend/windows/main.cpp @@ -2952,7 +2952,7 @@ void WavRecordTo(int wavmode) ofn.lpstrDefExt = "wav"; ofn.lpstrTitle = "Save WAV as"; - std::string dir = path.getpath(path.SOUNDS); + std::string dir = path.getpath(path.AVI_FILES); ofn.lpstrInitialDir = dir.c_str(); path.formatname(outFilename); ofn.lpstrFile = outFilename; @@ -2970,7 +2970,7 @@ void WavRecordTo(int wavmode) WAV_Begin(outFilename, (WAVMode)wavmode); dir = Path::GetFileDirectoryPath(outFilename); - path.setpath(path.SOUNDS, dir); + path.setpath(path.AVI_FILES, dir); WritePrivateProfileString(SECTION, SOUNDKEY, dir.c_str(), IniName); } From 35337a562a83acdb47c84a18f13e22c8ef21a8be Mon Sep 17 00:00:00 2001 From: SuuperW Date: Fri, 7 Sep 2018 16:34:31 -0500 Subject: [PATCH 10/10] fix commit 910661f --- desmume/src/frontend/windows/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/desmume/src/frontend/windows/main.cpp b/desmume/src/frontend/windows/main.cpp index e13549d64..73a423cff 100755 --- a/desmume/src/frontend/windows/main.cpp +++ b/desmume/src/frontend/windows/main.cpp @@ -2971,7 +2971,7 @@ void WavRecordTo(int wavmode) dir = Path::GetFileDirectoryPath(outFilename); path.setpath(path.AVI_FILES, dir); - WritePrivateProfileString(SECTION, SOUNDKEY, dir.c_str(), IniName); + WritePrivateProfileString(SECTION, AVIKEY, dir.c_str(), IniName); } NDS_UnPause();