path_utils.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <stdlib.h>
  36. #ifdef WINDOWS_ENABLED
  37. #define WIN32_LEAN_AND_MEAN
  38. #include <windows.h>
  39. #define ENV_PATH_SEP ";"
  40. #else
  41. #include <limits.h>
  42. #include <unistd.h>
  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;
  80. result.parse_utf16(buffer.ptr());
  81. if (result.is_empty()) {
  82. return ".";
  83. }
  84. return result.simplify_path();
  85. #else
  86. char buffer[PATH_MAX];
  87. if (::getcwd(buffer, sizeof(buffer)) == nullptr) {
  88. return ".";
  89. }
  90. String result;
  91. if (result.parse_utf8(buffer) != OK) {
  92. return ".";
  93. }
  94. return result.simplify_path();
  95. #endif
  96. }
  97. String abspath(const String &p_path) {
  98. if (p_path.is_absolute_path()) {
  99. return p_path.simplify_path();
  100. } else {
  101. return path::join(path::cwd(), p_path).simplify_path();
  102. }
  103. }
  104. String realpath(const String &p_path) {
  105. #ifdef WINDOWS_ENABLED
  106. // Open file without read/write access
  107. HANDLE hFile = ::CreateFileW((LPCWSTR)(p_path.utf16().get_data()), 0,
  108. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  109. nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  110. if (hFile == INVALID_HANDLE_VALUE) {
  111. return p_path;
  112. }
  113. const DWORD expected_size = ::GetFinalPathNameByHandleW(hFile, nullptr, 0, FILE_NAME_NORMALIZED);
  114. if (expected_size == 0) {
  115. ::CloseHandle(hFile);
  116. return p_path;
  117. }
  118. Char16String buffer;
  119. buffer.resize((int)expected_size);
  120. ::GetFinalPathNameByHandleW(hFile, (wchar_t *)buffer.ptrw(), expected_size, FILE_NAME_NORMALIZED);
  121. ::CloseHandle(hFile);
  122. String result;
  123. result.parse_utf16(buffer.ptr());
  124. if (result.is_empty()) {
  125. return p_path;
  126. }
  127. return result.simplify_path();
  128. #elif defined(UNIX_ENABLED)
  129. char *resolved_path = ::realpath(p_path.utf8().get_data(), nullptr);
  130. if (!resolved_path) {
  131. return p_path;
  132. }
  133. String result;
  134. Error parse_ok = result.parse_utf8(resolved_path);
  135. ::free(resolved_path);
  136. if (parse_ok != OK) {
  137. return p_path;
  138. }
  139. return result.simplify_path();
  140. #endif
  141. }
  142. String join(const String &p_a, const String &p_b) {
  143. if (p_a.is_empty()) {
  144. return p_b;
  145. }
  146. const char32_t a_last = p_a[p_a.length() - 1];
  147. if ((a_last == '/' || a_last == '\\') ||
  148. (p_b.size() > 0 && (p_b[0] == '/' || p_b[0] == '\\'))) {
  149. return p_a + p_b;
  150. }
  151. return p_a + "/" + p_b;
  152. }
  153. String join(const String &p_a, const String &p_b, const String &p_c) {
  154. return path::join(path::join(p_a, p_b), p_c);
  155. }
  156. String join(const String &p_a, const String &p_b, const String &p_c, const String &p_d) {
  157. return path::join(path::join(path::join(p_a, p_b), p_c), p_d);
  158. }
  159. String relative_to_impl(const String &p_path, const String &p_relative_to) {
  160. // This function assumes arguments are normalized and absolute paths
  161. if (p_path.begins_with(p_relative_to)) {
  162. return p_path.substr(p_relative_to.length() + 1);
  163. } else {
  164. String base_dir = p_relative_to.get_base_dir();
  165. if (base_dir.length() <= 2 && (base_dir.is_empty() || base_dir.ends_with(":"))) {
  166. return p_path;
  167. }
  168. return String("..").path_join(relative_to_impl(p_path, base_dir));
  169. }
  170. }
  171. #ifdef WINDOWS_ENABLED
  172. String get_drive_letter(const String &p_norm_path) {
  173. int idx = p_norm_path.find(":/");
  174. if (idx != -1 && idx < p_norm_path.find_char('/')) {
  175. return p_norm_path.substr(0, idx + 1);
  176. }
  177. return String();
  178. }
  179. #endif
  180. String relative_to(const String &p_path, const String &p_relative_to) {
  181. String relative_to_abs_norm = abspath(p_relative_to);
  182. String path_abs_norm = abspath(p_path);
  183. #ifdef WINDOWS_ENABLED
  184. if (get_drive_letter(relative_to_abs_norm) != get_drive_letter(path_abs_norm)) {
  185. return path_abs_norm;
  186. }
  187. #endif
  188. return relative_to_impl(path_abs_norm, relative_to_abs_norm);
  189. }
  190. const Vector<String> reserved_assembly_names = { "GodotSharp", "GodotSharpEditor", "Godot.SourceGenerators" };
  191. String get_csharp_project_name() {
  192. String name = GLOBAL_GET("dotnet/project/assembly_name");
  193. if (name.is_empty()) {
  194. name = GLOBAL_GET("application/config/name");
  195. Vector<String> invalid_chars = Vector<String>({ //
  196. // Windows reserved filename chars.
  197. ":", "*", "?", "\"", "<", ">", "|",
  198. // Directory separators.
  199. "/", "\\",
  200. // Other chars that have been found to break assembly loading.
  201. ";", "'", "=", "," });
  202. name = name.strip_edges();
  203. for (int i = 0; i < invalid_chars.size(); i++) {
  204. name = name.replace(invalid_chars[i], "-");
  205. }
  206. }
  207. if (name.is_empty()) {
  208. name = "UnnamedProject";
  209. }
  210. // Avoid reserved names that conflict with Godot assemblies.
  211. if (reserved_assembly_names.has(name)) {
  212. name += "_";
  213. }
  214. return name;
  215. }
  216. } // namespace path