123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- #include "mono_reg_utils.h"
- #ifdef WINDOWS_ENABLED
- #include "core/os/os.h"
- #include <windows.h>
- namespace MonoRegUtils {
- template <int>
- REGSAM bitness_sam_impl();
- template <>
- REGSAM bitness_sam_impl<4>() {
- return KEY_WOW64_64KEY;
- }
- template <>
- REGSAM bitness_sam_impl<8>() {
- return KEY_WOW64_32KEY;
- }
- REGSAM _get_bitness_sam() {
- return bitness_sam_impl<sizeof(size_t)>();
- }
- LONG _RegOpenKey(HKEY hKey, LPCWSTR lpSubKey, PHKEY phkResult) {
- LONG res = RegOpenKeyExW(hKey, lpSubKey, 0, KEY_READ, phkResult);
- if (res != ERROR_SUCCESS)
- res = RegOpenKeyExW(hKey, lpSubKey, 0, KEY_READ | _get_bitness_sam(), phkResult);
- return res;
- }
- LONG _RegKeyQueryString(HKEY hKey, const String &p_value_name, String &r_value) {
- Vector<WCHAR> buffer;
- buffer.resize(512);
- DWORD dwBufferSize = buffer.size();
- LONG res = RegQueryValueExW(hKey, p_value_name.c_str(), 0, NULL, (LPBYTE)buffer.ptr(), &dwBufferSize);
- if (res == ERROR_MORE_DATA) {
-
- Vector<WCHAR> buffer;
- buffer.resize(dwBufferSize);
- res = RegQueryValueExW(hKey, p_value_name.c_str(), 0, NULL, (LPBYTE)buffer.ptr(), &dwBufferSize);
- }
- if (res == ERROR_SUCCESS) {
- r_value = String(buffer.ptr(), buffer.size());
- } else {
- r_value = String();
- }
- return res;
- }
- LONG _find_mono_in_reg(const String &p_subkey, MonoRegInfo &r_info, bool p_old_reg = false) {
- HKEY hKey;
- LONG res = _RegOpenKey(HKEY_LOCAL_MACHINE, p_subkey.c_str(), &hKey);
- if (res != ERROR_SUCCESS)
- goto cleanup;
- if (!p_old_reg) {
- res = _RegKeyQueryString(hKey, "Version", r_info.version);
- if (res != ERROR_SUCCESS)
- goto cleanup;
- }
- res = _RegKeyQueryString(hKey, "SdkInstallRoot", r_info.install_root_dir);
- if (res != ERROR_SUCCESS)
- goto cleanup;
- res = _RegKeyQueryString(hKey, "FrameworkAssemblyDirectory", r_info.assembly_dir);
- if (res != ERROR_SUCCESS)
- goto cleanup;
- res = _RegKeyQueryString(hKey, "MonoConfigDir", r_info.config_dir);
- if (res != ERROR_SUCCESS)
- goto cleanup;
- if (r_info.install_root_dir.ends_with("\\"))
- r_info.bin_dir = r_info.install_root_dir + "bin";
- else
- r_info.bin_dir = r_info.install_root_dir + "\\bin";
- cleanup:
- RegCloseKey(hKey);
- return res;
- }
- LONG _find_mono_in_reg_old(const String &p_subkey, MonoRegInfo &r_info) {
- String default_clr;
- HKEY hKey;
- LONG res = _RegOpenKey(HKEY_LOCAL_MACHINE, p_subkey.c_str(), &hKey);
- if (res != ERROR_SUCCESS)
- goto cleanup;
- res = _RegKeyQueryString(hKey, "DefaultCLR", default_clr);
- if (res == ERROR_SUCCESS && default_clr.length()) {
- r_info.version = default_clr;
- res = _find_mono_in_reg(p_subkey + "\\" + default_clr, r_info, true);
- }
- cleanup:
- RegCloseKey(hKey);
- return res;
- }
- MonoRegInfo find_mono() {
- MonoRegInfo info;
- if (_find_mono_in_reg("Software\\Mono", info) == ERROR_SUCCESS)
- return info;
- if (_find_mono_in_reg_old("Software\\Novell\\Mono", info) == ERROR_SUCCESS)
- return info;
- ERR_PRINT("Cannot find mono in the registry");
- return MonoRegInfo();
- }
- String find_msbuild_tools_path() {
- String msbuild_tools_path;
-
- String vswhere_path = OS::get_singleton()->get_environment(sizeof(size_t) == 8 ? "ProgramFiles(x86)" : "ProgramFiles");
- vswhere_path += "\\Microsoft Visual Studio\\Installer\\vswhere.exe";
- List<String> vswhere_args;
- vswhere_args.push_back("-latest");
- vswhere_args.push_back("-products");
- vswhere_args.push_back("*");
- vswhere_args.push_back("-requires");
- vswhere_args.push_back("Microsoft.Component.MSBuild");
- String output;
- int exit_code;
- OS::get_singleton()->execute(vswhere_path, vswhere_args, true, NULL, &output, &exit_code);
- if (exit_code == 0) {
- Vector<String> lines = output.split("\n");
- for (int i = 0; i < lines.size(); i++) {
- const String &line = lines[i];
- int sep_idx = line.find(":");
- if (sep_idx > 0) {
- String key = line.substr(0, sep_idx);
- if (key == "installationPath") {
- String val = line.substr(sep_idx + 1, line.length()).strip_edges();
- ERR_BREAK(val.empty());
- if (!val.ends_with("\\")) {
- val += "\\";
- }
- return val + "MSBuild\\15.0\\Bin";
- }
- }
- }
- }
-
- HKEY hKey;
- LONG res = _RegOpenKey(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\14.0", &hKey);
- if (res != ERROR_SUCCESS)
- goto cleanup;
- res = _RegKeyQueryString(hKey, "MSBuildToolsPath", msbuild_tools_path);
- if (res != ERROR_SUCCESS)
- goto cleanup;
- cleanup:
- RegCloseKey(hKey);
- return msbuild_tools_path;
- }
- }
- #endif
|