From 5f068ec46a12b01950df3c3d53b995d7edf013e6 Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Wed, 17 Jun 2015 19:24:48 +0100 Subject: [PATCH] dev9ghz: Use .ini for saving settings Import settings from the registry if needed. --- plugins/dev9ghzdrk/DEV9.cpp | 4 +- plugins/dev9ghzdrk/DEV9.h | 3 +- plugins/dev9ghzdrk/Win32/Config.cpp | 71 ++++++++++++++++++++--------- 3 files changed, 53 insertions(+), 25 deletions(-) diff --git a/plugins/dev9ghzdrk/DEV9.cpp b/plugins/dev9ghzdrk/DEV9.cpp index b05fb3574b..ff5bd62849 100644 --- a/plugins/dev9ghzdrk/DEV9.cpp +++ b/plugins/dev9ghzdrk/DEV9.cpp @@ -81,7 +81,7 @@ u32 CALLBACK PS2EgetLibVersion2(u32 type) { } -//string s_strIniPath = "inis"; +std::string s_strIniPath = "inis"; std::string s_strLogPath = "logs"; // Warning: The below log function is SLOW. Better fix it before attempting to use it. #ifdef _DEBUG @@ -627,7 +627,7 @@ void CALLBACK DEV9setSettingsDir(const char* dir) { // Grab the ini directory. // TODO: Use - // s_strIniPath = (dir == NULL) ? "inis" : dir; + s_strIniPath = (dir == NULL) ? "inis" : dir; } void CALLBACK DEV9setLogDir(const char* dir) diff --git a/plugins/dev9ghzdrk/DEV9.h b/plugins/dev9ghzdrk/DEV9.h index 67d0958695..d9ca01192b 100644 --- a/plugins/dev9ghzdrk/DEV9.h +++ b/plugins/dev9ghzdrk/DEV9.h @@ -128,7 +128,8 @@ EXTERN DEV9callback DEV9irq; //void DEV9thread(); EXTERN PluginLog DEV9Log; -//Yes this is meant to be a lowercase extern +//Yes these are meant to be a lowercase extern +extern std::string s_strIniPath; extern std::string s_strLogPath; void __Log(char *fmt, ...); diff --git a/plugins/dev9ghzdrk/Win32/Config.cpp b/plugins/dev9ghzdrk/Win32/Config.cpp index 70d0efcace..4c8e895d9f 100644 --- a/plugins/dev9ghzdrk/Win32/Config.cpp +++ b/plugins/dev9ghzdrk/Win32/Config.cpp @@ -26,37 +26,46 @@ #define GetKeyVdw(name, var) \ GetKeyV(name, var, 4, REG_DWORD); -#define SetKeyV(name, var, s, t) \ - RegSetValueEx(myKey, name, 0, t, (LPBYTE) var, s); +//#define SetKeyV(name, var, s, t) \ +// RegSetValueEx(myKey, name, 0, t, (LPBYTE) var, s); -#define SetKeyVdw(name, var) \ - SetKeyV(name, var, 4, REG_DWORD); +//#define SetKeyVdw(name, var) \ +// SetKeyV(name, var, 4, REG_DWORD); -void SaveConf() { - HKEY myKey; - DWORD myDisp; - - RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\PS2Eplugin\\DEV9\\DEV9linuz", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &myKey, &myDisp); - SetKeyV("Eth", config.Eth, strlen(config.Eth), REG_SZ); - SetKeyV("Hdd", config.Hdd, strlen(config.Hdd), REG_SZ); - SetKeyVdw("HddSize", &config.HddSize); - SetKeyVdw("ethEnable", &config.ethEnable); - SetKeyVdw("hddEnable", &config.hddEnable); - - RegCloseKey(myKey); +BOOL WritePrivateProfileInt(LPCSTR lpAppName, LPCSTR lpKeyName, int intvar, LPCSTR lpFileName) +{ + return WritePrivateProfileString(lpAppName, lpKeyName, std::to_string(intvar).c_str(), lpFileName); +} +bool FileExists(std::string szPath) +{ + DWORD dwAttrib = GetFileAttributes(szPath.c_str()); + return (dwAttrib != INVALID_FILE_ATTRIBUTES && + !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); } -void LoadConf() { +void SaveConf() { + const std::string file(s_strIniPath + "/dev9ghz.ini"); + DeleteFile(file.c_str()); + + WritePrivateProfileString("DEV9", "Eth", config.Eth, file.c_str()); + WritePrivateProfileString("DEV9", "Hdd", config.Hdd, file.c_str()); + WritePrivateProfileInt("DEV9", "HddSize", config.HddSize, file.c_str()); + WritePrivateProfileInt("DEV9", "ethEnable", config.ethEnable, file.c_str()); + WritePrivateProfileInt("DEV9", "hddEnable", config.hddEnable, file.c_str()); +} + +void LoadRegConf() { HKEY myKey; DWORD type, size; - memset(&config, 0, sizeof(config)); - strcpy(config.Hdd, HDD_DEF); - config.HddSize=8*1024; - strcpy(config.Eth, ETH_DEF); + //memset(&config, 0, sizeof(config)); + //strcpy(config.Hdd, HDD_DEF); + //config.HddSize=8*1024; + //strcpy(config.Eth, ETH_DEF); if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\PS2Eplugin\\DEV9\\DEV9linuz", 0, KEY_ALL_ACCESS, &myKey)!=ERROR_SUCCESS) { - SaveConf(); return; + //SaveConf(); + return; } GetKeyV("Eth", config.Eth, sizeof(config.Eth), REG_SZ); GetKeyV("Hdd", config.Hdd, sizeof(config.Hdd), REG_SZ); @@ -66,4 +75,22 @@ void LoadConf() { RegCloseKey(myKey); } +void LoadConf() { + memset(&config, 0, sizeof(config)); + strcpy(config.Hdd, HDD_DEF); + config.HddSize = 8 * 1024; + strcpy(config.Eth, ETH_DEF); + + const std::string file(s_strIniPath + "/dev9ghz.ini"); + if (FileExists(file.c_str()) == false) { + LoadRegConf(); //Import old settings if user has upgraded this plugin + SaveConf(); + return; + } + GetPrivateProfileString("DEV9", "Eth", ETH_DEF, config.Eth, sizeof(config.Eth), file.c_str()); + GetPrivateProfileString("DEV9", "Hdd", HDD_DEF, config.Hdd, sizeof(config.Hdd), file.c_str()); + config.HddSize = GetPrivateProfileInt("DEV9", "HddSize", config.HddSize, file.c_str()); + config.ethEnable = GetPrivateProfileInt("DEV9", "ethEnable", config.ethEnable, file.c_str()); + config.ethEnable = GetPrivateProfileInt("DEV9", "hddEnable", config.hddEnable, file.c_str()); +}