From 310b2094c5d4c52487bddfcd4bda6356e6d3c917 Mon Sep 17 00:00:00 2001 From: Sam39 Date: Fri, 12 Aug 2022 14:45:46 +0300 Subject: [PATCH] add path detection add game path auto detection add profile path auto detection add cache path reset --- include/Launcher.h | 9 ++-- src/Launcher.cpp | 2 +- src/gui/Gui.cpp | 125 +++++++++++++++++++++++++++++++-------------- 3 files changed, 94 insertions(+), 42 deletions(-) diff --git a/include/Launcher.h b/include/Launcher.h index aaef043..a79dadd 100644 --- a/include/Launcher.h +++ b/include/Launcher.h @@ -18,6 +18,8 @@ struct VersionParser { std::vector split; std::vector data; }; +struct HKEY__; +typedef HKEY__ *HKEY; class Launcher { public: // constructors @@ -35,6 +37,7 @@ class Launcher { void LaunchGame(); void CheckKey(); void SetupMOD(); + static std::string QueryValue(HKEY& hKey, const char* Name); public: // Getters and Setters void setDiscordMessage(const std::string& message); @@ -82,9 +85,9 @@ class Launcher { std::string Version{"2.0"}; Server ServerHandler{this}; std::string TargetBuild{"default"}; - std::string GameConfigPath{""}; - std::string ProfileConfigPath{""}; - std::string CacheConfigPath{""}; + std::string GameConfigPath{}; + std::string ProfileConfigPath{}; + std::string CacheConfigPath{}; static inline std::atomic Shutdown{false}, Exit{false}; std::string FullVersion{Version + ".99"}; VersionParser SupportedVersion{"0.25.4.0"}; diff --git a/src/Launcher.cpp b/src/Launcher.cpp index f53f3fc..467f416 100644 --- a/src/Launcher.cpp +++ b/src/Launcher.cpp @@ -176,7 +176,7 @@ void Launcher::SendIPC(const std::string& Data, bool core) { } } -std::string QueryValue(HKEY& hKey, const char* Name) { +std::string Launcher::QueryValue(HKEY& hKey, const char* Name) { DWORD keySize; BYTE buffer[16384]; if (RegQueryValueExA(hKey, Name, nullptr, nullptr, buffer, &keySize) == diff --git a/src/gui/Gui.cpp b/src/gui/Gui.cpp index 4b95b86..8bdb1fc 100644 --- a/src/gui/Gui.cpp +++ b/src/gui/Gui.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include "Launcher.h" #include "Logger.h" #include @@ -72,6 +74,9 @@ class MySettingsFrame : public wxFrame { void OnClickConsole(wxCommandEvent& event); void OnChangedGameDir (wxFileDirPickerEvent& event); void OnChangedBuild (wxCommandEvent& event); + void OnAutoDetectGame(wxCommandEvent& event); + void OnAutoDetectProfile(wxCommandEvent& event); + void OnResetCache(wxCommandEvent& event); wxDECLARE_EVENT_TABLE(); }; @@ -106,6 +111,9 @@ wxBEGIN_EVENT_TABLE(MySettingsFrame, wxFrame) EVT_CHECKBOX(45, MySettingsFrame::OnClickConsole) EVT_DIRPICKER_CHANGED(46, MySettingsFrame::OnChangedGameDir) EVT_CHOICE(47, MySettingsFrame::OnChangedBuild) + EVT_BUTTON(10, MySettingsFrame::OnAutoDetectGame) + EVT_BUTTON(11, MySettingsFrame::OnAutoDetectProfile) + EVT_BUTTON(12, MySettingsFrame::OnResetCache) wxEND_EVENT_TABLE() /////////// OnInit function to show frame /////////// @@ -152,7 +160,6 @@ bool isSignedIn () { return false; } - /////////// Load Config function /////////// void MySettingsFrame:: LoadConfig() { if (fs::exists("Launcher.toml")) { @@ -387,18 +394,17 @@ MySettingsFrame::MySettingsFrame() : ctrlGameDirectory = new wxDirPickerCtrl (panel, 46, wxEmptyString, wxT("Game Directory"), wxPoint(130, 100), wxSize(300,-1)); ctrlGameDirectory->SetLabel("GamePath"); MySettingsFrame::SetFocus(); - auto btnDetectGameDirectory = new wxButton(panel, 40, wxT("Detect"), wxPoint(185,140), wxSize(90, 25)); + auto btnDetectGameDirectory = new wxButton(panel, 10, wxT("Detect"), wxPoint(185,140), wxSize(90, 25)); auto* txtProfileDirectory = new wxStaticText(panel, wxID_ANY, wxT("Profile Directory: "), wxPoint(30, 200)); ctrlProfileDirectory = new wxDirPickerCtrl (panel, 46, wxEmptyString, wxT("Profile Directory"), wxPoint(130, 200), wxSize(300,-1)); ctrlProfileDirectory->SetLabel("ProfilePath"); - auto btnDetectProfileDirectory = new wxButton(panel, 40, wxT("Detect"), wxPoint(185,240), wxSize(90, 25)); + auto btnDetectProfileDirectory = new wxButton(panel, 11, wxT("Detect"), wxPoint(185,240), wxSize(90, 25)); auto* txtCacheDirectory = new wxStaticText(panel, wxID_ANY, wxT("Cache Directory: "), wxPoint(30, 300)); ctrlCacheDirectory = new wxDirPickerCtrl (panel, 46, wxEmptyString, wxT("Cache Directory"), wxPoint(130, 300), wxSize(300,-1)); ctrlCacheDirectory->SetLabel("CachePath"); - auto btnCacheDirectory = new wxButton(panel, 40, wxT("Detect"), wxPoint(185,340), wxSize(90, 25)); - + auto btnCacheDirectory = new wxButton(panel, 12, wxT("Reset"), wxPoint(185,340), wxSize(90, 25)); auto* txtBuild = new wxStaticText(panel, wxID_ANY, wxT("Build: "), wxPoint(30, 400)); wxArrayString BuildChoices; @@ -427,6 +433,21 @@ MySettingsFrame::MySettingsFrame() : } } +/////////// UpdateConfig function /////////// +template +void UpdateConfig (const std::string& key, ValueType&& value) { + if (fs::exists("Launcher.toml")) { + toml::parse_result config = toml::parse_file("Launcher.toml"); + config.insert_or_assign(key, value); + + std::ofstream tml("Launcher.toml"); + if (tml.is_open()) { + tml << config; + tml.close(); + } else wxMessageBox("Failed to modify config file", "Error"); + } +} + /////////// OnClick Account Event /////////// void MyMainFrame::OnClickAccount(wxCommandEvent& event WXUNUSED(event)) { auto* AccountFrame = new MyAccountFrame(); @@ -506,52 +527,80 @@ void MyMainFrame::OnClickLaunch(wxCommandEvent& event WXUNUSED(event)) { void MySettingsFrame::OnClickConsole(wxCommandEvent& event) { WindowsConsole(checkConsole->IsChecked()); bool status = event.IsChecked(); - - if (fs::exists("Launcher.toml")) { - toml::parse_result config = toml::parse_file("Launcher.toml"); - config.insert_or_assign("Console", status); - - std::ofstream tml("Launcher.toml"); - if (tml.is_open()) { - tml << config; - tml.close(); - } else wxMessageBox("Failed to modify config file", "Error"); - } + UpdateConfig("Console", status); } - /////////// OnChanged Game Path Event /////////// void MySettingsFrame::OnChangedGameDir(wxFileDirPickerEvent& event) { std::string NewPath = event.GetPath().utf8_string(); - std::string key = reinterpret_cast (event.GetEventObject())->GetLabel(); - - if (fs::exists("Launcher.toml")) { - toml::parse_result config = toml::parse_file("Launcher.toml"); - config.insert_or_assign(key, NewPath); - - std::ofstream tml("Launcher.toml"); - if (tml.is_open()) { - tml << config; - tml.close(); - } else wxMessageBox("Failed to modify config file", "Error"); - } + UpdateConfig(key, NewPath); } /////////// OnChanged Build Event /////////// void MySettingsFrame::OnChangedBuild(wxCommandEvent& event) { std::string key = reinterpret_cast (event.GetEventObject())->GetString(event.GetSelection()); + UpdateConfig("Build", key); +} - if (fs::exists("Launcher.toml")) { - toml::parse_result config = toml::parse_file("Launcher.toml"); - config.insert_or_assign("Build", key); - - std::ofstream tml("Launcher.toml"); - if (tml.is_open()) { - tml << config; - tml.close(); - } else wxMessageBox("Failed to modify config file", "Error"); +/////////// AutoDetect Game Function /////////// +void MySettingsFrame::OnAutoDetectGame (wxCommandEvent& event) { + HKEY BeamNG; + std::string GamePath; + LONG RegRes = + RegOpenKeyExA(HKEY_CURRENT_USER, R"(Software\BeamNG\BeamNG.drive)", 0, + KEY_READ, &BeamNG); + if (RegRes == ERROR_SUCCESS) { + GamePath = Launcher::QueryValue(BeamNG, "rootpath"); + RegCloseKey(BeamNG); } + if (!GamePath.empty()) { + if (GamePath.ends_with('\\')) GamePath.pop_back(); + UpdateConfig("GamePath", GamePath); + ctrlGameDirectory->SetPath(GamePath); + } + else + wxMessageBox("Please launch the game at least once, failed to read registry key Software\\BeamNG\\BeamNG.drive", "Error"); +} + +/////////// AutoDetect Profile Function /////////// +void MySettingsFrame::OnAutoDetectProfile(wxCommandEvent& event){ + HKEY BeamNG; + std::string ProfilePath; + LONG RegRes = + RegOpenKeyExA(HKEY_CURRENT_USER, R"(Software\BeamNG\BeamNG.drive)", 0, + KEY_READ, &BeamNG); + if (RegRes == ERROR_SUCCESS) { + ProfilePath = Launcher::QueryValue(BeamNG, "userpath_override"); + RegCloseKey(BeamNG); + } + if (ProfilePath.empty()) { + PWSTR folderPath = nullptr; + HRESULT hr = + SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &folderPath); + + if (!SUCCEEDED(hr)) { + wxMessageBox( + "Please launch the game at least once, failed to read registry key Software\\BeamNG\\BeamNG.drive", + "Error"); + return; + } + else { + _bstr_t bstrPath(folderPath); + std::string Path((char*)bstrPath); + CoTaskMemFree(folderPath); + ProfilePath = Path + "\\BeamNG.drive"; + } + } + UpdateConfig("ProfilePath", ProfilePath); + ctrlProfileDirectory->SetPath(ProfilePath); +} + +/////////// Reset Cache Function /////////// +void MySettingsFrame::OnResetCache(wxCommandEvent& event) { + std::string CachePath = fs::current_path().append("Resources").string(); + UpdateConfig("CachePath", CachePath); + ctrlCacheDirectory->SetPath(CachePath); } /////////// MAIN FUNCTION ///////////