Loader: map title-shipped DLLs, patch static TLS loads to slot 0, crash reporter, title paths for GetModuleFileName*. HLE: task queue and async, XUser (one offline user), system ids, title id, features, invites, game events, persistent local storage, GameInput with no devices, xmem, console audio hardware queues, life cycle (close suspends, then terminates), and a file layer with virtual drives rewritten in path-taking imports. GPU: D3D12.X device, DXGI chain, heaps, resources, fences, queues and command lists translated onto a host D3D12 device; clears, barriers, copies, resolves and PresentX into the title's window. Shaders: SGD5 reader and `victory translate`; SSA IR with node splitting for irreducible flow; structurisation removing EXEC. Co-Authored-By: Claude Opus 5.5 <[email protected]>
299 lines
9.6 KiB
Rust
299 lines
9.6 KiB
Rust
//! XUser: one offline local user, signed in, never a guest, with every
|
|
//! privilege. Served as the user provider's objects from `QueryApiImpl`;
|
|
//! adding a user completes through `xasync` like any other async call. No
|
|
//! input device is associated with the user (there are none, `gameinput`).
|
|
#![allow(clippy::not_unsafe_ptr_arg_deref)]
|
|
|
|
use std::ffi::c_void;
|
|
|
|
use super::xasync::{self, AsyncBlock, ProviderData};
|
|
use super::xgameruntime::{
|
|
Guid, HRESULT, Interface, Methods, Object, S_OK, add_ref, missing, put, put_string, token,
|
|
};
|
|
|
|
/// The user provider.
|
|
pub const PROVIDER: Guid = Guid {
|
|
data1: 0x01ac_d177,
|
|
data2: 0x91f9,
|
|
data3: 0x4763,
|
|
data4: [0xa3, 0x8e, 0xcc, 0xbb, 0x55, 0xce, 0x32, 0xe0],
|
|
};
|
|
|
|
/// Its main interface.
|
|
pub const IID: Guid = Guid {
|
|
data1: 0xeb9b_f948,
|
|
data2: 0x18dc,
|
|
data3: 0x4d82,
|
|
data4: [0xbb, 0xcc, 0x40, 0xe0, 0xa8, 0x09, 0xc4, 0xc0],
|
|
};
|
|
|
|
/// The interface titles ask for to read a gamertag.
|
|
pub const GAMERTAG_IID: Guid = Guid {
|
|
data1: 0xcef4_fac0,
|
|
data2: 0x7676,
|
|
data3: 0x4a94,
|
|
data4: [0xa1, 0x19, 0x4c, 0x43, 0xf9, 0xeb, 0x5b, 0x74],
|
|
};
|
|
|
|
/// The user's devices and audio endpoints.
|
|
pub const DEVICE_PROVIDER: Guid = Guid::new(
|
|
0x7d82_4997,
|
|
0x10dc,
|
|
0x45ab,
|
|
[0x86, 0xb7, 0x27, 0x37, 0x76, 0x7c, 0x0b, 0xf1],
|
|
);
|
|
|
|
/// The device interface as Minecraft (2022) asks for it.
|
|
pub const DEVICE_IID: Guid = Guid::new(
|
|
0x0cc6_a956,
|
|
0xe7e1,
|
|
0x4fdf,
|
|
[0x93, 0x41, 0x9d, 0x5d, 0xa6, 0x49, 0xeb, 0xc8],
|
|
);
|
|
|
|
/// The documented "no such user" error.
|
|
pub const USER_NOT_FOUND: HRESULT = 0x8924_5104_u32 as i32;
|
|
|
|
pub const XUID: u64 = 0x0009_0000_0000_0001;
|
|
pub const LOCAL_ID: u64 = 1;
|
|
const GAMERTAG: &str = "Player";
|
|
|
|
/// The only user; a handle is its address, duplicating and closing are free.
|
|
pub struct User;
|
|
static USER: User = User;
|
|
type Handle = *const User;
|
|
|
|
fn user() -> Handle {
|
|
&USER
|
|
}
|
|
|
|
fn found(found: bool, out: *mut Handle) -> HRESULT {
|
|
if found {
|
|
put(out, user())
|
|
} else {
|
|
USER_NOT_FOUND
|
|
}
|
|
}
|
|
|
|
pub static OBJECT: Object = Object::new("user", &VTABLE);
|
|
pub static GAMERTAG_OBJECT: Object = Object::new("user gamertag", &GAMERTAG_VTABLE);
|
|
pub static DEVICE_OBJECT: Object = Object::new("user devices", &DEVICE_VTABLE);
|
|
|
|
pub const INTERFACES: &[Interface] = &[
|
|
(PROVIDER, IID, &OBJECT),
|
|
(PROVIDER, GAMERTAG_IID, &GAMERTAG_OBJECT),
|
|
(DEVICE_PROVIDER, DEVICE_IID, &DEVICE_OBJECT),
|
|
];
|
|
|
|
type This = *const Object;
|
|
extern "system" fn duplicate(_: This, _: Handle, out: *mut Handle) -> HRESULT {
|
|
put(out, user())
|
|
}
|
|
extern "system" fn close(_: This, _: Handle) {}
|
|
extern "system" fn compare(_: This, a: Handle, b: Handle) -> i32 {
|
|
i32::from(a != b)
|
|
}
|
|
|
|
/// The async provider for adding a user: done at once, the result is a
|
|
/// handle.
|
|
extern "system" fn add_provider(op: u32, data: *const ProviderData) -> HRESULT {
|
|
const BEGIN: u32 = 0;
|
|
const GET_RESULT: u32 = 2;
|
|
// SAFETY: the async runtime passes valid provider data.
|
|
let data = unsafe { &*data };
|
|
match op {
|
|
BEGIN => xasync::XAsyncComplete(data.block, S_OK, size_of::<Handle>()),
|
|
GET_RESULT => return put(data.buffer.cast::<Handle>(), user()),
|
|
_ => {}
|
|
}
|
|
S_OK
|
|
}
|
|
|
|
/// Tells our add calls apart from other async calls on the same block.
|
|
static ADD: u8 = 0;
|
|
|
|
/// Any options add the one user, silently.
|
|
extern "system" fn add_async(_: This, options: u32, block: *mut AsyncBlock) -> HRESULT {
|
|
tracing::info!(options, "XUserAddAsync: the offline local user");
|
|
xasync::XAsyncBegin(
|
|
block,
|
|
std::ptr::null_mut(),
|
|
(&raw const ADD).cast(),
|
|
std::ptr::null(),
|
|
Some(add_provider),
|
|
)
|
|
}
|
|
extern "system" fn add_result(_: This, block: *mut AsyncBlock, out: *mut Handle) -> HRESULT {
|
|
xasync::XAsyncGetResult(
|
|
block,
|
|
(&raw const ADD).cast(),
|
|
size_of::<Handle>(),
|
|
out.cast(),
|
|
std::ptr::null_mut(),
|
|
)
|
|
}
|
|
extern "system" fn get_local_id(_: This, _: Handle, out: *mut u64) -> HRESULT {
|
|
put(out, LOCAL_ID)
|
|
}
|
|
extern "system" fn find_by_local_id(_: This, id: u64, out: *mut Handle) -> HRESULT {
|
|
found(id == LOCAL_ID, out)
|
|
}
|
|
extern "system" fn get_id(_: This, _: Handle, out: *mut u64) -> HRESULT {
|
|
put(out, XUID)
|
|
}
|
|
extern "system" fn find_by_id(_: This, id: u64, out: *mut Handle) -> HRESULT {
|
|
found(id == XUID, out)
|
|
}
|
|
extern "system" fn get_is_guest(_: This, _: Handle, out: *mut bool) -> HRESULT {
|
|
put(out, false)
|
|
}
|
|
/// 0: signed in.
|
|
extern "system" fn get_state(_: This, _: Handle, out: *mut u32) -> HRESULT {
|
|
put(out, 0)
|
|
}
|
|
/// The same name for every component (classic, modern, suffix, unique).
|
|
extern "system" fn get_gamertag(
|
|
_: This,
|
|
_: Handle,
|
|
_component: u32,
|
|
size: usize,
|
|
buffer: *mut u8,
|
|
used: *mut usize,
|
|
) -> HRESULT {
|
|
put_string(GAMERTAG, size, buffer, used)
|
|
}
|
|
/// Granted; deny reason 0 (none).
|
|
extern "system" fn check_privilege(
|
|
_: This,
|
|
_: Handle,
|
|
_options: u32,
|
|
_privilege: u32,
|
|
has: *mut bool,
|
|
reason: *mut u32,
|
|
) -> HRESULT {
|
|
if !reason.is_null() {
|
|
// SAFETY: checked non-null.
|
|
unsafe { reason.write(0) };
|
|
}
|
|
put(has, true)
|
|
}
|
|
|
|
/// The user never changes and no device is ever associated, so these
|
|
/// callbacks are never called.
|
|
extern "system" fn register(
|
|
_: This,
|
|
_queue: *const c_void,
|
|
_context: *mut c_void,
|
|
_callback: *const c_void,
|
|
out: *mut u64,
|
|
) -> HRESULT {
|
|
put(out, token())
|
|
}
|
|
extern "system" fn unregister(_: This, _token: u64, _wait: bool) -> bool {
|
|
true
|
|
}
|
|
|
|
/// No input device belongs to a user.
|
|
extern "system" fn find_for_device(_: This, _device: *const c_void, _: *mut Handle) -> HRESULT {
|
|
USER_NOT_FOUND
|
|
}
|
|
|
|
// Unset slots: gamer pictures, token and signature, issue resolution.
|
|
static VTABLE: Methods<35> = Methods([
|
|
missing::<0> as *const (), // 0x00 QueryInterface
|
|
add_ref as *const (), // 0x08 AddRef
|
|
add_ref as *const (), // 0x10 Release: the object is static
|
|
duplicate as *const (), // 0x18
|
|
close as *const (), // 0x20
|
|
compare as *const (), // 0x28
|
|
missing::<6> as *const (), // 0x30 maximum users
|
|
add_async as *const (), // 0x38
|
|
add_result as *const (), // 0x40
|
|
get_local_id as *const (), // 0x48
|
|
find_by_local_id as *const (), // 0x50
|
|
get_id as *const (), // 0x58
|
|
find_by_id as *const (), // 0x60
|
|
get_is_guest as *const (), // 0x68
|
|
get_state as *const (), // 0x70
|
|
get_gamertag as *const (), // 0x78 older gamertag slot
|
|
missing::<16> as *const (), // 0x80 gamer picture: begin
|
|
missing::<17> as *const (), // 0x88 result size
|
|
missing::<18> as *const (), // 0x90 result
|
|
missing::<19> as *const (), // 0x98 age group
|
|
check_privilege as *const (), // 0xa0
|
|
missing::<21> as *const (), // 0xa8 resolve privilege with UI: begin
|
|
missing::<22> as *const (), // 0xb0 result
|
|
missing::<23> as *const (), // 0xb8 token and signature: begin
|
|
missing::<24> as *const (), // 0xc0 result size
|
|
missing::<25> as *const (), // 0xc8 result
|
|
missing::<26> as *const (), // 0xd0 the same, UTF-16: begin
|
|
missing::<27> as *const (), // 0xd8 result size
|
|
missing::<28> as *const (), // 0xe0 result
|
|
missing::<29> as *const (), // 0xe8 resolve issue with UI: begin
|
|
missing::<30> as *const (), // 0xf0 result
|
|
missing::<31> as *const (), // 0xf8 the same, UTF-16: begin
|
|
missing::<32> as *const (), // 0x100 result
|
|
register as *const (), // 0x108 change event
|
|
unregister as *const (), // 0x110
|
|
]);
|
|
|
|
static GAMERTAG_VTABLE: Methods<4> = Methods([
|
|
missing::<0> as *const (),
|
|
add_ref as *const (),
|
|
add_ref as *const (),
|
|
get_gamertag as *const (), // 0x18
|
|
]);
|
|
|
|
static DEVICE_VTABLE: Methods<6> = Methods([
|
|
missing::<0> as *const (),
|
|
add_ref as *const (),
|
|
add_ref as *const (),
|
|
find_for_device as *const (), // 0x18
|
|
register as *const (), // 0x20 device association changed
|
|
unregister as *const (), // 0x28
|
|
]);
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::modules::xasync::E_NOT_SUFFICIENT_BUFFER;
|
|
use crate::modules::xgameruntime::method as slot;
|
|
|
|
#[test]
|
|
fn startup_adds_the_user_and_reads_its_id() {
|
|
type Add = extern "system" fn(This, u32, *mut AsyncBlock) -> HRESULT;
|
|
type AddResult = extern "system" fn(This, *mut AsyncBlock, *mut Handle) -> HRESULT;
|
|
type GetId = extern "system" fn(This, Handle, *mut u64) -> HRESULT;
|
|
type Gamertag =
|
|
extern "system" fn(This, Handle, u32, usize, *mut u8, *mut usize) -> HRESULT;
|
|
let this: This = &OBJECT;
|
|
let mut block = AsyncBlock {
|
|
queue: std::ptr::null(),
|
|
context: std::ptr::null_mut(),
|
|
callback: None,
|
|
internal: [0; 4],
|
|
};
|
|
assert_eq!(slot::<Add>(&OBJECT, 0x38)(this, 1, &mut block), S_OK);
|
|
assert_eq!(xasync::XAsyncGetStatus(&mut block, true), S_OK);
|
|
let mut handle = std::ptr::null();
|
|
assert_eq!(
|
|
slot::<AddResult>(&OBJECT, 0x40)(this, &mut block, &mut handle),
|
|
S_OK
|
|
);
|
|
assert!(!handle.is_null());
|
|
let mut xuid = 0;
|
|
assert_eq!(slot::<GetId>(&OBJECT, 0x58)(this, handle, &mut xuid), S_OK);
|
|
assert_eq!(xuid, XUID);
|
|
|
|
let mut name = [0u8; 16];
|
|
let mut used = 0;
|
|
let get = slot::<Gamertag>(&GAMERTAG_OBJECT, 0x18);
|
|
assert_eq!(get(this, handle, 0, 16, name.as_mut_ptr(), &mut used), S_OK);
|
|
assert_eq!(&name[..used], b"Player\0");
|
|
assert_eq!(
|
|
get(this, handle, 0, 2, name.as_mut_ptr(), &mut used),
|
|
E_NOT_SUFFICIENT_BUFFER
|
|
);
|
|
}
|
|
}
|