Files
EonaCat.XSBox/crates/engine/src/hle/modules/xgame.rs
T
EonaCat 49e10bb398
Rust validation / Formatting (push) Failing after 26s
EonaCat/ci-workflows: SonarQube / sonar (push) Successful in 49s
Rust validation / Windows x64 build and tests (push) Canceled after 0s
Initial
2026-12-04 20:15:38 +01:00

276 lines
7.1 KiB
Rust

#![allow(clippy::not_unsafe_ptr_arg_deref)]
use std::ffi::{CStr, c_char, c_void};
use super::xasync::E_INVALIDARG;
use super::xgameruntime::{
Guid, HRESULT, Interface, Methods, Object, S_OK, add_ref, missing, put, text, token, with_spare,
};
use crate::hle::runtime;
pub const TITLE_PROVIDER: Guid = Guid::new(
0x973a_344e,
0x24bf,
0x4d0f,
[0x84, 0x57, 0x56, 0xc5, 0x34, 0x89, 0x2b, 0x29],
);
pub const TITLE_IID: Guid = Guid::new(
0x2549_f142,
0x6419,
0x4a06,
[0x97, 0xb5, 0x93, 0x1a, 0xab, 0x7c, 0x2f, 0x34],
);
pub const FEATURES: Guid = Guid::new(
0x8836_fe87,
0xedb9,
0x4fe3,
[0x8d, 0xad, 0x05, 0xf0, 0xd2, 0xcd, 0x5b, 0x40],
);
pub const INVITES: Guid = Guid::new(
0x0651_aae2,
0x4012,
0x4077,
[0xbf, 0x84, 0x8b, 0x90, 0x97, 0x09, 0x0e, 0x2c],
);
pub const EVENTS: Guid = Guid::new(
0xbbfb_dcc7,
0xbfe7,
0x409b,
[0xa5, 0xca, 0xed, 0xf0, 0x54, 0x96, 0x0b, 0x4d],
);
pub const PENDING_INVITES_IID: Guid = Guid::new(
0x014d_1cc3,
0xbcfe,
0x41ff,
[0xb2, 0xf0, 0xe1, 0xef, 0x07, 0x15, 0x58, 0x28],
);
pub const ACTIVATION_PROVIDER: Guid = Guid::new(
0x7f0f_e8b8,
0xe075,
0x49ab,
[0x9a, 0xa7, 0xa1, 0xe0, 0x65, 0x48, 0x9a, 0x9e],
);
pub const ACTIVATION_IID: Guid = Guid::new(
0x2e4f_76fe,
0x0fc7,
0x461e,
[0xab, 0x4d, 0xa4, 0x49, 0x94, 0x34, 0xc3, 0xcf],
);
pub const PROTOCOL_PROVIDER: Guid = Guid::new(
0x95fd_18d2,
0x74dd,
0x4d7c,
[0xaa, 0x1b, 0x0b, 0x51, 0x82, 0x76, 0x65, 0xd6],
);
pub const PROTOCOL_IID: Guid = Guid::new(
0x026b_010c,
0x06c3,
0x4cdd,
[0xbb, 0xcb, 0x43, 0xf2, 0x29, 0xdb, 0x1c, 0xff],
);
pub static TITLE_OBJECT: Object = Object::new("title", &TITLE_VTABLE);
pub static ACTIVATION_OBJECT: Object = Object::new("activation", &ACTIVATION_VTABLE);
pub static PROTOCOL_OBJECT: Object = Object::new("protocol activation", &PROTOCOL_VTABLE);
pub static FEATURES_OBJECT: Object = Object::new("features", &FEATURES_VTABLE);
pub static INVITES_OBJECT: Object = Object::new("invites", &INVITES_VTABLE);
pub static EVENTS_OBJECT: Object = Object::new("events", &EVENTS_VTABLE);
pub const INTERFACES: &[Interface] = &[
(TITLE_PROVIDER, TITLE_IID, &TITLE_OBJECT),
(FEATURES, FEATURES, &FEATURES_OBJECT),
(INVITES, INVITES, &INVITES_OBJECT),
(INVITES, PENDING_INVITES_IID, &INVITES_OBJECT),
(EVENTS, EVENTS, &EVENTS_OBJECT),
(ACTIVATION_PROVIDER, ACTIVATION_IID, &ACTIVATION_OBJECT),
(PROTOCOL_PROVIDER, PROTOCOL_IID, &PROTOCOL_OBJECT),
];
type This = *const Object;
extern "system" fn title_id(_: This, out: *mut u32) -> HRESULT {
put(out, runtime::title().id)
}
extern "system" fn launch_new_game(
_: This,
exe: *const c_char,
args: *const c_char,
_user: *const c_void,
) {
let (exe, args) = (text(exe).unwrap_or_default(), text(args).unwrap_or_default());
tracing::info!(exe, args, "XLaunchNewGame");
let current = runtime::main_image()
.and_then(runtime::image_path)
.unwrap_or_default();
let ours: Vec<_> = std::env::args_os().skip(1).collect();
let command = relaunch_args(ours, current.as_os_str(), &exe, &args);
#[cfg(windows)]
{
super::lifecycle::end_title(
"XLaunchNewGame",
Box::new(move || super::lifecycle::relaunch(command)),
);
loop {
std::thread::park();
}
}
#[cfg(not(windows))]
let _ = command;
}
fn relaunch_args(
ours: Vec<std::ffi::OsString>,
current: &std::ffi::OsStr,
exe: &str,
args: &str,
) -> Vec<std::ffi::OsString> {
let mut out: Vec<_> = ours.into_iter().take_while(|a| a != "--").collect();
if !exe.is_empty()
&& let Some(slot) = out.iter_mut().find(|a| {
a.as_os_str() == current
|| std::path::absolute(a).is_ok_and(|p| p.as_os_str() == current)
})
{
*slot = exe.into();
}
if !args.trim().is_empty() {
out.push("--".into());
out.extend(split_args(args).into_iter().map(Into::into));
}
out
}
fn split_args(args: &str) -> Vec<String> {
let mut out = Vec::new();
let mut current = String::new();
let mut quoted = false;
let mut started = false;
for c in args.chars() {
match c {
'"' => {
quoted = !quoted;
started = true;
}
c if !quoted && c.is_whitespace() => {
if started {
out.push(std::mem::take(&mut current));
started = false;
}
}
c => {
current.push(c);
started = true;
}
}
}
if started {
out.push(current);
}
out
}
const AVAILABLE: [u32; 20] = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20,
];
extern "system" fn is_feature_available(_: This, feature: u32) -> bool {
AVAILABLE.contains(&feature)
}
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
}
extern "system" fn accept_pending_invite(_: This, uri: *const c_char) -> HRESULT {
tracing::info!(
uri = text(uri).unwrap_or_default(),
"an invite accepted: none is pending offline"
);
E_INVALIDARG
}
extern "system" fn write_event(
_: This,
_user: *const c_void,
_service_config: *const c_char,
_session: *const c_char,
name: *const c_char,
_dimensions: *const c_char,
_measurements: *const c_char,
) -> HRESULT {
if !name.is_null() {
let name = unsafe { CStr::from_ptr(name) }.to_string_lossy();
tracing::debug!(%name, "game event dropped");
}
S_OK
}
static TITLE_VTABLE: Methods<14> = with_spare([
missing::<0> as *const (),
add_ref as *const (),
add_ref as *const (),
title_id as *const (),
launch_new_game as *const (),
missing::<5> as *const (),
]);
static FEATURES_VTABLE: Methods<12> = with_spare([
missing::<0> as *const (),
add_ref as *const (),
add_ref as *const (),
is_feature_available as *const (),
]);
static INVITES_VTABLE: Methods<16> = with_spare([
missing::<0> as *const (),
add_ref as *const (),
add_ref as *const (),
register as *const (),
unregister as *const (),
register as *const (),
unregister as *const (),
accept_pending_invite as *const (),
]);
static ACTIVATION_VTABLE: Methods<6> = Methods([
missing::<0> as *const (),
add_ref as *const (),
add_ref as *const (),
register as *const (),
unregister as *const (),
accept_pending_invite as *const (),
]);
static PROTOCOL_VTABLE: Methods<5> = Methods([
missing::<0> as *const (),
add_ref as *const (),
add_ref as *const (),
register as *const (),
unregister as *const (),
]);
static EVENTS_VTABLE: Methods<12> = with_spare([
missing::<0> as *const (),
add_ref as *const (),
add_ref as *const (),
write_event as *const (),
]);