Skip to content

Commit

Permalink
chore: catch filesystem errors
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlum committed Nov 23, 2024
1 parent 60da1c0 commit 206186c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class State
spdlog::level::level_enum logLevel = spdlog::level::info;
std::string shaderDefinesString = "";
std::vector<std::pair<std::string, std::string>> 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";
Expand Down

0 comments on commit 206186c

Please sign in to comment.