347 lines
9.3 KiB
Rust
347 lines
9.3 KiB
Rust
|
|
|
|
#![allow(non_snake_case)]
|
|
|
|
#![allow(clippy::not_unsafe_ptr_arg_deref)]
|
|
|
|
use std::ffi::{CStr, c_char, c_void};
|
|
use std::sync::Mutex;
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
use super::{
|
|
display, speech, storage, xasync, xgame, xgamesave, xgameui, xnetworking, xpackage, xstore,
|
|
xsystem, xuser,
|
|
};
|
|
use crate::hle::{HleModule};
|
|
use crate::hle_export;
|
|
|
|
pub type HRESULT = i32;
|
|
pub const S_OK: HRESULT = 0;
|
|
|
|
pub const NOT_SUPPORTED: HRESULT = 0x8007_0032_u32 as i32;
|
|
|
|
pub const E_POINTER: HRESULT = 0x8000_4003_u32 as i32;
|
|
|
|
#[repr(C)]
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub struct Guid {
|
|
pub data1: u32,
|
|
pub data2: u16,
|
|
pub data3: u16,
|
|
pub data4: [u8; 8],
|
|
}
|
|
|
|
impl Guid {
|
|
pub const fn new(data1: u32, data2: u16, data3: u16, data4: [u8; 8]) -> Guid {
|
|
Guid {
|
|
data1,
|
|
data2,
|
|
data3,
|
|
data4,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for Guid {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
let d = self.data4;
|
|
write!(
|
|
f,
|
|
"{:08x}-{:04x}-{:04x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
|
|
self.data1, self.data2, self.data3, d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7]
|
|
)
|
|
}
|
|
}
|
|
|
|
#[repr(C)]
|
|
pub struct Object {
|
|
vtable: *const *const (),
|
|
|
|
area: &'static str,
|
|
|
|
slots: usize,
|
|
}
|
|
|
|
unsafe impl Sync for Object {}
|
|
|
|
impl Object {
|
|
pub const fn new<const N: usize>(area: &'static str, methods: &'static Methods<N>) -> Object {
|
|
Object {
|
|
vtable: methods.0.as_ptr(),
|
|
area,
|
|
slots: N,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct Methods<const N: usize>(pub [*const (); N]);
|
|
|
|
unsafe impl<const N: usize> Sync for Methods<N> {}
|
|
|
|
macro_rules! spare {
|
|
($($slot:literal)*) => { [$(missing::<$slot> as *const ()),*] };
|
|
}
|
|
|
|
pub const fn with_spare<const K: usize, const N: usize>(known: [*const (); K]) -> Methods<N> {
|
|
let spare = spare!(
|
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
|
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
|
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
|
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
|
);
|
|
assert!(K <= N && N <= spare.len());
|
|
let mut table = [std::ptr::null(); N];
|
|
let mut i = 0;
|
|
while i < N {
|
|
table[i] = if i < K { known[i] } else { spare[i] };
|
|
i += 1;
|
|
}
|
|
Methods(table)
|
|
}
|
|
|
|
pub type Interface = (Guid, Guid, &'static Object);
|
|
|
|
const AREAS: &[&[Interface]] = &[
|
|
xasync::INTERFACES,
|
|
xuser::INTERFACES,
|
|
xsystem::INTERFACES,
|
|
xgame::INTERFACES,
|
|
storage::INTERFACES,
|
|
xnetworking::INTERFACES,
|
|
xpackage::INTERFACES,
|
|
display::INTERFACES,
|
|
xgamesave::INTERFACES,
|
|
xstore::INTERFACES,
|
|
xgameui::INTERFACES,
|
|
speech::INTERFACES,
|
|
ERROR_INTERFACES,
|
|
];
|
|
|
|
pub extern "system" fn add_ref(_: *const Object) -> u32 {
|
|
1
|
|
}
|
|
|
|
pub extern "system" fn missing<const SLOT: usize>(this: *const Object) -> HRESULT {
|
|
|
|
let area = unsafe { (*this).area };
|
|
tracing::warn!(
|
|
area,
|
|
slot = format_args!("{:#x}", SLOT * 8),
|
|
"runtime method not implemented"
|
|
);
|
|
xasync::E_NOTIMPL
|
|
}
|
|
|
|
pub fn put<T>(out: *mut T, value: T) -> HRESULT {
|
|
if out.is_null() {
|
|
return xasync::E_INVALIDARG;
|
|
}
|
|
|
|
unsafe { out.write(value) };
|
|
S_OK
|
|
}
|
|
|
|
pub fn put_string(text: &str, size: usize, buffer: *mut u8, used: *mut usize) -> HRESULT {
|
|
let len = text.len() + 1;
|
|
if buffer.is_null() || size < len {
|
|
return xasync::E_NOT_SUFFICIENT_BUFFER;
|
|
}
|
|
|
|
unsafe {
|
|
buffer.copy_from_nonoverlapping(text.as_ptr(), text.len());
|
|
buffer.add(text.len()).write(0);
|
|
}
|
|
if !used.is_null() {
|
|
|
|
unsafe { used.write(len) };
|
|
}
|
|
S_OK
|
|
}
|
|
|
|
pub fn text(s: *const c_char) -> Option<String> {
|
|
|
|
(!s.is_null()).then(|| unsafe { CStr::from_ptr(s) }.to_string_lossy().into_owned())
|
|
}
|
|
|
|
pub fn token() -> u64 {
|
|
static NEXT: AtomicU64 = AtomicU64::new(1);
|
|
NEXT.fetch_add(1, Ordering::Relaxed)
|
|
}
|
|
|
|
pub extern "system" fn XGameRuntimeInitialize() -> HRESULT {
|
|
tracing::info!("XGameRuntimeInitialize");
|
|
S_OK
|
|
}
|
|
|
|
pub extern "system" fn XGameRuntimeUninitialize() {
|
|
tracing::info!("XGameRuntimeUninitialize");
|
|
}
|
|
|
|
static RUNTIME_BUILD: AtomicU64 = AtomicU64::new(0);
|
|
|
|
pub fn runtime_build() -> u64 {
|
|
RUNTIME_BUILD.load(Ordering::Relaxed)
|
|
}
|
|
|
|
fn note_build(build: u64) {
|
|
let _ = RUNTIME_BUILD.compare_exchange(0, build, Ordering::Relaxed, Ordering::Relaxed);
|
|
eonacat_xsbox_gpu::device::note_runtime_build(build);
|
|
}
|
|
|
|
pub extern "system" fn InitializeApiImpl(version: u64, build: u64) -> HRESULT {
|
|
tracing::info!(version, build, "InitializeApiImpl");
|
|
note_build(build);
|
|
S_OK
|
|
}
|
|
|
|
pub extern "system" fn InitializeApiImplEx(version: u64, build: u64, flags: u64) -> HRESULT {
|
|
tracing::info!(version, build, flags, "InitializeApiImplEx");
|
|
note_build(build);
|
|
S_OK
|
|
}
|
|
|
|
pub extern "system" fn InitializeApiImplEx2(
|
|
version: u64,
|
|
build: u64,
|
|
flags: u64,
|
|
options: *const c_void,
|
|
) -> HRESULT {
|
|
tracing::info!(version, build, flags, ?options, "InitializeApiImplEx2");
|
|
note_build(build);
|
|
S_OK
|
|
}
|
|
|
|
pub extern "system" fn UninitializeApiImpl() {
|
|
tracing::info!("UninitializeApiImpl");
|
|
}
|
|
|
|
pub extern "system" fn QueryApiImpl(
|
|
provider: *const Guid,
|
|
iid: *const Guid,
|
|
out: *mut *mut c_void,
|
|
) -> HRESULT {
|
|
|
|
static QUERIED: Mutex<Vec<(Guid, Guid, u64)>> = Mutex::new(Vec::new());
|
|
if out.is_null() {
|
|
return E_POINTER;
|
|
}
|
|
|
|
let (Some(&provider), Some(&iid)) = (unsafe { provider.as_ref() }, unsafe { iid.as_ref() })
|
|
else {
|
|
return NOT_SUPPORTED;
|
|
};
|
|
#[cfg(feature = "diagnostics")]
|
|
if tracing::enabled!(tracing::Level::DEBUG) {
|
|
|
|
static CALLS: Mutex<Vec<String>> = Mutex::new(Vec::new());
|
|
let from = super::kernel::callers();
|
|
let mut calls = CALLS.lock().unwrap_or_else(|e| e.into_inner());
|
|
if !calls.contains(&from) {
|
|
tracing::debug!(%provider, %iid, %from, "runtime call");
|
|
calls.push(from);
|
|
}
|
|
}
|
|
let found = AREAS
|
|
.iter()
|
|
.flat_map(|area| area.iter())
|
|
.find(|(p, i, _)| (*p, *i) == (provider, iid));
|
|
if let Some(&(_, _, object)) = found {
|
|
|
|
unsafe { out.write((object as *const Object).cast_mut().cast()) };
|
|
return S_OK;
|
|
}
|
|
let mut queried = QUERIED.lock().unwrap_or_else(|e| e.into_inner());
|
|
match queried.iter_mut().find(|(p, i, _)| (*p, *i) == (provider, iid)) {
|
|
None => {
|
|
queried.push((provider, iid, 1));
|
|
tracing::warn!(%provider, %iid, "runtime API area not implemented");
|
|
}
|
|
|
|
Some((_, _, calls)) => {
|
|
*calls += 1;
|
|
#[cfg(feature = "diagnostics")]
|
|
if calls.is_power_of_two() {
|
|
tracing::info!(%provider, %iid, calls = *calls, "runtime API area not implemented, asked again");
|
|
}
|
|
}
|
|
}
|
|
NOT_SUPPORTED
|
|
}
|
|
|
|
pub extern "system" fn XErrorReport(hr: HRESULT, message: *const c_char) {
|
|
let text = if message.is_null() {
|
|
"".into()
|
|
} else {
|
|
|
|
unsafe { CStr::from_ptr(message) }.to_string_lossy()
|
|
};
|
|
tracing::warn!(hr = format_args!("{:#010x}", hr as u32), message = %text, "XErrorReport");
|
|
let callback = *ERROR_CALLBACK.lock().unwrap_or_else(|e| e.into_inner());
|
|
if let Some((callback, context)) = callback {
|
|
|
|
let callback: ErrorCallback = unsafe { std::mem::transmute(callback) };
|
|
let message = if message.is_null() {
|
|
c"".as_ptr()
|
|
} else {
|
|
message
|
|
};
|
|
callback(hr, message, context as *mut c_void);
|
|
}
|
|
}
|
|
|
|
type ErrorCallback = extern "system" fn(HRESULT, *const c_char, *mut c_void) -> bool;
|
|
|
|
static ERROR_CALLBACK: Mutex<Option<(usize, usize)>> = Mutex::new(None);
|
|
|
|
pub const ERROR_AREA: Guid = Guid::new(
|
|
0x8ca4_67f7,
|
|
0x22e8,
|
|
0x4096,
|
|
[0x84, 0x56, 0xbb, 0x8a, 0xa1, 0x3f, 0x79, 0xd8],
|
|
);
|
|
static ERROR_OBJECT: Object = Object::new("errors", &ERROR_METHODS);
|
|
const ERROR_INTERFACES: &[Interface] = &[(ERROR_AREA, ERROR_AREA, &ERROR_OBJECT)];
|
|
|
|
extern "system" fn error_report(_: *const Object, hr: HRESULT, message: *const c_char) {
|
|
XErrorReport(hr, message);
|
|
}
|
|
|
|
extern "system" fn error_set_callback(
|
|
_: *const Object,
|
|
callback: Option<ErrorCallback>,
|
|
context: *mut c_void,
|
|
) {
|
|
*ERROR_CALLBACK.lock().unwrap_or_else(|e| e.into_inner()) =
|
|
callback.map(|c| (c as usize, context as usize));
|
|
}
|
|
|
|
extern "system" fn error_set_options(_: *const Object, debugger: u32, no_debugger: u32) {
|
|
tracing::info!(debugger, no_debugger, "XErrorSetOptions: logged only");
|
|
}
|
|
static ERROR_METHODS: Methods<6> = Methods([
|
|
missing::<0> as *const (),
|
|
add_ref as *const (),
|
|
add_ref as *const (),
|
|
error_report as *const (),
|
|
error_set_callback as *const (),
|
|
error_set_options as *const (),
|
|
]);
|
|
|
|
pub(crate) fn module() -> HleModule {
|
|
HleModule {
|
|
name: "xgameruntime",
|
|
dlls: &["xgameruntime.dll"],
|
|
exports: vec![
|
|
hle_export!(XGameRuntimeInitialize),
|
|
hle_export!(XGameRuntimeUninitialize),
|
|
hle_export!(InitializeApiImpl),
|
|
hle_export!(InitializeApiImplEx),
|
|
hle_export!(InitializeApiImplEx2),
|
|
hle_export!(UninitializeApiImpl),
|
|
hle_export!(QueryApiImpl),
|
|
hle_export!(XErrorReport),
|
|
],
|
|
}
|
|
}
|
|
|