xpackage.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. //! XPackage: the title's own installation, complete and launchable, with
  2. //! every chunk ready; no other package (content, add-ons) is installed.
  3. //! The package is the folder of the title's executable (`G:` to the title).
  4. #![allow(clippy::not_unsafe_ptr_arg_deref)]
  5. use std::ffi::{c_char, c_void};
  6. use std::path::Path;
  7. use std::sync::OnceLock;
  8. use super::xasync::{self, AsyncBlock};
  9. use super::xgameruntime::{
  10. Guid, HRESULT, Interface, Methods, Object, S_OK, add_ref, missing, put, put_string, text, token,
  11. };
  12. use crate::runtime;
  13. pub const PROVIDER: Guid = Guid::new(
  14. 0xaf40_6016,
  15. 0xe850,
  16. 0x4aa8,
  17. [0xa8, 0x8d, 0x2f, 0x3d, 0xcb, 0x9d, 0xac, 0x7e],
  18. );
  19. /// As the title (2026) asks for it. Its slots up to 0xd0 take the
  20. /// arguments the public API lists in the same order; 0x110 uninstalls a
  21. /// package, 0x128/0x130 mount one with UI, 0x138 enumerates packages.
  22. pub const IID_2026: Guid = Guid::new(
  23. 0x0b9d_556e,
  24. 0xab70,
  25. 0x43f4,
  26. [0xba, 0xa0, 0x99, 0x99, 0xac, 0x4c, 0xb0, 0x40],
  27. );
  28. pub static OBJECT: Object = Object::new("package", &VTABLE);
  29. pub const INTERFACES: &[Interface] = &[(PROVIDER, IID_2026, &OBJECT)];
  30. /// The documented "could not resolve an identifier for the package".
  31. pub const NO_PACKAGE: HRESULT = 0x8924_5208_u32 as i32;
  32. /// Our installation's identifier: the title id in hex (titles keep it in 33
  33. /// bytes and hand it back to us).
  34. pub fn identifier() -> String {
  35. format!("{:08X}", runtime::title().id)
  36. }
  37. fn is_ours(id: *const c_char) -> bool {
  38. text(id).is_some_and(|id| id == identifier())
  39. }
  40. /// Bytes in the package folder, counted once.
  41. fn package_bytes() -> u64 {
  42. fn size(dir: &Path) -> u64 {
  43. let Ok(entries) = std::fs::read_dir(dir) else {
  44. return 0;
  45. };
  46. entries
  47. .flatten()
  48. .map(|e| match e.metadata() {
  49. Ok(m) if m.is_dir() => size(&e.path()),
  50. Ok(m) => m.len(),
  51. Err(_) => 0,
  52. })
  53. .sum()
  54. }
  55. static BYTES: OnceLock<u64> = OnceLock::new();
  56. *BYTES.get_or_init(|| {
  57. let exe = runtime::main_image().and_then(runtime::image_path);
  58. exe.as_deref().and_then(Path::parent).map_or(0, size)
  59. })
  60. }
  61. type This = *const Object;
  62. extern "system" fn current_identifier(_: This, size: usize, buffer: *mut u8) -> HRESULT {
  63. put_string(&identifier(), size, buffer, std::ptr::null_mut())
  64. }
  65. extern "system" fn is_packaged(_: This) -> bool {
  66. true
  67. }
  68. /// Installation monitors all watch the same finished installation, so the
  69. /// handle is a fixed address.
  70. static MONITOR: u8 = 0;
  71. fn monitor() -> *const c_void {
  72. (&raw const MONITOR).cast()
  73. }
  74. extern "system" fn create_monitor(
  75. _: This,
  76. id: *const c_char,
  77. _selector_count: u32,
  78. _selectors: *const c_void,
  79. _interval_ms: u32,
  80. _queue: *const c_void,
  81. out: *mut *const c_void,
  82. ) -> HRESULT {
  83. if !is_ours(id) {
  84. return NO_PACKAGE;
  85. }
  86. put(out, monitor())
  87. }
  88. extern "system" fn close_monitor(_: This, _: *const c_void) {}
  89. #[repr(C)]
  90. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  91. pub struct Progress {
  92. pub total_bytes: u64,
  93. pub installed_bytes: u64,
  94. pub launch_bytes: u64,
  95. pub launchable: bool,
  96. pub completed: bool,
  97. }
  98. extern "system" fn progress(_: This, _: *const c_void, out: *mut Progress) {
  99. let bytes = package_bytes();
  100. put(
  101. out,
  102. Progress {
  103. total_bytes: bytes,
  104. installed_bytes: bytes,
  105. launch_bytes: bytes,
  106. launchable: true,
  107. completed: true,
  108. },
  109. );
  110. }
  111. /// The installation is complete, so progress never changes.
  112. extern "system" fn register_progress(
  113. _: This,
  114. _monitor: *const c_void,
  115. _context: *mut c_void,
  116. _callback: *const c_void,
  117. out: *mut u64,
  118. ) -> HRESULT {
  119. put(out, token())
  120. }
  121. /// The completed installation has no pending progress callbacks to wait for.
  122. extern "system" fn unregister_progress(
  123. _: This,
  124. _monitor: *const c_void,
  125. _token: u64,
  126. _wait: bool,
  127. ) -> bool {
  128. true
  129. }
  130. /// 0: ready.
  131. // shortcut: every selector counts as installed, including ones the
  132. // package's layout does not have (the console answers those with an
  133. // invalid-selector error); the title asks only about its own
  134. // chunks, which a full install has.
  135. extern "system" fn find_availability(
  136. _: This,
  137. id: *const c_char,
  138. _selector_count: u32,
  139. _selectors: *const c_void,
  140. out: *mut u32,
  141. ) -> HRESULT {
  142. if !is_ours(id) {
  143. return NO_PACKAGE;
  144. }
  145. put(out, 0)
  146. }
  147. extern "system" fn change_order(
  148. _: This,
  149. id: *const c_char,
  150. _count: u32,
  151. _selectors: *const c_void,
  152. ) -> HRESULT {
  153. if is_ours(id) { S_OK } else { NO_PACKAGE }
  154. }
  155. static INSTALL: u8 = 0;
  156. /// Everything is installed: done at once, the result is a monitor.
  157. extern "system" fn install_chunks_async(
  158. _: This,
  159. id: *const c_char,
  160. _count: u32,
  161. _selectors: *const c_void,
  162. _interval_ms: u32,
  163. _suppress_confirmation: bool,
  164. block: *mut AsyncBlock,
  165. ) -> HRESULT {
  166. let result = if is_ours(id) {
  167. Ok(xasync::bytes(monitor()))
  168. } else {
  169. Err(NO_PACKAGE)
  170. };
  171. xasync::finished_with(block, (&raw const INSTALL).cast(), result)
  172. }
  173. extern "system" fn install_chunks_result(
  174. _: This,
  175. block: *mut AsyncBlock,
  176. out: *mut *const c_void,
  177. ) -> HRESULT {
  178. xasync::result(block, (&raw const INSTALL).cast(), out)
  179. }
  180. /// Nothing to download.
  181. extern "system" fn estimate_download(
  182. _: This,
  183. id: *const c_char,
  184. _count: u32,
  185. _selectors: *const c_void,
  186. bytes: *mut u64,
  187. confirm: *mut bool,
  188. ) -> HRESULT {
  189. if !is_ours(id) {
  190. return NO_PACKAGE;
  191. }
  192. if !confirm.is_null() {
  193. // SAFETY: checked non-null.
  194. unsafe { confirm.write(false) };
  195. }
  196. put(bytes, 0)
  197. }
  198. // shortcut: accepted but nothing is removed (the package folder is the
  199. // user's), so the chunks stay ready; the title's one call site
  200. // (title+0x293b4b0, its chunk manager) would then see them still installed.
  201. extern "system" fn uninstall_chunks(
  202. _: This,
  203. id: *const c_char,
  204. _count: u32,
  205. _selectors: *const c_void,
  206. ) -> HRESULT {
  207. if is_ours(id) { S_OK } else { NO_PACKAGE }
  208. }
  209. /// Mount handles only come from mounting another package, and there is
  210. /// none.
  211. extern "system" fn mount_path(_: This, _: *const c_void, _: usize, _: *mut u8) -> HRESULT {
  212. xasync::E_INVALIDARG
  213. }
  214. extern "system" fn close_mount(_: This, _: *const c_void) {}
  215. /// Only other packages are uninstalled this way, and none is installed.
  216. extern "system" fn uninstall_package(_: This, id: *const c_char) -> bool {
  217. tracing::info!(
  218. id = text(id).unwrap_or_default(),
  219. "package uninstall refused"
  220. );
  221. false
  222. }
  223. static MOUNT: u8 = 0;
  224. extern "system" fn mount_async(_: This, id: *const c_char, block: *mut AsyncBlock) -> HRESULT {
  225. tracing::info!(
  226. id = text(id).unwrap_or_default(),
  227. "package mount: not installed"
  228. );
  229. xasync::finished(block, (&raw const MOUNT).cast(), NO_PACKAGE)
  230. }
  231. extern "system" fn mount_result(
  232. _: This,
  233. block: *mut AsyncBlock,
  234. out: *mut *const c_void,
  235. ) -> HRESULT {
  236. xasync::result(block, (&raw const MOUNT).cast(), out)
  237. }
  238. /// Titles enumerate content packages (kind 1); there are none.
  239. // shortcut: the game kind (0) also lists nothing, not this installation;
  240. // The title enumerates only content (kind 1, title+0x2937bf0).
  241. extern "system" fn enumerate(
  242. _: This,
  243. kind: u32,
  244. _scope: u32,
  245. _context: *mut c_void,
  246. _callback: *const c_void,
  247. ) -> HRESULT {
  248. tracing::debug!(kind, "packages enumerated: none");
  249. S_OK
  250. }
  251. static VTABLE: Methods<40> = Methods([
  252. missing::<0> as *const (),
  253. add_ref as *const (),
  254. add_ref as *const (),
  255. current_identifier as *const (), // 0x18
  256. is_packaged as *const (), // 0x20
  257. create_monitor as *const (), // 0x28
  258. close_monitor as *const (), // 0x30
  259. progress as *const (), // 0x38
  260. missing::<8> as *const (), // 0x40 update a monitor
  261. register_progress as *const (), // 0x48
  262. unregister_progress as *const (), // 0x50
  263. missing::<11> as *const (), // 0x58 user locale
  264. find_availability as *const (), // 0x60
  265. missing::<13> as *const (), // 0x68 enumerate chunk availability
  266. change_order as *const (), // 0x70
  267. missing::<15> as *const (), // 0x78 install chunks
  268. install_chunks_async as *const (), // 0x80
  269. install_chunks_result as *const (), // 0x88
  270. estimate_download as *const (), // 0x90
  271. uninstall_chunks as *const (), // 0x98
  272. missing::<20> as *const (), // 0xa0
  273. missing::<21> as *const (), // 0xa8
  274. missing::<22> as *const (), // 0xb0
  275. missing::<23> as *const (), // 0xb8 mount (deprecated)
  276. missing::<24> as *const (), // 0xc0 mount path size
  277. mount_path as *const (), // 0xc8
  278. close_mount as *const (), // 0xd0
  279. missing::<27> as *const (), // 0xd8
  280. missing::<28> as *const (), // 0xe0 enumerate packages (older)
  281. missing::<29> as *const (), // 0xe8
  282. missing::<30> as *const (), // 0xf0
  283. missing::<31> as *const (), // 0xf8
  284. missing::<32> as *const (), // 0x100
  285. missing::<33> as *const (), // 0x108
  286. uninstall_package as *const (), // 0x110
  287. missing::<35> as *const (), // 0x118
  288. missing::<36> as *const (), // 0x120
  289. mount_async as *const (), // 0x128
  290. mount_result as *const (), // 0x130
  291. enumerate as *const (), // 0x138
  292. ]);
  293. #[cfg(test)]
  294. mod tests {
  295. use super::*;
  296. use crate::modules::xgameruntime::method;
  297. #[test]
  298. fn completed_installation_unregisters_progress_immediately() {
  299. type Register =
  300. extern "system" fn(This, *const c_void, *mut c_void, *const c_void, *mut u64) -> HRESULT;
  301. type Unregister = extern "system" fn(This, *const c_void, u64, bool) -> bool;
  302. extern "system" fn callback(_: *mut c_void, _: *const c_void) {}
  303. let this: This = &OBJECT;
  304. let register = method::<Register>(&OBJECT, 0x48);
  305. assert_eq!(VTABLE.0[0x50 / 8], unregister_progress as *const ());
  306. let unregister = method::<Unregister>(&OBJECT, 0x50);
  307. for wait in [false, true] {
  308. let mut token = 0;
  309. assert_eq!(
  310. register(
  311. this,
  312. monitor(),
  313. std::ptr::null_mut(),
  314. callback as *const c_void,
  315. &mut token
  316. ),
  317. S_OK
  318. );
  319. assert_ne!(token, 0);
  320. assert!(unregister(this, monitor(), token, wait));
  321. }
  322. }
  323. #[test]
  324. fn the_installation_is_complete_and_alone() {
  325. let this: This = &OBJECT;
  326. type Id = extern "system" fn(This, usize, *mut u8) -> HRESULT;
  327. let mut id = [0u8; 33];
  328. assert_eq!(method::<Id>(&OBJECT, 0x18)(this, 33, id.as_mut_ptr()), S_OK);
  329. let len = id.iter().position(|&b| b == 0).unwrap();
  330. assert_eq!(std::str::from_utf8(&id[..len]).unwrap(), identifier());
  331. assert!(method::<extern "system" fn(This) -> bool>(&OBJECT, 0x20)(
  332. this
  333. ));
  334. type Find =
  335. extern "system" fn(This, *const c_char, u32, *const c_void, *mut u32) -> HRESULT;
  336. let mut availability = 9;
  337. let ours = format!("{}\0", identifier());
  338. let find = method::<Find>(&OBJECT, 0x60);
  339. let none = std::ptr::null();
  340. assert_eq!(
  341. find(this, ours.as_ptr().cast(), 0, none, &mut availability),
  342. S_OK
  343. );
  344. assert_eq!(availability, 0, "ready");
  345. assert_eq!(
  346. find(this, c"dlc".as_ptr(), 0, none, &mut availability),
  347. NO_PACKAGE
  348. );
  349. type Progress_ = extern "system" fn(This, *const c_void, *mut Progress);
  350. let mut p = Progress {
  351. total_bytes: 1,
  352. installed_bytes: 0,
  353. launch_bytes: 0,
  354. launchable: false,
  355. completed: false,
  356. };
  357. method::<Progress_>(&OBJECT, 0x38)(this, monitor(), &mut p);
  358. assert!(p.completed && p.launchable && p.installed_bytes == p.total_bytes);
  359. type Mount = extern "system" fn(This, *const c_char, *mut AsyncBlock) -> HRESULT;
  360. let mut block = AsyncBlock {
  361. queue: std::ptr::null(),
  362. context: std::ptr::null_mut(),
  363. callback: None,
  364. internal: [0; 4],
  365. };
  366. assert_eq!(
  367. method::<Mount>(&OBJECT, 0x128)(this, c"dlc".as_ptr(), &mut block),
  368. S_OK
  369. );
  370. assert_eq!(xasync::XAsyncGetStatus(&mut block, true), NO_PACKAGE);
  371. }
  372. }