From 10f545ef61550d7ca52c3f1f0b95f2d0cba6e21e Mon Sep 17 00:00:00 2001 From: JacobLinCool Date: Mon, 29 Apr 2024 19:02:24 +0800 Subject: [PATCH] feat(taiko): support shiftjis encoding --- taiko-game/Cargo.toml | 1 + taiko-game/src/app.rs | 3 ++- taiko-game/src/sound.rs | 3 ++- taiko-game/src/utils.rs | 13 +++++++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/taiko-game/Cargo.toml b/taiko-game/Cargo.toml index 83c377d..a08a3ac 100644 --- a/taiko-game/Cargo.toml +++ b/taiko-game/Cargo.toml @@ -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" ]} diff --git a/taiko-game/src/app.rs b/taiko-game/src/app.rs index eb38a1b..887902c 100644 --- a/taiko-game/src/app.rs +++ b/taiko-game/src/app.rs @@ -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)] @@ -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) diff --git a/taiko-game/src/sound.rs b/taiko-game/src/sound.rs index 5b8c445..a31c678 100644 --- a/taiko-game/src/sound.rs +++ b/taiko-game/src/sound.rs @@ -15,7 +15,8 @@ impl SoundData { pub fn load_from_path( file_path: impl AsRef, ) -> Result { - 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) } diff --git a/taiko-game/src/utils.rs b/taiko-game/src/utils.rs index 4a67603..4fb94f1 100644 --- a/taiko-game/src/utils.rs +++ b/taiko-game/src/utils.rs @@ -172,3 +172,16 @@ Config directory: {config_dir_path} Data directory: {data_dir_path}" ) } + +pub fn read_shiftjis_or_utf8>(path: P) -> Result { + 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()) +}