test: parse maps only once
This commit is contained in:
+64
-10
@@ -1,30 +1,84 @@
|
||||
mod mode;
|
||||
|
||||
use rosu_pp::Beatmap;
|
||||
use rosu_pp::{Beatmap, GameMode};
|
||||
use std::sync::OnceLock;
|
||||
|
||||
pub use self::mode::{Catch, Mania, Mode, Osu, Taiko};
|
||||
|
||||
static MAPS: OnceLock<[Beatmap; 4]> = OnceLock::new();
|
||||
|
||||
#[macro_export]
|
||||
#[rustfmt::skip]
|
||||
macro_rules! test_map {
|
||||
($mode:ident) => {{
|
||||
#[cfg(not(any(feature = "async_tokio", feature = "async_std")))]
|
||||
{ common::test_map::<$mode>() }
|
||||
{ common::test_map(GameMode::$mode) }
|
||||
#[cfg(any(feature = "async_tokio", feature = "async_std"))]
|
||||
{ common::test_map::<$mode>().await }
|
||||
{ common::test_map(GameMode::$mode).await }
|
||||
}};
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "async_tokio", feature = "async_std")))]
|
||||
pub fn test_map<M: Mode>() -> Beatmap {
|
||||
let path = format!("./maps/{}.osu", M::TEST_MAP_ID);
|
||||
pub fn test_map(mode: GameMode) -> &'static Beatmap {
|
||||
let maps = MAPS.get_or_init(|| {
|
||||
let osu = Beatmap::from_path(path(GameMode::Osu)).unwrap();
|
||||
let taiko = Beatmap::from_path(path(GameMode::Taiko)).unwrap();
|
||||
let catch = Beatmap::from_path(path(GameMode::Catch)).unwrap();
|
||||
let mania = Beatmap::from_path(path(GameMode::Mania)).unwrap();
|
||||
|
||||
Beatmap::from_path(path).unwrap()
|
||||
[osu, taiko, catch, mania]
|
||||
});
|
||||
|
||||
&maps[mode as usize]
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "async_tokio", feature = "async_std"))]
|
||||
pub async fn test_map<M: Mode>() -> Beatmap {
|
||||
let path = format!("./maps/{}.osu", M::TEST_MAP_ID);
|
||||
#[cfg(feature = "async_tokio")]
|
||||
pub async fn test_map(mode: GameMode) -> &'static Beatmap {
|
||||
let maps = MAPS.get_or_init(|| {
|
||||
let fut = async {
|
||||
let osu = Beatmap::from_path(path(GameMode::Osu)).await.unwrap();
|
||||
let taiko = Beatmap::from_path(path(GameMode::Taiko)).await.unwrap();
|
||||
let catch = Beatmap::from_path(path(GameMode::Catch)).await.unwrap();
|
||||
let mania = Beatmap::from_path(path(GameMode::Mania)).await.unwrap();
|
||||
|
||||
Beatmap::from_path(path).await.unwrap()
|
||||
[osu, taiko, catch, mania]
|
||||
};
|
||||
|
||||
match tokio::runtime::Handle::try_current() {
|
||||
Ok(h) => std::thread::spawn(move || h.block_on(fut)).join().unwrap(),
|
||||
Err(_) => tokio::runtime::Builder::new_current_thread()
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(fut),
|
||||
}
|
||||
});
|
||||
|
||||
&maps[mode as usize]
|
||||
}
|
||||
|
||||
#[cfg(feature = "async_std")]
|
||||
pub async fn test_map(mode: GameMode) -> &'static Beatmap {
|
||||
let maps = MAPS.get_or_init(|| {
|
||||
async_std::task::block_on(async {
|
||||
let osu = Beatmap::from_path(path(GameMode::Osu)).await.unwrap();
|
||||
let taiko = Beatmap::from_path(path(GameMode::Taiko)).await.unwrap();
|
||||
let catch = Beatmap::from_path(path(GameMode::Catch)).await.unwrap();
|
||||
let mania = Beatmap::from_path(path(GameMode::Mania)).await.unwrap();
|
||||
|
||||
[osu, taiko, catch, mania]
|
||||
})
|
||||
});
|
||||
|
||||
&maps[mode as usize]
|
||||
}
|
||||
|
||||
fn path(mode: GameMode) -> String {
|
||||
let map_id = match mode {
|
||||
GameMode::Osu => <Osu as Mode>::TEST_MAP_ID,
|
||||
GameMode::Taiko => <Taiko as Mode>::TEST_MAP_ID,
|
||||
GameMode::Catch => <Catch as Mode>::TEST_MAP_ID,
|
||||
GameMode::Mania => <Mania as Mode>::TEST_MAP_ID,
|
||||
};
|
||||
|
||||
format!("./maps/{map_id}.osu")
|
||||
}
|
||||
|
||||
+4
-6
@@ -1,7 +1,5 @@
|
||||
use rosu_pp::{Beatmap, GameMode};
|
||||
|
||||
use crate::common::{Catch, Mania, Osu, Taiko};
|
||||
|
||||
mod common;
|
||||
|
||||
#[cfg(not(any(feature = "async_tokio", feature = "async_std")))]
|
||||
@@ -93,7 +91,7 @@ mod async_tokio {
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_osu(map: Beatmap) {
|
||||
fn assert_osu(map: &Beatmap) {
|
||||
assert_eq!(map.mode, GameMode::Osu);
|
||||
assert_eq!(map.version, 14);
|
||||
assert_eq!(map.n_circles, 307);
|
||||
@@ -114,7 +112,7 @@ fn assert_osu(map: Beatmap) {
|
||||
assert_eq!(map.breaks.len(), 1)
|
||||
}
|
||||
|
||||
fn assert_taiko(map: Beatmap) {
|
||||
fn assert_taiko(map: &Beatmap) {
|
||||
assert_eq!(map.mode, GameMode::Taiko);
|
||||
assert_eq!(map.version, 14);
|
||||
assert_eq!(map.n_circles, 289);
|
||||
@@ -135,7 +133,7 @@ fn assert_taiko(map: Beatmap) {
|
||||
assert_eq!(map.breaks.len(), 0)
|
||||
}
|
||||
|
||||
fn assert_catch(map: Beatmap) {
|
||||
fn assert_catch(map: &Beatmap) {
|
||||
assert_eq!(map.mode, GameMode::Catch);
|
||||
assert_eq!(map.version, 14);
|
||||
assert_eq!(map.n_circles, 249);
|
||||
@@ -156,7 +154,7 @@ fn assert_catch(map: Beatmap) {
|
||||
assert_eq!(map.breaks.len(), 0)
|
||||
}
|
||||
|
||||
fn assert_mania(map: Beatmap) {
|
||||
fn assert_mania(map: &Beatmap) {
|
||||
assert_eq!(map.mode, GameMode::Mania);
|
||||
assert_eq!(map.version, 14);
|
||||
assert_eq!(map.n_circles, 473);
|
||||
|
||||
Reference in New Issue
Block a user