mono_reg_utils.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**************************************************************************/
  2. /* mono_reg_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 "mono_reg_utils.h"
  31. #include "core/os/dir_access.h"
  32. #ifdef WINDOWS_ENABLED
  33. #include "core/os/os.h"
  34. #define WIN32_LEAN_AND_MEAN
  35. #include <windows.h>
  36. namespace MonoRegUtils {
  37. template <int>
  38. REGSAM bitness_sam_impl();
  39. template <>
  40. REGSAM bitness_sam_impl<4>() {
  41. return KEY_WOW64_64KEY;
  42. }
  43. template <>
  44. REGSAM bitness_sam_impl<8>() {
  45. return KEY_WOW64_32KEY;
  46. }
  47. REGSAM _get_bitness_sam() {
  48. return bitness_sam_impl<sizeof(size_t)>();
  49. }
  50. LONG _RegOpenKey(HKEY hKey, LPCWSTR lpSubKey, PHKEY phkResult) {
  51. LONG res = RegOpenKeyExW(hKey, lpSubKey, 0, KEY_READ, phkResult);
  52. if (res != ERROR_SUCCESS)
  53. res = RegOpenKeyExW(hKey, lpSubKey, 0, KEY_READ | _get_bitness_sam(), phkResult);
  54. return res;
  55. }
  56. LONG _RegKeyQueryString(HKEY hKey, const String &p_value_name, String &r_value) {
  57. Vector<WCHAR> buffer;
  58. buffer.resize(512);
  59. DWORD dwBufferSize = buffer.size();
  60. LONG res = RegQueryValueExW(hKey, p_value_name.c_str(), 0, NULL, (LPBYTE)buffer.ptr(), &dwBufferSize);
  61. if (res == ERROR_MORE_DATA) {
  62. // dwBufferSize now contains the actual size
  63. buffer.resize(dwBufferSize);
  64. res = RegQueryValueExW(hKey, p_value_name.c_str(), 0, NULL, (LPBYTE)buffer.ptr(), &dwBufferSize);
  65. }
  66. if (res == ERROR_SUCCESS) {
  67. r_value = String(buffer.ptr(), buffer.size());
  68. } else {
  69. r_value = String();
  70. }
  71. return res;
  72. }
  73. LONG _find_mono_in_reg(const String &p_subkey, MonoRegInfo &r_info, bool p_old_reg = false) {
  74. HKEY hKey;
  75. LONG res = _RegOpenKey(HKEY_LOCAL_MACHINE, p_subkey.c_str(), &hKey);
  76. if (res != ERROR_SUCCESS)
  77. goto cleanup;
  78. if (!p_old_reg) {
  79. res = _RegKeyQueryString(hKey, "Version", r_info.version);
  80. if (res != ERROR_SUCCESS)
  81. goto cleanup;
  82. }
  83. res = _RegKeyQueryString(hKey, "SdkInstallRoot", r_info.install_root_dir);
  84. if (res != ERROR_SUCCESS)
  85. goto cleanup;
  86. res = _RegKeyQueryString(hKey, "FrameworkAssemblyDirectory", r_info.assembly_dir);
  87. if (res != ERROR_SUCCESS)
  88. goto cleanup;
  89. res = _RegKeyQueryString(hKey, "MonoConfigDir", r_info.config_dir);
  90. if (res != ERROR_SUCCESS)
  91. goto cleanup;
  92. if (r_info.install_root_dir.ends_with("\\"))
  93. r_info.bin_dir = r_info.install_root_dir + "bin";
  94. else
  95. r_info.bin_dir = r_info.install_root_dir + "\\bin";
  96. cleanup:
  97. RegCloseKey(hKey);
  98. return res;
  99. }
  100. LONG _find_mono_in_reg_old(const String &p_subkey, MonoRegInfo &r_info) {
  101. String default_clr;
  102. HKEY hKey;
  103. LONG res = _RegOpenKey(HKEY_LOCAL_MACHINE, p_subkey.c_str(), &hKey);
  104. if (res != ERROR_SUCCESS)
  105. goto cleanup;
  106. res = _RegKeyQueryString(hKey, "DefaultCLR", default_clr);
  107. if (res == ERROR_SUCCESS && default_clr.length()) {
  108. r_info.version = default_clr;
  109. res = _find_mono_in_reg(p_subkey + "\\" + default_clr, r_info, true);
  110. }
  111. cleanup:
  112. RegCloseKey(hKey);
  113. return res;
  114. }
  115. MonoRegInfo find_mono() {
  116. MonoRegInfo info;
  117. if (_find_mono_in_reg("Software\\Mono", info) == ERROR_SUCCESS)
  118. return info;
  119. if (_find_mono_in_reg_old("Software\\Novell\\Mono", info) == ERROR_SUCCESS)
  120. return info;
  121. return MonoRegInfo();
  122. }
  123. String find_msbuild_tools_path() {
  124. String msbuild_tools_path;
  125. // Try to find 15.0 with vswhere
  126. String vswhere_path = OS::get_singleton()->get_environment(sizeof(size_t) == 8 ? "ProgramFiles(x86)" : "ProgramFiles");
  127. vswhere_path += "\\Microsoft Visual Studio\\Installer\\vswhere.exe";
  128. List<String> vswhere_args;
  129. vswhere_args.push_back("-latest");
  130. vswhere_args.push_back("-products");
  131. vswhere_args.push_back("*");
  132. vswhere_args.push_back("-requires");
  133. vswhere_args.push_back("Microsoft.Component.MSBuild");
  134. String output;
  135. int exit_code;
  136. OS::get_singleton()->execute(vswhere_path, vswhere_args, true, NULL, &output, &exit_code);
  137. if (exit_code == 0) {
  138. Vector<String> lines = output.split("\n");
  139. for (int i = 0; i < lines.size(); i++) {
  140. const String &line = lines[i];
  141. int sep_idx = line.find(":");
  142. if (sep_idx > 0) {
  143. String key = line.substr(0, sep_idx); // No need to trim
  144. if (key == "installationPath") {
  145. String val = line.substr(sep_idx + 1, line.length()).strip_edges();
  146. ERR_BREAK(val.empty());
  147. if (!val.ends_with("\\")) {
  148. val += "\\";
  149. }
  150. // Since VS2019, the directory is simply named "Current"
  151. String msbuild_dir = val + "MSBuild\\Current\\Bin";
  152. if (DirAccess::exists(msbuild_dir)) {
  153. return msbuild_dir;
  154. }
  155. // Directory name "15.0" is used in VS 2017
  156. return val + "MSBuild\\15.0\\Bin";
  157. }
  158. }
  159. }
  160. }
  161. // Try to find 14.0 in the Registry
  162. HKEY hKey;
  163. LONG res = _RegOpenKey(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\14.0", &hKey);
  164. if (res != ERROR_SUCCESS)
  165. goto cleanup;
  166. res = _RegKeyQueryString(hKey, "MSBuildToolsPath", msbuild_tools_path);
  167. if (res != ERROR_SUCCESS)
  168. goto cleanup;
  169. cleanup:
  170. RegCloseKey(hKey);
  171. return msbuild_tools_path;
  172. }
  173. } // namespace MonoRegUtils
  174. #endif // WINDOWS_ENABLED