1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! Representation and parsing for osu! binary formats: beatmap listing, collections, replays and
//! scores.
//!
//! # A note on strings
//!
//! The osu `.db` file format allows for strings to be absent.
//! This notably happens with the unicode versions of titles and authors.
//! For this reason all of the parsed strings are expressed as `Option<String>` instead of a
//! simple `String`.
//! You can default to an empty string by using `string.unwrap_or_default()`, which does no
//! allocations and is very cheap.
//!
//! # A note on features and replays
//!
//! By default, replay data will be decompressed and parsed, using the `xz2` dependency.
//! To disable this behaviour and remove the dependency on `xz2`, disable the `compression` feature:
//!
//! ```toml
//! osu-db = { version = "*", default-features = false }
//! ```
//!
//! When `compression` is disabled, the
//! [`Replay::replay_data`](replay/struct.Replay.html#structfield.replay_data) field will always be
//! `None`, and will be ignored when writing.
//! In any case, the
//! [`Replay::raw_replay_data`](replay/struct.Replay.html#structfield.raw_replay_data) field is
//! always available.
//!
//! # A note on future-proofness
//!
//! Osu `.db` formats are used internally by osu!, and are not intended to be shared.
//! There does not seem to be any public contract on breaking changes, and breaking changes
//! already occured twice (in 2014 and 2019), so this library might not work with future versions
//! of osu!.
//!
//! It is currently guaranteed to work on osu! `.db` versions up to at least `20211103`.
//! The current implementation might work for a long time, or break tomorrow.

//Because otherwise compiling the large beatmap nom combinator fails
#![recursion_limit = "128"]

use crate::prelude::*;

pub use crate::{collection::CollectionList, listing::Listing, replay::Replay, score::ScoreList};

//Writer generator macro
trait Writable {
    type Args;
    fn wr_args<W: Write>(&self, out: &mut W, args: Self::Args) -> io::Result<()>;
}
trait SimpleWritable
where
    Self: Writable,
{
    fn wr<W: Write>(&self, out: &mut W) -> io::Result<()>;
}
impl<T> SimpleWritable for T
where
    T: Writable<Args = ()>,
{
    fn wr<W: Write>(&self, out: &mut W) -> io::Result<()> {
        self.wr_args(out, ())
    }
}
macro_rules! writer {
    ($type:ty [$this:ident, $out:ident] $code:expr) => {
        writer!($type [$this, $out, _arg: ()] $code);
    };
    ($type:ty [$this:ident, $out:ident, $args:ident : $args_ty:ty] $code:expr) => {
        impl crate::Writable for $type {
            type Args=$args_ty;
            fn wr_args<W: Write>(&self, $out: &mut W, $args: $args_ty) -> io::Result<()> {
                let $this = self;
                let () = $code;
                Ok(())
            }
        }
    };
}

mod prelude {
    pub(crate) use crate::{
        boolean, byte, datetime, double, int, long, opt_string, short, single, Bit, Error, ModSet,
        Mode, PrefixedList, SimpleWritable, Writable,
    };
    pub(crate) use chrono::{DateTime, Duration, TimeZone, Utc};
    pub(crate) use nom::{
        bytes::complete::{tag, take, take_while, take_while1},
        combinator::{cond, map, map_opt, map_res, opt},
        error::{Error as NomError, ErrorKind as NomErrorKind},
        multi::{length_count, length_data, many0},
        Err as NomErr, IResult, Needed,
    };
    #[cfg(feature = "ser-de")]
    pub use serde_derive::{Deserialize, Serialize};
    pub(crate) use std::{
        fmt,
        fs::{self, File},
        io::{self, BufWriter, Write},
        ops,
        path::Path,
    };
    #[cfg(feature = "compression")]
    pub use xz2::stream::Error as LzmaError;
}

pub mod collection;
pub mod listing;
pub mod replay;
pub mod score;

#[derive(Debug)]
pub enum Error {
    /// Only available with the `compression` feature enabled.
    #[cfg(feature = "compression")]
    Compression(LzmaError),
    Io(io::Error),
    ParseError(NomErrorKind),
    ParseIncomplete(Needed),
}
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            #[cfg(feature = "compression")]
            Error::Compression(_err) => f.write_str("failed to compress/decompress replay data"),
            Error::Io(_err) => f.write_str("failed to read osu .db file"),
            Error::ParseError(kind) => {
                write!(f, "failed to parse osu file: {}", kind.description())
            }
            Error::ParseIncomplete(Needed::Size(u)) => write!(
                f,
                "failed to parse osu file: parsing requires {} bytes/chars",
                u
            ),
            Error::ParseIncomplete(Needed::Unknown) => {
                f.write_str("failed to parse osu file: parsing requires more data")
            }
        }
    }
}
impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            #[cfg(feature = "compression")]
            Error::Compression(err) => Some(err as &dyn std::error::Error),
            Error::Io(err) => Some(err as &dyn std::error::Error),
            Error::ParseError(_kind) => None,
            Error::ParseIncomplete(_needed) => None,
        }
    }
}
impl From<io::Error> for Error {
    fn from(err: io::Error) -> Self {
        Error::Io(err)
    }
}
impl From<NomErr<NomError<&[u8]>>> for Error {
    fn from(err: NomErr<NomError<&[u8]>>) -> Self {
        match err {
            NomErr::Incomplete(needed) => Self::ParseIncomplete(needed),
            NomErr::Error(err) | NomErr::Failure(err) => Self::ParseError(err.code),
        }
    }
}

#[cfg(feature = "compression")]
impl From<LzmaError> for Error {
    fn from(err: LzmaError) -> Self {
        Error::Compression(err)
    }
}

trait Bit {
    fn bit(&self, pos: u32) -> bool;
    fn bit_range(&self, pos: ops::Range<u32>) -> Self;
    fn set_bit(&mut self, pos: u32, val: bool);
    fn set_bit_range(&mut self, pos: ops::Range<u32>, val: Self);
}
macro_rules! impl_bit {
    (@ $ty:ty) => {
        impl Bit for $ty {
            fn bit(&self, pos: u32) -> bool {
                (*self & 1 << pos) != 0
            }
            fn bit_range(&self, pos: ops::Range<u32>) -> Self {
                (*self & ((1<<pos.end)-1)) >> pos.start
            }
            fn set_bit(&mut self, pos: u32, val: bool) {
                *self = (*self & !(1<<pos)) | ((val as Self)<<pos);
            }
            fn set_bit_range(&mut self, pos: ops::Range<u32>, val: Self) {
                let mask = ((1<<(pos.end-pos.start))-1) << pos.start;
                *self = (*self & !mask) | ((val<<pos.start)&mask);
            }
        }
    };
    ($($ty:ty),*) => {
        $(
            impl_bit!(@ $ty);
        )*
    }
}
impl_bit!(u8, u16, u32, u64);

//Common fixed-size osu `.db` primitives.
use nom::number::complete::le_f32 as single;
use nom::number::complete::le_f64 as double;
use nom::number::complete::le_u16 as short;
use nom::number::complete::le_u32 as int;
use nom::number::complete::le_u64 as long;
use nom::number::complete::le_u8 as byte;

fn boolean(bytes: &[u8]) -> IResult<&[u8], bool> {
    map(byte, |byte: u8| byte != 0)(bytes)
}

writer!(u8 [this,out] out.write_all(&this.to_le_bytes())?);
writer!(u16 [this,out] out.write_all(&this.to_le_bytes())?);
writer!(u32 [this,out] out.write_all(&this.to_le_bytes())?);
writer!(u64 [this,out] out.write_all(&this.to_le_bytes())?);
writer!(f32 [this,out] this.to_bits().wr(out)?);
writer!(f64 [this,out] this.to_bits().wr(out)?);
writer!(bool [this,out] (if *this {1_u8} else {0_u8}).wr(out)?);

//Writer for a list of items preceded by its length as an int
struct PrefixedList<'a, T>(&'a [T]);
impl<T> Writable for PrefixedList<'_, T>
where
    T: Writable,
    T::Args: Clone,
{
    type Args = T::Args;
    fn wr_args<W: Write>(&self, out: &mut W, args: T::Args) -> io::Result<()> {
        (self.0.len() as u32).wr(out)?;
        for item in self.0 {
            item.wr_args(out, args.clone())?;
        }
        Ok(())
    }
}

/// Get a datetime from an amount of "windows ticks":
/// The amount of 100-nanosecond units since midnight of the date 0001/01/01.
fn windows_ticks_to_datetime(ticks: u64) -> DateTime<Utc> {
    let epoch = Utc.ymd(1, 1, 1).and_hms(0, 0, 0);
    epoch
        + Duration::microseconds((ticks / 10) as i64)
        + Duration::nanoseconds((ticks % 10 * 100) as i64)
}

fn datetime(bytes: &[u8]) -> IResult<&[u8], DateTime<Utc>> {
    map(long, windows_ticks_to_datetime)(bytes)
}

fn datetime_to_windows_ticks(datetime: &DateTime<Utc>) -> u64 {
    let epoch = Utc.ymd(1, 1, 1).and_hms(0, 0, 0);
    let duration = datetime.signed_duration_since(epoch);
    let ticks_since: i64 = (duration * 10).num_microseconds().unwrap_or(0);
    ticks_since.max(0) as u64
}
writer!(DateTime<Utc> [this,out] datetime_to_windows_ticks(this).wr(out)?);

// The variable-length ULEB128 encoding used mainly for string lengths.
fn uleb(bytes: &[u8]) -> IResult<&[u8], usize> {
    let (rem, prelude) = take_while(|b: u8| b.bit(7))(bytes)?;
    let (rem, finalizer) = byte(rem)?;

    let mut out = 0;
    let mut offset = 0;

    for byte in prelude {
        out |= (byte.bit_range(0..7) as usize) << offset;
        offset += 7;
    }

    out |= (finalizer as usize) << offset;

    Ok((rem, out))
}

writer!(usize [this,out] {
    let mut this=*this;
    loop {
        let mut byte=this as u8;
        this>>=7;
        let continues={this!=0};
        byte.set_bit(7, continues);
        byte.wr(out)?;
        if !continues {break}
    }
});

// An optional string.
fn opt_string(bytes: &[u8]) -> IResult<&[u8], Option<String>> {
    let (rem, first_byte) = byte(bytes)?;

    match first_byte {
        0x00 => Ok((rem, None)),
        0x0b => {
            let (rem, len) = uleb(rem)?;
            let (rem, string) = map_res(take(len), std::str::from_utf8)(rem)?;

            Ok((rem, Some(string.to_owned())))
        }
        _ => Err(NomErr::Error(NomError::new(bytes, NomErrorKind::Switch))),
    }
}

writer!(Option<String> [this,out] {
    match this {
        Some(string) => {
            0x0b_u8.wr(out)?;
            string.len().wr(out)?;
            out.write_all(string.as_bytes())?;
        },
        None => 0x00_u8.wr(out)?,
    }
});

/// An osu! gamemode.
#[cfg_attr(feature = "ser-de", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Mode {
    Standard,
    Taiko,
    CatchTheBeat,
    Mania,
}
impl Mode {
    pub fn raw(self) -> u8 {
        self as u8
    }

    pub fn from_raw(raw: u8) -> Option<Mode> {
        use self::Mode::*;
        Some(match raw {
            0 => Standard,
            1 => Taiko,
            2 => CatchTheBeat,
            3 => Mania,
            _ => return None,
        })
    }
}

/// A single osu! mod.
#[cfg_attr(feature = "ser-de", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Mod {
    NoFail,
    Easy,
    TouchDevice,
    Hidden,
    HardRock,
    SuddenDeath,
    DoubleTime,
    Relax,
    HalfTime,
    /// Always goes with `DoubleTime`.
    Nightcore,
    Flashlight,
    Autoplay,
    SpunOut,
    /// Also called "Relax2".
    Autopilot,
    Perfect,
    Key4,
    Key5,
    Key6,
    Key7,
    Key8,
    FadeIn,
    Random,
    /// Cinema.
    LastMod,
    /// Only on osu!cuttingedge it seems.
    TargetPractice,
    Key9,
    Coop,
    Key1,
    Key3,
    Key2,
}
impl Mod {
    /// Each of the 29 mods have a corresponding integer between [0,28], inclusive.
    /// This method retrieves its integer.
    pub fn raw(&self) -> u8 {
        *self as u8
    }

    /// Build a mod from its corresponding integer.
    /// Returns `None` if the integer is out-of-range (>28).
    pub fn from_raw(bit_offset: u8) -> Option<Mod> {
        use self::Mod::*;
        Some(match bit_offset {
            0 => NoFail,
            1 => Easy,
            2 => TouchDevice,
            3 => Hidden,
            4 => HardRock,
            5 => SuddenDeath,
            6 => DoubleTime,
            7 => Relax,
            8 => HalfTime,
            9 => Nightcore,
            10 => Flashlight,
            11 => Autoplay,
            12 => SpunOut,
            13 => Autopilot,
            14 => Perfect,
            15 => Key4,
            16 => Key5,
            17 => Key6,
            18 => Key7,
            19 => Key8,
            20 => FadeIn,
            21 => Random,
            22 => LastMod,
            23 => TargetPractice,
            24 => Key9,
            25 => Coop,
            26 => Key1,
            27 => Key3,
            28 => Key2,
            _ => return None,
        })
    }
}

/// A combination of `Mod`s.
///
/// Very cheap to copy around, as it is a just a wrapped 32-bit integer.
#[cfg_attr(feature = "ser-de", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct ModSet(pub u32);
impl ModSet {
    pub fn bits(&self) -> u32 {
        self.0
    }
    pub fn from_bits(bits: u32) -> ModSet {
        ModSet(bits)
    }

    /// Create a `ModSet` with no mods included.
    pub fn empty() -> ModSet {
        ModSet::from_bits(0)
    }

    /// Check whether the set contains the given mod.
    pub fn contains(&self, m: Mod) -> bool {
        self.bits().bit(m.raw() as u32)
    }

    /// Make a new set of mods with the given mod included or not included.
    pub fn set(&self, m: Mod, include: bool) -> ModSet {
        let mut bits = self.bits();
        bits.set_bit(m.raw() as u32, include);
        ModSet::from_bits(bits)
    }

    /// Make a new set of mods with the given mod included.
    pub fn with(&self, m: Mod) -> ModSet {
        self.set(m, true)
    }

    /// Make a new set of mods with the given mod removed.
    pub fn without(&self, m: Mod) -> ModSet {
        self.set(m, false)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn basic() {
        assert_eq!(
            byte::<_, NomError<&[u8]>>(" ".as_bytes()),
            Ok((&[][..], 32))
        );
        assert_eq!(
            short::<_, NomError<&[u8]>>(&[10, 2][..]),
            Ok((&[][..], 522))
        );
        assert_eq!(
            int::<_, NomError<&[u8]>>(&[10, 10, 0, 0, 3][..]),
            Ok((&[3][..], 2570))
        );
        assert_eq!(
            long::<_, NomError<&[u8]>>(&[0, 0, 1, 0, 2, 0, 3, 0][..]),
            Ok((&[][..], 844_433_520_132_096))
        );
        assert_eq!(
            single::<_, NomError<&[u8]>>(&[0, 0, 0b00100000, 0b00111110, 4][..]),
            Ok((&[4][..], 0.15625))
        );
        assert_eq!(
            double::<_, NomError<&[u8]>>(&[0b00000010, 0, 0, 0, 0, 0, 0b11110000, 0b00111111][..]),
            Ok((&[][..], 1.0000000000000004))
        );
        assert_eq!(boolean(&[34, 4, 0][..]), Ok((&[4, 0][..], true)));
        assert_eq!(
            int::<_, NomError<&[u8]>>(&[3, 5, 4][..]),
            Err(NomErr::Error(NomError::new(
                &[3, 5, 4][..],
                NomErrorKind::Eof
            )))
        );
        assert_eq!(
            boolean(&[][..]),
            Err(NomErr::Error(NomError::new(&[][..], NomErrorKind::Eof)))
        );
        assert_eq!(
            double::<_, NomError<&[u8]>>(&[14, 25, 15, 24, 3][..]),
            Err(NomErr::Error(NomError::new(
                &[14, 25, 15, 24, 3][..],
                NomErrorKind::Eof
            )))
        );
    }

    #[test]
    fn uleb128() {
        assert_eq!(uleb(&[70]), Ok((&[][..], 70)));
        assert_eq!(
            uleb(&[]),
            Err(NomErr::Error(NomError::new(&[][..], NomErrorKind::Eof)))
        );
        assert_eq!(uleb(&[129, 2]), Ok((&[][..], 257)));
        assert_eq!(uleb(&[124, 2]), Ok((&[2][..], 124)));
    }

    #[test]
    fn strings() {
        let long_str = "w".repeat(129);

        assert_eq!(opt_string(b"\x00sf"), Ok((&b"sf"[..], None)));
        assert_eq!(
            opt_string(b"\x0b\x02ghf"),
            Ok((&b"f"[..], Some("gh".to_string())))
        );
        //Invalid string header
        assert!(opt_string(b"\x01ww").is_err());
        //Invalid utf-8
        assert!(opt_string(b"\x0b\x01\xff").is_err());
        //Missing string length
        assert_eq!(
            opt_string(b"\x0b"),
            Err(NomErr::Error(NomError::new(&[][..], NomErrorKind::Eof)))
        );
        //Long strings
        let mut raw = Vec::from(&b"\x0b\x81\x01"[..]);
        raw.extend_from_slice(long_str.as_bytes());
        raw.extend_from_slice(&b"afaf"[..]);
        assert_eq!(opt_string(&raw), Ok((&b"afaf"[..], Some(long_str))));
    }
}