Skip to main content

wows_data_mgr/
lib.rs

1//! Test helper API for accessing downloaded World of Warships game data.
2//!
3//! Use these functions in integration tests to get VFS access to game builds.
4//! Tests should skip gracefully when game data is unavailable.
5//!
6//! # Example
7//!
8//! ```ignore
9//! use wows_data_mgr::{available_builds, vfs_for_build};
10//!
11//! #[test]
12//! fn test_game_params_load() {
13//!     let builds = available_builds();
14//!     if builds.is_empty() {
15//!         eprintln!("Skipping: no game data available");
16//!         return;
17//!     }
18//!     for build in builds {
19//!         let vfs = vfs_for_build(build).unwrap();
20//!         // test with vfs...
21//!     }
22//! }
23//! ```
24
25pub mod manifest;
26pub mod registry;
27
28use std::path::PathBuf;
29
30use wowsunpack::game_data;
31use wowsunpack::vfs::VfsPath;
32
33/// Returns the path to the game_data/ directory.
34///
35/// Checks `WOWS_GAME_DATA` env var first, then walks up from the current
36/// directory to find the workspace root (identified by `game_versions.toml`).
37pub fn game_data_dir() -> Option<PathBuf> {
38    if let Ok(dir) = std::env::var("WOWS_GAME_DATA") {
39        let path = PathBuf::from(dir);
40        if path.exists() {
41            return Some(path);
42        }
43    }
44
45    // Walk up from current dir to find repo root
46    let mut dir = std::env::current_dir().ok()?;
47    loop {
48        if dir.join("game_versions.toml").exists() {
49            let data_dir = dir.join("game_data");
50            return Some(data_dir);
51        }
52        if !dir.pop() {
53            return None;
54        }
55    }
56}
57
58/// Returns sorted list of locally available build numbers.
59///
60/// Reads the local registry to find both downloaded builds
61/// (in `game_data/builds/<build>/`) and registered overrides.
62pub fn available_builds() -> Vec<u32> {
63    let Some(data_dir) = game_data_dir() else {
64        return Vec::new();
65    };
66    let reg = registry::load_registry(&data_dir.join("versions.toml"));
67    reg.available_builds()
68}
69
70/// Returns the game root path for a specific build.
71///
72/// For registered overrides, returns the override path.
73/// For downloaded builds, returns `game_data/builds/<build>/`.
74pub fn game_dir_for_build(build: u32) -> Option<PathBuf> {
75    let data_dir = game_data_dir()?;
76    let reg = registry::load_registry(&data_dir.join("versions.toml"));
77    reg.game_dir_for_build(build, &data_dir)
78}
79
80/// Constructs a VFS for a specific build.
81///
82/// Resolves path via [`game_dir_for_build`], then calls
83/// [`wowsunpack::game_data::build_game_vfs`].
84pub fn vfs_for_build(build: u32) -> Option<VfsPath> {
85    let game_dir = game_dir_for_build(build)?;
86    game_data::build_game_vfs(&game_dir).ok()
87}
88
89/// Returns the latest available build number and its VFS.
90pub fn latest_build() -> Option<(u32, VfsPath)> {
91    let builds = available_builds();
92    let build = *builds.last()?;
93    let vfs = vfs_for_build(build)?;
94    Some((build, vfs))
95}