tiger_lib/
launcher_settings.rs

1//! Loader for the `launcher-settings.json` file.
2
3use std::path::Path;
4
5use anyhow::{Context, Result, bail};
6
7use crate::fileset::{FileEntry, FileKind, FileStage};
8use crate::game::Game;
9use crate::parse::json::parse_json_file;
10
11/// Looks up the game's version in the launcher settings.
12pub fn get_version_from_launcher(game_dir: &Path) -> Result<String> {
13    let launcher_pathname = if Game::is_hoi4() {
14        game_dir.join("launcher-settings.json")
15    } else {
16        game_dir.join("launcher/launcher-settings.json")
17    };
18    let launcher_entry = FileEntry::new(
19        launcher_pathname.clone(),
20        FileStage::NoStage,
21        FileKind::Vanilla,
22        launcher_pathname.clone(),
23    );
24    let block = parse_json_file(&launcher_entry).with_context(|| {
25        format!("Could not parse launcher file {}", launcher_pathname.display())
26    })?;
27    if let Some(topblock) = block.iter_blocks().next() {
28        if let Some(version) = topblock.get_field_value("rawVersion") {
29            return Ok(version.as_str().to_owned());
30        }
31    }
32    bail!("Version not found in {}", launcher_pathname.display())
33}