#![allow(clippy::not_unsafe_ptr_arg_deref)] use std::ffi::c_void; use super::xgameruntime::{ Guid, HRESULT, Interface, Methods, Object, S_OK, add_ref, missing, put_string, with_spare, }; pub const PROVIDER: Guid = Guid::new( 0xe349_bd1a, 0xfc20, 0x4e40, [0xb9, 0x9c, 0x41, 0x78, 0xcc, 0x6b, 0x40, 0x9f], ); pub const IID: Guid = Guid::new( 0x6fd7_1f09, 0x7513, 0x49f0, [0x89, 0xbc, 0xbf, 0xaf, 0x5d, 0xf6, 0xf8, 0x52], ); pub const ANALYTICS: Guid = Guid::new( 0xb884_675d, 0xb738, 0x4a9c, [0x81, 0x5d, 0x9a, 0x9a, 0x1e, 0x0c, 0x6c, 0x9b], ); const CONSOLE_ID: &str = "00000000000000000000000000000000000001"; const SANDBOX: &str = "RETAIL"; const DEVICE_ID: &str = "VkljdG9yeSBvZmZsaW5lIGRldmljZSBpZGVudGl0eSE="; pub static OBJECT: Object = Object::new("system", &VTABLE); pub static ANALYTICS_OBJECT: Object = Object::new("analytics", &ANALYTICS_VTABLE); pub const IID_2026: Guid = Guid::new( 0x1861_cf2e, 0xe18b, 0x4834, [0xa9, 0xf5, 0xb4, 0xa4, 0xe6, 0xef, 0xb4, 0xcf], ); pub const INTERFACES: &[Interface] = &[ (PROVIDER, IID, &OBJECT), (PROVIDER, IID_2026, &OBJECT), (ANALYTICS, ANALYTICS, &ANALYTICS_OBJECT), ]; type This = *const Object; extern "system" fn console_id(_: This, size: usize, buffer: *mut u8, used: *mut usize) -> HRESULT { put_string(CONSOLE_ID, size, buffer, used) } extern "system" fn sandbox_id(_: This, size: usize, buffer: *mut u8, used: *mut usize) -> HRESULT { put_string(SANDBOX, size, buffer, used) } extern "system" fn device_id(_: This, size: usize, buffer: *mut u8, used: *mut usize) -> HRESULT { put_string(DEVICE_ID, size, buffer, used) } #[repr(C)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Version { pub major: u16, pub minor: u16, pub build: u16, pub revision: u16, } #[repr(C)] pub struct AnalyticsInfo { pub os: Version, pub hosting_os: Version, pub family: [u8; 64], pub form: [u8; 64], } const OS: Version = Version { major: 10, minor: 0, build: 22000, revision: 0, }; fn text(s: &str) -> [u8; 64] { let mut out = [0; 64]; out[..s.len()].copy_from_slice(s.as_bytes()); out } extern "system" fn analytics_info(_: This, out: *mut AnalyticsInfo) -> *mut AnalyticsInfo { let info = AnalyticsInfo { os: OS, hosting_os: OS, family: text("Xbox"), form: text(eonacat_xsbox_gpu::console::current().name()), }; unsafe { out.write(info) }; out } extern "system" fn handle_track(_: This, _callback: *const (), _context: *mut c_void) -> HRESULT { tracing::debug!("XSystemHandleTrack: runtime handles are not reported"); S_OK } extern "system" fn is_handle_valid(_: This, _handle: *const c_void) -> bool { true } extern "system" fn allow_full_download_bandwidth(_: This, _enable: bool) {} static VTABLE: Methods<17> = with_spare([ missing::<0> as *const (), add_ref as *const (), add_ref as *const (), console_id as *const (), sandbox_id as *const (), device_id as *const (), handle_track as *const (), is_handle_valid as *const (), allow_full_download_bandwidth as *const (), ]); static ANALYTICS_VTABLE: Methods<12> = with_spare([ missing::<0> as *const (), add_ref as *const (), add_ref as *const (), analytics_info as *const (), ]); #[cfg(windows)] mod device_type { #![allow(non_snake_case)] use std::ffi::c_void; use eonacat_xsbox_gpu::console::{self, Console}; use crate::hle::modules::files::host; use crate::hle::{HleModule}; use crate::hle_export; type Key = *mut c_void; const LOCAL_MACHINE: Key = 0xffff_ffff_8000_0002_u64 as Key; const KEY: &str = r"Software\Microsoft\Windows NT\CurrentVersion\OEM"; const VALUE: &str = "ConsoleMode"; const SUCCESS: u32 = 0; const FILE_NOT_FOUND: u32 = 2; const INVALID_PARAMETER: u32 = 87; const MORE_DATA: u32 = 234; const DWORD: u32 = 4; static OEM: u8 = 0; fn oem() -> Key { (&raw const OEM).cast_mut().cast() } pub(super) fn console_mode(console: Console) -> u32 { match console { Console::SeriesX => 0x64, Console::SeriesS => 0x60, } } fn is(text: *const u16, expected: &str) -> bool { if text.is_null() { return false; } let text = unsafe { let len = (0..).take_while(|&i| *text.add(i) != 0).count(); String::from_utf16_lossy(std::slice::from_raw_parts(text, len)) }; text.eq_ignore_ascii_case(expected) } fn open(root: Key, sub: *const u16, out: *mut Key) -> Option { if root == oem() { return Some(FILE_NOT_FOUND); } if root == LOCAL_MACHINE && is(sub, KEY) && !out.is_null() { unsafe { out.write(oem()) }; return Some(SUCCESS); } None } pub extern "system" fn RegOpenKeyW(root: Key, sub: *const u16, out: *mut Key) -> u32 { open(root, sub, out).unwrap_or_else(|| { host!("advapi32.dll", "RegOpenKeyW": extern "system" fn(Key, *const u16, *mut Key) -> u32)( root, sub, out, ) }) } pub extern "system" fn RegOpenKeyExW( root: Key, sub: *const u16, options: u32, access: u32, out: *mut Key, ) -> u32 { open(root, sub, out).unwrap_or_else(|| { host!("advapi32.dll", "RegOpenKeyExW": extern "system" fn( Key, *const u16, u32, u32, *mut Key, ) -> u32)(root, sub, options, access, out) }) } pub extern "system" fn RegQueryValueExW( key: Key, name: *const u16, reserved: *mut u32, kind: *mut u32, data: *mut u8, size: *mut u32, ) -> u32 { if key != oem() { return host!("advapi32.dll", "RegQueryValueExW": extern "system" fn( Key, *const u16, *mut u32, *mut u32, *mut u8, *mut u32, ) -> u32)(key, name, reserved, kind, data, size); } if !is(name, VALUE) { return FILE_NOT_FOUND; } if !data.is_null() && size.is_null() { return INVALID_PARAMETER; } unsafe { if !kind.is_null() { kind.write(DWORD); } if size.is_null() { return SUCCESS; } let room = size.read(); size.write(4); if data.is_null() { return SUCCESS; } if room < 4 { return MORE_DATA; } data.cast::() .write_unaligned(console_mode(console::current())); } tracing::debug!(console = console::current().name(), "device type read"); SUCCESS } pub extern "system" fn RegCloseKey(key: Key) -> u32 { if key == oem() { return SUCCESS; } host!("advapi32.dll", "RegCloseKey": extern "system" fn(Key) -> u32)(key) } pub(crate) fn module() -> HleModule { HleModule { name: "device type", dlls: &[ "advapi32.dll", "kernelbase.dll", "api-ms-win-core-registry-*", ], exports: vec![ hle_export!(RegOpenKeyW), hle_export!(RegOpenKeyExW), hle_export!(RegQueryValueExW), hle_export!(RegCloseKey), ], } } } #[cfg(windows)] pub(crate) use device_type::module;