use std::sync::OnceLock;
static GAME: OnceLock<Game> = OnceLock::new();
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Game {
#[cfg(feature = "ck3")]
Ck3,
#[cfg(feature = "vic3")]
Vic3,
#[cfg(feature = "imperator")]
Imperator,
#[cfg(feature = "eu5")]
Eu5,
#[cfg(feature = "hoi4")]
Hoi4,
}
impl Game {
pub fn set(game: Game) -> Result<(), String> {
GAME.set(game).map_err(|_| "tried to set game type twice".to_string())?;
Ok(())
}
#[allow(clippy::self_named_constructors)] #[allow(unreachable_code)]
pub fn game() -> Game {
#[cfg(all(
feature = "ck3",
not(feature = "vic3"),
not(feature = "imperator"),
not(feature = "eu5"),
not(feature = "hoi4")
))]
return Game::Ck3;
#[cfg(all(
feature = "vic3",
not(feature = "ck3"),
not(feature = "imperator"),
not(feature = "eu5"),
not(feature = "hoi4")
))]
return Game::Vic3;
#[cfg(all(
feature = "imperator",
not(feature = "ck3"),
not(feature = "vic3"),
not(feature = "eu5"),
not(feature = "hoi4")
))]
return Game::Imperator;
#[cfg(all(
feature = "hoi4",
not(feature = "ck3"),
not(feature = "vic3"),
not(feature = "imperator"),
not(feature = "eu5"),
))]
return Game::Hoi4;
#[cfg(all(
feature = "eu5",
not(feature = "ck3"),
not(feature = "vic3"),
not(feature = "imperator"),
not(feature = "hoi4"),
))]
return Game::Eu5;
*GAME.get().expect("internal error: don't know which game we are validating")
}
#[inline]
pub fn is_ck3() -> bool {
#[cfg(not(feature = "ck3"))]
return false;
#[cfg(all(
feature = "ck3",
not(feature = "vic3"),
not(feature = "imperator"),
not(feature = "eu5"),
not(feature = "hoi4")
))]
return true;
#[cfg(all(
feature = "ck3",
any(feature = "vic3", feature = "imperator", feature = "eu5", feature = "hoi4")
))]
return GAME.get() == Some(&Game::Ck3);
}
#[inline]
pub fn is_vic3() -> bool {
#[cfg(not(feature = "vic3"))]
return false;
#[cfg(all(
feature = "vic3",
not(feature = "ck3"),
not(feature = "imperator"),
not(feature = "eu5"),
not(feature = "hoi4")
))]
return true;
#[cfg(all(
feature = "vic3",
any(feature = "ck3", feature = "imperator", feature = "eu5", feature = "hoi4")
))]
return GAME.get() == Some(&Game::Vic3);
}
#[inline]
pub fn is_imperator() -> bool {
#[cfg(not(feature = "imperator"))]
return false;
#[cfg(all(
feature = "imperator",
not(feature = "ck3"),
not(feature = "vic3"),
not(feature = "eu5"),
not(feature = "hoi4")
))]
return true;
#[cfg(all(
feature = "imperator",
any(feature = "ck3", feature = "vic3", feature = "hoi4", feature = "eu5")
))]
return GAME.get() == Some(&Game::Imperator);
}
#[inline]
pub fn is_eu5() -> bool {
#[cfg(not(feature = "eu5"))]
return false;
#[cfg(all(
feature = "eu5",
not(feature = "ck3"),
not(feature = "vic3"),
not(feature = "imperator"),
not(feature = "hoi4")
))]
return true;
#[cfg(all(
feature = "eu5",
any(feature = "ck3", feature = "vic3", feature = "hoi4", feature = "imperator")
))]
return GAME.get() == Some(&Game::Eu5);
}
#[inline]
pub fn is_jomini() -> bool {
Game::is_ck3() || Game::is_vic3() || Game::is_imperator() || Game::is_eu5()
}
#[inline]
pub fn is_hoi4() -> bool {
#[cfg(not(feature = "hoi4"))]
return false;
#[cfg(all(
feature = "hoi4",
not(feature = "ck3"),
not(feature = "vic3"),
not(feature = "imperator"),
not(feature = "eu5")
))]
return true;
#[cfg(all(
feature = "hoi4",
any(feature = "ck3", feature = "vic3", feature = "imperator", feature = "eu5")
))]
return GAME.get() == Some(&Game::Hoi4);
}
}