diff --git a/include/Launcher.h b/include/Launcher.h index cdf4329..ac69923 100644 --- a/include/Launcher.h +++ b/include/Launcher.h @@ -33,6 +33,7 @@ public: //Getters const std::string& getUserRole(); const std::string& getVersion(); private: //functions + std::string GetLocalAppdata(); void AdminRelaunch(); void RichPresence(); void WindowsInit(); diff --git a/src/Launcher.cpp b/src/Launcher.cpp index e246812..1519f7d 100644 --- a/src/Launcher.cpp +++ b/src/Launcher.cpp @@ -46,16 +46,28 @@ void Launcher::WindowsInit() { SetConsoleTitleA(("BeamMP Launcher v" + FullVersion).c_str()); } -std::string QueryValue(HKEY& BeamNG, const char* Name) { +std::string QueryValue(HKEY& hKey, const char* Name) { DWORD keySize; - BYTE buffer[1024]; - ZeroMemory(buffer, 1024); - if(RegQueryValueExA(BeamNG, Name, nullptr, nullptr, buffer, &keySize) == ERROR_SUCCESS) { + BYTE buffer[16384]; + if(RegQueryValueExA(hKey, Name, nullptr, nullptr, buffer, &keySize) == ERROR_SUCCESS) { return {(char*)buffer, keySize}; } return {}; } - +std::string Launcher::GetLocalAppdata() { + HKEY Folders; + LONG RegRes = RegOpenKeyExA(HKEY_CURRENT_USER, R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders)", 0, KEY_READ, &Folders); + if(RegRes == ERROR_SUCCESS) { + std::string Path = QueryValue(Folders, "Local AppData"); + if(!Path.empty()) { + Path += "\\BeamNG.drive\\"; + VersionParser GameVer(BeamVersion); + Path += std::to_string(GameVer.data[0]) + '.' + std::to_string(GameVer.data[1]) + '\\'; + return Path; + } + } + return {}; +} void Launcher::QueryRegistry() { HKEY BeamNG; LONG RegRes = RegOpenKeyExA(HKEY_CURRENT_USER, R"(Software\BeamNG\BeamNG.drive)", 0, KEY_READ, &BeamNG); @@ -63,9 +75,11 @@ void Launcher::QueryRegistry() { BeamRoot = QueryValue(BeamNG, "rootpath"); BeamVersion = QueryValue(BeamNG, "version"); BeamUserPath = QueryValue(BeamNG, "userpath_override"); - //get shell folders for appdata dir RegCloseKey(BeamNG); - if(!BeamRoot.empty() && !BeamVersion.empty())return; + if(BeamUserPath.empty() && !BeamVersion.empty()) { + BeamUserPath = GetLocalAppdata(); + } + if(!BeamRoot.empty() && !BeamVersion.empty() && !BeamUserPath.empty())return; } LOG(FATAL) << "Please launch the game at least once, failed to read registry key Software\\BeamNG\\BeamNG.drive"; throw ShutdownException("Fatal Error"); diff --git a/src/main.cpp b/src/main.cpp index 99ed201..16a20db 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,9 +12,9 @@ int main(int argc, char* argv[]) { launcher.RunDiscordRPC(); launcher.LoadConfig(); launcher.CheckKey(); + launcher.QueryRegistry(); launcher.LaunchGame(); //launcher.WaitForGame(); - launcher.QueryRegistry(); //UI call