Skip to content
0.2.0-dev Download ZIP
261 lines ·7.6 KB Raw
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/file_access.h"
35#include "core/os/os.h"
36
37#include <cstdlib>
38
39#ifdef WINDOWS_ENABLED
40#include <windows.h>
41
42#define ENV_PATH_SEP ";"
43#else
44#include <unistd.h>
45
46#define ENV_PATH_SEP ":"
47#endif
48
49namespace Path {
50
51String find_executable(const String &p_name) {
52#ifdef WINDOWS_ENABLED
53 Vector<String> exts = OS::get_singleton()->get_environment("PATHEXT").split(ENV_PATH_SEP, false);
54#endif
55 Vector<String> env_path = OS::get_singleton()->get_environment("PATH").split(ENV_PATH_SEP, false);
56
57 if (env_path.is_empty()) {
58 return String();
59 }
60
61 for (int i = 0; i < env_path.size(); i++) {
62 String p = Path::join(env_path[i], p_name);
63
64#ifdef WINDOWS_ENABLED
65 for (int j = 0; j < exts.size(); j++) {
66 String p2 = p + exts[j].to_lower(); // lowercase to reduce risk of case mismatch warning
67
68 if (FileAccess::exists(p2)) {
69 return p2;
70 }
71 }
72#else
73 if (FileAccess::exists(p)) {
74 return p;
75 }
76#endif
77 }
78
79 return String();
80}
81
82String cwd() {
83#ifdef WINDOWS_ENABLED
84 const DWORD expected_size = ::GetCurrentDirectoryW(0, nullptr);
85
86 Char16String buffer;
87 buffer.resize_uninitialized((int)expected_size);
88 if (::GetCurrentDirectoryW(expected_size, (wchar_t *)buffer.ptrw()) == 0) {
89 return ".";
90 }
91
92 String result = String::utf16(buffer.ptr());
93 if (result.is_empty()) {
94 return ".";
95 }
96 return result.simplify_path();
97#else
98 char buffer[PATH_MAX];
99 if (::getcwd(buffer, sizeof(buffer)) == nullptr) {
100 return ".";
101 }
102
103 String result;
104 if (result.append_utf8(buffer) != OK) {
105 return ".";
106 }
107
108 return result.simplify_path();
109#endif
110}
111
112String abspath(const String &p_path) {
113 if (p_path.is_absolute_path()) {
114 return p_path.simplify_path();
115 } else {
116 return Path::join(Path::cwd(), p_path).simplify_path();
117 }
118}
119
120String realpath(const String &p_path) {
121#ifdef WINDOWS_ENABLED
122 // Open file without read/write access
123 HANDLE hFile = ::CreateFileW((LPCWSTR)(p_path.utf16().get_data()), 0,
124 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
125 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
126
127 if (hFile == INVALID_HANDLE_VALUE) {
128 return p_path;
129 }
130
131 const DWORD expected_size = ::GetFinalPathNameByHandleW(hFile, nullptr, 0, FILE_NAME_NORMALIZED);
132
133 if (expected_size == 0) {
134 ::CloseHandle(hFile);
135 return p_path;
136 }
137
138 Char16String buffer;
139 buffer.resize_uninitialized((int)expected_size);
140 ::GetFinalPathNameByHandleW(hFile, (wchar_t *)buffer.ptrw(), expected_size, FILE_NAME_NORMALIZED);
141
142 ::CloseHandle(hFile);
143
144 String result = String::utf16(buffer.ptr());
145 if (result.is_empty()) {
146 return p_path;
147 }
148
149 return result.simplify_path();
150#elif defined(UNIX_ENABLED)
151 char *resolved_path = ::realpath(p_path.utf8().get_data(), nullptr);
152
153 if (!resolved_path) {
154 return p_path;
155 }
156
157 String result;
158 Error parse_ok = result.append_utf8(resolved_path);
159 ::free(resolved_path);
160
161 if (parse_ok != OK) {
162 return p_path;
163 }
164
165 return result.simplify_path();
166#endif
167}
168
169String join(const String &p_a, const String &p_b) {
170 if (p_a.is_empty()) {
171 return p_b;
172 }
173
174 const char32_t a_last = p_a[p_a.length() - 1];
175 if ((a_last == '/' || a_last == '\\') ||
176 (p_b.size() > 0 && (p_b[0] == '/' || p_b[0] == '\\'))) {
177 return p_a + p_b;
178 }
179
180 return p_a + "/" + p_b;
181}
182
183String join(const String &p_a, const String &p_b, const String &p_c) {
184 return Path::join(Path::join(p_a, p_b), p_c);
185}
186
187String join(const String &p_a, const String &p_b, const String &p_c, const String &p_d) {
188 return Path::join(Path::join(Path::join(p_a, p_b), p_c), p_d);
189}
190
191String relative_to_impl(const String &p_path, const String &p_relative_to) {
192 // This function assumes arguments are normalized and absolute paths
193
194 if (p_path.begins_with(p_relative_to)) {
195 return p_path.substr(p_relative_to.length() + 1);
196 } else {
197 String base_dir = p_relative_to.get_base_dir();
198
199 if (base_dir.length() <= 2 && (base_dir.is_empty() || base_dir.ends_with(":"))) {
200 return p_path;
201 }
202
203 return String("..").path_join(relative_to_impl(p_path, base_dir));
204 }
205}
206
207#ifdef WINDOWS_ENABLED
208String get_drive_letter(const String &p_norm_path) {
209 int idx = p_norm_path.find(":/");
210 if (idx != -1 && idx < p_norm_path.find_char('/')) {
211 return p_norm_path.substr(0, idx + 1);
212 }
213 return String();
214}
215#endif
216
217String relative_to(const String &p_path, const String &p_relative_to) {
218 String relative_to_abs_norm = abspath(p_relative_to);
219 String path_abs_norm = abspath(p_path);
220
221#ifdef WINDOWS_ENABLED
222 if (get_drive_letter(relative_to_abs_norm) != get_drive_letter(path_abs_norm)) {
223 return path_abs_norm;
224 }
225#endif
226
227 return relative_to_impl(path_abs_norm, relative_to_abs_norm);
228}
229
230const Vector<String> reserved_assembly_names = { "GodotSharp", "GodotSharpEditor", "Godot.SourceGenerators" };
231
232String get_csharp_project_name() {
233 String name = GLOBAL_GET("dotnet/project/assembly_name");
234 if (name.is_empty()) {
235 name = GLOBAL_GET("application/config/name");
236 Vector<String> invalid_chars = Vector<String>({ //
237 // Windows reserved filename chars.
238 ":", "*", "?", "\"", "<", ">", "|",
239 // Directory separators.
240 "/", "\\",
241 // Other chars that have been found to break assembly loading.
242 ";", "'", "=", "," });
243 name = name.strip_edges();
244 for (int i = 0; i < invalid_chars.size(); i++) {
245 name = name.replace(invalid_chars[i], "-");
246 }
247 }
248
249 if (name.is_empty()) {
250 name = "UnnamedProject";
251 }
252
253 // Avoid reserved names that conflict with Godot assemblies.
254 if (reserved_assembly_names.has(name)) {
255 name += "_";
256 }
257
258 return name;
259}
260
261} // namespace Path