Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
105 changes: 86 additions & 19 deletions phira/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{
ops::DerefMut,
path::Path,
};
use tracing::debug;
use tracing::{debug, warn};

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -99,10 +99,44 @@ pub struct Data {

#[serde(default)]
pub collections: Vec<LocalCollection>,
#[serde(default)]
pub import_scan_retry: HashMap<String, u8>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a need to persist this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to know what path caused the problem when restarting the program next time

}

impl Data {
pub async fn init(&mut self) -> Result<()> {
fn persist_retry_state(data: &Data) {
let res = (|| -> Result<()> {
let root = dir::root().map_err(|e| anyhow::anyhow!("failed to get root directory: {}", e))?;
let path = format!("{}/data.json", root);
std::fs::write(&path, serde_json::to_string(data)?).map_err(|e| anyhow::anyhow!("failed to write to {}: {}", path, e))?;
Ok(())
})();
if let Err(err) = res {
warn!(?err, "failed to persist import scan retry state");
}
}

fn remove_failed_entry(path: &Path, key: &str, retry_map: &mut HashMap<String, u8>) {
let remove_res = if path.is_dir() {
std::fs::remove_dir_all(path)
} else if path.exists() {
std::fs::remove_file(path)
} else {
Ok(())
};
if let Err(err) = remove_res {
warn!(?err, "failed to remove exhausted import entry: {}", key);
}
retry_map.remove(key);
}

fn bump_retry(map: &mut HashMap<String, u8>, key: &str) {
const MAX_RETRIES: u8 = 2;
let entry = map.entry(key.to_owned()).or_default();
*entry = (*entry + 1).min(MAX_RETRIES);
}

let charts = dir::charts()?;
self.charts.retain(|it| Path::new(&format!("{}/{}", charts, it.local_path)).exists());
let occurred: HashSet<_> = self.charts.iter().map(|it| it.local_path.clone()).collect();
Expand All @@ -111,22 +145,38 @@ impl Data {
let filename = entry.file_name();
let filename = filename.to_str().unwrap();
let filename = format!("custom/{filename}");
let path = entry.path();
if occurred.contains(&filename) {
self.import_scan_retry.remove(&filename);
continue;
}
let path = entry.path();
if self.import_scan_retry.get(&filename).copied().unwrap_or_default() >= 2 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bump_entry should be combined into this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what? bump_entry?Do you mean bump_retry ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging the bump_retry method into this code block will result in a complete semantic error in the code. Without bump_retry, this code block can never trigger it

remove_failed_entry(&path, &filename, &mut self.import_scan_retry);
persist_retry_state(self);
warn!("skip startup import scan after retry limit reached: {filename}");
continue;
}
// Persist retry count before parsing so crashes during parsing still consume one retry.
bump_retry(&mut self.import_scan_retry, &filename);
persist_retry_state(self);
let Ok(mut fs) = prpr::fs::fs_from_file(&path) else {
continue;
};
let result = prpr::fs::load_info(fs.deref_mut()).await;
if let Ok(info) = result {
self.charts.push(LocalChart {
info: BriefChartInfo { id: None, ..info.into() },
local_path: filename,
record: None,
mods: Mods::default(),
played_unlock: false,
});
match result {
Ok(info) => {
self.import_scan_retry.remove(&filename);
self.charts.push(LocalChart {
info: BriefChartInfo { id: None, ..info.into() },
local_path: filename,
record: None,
mods: Mods::default(),
played_unlock: false,
});
}
Err(err) => {
warn!(?err, "failed to parse startup custom import candidate: {}", filename);
}
}
}
for entry in std::fs::read_dir(dir::downloaded_charts()?)? {
Expand All @@ -135,22 +185,39 @@ impl Data {
let filename = filename.to_str().unwrap();
let Ok(id): Result<i32, _> = filename.parse() else { continue };
let filename = format!("download/{filename}");
let path = entry.path();
if occurred.contains(&filename) {
self.import_scan_retry.remove(&filename);
continue;
}
let path = entry.path();
if self.import_scan_retry.get(&filename).copied().unwrap_or_default() >= 2 {
remove_failed_entry(&path, &filename, &mut self.import_scan_retry);
persist_retry_state(self);
warn!("skip startup import scan after retry limit reached: {filename}");
continue;
}
// Persist retry count before parsing so crashes during parsing still consume one retry.
bump_retry(&mut self.import_scan_retry, &filename);
persist_retry_state(self);
let Ok(mut fs) = prpr::fs::fs_from_file(&path) else {
warn!("failed to open file system for downloaded chart: {}", filename);
continue;
};
let result = prpr::fs::load_info(fs.deref_mut()).await;
if let Ok(info) = result {
self.charts.push(LocalChart {
info: BriefChartInfo { id: Some(id), ..info.into() },
local_path: filename,
record: None,
mods: Mods::default(),
played_unlock: false,
});
match result {
Ok(info) => {
self.import_scan_retry.remove(&filename);
self.charts.push(LocalChart {
info: BriefChartInfo { id: Some(id), ..info.into() },
local_path: filename,
record: None,
mods: Mods::default(),
played_unlock: false,
});
}
Err(err) => {
warn!(?err, "failed to parse startup downloaded import candidate: {}", filename);
}
}
}
let respacks: HashSet<_> = self.respacks.iter().cloned().collect();
Expand Down
1 change: 1 addition & 0 deletions phira/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ async fn the_main() -> Result<()> {
data.init().await?;
set_data(data);
sync_data();
save_data()?;

let rx = {
let (tx, rx) = mpsc::channel();
Expand Down
10 changes: 4 additions & 6 deletions phira/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,17 @@ pub fn gen_custom_dir() -> Result<(PathBuf, Uuid)> {
}

pub async fn import_chart_to(dir: &Path, id: Uuid, path: String) -> Result<LocalChart> {
let path_str = path.clone();
let path = Path::new(&path_str);
let path = Path::new(&path);
if !path.exists() || !path.is_file() {
bail!("not a file");
}
let dir_obj = prpr::dir::Dir::new(dir)?;
unzip_into(BufReader::new(File::open(path)?), &dir_obj, true)?;
let _ = std::fs::remove_file(path);
let dir = prpr::dir::Dir::new(dir)?;
unzip_into(BufReader::new(File::open(path)?), &dir, true)?;
let local_path = format!("custom/{id}");
let mut fs = fs_from_path(&local_path)?;
let mut info = fs::load_info(fs.as_mut()).await.with_context(|| itl!("info-fail"))?;
fs::fix_info(fs.as_mut(), &mut info).await.with_context(|| itl!("invalid-chart"))?;
dir_obj.create("info.yml")?.write_all(serde_yaml::to_string(&info)?.as_bytes())?;
dir.create("info.yml")?.write_all(serde_yaml::to_string(&info)?.as_bytes())?;
Ok(LocalChart {
info: info.into(),
local_path,
Expand Down
7 changes: 2 additions & 5 deletions phira/src/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use once_cell::sync::Lazy;
use prpr::{
core::ResPackInfo,
ext::{unzip_into, RectExt, SafeTexture},
scene::{return_file, show_error, show_message, take_file, NextScene, Scene, CHOSEN_FILE},
scene::{return_file, show_error, show_message, take_file, NextScene, Scene},
task::Task,
time::TimeManager,
ui::{button_hit, FontArc, RectButton, Ui, UI_AUDIO},
Expand Down Expand Up @@ -333,10 +333,8 @@ impl Scene for MainScene {
match id.as_str() {
"_import" => {
self.import_task = Some(Task::new(import_chart(file)));
*CHOSEN_FILE.lock().unwrap() = (None, None);
}
"_import_respack" => {
*CHOSEN_FILE.lock().unwrap() = (None, None);
let root = dir::respacks()?;
let dir = prpr::dir::Dir::new(&root)?;
let mut dir_id = String::new();
Expand All @@ -348,9 +346,8 @@ impl Scene for MainScene {
dir_id = uuid.to_string();
dir.create_dir_all(&dir_id)?;
let dir = dir.open_dir(&dir_id)?;
unzip_into(BufReader::new(File::open(&file)?), &dir, false).context("failed to unzip")?;
unzip_into(BufReader::new(File::open(file)?), &dir, false).context("failed to unzip")?;
let config: ResPackInfo = serde_yaml::from_reader(dir.open("info.yml").context("missing yml")?)?;
let _ = std::fs::remove_file(file);
get_data_mut().respacks.push(dir_id.clone());
save_data()?;
Ok(ResPackItem::new(Some(format!("{root}/{dir_id}").into()), config.name))
Expand Down
Loading