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