save configuration

This commit is contained in:
itsmattkc 2021-09-19 12:40:17 -07:00
parent be075f8f4b
commit f5dae0804a
4 changed files with 104 additions and 8 deletions

View file

@ -129,6 +129,21 @@ PatchGrid::PatchGrid()
AddBoolItem(sectionMusic, _T("Play Music"), true));
}
BOOL PatchGrid::SaveConfiguration(LPCTSTR filename)
{
LPCTSTR appName = TEXT("Rebuilder");
for (std::map<std::string, HITEM>::const_iterator it=m_mPatchItems.begin(); it!=m_mPatchItems.end(); it++) {
std::string value;
this->GetItemValue(it->second, value);
if (!WritePrivateProfileString(appName, it->first.c_str(), value.c_str(), filename)) {
return FALSE;
}
}
return TRUE;
}
void PatchGrid::AddPatch(const string &id, const CString &description, HITEM item)
{
m_mPatchItems[id] = item;

View file

@ -9,6 +9,8 @@ class PatchGrid : public CPropertyGrid
public:
PatchGrid();
BOOL SaveConfiguration(LPCTSTR filename);
private:
void AddPatch(const std::string &id, const CString &description, HITEM item);

View file

@ -1,5 +1,6 @@
#include "window.h"
#include <SHLWAPI.H>
#include <WINDOWS.H>
#include "launcher.h"
@ -56,7 +57,7 @@ CRebuilderWindow::CRebuilderWindow()
// Create property grid
m_cPatchGrid.Create(AfxRegisterWndClass(CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW), _T("Patch Grid"), WS_CHILD | WS_VISIBLE, CRect(), &m_cTabCtrl, ID_PATCHGRID);
m_cMusicTable.Create(_T("Music Tab"), WS_CHILD, CRect(), &m_cTabCtrl);
m_cMusicTable.Create(_T("Coming back soon"), WS_CHILD | SS_CENTER, CRect(), &m_cTabCtrl);
// Create run button
m_cRunBtn.Create(GetResourceString(IDS_RUN), WS_CHILD | WS_VISIBLE, CRect(), this, ID_RUN);
@ -89,9 +90,13 @@ DWORD WINAPI WaitForProcessToClose(HANDLE hProcess)
void CRebuilderWindow::OnRunClick()
{
HANDLE proc = Launcher::Launch(this->GetSafeHwnd());
TCHAR configPath[MAX_PATH];
if (GetConfigPath(configPath)) {
// Append path and save configuration
_tcscat(configPath, _T("\\settings.ini"));
if (proc) {
if (m_cPatchGrid.SaveConfiguration(configPath)) {
if (HANDLE proc = Launcher::Launch(this->GetSafeHwnd())) {
m_lProcesses.push_back(proc);
SwitchButtonMode(TRUE);
@ -99,6 +104,12 @@ void CRebuilderWindow::OnRunClick()
DWORD threadId; // We don't use this, but Windows 95 will fail without it
CloseHandle(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WaitForProcessToClose, proc, 0, &threadId));
}
} else {
MessageBox(_T("Failed to save configuration file."));
}
} else {
MessageBox(_T("Failed to determine configuration path."));
}
}
void CRebuilderWindow::OnKillClick()
@ -241,6 +252,72 @@ void CRebuilderWindow::SwitchButtonMode(BOOL running)
}
}
BOOL DirectoryExists(LPCTSTR szPath)
{
return PathFileExists(szPath) && PathIsDirectory(szPath);
}
BOOL RecursivelyCreateDirectory(LPCTSTR directory)
{
if (DirectoryExists(directory)) {
// Directory already exists, do nothing
return TRUE;
} else {
// Determine directory of this directory
std::basic_string<TCHAR> copy = directory;
PathRemoveFileSpec(&copy[0]);
// Create if necessary
if (RecursivelyCreateDirectory(copy.c_str())) {
return CreateDirectory(directory, NULL);
} else {
return FALSE;
}
}
}
#ifdef UNICODE
typedef BOOL (WINAPI *SHGetSpecialFolderPathSignature)(HWND hwndOwner, LPWSTR lpszPath, int nFolder, BOOL fCreate);
#else
typedef BOOL (WINAPI *SHGetSpecialFolderPathSignature)(HWND hwndOwner, LPSTR lpszPath, int nFolder, BOOL fCreate);
#endif
BOOL CRebuilderWindow::GetConfigPath(LPTSTR s)
{
OSVERSIONINFO info;
ZeroMemory(&info, sizeof(info));
info.dwOSVersionInfoSize = sizeof(info);
GetVersionEx(&info);
// Dynamically link to SHGetSpecialFolderPath because not all versions of Windows have it
#ifdef UNICODE
LPCSTR functionName = "SHGetSpecialFolderPathW";
#else
LPCSTR functionName = "SHGetSpecialFolderPathA";
#endif
SHGetSpecialFolderPathSignature GetSpecialFolderPath = (SHGetSpecialFolderPathSignature)GetProcAddress(GetModuleHandle(_T("SHELL32.DLL")), functionName);
BOOL haveDir = FALSE;
BOOL usedShell = FALSE;
if (GetSpecialFolderPath) {
haveDir = GetSpecialFolderPath(NULL, s, CSIDL_APPDATA, TRUE);
usedShell = TRUE;
} else {
// Assume we're on Windows 95 which has no application data folder, we bodge it to write to
// "C:\Windows\Application Data" which is roughly where 98/Me would do it
GetWindowsDirectory(s, MAX_PATH);
_tcscat(s, _T("\\Application Data"));
haveDir = TRUE;
}
if (haveDir) {
_tcscat(s, _T("\\LEGOIslandRebuilder"));
MessageBox(s, usedShell ? _T("Using API") : _T("Is this Windows 95?"));
return RecursivelyCreateDirectory(s);
} else {
return FALSE;
}
}
BEGIN_MESSAGE_MAP(CRebuilderWindow, super)
ON_WM_SIZE()
ON_WM_GETMINMAXINFO()

View file

@ -34,6 +34,8 @@ private:
void SwitchButtonMode(BOOL running);
BOOL GetConfigPath(LPTSTR s);
enum {
ID_RUN = 1000,
ID_KILL,