From 6f268c09784f30364aa84720a70756f9b7264ce6 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 27 Mar 2022 12:09:46 +0200 Subject: [PATCH] Check if ImGui config file exist before attempting to parse it --- r5dev/thirdparty/imgui/src/imgui_utility.cpp | 50 +++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/r5dev/thirdparty/imgui/src/imgui_utility.cpp b/r5dev/thirdparty/imgui/src/imgui_utility.cpp index 4f9219a4..789e46c3 100644 --- a/r5dev/thirdparty/imgui/src/imgui_utility.cpp +++ b/r5dev/thirdparty/imgui/src/imgui_utility.cpp @@ -44,34 +44,38 @@ void Strtrim(char* s) void ImGuiConfig::Load() { - std::filesystem::path fsPath = std::filesystem::current_path() /= "platform\\imgui.json"; // Get current path + imgui.json - DevMsg(eDLL_T::MS, "Loading ImGui config file '%s'\n", fsPath.string().c_str()); + std::filesystem::path fsPath = "platform\\imgui.json"; + DevMsg(eDLL_T::MS, "Loading ImGui config file '%s'\n", fsPath.relative_path().string().c_str()); - nlohmann::json jsIn; - try + if (std::filesystem::exists(fsPath)) { - std::ifstream configFile(fsPath, std::ios::binary); // Parse config file. - configFile >> jsIn; - configFile.close(); - - if (!jsIn.is_null()) + try { - if (!jsIn["config"].is_null()) - { - // IConsole - IConsole_Config.m_nBind0 = jsIn["config"]["IConsole"]["bind0"].get(); - IConsole_Config.m_nBind1 = jsIn["config"]["IConsole"]["bind1"].get(); + nlohmann::json jsIn; + std::ifstream configFile(fsPath, std::ios::binary); // Parse config file. - // IBrowser - IBrowser_Config.m_nBind0 = jsIn["config"]["IBrowser"]["bind0"].get(); - IBrowser_Config.m_nBind1 = jsIn["config"]["IBrowser"]["bind1"].get(); + configFile >> jsIn; + configFile.close(); + + if (!jsIn.is_null()) + { + if (!jsIn["config"].is_null()) + { + // IConsole + IConsole_Config.m_nBind0 = jsIn["config"]["IConsole"]["bind0"].get(); + IConsole_Config.m_nBind1 = jsIn["config"]["IConsole"]["bind1"].get(); + + // IBrowser + IBrowser_Config.m_nBind0 = jsIn["config"]["IBrowser"]["bind0"].get(); + IBrowser_Config.m_nBind1 = jsIn["config"]["IBrowser"]["bind1"].get(); + } } } - } - catch (const std::exception& ex) - { - DevMsg(eDLL_T::MS, "ImGui config file '%s' not found\n Changing the settings in the console or server browser options re-create's it\n Exception: '%s'\n", fsPath.string().c_str(), ex.what()); - return; + catch (const std::exception& ex) + { + Warning(eDLL_T::MS, "Exception while parsing ImGui config file:\n%s\n", ex.what()); + return; + } } } @@ -87,7 +91,7 @@ void ImGuiConfig::Save() jsOut["config"]["IBrowser"]["bind0"] = IBrowser_Config.m_nBind0; jsOut["config"]["IBrowser"]["bind1"] = IBrowser_Config.m_nBind1; - std::filesystem::path fsPath = std::filesystem::current_path() /= "platform\\imgui.json"; // Get current path + imgui.json + std::filesystem::path fsPath = "platform\\imgui.json"; DevMsg(eDLL_T::MS, "Saving ImGui config file '%s'\n", fsPath.string().c_str()); std::ofstream outFile(fsPath, std::ios::out | std::ios::trunc); // Write config file.