mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-01-09 04:27:53 +08:00
solution for loose file mods
This commit is contained in:
parent
412acdd317
commit
0551f3e6a0
@ -3,7 +3,13 @@ use std::path;
|
||||
use std::thread;
|
||||
|
||||
#[tauri::command]
|
||||
pub fn unzip(window: tauri::Window, zipfile: String, destpath: String, top_level: Option<bool>) {
|
||||
pub fn unzip(
|
||||
window: tauri::Window,
|
||||
zipfile: String,
|
||||
destpath: String,
|
||||
top_level: Option<bool>,
|
||||
folder_if_loose: Option<bool>,
|
||||
) {
|
||||
// Read file TODO: replace test file
|
||||
let f = match File::open(&zipfile) {
|
||||
Ok(f) => f,
|
||||
@ -27,10 +33,33 @@ pub fn unzip(window: tauri::Window, zipfile: String, destpath: String, top_level
|
||||
|
||||
// Run extraction in seperate thread
|
||||
thread::spawn(move || {
|
||||
let full_path = &write_path;
|
||||
let mut full_path = write_path.clone();
|
||||
|
||||
window.emit("extract_start", &zipfile).unwrap();
|
||||
|
||||
if folder_if_loose.unwrap_or(false) {
|
||||
// Create a new folder with the same name as the zip file
|
||||
let mut file_name = path::Path::new(&zipfile)
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap();
|
||||
|
||||
// remove ".zip" from the end of the file name
|
||||
file_name = &file_name[..file_name.len() - 4];
|
||||
|
||||
let new_path = full_path.join(file_name);
|
||||
match std::fs::create_dir_all(&new_path) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("Failed to create directory: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
full_path = new_path.clone();
|
||||
}
|
||||
|
||||
match zip_extract::extract(&f, &full_path, top_level.unwrap_or(false)) {
|
||||
Ok(_) => {
|
||||
println!(
|
||||
|
@ -65,7 +65,7 @@ export class Mods extends React.Component<IProps, IState> {
|
||||
const firstLink = dlLinks[0].downloadUrl
|
||||
|
||||
this.props.downloadHandler.addDownload(firstLink, modPath, async () => {
|
||||
const unzipRes = await unzip(modPath, modFolder, false)
|
||||
const unzipRes = await unzip(modPath, modFolder, false, true)
|
||||
|
||||
// Write a modinfo.json file
|
||||
invoke('write_file', {
|
||||
|
@ -6,12 +6,18 @@ interface UnzipPayload {
|
||||
new_folder: string
|
||||
}
|
||||
|
||||
export function unzip(file: string, dest: string, topLevelStrip?: boolean): Promise<UnzipPayload> {
|
||||
export function unzip(
|
||||
file: string,
|
||||
dest: string,
|
||||
topLevelStrip?: boolean,
|
||||
folderIfLoose?: boolean
|
||||
): Promise<UnzipPayload> {
|
||||
return new Promise((resolve) => {
|
||||
invoke('unzip', {
|
||||
zipfile: file,
|
||||
destpath: dest,
|
||||
topLevelStrip,
|
||||
folderIfLoose,
|
||||
})
|
||||
|
||||
listen('extract_end', ({ payload }) => {
|
||||
|
Loading…
Reference in New Issue
Block a user