From e485d457a6b4f33e8a2a63e51e09aada63a59c83 Mon Sep 17 00:00:00 2001 From: Sam39 Date: Mon, 22 Aug 2022 11:15:08 +0300 Subject: [PATCH] add specify a config file as argument --- include/Launcher.h | 6 ++--- src/Config.cpp | 26 ++++++++++---------- src/gui/Gui.cpp | 60 +++++++++++++++++++++++++++++----------------- src/main.cpp | 5 ++-- 4 files changed, 56 insertions(+), 41 deletions(-) diff --git a/include/Launcher.h b/include/Launcher.h index f33424f..ba35795 100644 --- a/include/Launcher.h +++ b/include/Launcher.h @@ -98,9 +98,9 @@ class ShutdownException : public std::runtime_error { }; struct UIData { - static inline std::string GamePath, ProfilePath, CachePath, Build, PublicKey, UserRole, Username, GameVer; - static inline bool LoginAuth{false}, Console{false}; + static inline std::string GamePath, ProfilePath, CachePath, Build, PublicKey, UserRole, Username, GameVer, ConfigPath; + static inline bool LoginAuth{false}, Console{false}, UI; }; void UpdateKey(const std::string& newKey); -void entry(); +int entry(); diff --git a/src/Config.cpp b/src/Config.cpp index 74ffa3f..b343d38 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -8,8 +8,8 @@ #include "Logger.h" void Launcher::LoadConfig() { - if (fs::exists("Launcher.toml")) { - toml::parse_result config = toml::parse_file("Launcher.toml"); + if (fs::exists(UIData::ConfigPath)) { + toml::parse_result config = toml::parse_file(UIData::ConfigPath); auto ui = config["UI"]; auto build = config["Build"]; auto GamePath = config["GamePath"]; @@ -26,24 +26,24 @@ void Launcher::LoadConfig() { } else LOG(ERROR) << "Failed to get 'Build' string from config"; if (GamePath.is_string()) { - if(!GamePath.as_string()->get().empty()) { + if (!GamePath.as_string()->get().empty()) { BeamRoot = GamePath.as_string()->get(); } else throw ShutdownException("GamePath cannot be empty"); - } - else LOG(ERROR) << "Failed to get 'GamePath' string from config"; + } else LOG(ERROR) << "Failed to get 'GamePath' string from config"; if (ProfilePath.is_string()) { - auto GameVer = VersionParser(UIData::GameVer).split; + if (!UIData::GameVer.empty()) { + auto GameVer = VersionParser(UIData::GameVer).split; - if (!ProfilePath.as_string()->get().empty()) { - BeamUserPath = fs::path(ProfilePath.as_string()->get())/(GameVer[0] + '.' + GameVer[1]); - MPUserPath = BeamUserPath/"mods"/"multiplayer"; - } else throw ShutdownException("ProfilePath cannot be empty"); - } - else LOG(ERROR) << "Failed to get 'ProfilePath' string from config"; + if (!ProfilePath.as_string()->get().empty()) { + BeamUserPath = fs::path(ProfilePath.as_string()->get()) / (GameVer[0] + '.' + GameVer[1]); + MPUserPath = BeamUserPath / "mods" / "multiplayer"; + } else throw ShutdownException("ProfilePath cannot be empty"); + } else throw ShutdownException ("Check game path in config"); + } else LOG(ERROR) << "Failed to get 'ProfilePath' string from config"; if (CachePath.is_string()) { - if(!CachePath.as_string()->get().empty()) { + if (!CachePath.as_string()->get().empty()) { LauncherCache = CachePath.as_string()->get(); } else throw ShutdownException("CachePath cannot be empty"); } else LOG(ERROR) << "Failed to get 'CachePath' string from config"; diff --git a/src/gui/Gui.cpp b/src/gui/Gui.cpp index abf5373..f22b15d 100644 --- a/src/gui/Gui.cpp +++ b/src/gui/Gui.cpp @@ -198,11 +198,11 @@ void MySettingsFrame::UpdateCacheDirectory(const std::string& path) { /////////// 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"); + if (fs::exists(UIData::ConfigPath)) { + toml::parse_result config = toml::parse_file(UIData::ConfigPath); config.insert_or_assign(key, value); - std::ofstream tml("Launcher.toml"); + std::ofstream tml(UIData::ConfigPath); if (tml.is_open()) { tml << config; tml.close(); @@ -272,9 +272,9 @@ std::string ResetCache() { /////////// Load Config Function /////////// void LoadConfig() { - if (fs::exists("Launcher.toml")) { - toml::parse_result config = toml::parse_file("Launcher.toml"); - auto ui = config["UI"]; + if (fs::exists(UIData::ConfigPath)) { + toml::parse_result config = toml::parse_file(UIData::ConfigPath); + auto UI = config["UI"]; auto Build = config["Build"]; auto GamePath = config["GamePath"]; auto ProfilePath = config["ProfilePath"]; @@ -301,8 +301,12 @@ void LoadConfig() { UIData::Build = Build.as_string()->get(); } else wxMessageBox("Unable to retrieve build state!", "Error"); + if (UI.is_boolean()) { + UIData::UI = UI.as_boolean()->get(); + } else wxMessageBox("Unable to retrieve UI state!", "Error"); + } else { - std::ofstream tml("Launcher.toml"); + std::ofstream tml(UIData::ConfigPath); if (tml.is_open()) { tml << "UI = true\n" "Build = 'Default'\n" @@ -415,8 +419,6 @@ bool MyApp::OnInit() { } } - Log::Init(); - LoadConfig(); CheckKey(); WindowsConsole(UIData::Console); @@ -963,20 +965,34 @@ void MySettingsFrame::OnResetCache(wxCommandEvent& event) { /////////// MAIN FUNCTION /////////// int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { + std::string ConfigPath(lpCmdLine); + if (!ConfigPath.empty()) { + UIData::ConfigPath = ConfigPath; + } else UIData::ConfigPath = "Launcher.toml"; wxDisableAsserts(); wxLog::SetLogLevel(wxLOG_Info); - int result = 0; - try { - new MyApp(); - result = wxEntry(hInstance, hPrevInstance, lpCmdLine, nShowCmd); - if (Launcher::EntryThread.joinable()) - Launcher::EntryThread.join(); - if (MyMainFrame::UpdateThread.joinable()) - MyMainFrame::UpdateThread.join(); - } catch (const ShutdownException& e) { - LOG(INFO) << "Launcher shutting down with reason: " << e.what(); - } catch (const std::exception& e) { - LOG(FATAL) << e.what(); + Log::Init(); + LoadConfig(); + UIData::GameVer = jsonRead(); + + if (UIData::UI) { + int result = 0; + try { + new MyApp(); + result = wxEntry(hInstance, hPrevInstance, lpCmdLine, nShowCmd); + if (Launcher::EntryThread.joinable()) + Launcher::EntryThread.join(); + if (MyMainFrame::UpdateThread.joinable()) + MyMainFrame::UpdateThread.join(); + } catch (const ShutdownException& e) { + LOG(INFO) << "Launcher shutting down with reason: " << e.what(); + } catch (const std::exception& e) { + LOG(FATAL) << e.what(); + } + return result; + } else { + WindowsConsole(true); + Log::ConsoleOutput(true); + return entry(); } - return result; } diff --git a/src/main.cpp b/src/main.cpp index 167bfb8..6a8cab4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,8 +6,7 @@ #include "Launcher.h" #include "Logger.h" -void entry() { - +int entry() { try { Launcher launcher; launcher.RunDiscordRPC(); @@ -25,5 +24,5 @@ void entry() { } std::this_thread::sleep_for(std::chrono::seconds(2)); Launcher::setExit(true); - + return 0; }