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

fix: #362 #365

Merged
merged 1 commit into from
Aug 9, 2024
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
45 changes: 36 additions & 9 deletions phira/src/page/respack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use prpr::{
ui::{DRectButton, Dialog, Scroll, Ui},
};
use sasa::{AudioManager, PlaySfxParams, Sfx};
use serde_yaml::Error;
use std::{
fs::File,
path::{Path, PathBuf},
Expand Down Expand Up @@ -84,15 +85,41 @@ impl ResPackPage {
MainScene::take_imported_respack();
let dir = dir::respacks()?;
let mut items = vec![ResPackItem::new(None, tl!("default").into_owned())];
for path in &get_data().respacks {
let p = format!("{dir}/{path}");
let p = Path::new(&p);
if !p.is_dir() {
continue;
}
let info: ResPackInfo = serde_yaml::from_reader(File::open(p.join("info.yml"))?)?;
items.push(ResPackItem::new(Some(p.to_owned()), info.name));
}
let data = get_data_mut();
data.respacks = data
.respacks
.clone()
.into_iter()
.filter(|path| -> bool {
let p = format!("{dir}/{path}");
let p = Path::new(&p);
if !p.is_dir() {
return false;
}
let cfg = File::open(p.join("info.yml"));
match cfg {
Err(_) => {
let _ = std::fs::remove_dir_all(p);
false
}
Ok(cfg) => {
let info: Result<ResPackInfo, Error> = serde_yaml::from_reader(cfg);
match info {
Err(_) => {
let _ = std::fs::remove_dir_all(p);
false
}
Ok(info) => {
items.push(ResPackItem::new(Some(p.to_owned()), info.name));
true
}
}
}
}
})
.collect();
save_data()?;

let index = get_data().respack_id;
items[index].load();
let delete_btn = DRectButton::new().with_delta(-0.004).with_elevation(0.);
Expand Down
22 changes: 12 additions & 10 deletions phira/src/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,24 +335,26 @@ impl Scene for MainScene {
self.import_task = Some(Task::new(import_chart(file)));
}
"_import_respack" => {
let root = dir::respacks()?;
let dir = prpr::dir::Dir::new(&root)?;
let mut dir_id = String::new();
let item: Result<ResPackItem> = (|| {
let root = dir::respacks()?;
let dir = prpr::dir::Dir::new(&root)?;
let mut id = Uuid::new_v4();
while dir.exists(id.to_string())? {
id = Uuid::new_v4();
let mut uuid = Uuid::new_v4();
while dir.exists(uuid.to_string())? {
uuid = Uuid::new_v4();
}
let id = id.to_string();
dir.create_dir_all(&id)?;
let dir = dir.open_dir(&id)?;
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")?;
let config: ResPackInfo = serde_yaml::from_reader(dir.open("info.yml").context("missing yml")?)?;
get_data_mut().respacks.push(id.clone());
get_data_mut().respacks.push(dir_id.clone());
save_data()?;
Ok(ResPackItem::new(Some(format!("{root}/{id}").into()), config.name))
Ok(ResPackItem::new(Some(format!("{root}/{dir_id}").into()), config.name))
})();
match item {
Err(err) => {
dir.remove_dir_all(&dir_id)?;
show_error(err.context(itl!("import-respack-failed")));
}
Ok(item) => {
Expand Down
6 changes: 6 additions & 0 deletions prpr/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ impl Dir {
Ok(())
}

#[inline]
pub fn remove_dir_all(&self, p: impl AsRef<Path>) -> Result<()> {
std::fs::remove_dir_all(self.join(p)?)?;
Ok(())
}

#[inline]
pub fn open_dir(&self, p: impl AsRef<Path>) -> Result<Self> {
Self::new(self.join(p)?)
Expand Down