From: <man...@us...> - 2014-12-15 12:11:59
|
Revision: 4653 http://sourceforge.net/p/modplug/code/4653 Author: manxorist Date: 2014-12-15 12:11:45 +0000 (Mon, 15 Dec 2014) Log Message: ----------- [Ref] Rewrite CTrackApp::SetupPaths into 4 phases: 1. Changing to the exe directory. 2. Determining the configuration paths, portable mode, first run flag. 3. Applying the determined paths. 4. Handling updates from older versions (moving the configuration files). Modified Paths: -------------- trunk/OpenMPT/mptrack/Mptrack.cpp trunk/OpenMPT/mptrack/Mptrack.h Modified: trunk/OpenMPT/mptrack/Mptrack.cpp =================================================================== --- trunk/OpenMPT/mptrack/Mptrack.cpp 2014-12-15 00:56:12 UTC (rev 4652) +++ trunk/OpenMPT/mptrack/Mptrack.cpp 2014-12-15 12:11:45 UTC (rev 4653) @@ -745,7 +745,6 @@ MPT_REGISTERED_COMPONENT(ComponentUXTheme) -#ifdef WIN32 // Legacy stuff // Move a config file called sFileName from the App's directory (or one of its sub directories specified by sSubDir) to // %APPDATA%. If specified, it will be renamed to sNewFileName. Existing files are never overwritten. // Returns true on success. @@ -775,70 +774,108 @@ } return false; } -#endif // WIN32 Legacy Stuff // Set up paths were configuration data is written to. Set overridePortable to true if application's own directory should always be used. void CTrackApp::SetupPaths(bool overridePortable) //----------------------------------------------- { - m_szExePath = mpt::GetAppPath(); - SetCurrentDirectoryW(m_szExePath.AsNative().c_str()); - m_szConfigDirectory = mpt::PathString(); - // Try to find a nice directory where we should store our settings (default: %APPDATA%) - bool bIsAppDir = overridePortable; - WCHAR tempConfigDirectory[MAX_PATH]; - tempConfigDirectory[0] = 0; - if(SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, tempConfigDirectory) != S_OK) + // change to exe directory + + SetCurrentDirectoryW(mpt::GetAppPath().AsNative().c_str()); + + + // Determine paths, portable mode, first run. Do not yet update any state. + + mpt::PathString configPathApp = mpt::GetAppPath(); // config path in portable mode + mpt::PathString configPathGlobal; // config path in default non-portable mode { - if(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, tempConfigDirectory) != S_OK) + // Try to find a nice directory where we should store our settings (default: %APPDATA%) + WCHAR tempConfigDirectory[MAX_PATH]; + MemsetZero(tempConfigDirectory); + if((SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, tempConfigDirectory) == S_OK) + || (SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, tempConfigDirectory) == S_OK)) { - bIsAppDir = true; + // Store our app settings in %APPDATA% or "My Files" + configPathGlobal = mpt::PathString::FromNative(tempConfigDirectory) + MPT_PATHSTRING("\\OpenMPT\\"); } } - m_szConfigDirectory = mpt::PathString::FromNative(tempConfigDirectory); + bool portableMode = overridePortable; + + if(configPathGlobal.empty()) + { + // no usable global directory found + portableMode = true; + } + // Check if the user prefers to use the app's directory - m_szConfigFileName = GetAppDirPath(); // config file - m_szConfigFileName += MPT_PATHSTRING("mptrack.ini"); - if(GetPrivateProfileIntW(L"Paths", L"UseAppDataDirectory", 1, m_szConfigFileName.AsNative().c_str()) == 0) + bool configAppPortable = (GetPrivateProfileIntW(L"Paths", L"UseAppDataDirectory", 1, (configPathApp + MPT_PATHSTRING("mptrack.ini")).AsNative().c_str()) == 0); + if(configAppPortable) { - bIsAppDir = true; + portableMode = true; } - if(!bIsAppDir) + // chose config directory + mpt::PathString configPath = portableMode ? configPathApp : configPathGlobal; + + // check if this is the first run + bool firstRun = true; { - // Store our app settings in %APPDATA% or "My Files" - m_szConfigDirectory += MPT_PATHSTRING("\\OpenMPT\\"); - - // Path doesn't exist yet, so it has to be created - if(!m_szConfigDirectory.IsDirectory()) + WCHAR tempVersion[MAX_PATH]; + MemsetZero(tempVersion); + GetPrivateProfileStringW(L"Version", L"Version", L"", tempVersion, CountOf(tempVersion), (configPathApp + MPT_PATHSTRING("mptrack.ini")).AsNative().c_str()); + if(!std::wstring(tempVersion).empty()) { - CreateDirectoryW(m_szConfigDirectory.AsNative().c_str(), 0); + firstRun = false; } + } - #ifdef WIN32 // Legacy stuff - // Move the config files if they're still in the old place. - MoveConfigFile(MPT_PATHSTRING("mptrack.ini")); - MoveConfigFile(MPT_PATHSTRING("plugin.cache")); - #endif // WIN32 Legacy Stuff - } else + + // Update state. + + // store exe path + m_szExePath = mpt::GetAppPath(); + + // store the config path + m_szConfigDirectory = configPath; + + // Set up default file locations + m_szConfigFileName = m_szConfigDirectory + MPT_PATHSTRING("mptrack.ini"); // config file + m_szPluginCacheFileName = m_szConfigDirectory + MPT_PATHSTRING("plugin.cache"); // plugin cache + TrackerDirectories::Instance().SetDefaultDirectory(m_szConfigDirectory + MPT_PATHSTRING("tunings\\"), DIR_TUNING); + TrackerDirectories::Instance().SetDefaultDirectory(m_szConfigDirectory + MPT_PATHSTRING("TemplateModules\\"), DIR_TEMPLATE_FILES_USER); + + // Force use of custom ini file rather than windowsDir\executableName.ini + if(m_pszProfileName) { - m_szConfigDirectory = GetAppDirPath(); + free((void *)m_pszProfileName); } + m_pszProfileName = _tcsdup(m_szConfigFileName.ToCString()); - // Create tunings dir - mpt::PathString sTuningPath = m_szConfigDirectory + MPT_PATHSTRING("tunings\\"); - TrackerDirectories::Instance().SetDefaultDirectory(sTuningPath, DIR_TUNING); + m_bPortableMode = portableMode; + // Create missing diretories + if(!m_szConfigDirectory.IsDirectory()) + { + CreateDirectoryW(m_szConfigDirectory.AsNative().c_str(), 0); + } if(!TrackerDirectories::Instance().GetDefaultDirectory(DIR_TUNING).IsDirectory()) { CreateDirectoryW(TrackerDirectories::Instance().GetDefaultDirectory(DIR_TUNING).AsNative().c_str(), 0); } - if(!bIsAppDir) + + // Handle updates from old versions. + + if(!portableMode) { + + // Move the config files if they're still in the old place. + MoveConfigFile(MPT_PATHSTRING("mptrack.ini")); + MoveConfigFile(MPT_PATHSTRING("plugin.cache")); + // Import old tunings mpt::PathString sOldTunings; sOldTunings = GetAppDirPath(); @@ -864,25 +901,6 @@ } } - // Set up default file locations - m_szConfigFileName = m_szConfigDirectory; // config file - m_szConfigFileName += MPT_PATHSTRING("mptrack.ini"); - - m_szPluginCacheFileName = m_szConfigDirectory + MPT_PATHSTRING("plugin.cache"); // plugin cache - - mpt::PathString szTemplatePath; - szTemplatePath = m_szConfigDirectory; - szTemplatePath += MPT_PATHSTRING("TemplateModules\\"); - TrackerDirectories::Instance().SetDefaultDirectory(szTemplatePath, DIR_TEMPLATE_FILES_USER); - - //Force use of custom ini file rather than windowsDir\executableName.ini - if(m_pszProfileName) - { - free((void *)m_pszProfileName); - } - m_pszProfileName = _tcsdup(m_szConfigFileName.ToCString()); - - m_bPortableMode = bIsAppDir; } BOOL CTrackApp::InitInstance() Modified: trunk/OpenMPT/mptrack/Mptrack.h =================================================================== --- trunk/OpenMPT/mptrack/Mptrack.h 2014-12-15 00:56:12 UTC (rev 4652) +++ trunk/OpenMPT/mptrack/Mptrack.h 2014-12-15 12:11:45 UTC (rev 4653) @@ -333,9 +333,7 @@ BOOL UninitializeDXPlugins(); -#ifdef WIN32 // Legacy stuff bool MoveConfigFile(mpt::PathString sFileName, mpt::PathString sSubDir = mpt::PathString(), mpt::PathString sNewFileName = mpt::PathString()); -#endif // WIN32 }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |