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

feat: Add button to DevView to calculate checksums #446

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 41 additions & 3 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ semver = "1.0"
# simplified filesystem access
glob = "0.3.1"
dirs = "5"
# Walk directories
walkdir = "2.3"
# Crypto stuff like checksum calculation
crypto-hash = "0.3"

[target.'cfg(windows)'.dependencies]
# Windows API stuff
Expand Down
78 changes: 78 additions & 0 deletions src-tauri/src/development/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::github::{
pull_requests::{check_github_api, download_zip_into_memory, get_launcher_download_link},
CommitInfo,
};
use crate::GameInstall;
use serde::{Deserialize, Serialize};
use std::io::Read;

#[tauri::command]
pub async fn install_git_main(game_install_path: &str) -> Result<String, String> {
Expand Down Expand Up @@ -82,3 +85,78 @@ pub async fn install_git_main(game_install_path: &str) -> Result<String, String>
);
Ok(latest_commit_sha)
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Checksum {
path: String,
checksum: String,
}

impl ToString for Checksum {
fn to_string(&self) -> String {
format!("{} {}", self.checksum, self.path)
}
}

/// Computes the checksum of a given file
fn compute_checksum<P: AsRef<std::path::Path>>(
path: P,
) -> Result<Checksum, Box<dyn std::error::Error>> {
dbg!(path.as_ref().to_string_lossy().into_owned());
let mut file = std::fs::File::open(&path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;

let checksum = crypto_hash::hex_digest(crypto_hash::Algorithm::SHA256, &buffer);
Ok(Checksum {
path: path.as_ref().to_string_lossy().into_owned(),
checksum,
})
}

fn convert_to_string(checksums: Vec<Checksum>) -> String {
let mut result = String::new();

for entry in checksums {
result.push_str(&entry.to_string());
result.push('\n');
}
result
}

#[tauri::command]
/// Calculates checksums over the passed game_install folder and returns results
pub async fn calculate_checksums_gameinstall(game_install: GameInstall) -> Result<String, String> {
log::info!("Computing checksums");

let path = game_install.game_path;
let mut checksums = Vec::new();

// Iterate over folder
for entry in walkdir::WalkDir::new(path.clone()) {
let entry = entry.unwrap();
if !entry.file_type().is_file() {
continue;
}

match compute_checksum(entry.path()) {
Ok(mut checksum) => {
checksum.path = checksum
.path
.strip_prefix(&path.clone())
.unwrap()
.to_string();
checksums.push(checksum)
}
Err(err) => log::warn!("Failed to compute checksum for {:?}: {:?}", entry, err),
}
}

for checksum in &checksums {
println!("{:?}", checksum);
}

log::info!("Done calculating");
let s = convert_to_string(checksums);
Ok(s)
}
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ fn main() {
github::pull_requests::get_launcher_download_link,
close_application,
development::install_git_main,
development::calculate_checksums_gameinstall,
get_available_northstar_versions,
])
.run(tauri::generate_context!())
Expand Down
53 changes: 53 additions & 0 deletions src-vue/src/views/DeveloperView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@
<br />
<br />

<el-button type="primary" @click="computeChecksumsGameInstallFolder">
Compute checksums
</el-button>
<el-button type="primary" @click="copyToClipboard">
Copy to clipboard
</el-button>


<el-input
v-model="checksum_results"
type="textarea"
:rows="1"
placeholder="Output"
/>

<br />
<br />

<el-button type="primary" @click="getAvailableNorthstarVersions">
Get available versions
</el-button>
Expand Down Expand Up @@ -147,6 +165,7 @@ export default defineComponent({
return {
mod_to_install_field_string: "",
release_notes_text: "",
checksum_results: "",
first_tag: { label: '', value: { name: '' } },
second_tag: { label: '', value: { name: '' } },
ns_release_tags: [] as TagWrapper[],
Expand Down Expand Up @@ -322,6 +341,40 @@ export default defineComponent({
.then((message) => { showNotification(`NSProton Version`, message as string); })
.catch((error) => { showNotification(`Error`, error, "error"); })
},
async computeChecksumsGameInstallFolder() {
// Send notification telling the user to wait for the process to finish
const notification = showNotification(
"Calculating checksums",
"Please wait",
'info',
0
);

let checksum_calc_result = invoke<string>("calculate_checksums_gameinstall", { gameInstall: this.$store.state.game_install });
await checksum_calc_result
.then((message) => {
// Send notification
this.checksum_results = message;
showNotification("Done calculating checksums");
})
.catch((error) => {
showErrorNotification(error);
console.error(error);
})
.finally(() => {
// Clear old notification
notification.close();
});
},
async copyToClipboard() {
navigator.clipboard.writeText(this.checksum_results)
.then(() => {
showNotification("Copied to clipboard");
})
.catch(() => {
showErrorNotification("Failed copying to clipboard");
});
}
}
});
</script>
Expand Down