321 lines
9.0 KiB
Rust
321 lines
9.0 KiB
Rust
|
|
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::Mutex;
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
use std::thread::ThreadId;
|
|
|
|
static MAIN_IMAGE_BASE: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
pub fn set_main_image(base: usize) {
|
|
MAIN_IMAGE_BASE.store(base, Ordering::Release);
|
|
crate::hle::image_watch::start(base);
|
|
}
|
|
|
|
pub fn main_image() -> Option<usize> {
|
|
match MAIN_IMAGE_BASE.load(Ordering::Acquire) {
|
|
0 => None,
|
|
base => Some(base),
|
|
}
|
|
}
|
|
|
|
struct ImagePath {
|
|
base: usize,
|
|
path: PathBuf,
|
|
|
|
pending_on: Option<ThreadId>,
|
|
}
|
|
|
|
impl ImagePath {
|
|
fn visible_to(&self, thread: ThreadId) -> bool {
|
|
self.pending_on.is_none_or(|owner| owner == thread)
|
|
}
|
|
}
|
|
|
|
static IMAGE_PATHS: Mutex<Vec<ImagePath>> = Mutex::new(Vec::new());
|
|
|
|
fn insert_image_path(base: usize, path: &Path, pending_on: Option<ThreadId>) {
|
|
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(|e| e.base != base);
|
|
paths.push(ImagePath {
|
|
base,
|
|
path,
|
|
pending_on,
|
|
});
|
|
}
|
|
|
|
pub fn set_image_path(base: usize, path: &Path) {
|
|
insert_image_path(base, path, None);
|
|
}
|
|
|
|
pub fn stage_image_path(base: usize, path: &Path) {
|
|
insert_image_path(base, path, Some(std::thread::current().id()));
|
|
}
|
|
|
|
pub fn publish_images(bases: &[usize]) {
|
|
if bases.is_empty() {
|
|
return;
|
|
}
|
|
let mut paths = IMAGE_PATHS.lock().unwrap_or_else(|e| e.into_inner());
|
|
for entry in paths.iter_mut() {
|
|
if bases.contains(&entry.base) {
|
|
entry.pending_on = None;
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn forget_image(base: usize) {
|
|
let mut paths = IMAGE_PATHS.lock().unwrap_or_else(|e| e.into_inner());
|
|
paths.retain(|e| e.base != base);
|
|
}
|
|
|
|
pub fn image_path(base: usize) -> Option<PathBuf> {
|
|
let me = std::thread::current().id();
|
|
let paths = IMAGE_PATHS.lock().unwrap_or_else(|e| e.into_inner());
|
|
paths
|
|
.iter()
|
|
.find(|e| e.base == base && e.visible_to(me))
|
|
.map(|e| e.path.clone())
|
|
}
|
|
|
|
pub fn image_containing(address: usize) -> Option<usize> {
|
|
let me = std::thread::current().id();
|
|
let size = |base: usize| -> usize {
|
|
|
|
unsafe {
|
|
let nt = base + ((base + 0x3c) as *const u32).read_unaligned() as usize;
|
|
((nt + 0x50) as *const u32).read_unaligned() as usize
|
|
}
|
|
};
|
|
let bases: Vec<usize> = {
|
|
let paths = IMAGE_PATHS.lock().unwrap_or_else(|e| e.into_inner());
|
|
paths
|
|
.iter()
|
|
.filter(|e| e.visible_to(me))
|
|
.map(|e| e.base)
|
|
.chain(main_image())
|
|
.collect()
|
|
};
|
|
bases
|
|
.into_iter()
|
|
.find(|&base| address >= base && address - base < size(base))
|
|
}
|
|
|
|
pub fn title_dir() -> Option<PathBuf> {
|
|
Some(image_path(main_image()?)?.parent()?.to_owned())
|
|
}
|
|
|
|
pub fn image_named(file: &str) -> Option<usize> {
|
|
let me = std::thread::current().id();
|
|
let paths = IMAGE_PATHS.lock().unwrap_or_else(|e| e.into_inner());
|
|
paths
|
|
.iter()
|
|
.find(|e| {
|
|
e.visible_to(me)
|
|
&& e.path
|
|
.file_name()
|
|
.is_some_and(|n| n.eq_ignore_ascii_case(file))
|
|
})
|
|
.map(|e| e.base)
|
|
}
|
|
|
|
static TITLE_ARGS: std::sync::OnceLock<Vec<String>> = std::sync::OnceLock::new();
|
|
|
|
pub fn set_title_args(args: Vec<String>) {
|
|
let _ = TITLE_ARGS.set(args);
|
|
}
|
|
|
|
pub fn title_args() -> &'static [String] {
|
|
TITLE_ARGS.get().map_or(&[], Vec::as_slice)
|
|
}
|
|
|
|
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();
|
|
|
|
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))
|
|
}
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
|
pub struct Title {
|
|
pub id: u32,
|
|
|
|
pub store_id: String,
|
|
|
|
pub storage: PathBuf,
|
|
pub storage_bytes: u64,
|
|
}
|
|
|
|
impl Title {
|
|
|
|
pub fn from_manifest(text: &str) -> Title {
|
|
let elements = elements(text);
|
|
let get = |path: &str| {
|
|
elements
|
|
.iter()
|
|
.find(|(p, _)| p == path)
|
|
.map(|(_, text)| text.as_str())
|
|
};
|
|
let id = get("Game/TitleId").and_then(|v| u32::from_str_radix(v, 16).ok());
|
|
let storage_mb = get("Game/PersistentLocalStorage/SizeMB").and_then(|v| v.parse::<u64>().ok());
|
|
Title {
|
|
id: id.unwrap_or(0),
|
|
store_id: get("Game/StoreId").unwrap_or_default().to_owned(),
|
|
storage: PathBuf::new(),
|
|
storage_bytes: storage_mb.unwrap_or(0).saturating_mul(1 << 20),
|
|
}
|
|
}
|
|
|
|
pub fn saves(&self) -> PathBuf {
|
|
self.storage.with_file_name("saves")
|
|
}
|
|
|
|
pub fn screenshots(&self) -> PathBuf {
|
|
self.storage.with_file_name("screenshots")
|
|
}
|
|
}
|
|
|
|
fn field_text(rest: &str) -> String {
|
|
let mut out = String::new();
|
|
let mut rest = rest;
|
|
loop {
|
|
let Some(open) = rest.find('<') else {
|
|
out.push_str(rest);
|
|
break;
|
|
};
|
|
out.push_str(&rest[..open]);
|
|
if rest[open..].starts_with("<!--") {
|
|
let Some(end) = rest[open..].find("-->") else {
|
|
break;
|
|
};
|
|
rest = &rest[open + end + 3..];
|
|
} else {
|
|
|
|
break;
|
|
}
|
|
}
|
|
out.trim().to_owned()
|
|
}
|
|
|
|
fn elements(text: &str) -> Vec<(String, String)> {
|
|
let mut out = Vec::new();
|
|
let mut path: Vec<&str> = Vec::new();
|
|
let mut rest = text;
|
|
while let Some(open) = rest.find('<') {
|
|
rest = &rest[open..];
|
|
let end = if rest.starts_with("<!--") {
|
|
rest.find("-->").map(|e| e + 3)
|
|
} else if rest.starts_with("<![CDATA[") {
|
|
rest.find("]]>").map(|e| e + 3)
|
|
} else {
|
|
tag_end(rest)
|
|
};
|
|
let Some(end) = end else { break };
|
|
let tag = &rest[1..end - 1];
|
|
rest = &rest[end..];
|
|
if tag.starts_with(['!', '?']) {
|
|
continue;
|
|
}
|
|
if let Some(name) = tag.strip_prefix('/') {
|
|
if let Some(at) = path.iter().rposition(|n| *n == name.trim()) {
|
|
path.truncate(at);
|
|
}
|
|
continue;
|
|
}
|
|
let closed = tag.ends_with('/');
|
|
let name = tag
|
|
.trim_end_matches('/')
|
|
.split_whitespace()
|
|
.next()
|
|
.unwrap_or("");
|
|
path.push(name);
|
|
let body = field_text(rest);
|
|
let body = [("<", "<"), (">", ">"), (""", "\""), ("'", "'"), ("&", "&")]
|
|
.iter()
|
|
.fold(body, |b, (entity, c)| b.replace(entity, c));
|
|
out.push((path.join("/"), body));
|
|
if closed {
|
|
path.pop();
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
fn tag_end(text: &str) -> Option<usize> {
|
|
let mut quote = None;
|
|
for (at, c) in text.char_indices() {
|
|
match (quote, c) {
|
|
(None, '"' | '\'') => quote = Some(c),
|
|
(Some(open), _) if c == open => quote = None,
|
|
(None, '>') => return Some(at + 1),
|
|
_ => {}
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
pub fn title() -> Title {
|
|
TITLE
|
|
.lock()
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
.clone()
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
pub fn use_console_timers() {
|
|
use windows_sys::Win32::Media::timeBeginPeriod;
|
|
use windows_sys::Win32::System::Threading::{
|
|
GetCurrentProcess, PROCESS_POWER_THROTTLING_CURRENT_VERSION, PROCESS_POWER_THROTTLING_EXECUTION_SPEED,
|
|
PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION, PROCESS_POWER_THROTTLING_STATE,
|
|
ProcessPowerThrottling, SetProcessInformation,
|
|
};
|
|
|
|
let execution = if std::env::var_os("EONACAT_XS_EFFICIENCY_MODE").is_some_and(|v| v == "1") {
|
|
0
|
|
} else {
|
|
PROCESS_POWER_THROTTLING_EXECUTION_SPEED
|
|
};
|
|
let keep = PROCESS_POWER_THROTTLING_STATE {
|
|
Version: PROCESS_POWER_THROTTLING_CURRENT_VERSION,
|
|
ControlMask: PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION | execution,
|
|
StateMask: 0,
|
|
};
|
|
|
|
unsafe {
|
|
SetProcessInformation(
|
|
GetCurrentProcess(),
|
|
ProcessPowerThrottling,
|
|
(&raw const keep).cast(),
|
|
size_of_val(&keep) as u32,
|
|
);
|
|
timeBeginPeriod(1);
|
|
}
|
|
ahead_of_other_programs();
|
|
eonacat_xsbox_gpu::place_process();
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
fn ahead_of_other_programs() {
|
|
use windows_sys::Win32::System::Threading::{ABOVE_NORMAL_PRIORITY_CLASS, GetCurrentProcess, SetPriorityClass};
|
|
if std::env::var_os("EONACAT_XS_PROCESS_PRIORITY").is_some_and(|v| v == "normal" || v == "0") {
|
|
return;
|
|
}
|
|
|
|
let set = unsafe { SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS) } != 0;
|
|
tracing::info!(set, "the process runs above normal priority (EONACAT_XS_PROCESS_PRIORITY=normal: not)");
|
|
}
|
|
|