294 lines
12 KiB
Rust
294 lines
12 KiB
Rust
// SPDX-License-Identifier: CPAL-1.0
|
|
// Copyright (c) 2026 Marek Novák. Built on Domi, https://aula.marek-novak.cz
|
|
|
|
//! Game detection for the overlay: the three rules, Windows path matching, and the persisted marks.
|
|
|
|
use arena_core::detect::{Detection, How, Marks, detect};
|
|
use arena_core::{Game, GameId, Launch, ParseError, Source, WinPath, limits, manual};
|
|
use proptest::prelude::*;
|
|
|
|
fn p(s: &str) -> WinPath {
|
|
WinPath::parse(s).unwrap()
|
|
}
|
|
|
|
fn steam(app: u32, title: &str, dir: &str) -> Game {
|
|
let id = GameId::new(Source::Steam, &app.to_string()).unwrap();
|
|
Game {
|
|
id,
|
|
source: Source::Steam,
|
|
title: title.into(),
|
|
install_dir: Some(p(dir)),
|
|
launch: Launch::steam(app).unwrap(),
|
|
cover: None,
|
|
}
|
|
}
|
|
|
|
fn domi(app: &str) -> Game {
|
|
Game {
|
|
id: GameId::new(Source::Domi, app).unwrap(),
|
|
source: Source::Domi,
|
|
title: app.into(),
|
|
install_dir: None,
|
|
launch: Launch::domi_link(app).unwrap(),
|
|
cover: None,
|
|
}
|
|
}
|
|
|
|
fn library() -> Vec<Game> {
|
|
vec![
|
|
steam(10, "Foo", r"C:\Games\Foo"),
|
|
steam(20, "FooBar", r"C:\Games\FooBar"),
|
|
steam(30, "Nested", r"C:\Games\Foo\Expansion"),
|
|
domi("lapilli"),
|
|
]
|
|
}
|
|
|
|
fn id(s: &str) -> Option<GameId> {
|
|
Some(s.parse().unwrap())
|
|
}
|
|
|
|
fn found(game: &str, how: How) -> Option<Detection> {
|
|
Some(Detection { game: id(game), how })
|
|
}
|
|
|
|
const NONE: &Marks = &Marks::new();
|
|
|
|
// --- Rule 1: an exe outside every install_dir is never auto-detected ---
|
|
|
|
#[test]
|
|
fn outside_every_install_dir_is_not_detected() {
|
|
let lib = library();
|
|
for exe in [
|
|
r"C:\Windows\System32\notepad.exe",
|
|
r"C:\Games\foo.exe",
|
|
r"C:\Games\Fo\game.exe",
|
|
r"C:\Games\FooBarBaz\game.exe",
|
|
r"C:\Games\Foo Bar\game.exe",
|
|
r"D:\Games\Foo\game.exe",
|
|
r"C:\Program Files\Steam\steam.exe",
|
|
] {
|
|
assert_eq!(detect(&p(exe), false, &lib, NONE), None, "{exe}");
|
|
}
|
|
assert_eq!(detect(&p(r"C:\x.exe"), false, &[], NONE), None);
|
|
}
|
|
|
|
#[test]
|
|
fn inside_an_install_dir_is_detected() {
|
|
let lib = library();
|
|
assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), false, &lib, NONE), found("steam:10", How::InstallDir));
|
|
assert_eq!(detect(&p(r"C:\Games\Foo\bin\x64\foo.exe"), false, &lib, NONE), found("steam:10", How::InstallDir));
|
|
assert_eq!(detect(&p(r"C:\Games\FooBar\fb.exe"), false, &lib, NONE), found("steam:20", How::InstallDir));
|
|
}
|
|
|
|
#[test]
|
|
fn prefix_of_a_segment_never_matches() {
|
|
// C:\Games\Foo must not claim C:\Games\FooBar's exes, in either direction, whatever the order.
|
|
let only_foo = vec![steam(10, "Foo", r"C:\Games\Foo")];
|
|
assert_eq!(detect(&p(r"C:\Games\FooBar\fb.exe"), false, &only_foo, NONE), None);
|
|
let only_foobar = vec![steam(20, "FooBar", r"C:\Games\FooBar")];
|
|
assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), false, &only_foobar, NONE), None);
|
|
assert_eq!(detect(&p(r"C:\Games\Foo.exe"), false, &only_foo, NONE), None);
|
|
}
|
|
|
|
#[test]
|
|
fn the_deepest_install_dir_wins() {
|
|
let lib = library();
|
|
let exe = p(r"C:\Games\Foo\Expansion\exp.exe");
|
|
assert_eq!(detect(&exe, false, &lib, NONE), found("steam:30", How::InstallDir));
|
|
let mut rev = lib.clone();
|
|
rev.reverse();
|
|
assert_eq!(detect(&exe, false, &rev, NONE), found("steam:30", How::InstallDir));
|
|
}
|
|
|
|
#[test]
|
|
fn the_install_dir_itself_is_not_an_exe_inside_it() {
|
|
let lib = library();
|
|
assert_eq!(detect(&p(r"C:\Games\FooBar"), false, &lib, NONE), None);
|
|
}
|
|
|
|
#[test]
|
|
fn a_drive_root_install_dir_does_not_claim_the_whole_drive() {
|
|
// A manual game picked at D:\game.exe has the install folder D:\ — that must not make every exe on D: a game.
|
|
let g = manual::game(r"D:\game.exe", Some("Root game")).unwrap();
|
|
assert_eq!(g.install_dir, Some(p(r"D:\")));
|
|
let lib = vec![g.clone()];
|
|
assert_eq!(detect(&p(r"D:\Tools\editor.exe"), false, &lib, NONE), None);
|
|
assert_eq!(detect(&p(r"D:\other.exe"), false, &lib, NONE), None);
|
|
// Its own exe is still found, through the game's launch exe.
|
|
assert_eq!(detect(&p(r"d:\GAME.EXE"), false, &lib, NONE), Some(Detection { game: Some(g.id), how: How::InstallDir }));
|
|
}
|
|
|
|
#[test]
|
|
fn a_game_s_own_launch_exe_is_detected() {
|
|
let g = manual::game(r"E:\Old\Thing\thing.exe", None).unwrap();
|
|
let lib = vec![g.clone()];
|
|
assert_eq!(detect(&p(r"E:\Old\Thing\thing.exe"), false, &lib, NONE).map(|d| d.how), Some(How::InstallDir));
|
|
assert_eq!(detect(&p(r"E:\Old\Thing\helper.exe"), false, &lib, NONE).and_then(|d| d.game), Some(g.id));
|
|
}
|
|
|
|
// --- Windows path rules ---
|
|
|
|
#[test]
|
|
fn matching_ignores_case_and_separator_style() {
|
|
let lib = library();
|
|
for exe in [r"c:\games\foo\foo.exe", r"C:\GAMES\FOO\FOO.EXE", "C:/Games/foo/Bin/foo.exe", r"c:\\Games\\\Foo\\x.exe"]
|
|
{
|
|
assert_eq!(detect(&p(exe), false, &lib, NONE), found("steam:10", How::InstallDir), "{exe}");
|
|
}
|
|
let upper = vec![steam(10, "Foo", r"C:\GAMES\FOO")];
|
|
assert_eq!(detect(&p(r"c:\games\foo\a.exe"), false, &upper, NONE), found("steam:10", How::InstallDir));
|
|
}
|
|
|
|
#[test]
|
|
fn non_ascii_case_folds_too() {
|
|
let lib = vec![steam(40, "Hra", r"C:\Hry\Žluťoučký Kůň")];
|
|
assert_eq!(detect(&p(r"C:\HRY\ŽLUŤOUČKÝ KŮŇ\kun.exe"), false, &lib, NONE), found("steam:40", How::InstallDir));
|
|
}
|
|
|
|
#[test]
|
|
fn dot_dot_never_escapes() {
|
|
// A path with `..` cannot even be built, so `C:\Games\Foo\..\Other\x.exe` can never pass as inside Foo.
|
|
for s in [
|
|
r"C:\Games\Foo\..\Other\x.exe",
|
|
r"C:\Games\Foo\..\..\Windows\x.exe",
|
|
r"C:\Games\Foo\.\x.exe",
|
|
r"C:\Games\Foo.\x.exe",
|
|
r"C:\Games\Foo \x.exe",
|
|
r"\\?\C:\Games\Foo\x.exe",
|
|
r"\\server\share\Games\Foo\x.exe",
|
|
] {
|
|
assert!(WinPath::parse(s).is_err(), "{s}");
|
|
}
|
|
// Nor can a mark or a game folder carry one in through JSON.
|
|
assert!(serde_json::from_str::<Marks>(r#"["C:\\Games\\Foo\\..\\x.exe"]"#).is_err());
|
|
// An exe in a sibling folder is not inside, even if a join tried to climb.
|
|
assert!(p(r"C:\Games\Foo").join(r"..\FooBar\fb.exe").is_err());
|
|
}
|
|
|
|
// --- Rule 2: launched by Arena ---
|
|
|
|
#[test]
|
|
fn launched_by_arena_is_always_detected() {
|
|
let lib = library();
|
|
assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), true, &lib, NONE), found("steam:10", How::Launched));
|
|
// Outside every install dir (a launcher stub, a Domi app): still a game, the window says which.
|
|
assert_eq!(
|
|
detect(&p(r"C:\Users\u\AppData\Local\Lapilli\lapilli.exe"), true, &lib, NONE),
|
|
Some(Detection { game: None, how: How::Launched })
|
|
);
|
|
assert_eq!(detect(&p(r"C:\x.exe"), true, &[], NONE), Some(Detection { game: None, how: How::Launched }));
|
|
}
|
|
|
|
#[test]
|
|
fn launched_beats_marked() {
|
|
let mut marks = Marks::new();
|
|
marks.insert(p(r"C:\Games\Foo\foo.exe")).unwrap();
|
|
assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), true, &library(), &marks).map(|d| d.how), Some(How::Launched));
|
|
}
|
|
|
|
// --- Rule 3: marked ("This is a game") ---
|
|
|
|
#[test]
|
|
fn marked_exe_outside_install_dirs_is_detected_without_a_game() {
|
|
let mut marks = Marks::new();
|
|
assert!(marks.insert(p(r"D:\Emulators\retro.exe")).unwrap());
|
|
let lib = library();
|
|
assert_eq!(detect(&p(r"D:\Emulators\retro.exe"), false, &lib, &marks), Some(Detection { game: None, how: How::Marked }));
|
|
assert_eq!(detect(&p(r"d:\emulators\RETRO.exe"), false, &lib, &marks).map(|d| d.how), Some(How::Marked));
|
|
// Only that exe: not its folder, not its neighbours.
|
|
assert_eq!(detect(&p(r"D:\Emulators\other.exe"), false, &lib, &marks), None);
|
|
assert_eq!(detect(&p(r"D:\Emulators\retro.exe\x.exe"), false, &lib, &marks), None);
|
|
}
|
|
|
|
#[test]
|
|
fn marked_exe_inside_an_install_dir_reports_the_game() {
|
|
let mut marks = Marks::new();
|
|
marks.insert(p(r"C:\Games\Foo\foo.exe")).unwrap();
|
|
assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), false, &library(), &marks), found("steam:10", How::InstallDir));
|
|
}
|
|
|
|
#[test]
|
|
fn marks_are_keyed_by_the_normalised_path() {
|
|
let mut marks = Marks::new();
|
|
assert!(marks.insert(p(r"C:\Emu\Retro.exe")).unwrap());
|
|
assert!(!marks.insert(p("c:/emu//RETRO.EXE")).unwrap(), "same exe, another spelling");
|
|
assert_eq!(marks.len(), 1);
|
|
assert!(marks.contains(&p(r"c:\EMU\retro.exe")));
|
|
assert!(marks.remove(&p(r"C:\EMU\RETRO.EXE")));
|
|
assert!(marks.is_empty());
|
|
assert!(!marks.remove(&p(r"C:\Emu\Retro.exe")));
|
|
}
|
|
|
|
#[test]
|
|
fn marks_persist_through_json() {
|
|
let mut marks = Marks::new();
|
|
marks.insert(p(r"D:\Emu\Retro.exe")).unwrap();
|
|
marks.insert(p(r"C:\Tools\Game.exe")).unwrap();
|
|
let json = serde_json::to_string(&marks).unwrap();
|
|
// A plain array of paths, the first spelling kept, sorted by the case-folded key.
|
|
assert_eq!(json, r#"["C:\\Tools\\Game.exe","D:\\Emu\\Retro.exe"]"#);
|
|
let back: Marks = serde_json::from_str(&json).unwrap();
|
|
assert_eq!(back, marks);
|
|
assert_eq!(detect(&p(r"d:\emu\retro.exe"), false, &[], &back).map(|d| d.how), Some(How::Marked));
|
|
}
|
|
|
|
#[test]
|
|
fn marks_json_is_checked() {
|
|
// Spellings of one exe collapse on load; bad paths and non-strings are refused.
|
|
let m: Marks = serde_json::from_str(r#"["C:\\A\\x.exe","c:/a/X.EXE"]"#).unwrap();
|
|
assert_eq!(m.len(), 1);
|
|
for bad in [r#"["relative\\x.exe"]"#, r#"["\\\\srv\\s\\x.exe"]"#, r#"[1]"#, r#"{"a":1}"#, r#""C:\\x.exe""#] {
|
|
assert!(serde_json::from_str::<Marks>(bad).is_err(), "{bad}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn marks_are_capped() {
|
|
let mut marks = Marks::new();
|
|
for i in 0..limits::MAX_MARKS {
|
|
marks.insert(p(&format!(r"C:\G\{i}.exe"))).unwrap();
|
|
}
|
|
assert_eq!(marks.insert(p(r"C:\G\one-more.exe")), Err(ParseError::TooMany("marks")));
|
|
// Re-marking one already there is fine at the cap.
|
|
assert_eq!(marks.insert(p(r"C:\G\0.exe")), Ok(false));
|
|
let json = serde_json::to_string(&marks).unwrap();
|
|
assert!(serde_json::from_str::<Marks>(&json).is_ok());
|
|
let over: Vec<String> = (0..=limits::MAX_MARKS).map(|i| format!(r"C:\G\{i}.exe")).collect();
|
|
assert!(serde_json::from_str::<Marks>(&serde_json::to_string(&over).unwrap()).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn how_serialises_snake_case() {
|
|
let d = Detection { game: id("steam:10"), how: How::InstallDir };
|
|
assert_eq!(serde_json::to_string(&d).unwrap(), r#"{"game":"steam:10","how":"install_dir"}"#);
|
|
assert_eq!(serde_json::to_string(&How::Launched).unwrap(), r#""launched""#);
|
|
assert_eq!(serde_json::to_string(&How::Marked).unwrap(), r#""marked""#);
|
|
}
|
|
|
|
proptest! {
|
|
/// Whatever the folder names, auto-detection only ever claims exes inside an install dir, on whole segments.
|
|
#[test]
|
|
fn auto_detection_never_leaves_install_dirs(
|
|
dirs in proptest::collection::vec("[A-Za-z]{1,4}( [A-Za-z]{1,3})?", 1..4),
|
|
exe in r"[A-Za-z]{1,4}(\\[A-Za-z]{1,4}){0,3}\\[a-z]{1,4}\.exe",
|
|
) {
|
|
// Letters can spell device names (`con`, `nul`): those paths do not exist, so skip them.
|
|
prop_assume!(dirs.iter().all(|d| WinPath::parse(&format!(r"C:\Games\{d}")).is_ok()));
|
|
let exe = WinPath::parse(&format!(r"C:\Games\{exe}"));
|
|
prop_assume!(exe.is_ok());
|
|
let exe = exe.unwrap();
|
|
let lib: Vec<Game> = dirs.iter().enumerate()
|
|
.map(|(i, d)| steam(i as u32 + 1, d, &format!(r"C:\Games\{d}")))
|
|
.collect();
|
|
let first = exe.components().nth(1).unwrap().to_lowercase();
|
|
let expected = dirs.iter().any(|d| d.to_lowercase() == first);
|
|
let got = detect(&exe, false, &lib, NONE);
|
|
prop_assert_eq!(got.is_some(), expected);
|
|
if let Some(d) = got {
|
|
prop_assert_eq!(d.how, How::InstallDir);
|
|
let game = lib.iter().find(|g| Some(&g.id) == d.game.as_ref()).unwrap();
|
|
prop_assert!(exe.starts_with(game.install_dir.as_ref().unwrap()));
|
|
}
|
|
}
|
|
}
|