Files
6gbvram---16gb-ram/crates/victory-hle/src/runtime.rs
T

105 lines
3.6 KiB
Rust
Raw Normal View History

2026-11-23 01:15:26 +01:00
//! Process-wide state the implementations need.
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),
}
}
/// 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())
}
/// 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);
}
}