Files
EonaCat.XSBox/crates/engine/src/hle/modules/xpackage.rs
T
EonaCat 53e6e612c9
Rust validation / Formatting (push) Failing after 26s
EonaCat/ci-workflows: SonarQube / sonar (push) Successful in 47s
Rust validation / Windows x64 build and tests (push) Canceled after 0s
Initial
2026-12-04 21:27:30 +01:00

455 lines
12 KiB
Rust

#![allow(clippy::not_unsafe_ptr_arg_deref)]
use std::ffi::{c_char, c_void};
use std::path::Path;
use std::sync::OnceLock;
use super::xasync::{self, AsyncBlock};
use super::xgameruntime::{
Guid, HRESULT, Interface, Methods, Object, S_OK, add_ref, missing, put, put_string, text,
token, with_spare,
};
use crate::hle::runtime;
pub const PROVIDER: Guid = Guid::new(
0xaf40_6016,
0xe850,
0x4aa8,
[0xa8, 0x8d, 0x2f, 0x3d, 0xcb, 0x9d, 0xac, 0x7e],
);
pub const IID_2026: Guid = Guid::new(
0x0b9d_556e,
0xab70,
0x43f4,
[0xba, 0xa0, 0x99, 0x99, 0xac, 0x4c, 0xb0, 0x40],
);
pub static OBJECT: Object = Object::new("package", &VTABLE);
pub const INTERFACES: &[Interface] = &[(PROVIDER, IID_2026, &OBJECT)];
pub const NO_PACKAGE: HRESULT = 0x8924_5208_u32 as i32;
pub fn identifier() -> String {
format!("{:08X}", runtime::title().id)
}
fn is_ours(id: *const c_char) -> bool {
text(id).is_some_and(|id| id == identifier())
}
fn package_bytes() -> u64 {
fn size(dir: &Path) -> u64 {
let Ok(entries) = std::fs::read_dir(dir) else {
return 0;
};
entries
.flatten()
.map(|e| match e.metadata() {
Ok(m) if m.is_dir() => size(&e.path()),
Ok(m) => m.len(),
Err(_) => 0,
})
.sum()
}
static BYTES: OnceLock<u64> = OnceLock::new();
static STARTED: std::sync::Once = std::sync::Once::new();
STARTED.call_once(|| {
let folder = runtime::main_image()
.and_then(runtime::image_path)
.and_then(|exe| exe.parent().map(Path::to_path_buf));
let counted = std::thread::Builder::new().name("package size".into()).spawn(move || {
let _ = BYTES.set(folder.as_deref().map_or(0, size));
});
if counted.is_err() {
let _ = BYTES.set(0);
}
});
BYTES.get().copied().unwrap_or(0)
}
type This = *const Object;
extern "system" fn current_identifier(_: This, size: usize, buffer: *mut u8) -> HRESULT {
package_bytes();
put_string(&identifier(), size, buffer, std::ptr::null_mut())
}
extern "system" fn is_packaged(_: This) -> bool {
true
}
static MONITOR: u8 = 0;
fn monitor() -> *const c_void {
(&raw const MONITOR).cast()
}
extern "system" fn create_monitor(
_: This,
id: *const c_char,
_selector_count: u32,
_selectors: *const c_void,
_interval_ms: u32,
_queue: *const c_void,
out: *mut *const c_void,
) -> HRESULT {
if !is_ours(id) {
return NO_PACKAGE;
}
put(out, monitor())
}
extern "system" fn close_monitor(_: This, _: *const c_void) {}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Progress {
pub total_bytes: u64,
pub installed_bytes: u64,
pub launch_bytes: u64,
pub launchable: bool,
pub completed: bool,
}
extern "system" fn progress(_: This, _: *const c_void, out: *mut Progress) {
let bytes = package_bytes();
put(
out,
Progress {
total_bytes: bytes,
installed_bytes: bytes,
launch_bytes: bytes,
launchable: true,
completed: true,
},
);
}
extern "system" fn register_progress(
_: This,
_monitor: *const c_void,
_context: *mut c_void,
_callback: *const c_void,
out: *mut u64,
) -> HRESULT {
put(out, token())
}
extern "system" fn unregister_progress(
_: This,
_monitor: *const c_void,
_token: u64,
_wait: bool,
) -> bool {
true
}
extern "system" fn update_monitor(_: This, _monitor: *const c_void) -> bool {
false
}
extern "system" fn register_installed(
_: This,
_queue: *const c_void,
_context: *mut c_void,
_callback: *const c_void,
out: *mut u64,
) -> HRESULT {
put(out, token())
}
extern "system" fn unregister_installed(_: This, _token: u64, _wait: bool) -> bool {
true
}
extern "system" fn find_availability(
_: This,
id: *const c_char,
_selector_count: u32,
_selectors: *const c_void,
out: *mut u32,
) -> HRESULT {
if !is_ours(id) {
return NO_PACKAGE;
}
put(out, 0)
}
extern "system" fn change_order(
_: This,
id: *const c_char,
_count: u32,
_selectors: *const c_void,
) -> HRESULT {
if is_ours(id) { S_OK } else { NO_PACKAGE }
}
static INSTALL: u8 = 0;
extern "system" fn install_chunks_async(
_: This,
id: *const c_char,
_count: u32,
_selectors: *const c_void,
_interval_ms: u32,
_suppress_confirmation: bool,
block: *mut AsyncBlock,
) -> HRESULT {
let result = if is_ours(id) {
Ok(xasync::bytes(monitor()))
} else {
Err(NO_PACKAGE)
};
xasync::finished_with(block, (&raw const INSTALL).cast(), result)
}
extern "system" fn install_chunks_result(
_: This,
block: *mut AsyncBlock,
out: *mut *const c_void,
) -> HRESULT {
xasync::result(block, (&raw const INSTALL).cast(), out)
}
extern "system" fn estimate_download(
_: This,
id: *const c_char,
_count: u32,
_selectors: *const c_void,
bytes: *mut u64,
confirm: *mut bool,
) -> HRESULT {
if !is_ours(id) {
return NO_PACKAGE;
}
if !confirm.is_null() {
unsafe { confirm.write(false) };
}
put(bytes, 0)
}
extern "system" fn uninstall_chunks(
_: This,
id: *const c_char,
_count: u32,
_selectors: *const c_void,
) -> HRESULT {
if is_ours(id) { S_OK } else { NO_PACKAGE }
}
extern "system" fn mount_path(_: This, _: *const c_void, _: usize, _: *mut u8) -> HRESULT {
xasync::E_INVALIDARG
}
extern "system" fn close_mount(_: This, _: *const c_void) {}
extern "system" fn uninstall_package(_: This, id: *const c_char) -> bool {
tracing::info!(
id = text(id).unwrap_or_default(),
"package uninstall refused"
);
false
}
static MOUNT: u8 = 0;
extern "system" fn mount_async(_: This, id: *const c_char, block: *mut AsyncBlock) -> HRESULT {
tracing::info!(
id = text(id).unwrap_or_default(),
"package mount: not installed"
);
xasync::finished(block, (&raw const MOUNT).cast(), NO_PACKAGE)
}
extern "system" fn mount_result(
_: This,
block: *mut AsyncBlock,
out: *mut *const c_void,
) -> HRESULT {
xasync::result(block, (&raw const MOUNT).cast(), out)
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Details {
pub identifier: *const c_char,
pub version: [u16; 4],
pub kind: u32,
pub display_name: *const c_char,
pub description: *const c_char,
pub publisher: *const c_char,
pub store_id: *const c_char,
pub installing: bool,
pub index: u32,
pub count: u32,
pub age_restricted: bool,
pub title_id: *const c_char,
}
type EnumerationCallback = extern "system" fn(*mut c_void, *const Details) -> bool;
extern "system" fn enumerate(
_: This,
kind: u32,
_scope: u32,
context: *mut c_void,
callback: *const c_void,
) -> HRESULT {
if kind != 0 || callback.is_null() {
tracing::debug!(kind, "packages enumerated: none");
return S_OK;
}
let title = runtime::title();
let text = |s: String| std::ffi::CString::new(s).unwrap_or_default();
let (identifier, store_id, title_id, empty) =
(text(identifier()), text(title.store_id.clone()), text(format!("{:08X}", title.id)), text(String::new()));
let details = Details {
identifier: identifier.as_ptr(),
version: [0; 4],
kind: 0,
display_name: empty.as_ptr(),
description: empty.as_ptr(),
publisher: empty.as_ptr(),
store_id: store_id.as_ptr(),
installing: false,
index: 0,
count: 1,
age_restricted: false,
title_id: title_id.as_ptr(),
};
let callback: EnumerationCallback = unsafe { std::mem::transmute(callback) };
callback(context, &details);
S_OK
}
pub fn user_locale() -> String {
#[cfg(windows)]
{
use windows_sys::Win32::Globalization::GetUserDefaultLocaleName;
let mut name = [0u16; 85];
let len = unsafe { GetUserDefaultLocaleName(name.as_mut_ptr(), name.len() as i32) };
if len > 1 {
return String::from_utf16_lossy(&name[..len as usize - 1]);
}
}
"en-US".into()
}
extern "system" fn get_user_locale(_: This, size: usize, buffer: *mut u8) -> HRESULT {
put_string(&user_locale(), size, buffer, std::ptr::null_mut())
}
extern "system" fn enumerate_chunk_availability(
_: This,
id: *const c_char,
_kind: u32,
_context: *mut c_void,
_callback: *const c_void,
) -> HRESULT {
if is_ours(id) { S_OK } else { NO_PACKAGE }
}
extern "system" fn install_chunks(
_: This,
id: *const c_char,
_count: u32,
_selectors: *const c_void,
_interval_ms: u32,
_suppress_confirmation: bool,
_queue: *const c_void,
out: *mut *const c_void,
) -> HRESULT {
if !is_ours(id) {
return NO_PACKAGE;
}
put(out, monitor())
}
extern "system" fn mount_path_size(_: This, _: *const c_void, _: *mut usize) -> HRESULT {
xasync::E_INVALIDARG
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WriteStats {
pub interval: u64,
pub budget: u64,
pub elapsed: u64,
pub bytes_written: u64,
}
extern "system" fn write_stats(_: This, out: *mut WriteStats) -> HRESULT {
put(out, WriteStats { interval: 300, budget: 1 << 40, elapsed: 0, bytes_written: 0 })
}
extern "system" fn uninstall_uwp_instance(_: This, name: *const c_char) -> HRESULT {
tracing::info!(name = text(name).unwrap_or_default(), "UWP instance uninstall: none installed");
NO_PACKAGE
}
extern "system" fn enumerate_features(
_: This,
id: *const c_char,
_context: *mut c_void,
_callback: *const c_void,
) -> HRESULT {
if is_ours(id) { S_OK } else { NO_PACKAGE }
}
extern "system" fn package_kind(_: This, id: *const c_char, out: *mut u32) -> HRESULT {
if !is_ours(id) {
return NO_PACKAGE;
}
put(out, 0u32)
}
static VTABLE: Methods<50> = with_spare([
missing::<0> as *const (),
add_ref as *const (),
add_ref as *const (),
current_identifier as *const (),
is_packaged as *const (),
create_monitor as *const (),
close_monitor as *const (),
progress as *const (),
update_monitor as *const (),
register_progress as *const (),
unregister_progress as *const (),
get_user_locale as *const (),
find_availability as *const (),
enumerate_chunk_availability as *const (),
change_order as *const (),
install_chunks as *const (),
install_chunks_async as *const (),
install_chunks_result as *const (),
estimate_download as *const (),
uninstall_chunks as *const (),
missing::<20> as *const (),
missing::<21> as *const (),
unregister_installed as *const (),
missing::<23> as *const (),
mount_path_size as *const (),
mount_path as *const (),
close_mount as *const (),
missing::<27> as *const (),
missing::<28> as *const (),
missing::<29> as *const (),
write_stats as *const (),
missing::<31> as *const (),
uninstall_uwp_instance as *const (),
enumerate_features as *const (),
uninstall_package as *const (),
missing::<35> as *const (),
missing::<36> as *const (),
mount_async as *const (),
mount_result as *const (),
enumerate as *const (),
register_installed as *const (),
package_kind as *const (),
]);