Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Expand environment variables in `FileDialog` folder/file path fields [no ci]

AR-DEV-194818c057e1e
diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp
index 21e0abfe3d2..6a7a168c426 100644
--- a/drivers/unix/os_unix.cpp
+++ b/drivers/unix/os_unix.cpp
@@ -34,6 +34,7 @@
#include "core/debugger/engine_debugger.h"
#include "core/debugger/script_debugger.h"
+#include "core/string/char_utils.h"
#include "drivers/unix/dir_access_unix.h"
#include "drivers/unix/file_access_unix.h"
#include "drivers/unix/file_access_unix_pipe.h"
@@ -1220,6 +1221,45 @@ String OS_Unix::expand_path(const String &p_path) const {
}
}
+ int pos = 0;
+
+ while (pos < path.length()) {
+ int dollar = path.find_char('$', pos);
+ if (dollar == -1) {
+ break;
+ }
+
+ const int begin = dollar + 1;
+ if (begin >= path.length()) {
+ break;
+ }
+
+ if (!(is_ascii_alphabet_char(path[begin]) || is_underscore(path[begin]))) {
+ pos = dollar + 1;
+ continue;
+ }
+
+ int end = begin + 1;
+ while (end < path.length()) {
+ const char32_t c = path[end];
+
+ if (!(is_ascii_alphanumeric_char(c) || is_underscore(c))) {
+ break;
+ }
+ end++;
+ }
+
+ const String var_name = path.substr(begin, end - begin);
+ const String value = get_environment(var_name);
+
+ if (!value.is_empty()) {
+ path = path.substr(0, dollar) + value + path.substr(end);
+ pos = dollar + value.length();
+ } else {
+ pos = end;
+ }
+ }
+
return path;
}
diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp
index 26e25605466..55661591b71 100644
--- a/platform/windows/os_windows.cpp
+++ b/platform/windows/os_windows.cpp
@@ -2525,6 +2525,36 @@ String OS_Windows::expand_path(const String &p_path) const {
}
}
+ int pos = 0;
+
+ while (true) {
+ int left = path.find_char('%', pos);
+ if (left == -1) {
+ break;
+ }
+
+ int right = path.find_char('%', left + 1);
+ if (right == -1) {
+ break;
+ }
+
+ String var = path.substr(left + 1, right - left - 1);
+
+ if (var.is_empty()) {
+ pos = right + 1;
+ continue;
+ }
+
+ String value = get_environment(var);
+
+ if (!value.is_empty()) {
+ path = path.substr(0, left) + value + path.substr(right + 1);
+ pos = left + value.length();
+ } else {
+ pos = right + 1;
+ }
+ }
+
return path;
}