From 206186c382d99738d11ca37ae7839c9bfbb163da Mon Sep 17 00:00:00 2001 From: Tim <15017472+doodlum@users.noreply.github.com> Date: Sat, 23 Nov 2024 23:11:10 +0000 Subject: [PATCH] chore: catch filesystem errors --- src/State.cpp | 18 ++++++++++++++---- src/State.h | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/State.cpp b/src/State.cpp index df1cb6086..74f1099cb 100644 --- a/src/State.cpp +++ b/src/State.cpp @@ -136,13 +136,18 @@ static const std::string& GetConfigPath(State::ConfigMode a_configMode) void State::Load(ConfigMode a_configMode, bool a_allowReload) { - std::filesystem::create_directories(basePath); - ConfigMode configMode = a_configMode; auto& shaderCache = SIE::ShaderCache::Instance(); json settings; bool errorDetected = false; + try { + std::filesystem::create_directories(folderPath); + } catch (const std::filesystem::filesystem_error& e) { + logger::warn("Error trying to create directory({}) : {}\n", folderPath, e.what()); + errorDetected = true; + } + // Attempt to load the config file auto tryLoadConfig = [&](const std::string& path) { std::ifstream i(path); @@ -310,12 +315,17 @@ void State::Load(ConfigMode a_configMode, bool a_allowReload) void State::Save(ConfigMode a_configMode) { - std::filesystem::create_directories(basePath); - const auto& shaderCache = SIE::ShaderCache::Instance(); std::string configPath = GetConfigPath(a_configMode); std::ofstream o{ configPath }; + try { + std::filesystem::create_directories(folderPath); + } catch (const std::filesystem::filesystem_error& e) { + logger::warn("Error trying to create directory({}) : {}\n", folderPath, e.what()); + return; + } + // Check if the file opened successfully if (!o.is_open()) { logger::warn("Failed to open config file for saving: {}", configPath); diff --git a/src/State.h b/src/State.h index c785c8981..645ed7e1e 100644 --- a/src/State.h +++ b/src/State.h @@ -33,7 +33,7 @@ class State spdlog::level::level_enum logLevel = spdlog::level::info; std::string shaderDefinesString = ""; std::vector> shaderDefines{}; // data structure to parse string into; needed to avoid dangling pointers - const std::string basePath = "Data\\SKSE\\Plugins\\CommunityShaders"; + const std::string folderPath = "Data\\SKSE\\Plugins\\CommunityShaders"; const std::string testConfigPath = "Data\\SKSE\\Plugins\\CommunityShaders\\SettingsTest.json"; const std::string userConfigPath = "Data\\SKSE\\Plugins\\CommunityShaders\\SettingsUser.json"; const std::string defaultConfigPath = "Data\\SKSE\\Plugins\\CommunityShaders\\SettingsDefault.json";