pub use wows_core::game_constants::*;
#[cfg(feature = "vfs")]
use std::io::Read;
#[cfg(feature = "vfs")]
fn read_vfs_file(vfs: &vfs::VfsPath, path: &str) -> Result<Vec<u8>, vfs::VfsError> {
let mut buf = Vec::new();
vfs.join(path)?.open_file()?.read_to_end(&mut buf).map_err(vfs::VfsError::from)?;
Ok(buf)
}
#[cfg(feature = "vfs")]
pub fn load_battle_constants(vfs: &vfs::VfsPath) -> BattleConstants {
if let Ok(buf) = read_vfs_file(vfs, BATTLE_CONSTANTS_PATH) {
BattleConstants::from_xml(&buf)
} else {
BattleConstants::defaults()
}
}
#[cfg(feature = "vfs")]
pub fn load_ships_constants(vfs: &vfs::VfsPath) -> ShipsConstants {
if let Ok(buf) = read_vfs_file(vfs, SHIPS_CONSTANTS_PATH) {
ShipsConstants::from_xml(&buf)
} else {
ShipsConstants::defaults()
}
}
#[cfg(feature = "vfs")]
pub fn load_weapons_constants(vfs: &vfs::VfsPath) -> WeaponsConstants {
if let Ok(buf) = read_vfs_file(vfs, WEAPONS_CONSTANTS_PATH) {
WeaponsConstants::from_xml(&buf)
} else {
WeaponsConstants::defaults()
}
}
#[cfg(feature = "vfs")]
pub fn load_common_constants(vfs: &vfs::VfsPath) -> CommonConstants {
if let Ok(buf) = read_vfs_file(vfs, COMMON_CONSTANTS_PATH) {
CommonConstants::from_xml(&buf)
} else {
CommonConstants::defaults()
}
}
#[cfg(feature = "vfs")]
pub fn load_channel_constants(vfs: &vfs::VfsPath) -> ChannelConstants {
if let Ok(buf) = read_vfs_file(vfs, CHANNEL_CONSTANTS_PATH) {
ChannelConstants::from_xml(&buf)
} else {
ChannelConstants::defaults()
}
}
pub fn apply_version_consumables(common: &mut CommonConstants, version: crate::data::Version) {
if let Some(table) = crate::consumable_versions::consumable_ids_for_version(version) {
*common.consumable_types_mut() =
table.iter().map(|(id, name)| (*id, std::borrow::Cow::Borrowed(*name))).collect();
}
}
#[cfg(test)]
mod version_consumable_tests {
use super::*;
use crate::data::Version;
fn v(major: u32, minor: u32, patch: u32, build: u32) -> Version {
Version { major, minor, patch, build }
}
#[test]
fn legacy_client_resolves_its_own_consumable_layout() {
let mut common = CommonConstants::defaults();
apply_version_consumables(&mut common, v(0, 9, 10, 3052606));
assert_eq!(common.consumable_type(22), Some("callFighters"));
assert_eq!(common.consumable_type(23), Some("regenerateHealth"));
assert_eq!(common.consumable_type(27), Some("depthCharges"));
assert_ne!(common.consumable_type(27), Some("trigger7"));
}
#[test]
fn modern_client_keeps_the_full_layout() {
let mut common = CommonConstants::defaults();
apply_version_consumables(&mut common, v(15, 2, 0, 12116141));
assert_eq!(common.consumable_type(0), Some("crashCrew"));
assert!(common.consumable_types().len() > 40);
}
}