Debian Codegraph @

modules/mono/utils/path_utils.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* path_utils.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "path_utils.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/io/dir_access.h"
35 #include "core/io/file_access.h"
36 #include "core/os/os.h"
37
38 #include <cstdlib>
39
40 #ifdef WINDOWS_ENABLED
41 #define WIN32_LEAN_AND_MEAN
42 #include <windows.h>
43
44 #define ENV_PATH_SEP ";"
45 #else
46 #include <unistd.h>
47 #include <climits>
48
49 #define ENV_PATH_SEP ":"
50 #endif
51
52 namespace Path {
53
54 String find_executable(const String &p_name) {
55 #ifdef WINDOWS_ENABLED
56 Vector<String> exts = OS::get_singleton()->get_environment("PATHEXT").split(ENV_PATH_SEP, false);
57 #endif
58 Vector<String> env_path = OS::get_singleton()->get_environment("PATH").split(ENV_PATH_SEP, false);
59
60 if (env_path.is_empty()) {
61 return String();
62 }
63
64 for (int i = 0; i < env_path.size(); i++) {
65 String p = Path::join(env_path[i], p_name);
66
67 #ifdef WINDOWS_ENABLED
68 for (int j = 0; j < exts.size(); j++) {
69 String p2 = p + exts[j].to_lower(); // lowercase to reduce risk of case mismatch warning
70
71 if (FileAccess::exists(p2)) {
72 return p2;
73 }
74 }
75 #else
76 if (FileAccess::exists(p)) {
77 return p;
78 }
79 #endif
80 }
81
82 return String();
83 }
84
85 String cwd() {
86 #ifdef WINDOWS_ENABLED
87 const DWORD expected_size = ::GetCurrentDirectoryW(0, nullptr);
88
89 Char16String buffer;
90 buffer.resize_uninitialized((int)expected_size);
91 if (::GetCurrentDirectoryW(expected_size, (wchar_t *)buffer.ptrw()) == 0) {
92 return ".";
93 }
94
95 String result = String::utf16(buffer.ptr());
96 if (result.is_empty()) {
97 return ".";
98 }
99 return result.simplify_path();
100 #else
101 char buffer[PATH_MAX];
102 if (::getcwd(buffer, sizeof(buffer)) == nullptr) {
103 return ".";
104 }
105
106 String result;
107 if (result.append_utf8(buffer) != OK) {
108 return ".";
109 }
110
111 return result.simplify_path();
112 #endif
113 }
114
115 String abspath(const String &p_path) {
116 if (p_path.is_absolute_path()) {
117 return p_path.simplify_path();
118 } else {
119 return Path::join(Path::cwd(), p_path).simplify_path();
120 }
121 }
122
123 String realpath(const String &p_path) {
124 #ifdef WINDOWS_ENABLED
125 // Open file without read/write access
126 HANDLE hFile = ::CreateFileW((LPCWSTR)(p_path.utf16().get_data()), 0,
127 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
128 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
129
130 if (hFile == INVALID_HANDLE_VALUE) {
131 return p_path;
132 }
133
134 const DWORD expected_size = ::GetFinalPathNameByHandleW(hFile, nullptr, 0, FILE_NAME_NORMALIZED);
135
136 if (expected_size == 0) {
137 ::CloseHandle(hFile);
138 return p_path;
139 }
140
141 Char16String buffer;
142 buffer.resize_uninitialized((int)expected_size);
143 ::GetFinalPathNameByHandleW(hFile, (wchar_t *)buffer.ptrw(), expected_size, FILE_NAME_NORMALIZED);
144
145 ::CloseHandle(hFile);
146
147 String result = String::utf16(buffer.ptr());
148 if (result.is_empty()) {
149 return p_path;
150 }
151
152 return result.simplify_path();
153 #elif defined(UNIX_ENABLED)
154 char *resolved_path = ::realpath(p_path.utf8().get_data(), nullptr);
155
156 if (!resolved_path) {
157 return p_path;
158 }
159
160 String result;
161 Error parse_ok = result.append_utf8(resolved_path);
162 ::free(resolved_path);
163
164 if (parse_ok != OK) {
165 return p_path;
166 }
167
168 return result.simplify_path();
169 #endif
170 }
171
172 String join(const String &p_a, const String &p_b) {
173 if (p_a.is_empty()) {
174 return p_b;
175 }
176
177 const char32_t a_last = p_a[p_a.length() - 1];
178 if ((a_last == '/' || a_last == '\\') ||
179 (p_b.size() > 0 && (p_b[0] == '/' || p_b[0] == '\\'))) {
180 return p_a + p_b;
181 }
182
183 return p_a + "/" + p_b;
184 }
185
186 String join(const String &p_a, const String &p_b, const String &p_c) {
187 return Path::join(Path::join(p_a, p_b), p_c);
188 }
189
190 String join(const String &p_a, const String &p_b, const String &p_c, const String &p_d) {
191 return Path::join(Path::join(Path::join(p_a, p_b), p_c), p_d);
192 }
193
194 String relative_to_impl(const String &p_path, const String &p_relative_to) {
195 // This function assumes arguments are normalized and absolute paths
196
197 if (p_path.begins_with(p_relative_to)) {
198 return p_path.substr(p_relative_to.length() + 1);
199 } else {
200 String base_dir = p_relative_to.get_base_dir();
201
202 if (base_dir.length() <= 2 && (base_dir.is_empty() || base_dir.ends_with(":"))) {
203 return p_path;
204 }
205
206 return String("..").path_join(relative_to_impl(p_path, base_dir));
207 }
208 }
209
210 #ifdef WINDOWS_ENABLED
211 String get_drive_letter(const String &p_norm_path) {
212 int idx = p_norm_path.find(":/");
213 if (idx != -1 && idx < p_norm_path.find_char('/')) {
214 return p_norm_path.substr(0, idx + 1);
215 }
216 return String();
217 }
218 #endif
219
220 String relative_to(const String &p_path, const String &p_relative_to) {
221 String relative_to_abs_norm = abspath(p_relative_to);
222 String path_abs_norm = abspath(p_path);
223
224 #ifdef WINDOWS_ENABLED
225 if (get_drive_letter(relative_to_abs_norm) != get_drive_letter(path_abs_norm)) {
226 return path_abs_norm;
227 }
228 #endif
229
230 return relative_to_impl(path_abs_norm, relative_to_abs_norm);
231 }
232
233 const Vector<String> reserved_assembly_names = { "GodotSharp", "GodotSharpEditor", "Godot.SourceGenerators" };
234
235 String get_csharp_project_name() {
236 String name = GLOBAL_GET("dotnet/project/assembly_name");
237 if (name.is_empty()) {
238 name = GLOBAL_GET("application/config/name");
239 Vector<String> invalid_chars = Vector<String>({ //
240 // Windows reserved filename chars.
241 ":", "*", "?", "\"", "<", ">", "|",
242 // Directory separators.
243 "/", "\\",
244 // Other chars that have been found to break assembly loading.
245 ";", "'", "=", "," });
246 name = name.strip_edges();
247 for (int i = 0; i < invalid_chars.size(); i++) {
248 name = name.replace(invalid_chars[i], "-");
249 }
250 }
251
252 if (name.is_empty()) {
253 name = "UnnamedProject";
254 }
255
256 // Avoid reserved names that conflict with Godot assemblies.
257 if (reserved_assembly_names.has(name)) {
258 name += "_";
259 }
260
261 return name;
262 }
263
264 } // namespace Path