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 builds;
26pub mod cas;
27pub mod cas_vfs;
28#[cfg(feature = "constants")]
29pub mod constants;
30#[cfg(feature = "download")]
31pub mod download_repo;
32pub mod dump;
33pub mod manifest;
34pub mod registry;
35
36use std::path::PathBuf;
37
38use wowsunpack::game_data;
39use wowsunpack::vfs::VfsPath;
40
41/// Returns the path to the game_data/ directory.
42///
43/// Checks `WOWS_GAME_DATA` env var first, then walks up from the current
44/// directory to find the workspace root (identified by `game_versions.toml`).
45pub fn game_data_dir() -> Option<PathBuf> {
46 if let Ok(dir) = std::env::var("WOWS_GAME_DATA") {
47 let path = PathBuf::from(dir);
48 if path.exists() {
49 return Some(path);
50 }
51 }
52
53 // Walk up from current dir to find repo root
54 let mut dir = std::env::current_dir().ok()?;
55 loop {
56 if dir.join("game_versions.toml").exists() {
57 let data_dir = dir.join("game_data");
58 return Some(data_dir);
59 }
60 if !dir.pop() {
61 return None;
62 }
63 }
64}
65
66/// Returns sorted list of locally available build numbers.
67///
68/// Reads the local registry to find both downloaded builds
69/// (in `game_data/builds/<build>/`) and registered overrides.
70pub fn available_builds() -> Vec<u32> {
71 let Some(data_dir) = game_data_dir() else {
72 return Vec::new();
73 };
74 let reg = registry::load_registry(&data_dir.join("versions.toml"));
75 reg.available_builds()
76}
77
78/// Returns the game root path for a specific build.
79///
80/// For registered overrides, returns the override path.
81/// For downloaded builds, returns `game_data/builds/<build>/`.
82pub fn game_dir_for_build(build: u32) -> Option<PathBuf> {
83 let data_dir = game_data_dir()?;
84 let reg = registry::load_registry(&data_dir.join("versions.toml"));
85 reg.game_dir_for_build(build, &data_dir)
86}
87
88/// Constructs a VFS for a specific build.
89///
90/// Resolves path via [`game_dir_for_build`], then calls
91/// [`wowsunpack::game_data::build_game_vfs`].
92pub fn vfs_for_build(build: u32) -> Option<VfsPath> {
93 let game_dir = game_dir_for_build(build)?;
94 game_data::build_game_vfs(&game_dir).ok()
95}
96
97/// Returns the latest available build number and its VFS.
98pub fn latest_build() -> Option<(u32, VfsPath)> {
99 let builds = available_builds();
100 let build = *builds.last()?;
101 let vfs = vfs_for_build(build)?;
102 Some((build, vfs))
103}