// SPDX-License-Identifier: CPAL-1.0 // Copyright (c) 2026 Marek Novák. Built on Domi, https://aula.marek-novak.cz //! Epic Games Launcher: one JSON `.item` manifest per installed app in //! `%ProgramData%\Epic\EpicGamesLauncher\Data\Manifests`. Launch is //! `com.epicgames.launcher://apps/?action=launch&silent=true`. use crate::error::ParseError; use crate::id::{GameId, Source}; use crate::launch::Launch; use crate::limits::{MAX_DEPTH, MAX_FILE}; use crate::model::{CoverHint, Game}; use crate::path::WinPath; use crate::text; use serde::de::IgnoredAny; use serde::{Deserialize, Deserializer}; use serde_json::Value; /// The fields read from a manifest. Present-but-`null` is kept apart from absent, so `null` is a wrong type. #[derive(Deserialize)] struct Raw { #[serde(rename = "AppName", default, deserialize_with = "present")] app_name: Option, #[serde(rename = "DisplayName", default, deserialize_with = "present")] display_name: Option, #[serde(rename = "InstallLocation", default, deserialize_with = "present")] install_location: Option, #[serde(rename = "MainGameAppName", default, deserialize_with = "present")] main_game_app_name: Option, } const FIELDS: [&str; 4] = ["AppName", "DisplayName", "InstallLocation", "MainGameAppName"]; fn present<'de, D: Deserializer<'de>>(d: D) -> Result, D::Error> { Value::deserialize(d).map(Some) } /// The game described by one `.item` manifest. Reads `AppName`, `DisplayName` and `InstallLocation` (all /// required strings) and `MainGameAppName` (optional; when it names another app the file is an add-on and gives /// [`ParseError::NotAGame`]). Other fields are ignored but must still be well-formed JSON within the caps. pub fn item(json: &[u8]) -> Result { if json.len() > MAX_FILE { return Err(ParseError::TooLarge); } let whole = std::str::from_utf8(json).map_err(|_| ParseError::NotUtf8)?; let (bom, text) = match whole.strip_prefix('\u{feff}') { Some(rest) => (3, rest), None => (0, whole), }; check_depth(text)?; // serde would read a struct from an array too; only an object is a manifest. if !text.trim_start().starts_with('{') { return Err(match serde_json::from_str::(text) { Ok(_) => ParseError::WrongType("item"), Err(e) => map_error(&e, text, bom), }); } let raw: Raw = serde_json::from_str(text).map_err(|e| map_error(&e, text, bom))?; let app_name = required(raw.app_name.as_ref(), "AppName")?; let display_name = required(raw.display_name.as_ref(), "DisplayName")?; let install_location = required(raw.install_location.as_ref(), "InstallLocation")?; let main_game = optional(raw.main_game_app_name.as_ref(), "MainGameAppName")?; let launch = Launch::epic(app_name)?; let title = text::title(display_name, "DisplayName")?; let install_dir = WinPath::parse(install_location).map_err(|_| ParseError::BadPath("InstallLocation"))?; if main_game.is_some_and(|m| m != app_name) { return Err(ParseError::NotAGame("Epic add-on")); } let id = GameId::new(Source::Epic, &app_name.to_ascii_lowercase())?; Ok(Game { cover: Some(CoverHint::lettered(&title, &id)), id, source: Source::Epic, title, install_dir: Some(install_dir), launch, }) } fn required<'a>(v: Option<&'a Value>, field: &'static str) -> Result<&'a str, ParseError> { optional(v, field)?.ok_or(ParseError::Missing(field)) } fn optional<'a>(v: Option<&'a Value>, field: &'static str) -> Result, ParseError> { match v { None => Ok(None), Some(Value::String(s)) => Ok(Some(s)), Some(_) => Err(ParseError::WrongType(field)), } } /// Refuses nesting deeper than `MAX_DEPTH` before serde sees the text (objects and arrays both count; brackets /// inside strings do not). fn check_depth(text: &str) -> Result<(), ParseError> { let mut depth = 0usize; let mut in_string = false; let mut escaped = false; for b in text.bytes() { if in_string { match (escaped, b) { (true, _) => escaped = false, (false, b'\\') => escaped = true, (false, b'"') => in_string = false, _ => {} } continue; } match b { b'"' => in_string = true, b'{' | b'[' => { depth += 1; if depth > MAX_DEPTH { return Err(ParseError::TooDeep); } } b'}' | b']' => depth = depth.saturating_sub(1), _ => {} } } Ok(()) } /// Turns a serde_json error into a typed one: a repeated field, a document that is not an object, or a syntax /// error at a byte offset into the original input. fn map_error(e: &serde_json::Error, text: &str, bom: usize) -> ParseError { if e.is_data() { let msg = e.to_string(); if msg.starts_with("duplicate field") { let field = FIELDS.into_iter().find(|f| msg.contains(&format!("`{f}`"))).unwrap_or("field"); return ParseError::Duplicate(field); } return ParseError::WrongType("item"); } ParseError::Syntax { at: bom + offset(text, e.line(), e.column()) } } /// The byte offset of serde_json's 1-based line and column, clamped to the text. fn offset(text: &str, line: usize, column: usize) -> usize { let line_start: usize = text.split_inclusive('\n').take(line.saturating_sub(1)).map(str::len).sum(); (line_start + column.saturating_sub(1)).min(text.len().saturating_sub(1)) }