From 59247c7838fe050e1ddf9eb799060e423d09094a Mon Sep 17 00:00:00 2001 From: Jonathan Li Date: Mon, 24 Oct 2016 21:32:06 +0100 Subject: [PATCH] cdvdgigaherz: Switch to using the new settings system Also refactor the default drive selection and GUI code so optical drive detection is shared. Note: This breaks the current config, but there's only one setting anyway. --- plugins/cdvdGigaherz/src/CDVD.cpp | 41 +++-- plugins/cdvdGigaherz/src/CDVD.h | 9 +- plugins/cdvdGigaherz/src/Windows/IOCtlSrc.cpp | 2 +- plugins/cdvdGigaherz/src/Windows/config.cpp | 140 ++++++++---------- 4 files changed, 87 insertions(+), 105 deletions(-) diff --git a/plugins/cdvdGigaherz/src/CDVD.cpp b/plugins/cdvdGigaherz/src/CDVD.cpp index e4afb28e7..78f59cdef 100644 --- a/plugins/cdvdGigaherz/src/CDVD.cpp +++ b/plugins/cdvdGigaherz/src/CDVD.cpp @@ -17,6 +17,9 @@ #include #include "svnrev.h" +Settings g_settings; +static std::string s_config_file{"inis/cdvdGigaherz.ini"}; + void (*newDiscCB)(); HANDLE hThread_keepAlive = nullptr; @@ -113,12 +116,20 @@ void __inline lba_to_msf(s32 lba, u8 *m, u8 *s, u8 *f) *f = (u8)(lba % 75); } +void ReadSettings() +{ + g_settings.Load(s_config_file); +} + +void WriteSettings() +{ + g_settings.Save(s_config_file); +} + /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // CDVD processing functions // -char csrc[20]; - bool cdvd_is_open = false; bool cdvdKeepAlive_is_open = false; bool disc_has_changed = false; @@ -192,7 +203,7 @@ void StopKeepAliveThread() void CALLBACK CDVDsetSettingsDir(const char *dir) { - CfgSetSettingsDir(dir); + s_config_file = std::string(dir ? dir : "inis") + "/cdvdGigaherz.ini"; } s32 CALLBACK CDVDinit() @@ -204,29 +215,13 @@ s32 CALLBACK CDVDopen(const char *pTitleFilename) { ReadSettings(); - if (source_drive == '-') { - // MSDN : Trailing backslash is required to ensure consistent behavior across - // various versions of Windows and storage types. - char temp[] = "A:\\"; - - for (char d = 'A'; d <= 'Z'; d++) { - temp[0] = d; - if (GetDriveType(temp) == DRIVE_CDROM) { - source_drive = d; - break; - } - } - if (source_drive == '-') - return -1; - } - - sprintf(csrc, "\\\\.\\%c:", source_drive); - - printf(" * CDVD: Opening drive '%s'...\n", csrc); + auto drive = GetValidDrive(); + if (drive.empty()) + return -1; // open device file try { - src = new IOCtlSrc(csrc); + src = new IOCtlSrc(drive); } catch (std::runtime_error &ex) { fputs(ex.what(), stdout); return -1; diff --git a/plugins/cdvdGigaherz/src/CDVD.h b/plugins/cdvdGigaherz/src/CDVD.h index d4ca29d2b..bbd03357c 100644 --- a/plugins/cdvdGigaherz/src/CDVD.h +++ b/plugins/cdvdGigaherz/src/CDVD.h @@ -28,6 +28,7 @@ #define CDVDdefs #include +#include "Settings.h" struct track { @@ -69,7 +70,7 @@ class IOCtlSrc bool Reopen(); public: - IOCtlSrc(const char *filename); + IOCtlSrc(decltype(m_filename) filename); ~IOCtlSrc(); u32 GetSectorCount() const; @@ -86,13 +87,13 @@ extern IOCtlSrc *src; void configure(); -extern char source_drive; - extern HINSTANCE hinst; void ReadSettings(); void WriteSettings(); -void CfgSetSettingsDir(const char *dir); +std::string GetValidDrive(); + +extern Settings g_settings; extern bool cdvd_is_open; extern bool cdvdKeepAlive_is_open; diff --git a/plugins/cdvdGigaherz/src/Windows/IOCtlSrc.cpp b/plugins/cdvdGigaherz/src/Windows/IOCtlSrc.cpp index 35b4124e9..ae9fb8a77 100644 --- a/plugins/cdvdGigaherz/src/Windows/IOCtlSrc.cpp +++ b/plugins/cdvdGigaherz/src/Windows/IOCtlSrc.cpp @@ -30,7 +30,7 @@ #include #include -IOCtlSrc::IOCtlSrc(const char *filename) +IOCtlSrc::IOCtlSrc(decltype(m_filename) filename) : m_filename(filename) { if (!Reopen()) diff --git a/plugins/cdvdGigaherz/src/Windows/config.cpp b/plugins/cdvdGigaherz/src/Windows/config.cpp index 9d82a819e..fed41126a 100644 --- a/plugins/cdvdGigaherz/src/Windows/config.cpp +++ b/plugins/cdvdGigaherz/src/Windows/config.cpp @@ -1,4 +1,4 @@ -//GiGaHeRz SPU2 Driver +//Copyright (C) 2016 PCSX2 Dev Team //Copyright (c) David Quintana // //This library is free software; you can redistribute it and/or @@ -21,23 +21,8 @@ #include #include "resource.h" -// Config Vars - -// DEBUG - -char source_drive; - char CfgFile[MAX_PATH + 10] = "inis/cdvdGigaherz.ini"; -void CfgSetSettingsDir(const char *dir) -{ - // a better std::string version, but it's inconvenient for other reasons. - //CfgFile = std::string(( dir == NULL ) ? "inis/" : dir) + "cdvdGigaherz.ini"; - - strcpy_s(CfgFile, (dir == NULL) ? "inis" : dir); - strcat_s(CfgFile, "/cdvdGigaherz.ini"); -} - /*| Config File Format: |ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*\ +--+---------------------+------------------------+ @@ -127,72 +112,39 @@ void CfgReadStr(char *Section, char *Name, char *Data, int DataSize, char *Defau /*****************************************************************************/ -void ReadSettings() -{ - char temp[512]; - - CfgReadStr("Config", "Source", temp, 511, "-"); - source_drive = temp[0]; -} - -/*****************************************************************************/ - -void WriteSettings() -{ - char temp[2]; - - temp[0] = source_drive; - temp[1] = 0; - - CfgWriteStr("Config", "Source", temp); -} - -char *path[] = { - "A:", "B:", "C:", "D:", "E:", "F:", "G:", "H:", "I:", "J:", "K:", "L:", "M:", - "N:", "O:", "P:", "Q:", "R:", "S:", "T:", "U:", "V:", "W:", "X:", "Y:", "Z:", -}; - static INT_PTR CALLBACK ConfigProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { - int wmId, wmEvent; - char temp[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - switch (uMsg) { case WM_INITDIALOG: { - int n = -1; - int s = 0; - - SendMessage(GetDlgItem(hWnd, IDC_DRIVE), CB_RESETCONTENT, 0, 0); - for (char d = 'A'; d <= 'Z'; d++) { - if (GetDriveType(path[d - 'A']) == DRIVE_CDROM) { - n++; - - SendMessage(GetDlgItem(hWnd, IDC_DRIVE), CB_ADDSTRING, 0, (LPARAM)path[d - 'A']); - - if (source_drive == d) { - s = n; - } - } + std::vector &drives = + *reinterpret_cast *>(lParam); + HWND combobox = GetDlgItem(hWnd, IDC_DRIVE); + std::string drive; + g_settings.Get("drive", drive); + for (size_t n = 0; n < drives.size(); ++n) { + SendMessage(combobox, CB_ADDSTRING, 0, + reinterpret_cast(drives[n].c_str())); + if (drive == drives[n]) + SendMessage(combobox, CB_SETCURSEL, n, 0); } - - if (n != -1) - SendMessage(GetDlgItem(hWnd, IDC_DRIVE), CB_SETCURSEL, s, 0); - } break; case WM_COMMAND: - wmId = LOWORD(wParam); - wmEvent = HIWORD(wParam); // Parse the menu selections: - switch (wmId) { - case IDOK: - if (SendMessage(GetDlgItem(hWnd, IDC_DRIVE), CB_GETCOUNT, 0, 0) > 0) { - GetDlgItemText(hWnd, IDC_DRIVE, temp, 20); - temp[19] = 0; - source_drive = temp[0]; + switch (LOWORD(wParam)) { + case IDOK: { + HWND combobox = GetDlgItem(hWnd, IDC_DRIVE); + LRESULT index = SendMessage(combobox, CB_GETCURSEL, 0, 0); + if (index != CB_ERR) { + LRESULT length = SendMessage(combobox, CB_GETLBTEXTLEN, + index, 0); + std::vector drive(length + 1); + SendMessage(combobox, CB_GETLBTEXT, index, + reinterpret_cast(drive.data())); + g_settings.Set("drive", std::string(drive.data())); WriteSettings(); } EndDialog(hWnd, 0); - break; + } break; case IDCANCEL: EndDialog(hWnd, 0); break; @@ -207,13 +159,47 @@ static INT_PTR CALLBACK ConfigProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l return TRUE; } +static std::vector GetOpticalDriveList() +{ + DWORD size = GetLogicalDriveStrings(0, nullptr); + std::vector drive_strings(size); + if (GetLogicalDriveStrings(size, drive_strings.data()) != size - 1) + return {}; + + std::vector drives; + for (auto p = drive_strings.data(); *p; ++p) { + if (GetDriveType(p) == DRIVE_CDROM) + drives.push_back(p); + while (*p) + ++p; + } + return drives; +} + +std::string GetValidDrive() +{ + std::string drive; + g_settings.Get("drive", drive); + if (drive.empty() || GetDriveType(drive.c_str()) != DRIVE_CDROM) { + auto drives = GetOpticalDriveList(); + if (drives.empty()) + return {}; + drive = drives.front(); + } + + printf(" * CDVD: Opening drive '%s'...\n", drive.c_str()); + + // The drive string has the form "X:\", but to open the drive, the string + // has to be in the form "\\.\X:" + drive.pop_back(); + drive.insert(0, "\\\\.\\"); + return drive; +} + void configure() { ReadSettings(); - INT_PTR ret = DialogBoxParam(hinst, MAKEINTRESOURCE(IDD_CONFIG), GetActiveWindow(), ConfigProc, 1); - if (ret == -1) { - MessageBoxEx(GetActiveWindow(), "Error Opening the config dialog.", "OMG ERROR!", MB_OK, 0); - return; - } - ReadSettings(); + auto drives = GetOpticalDriveList(); + DialogBoxParam(hinst, MAKEINTRESOURCE(IDD_CONFIG), GetActiveWindow(), + ConfigProc, reinterpret_cast(&drives)); }