path_utils.cpp 7.6 KB

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