From 4b1e6683d258412dd6e02c55618ff158c749ab4a Mon Sep 17 00:00:00 2001 From: Tannin Date: Mon, 18 Aug 2014 23:00:25 +0200 Subject: - can now filter by mod content - all files/directories that require write access can now be placed in AppData instead of the MO directory --- src/shared/gameinfo.cpp | 83 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 29 deletions(-) (limited to 'src/shared/gameinfo.cpp') diff --git a/src/shared/gameinfo.cpp b/src/shared/gameinfo.cpp index ebeff55a..f1b72cfb 100644 --- a/src/shared/gameinfo.cpp +++ b/src/shared/gameinfo.cpp @@ -27,10 +27,12 @@ along with Mod Organizer. If not, see . #include "skyriminfo.h" #include "util.h" + #include #include #include #include +#include namespace MOShared { @@ -38,8 +40,8 @@ namespace MOShared { GameInfo* GameInfo::s_Instance = NULL; -GameInfo::GameInfo(const std::wstring &omoDirectory, const std::wstring &gameDirectory) - : m_GameDirectory(gameDirectory), m_OrganizerDirectory(omoDirectory) +GameInfo::GameInfo(const std::wstring &moDirectory, const std::wstring &moDataDirectory, const std::wstring &gameDirectory) + : m_GameDirectory(gameDirectory), m_OrganizerDirectory(moDirectory), m_OrganizerDataDirectory(moDataDirectory) { atexit(&cleanup); } @@ -53,53 +55,68 @@ void GameInfo::cleanup() { void GameInfo::identifyMyGamesDirectory(const std::wstring &file) { + // this function attempts 3 (three!) ways to determine the correct "My Games" folder. wchar_t myDocuments[MAX_PATH]; memset(myDocuments, '\0', MAX_PATH); - ::SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, myDocuments); + m_MyGamesDirectory.clear(); + + // a) this is the way it should work. get the configured My Documents\My Games directory + if (::SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, myDocuments) == S_OK) { + m_MyGamesDirectory = std::wstring(myDocuments) + L"\\My Games"; + } - m_MyGamesDirectory.assign(myDocuments).append(L"\\My Games"); - if (!FileExists(m_MyGamesDirectory.substr().append(L"/").append(file))) { - if (::SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_DEFAULT, myDocuments) != S_OK) { - m_MyGamesDirectory.assign(myDocuments).append(L"\\My Games"); + // b) if there is no directory there, look in the default directory + if (m_MyGamesDirectory.empty() + || !FileExists(m_MyGamesDirectory + L"\\" + file)) { + if (::SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_DEFAULT, myDocuments) == S_OK) { + std::wstring fromDefault = std::wstring(myDocuments) + L"\\My Games"; + if (FileExists(fromDefault + L"\\" + file)) { + m_MyGamesDirectory = fromDefault; + } } } - if (!FileExists(m_MyGamesDirectory.substr().append(L"/").append(file))) { - m_MyGamesDirectory.assign(getSpecialPath(L"Personal")).append(L"\\My Games"); + // c) finally, look in the registry. This is discouraged + if (m_MyGamesDirectory.empty() + || !FileExists(m_MyGamesDirectory + L"\\" + file)) { + std::wstring fromRegistry = getSpecialPath(L"Personal") + L"\\My Games"; + if (FileExists(fromRegistry + L"\\" + file)) { + m_MyGamesDirectory = fromRegistry; + } } } -bool GameInfo::identifyGame(const std::wstring &moDirectory, const std::wstring &searchPath) +bool GameInfo::identifyGame(const std::wstring &moDirectory, const std::wstring &moDataDirectory, const std::wstring &searchPath) { if (OblivionInfo::identifyGame(searchPath)) { - s_Instance = new OblivionInfo(moDirectory, searchPath); + s_Instance = new OblivionInfo(moDirectory, moDataDirectory, searchPath); } else if (Fallout3Info::identifyGame(searchPath)) { - s_Instance = new Fallout3Info(moDirectory, searchPath); + s_Instance = new Fallout3Info(moDirectory, moDataDirectory, searchPath); } else if (FalloutNVInfo::identifyGame(searchPath)) { - s_Instance = new FalloutNVInfo(moDirectory, searchPath); + s_Instance = new FalloutNVInfo(moDirectory, moDataDirectory, searchPath); } else if (SkyrimInfo::identifyGame(searchPath)) { - s_Instance = new SkyrimInfo(moDirectory, searchPath); + s_Instance = new SkyrimInfo(moDirectory, moDataDirectory, searchPath); } return s_Instance != NULL; } -bool GameInfo::init(const std::wstring &moDirectory, const std::wstring &gamePath) +bool GameInfo::init(const std::wstring &moDirectory, const std::wstring &moDataDirectory, const std::wstring &gamePath) { if (s_Instance == NULL) { if (gamePath.length() == 0) { // search upward in the directory until a recognized game-binary is found std::wstring searchPath(moDirectory); - while (!identifyGame(moDirectory, searchPath)) { + while (!identifyGame(moDirectory, moDataDirectory, searchPath)) { size_t lastSep = searchPath.find_last_of(L"/\\"); if (lastSep == std::string::npos) { return false; } searchPath.erase(lastSep); } - } else if (!identifyGame(moDirectory, gamePath)) { + } else if (!identifyGame(moDirectory, moDataDirectory, gamePath)) { return false; } } @@ -121,21 +138,21 @@ std::wstring GameInfo::getGameDirectory() const std::wstring GameInfo::getModsDir() const { std::wostringstream temp; - temp << m_OrganizerDirectory << L"\\mods"; + temp << m_OrganizerDataDirectory << L"\\mods"; return temp.str(); } std::wstring GameInfo::getProfilesDir() const { std::wostringstream temp; - temp << m_OrganizerDirectory << L"\\profiles"; + temp << m_OrganizerDataDirectory << L"\\profiles"; return temp.str(); } std::wstring GameInfo::getIniFilename() const { std::wostringstream temp; - temp << m_OrganizerDirectory << L"\\ModOrganizer.ini"; + temp << m_OrganizerDataDirectory << L"\\ModOrganizer.ini"; return temp.str(); } @@ -143,7 +160,7 @@ std::wstring GameInfo::getIniFilename() const std::wstring GameInfo::getDownloadDir() const { std::wostringstream temp; - temp << m_OrganizerDirectory << L"\\downloads"; + temp << m_OrganizerDataDirectory << L"\\downloads"; return temp.str(); } @@ -151,7 +168,7 @@ std::wstring GameInfo::getDownloadDir() const std::wstring GameInfo::getCacheDir() const { std::wostringstream temp; - temp << m_OrganizerDirectory << L"\\webcache"; + temp << m_OrganizerDataDirectory << L"\\webcache"; return temp.str(); } @@ -159,7 +176,7 @@ std::wstring GameInfo::getCacheDir() const std::wstring GameInfo::getOverwriteDir() const { std::wostringstream temp; - temp << m_OrganizerDirectory << "\\overwrite"; + temp << m_OrganizerDataDirectory << "\\overwrite"; return temp.str(); } @@ -167,7 +184,7 @@ std::wstring GameInfo::getOverwriteDir() const std::wstring GameInfo::getLogDir() const { std::wostringstream temp; - temp << m_OrganizerDirectory << "\\logs"; + temp << m_OrganizerDataDirectory << "\\logs"; return temp.str(); } @@ -201,17 +218,25 @@ std::vector GameInfo::getSteamVariants() const std::wstring GameInfo::getLocalAppFolder() const { - return getSpecialPath(L"Local AppData"); + wchar_t localAppFolder[MAX_PATH]; + memset(localAppFolder, '\0', MAX_PATH); + + if (::SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, localAppFolder) == S_OK) { + return localAppFolder; + } else { + // fallback: try the registry + return getSpecialPath(L"Local AppData"); + } } std::wstring GameInfo::getSpecialPath(LPCWSTR name) const { HKEY key; - LONG errorcode = ::RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", - 0, KEY_QUERY_VALUE, &key); + LONG errorcode = ::RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", + 0, KEY_QUERY_VALUE, &key); if (errorcode != ERROR_SUCCESS) { - throw windows_error("failed to look up special folder", errorcode); + throw windows_error("failed to look up special folder (path)", errorcode); } WCHAR temp[MAX_PATH]; @@ -219,7 +244,7 @@ std::wstring GameInfo::getSpecialPath(LPCWSTR name) const errorcode = ::RegQueryValueExW(key, name, NULL, NULL, (LPBYTE)temp, &bufferSize); if (errorcode != ERROR_SUCCESS) { - throw windows_error("failed to look up special folder", errorcode); + throw windows_error((boost::format("failed to look up special folder (%1%)") % ToString(name, true)).str(), errorcode); } WCHAR temp2[MAX_PATH]; -- cgit v1.3.1