Skip to content

Commit

Permalink
when opening an archive file, point to a reasonable dir file
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabwhy committed Mar 1, 2024
1 parent 135cad2 commit 0fb04f8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
56 changes: 43 additions & 13 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ async fn load_vpk(state: tauri::State<'_, AppState>, vpk_path: String) -> Result
}
}
fmt => {
if !vpk_path.ends_with("_dir.vpk") {
let vpk_path_buf = PathBuf::from(&vpk_path);
let vpk_file_name = vpk_path_buf
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string();
if !LANG_STRS.iter().any(|lang| vpk_file_name.starts_with(lang)) {
println!("Tried to open archive file: {}", fmt);
return Err(format!("Tried to open archive file, consider opening english{}_dir.vpk instead.", &vpk_file_name.as_str()[..(vpk_file_name.len() - 8)]));
}
}
println!("Unsupported format: {}", fmt);
return Err(format!("Unsupported format: {}", fmt).to_string());
}
Expand Down Expand Up @@ -246,22 +259,39 @@ fn vpk_archive_path(path: &String) -> String {

fn get_vpk_name(path: &String) -> String {
let path = PathBuf::from(path);
path.file_name()
strip_lang(&path)
.unwrap()
.file_name()
.unwrap()
.to_str()
.unwrap()
.replace("english", "")
.replace("french", "")
.replace("german", "")
.replace("italian", "")
.replace("japanese", "")
.replace("korean", "")
.replace("polish", "")
.replace("portugese", "")
.replace("russian", "")
.replace("spanish", "")
.replace("tchinese", "")
.replace("_dir.vpk", "")
.to_string()
}

const LANG_STRS: [&str; 11] = [
"english",
"french",
"german",
"italian",
"japanese",
"korean",
"polish",
"portugese",
"russian",
"spanish",
"tchinese",
];

fn strip_lang(path: &PathBuf) -> Option<PathBuf> {
let file_name = path.file_name()?.to_str()?.to_string();
let stripped_name = PathBuf::from(LANG_STRS.iter().find_map(|lang| {
if file_name.starts_with(lang) {
Some(file_name.replacen(lang, "", 1))
} else {
None
}
})?);
Some(path.parent()?.join(stripped_name))
}

#[tauri::command]
Expand Down
2 changes: 1 addition & 1 deletion src/stores/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const useStore = defineStore('main', () => {
loading.value = false;
loaded.value = false;
hasError.value = true;
error.value = e;
error.value = JSON.parse(e);
}
}

Expand Down

0 comments on commit 0fb04f8

Please sign in to comment.