diff --git a/components/files/linuxpath.cpp b/components/files/linuxpath.cpp index 8466f0222e..a1d1168d9a 100644 --- a/components/files/linuxpath.cpp +++ b/components/files/linuxpath.cpp @@ -28,6 +28,7 @@ #include #include #include +#include /** * \namespace Files @@ -154,10 +155,75 @@ boost::filesystem::path LinuxPath::getLocalDataPath() const return boost::filesystem::path("./data/"); } - boost::filesystem::path LinuxPath::getInstallPath() const { - return boost::filesystem::path("./"); + char *homePath = getenv("HOME"); + if(!homePath) + { + return boost::filesystem::path(""); + } + + boost::filesystem::path wineDefaultRegistry(homePath); + wineDefaultRegistry /= ".wine/system.reg"; + + boost::filesystem::path wineDriveC(homePath); + wineDriveC /= ".wine/drive_c"; + + boost::filesystem::file_status fileStatus = boost::filesystem::status(wineDefaultRegistry); + boost::filesystem::file_status dirStatus = boost::filesystem::status(wineDriveC); + if(!boost::filesystem::is_regular_file(fileStatus) || !boost::filesystem::is_directory(dirStatus)) + { + return boost::filesystem::path(""); + } + + + boost::filesystem::ifstream file(wineDefaultRegistry); + bool isRegEntry = false; + std::string line; + + while (std::getline(file, line)) + { + if(!line.empty() && line[0] == '[') // we found an entry + { + std::string regkey = line.substr(1, line.find(']')-1); + if( regkey.compare("SOFTWARE\\\\Wow6432Node\\\\Bethesda Softworks\\\\Morrowind") == 0 + || regkey.compare("SOFTWARE\\\\Bethesda Softworks\\\\Morrowind") == 0 ) + { + isRegEntry = true; + } + } + else if(isRegEntry) + { + if(line.empty() || line[0] != '"') // empty line means new registry key + { + break; + } + std::string key = line.substr(1, line.find('"', 1)-1); + if(key.compare("Installed Path") == 0) { + std::string::size_type pos, startPos; + + startPos = line.find('=')+2; + std::string installPath = line.substr(startPos, line.find('"', startPos+1)-startPos); + installPath.replace(0, 2, wineDriveC.string()); + + pos = -1; + do + { + pos = installPath.find("\\\\", pos+1); + if(pos == std::string::npos) + { + break; + } + + installPath.replace(pos, 2, "/"); + } while(true); + + return boost::filesystem::path(installPath); + } + } + } + + return boost::filesystem::path(""); } } /* namespace Files */ diff --git a/components/files/linuxpath.hpp b/components/files/linuxpath.hpp index 51be6242cc..71f45ae32b 100644 --- a/components/files/linuxpath.hpp +++ b/components/files/linuxpath.hpp @@ -81,6 +81,11 @@ struct LinuxPath */ boost::filesystem::path getLocalDataPath() const; + /** + * \brief Gets the path of the installed Morrowind version if there is one. + * + * \return boost::filesystem::path + */ boost::filesystem::path getInstallPath() const; }; diff --git a/components/files/windowspath.cpp b/components/files/windowspath.cpp index a54f800d40..9bb66a3cb3 100644 --- a/components/files/windowspath.cpp +++ b/components/files/windowspath.cpp @@ -5,7 +5,10 @@ #include #include -#include +#include +#include + +#pragma comment(lib, "Shlwapi.lib") namespace Files { @@ -76,9 +79,36 @@ boost::filesystem::path WindowsPath::getLocalDataPath() const return boost::filesystem::path("./data/"); } -boost::filesystem::path WindowsPath::getInstallPath() const; +boost::filesystem::path WindowsPath::getInstallPath() const { - return boost::filesystem::path("./"); + boost::filesystem::path installPath(""); + + HKEY hKey; + + BOOL f64 = FALSE; + LPCTSTR regkey; + if (IsWow64Process(GetCurrentProcess(), &f64) && f64) + { + regkey = "SOFTWARE\\Wow6432Node\\Bethesda Softworks\\Morrowind"; + } + else + { + regkey = "SOFTWARE\\Bethesda Softworks\\Morrowind"; + } + + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(regkey), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) + { + //Key existed, let's try to read the install dir + std::vector buf(512); + int len = 512; + + if (RegQueryValueEx(hKey, TEXT("Installed Path"), NULL, NULL, (LPBYTE)&buf[0], (LPDWORD)&len) == ERROR_SUCCESS) + { + installPath = &buf[0]; + } + } + + return installPath; } } /* namespace Files */ diff --git a/components/files/windowspath.hpp b/components/files/windowspath.hpp index 95af3ea902..0240de257f 100644 --- a/components/files/windowspath.hpp +++ b/components/files/windowspath.hpp @@ -82,6 +82,11 @@ struct WindowsPath */ boost::filesystem::path getLocalDataPath() const; + /** + * \brief Gets the path of the installed Morrowind version if there is one. + * + * \return boost::filesystem::path + */ boost::filesystem::path getInstallPath() const; };