Skip to content

Commit

Permalink
Add button to clean up and save current preset file
Browse files Browse the repository at this point in the history
  • Loading branch information
crosire committed Aug 28, 2021
1 parent 2746a1f commit 8da20ef
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
25 changes: 13 additions & 12 deletions source/ini_file.cpp
Expand Up @@ -14,23 +14,21 @@ ini_file::ini_file(const std::filesystem::path &path) : _path(path)
{
load();
}
ini_file::~ini_file()
{
save();
}

void ini_file::load()
{
std::error_code ec;
const std::filesystem::file_time_type modified_at = std::filesystem::last_write_time(_path, ec);
if (ec || _modified_at >= modified_at)
return; // Skip loading if there was an error (e.g. file does not exist) or there was no modification to the file since it was last loaded
if (!ec && _modified_at >= modified_at)
return; // Skip loading if there was no modification to the file since it was last loaded

// Clear when file does not exist too
_sections.clear();

std::ifstream file;
if (file.open(_path); !file)
return;

_sections.clear();
_modified = false;
_modified_at = modified_at;

Expand Down Expand Up @@ -106,7 +104,7 @@ bool ini_file::save()
std::error_code ec;
const std::filesystem::file_time_type modified_at = std::filesystem::last_write_time(_path, ec);
if (!ec && modified_at >= _modified_at)
return true; // File exists and was modified on disk and therefore may have different data, so cannot save
return false; // File exists and was modified on disk and therefore may have different data, so cannot save

std::stringstream data;
std::vector<std::string> section_names, key_names;
Expand Down Expand Up @@ -210,10 +208,13 @@ bool ini_file::flush_cache(const std::filesystem::path &path)
ini_file &ini_file::load_cache(const std::filesystem::path &path)
{
const auto it = g_ini_cache.try_emplace(path, path);
if (it.second || (std::filesystem::file_time_type::clock::now() - it.first->second._modified_at) < std::chrono::seconds(1))
return it.first->second; // Don't need to reload file when it was just loaded or there are still modifications pending
else
return it.first->second.load(), it.first->second;
std::pair<const std::wstring, ini_file> &file = *it.first;

// Don't reload file when it was just loaded or there are still modifications pending
if (!it.second && !file.second._modified)
file.second.load();

return file.second;
}

ini_file & reshade::global_config()
Expand Down
1 change: 0 additions & 1 deletion source/ini_file.hpp
Expand Up @@ -34,7 +34,6 @@ class ini_file
/// </summary>
/// <param name="path">The path to the INI file to access.</param>
explicit ini_file(const std::filesystem::path &path);
~ini_file();

/// <summary>
/// Gets the path to this INI file.
Expand Down
21 changes: 18 additions & 3 deletions source/runtime_gui.cpp
Expand Up @@ -976,7 +976,7 @@ void reshade::runtime::draw_gui_home()

const float button_size = ImGui::GetFrameHeight();
const float button_spacing = _imgui_context->Style.ItemInnerSpacing.x;
const float browse_button_width = ImGui::GetWindowContentRegionWidth() - (button_spacing + button_size) * 3;
const float browse_button_width = ImGui::GetWindowContentRegionWidth() - (button_spacing + button_size) * 4;

bool reload_preset = false;

Expand Down Expand Up @@ -1006,21 +1006,34 @@ void reshade::runtime::draw_gui_home()
}
ImGui::PopStyleVar();

ImGui::SameLine(0, button_spacing);
if (ImGui::ButtonEx(ICON_FK_FLOPPY, ImVec2(button_size, 0), button_flags))
{
DeleteFileW(_current_preset_path.c_str());
save_current_preset();
}

if (ImGui::IsItemHovered())
ImGui::SetTooltip("Clean up and save the current preset (removes all settings for disabled techniques)");

ImGui::SameLine(0, button_spacing);
if (ImGui::ButtonEx(ICON_FK_PLUS, ImVec2(button_size, 0), button_flags | ImGuiButtonFlags_PressedOnClick))
{
_file_selection_path = _current_preset_path.parent_path();
ImGui::OpenPopup("##create");
}

if (ImGui::IsItemHovered())
ImGui::SetTooltip("Add a new preset");

if (is_loading())
ImGui::PopStyleColor();

ImGui::SetNextWindowPos(popup_pos);
if (widgets::file_dialog("##browse", _file_selection_path, browse_button_width, { L".ini", L".txt" }))
{
// Check that this is actually a valid preset file
if (ini_file::load_cache(_file_selection_path).has("", "Techniques"))
if (ini_file::load_cache(_file_selection_path).has({}, "Techniques"))
{
reload_preset = true;
_current_preset_path = _file_selection_path;
Expand All @@ -1044,7 +1057,7 @@ void reshade::runtime::draw_gui_home()
{
reload_preset =
file_type == std::filesystem::file_type::not_found ||
ini_file::load_cache(new_preset_path).has("", "Techniques");
ini_file::load_cache(new_preset_path).has({}, "Techniques");

if (_duplicate_current_preset && file_type == std::filesystem::file_type::not_found)
CopyFileW(_current_preset_path.c_str(), new_preset_path.c_str(), TRUE);
Expand Down Expand Up @@ -1287,6 +1300,8 @@ void reshade::runtime::draw_gui_home()

if (ImGui::Button(ICON_FK_REFRESH " Reload", ImVec2(-11.5f * _font_size, 0)))
{
load_config(); // Reload configuration too

if (!_no_effect_cache && (ImGui::GetIO().KeyCtrl || ImGui::GetIO().KeyShift))
clear_effect_cache();

Expand Down

0 comments on commit 8da20ef

Please sign in to comment.