hostfxr_resolver.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**************************************************************************/
  2. /* hostfxr_resolver.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. Adapted to Godot from the nethost library: https://github.com/dotnet/runtime/tree/main/src/native/corehost
  32. */
  33. /*
  34. The MIT License (MIT)
  35. Copyright (c) .NET Foundation and Contributors
  36. All rights reserved.
  37. Permission is hereby granted, free of charge, to any person obtaining a copy
  38. of this software and associated documentation files (the "Software"), to deal
  39. in the Software without restriction, including without limitation the rights
  40. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  41. copies of the Software, and to permit persons to whom the Software is
  42. furnished to do so, subject to the following conditions:
  43. The above copyright notice and this permission notice shall be included in all
  44. copies or substantial portions of the Software.
  45. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  46. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  47. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  48. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  49. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  50. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  51. SOFTWARE.
  52. */
  53. #include "hostfxr_resolver.h"
  54. #include "../utils/path_utils.h"
  55. #include "semver.h"
  56. #include "core/config/engine.h"
  57. #include "core/io/dir_access.h"
  58. #include "core/io/file_access.h"
  59. #include "core/os/os.h"
  60. #ifdef WINDOWS_ENABLED
  61. #define WIN32_LEAN_AND_MEAN
  62. #include <windows.h>
  63. #endif
  64. // We don't use libnethost as it gives us issues with some compilers.
  65. // This file tries to mimic libnethost's hostfxr_resolver search logic. We try to use the
  66. // same function names for easier comparing in case we need to update this in the future.
  67. namespace {
  68. String get_hostfxr_file_name() {
  69. #if defined(WINDOWS_ENABLED)
  70. return "hostfxr.dll";
  71. #elif defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  72. return "libhostfxr.dylib";
  73. #else
  74. return "libhostfxr.so";
  75. #endif
  76. }
  77. bool get_latest_fxr(const String &fxr_root, String &r_fxr_path) {
  78. godotsharp::SemVerParser sem_ver_parser;
  79. bool found_ver = false;
  80. godotsharp::SemVer latest_ver;
  81. String latest_ver_str;
  82. Ref<DirAccess> da = DirAccess::open(fxr_root);
  83. da->list_dir_begin();
  84. for (String dir = da->get_next(); !dir.is_empty(); dir = da->get_next()) {
  85. if (!da->current_is_dir() || dir == "." || dir == "..") {
  86. continue;
  87. }
  88. String ver = dir.get_file();
  89. godotsharp::SemVer fx_ver;
  90. if (sem_ver_parser.parse(ver, fx_ver)) {
  91. if (!found_ver || fx_ver > latest_ver) {
  92. latest_ver = fx_ver;
  93. latest_ver_str = ver;
  94. found_ver = true;
  95. }
  96. }
  97. }
  98. if (!found_ver) {
  99. return false;
  100. }
  101. String fxr_with_ver = path::join(fxr_root, latest_ver_str);
  102. String hostfxr_file_path = path::join(fxr_with_ver, get_hostfxr_file_name());
  103. ERR_FAIL_COND_V_MSG(!FileAccess::exists(hostfxr_file_path), false, "Missing hostfxr library in directory: " + fxr_with_ver);
  104. r_fxr_path = hostfxr_file_path;
  105. return true;
  106. }
  107. #ifdef WINDOWS_ENABLED
  108. typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
  109. BOOL is_wow64() {
  110. BOOL wow64 = FALSE;
  111. LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
  112. if (fnIsWow64Process) {
  113. if (!fnIsWow64Process(GetCurrentProcess(), &wow64)) {
  114. wow64 = FALSE;
  115. }
  116. }
  117. return wow64;
  118. }
  119. #endif
  120. static const char *arch_name_map[][2] = {
  121. { "arm32", "arm" },
  122. { "arm64", "arm64" },
  123. { "rv64", "riscv64" },
  124. { "x86_64", "x64" },
  125. { "x86_32", "x86" },
  126. { nullptr, nullptr }
  127. };
  128. String get_dotnet_arch() {
  129. String arch = Engine::get_singleton()->get_architecture_name();
  130. int idx = 0;
  131. while (arch_name_map[idx][0] != nullptr) {
  132. if (arch_name_map[idx][0] == arch) {
  133. return arch_name_map[idx][1];
  134. }
  135. idx++;
  136. }
  137. return "";
  138. }
  139. bool get_default_installation_dir(String &r_dotnet_root) {
  140. #if defined(WINDOWS_ENABLED)
  141. String program_files_env;
  142. if (is_wow64()) {
  143. // Running x86 on x64, looking for x86 install
  144. program_files_env = "ProgramFiles(x86)";
  145. } else {
  146. program_files_env = "ProgramFiles";
  147. }
  148. String program_files_dir = OS::get_singleton()->get_environment(program_files_env);
  149. if (program_files_dir.is_empty()) {
  150. return false;
  151. }
  152. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
  153. // When emulating x64 on arm
  154. String dotnet_root_emulated = path::join(program_files_dir, "dotnet", "x64");
  155. if (FileAccess::exists(path::join(dotnet_root_emulated, "dotnet.exe"))) {
  156. r_dotnet_root = dotnet_root_emulated;
  157. return true;
  158. }
  159. #endif
  160. r_dotnet_root = path::join(program_files_dir, "dotnet");
  161. return true;
  162. #elif defined(MACOS_ENABLED)
  163. r_dotnet_root = "/usr/local/share/dotnet";
  164. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
  165. // When emulating x64 on arm
  166. String dotnet_root_emulated = path::join(r_dotnet_root, "x64");
  167. if (FileAccess::exists(path::join(dotnet_root_emulated, "dotnet"))) {
  168. r_dotnet_root = dotnet_root_emulated;
  169. return true;
  170. }
  171. #endif
  172. return true;
  173. #else
  174. r_dotnet_root = "/usr/share/dotnet";
  175. return true;
  176. #endif
  177. }
  178. #ifndef WINDOWS_ENABLED
  179. bool get_install_location_from_file(const String &p_file_path, String &r_dotnet_root) {
  180. Error err = OK;
  181. Ref<FileAccess> f = FileAccess::open(p_file_path, FileAccess::READ, &err);
  182. if (f.is_null() || err != OK) {
  183. return false;
  184. }
  185. String line = f->get_line();
  186. if (line.is_empty()) {
  187. return false;
  188. }
  189. r_dotnet_root = line;
  190. return true;
  191. }
  192. #endif
  193. bool get_dotnet_self_registered_dir(String &r_dotnet_root) {
  194. #if defined(WINDOWS_ENABLED)
  195. String sub_key = "SOFTWARE\\dotnet\\Setup\\InstalledVersions\\" + get_dotnet_arch();
  196. Char16String value = String("InstallLocation").utf16();
  197. HKEY hkey = nullptr;
  198. LSTATUS result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, (LPCWSTR)(sub_key.utf16().get_data()), 0, KEY_READ | KEY_WOW64_32KEY, &hkey);
  199. if (result != ERROR_SUCCESS) {
  200. return false;
  201. }
  202. DWORD size = 0;
  203. result = RegGetValueW(hkey, nullptr, (LPCWSTR)(value.get_data()), RRF_RT_REG_SZ, nullptr, nullptr, &size);
  204. if (result != ERROR_SUCCESS || size == 0) {
  205. RegCloseKey(hkey);
  206. return false;
  207. }
  208. Vector<WCHAR> buffer;
  209. buffer.resize(size / sizeof(WCHAR));
  210. result = RegGetValueW(hkey, nullptr, (LPCWSTR)(value.get_data()), RRF_RT_REG_SZ, nullptr, (LPBYTE)buffer.ptrw(), &size);
  211. if (result != ERROR_SUCCESS) {
  212. RegCloseKey(hkey);
  213. return false;
  214. }
  215. r_dotnet_root = String::utf16((const char16_t *)buffer.ptr()).replace("\\", "/");
  216. RegCloseKey(hkey);
  217. return true;
  218. #else
  219. String install_location_file = path::join("/etc/dotnet", "install_location_" + get_dotnet_arch().to_lower());
  220. if (get_install_location_from_file(install_location_file, r_dotnet_root)) {
  221. return true;
  222. }
  223. if (FileAccess::exists(install_location_file)) {
  224. // Don't try with the legacy location, this will fall back to the hard-coded default install location
  225. return false;
  226. }
  227. String legacy_install_location_file = path::join("/etc/dotnet", "install_location");
  228. return get_install_location_from_file(legacy_install_location_file, r_dotnet_root);
  229. #endif
  230. }
  231. bool get_file_path_from_env(const String &p_env_key, String &r_dotnet_root) {
  232. String env_value = OS::get_singleton()->get_environment(p_env_key);
  233. if (!env_value.is_empty()) {
  234. env_value = path::realpath(env_value);
  235. if (DirAccess::exists(env_value)) {
  236. r_dotnet_root = env_value;
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. bool get_dotnet_root_from_env(String &r_dotnet_root) {
  243. String dotnet_root_env = "DOTNET_ROOT";
  244. String arch_for_env = get_dotnet_arch();
  245. if (!arch_for_env.is_empty()) {
  246. // DOTNET_ROOT_<arch>
  247. if (get_file_path_from_env(dotnet_root_env + "_" + arch_for_env.to_upper(), r_dotnet_root)) {
  248. return true;
  249. }
  250. }
  251. #ifdef WINDOWS_ENABLED
  252. // WoW64-only: DOTNET_ROOT(x86)
  253. if (is_wow64() && get_file_path_from_env("DOTNET_ROOT(x86)", r_dotnet_root)) {
  254. return true;
  255. }
  256. #endif
  257. // DOTNET_ROOT
  258. return get_file_path_from_env(dotnet_root_env, r_dotnet_root);
  259. }
  260. } //namespace
  261. bool godotsharp::hostfxr_resolver::try_get_path_from_dotnet_root(const String &p_dotnet_root, String &r_fxr_path) {
  262. String fxr_dir = path::join(p_dotnet_root, "host", "fxr");
  263. if (!DirAccess::exists(fxr_dir)) {
  264. if (OS::get_singleton()->is_stdout_verbose()) {
  265. ERR_PRINT("The host fxr folder does not exist: " + fxr_dir + ".");
  266. }
  267. return false;
  268. }
  269. return get_latest_fxr(fxr_dir, r_fxr_path);
  270. }
  271. bool godotsharp::hostfxr_resolver::try_get_path(String &r_dotnet_root, String &r_fxr_path) {
  272. if (!get_dotnet_root_from_env(r_dotnet_root) &&
  273. !get_dotnet_self_registered_dir(r_dotnet_root) &&
  274. !get_default_installation_dir(r_dotnet_root)) {
  275. return false;
  276. }
  277. return try_get_path_from_dotnet_root(r_dotnet_root, r_fxr_path);
  278. }