2026-11-23 01:15:26 +01:00
|
|
|
//! Process-wide state the implementations need.
|
|
|
|
|
|
2026-11-23 14:42:21 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
use std::sync::Mutex;
|
2026-11-23 01:15:26 +01:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
|
|
|
|
|
|
static MAIN_IMAGE_BASE: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
|
|
|
|
|
|
/// Records where the title's main image is mapped. The title expects
|
|
|
|
|
/// `GetModuleHandle(NULL)` and friends to return this, not our own exe.
|
|
|
|
|
pub fn set_main_image(base: usize) {
|
|
|
|
|
MAIN_IMAGE_BASE.store(base, Ordering::Release);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn main_image() -> Option<usize> {
|
|
|
|
|
match MAIN_IMAGE_BASE.load(Ordering::Acquire) {
|
|
|
|
|
0 => None,
|
|
|
|
|
base => Some(base),
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-23 14:42:21 +01:00
|
|
|
|
|
|
|
|
/// Original file of each title image we mapped, by base address.
|
|
|
|
|
static IMAGE_PATHS: Mutex<Vec<(usize, PathBuf)>> = Mutex::new(Vec::new());
|
|
|
|
|
|
|
|
|
|
/// Records the file a title image was mapped from: the title asks for its
|
|
|
|
|
/// own path (`GetModuleFileName`) to find its data, and must not get our
|
|
|
|
|
/// cache copy.
|
|
|
|
|
pub fn set_image_path(base: usize, path: &Path) {
|
|
|
|
|
let path = std::path::absolute(path).unwrap_or_else(|_| path.to_owned());
|
|
|
|
|
let mut paths = IMAGE_PATHS.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
paths.retain(|(b, _)| *b != base);
|
|
|
|
|
paths.push((base, path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn image_path(base: usize) -> Option<PathBuf> {
|
|
|
|
|
let paths = IMAGE_PATHS.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
paths
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|(b, _)| *b == base)
|
|
|
|
|
.map(|(_, p)| p.clone())
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:59:03 +01:00
|
|
|
/// The title image whose original file is called `file` (any case).
|
|
|
|
|
pub fn image_named(file: &str) -> Option<usize> {
|
|
|
|
|
let paths = IMAGE_PATHS.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
paths
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|(_, p)| p.file_name().is_some_and(|n| n.eq_ignore_ascii_case(file)))
|
|
|
|
|
.map(|(b, _)| *b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Maps a title DLL the title loads itself (`LoadLibrary`) and returns its
|
|
|
|
|
/// base; `Ok(None)` if `file` is not one of the title's DLLs.
|
|
|
|
|
pub type TitleDllLoader = Box<dyn Fn(&str) -> Result<Option<usize>, String> + Send + Sync>;
|
|
|
|
|
|
|
|
|
|
static TITLE_DLL_LOADER: std::sync::OnceLock<TitleDllLoader> = std::sync::OnceLock::new();
|
|
|
|
|
|
|
|
|
|
/// Set once, by whoever mapped the title (it knows the folders and the
|
|
|
|
|
/// resolver); later calls are ignored.
|
|
|
|
|
pub fn set_title_dll_loader(loader: TitleDllLoader) {
|
|
|
|
|
let _ = TITLE_DLL_LOADER.set(loader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn load_title_dll(file: &str) -> Result<Option<usize>, String> {
|
|
|
|
|
TITLE_DLL_LOADER.get().map_or(Ok(None), |load| load(file))
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:42:21 +01:00
|
|
|
/// What the title's manifest (`MicrosoftGame.config` beside its executable)
|
|
|
|
|
/// says about it, and where its persistent local storage lives.
|
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
|
|
|
|
pub struct Title {
|
|
|
|
|
pub id: u32,
|
|
|
|
|
/// Persistent local storage: our folder for it and the title's quota.
|
|
|
|
|
pub storage: PathBuf,
|
|
|
|
|
pub storage_bytes: u64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Title {
|
|
|
|
|
/// Title id and storage quota from the manifest's text (zero where it
|
|
|
|
|
/// has none); the storage folder is left for the caller to choose.
|
|
|
|
|
// shortcut: finds the two elements by text, not as XML; parse properly
|
|
|
|
|
// once more of the manifest is needed.
|
|
|
|
|
pub fn from_manifest(text: &str) -> Title {
|
|
|
|
|
fn element<'a>(text: &'a str, tag: &str) -> Option<&'a str> {
|
|
|
|
|
let start = text.find(&format!("<{tag}>"))? + tag.len() + 2;
|
|
|
|
|
let len = text[start..].find('<')?;
|
|
|
|
|
Some(text[start..start + len].trim())
|
|
|
|
|
}
|
|
|
|
|
let id = element(text, "TitleId").and_then(|v| u32::from_str_radix(v, 16).ok());
|
|
|
|
|
let storage_mb = text
|
|
|
|
|
.find("<PersistentLocalStorage>")
|
|
|
|
|
.and_then(|at| element(&text[at..], "SizeMB"))
|
|
|
|
|
.and_then(|v| v.parse::<u64>().ok());
|
|
|
|
|
Title {
|
|
|
|
|
id: id.unwrap_or(0),
|
|
|
|
|
storage: PathBuf::new(),
|
|
|
|
|
storage_bytes: storage_mb.unwrap_or(0) << 20,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static TITLE: Mutex<Option<Title>> = Mutex::new(None);
|
|
|
|
|
|
|
|
|
|
pub fn set_title(title: Title) {
|
|
|
|
|
*TITLE.lock().unwrap_or_else(|e| e.into_inner()) = Some(title);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The running title; the default (id 0, no storage) until set.
|
|
|
|
|
pub fn title() -> Title {
|
|
|
|
|
TITLE
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
|
|
|
.clone()
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn reads_title_id_and_storage_quota() {
|
|
|
|
|
let text = "<Game><Identity Name=\"x\"/>\n <TitleId>639AA8DD</TitleId>\n\
|
|
|
|
|
<PersistentLocalStorage>\n <SizeMB>1024</SizeMB> <!-- c -->\n\
|
|
|
|
|
</PersistentLocalStorage></Game>";
|
|
|
|
|
let t = Title::from_manifest(text);
|
|
|
|
|
assert_eq!((t.id, t.storage_bytes), (0x639A_A8DD, 1024 << 20));
|
|
|
|
|
assert_eq!(Title::from_manifest("").storage_bytes, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|