From 00456f501f9bf51be510bb8ee4e43655788c0943 Mon Sep 17 00:00:00 2001 From: liquidhelium Date: Sat, 4 May 2024 11:17:35 +0800 Subject: [PATCH] fix: chapter offset adjusting (#243) --- phira/src/charts_view.rs | 3 ++- phira/src/page.rs | 9 +++++++++ phira/src/scene/chapter.rs | 19 +++++++++++++++++-- phira/src/scene/song.rs | 30 ++++++++++++++++++++++-------- 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/phira/src/charts_view.rs b/phira/src/charts_view.rs index f5f8d8f3..c92e804a 100644 --- a/phira/src/charts_view.rs +++ b/phira/src/charts_view.rs @@ -2,7 +2,7 @@ use crate::{ client::Chart, dir, get_data, get_data_mut, icons::Icons, - page::{ChartItem, Fader, Illustration}, + page::{ChartItem, ChartType, Fader, Illustration}, save_data, scene::{render_release_to_refresh, SongScene, MP_PANEL}, }; @@ -67,6 +67,7 @@ impl ChartDisplayItem { } }, local_path: None, + chart_type: ChartType::Downloaded, }), if chart.stable_request { Some('+') diff --git a/phira/src/page.rs b/phira/src/page.rs index 4e4d06ec..7b5166a9 100644 --- a/phira/src/page.rs +++ b/phira/src/page.rs @@ -100,6 +100,7 @@ pub fn load_local(order: &(ChartOrder, bool)) -> Vec { info: it.info.clone(), local_path: Some(it.local_path.clone()), illu: local_illustration(it.local_path.clone(), tex.clone(), false), + chart_type: ChartType::Imported, }) .collect(); order.0.apply(&mut res); @@ -191,6 +192,14 @@ pub struct ChartItem { pub info: BriefChartInfo, pub local_path: Option, pub illu: Illustration, + pub chart_type: ChartType +} + +#[derive(Clone, Copy)] +pub enum ChartType { + Downloaded, + Imported, + Integrated, } // srange name, isn't it? diff --git a/phira/src/scene/chapter.rs b/phira/src/scene/chapter.rs index 9f1172ee..3170ddd6 100644 --- a/phira/src/scene/chapter.rs +++ b/phira/src/scene/chapter.rs @@ -3,9 +3,10 @@ prpr::tl_file!("chapter"); use crate::{ anim::Anim, data::BriefChartInfo, + dir, icons::Icons, load_res_tex, - page::{ChartItem, Illustration, SFader}, + page::{ChartItem, ChartType, Illustration, SFader}, resource::rtl, }; use anyhow::Result; @@ -202,8 +203,15 @@ impl Scene for ChapterScene { }, illu: Illustration::from_done(chart.illu.clone()), local_path: Some(local_path.clone()), + chart_type: ChartType::Integrated, }; let info = &item.info; + let dir = format!("{}/{}", dir::charts()?, item.local_path.as_ref().unwrap().replace(':', "_")); + let path = std::path::Path::new(&dir); + if !path.exists() { + std::fs::create_dir_all(path)?; + } + let dir = prpr::dir::Dir::new(dir)?; *ASSET_CHART_INFO.lock().unwrap() = Some(ChartInfo { id: None, uploader: None, @@ -226,7 +234,14 @@ impl Scene for ChapterScene { aspect_ratio: 16. / 9., background_dim: 0.6, line_length: 6., - offset: 0., + offset: dir + .read("offset") + .map(|d| f32::from_be_bytes(d.get(0..4).map(|first4| { + let mut result = <[u8;4]>::default(); + result.copy_from_slice(first4); + result + }).unwrap_or_default())) + .unwrap_or_default(), tip: None, tags: Vec::new(), diff --git a/phira/src/scene/song.rs b/phira/src/scene/song.rs index cad2fcf4..ea844cb8 100644 --- a/phira/src/scene/song.rs +++ b/phira/src/scene/song.rs @@ -7,7 +7,7 @@ use crate::{ data::{BriefChartInfo, LocalChart}, dir, get_data, get_data_mut, icons::Icons, - page::{local_illustration, thumbnail_path, ChartItem, Fader, Illustration, SFader}, + page::{local_illustration, thumbnail_path, ChartItem, ChartType, Fader, Illustration, SFader}, popup::Popup, rate::RateDialog, save_data, @@ -313,6 +313,7 @@ pub struct SongScene { update_cksum_passed: Option, update_cksum_task: Option>>, + chart_type: ChartType, } impl SongScene { @@ -483,6 +484,7 @@ impl SongScene { update_cksum_passed: None, update_cksum_task: None, + chart_type: chart.chart_type, } } @@ -1279,13 +1281,25 @@ impl Scene for SongScene { let _res = match res.downcast::>() { Ok(offset) => { if let Some(offset) = *offset { - let dir = prpr::dir::Dir::new(format!("{}/{}", dir::charts()?, self.local_path.as_ref().unwrap()))?; - let mut info: ChartInfo = serde_yaml::from_reader(&dir.open("info.yml")?)?; - info.offset = offset; - dir.create("info.yml")?.write_all(serde_yaml::to_string(&info)?.as_bytes())?; - let path = thumbnail_path(self.local_path.as_ref().unwrap())?; - if path.exists() { - std::fs::remove_file(path)?; + let dir = format!("{}/{}", dir::charts()?, self.local_path.as_ref().unwrap().replace(':', "_")); + let path = std::path::Path::new(&dir); + if !path.exists() { + std::fs::create_dir_all(path)?; + } + let dir = prpr::dir::Dir::new(dir)?; + match self.chart_type { + ChartType::Integrated => { + dir.create("offset")?.write_all(&offset.to_be_bytes())?; + } + _ => { + let mut info: ChartInfo = serde_yaml::from_reader(&dir.open("info.yml")?)?; + info.offset = offset; + dir.create("info.yml")?.write_all(serde_yaml::to_string(&info)?.as_bytes())?; + let path = thumbnail_path(self.local_path.as_ref().unwrap())?; + if path.exists() { + std::fs::remove_file(path)?; + } + } } show_message(tl!("edit-saved")).ok(); }