Skip to content

Commit 6d85f13

Browse files
committed
Consolidate utility functions that resolve paths
1 parent f26b990 commit 6d85f13

File tree

5 files changed

+11
-29
lines changed

5 files changed

+11
-29
lines changed

source/dll_main.cpp

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ std::filesystem::path g_reshade_dll_path;
2121
std::filesystem::path g_reshade_base_path;
2222
std::filesystem::path g_target_executable_path;
2323

24+
extern bool resolve_path(std::filesystem::path &path, std::error_code &ec, const std::filesystem::path &base);
25+
2426
/// <summary>
2527
/// Checks whether the current application is an UWP app.
2628
/// </summary>
@@ -47,42 +49,22 @@ bool is_windows7()
4749
return VerifyVersionInfo(&verinfo_windows7, VER_MAJORVERSION | VER_MINORVERSION, condition) != FALSE;
4850
}
4951

50-
/// <summary>
51-
/// Expands any environment variables in the path (like "%USERPROFILE%") and checks whether it points towards an existing directory.
52-
/// </summary>
53-
static bool resolve_env_path(std::filesystem::path &path, const std::filesystem::path &base = g_reshade_dll_path.parent_path())
54-
{
55-
if (path.empty())
56-
return false;
57-
58-
WCHAR buf[4096];
59-
if (ExpandEnvironmentStringsW(path.c_str(), buf, ARRAYSIZE(buf)))
60-
path = buf;
61-
else
62-
return false;
63-
64-
path = base / path;
65-
66-
std::error_code ec;
67-
path = std::filesystem::canonical(path, ec);
68-
return !ec && std::filesystem::is_directory(path, ec);
69-
}
70-
7152
/// <summary>
7253
/// Returns the path that should be used as base for relative paths.
7354
/// </summary>
7455
std::filesystem::path get_base_path(bool default_to_target_executable_path = false)
7556
{
57+
std::error_code ec;
7658
std::filesystem::path path_override;
7759

7860
// Cannot use global config here yet, since it uses base path for look up, so look at config file next to target executable instead
7961
if (reshade::ini_file::load_cache(g_target_executable_path.parent_path() / L"ReShade.ini").get("INSTALL", "BasePath", path_override) &&
80-
resolve_env_path(path_override))
62+
resolve_path(path_override, ec, g_reshade_dll_path.parent_path()) && std::filesystem::is_directory(path_override, ec))
8163
return path_override;
8264

8365
WCHAR buf[4096];
8466
path_override.assign(buf, buf + GetEnvironmentVariableW(L"RESHADE_BASE_PATH_OVERRIDE", buf, ARRAYSIZE(buf)));
85-
if (resolve_env_path(path_override))
67+
if (resolve_path(path_override, ec, g_reshade_dll_path.parent_path()) && std::filesystem::is_directory(path_override, ec))
8668
return path_override;
8769

8870
return default_to_target_executable_path ? g_target_executable_path.parent_path() : g_reshade_dll_path.parent_path();

source/imgui_widgets.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
extern std::filesystem::path g_reshade_base_path;
1515

16-
extern bool resolve_path(std::filesystem::path &path, std::error_code &ec);
16+
extern bool resolve_path(std::filesystem::path &path, std::error_code &ec, const std::filesystem::path &base = g_reshade_base_path);
1717
extern std::string expand_macro_string(const std::string &input, std::vector<std::pair<std::string, std::string>> macros = {});
1818

1919
// Resolve environment variables in input text widgets when tab is pressed
20-
static int resolve_macros(ImGuiInputTextCallbackData *data)
20+
static auto resolve_macros(ImGuiInputTextCallbackData *data) -> int
2121
{
2222
const std::string text(data->Buf, data->BufTextLen);
2323
const std::string resolved = expand_macro_string(text);

source/runtime.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ std::string expand_macro_string(const std::string &input, std::vector<std::pair<
155155
return expand_macro_string(input, macros);
156156
}
157157

158-
bool resolve_path(std::filesystem::path &path, std::error_code &ec)
158+
bool resolve_path(std::filesystem::path &path, std::error_code &ec, const std::filesystem::path &base = g_reshade_base_path)
159159
{
160160
path = std::filesystem::u8path(expand_macro_string(path.u8string()));
161161

162162
// First convert path to an absolute path
163163
// Ignore the working directory and instead start relative paths at the DLL location
164164
if (path.is_relative())
165-
path = g_reshade_base_path / path;
165+
path = base / path;
166166
// Finally try to canonicalize the path too
167167
if (std::filesystem::path canonical_path = std::filesystem::canonical(path, ec); !ec)
168168
path = std::move(canonical_path);

source/runtime_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "input.hpp"
1111
#include <algorithm> // std::all_of, std::find, std::find_if, std::for_each, std::remove_if
1212

13-
extern bool resolve_path(std::filesystem::path &path, std::error_code &ec);
13+
extern bool resolve_path(std::filesystem::path &path, std::error_code &ec, const std::filesystem::path &base = g_reshade_base_path);
1414
extern bool resolve_preset_path(std::filesystem::path &path, std::error_code &ec);
1515

1616
bool reshade::runtime::is_key_down(uint32_t keycode) const

source/runtime_gui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <cstring> // std::memcmp, std::memcpy
2525
#include <algorithm> // std::any_of, std::count_if, std::find, std::find_if, std::max, std::min, std::replace, std::rotate, std::search, std::swap, std::transform
2626

27-
extern bool resolve_path(std::filesystem::path &path, std::error_code &ec);
27+
extern bool resolve_path(std::filesystem::path &path, std::error_code &ec, const std::filesystem::path &base = g_reshade_base_path);
2828

2929
static bool string_contains(const std::string_view text, const std::string_view filter)
3030
{

0 commit comments

Comments
 (0)