Files
ArenaPublic/crates/arena-core/src/steam.rs
T

112 lines
4.4 KiB
Rust

// SPDX-License-Identifier: CPAL-1.0
// Copyright (c) 2026 Marek Novák. Built on Domi, https://aula.marek-novak.cz
//! Steam: `config/libraryfolders.vdf` lists the library folders; each installed app has
//! `<library>/steamapps/appmanifest_<id>.acf`. Launch is `steam://rungameid/<id>`.
use crate::error::ParseError;
use crate::id::{GameId, Source};
use crate::launch::Launch;
use crate::limits::MAX_LIBRARIES;
use crate::model::{CoverHint, Game};
use crate::path::WinPath;
use crate::text;
use crate::vdf::{self, Obj, Value};
/// Steam's own redistributables and runtimes: installed like games, never played.
const TOOLS: [u32; 7] = [
228_980, // Steamworks Common Redistributables
1_070_560, // Steam Linux Runtime 1.0 (scout)
1_391_110, // Steam Linux Runtime 2.0 (soldier)
1_628_350, // Steam Linux Runtime 3.0 (sniper)
1_493_710, // Proton Experimental
1_826_330, // Proton EasyAntiCheat Runtime
1_161_040, // Proton BattlEye Runtime
];
/// The library folders listed in `libraryfolders.vdf` (both the current layout, `"0" { "path" "..." }`, and the
/// old one, `"1" "D:\\SteamLibrary"`). At most [`crate::limits::MAX_LIBRARIES`]; a path listed twice is refused.
/// Keys under the root that are not plain numbers (`contentstatsid`, ...) are skipped.
pub fn library_folders(vdf: &[u8]) -> Result<Vec<WinPath>, ParseError> {
let doc = vdf::parse(vdf)?;
let root = obj(&doc, "libraryfolders")?;
let mut out: Vec<WinPath> = Vec::new();
for (key, value) in root.iter() {
if !key.bytes().all(|b| b.is_ascii_digit()) {
continue;
}
let raw = match value {
Value::Str(s) => s.as_str(),
Value::Obj(o) => string(o, "path")?,
};
let path = WinPath::parse(raw).map_err(|_| ParseError::BadPath("path"))?;
if out.iter().any(|p| p.eq_ignore_case(&path)) {
return Err(ParseError::Duplicate("path"));
}
if out.len() >= MAX_LIBRARIES {
return Err(ParseError::TooMany("libraries"));
}
out.push(path);
}
Ok(out)
}
/// The game described by `appmanifest_<id>.acf` inside `library` (the folder holding `steamapps`). The install
/// folder is `<library>\steamapps\common\<installdir>`, where `installdir` must be one plain folder name.
/// Steam's own redistributables and runtimes ([`is_tool`]) give [`ParseError::NotAGame`].
pub fn app_manifest(acf: &[u8], library: &WinPath) -> Result<Game, ParseError> {
let doc = vdf::parse(acf)?;
let state = obj(&doc, "AppState")?;
let app_id = text::canonical_u32(string(state, "appid")?).ok_or(ParseError::BadId)?;
let title = text::title(string(state, "name")?, "name")?;
let installdir = string(state, "installdir")?;
if is_tool(app_id) {
return Err(ParseError::NotAGame("Steam redistributable or runtime"));
}
if installdir.is_empty() || installdir.contains(['\\', '/']) {
return Err(ParseError::BadPath("installdir"));
}
let install_dir = library
.join("steamapps")
.and_then(|p| p.join("common"))
.and_then(|p| p.join(installdir))
.map_err(|_| ParseError::BadPath("installdir"))?;
Ok(Game {
id: GameId::new(Source::Steam, &app_id.to_string())?,
source: Source::Steam,
title,
install_dir: Some(install_dir),
launch: Launch::steam(app_id)?,
cover: Some(CoverHint::SteamLibraryCache { app_id }),
})
}
/// The app id in a file name `appmanifest_<id>.acf` (exact, lowercase, canonical id), for filtering a folder
/// listing; `None` for any other name.
pub fn app_id_from_manifest_name(name: &str) -> Option<u32> {
text::canonical_u32(name.strip_prefix("appmanifest_")?.strip_suffix(".acf")?)
}
/// Steam apps that are not games: the common redistributables and the Linux runtimes.
pub fn is_tool(app_id: u32) -> bool {
TOOLS.contains(&app_id)
}
/// The object under `key`.
fn obj<'a>(o: &'a Obj, key: &'static str) -> Result<&'a Obj, ParseError> {
match o.get(key) {
Some(Value::Obj(inner)) => Ok(inner),
Some(Value::Str(_)) => Err(ParseError::WrongType(key)),
None => Err(ParseError::Missing(key)),
}
}
/// The string under `key`.
fn string<'a>(o: &'a Obj, key: &'static str) -> Result<&'a str, ParseError> {
match o.get(key) {
Some(Value::Str(s)) => Ok(s),
Some(Value::Obj(_)) => Err(ParseError::WrongType(key)),
None => Err(ParseError::Missing(key)),
}
}