Skip to content

Commit

Permalink
load last-used directory on a per-game basis
Browse files Browse the repository at this point in the history
Given that PalMod supports ~107 games currently, remembering which folder you probably want to be browsing to load the roms for whichever game seems like a useful  and probably overdue QoL improvement.
  • Loading branch information
Preppy committed May 3, 2021
1 parent 34e1124 commit 58f3d38
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 58 deletions.
4 changes: 2 additions & 2 deletions palmod/Game/GameClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CGameClass
BOOL m_fGameUnitsMapToIndividualFiles = FALSE;

UINT16 nUnitAmt = 0;
int nGameFlag = 0;
SupportedGamesList nGameFlag = NUM_GAMES;
int nImgGameFlag = 0;
int nImgUnitAmt = 0;
const UINT16* m_prgGameImageSet = nullptr;
Expand Down Expand Up @@ -220,7 +220,7 @@ class CGameClass

BOOL SpecSel(int* nVarSet, int nPalId, int nStart, int nInc, int nAmt = 1, int nMax = 6);

int GetGameFlag() { return nGameFlag; };
SupportedGamesList GetGameFlag() { return nGameFlag; };
int GetImgGameFlag() { return nImgGameFlag; };
int GetUnitCt() { return nUnitAmt; };
int GetImgUnitCt() { return nImgUnitAmt; };
Expand Down
4 changes: 2 additions & 2 deletions palmod/PalModDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
return TRUE;
}

BOOL CPalModDlg::SetLoadDir(CString* strOut, LPCWSTR pszDescriptionString /* = nullptr */)
BOOL CPalModDlg::SetLoadDir(CString* strOut, LPCWSTR pszDescriptionString /* = nullptr */, SupportedGamesList nDefaultGameFlag /* = NUM_GAMES */)
{
LPMALLOC pMalloc;
BOOL fSuccess = TRUE;
Expand All @@ -453,7 +453,7 @@ BOOL CPalModDlg::SetLoadDir(CString* strOut, LPCWSTR pszDescriptionString /* = n
bi.lpszTitle = pszDescriptionString ? pszDescriptionString : L"Select a target directory";
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_NONEWFOLDERBUTTON;
bi.lpfn = OnBrowseDialog;
bi.lParam = 0;
bi.lParam = nDefaultGameFlag;

if (pidl = ::SHBrowseForFolder(&bi))
{
Expand Down
15 changes: 8 additions & 7 deletions palmod/PalModDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ class CPalModDlg : public CDialog

//Program functions

void LoadGameDir(int nGameFlag, WCHAR* pszLoadDir);
void OnLoadGameByDirectory(int nGameFlag);
BOOL SetLoadDir(CString* strOut, LPCWSTR pszDescriptionString = nullptr);
void LoadGameDir(SupportedGamesList nGameFlag, WCHAR* pszLoadDir);
void OnLoadGameByDirectory(SupportedGamesList nGameFlag);
BOOL SetLoadDir(CString* strOut, LPCWSTR pszDescriptionString = nullptr, SupportedGamesList nDefaultGameFlag = NUM_GAMES);
void UpdateAppTitle();

void LoadGameFile(int nGameFlag, WCHAR* pszFile);
void LoadGameFile(SupportedGamesList nGameFlag, WCHAR* pszFile);

void LoadLastDir();

Expand Down Expand Up @@ -271,6 +271,8 @@ class CPalModDlg : public CDialog
DWORD GetColorAtCurrentMouseCursorPosition(int ptX = -1, int ptY = -1);
bool SelectMatchingColorsInPalette(DWORD dwColorToMatch);

static int CALLBACK OnBrowseDialog(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData);

afx_msg void OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu);
afx_msg void OnSettingsSettings();
afx_msg void OnEditUndo();
Expand Down Expand Up @@ -329,6 +331,5 @@ extern BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam);
extern BOOL IsPasteSupported();
extern CStringA szPasteStr;

extern int CALLBACK OnBrowseDialog( HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData );
extern void SetLastUsedDirectory(LPCWSTR pszPath, int nGameFlag);
extern BOOL GetLastUsedPath(LPWSTR pszPath, DWORD cbSize, int* nGameFlag, BOOL bCheckOnly = FALSE, BOOL* bIsDir = NULL);
extern void SetLastUsedDirectory(LPCWSTR pszPath, SupportedGamesList nGameFlag);
extern BOOL GetLastUsedPath(LPWSTR pszPath, DWORD cbSize, SupportedGamesList* nGameFlag, BOOL bCheckOnly = FALSE, BOOL* bIsDir = NULL);
147 changes: 102 additions & 45 deletions palmod/PalModDlg_File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ sSupportedGameList SupportedGameList[] =
sSupportedGameList* pSupportedGameList = SupportedGameList;
const int nNumberOfLoadROMOptions = ARRAYSIZE(SupportedGameList);

void CPalModDlg::LoadGameDir(int nGameFlag, WCHAR* pszLoadDir)
void CPalModDlg::LoadGameDir(SupportedGamesList nGameFlag, WCHAR* pszLoadDir)
{
ClearGameVar();

Expand Down Expand Up @@ -423,15 +423,15 @@ void CPalModDlg::SetMaximumWritePerEachTransparency(PALWriteOutputOptions eUpdat

void CPalModDlg::LoadLastDir()
{
int nLastUsedGFlag;
SupportedGamesList nLastUsedGFlag = NUM_GAMES;
BOOL bIsDir;
WCHAR szLastDir[MAX_PATH];

if (GetLastUsedPath(szLastDir, sizeof(szLastDir), &nLastUsedGFlag, FALSE, &bIsDir))
{
if (VerifyMsg(eVerifyType::VM_FILECHANGE)) // Save current changes if needed
{
if ((nLastUsedGFlag > NUM_GAMES) || (nLastUsedGFlag < 0))
if ((nLastUsedGFlag >= NUM_GAMES) || (nLastUsedGFlag < 0))
{
CString strError;
if (strError.LoadString(IDS_ERROR_PARAMETERS))
Expand Down Expand Up @@ -460,16 +460,17 @@ void CPalModDlg::LoadLastDir()
}
}

int CALLBACK OnBrowseDialog(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
int CALLBACK CPalModDlg::OnBrowseDialog(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
switch (uMsg)
{
case BFFM_INITIALIZED:
{
WCHAR szPath[MAX_PATH];
BOOL fIsDir = FALSE;
SupportedGamesList nDefaultGameFlag = (SupportedGamesList)lpData;

if (GetLastUsedPath(szPath, sizeof(szPath), nullptr, FALSE, &fIsDir))
if (GetLastUsedPath(szPath, sizeof(szPath), &nDefaultGameFlag, FALSE, &fIsDir))
{
if (!fIsDir)
{
Expand All @@ -482,7 +483,7 @@ int CALLBACK OnBrowseDialog(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
}
}

SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szPath);
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szPath);
}
break;
}
Expand All @@ -493,7 +494,7 @@ int CALLBACK OnBrowseDialog(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
return 0;
}

void SetLastUsedDirectory(LPCWSTR pszPath, int nGameFlag)
void SetLastUsedDirectory(LPCWSTR pszPath, SupportedGamesList nGameFlag)
{
if (NULL != pszPath)
{
Expand All @@ -502,6 +503,10 @@ void SetLastUsedDirectory(LPCWSTR pszPath, int nGameFlag)
//Set the directory / Game Flag
if (ERROR_SUCCESS == RegCreateKeyEx(HKEY_CURRENT_USER, c_AppRegistryRoot, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE | KEY_SET_VALUE, NULL, &hKey, NULL))
{
CString strPerGameString;

strPerGameString.Format(L"%s_%u", c_strLastUsedPath, nGameFlag);
RegSetValueEx(hKey, strPerGameString, 0, REG_SZ, (LPBYTE)pszPath, (DWORD)(wcslen(pszPath) + 1) * sizeof(WCHAR));
RegSetValueEx(hKey, c_strLastUsedPath, 0, REG_SZ, (LPBYTE)pszPath, (DWORD)(wcslen(pszPath) + 1) * sizeof(WCHAR));
RegSetValueEx(hKey, c_strLastUsedGFlag, 0, REG_DWORD, (LPBYTE)&nGameFlag, (DWORD)sizeof(int));

Expand All @@ -512,7 +517,7 @@ void SetLastUsedDirectory(LPCWSTR pszPath, int nGameFlag)
return;
}

BOOL GetLastUsedPath(LPWSTR pszPath, DWORD cbSize, int* nGameFlag, BOOL bCheckOnly, BOOL* bIsDir)
BOOL GetLastUsedPath(LPWSTR pszPath, DWORD cbSize, SupportedGamesList* nGameFlag, BOOL bCheckOnly, BOOL* bIsDir)
{
BOOL fFound = FALSE;
HKEY hKey = NULL;
Expand All @@ -523,38 +528,60 @@ BOOL GetLastUsedPath(LPWSTR pszPath, DWORD cbSize, int* nGameFlag, BOOL bCheckOn
WCHAR szPath[MAX_PATH] = {};
DWORD cbDataSize = sizeof(szPath);

//Get the directory
if ((ERROR_SUCCESS == RegQueryValueEx(hKey, c_strLastUsedPath, 0, &dwRegType, (LPBYTE)szPath, &cbDataSize))
&& (REG_SZ == dwRegType))
//Get the directory: tune to the last usage of the current game if desired and possible
for (int nPass = 0; (nPass < 2) && !fFound; nPass++)
{
if (bCheckOnly)
cbDataSize = sizeof(szPath);

CString strPerGameString;

if (nPass == 0)
{
fFound = TRUE;
if ((nGameFlag) && (*nGameFlag != NUM_GAMES))
{
strPerGameString.Format(L"%s_%u", c_strLastUsedPath, *nGameFlag);
}
else
{
continue;
}
}
else
{
DWORD dwAttribs = GetFileAttributes(szPath);
strPerGameString = c_strLastUsedPath;
}

if (INVALID_FILE_ATTRIBUTES != dwAttribs)
if ((ERROR_SUCCESS == RegQueryValueEx(hKey, strPerGameString, 0, &dwRegType, (LPBYTE)szPath, &cbDataSize))
&& (REG_SZ == dwRegType))
{
if (bCheckOnly)
{
if (bIsDir)
{
//Check to see if it's actually a file without an extension
*bIsDir = (dwAttribs & FILE_ATTRIBUTE_DIRECTORY);
}
fFound = TRUE;
}
else
{
DWORD dwAttribs = GetFileAttributes(szPath);

// This code used to be testing for (dwAttribs & FILE_ATTRIBUTE_ARCHIVE), but I don't think we need that currently.
if (INVALID_FILE_ATTRIBUTES != dwAttribs)
{
if (bIsDir)
{
//Check to see if it's actually a file without an extension
*bIsDir = (dwAttribs & FILE_ATTRIBUTE_DIRECTORY);
}

wcscpy(pszPath, szPath);
fFound = TRUE;
// This code used to be testing for (dwAttribs & FILE_ATTRIBUTE_ARCHIVE), but I don't think we need that currently.
wcscpy(pszPath, szPath);
fFound = TRUE;
}
}
}
}

//Grab the game flag
if (nGameFlag)
{
*nGameFlag = 0xFF;
*nGameFlag = NUM_GAMES;

dwRegType = REG_DWORD;
cbDataSize = sizeof(int);
Expand Down Expand Up @@ -589,33 +616,47 @@ void CPalModDlg::OnFileOpenInternal(UINT nDefaultGameFilter /* = NUM_GAMES */)
{
CString szGameFileDef = L"";

nDefaultGameFilter = nDefaultGameFilter & 0xffff; // eliminate the applied mask that we use to avoid existing menu items
nDefaultGameFilter = nDefaultGameFilter & 0xffff; // eliminate the applied mask (k_nGameLoadROMListMask, 0xf0000) that we use to avoid existing menu items

// The following logic ensures that their last used selection is the default filter view.
int nLastUsedGFlag = nDefaultGameFilter;
SupportedGamesList nLastUsedGFlag = (SupportedGamesList)nDefaultGameFilter;

if ((nLastUsedGFlag == NUM_GAMES) &&
!GetLastUsedPath(nullptr, 0, &nLastUsedGFlag, TRUE, nullptr))
WCHAR szLastDir[MAX_PATH];
BOOL fIsDir = FALSE;
bool fHaveLastUsedPath = GetLastUsedPath(szLastDir, sizeof(szLastDir), &nLastUsedGFlag, FALSE, &fIsDir);

if (nLastUsedGFlag == NUM_GAMES)
{
// If we're here, that means that they have never used PalMod to load a game before. Help them.
CString strInfo;
LPCWSTR pszParagraph1 = L"Howdy! You appear to be new to PalMod. Welcome!\n\n";
LPCWSTR pszParagraph2 = L"The first step is to load the ROM for the game you care about. There are a lot of game ROMs out there: the filter in the bottom right of the Load ROM dialog that you will see next helps show the right one for your game.\n\n";
if (!fHaveLastUsedPath)
{
// If we're here, that means that they have never used PalMod to load a game before. Help them.
CString strInfo;
LPCWSTR pszParagraph1 = L"Howdy! You appear to be new to PalMod. Welcome!\n\n";
LPCWSTR pszParagraph2 = L"The first step is to load the ROM for the game you care about. There are a lot of game ROMs out there: the filter in the bottom right of the Load ROM dialog that you will see next helps show the right one for your game.\n\n";

WCHAR szGameFilter[MAX_DESCRIPTION_LENGTH];
wcsncpy(szGameFilter, SupportedGameList[0].szGameFilterString, ARRAYSIZE(szGameFilter));
szGameFilter[MAX_DESCRIPTION_LENGTH - 1] = 0;
WCHAR szGameFilter[MAX_DESCRIPTION_LENGTH];
wcsncpy(szGameFilter, SupportedGameList[0].szGameFilterString, ARRAYSIZE(szGameFilter));
szGameFilter[MAX_DESCRIPTION_LENGTH - 1] = 0;

LPTSTR pszPipe = wcsstr(szGameFilter, L"|");
LPTSTR pszPipe = wcsstr(szGameFilter, L"|");

if (pszPipe != nullptr)
if (pszPipe != nullptr)
{
// Truncate off the filter information
pszPipe[0] = 0;
}

strInfo.Format(L"%s%sRight now this is going to be set to \'%s\' for the default game, \'%s\': you need to change that to the game you're interested in so that your ROM shows up.", pszParagraph1, pszParagraph2, szGameFilter, g_GameFriendlyName[SupportedGameList[0].nInternalGameIndex]);
MessageBox(strInfo, GetHost()->GetAppName(), MB_ICONINFORMATION);
}
}
else
{
// If there wasn't a specfic filter requested, use the last used game flag
if (nDefaultGameFilter == NUM_GAMES)
{
// Truncate off the filter information
pszPipe[0] = 0;
nDefaultGameFilter = nLastUsedGFlag;
}

strInfo.Format(L"%s%sRight now this is going to be set to \'%s\' for the default game, \'%s\': you need to change that to the game you're interested in so that your ROM shows up.", pszParagraph1, pszParagraph2, szGameFilter, g_GameFriendlyName[SupportedGameList[0].nInternalGameIndex]);
MessageBox(strInfo, GetHost()->GetAppName(), MB_ICONINFORMATION);
}

DWORD dwLastUsedGameIndex = 0;
Expand All @@ -626,7 +667,7 @@ void CPalModDlg::OnFileOpenInternal(UINT nDefaultGameFilter /* = NUM_GAMES */)
szGameFileDef.Append(SupportedGameList[nArrayPosition].szGameFilterString);
SupportedGameList[nArrayPosition].nListedGameIndex = nArrayPosition;

if (SupportedGameList[nArrayPosition].nInternalGameIndex == nLastUsedGFlag)
if (SupportedGameList[nArrayPosition].nInternalGameIndex == nDefaultGameFilter)
{
// user nFilterIndex starts at 1
dwLastUsedGameIndex = SupportedGameList[nArrayPosition].nListedGameIndex + 1;
Expand All @@ -647,6 +688,22 @@ void CPalModDlg::OnFileOpenInternal(UINT nDefaultGameFilter /* = NUM_GAMES */)

pOFN.nFilterIndex = dwLastUsedGameIndex;

if (fHaveLastUsedPath)
{
if (!fIsDir)
{
// We're pointing at a file, so switch over to the path
LPWSTR pszSlash = wcsrchr(szLastDir, L'\\');

if (pszSlash)
{
pszSlash[0] = 0;
}
}

pOFN.lpstrInitialDir = szLastDir;
}

if (OpenDialog.DoModal() == IDOK)
{
bool fSafeToContinue = true;
Expand All @@ -672,15 +729,15 @@ void CPalModDlg::OnFileOpenInternal(UINT nDefaultGameFilter /* = NUM_GAMES */)
// user nFilterIndex starts at 1
if ((currentGame.nListedGameIndex + 1) == ofn.nFilterIndex)
{
LoadGameFile(currentGame.nInternalGameIndex, (WCHAR*)ofn.lpstrFile);
LoadGameFile((SupportedGamesList)currentGame.nInternalGameIndex, (WCHAR*)ofn.lpstrFile);
break;
}
}
}
}
}

void CPalModDlg::LoadGameFile(int nGameFlag, WCHAR* pszFile)
void CPalModDlg::LoadGameFile(SupportedGamesList nGameFlag, WCHAR* pszFile)
{
if (!VerifyMsg(eVerifyType::VM_FILECHANGE))
{
Expand Down
4 changes: 2 additions & 2 deletions palmod/PalModDlg_Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "PalModDlg.h"
#include "PalMod.h"

void CPalModDlg::OnLoadGameByDirectory(int nGameFlag)
void CPalModDlg::OnLoadGameByDirectory(SupportedGamesList nGameFlag)
{
if (VerifyMsg(eVerifyType::VM_FILECHANGE))
{
Expand Down Expand Up @@ -78,7 +78,7 @@ void CPalModDlg::OnLoadGameByDirectory(int nGameFlag)
break;
}

if (SetLoadDir(&strGet, pszExtraInfo))
if (SetLoadDir(&strGet, pszExtraInfo, nGameFlag))
{
LoadGameDir(nGameFlag, strGet.GetBuffer());
}
Expand Down

0 comments on commit 58f3d38

Please sign in to comment.