|
30 | 30 |
|
31 | 31 | #include "openxr_select_runtime.h" |
32 | 32 |
|
| 33 | +#ifdef WINDOWS_ENABLED |
| 34 | +#define WIN32_LEAN_AND_MEAN |
| 35 | +#include <windows.h> |
| 36 | +#endif |
| 37 | + |
33 | 38 | #include "core/io/dir_access.h" |
| 39 | +#include "core/io/json.h" |
34 | 40 | #include "core/os/os.h" |
35 | 41 | #include "editor/settings/editor_settings.h" |
36 | 42 |
|
| 43 | +constexpr char GENERIC_PREFIX[] = "Unknown OpenXR Runtime"; |
| 44 | + |
37 | 45 | void OpenXRSelectRuntime::_update_items() { |
38 | 46 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
39 | 47 | OS *os = OS::get_singleton(); |
@@ -102,23 +110,119 @@ void OpenXRSelectRuntime::_notification(int p_notification) { |
102 | 110 | } |
103 | 111 | } |
104 | 112 |
|
105 | | -OpenXRSelectRuntime::OpenXRSelectRuntime() { |
| 113 | +String OpenXRSelectRuntime::_try_and_get_runtime_name(const String &p_config_file) { |
| 114 | + if constexpr (!GD_IS_CLASS_ENABLED(JSON)) { |
| 115 | + return ""; |
| 116 | + } |
| 117 | + |
| 118 | + // Attempt to get a valid runtime name from the json file |
| 119 | + String file_contents = FileAccess::get_file_as_string(p_config_file); |
| 120 | + Dictionary root_node = JSON::parse_string(file_contents); |
| 121 | + if (!root_node.has("runtime")) { |
| 122 | + return ""; |
| 123 | + } |
| 124 | + Dictionary api_layer = root_node["runtime"]; |
| 125 | + if (!api_layer.has("name") || api_layer["name"].get_type() != Variant::STRING) { |
| 126 | + return ""; |
| 127 | + } |
| 128 | + return api_layer["name"]; |
| 129 | +} |
| 130 | + |
| 131 | +void OpenXRSelectRuntime::_add_runtime(Dictionary &r_runtimes, const String &p_config_file) { |
| 132 | + if (r_runtimes.values().has(p_config_file)) { |
| 133 | + // config file already in the list of runtimes, do not add a duplicate |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + String runtime_name = _try_and_get_runtime_name(p_config_file); |
| 138 | + if (runtime_name.is_empty()) { |
| 139 | + runtime_name = GENERIC_PREFIX; |
| 140 | + } |
| 141 | + |
| 142 | + if (r_runtimes.keys().has(runtime_name)) { |
| 143 | + // Highly unlikely, performance is not critical |
| 144 | + unsigned int index = 1; |
| 145 | + while (r_runtimes.keys().has(runtime_name + " " + uitos(index))) { |
| 146 | + index++; |
| 147 | + } |
| 148 | + runtime_name = runtime_name + " " + uitos(index); |
| 149 | + } |
| 150 | + r_runtimes[runtime_name] = p_config_file; |
| 151 | +} |
| 152 | + |
| 153 | +Dictionary OpenXRSelectRuntime::_enumerate_runtimes() { |
106 | 154 | Dictionary default_runtimes; |
107 | 155 |
|
108 | | - // Add known common runtimes by default. |
109 | | -#ifdef WINDOWS_ENABLED |
| 156 | +#if defined(WINDOWS_ENABLED) |
| 157 | + // Add known common runtimes in case they are not populated in registry |
110 | 158 | default_runtimes["Meta"] = "C:\\Program Files\\Oculus\\Support\\oculus-runtime\\oculus_openxr_64.json"; |
111 | 159 | default_runtimes["SteamVR"] = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\SteamVR\\steamxr_win64.json"; |
112 | 160 | default_runtimes["Varjo"] = "C:\\Program Files\\Varjo\\varjo-openxr\\VarjoOpenXR.json"; |
113 | 161 | default_runtimes["WMR"] = "C:\\WINDOWS\\system32\\MixedRealityRuntime.json"; |
114 | | -#endif |
115 | | -#ifdef LINUXBSD_ENABLED |
| 162 | + |
| 163 | + // Hard code openxr version 1. |
| 164 | + LPCWSTR runtimes_key = L"SOFTWARE\\Khronos\\OpenXR\\1\\AvailableRuntimes"; |
| 165 | + HKEY hkey = nullptr; |
| 166 | + LSTATUS result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, runtimes_key, 0, KEY_READ | KEY_QUERY_VALUE, &hkey); |
| 167 | + if (result != ERROR_SUCCESS) { |
| 168 | + return default_runtimes; |
| 169 | + } |
| 170 | + |
| 171 | + DWORD max_value_len, value_count; |
| 172 | + result = RegQueryInfoKeyW( |
| 173 | + hkey, // hKey |
| 174 | + nullptr, // lpClass |
| 175 | + nullptr, // lpcchClass |
| 176 | + nullptr, // lpReserved |
| 177 | + nullptr, // lpcSubKeys |
| 178 | + nullptr, // lpcbMaxSubKeyLen |
| 179 | + nullptr, // lpcbMaxClassLen |
| 180 | + &value_count, // lpcValues |
| 181 | + &max_value_len, // lpcbMaxValueNameLen |
| 182 | + nullptr, // lpcbMaxValueLen |
| 183 | + nullptr, // lpcbSecurityDescriptor |
| 184 | + nullptr // lpftLastWriteTime |
| 185 | + ); |
| 186 | + if (result != ERROR_SUCCESS) { |
| 187 | + return default_runtimes; |
| 188 | + } |
| 189 | + |
| 190 | + Char16String value_name; |
| 191 | + value_name.resize_uninitialized(max_value_len + 1); |
| 192 | + DWORD value_len, value_type; |
| 193 | + |
| 194 | + for (DWORD i = 0; i < value_count; i++) { |
| 195 | + value_len = max_value_len + 1; |
| 196 | + result = RegEnumValueW( |
| 197 | + hkey, // hKey |
| 198 | + i, // dwIndex |
| 199 | + (LPWSTR)value_name.get_data(), // lpValueName |
| 200 | + &value_len, // lpcchValueName |
| 201 | + nullptr, // lpReserved |
| 202 | + &value_type, // lpType |
| 203 | + nullptr, // lpData |
| 204 | + nullptr // lpcbData |
| 205 | + ); |
| 206 | + if (result != ERROR_SUCCESS || value_type != REG_DWORD) { |
| 207 | + continue; |
| 208 | + } |
| 209 | + |
| 210 | + _add_runtime(default_runtimes, String::utf16((const char16_t *)value_name.get_data())); |
| 211 | + } |
| 212 | + |
| 213 | + // Cleanup, close the key we opened |
| 214 | + RegCloseKey(hkey); |
| 215 | + |
| 216 | +#elif defined(LINUXBSD_ENABLED) |
116 | 217 | default_runtimes["Monado"] = "/usr/share/openxr/1/openxr_monado.json"; |
117 | 218 | default_runtimes["SteamVR"] = "~/.steam/steam/steamapps/common/SteamVR/steamxr_linux64.json"; |
118 | 219 | #endif |
| 220 | + return default_runtimes; |
| 221 | +} |
119 | 222 |
|
| 223 | +OpenXRSelectRuntime::OpenXRSelectRuntime() { |
120 | 224 | // TODO: Move to editor_settings.cpp |
121 | | - EDITOR_DEF_RST("xr/openxr/runtime_paths", default_runtimes); |
| 225 | + EDITOR_DEF_RST("xr/openxr/runtime_paths", _enumerate_runtimes()); |
122 | 226 |
|
123 | 227 | set_flat(true); |
124 | 228 | set_theme_type_variation("TopBarOptionButton"); |
|
0 commit comments