store is_convert in Beatmap itself

This commit is contained in:
MaxOhn
2024-03-02 06:27:52 +01:00
parent 0f23f81234
commit 59dfc109e0
16 changed files with 66 additions and 115 deletions
+3 -7
View File
@@ -34,13 +34,13 @@ pub struct HitWindows {
#[derive(Clone, Debug, Default, PartialEq)]
pub struct BeatmapAttributesBuilder {
mode: GameMode,
is_convert: bool,
ar: f32,
od: f32,
cs: f32,
hp: f32,
mods: u32,
clock_rate: Option<f64>,
is_convert: bool,
}
impl BeatmapAttributesBuilder {
@@ -54,10 +54,6 @@ impl BeatmapAttributesBuilder {
/// Create a new [`BeatmapAttributesBuilder`].
pub const fn new(map: &Beatmap) -> Self {
Self::new_internal(map, false)
}
const fn new_internal(map: &Beatmap, is_convert: bool) -> Self {
Self {
mode: map.mode,
ar: map.ar,
@@ -66,7 +62,7 @@ impl BeatmapAttributesBuilder {
hp: map.hp,
mods: 0,
clock_rate: None,
is_convert,
is_convert: map.is_convert,
}
}
@@ -243,7 +239,7 @@ impl From<&Beatmap> for BeatmapAttributesBuilder {
impl<M> From<&Converted<'_, M>> for BeatmapAttributesBuilder {
fn from(converted: &Converted<'_, M>) -> Self {
Self::new_internal(converted.map.as_ref(), converted.is_convert)
Self::new(converted.map.as_ref())
}
}
+12 -14
View File
@@ -30,17 +30,15 @@ const INCOMPATIBLE_MODES: &str = "the gamemodes were incompatible";
/// All other conversions are incompatible.
pub struct Converted<'a, M> {
pub(crate) map: Cow<'a, Beatmap>,
pub(crate) is_convert: bool,
mode: PhantomData<M>,
}
impl<'a, M> Converted<'a, M> {
/// Initialize a [`Converted`] beatmap by promising the given map's mode
/// matches the generic type `M`.
pub(crate) const fn new(map: Cow<'a, Beatmap>, is_convert: bool) -> Self {
pub(crate) const fn new(map: Cow<'a, Beatmap>) -> Self {
Self {
map,
is_convert,
mode: PhantomData,
}
}
@@ -72,8 +70,8 @@ impl<M: IGameMode> Converted<'_, M> {
#[allow(clippy::result_large_err)]
pub fn try_from_owned(mut map: Beatmap) -> Result<Self, Beatmap> {
match M::try_convert(&mut map) {
ConvertStatus::Noop => Ok(Self::new(Cow::Owned(map), false)),
ConvertStatus::Conversion => Ok(Self::new(Cow::Owned(map), true)),
ConvertStatus::Noop => Ok(Self::new(Cow::Owned(map))),
ConvertStatus::Conversion => Ok(Self::new(Cow::Owned(map))),
ConvertStatus::Incompatible => Err(map),
}
}
@@ -106,7 +104,7 @@ impl<'a, M: IGameMode> Converted<'a, M> {
/// shorter.
#[must_use]
pub fn as_owned(&'a self) -> Self {
Self::new(Cow::Borrowed(self.map.as_ref()), self.is_convert)
Self::new(Cow::Borrowed(self.map.as_ref()))
}
/// Create a performance calculator for the map.
@@ -121,14 +119,14 @@ impl<'a, M: IGameMode> Converted<'a, M> {
/// [`&Beatmap`]: Beatmap
pub fn try_from_ref(map: &'a Beatmap) -> Option<Self> {
let mut map = match M::check_convert(map) {
ConvertStatus::Noop => return Some(Self::new(Cow::Borrowed(map), false)),
ConvertStatus::Noop => return Some(Self::new(Cow::Borrowed(map))),
ConvertStatus::Conversion => map.to_owned(),
ConvertStatus::Incompatible => return None,
};
match M::try_convert(&mut map) {
ConvertStatus::Conversion => Some(Self::new(Cow::Owned(map), true)),
ConvertStatus::Noop => Some(Self::new(Cow::Owned(map), false)),
ConvertStatus::Conversion => Some(Self::new(Cow::Owned(map))),
ConvertStatus::Noop => Some(Self::new(Cow::Owned(map))),
ConvertStatus::Incompatible => None,
}
}
@@ -152,8 +150,9 @@ impl<'a, M: IGameMode> Converted<'a, M> {
pub fn try_convert<N: IGameMode>(self) -> Result<Converted<'a, N>, Self> {
match self.map {
Cow::Borrowed(map) => Converted::<N>::try_from_ref(map).ok_or(self),
Cow::Owned(map) => Converted::<N>::try_from_owned(map)
.map_err(|map| Self::new(Cow::Owned(map), self.is_convert)),
Cow::Owned(map) => {
Converted::<N>::try_from_owned(map).map_err(|map| Self::new(Cow::Owned(map)))
}
}
}
@@ -172,7 +171,7 @@ impl<'a, M: IGameMode> Converted<'a, M> {
impl<M> Clone for Converted<'_, M> {
fn clone(&self) -> Self {
Self::new(self.map.clone(), self.is_convert)
Self::new(self.map.clone())
}
}
@@ -203,7 +202,6 @@ impl<M> Debug for Converted<'_, M> {
f.debug_struct("Converted")
.field("map", &self.map)
.field("is_convert", &self.is_convert)
.field("mode", &GenericFormatter::<M>::default())
.finish()
}
@@ -211,6 +209,6 @@ impl<M> Debug for Converted<'_, M> {
impl<M> PartialEq for Converted<'_, M> {
fn eq(&self, other: &Self) -> bool {
self.map == other.map && self.is_convert == other.is_convert
self.map == other.map
}
}
+1
View File
@@ -297,6 +297,7 @@ impl From<BeatmapState> for Beatmap {
Beatmap {
version: state.version,
is_convert: false,
stack_leniency: state.stack_leniency,
mode: state.mode,
ar: approach_rate,
+2
View File
@@ -32,6 +32,7 @@ mod decode;
#[derive(Clone, Debug, PartialEq)]
pub struct Beatmap {
pub version: i32,
pub is_convert: bool,
// General
pub stack_leniency: f32,
@@ -176,6 +177,7 @@ impl Default for Beatmap {
fn default() -> Self {
Self {
version: LATEST_FORMAT_VERSION,
is_convert: false,
stack_leniency: DEFAULT_SLIDER_LENIENCY,
mode: GameMode::default(),
ar: 5.0,