From f5dae0804a39b9496651267915a5d233983729b5 Mon Sep 17 00:00:00 2001 From: itsmattkc <34096995+itsmattkc@users.noreply.github.com> Date: Sun, 19 Sep 2021 12:40:17 -0700 Subject: [PATCH] save configuration --- src/patchgrid.cpp | 15 ++++++++ src/patchgrid.h | 2 + src/window.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++---- src/window.h | 2 + 4 files changed, 104 insertions(+), 8 deletions(-) diff --git a/src/patchgrid.cpp b/src/patchgrid.cpp index 62c6469..e969813 100644 --- a/src/patchgrid.cpp +++ b/src/patchgrid.cpp @@ -129,6 +129,21 @@ PatchGrid::PatchGrid() AddBoolItem(sectionMusic, _T("Play Music"), true)); } +BOOL PatchGrid::SaveConfiguration(LPCTSTR filename) +{ + LPCTSTR appName = TEXT("Rebuilder"); + + for (std::map::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; diff --git a/src/patchgrid.h b/src/patchgrid.h index 7ed698e..c9414f4 100644 --- a/src/patchgrid.h +++ b/src/patchgrid.h @@ -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); diff --git a/src/window.cpp b/src/window.cpp index 3685f05..298674a 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,5 +1,6 @@ #include "window.h" +#include #include #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,15 +90,25 @@ 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) { - m_lProcesses.push_back(proc); - SwitchButtonMode(TRUE); + if (m_cPatchGrid.SaveConfiguration(configPath)) { + if (HANDLE proc = Launcher::Launch(this->GetSafeHwnd())) { + m_lProcesses.push_back(proc); + SwitchButtonMode(TRUE); - // Register callback when process exits - 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)); + // Register callback when process exits + 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.")); } } @@ -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 copy = directory; + PathRemoveFileSpec(©[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() diff --git a/src/window.h b/src/window.h index fc10700..29cd54e 100644 --- a/src/window.h +++ b/src/window.h @@ -34,6 +34,8 @@ private: void SwitchButtonMode(BOOL running); + BOOL GetConfigPath(LPTSTR s); + enum { ID_RUN = 1000, ID_KILL,