mono_reg_utils.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #ifdef WINDOWS_ENABLED
  32. #include "core/os/os.h"
  33. // Here, after os/os.h
  34. #include <windows.h>
  35. namespace MonoRegUtils {
  36. template <int>
  37. REGSAM bitness_sam_impl();
  38. template <>
  39. REGSAM bitness_sam_impl<4>() {
  40. return KEY_WOW64_64KEY;
  41. }
  42. template <>
  43. REGSAM bitness_sam_impl<8>() {
  44. return KEY_WOW64_32KEY;
  45. }
  46. REGSAM _get_bitness_sam() {
  47. return bitness_sam_impl<sizeof(size_t)>();
  48. }
  49. LONG _RegOpenKey(HKEY hKey, LPCWSTR lpSubKey, PHKEY phkResult) {
  50. LONG res = RegOpenKeyExW(hKey, lpSubKey, 0, KEY_READ, phkResult);
  51. if (res != ERROR_SUCCESS)
  52. res = RegOpenKeyExW(hKey, lpSubKey, 0, KEY_READ | _get_bitness_sam(), phkResult);
  53. return res;
  54. }
  55. LONG _RegKeyQueryString(HKEY hKey, const String &p_value_name, String &r_value) {
  56. Vector<WCHAR> buffer;
  57. buffer.resize(512);
  58. DWORD dwBufferSize = buffer.size();
  59. LONG res = RegQueryValueExW(hKey, p_value_name.c_str(), 0, NULL, (LPBYTE)buffer.ptr(), &dwBufferSize);
  60. if (res == ERROR_MORE_DATA) {
  61. // dwBufferSize now contains the actual size
  62. Vector<WCHAR> buffer;
  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. ERR_PRINT("Cannot find mono in the registry");
  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. return val + "MSBuild\\15.0\\Bin";
  152. }
  153. }
  154. }
  155. }
  156. // Try to find 14.0 in the Registry
  157. HKEY hKey;
  158. LONG res = _RegOpenKey(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\14.0", &hKey);
  159. if (res != ERROR_SUCCESS)
  160. goto cleanup;
  161. res = _RegKeyQueryString(hKey, "MSBuildToolsPath", msbuild_tools_path);
  162. if (res != ERROR_SUCCESS)
  163. goto cleanup;
  164. cleanup:
  165. RegCloseKey(hKey);
  166. return msbuild_tools_path;
  167. }
  168. } // namespace MonoRegUtils
  169. #endif // WINDOWS_ENABLED