Skip to content

Commit

Permalink
Check plugin state file exists, offer to browse for it if not
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgammon committed Aug 30, 2010
1 parent c765a89 commit a9b71cb
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions plugins.cpp
Expand Up @@ -601,6 +601,25 @@ void CPlugin::ExecutePluginScript (const char * sName,

} // end of CPlugin::ExecutePluginScript

void RemoveTrailingBackslash (CString& str);

// function prototypes needed for folder browsing

int __stdcall BrowseCallbackProc(
HWND hwnd,
UINT uMsg,
LPARAM lParam,
LPARAM lpData
);

// The field below is needed to initialise the browse directory dialog
// with the initial directory

extern CString strStartingDirectory;

// flag not in my version of the API
#define BIF_NEWDIALOGSTYLE 0x00000040

bool CPlugin::SaveState (const bool bScripted)
{

Expand All @@ -623,6 +642,83 @@ bool bError = true;
if (m_pDoc->m_strWorldID.IsEmpty ())
return true;

// sigh ... check plugin state folder exists

CFileStatus status;
CString strError;
CString strFolder = strFilename;
RemoveTrailingBackslash (strFolder); // trailing slash not wanted, thanks

if (!CFile::GetStatus(strFolder, status))
{
strError = "does not exist";
}
else
{ // file exists, is it a writable folder?
if ((status.m_attribute & CFile::directory) == 0)
strError = "is not a directory";
else if ((status.m_attribute & CFile::readOnly) == 1)
strError = "is not writable";
} // end of checking for save state folder

// if some error, alert the media
if (!strError.IsEmpty ())
{
::AfxMessageBox ((LPCTSTR) CFormat ("The plugins 'save state' folder:\r\n\r\n"
"%s\r\n\r\n"
"%s. Please click OK and then browse for it, or create it.",
(LPCTSTR) strFilename,
(LPCTSTR) strError),
MB_ICONSTOP);

// BROWSE FOR STATE FOLDER

// Gets the Shell's default allocator
LPMALLOC pMalloc;
if (::SHGetMalloc(&pMalloc) == NOERROR)
{
char pszBuffer[MAX_PATH];
BROWSEINFO bi;
LPITEMIDLIST pidl;

// Get help on BROWSEINFO struct - it's got all the bit settings.
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = pszBuffer;
bi.lpszTitle = "Folder for saving plugin state files";
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;

// if possible, let them create one
if (!bWine)
bi.ulFlags |= BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // requires CoInitialize

bi.lpfn = BrowseCallbackProc;
bi.lParam = 0;
strStartingDirectory = App.m_strPluginsDirectory; // really should be under plugins directory

// This next call issues the dialog box.
if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
{
if (::SHGetPathFromIDList(pidl, pszBuffer))
{
App.m_strDefaultStateFilesDirectory = pszBuffer;
App.m_strDefaultStateFilesDirectory += "\\";
strFilename = App.m_strDefaultStateFilesDirectory;
// save back to database
App.SaveGlobalsToDatabase ();

}

// Free the PIDL allocated by SHBrowseForFolder.
pMalloc->Free(pidl);
}
// Release the shell's allocator.
pMalloc->Release();
}

} // end of no save state folder


ExecutePluginScript (ON_PLUGIN_SAVE_STATE, m_dispid_plugin_save_state);

strFilename += m_pDoc->m_strWorldID; // world ID
Expand Down

0 comments on commit a9b71cb

Please sign in to comment.