Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Support Bemuse extensions #93

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lex/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ impl Default for PoorMode {

/// The channel, or lane, where the note will be on.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum Channel {
/// The BGA channel.
BgaBase,
Expand Down Expand Up @@ -257,6 +259,10 @@ pub enum Channel {
SectionLen,
/// For the stop object.
Stop,
/// For the scroll speed change object.
Scroll,
/// For the note spacing change object.
Speed,
}

impl Channel {
Expand All @@ -271,6 +277,8 @@ impl Channel {
"06" => BgaPoor,
"07" => BgaLayer,
"09" => Stop,
"SC" => Scroll,
"SP" => Speed,
player1 if player1.starts_with('1') => Note {
kind: NoteKind::Visible,
is_player1: true,
Expand Down
51 changes: 51 additions & 0 deletions src/lex/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::{command::*, cursor::Cursor, Result};

/// A token of BMS format.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Token<'a> {
/// `#ARTIST [string]`. Defines the artist name of the music.
Artist(&'a str),
Expand Down Expand Up @@ -75,6 +76,15 @@ pub enum Token<'a> {
EndRandom,
/// `#ENDSWITCH`. Closes the random scope. See [Token::Switch].
EndSwitch,
/// `#EXT #XXXYY:...`. Defines the extended message. `XXX` is the track, `YY` is the channel.
ExtendedMessage {
/// The track, or measure, must start from 1. But some player may allow the 0 measure (i.e. Lunatic Rave 2).
track: Track,
/// The channel commonly expresses what the lane be arranged the note to.
channel: Channel,
/// The message to the channel, but not only object ids.
message: &'a str,
},
/// `#BMP[01-ZZ] [0-255],[0-255],[0-255],[0-255] [filename]`. Defines the background image/movie object with the color (alpha, red, green and blue) which will be treated as transparent.
ExBmp(ObjId, Argb, &'a Path),
/// `#EXRANK[01-ZZ] [0-3]`. Defines the judgement level change object.
Expand Down Expand Up @@ -120,12 +130,16 @@ pub enum Token<'a> {
Random(u32),
/// `#RANK [0-3]`. Defines the judgement level.
Rank(JudgeLevel),
/// `#SCROLL[01-ZZ] [f64]`. Defines the scroll speed change object. It changes relative falling speed of notes with keeping BPM. For example, if applying `2.0`, the scroll speed will become double.
Scroll(ObjId, &'a str),
/// `#SETRANDOM [u32]`. Starts a random scope but the integer will be used as the generated random number. It should be used only for tests.
SetRandom(u32),
/// `#SETSWITCH [u32]`. Starts a switch scope but the integer will be used as the generated random number. It should be used only for tests.
SetSwitch(u32),
/// `#SKIP`. Escapes the current switch scope. It is often used in the end of every case scope.
Skip,
/// `#SPEED[01-ZZ] [f64]`. Defines the spacing change object. It changes relative spacing of notes with linear interpolation. For example, if playing score between the objects `1.0` and `2.0`, the spaces of notes will increase at the certain rate until the `2.0` object.
Speed(ObjId, &'a str),
/// `#STAGEFILE [filename]`. Defines the splashscreen image. It should be 640x480.
StageFile(&'a Path),
/// `#STOP[01-ZZ] [0-4294967295]`. Defines the stop object. The scroll will stop the beats of the integer divided by 192. A beat length depends on the current BPM. If there are other objects on same time, the stop object must be evaluated at last.
Expand Down Expand Up @@ -275,6 +289,43 @@ impl<'a> Token<'a> {
.map_err(|_| c.err_expected_token("integer"))?;
Self::Stop(ObjId::from(id, c)?, stop)
}
scroll if scroll.starts_with("#SCROLL") => {
let id = command.trim_start_matches("#SCROLL");
let scroll = c
.next_token()
.ok_or_else(|| c.err_expected_token("scroll factor"))?;
Self::Scroll(ObjId::from(id, c)?, scroll)
}
speed if speed.starts_with("#SPEED") => {
let id = command.trim_start_matches("#SPEED");
let scroll = c
.next_token()
.ok_or_else(|| c.err_expected_token("spacing factor"))?;
Self::Speed(ObjId::from(id, c)?, scroll)
}
ext_message if ext_message.starts_with("#EXT") => {
let message = c
.next_token()
.ok_or_else(|| c.err_expected_token("message definition"))?;
if !(message.starts_with('#')
&& message.chars().nth(6) == Some(':')
&& 8 <= message.len())
{
eprintln!("unknown #EXT format: {:?}", message);
continue;
}

let track = message[1..4]
.parse()
.map_err(|_| c.err_expected_token("[000-999]"))?;
let channel = &message[4..6];
let message = &message[7..];
Self::ExtendedMessage {
track: Track(track),
channel: Channel::from(channel, c)?,
message,
}
}
message
if message.starts_with('#')
&& message.chars().nth(6) == Some(':')
Expand Down
32 changes: 32 additions & 0 deletions src/parse/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ pub struct Header {
pub bmp_files: HashMap<ObjId, Bmp>,
/// The BPMs corresponding to the id of the BPM change object.
pub bpm_changes: HashMap<ObjId, f64>,
/// The scrolling factors corresponding to the id of the scroll speed change object.
pub scrolling_factor_changes: HashMap<ObjId, f64>,
/// The spacing factors corresponding to the id of the spacing change object.
pub spacing_factor_changes: HashMap<ObjId, f64>,
/// The texts corresponding to the id of the text object.
pub texts: HashMap<ObjId, String>,
/// The option messages corresponding to the id of the change option object.
Expand Down Expand Up @@ -211,6 +215,34 @@ impl Header {
Token::PlayLevel(play_level) => self.play_level = Some(play_level),
Token::PoorBga(poor_bga_mode) => self.poor_bga_mode = poor_bga_mode,
Token::Rank(rank) => self.rank = Some(rank),
Token::Scroll(id, factor) => {
let parsed: f64 = factor
.parse()
.map_err(|_| ParseError::BpmParseError(factor.into()))?;
if parsed <= 0.0 || !parsed.is_finite() {
return Err(ParseError::BpmParseError(factor.into()));
}
if self.scrolling_factor_changes.insert(id, parsed).is_some() {
eprintln!(
"duplicated bpm change definition found: {:?} {:?}",
id, factor
);
}
}
Token::Speed(id, factor) => {
let parsed: f64 = factor
.parse()
.map_err(|_| ParseError::BpmParseError(factor.into()))?;
if parsed <= 0.0 || !parsed.is_finite() {
return Err(ParseError::BpmParseError(factor.into()));
}
if self.spacing_factor_changes.insert(id, parsed).is_some() {
eprintln!(
"duplicated bpm change definition found: {:?} {:?}",
id, factor
);
}
}
Token::StageFile(file) => self.stage_file = Some(file.into()),
Token::Stop(id, len) => {
self.stops
Expand Down
Loading
Loading