xstore.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. //! XStore as an offline console answers it: the game's own licence is
  2. //! local and active (a full, owned game); the user owns no add-ons; what
  3. //! needs the store service (catalog, consumables, purchase and update
  4. //! pages, collection ids, update checks) fails with the documented store
  5. //! network error.
  6. #![allow(clippy::not_unsafe_ptr_arg_deref)]
  7. use std::ffi::{c_char, c_void};
  8. use super::xasync::{self, AsyncBlock, E_ILLEGAL_METHOD_CALL};
  9. use super::xgameruntime::{
  10. Guid, HRESULT, Interface, Methods, Object, S_OK, add_ref, missing, put, put_string, text, token,
  11. };
  12. use super::xpackage;
  13. use crate::runtime;
  14. pub const PROVIDER: Guid = Guid::new(
  15. 0x0dd1_12ac,
  16. 0x7c24,
  17. 0x448c,
  18. [0xb9, 0x2b, 0x39, 0x60, 0xfb, 0x5b, 0xd3, 0x0c],
  19. );
  20. /// As Grand Theft Auto VI (2026) asks for it; every slot it calls takes
  21. /// the public API's arguments in the 2020 order.
  22. pub const IID_2026: Guid = Guid::new(
  23. 0xb09d_803c,
  24. 0x2414,
  25. 0x4a05,
  26. [0x82, 0xc6, 0x66, 0xdf, 0xdc, 0x9e, 0x9a, 0x44],
  27. );
  28. /// The same table extended by durables licensing (0x238, 0x240).
  29. pub const DURABLES_IID: Guid = Guid::new(
  30. 0x2d42_fea5,
  31. 0xe71d,
  32. 0x4b76,
  33. [0x97, 0xcd, 0xc5, 0x0a, 0xfb, 0xb3, 0xae, 0x5d],
  34. );
  35. pub static OBJECT: Object = Object::new("store", &VTABLE);
  36. pub const INTERFACES: &[Interface] = &[
  37. (PROVIDER, IID_2026, &OBJECT),
  38. (PROVIDER, DURABLES_IID, &OBJECT),
  39. ];
  40. /// The documented "failed to communicate with the store network".
  41. pub const NETWORK_ERROR: HRESULT = 0x8924_5301_u32 as i32;
  42. type This = *const Object;
  43. type Handle = *const c_void;
  44. /// Contexts and product queries hold nothing (queries are always empty),
  45. /// so their handles are fixed addresses. A licence handle says whether it
  46. /// is valid by which address it is.
  47. static CONTEXT: u8 = 0;
  48. static QUERY: u8 = 0;
  49. static VALID: u8 = 0;
  50. static INVALID: u8 = 0;
  51. fn handle(of: &'static u8) -> Handle {
  52. (of as *const u8).cast()
  53. }
  54. /// Identifies our calls on a block: one per result shape.
  55. static FAILED: u8 = 0;
  56. static QUERY_CALL: u8 = 0;
  57. static LICENSE_CALL: u8 = 0;
  58. static GAME_LICENSE: u8 = 0;
  59. fn offline(block: *mut AsyncBlock, what: &str) -> HRESULT {
  60. tracing::info!(what, "store call: offline");
  61. xasync::finished(block, (&raw const FAILED).cast(), NETWORK_ERROR)
  62. }
  63. /// The result functions of calls that failed: their status.
  64. extern "system" fn status(_: This, block: *mut AsyncBlock) -> HRESULT {
  65. xasync::XAsyncGetStatus(block, false)
  66. }
  67. extern "system" fn create_context(_: This, _user: *const c_void, out: *mut Handle) -> HRESULT {
  68. put(out, handle(&CONTEXT))
  69. }
  70. extern "system" fn close(_: This, _: Handle) {}
  71. extern "system" fn associated_products_async(
  72. _: This,
  73. _: Handle,
  74. _kinds: u32,
  75. _per_page: u32,
  76. block: *mut AsyncBlock,
  77. ) -> HRESULT {
  78. offline(block, "associated products")
  79. }
  80. /// The user owns no add-ons: an empty query.
  81. extern "system" fn entitled_products_async(
  82. _: This,
  83. _: Handle,
  84. _kinds: u32,
  85. _per_page: u32,
  86. block: *mut AsyncBlock,
  87. ) -> HRESULT {
  88. xasync::finished_with(
  89. block,
  90. (&raw const QUERY_CALL).cast(),
  91. Ok(xasync::bytes(handle(&QUERY))),
  92. )
  93. }
  94. extern "system" fn query_result(_: This, block: *mut AsyncBlock, out: *mut Handle) -> HRESULT {
  95. xasync::result(block, (&raw const QUERY_CALL).cast(), out)
  96. }
  97. /// Empty, so the callback never runs.
  98. extern "system" fn enumerate_products(
  99. _: This,
  100. _: Handle,
  101. _: *mut c_void,
  102. _: *const c_void,
  103. ) -> HRESULT {
  104. S_OK
  105. }
  106. extern "system" fn has_more_pages(_: This, _: Handle) -> bool {
  107. false
  108. }
  109. /// There is no next page to ask for.
  110. extern "system" fn next_page_async(_: This, _: Handle, block: *mut AsyncBlock) -> HRESULT {
  111. xasync::finished(block, (&raw const QUERY_CALL).cast(), E_ILLEGAL_METHOD_CALL)
  112. }
  113. fn license_for(owned: bool, block: *mut AsyncBlock) -> HRESULT {
  114. let license = handle(if owned { &VALID } else { &INVALID });
  115. xasync::finished_with(
  116. block,
  117. (&raw const LICENSE_CALL).cast(),
  118. Ok(xasync::bytes(license)),
  119. )
  120. }
  121. /// Only the game's own package is installed, so only its licence is valid.
  122. extern "system" fn license_for_package_async(
  123. _: This,
  124. _: Handle,
  125. package: *const c_char,
  126. block: *mut AsyncBlock,
  127. ) -> HRESULT {
  128. let ours = text(package).is_some_and(|p| p == xpackage::identifier());
  129. license_for(ours, block)
  130. }
  131. /// The user owns no durables: their licences are not valid.
  132. extern "system" fn license_for_durables_async(
  133. _: This,
  134. _: Handle,
  135. store_id: *const c_char,
  136. block: *mut AsyncBlock,
  137. ) -> HRESULT {
  138. tracing::debug!(
  139. store_id = text(store_id).unwrap_or_default(),
  140. "durable not owned"
  141. );
  142. license_for(false, block)
  143. }
  144. extern "system" fn license_result(_: This, block: *mut AsyncBlock, out: *mut Handle) -> HRESULT {
  145. xasync::result(block, (&raw const LICENSE_CALL).cast(), out)
  146. }
  147. extern "system" fn is_license_valid(_: This, license: Handle) -> bool {
  148. license == handle(&VALID)
  149. }
  150. /// The game licence, as the public API lays it out.
  151. #[repr(C)]
  152. #[derive(Debug, Clone, Copy)]
  153. pub struct GameLicense {
  154. pub sku_store_id: [u8; 18],
  155. pub is_active: bool,
  156. pub is_trial_owned_by_this_user: bool,
  157. pub is_disc_license: bool,
  158. pub is_trial: bool,
  159. pub trial_seconds_remaining: u32,
  160. pub trial_unique_id: [u8; 64],
  161. pub expiration: i64,
  162. }
  163. /// A full, active licence for the game's SKU (`<store id>/0010`, the
  164. /// store's first SKU), not a trial, without expiry.
  165. fn game_license() -> GameLicense {
  166. let mut sku = [0u8; 18];
  167. let id = format!("{}/0010", runtime::title().store_id);
  168. let len = id.len().min(sku.len() - 1);
  169. sku[..len].copy_from_slice(&id.as_bytes()[..len]);
  170. GameLicense {
  171. sku_store_id: sku,
  172. is_active: true,
  173. is_trial_owned_by_this_user: false,
  174. is_disc_license: false,
  175. is_trial: false,
  176. trial_seconds_remaining: 0,
  177. trial_unique_id: [0; 64],
  178. expiration: 0,
  179. }
  180. }
  181. extern "system" fn game_license_async(_: This, _: Handle, block: *mut AsyncBlock) -> HRESULT {
  182. let license = xasync::bytes(game_license());
  183. xasync::finished_with(block, (&raw const GAME_LICENSE).cast(), Ok(license))
  184. }
  185. extern "system" fn game_license_result(
  186. _: This,
  187. block: *mut AsyncBlock,
  188. out: *mut GameLicense,
  189. ) -> HRESULT {
  190. xasync::result(block, (&raw const GAME_LICENSE).cast(), out)
  191. }
  192. extern "system" fn consumable_balance_async(
  193. _: This,
  194. _: Handle,
  195. _store_id: *const c_char,
  196. block: *mut AsyncBlock,
  197. ) -> HRESULT {
  198. offline(block, "consumable balance")
  199. }
  200. extern "system" fn report_fulfillment_async(
  201. _: This,
  202. _: Handle,
  203. _store_id: *const c_char,
  204. _quantity: u32,
  205. _tracking_id: *const c_void,
  206. block: *mut AsyncBlock,
  207. ) -> HRESULT {
  208. offline(block, "consumable fulfillment")
  209. }
  210. /// Collections and purchase ids alike.
  211. extern "system" fn user_id_async(
  212. _: This,
  213. _: Handle,
  214. _ticket: *const c_char,
  215. _publisher_user_id: *const c_char,
  216. block: *mut AsyncBlock,
  217. ) -> HRESULT {
  218. offline(block, "user store id")
  219. }
  220. extern "system" fn size_status(_: This, block: *mut AsyncBlock, _out: *mut usize) -> HRESULT {
  221. xasync::XAsyncGetStatus(block, false)
  222. }
  223. extern "system" fn string_status(
  224. _: This,
  225. block: *mut AsyncBlock,
  226. _: usize,
  227. _: *mut c_char,
  228. ) -> HRESULT {
  229. xasync::XAsyncGetStatus(block, false)
  230. }
  231. extern "system" fn count_status(_: This, block: *mut AsyncBlock, _out: *mut u32) -> HRESULT {
  232. xasync::XAsyncGetStatus(block, false)
  233. }
  234. extern "system" fn array_status(
  235. _: This,
  236. block: *mut AsyncBlock,
  237. _: u32,
  238. _: *mut c_void,
  239. ) -> HRESULT {
  240. xasync::XAsyncGetStatus(block, false)
  241. }
  242. extern "system" fn purchase_ui_async(
  243. _: This,
  244. _: Handle,
  245. _store_id: *const c_char,
  246. _name: *const c_char,
  247. _json: *const c_char,
  248. block: *mut AsyncBlock,
  249. ) -> HRESULT {
  250. offline(block, "purchase page")
  251. }
  252. extern "system" fn product_page_ui_async(
  253. _: This,
  254. _: Handle,
  255. _store_id: *const c_char,
  256. block: *mut AsyncBlock,
  257. ) -> HRESULT {
  258. offline(block, "product page")
  259. }
  260. extern "system" fn redeem_ui_async(
  261. _: This,
  262. _: Handle,
  263. _token: *const c_char,
  264. _allowed: *const *const c_char,
  265. _allowed_count: usize,
  266. _disallow_csv: bool,
  267. block: *mut AsyncBlock,
  268. ) -> HRESULT {
  269. offline(block, "redeem page")
  270. }
  271. extern "system" fn updates_async(_: This, _: Handle, block: *mut AsyncBlock) -> HRESULT {
  272. offline(block, "update check")
  273. }
  274. /// Installing updates or packages by id alike.
  275. extern "system" fn install_async(
  276. _: This,
  277. _: Handle,
  278. _ids: *const *const c_char,
  279. _count: usize,
  280. block: *mut AsyncBlock,
  281. ) -> HRESULT {
  282. offline(block, "package download")
  283. }
  284. /// The game's own store id maps to its installation; nothing else is
  285. /// installed.
  286. extern "system" fn package_identifier(
  287. _: This,
  288. store_id: *const c_char,
  289. size: usize,
  290. buffer: *mut u8,
  291. ) -> HRESULT {
  292. let title = runtime::title();
  293. match text(store_id) {
  294. Some(id) if !title.store_id.is_empty() && id == title.store_id => {
  295. put_string(&xpackage::identifier(), size, buffer, std::ptr::null_mut())
  296. }
  297. _ => xpackage::NO_PACKAGE,
  298. }
  299. }
  300. /// Licences never change offline, so these callbacks never run.
  301. extern "system" fn register(
  302. _: This,
  303. _: Handle,
  304. _queue: *const c_void,
  305. _context: *mut c_void,
  306. _callback: *const c_void,
  307. out: *mut u64,
  308. ) -> HRESULT {
  309. put(out, token())
  310. }
  311. extern "system" fn unregister(_: This, _: Handle, _token: u64, _wait: bool) -> bool {
  312. true
  313. }
  314. static VTABLE: Methods<76> = Methods([
  315. missing::<0> as *const (),
  316. add_ref as *const (),
  317. add_ref as *const (),
  318. create_context as *const (), // 0x18
  319. close as *const (), // 0x20
  320. associated_products_async as *const (), // 0x28
  321. query_result as *const (), // 0x30
  322. missing::<7> as *const (), // 0x38 products by id
  323. missing::<8> as *const (), // 0x40
  324. entitled_products_async as *const (), // 0x48
  325. query_result as *const (), // 0x50
  326. missing::<11> as *const (), // 0x58 product for the game
  327. missing::<12> as *const (), // 0x60
  328. missing::<13> as *const (), // 0x68 product for a package
  329. missing::<14> as *const (), // 0x70
  330. enumerate_products as *const (), // 0x78
  331. has_more_pages as *const (), // 0x80
  332. next_page_async as *const (), // 0x88
  333. query_result as *const (), // 0x90
  334. close as *const (), // 0x98 a product query
  335. license_for_package_async as *const (), // 0xa0
  336. license_result as *const (), // 0xa8
  337. is_license_valid as *const (), // 0xb0
  338. close as *const (), // 0xb8 a licence
  339. missing::<24> as *const (), // 0xc0 can acquire for store id
  340. missing::<25> as *const (), // 0xc8
  341. missing::<26> as *const (), // 0xd0 can acquire for package
  342. missing::<27> as *const (), // 0xd8
  343. game_license_async as *const (), // 0xe0
  344. game_license_result as *const (), // 0xe8
  345. missing::<30> as *const (), // 0xf0 add-on licences
  346. missing::<31> as *const (), // 0xf8
  347. missing::<32> as *const (), // 0x100
  348. consumable_balance_async as *const (), // 0x108
  349. status as *const (), // 0x110
  350. report_fulfillment_async as *const (), // 0x118
  351. missing::<36> as *const (), // 0x120
  352. user_id_async as *const (), // 0x128 collections id
  353. size_status as *const (), // 0x130
  354. string_status as *const (), // 0x138
  355. user_id_async as *const (), // 0x140 purchase id
  356. size_status as *const (), // 0x148
  357. string_status as *const (), // 0x150
  358. missing::<43> as *const (), // 0x158 licence token
  359. missing::<44> as *const (), // 0x160
  360. missing::<45> as *const (), // 0x168
  361. missing::<46> as *const (), // 0x170
  362. missing::<47> as *const (), // 0x178
  363. missing::<48> as *const (), // 0x180
  364. purchase_ui_async as *const (), // 0x188
  365. missing::<50> as *const (), // 0x190
  366. missing::<51> as *const (), // 0x198 rate and review
  367. missing::<52> as *const (), // 0x1a0
  368. redeem_ui_async as *const (), // 0x1a8
  369. missing::<54> as *const (), // 0x1b0
  370. updates_async as *const (), // 0x1b8
  371. count_status as *const (), // 0x1c0
  372. array_status as *const (), // 0x1c8
  373. missing::<58> as *const (), // 0x1d0 download updates
  374. missing::<59> as *const (), // 0x1d8
  375. install_async as *const (), // 0x1e0 updates
  376. status as *const (), // 0x1e8
  377. install_async as *const (), // 0x1f0 packages by store id
  378. count_status as *const (), // 0x1f8
  379. array_status as *const (), // 0x200
  380. package_identifier as *const (), // 0x208
  381. register as *const (), // 0x210 game licence changed
  382. unregister as *const (), // 0x218
  383. register as *const (), // 0x220 package licence lost
  384. unregister as *const (), // 0x228
  385. missing::<70> as *const (), // 0x230
  386. license_for_durables_async as *const (), // 0x238
  387. license_result as *const (), // 0x240
  388. missing::<73> as *const (), // 0x248 associated products page
  389. missing::<74> as *const (), // 0x250
  390. product_page_ui_async as *const (), // 0x258
  391. ]);
  392. #[cfg(test)]
  393. mod tests {
  394. use super::*;
  395. use crate::modules::xgameruntime::method;
  396. fn block() -> AsyncBlock {
  397. AsyncBlock {
  398. queue: std::ptr::null(),
  399. context: std::ptr::null_mut(),
  400. callback: None,
  401. internal: [0; 4],
  402. }
  403. }
  404. #[test]
  405. fn the_game_is_licensed_and_the_store_is_offline() {
  406. let this: This = &OBJECT;
  407. assert_eq!(size_of::<GameLicense>(), 104);
  408. type Begin = extern "system" fn(This, Handle, *mut AsyncBlock) -> HRESULT;
  409. type Result = extern "system" fn(This, *mut AsyncBlock, *mut GameLicense) -> HRESULT;
  410. let mut b = block();
  411. assert_eq!(
  412. method::<Begin>(&OBJECT, 0xe0)(this, handle(&CONTEXT), &mut b),
  413. S_OK
  414. );
  415. assert_eq!(xasync::XAsyncGetStatus(&mut b, true), S_OK);
  416. let mut license = std::mem::MaybeUninit::<GameLicense>::zeroed();
  417. assert_eq!(
  418. method::<Result>(&OBJECT, 0xe8)(this, &mut b, license.as_mut_ptr()),
  419. S_OK
  420. );
  421. // SAFETY: written by the call.
  422. let license = unsafe { license.assume_init() };
  423. assert!(license.is_active && !license.is_trial);
  424. let mut b = block();
  425. assert_eq!(
  426. method::<Begin>(&OBJECT, 0x1b8)(this, handle(&CONTEXT), &mut b),
  427. S_OK
  428. );
  429. assert_eq!(xasync::XAsyncGetStatus(&mut b, true), NETWORK_ERROR);
  430. type Count = extern "system" fn(This, *mut AsyncBlock, *mut u32) -> HRESULT;
  431. assert_eq!(
  432. method::<Count>(&OBJECT, 0x1c0)(this, &mut b, &mut 0),
  433. NETWORK_ERROR
  434. );
  435. type Durable = extern "system" fn(This, Handle, *const c_char, *mut AsyncBlock) -> HRESULT;
  436. type License = extern "system" fn(This, *mut AsyncBlock, *mut Handle) -> HRESULT;
  437. let mut b = block();
  438. let durable = method::<Durable>(&OBJECT, 0x238);
  439. assert_eq!(
  440. durable(this, handle(&CONTEXT), c"9XYZ".as_ptr(), &mut b),
  441. S_OK
  442. );
  443. assert_eq!(xasync::XAsyncGetStatus(&mut b, true), S_OK);
  444. let mut l = std::ptr::null();
  445. assert_eq!(
  446. method::<License>(&OBJECT, 0x240)(this, &mut b, &mut l),
  447. S_OK
  448. );
  449. assert!(!method::<extern "system" fn(This, Handle) -> bool>(
  450. &OBJECT, 0xb0
  451. )(this, l));
  452. }
  453. }