mono_reg_utils.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*************************************************************************/
  2. /* mono_reg_utils.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. // Here, after os/os.h
  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. Vector<WCHAR> buffer;
  64. buffer.resize(dwBufferSize);
  65. res = RegQueryValueExW(hKey, p_value_name.c_str(), 0, NULL, (LPBYTE)buffer.ptr(), &dwBufferSize);
  66. }
  67. if (res == ERROR_SUCCESS) {
  68. r_value = String(buffer.ptr(), buffer.size());
  69. } else {
  70. r_value = String();
  71. }
  72. return res;
  73. }
  74. LONG _find_mono_in_reg(const String &p_subkey, MonoRegInfo &r_info, bool p_old_reg = false) {
  75. HKEY hKey;
  76. LONG res = _RegOpenKey(HKEY_LOCAL_MACHINE, p_subkey.c_str(), &hKey);
  77. if (res != ERROR_SUCCESS)
  78. goto cleanup;
  79. if (!p_old_reg) {
  80. res = _RegKeyQueryString(hKey, "Version", r_info.version);
  81. if (res != ERROR_SUCCESS)
  82. goto cleanup;
  83. }
  84. res = _RegKeyQueryString(hKey, "SdkInstallRoot", r_info.install_root_dir);
  85. if (res != ERROR_SUCCESS)
  86. goto cleanup;
  87. res = _RegKeyQueryString(hKey, "FrameworkAssemblyDirectory", r_info.assembly_dir);
  88. if (res != ERROR_SUCCESS)
  89. goto cleanup;
  90. res = _RegKeyQueryString(hKey, "MonoConfigDir", r_info.config_dir);
  91. if (res != ERROR_SUCCESS)
  92. goto cleanup;
  93. if (r_info.install_root_dir.ends_with("\\"))
  94. r_info.bin_dir = r_info.install_root_dir + "bin";
  95. else
  96. r_info.bin_dir = r_info.install_root_dir + "\\bin";
  97. cleanup:
  98. RegCloseKey(hKey);
  99. return res;
  100. }
  101. LONG _find_mono_in_reg_old(const String &p_subkey, MonoRegInfo &r_info) {
  102. String default_clr;
  103. HKEY hKey;
  104. LONG res = _RegOpenKey(HKEY_LOCAL_MACHINE, p_subkey.c_str(), &hKey);
  105. if (res != ERROR_SUCCESS)
  106. goto cleanup;
  107. res = _RegKeyQueryString(hKey, "DefaultCLR", default_clr);
  108. if (res == ERROR_SUCCESS && default_clr.length()) {
  109. r_info.version = default_clr;
  110. res = _find_mono_in_reg(p_subkey + "\\" + default_clr, r_info, true);
  111. }
  112. cleanup:
  113. RegCloseKey(hKey);
  114. return res;
  115. }
  116. MonoRegInfo find_mono() {
  117. MonoRegInfo info;
  118. if (_find_mono_in_reg("Software\\Mono", info) == ERROR_SUCCESS)
  119. return info;
  120. if (_find_mono_in_reg_old("Software\\Novell\\Mono", info) == ERROR_SUCCESS)
  121. return info;
  122. return MonoRegInfo();
  123. }
  124. String find_msbuild_tools_path() {
  125. String msbuild_tools_path;
  126. // Try to find 15.0 with vswhere
  127. String vswhere_path = OS::get_singleton()->get_environment(sizeof(size_t) == 8 ? "ProgramFiles(x86)" : "ProgramFiles");
  128. vswhere_path += "\\Microsoft Visual Studio\\Installer\\vswhere.exe";
  129. List<String> vswhere_args;
  130. vswhere_args.push_back("-latest");
  131. vswhere_args.push_back("-products");
  132. vswhere_args.push_back("*");
  133. vswhere_args.push_back("-requires");
  134. vswhere_args.push_back("Microsoft.Component.MSBuild");
  135. String output;
  136. int exit_code;
  137. OS::get_singleton()->execute(vswhere_path, vswhere_args, true, NULL, &output, &exit_code);
  138. if (exit_code == 0) {
  139. Vector<String> lines = output.split("\n");
  140. for (int i = 0; i < lines.size(); i++) {
  141. const String &line = lines[i];
  142. int sep_idx = line.find(":");
  143. if (sep_idx > 0) {
  144. String key = line.substr(0, sep_idx); // No need to trim
  145. if (key == "installationPath") {
  146. String val = line.substr(sep_idx + 1, line.length()).strip_edges();
  147. ERR_BREAK(val.empty());
  148. if (!val.ends_with("\\")) {
  149. val += "\\";
  150. }
  151. // Since VS2019, the directory is simply named "Current"
  152. String msBuildDirectory = val + "MSBuild\\Current\\Bin";
  153. if (DirAccess::exists(msBuildDirectory)) {
  154. return msBuildDirectory;
  155. }
  156. // Directory name "15.0" is used in VS 2017
  157. return val + "MSBuild\\15.0\\Bin";
  158. }
  159. }
  160. }
  161. }
  162. // Try to find 14.0 in the Registry
  163. HKEY hKey;
  164. LONG res = _RegOpenKey(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\14.0", &hKey);
  165. if (res != ERROR_SUCCESS)
  166. goto cleanup;
  167. res = _RegKeyQueryString(hKey, "MSBuildToolsPath", msbuild_tools_path);
  168. if (res != ERROR_SUCCESS)
  169. goto cleanup;
  170. cleanup:
  171. RegCloseKey(hKey);
  172. return msbuild_tools_path;
  173. }
  174. } // namespace MonoRegUtils
  175. #endif // WINDOWS_ENABLED