Skip to content

Commit

Permalink
feat(taiko): support shiftjis encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Apr 29, 2024
1 parent 3ae34a3 commit 10f545e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions taiko-game/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tracing-subscriber = { version = "0.3.17", features = ["env-filter", "serde"] }
rodio = "0.17.3"
cpal = "0.15.3"
anyhow = "1.0.82"
encoding_rs = "0.8.34"

[build-dependencies]
vergen = { version = "8.3.1", features = [ "build", "git", "gitoxide", "cargo" ]}
3 changes: 2 additions & 1 deletion taiko-game/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use tja::{TJACourse, TJAParser, TaikoNote, TaikoNoteType, TaikoNoteVariant, TJA}
use crate::assets::{DON_WAV, KAT_WAV};
use crate::cli::AppArgs;
use crate::sound::{SoundData, SoundPlayer};
use crate::utils::read_shiftjis_or_utf8;
use crate::{action::Action, sound, tui};

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -117,7 +118,7 @@ impl App {

async fn enter_course_menu(&mut self) -> Result<()> {
let selected = self.song_selector.selected().unwrap_or(0);
let content = fs::read_to_string(&self.songs[selected].1).unwrap();
let content = read_shiftjis_or_utf8(&self.songs[selected].1).unwrap();
let parser = TJAParser::new();
let mut song = parser
.parse(content)
Expand Down
3 changes: 2 additions & 1 deletion taiko-game/src/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ impl SoundData {
pub fn load_from_path(
file_path: impl AsRef<Path>,
) -> Result<Self, rodio::decoder::DecoderError> {
let file = File::open(file_path).expect("Failed to open file");
let file = File::open(&file_path)
.expect(format!("Failed to open file: {:?}", file_path.as_ref()).as_str());
Self::load_from_file(file)
}

Expand Down
13 changes: 13 additions & 0 deletions taiko-game/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,16 @@ Config directory: {config_dir_path}
Data directory: {data_dir_path}"
)
}

pub fn read_shiftjis_or_utf8<P: AsRef<std::path::Path>>(path: P) -> Result<String> {
let path = path.as_ref();
let bytes = std::fs::read(path)?;
let encoding = if !encoding_rs::SHIFT_JIS.decode_without_bom_handling(&bytes).1 {
encoding_rs::SHIFT_JIS
} else {
encoding_rs::UTF_8
};

let (cow, _, _) = encoding.decode(&bytes);
Ok(cow.into_owned())
}

0 comments on commit 10f545e

Please sign in to comment.