Skip to content

Commit

Permalink
Update frontend for backend changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SonnyX committed May 18, 2022
1 parent cbc9768 commit 1a9f7d9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ embed-resource = "1.6"

[profile.release]
opt-level = 'z'
debug = 2
lto = true
16 changes: 12 additions & 4 deletions backend/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,26 @@ impl Handler {


patcher.set_progress_callback(Box::new(move |progress| {
let report_progress = || -> Result<(), Error> {
let mut old_bytes = 0_f64;
let mut report_progress = || -> Result<(), Error> {
let progress_callback = progress_callback.clone();

let current_bytes : f64 = progress.downloaded_bytes.0.load(Ordering::Relaxed) as f64;

let json = format!(
"{{\"action\": \"{}\",\"hash\": [{},{}],\"download\": [{}.0,{}.0],\"patch\": [{},{}],\"download_speed\": \"{}\"}}",
"{{\"action\": \"{}\",\"hash\": {{\"value\":{}, \"maximum\":{}}},\"download\": {{\"bytes\": {{\"value\":{}.0, \"maximum\":{}.0}}, \"files\": {{\"value\":{}, \"maximum\":{}}} }},\"patch\": {{\"value\":{}, \"ready\": {}, \"maximum\":{}}},\"download_speed\": \"{}\"}}",
progress.get_current_action()?,
progress.processed_instructions.0.load(Ordering::Relaxed),
progress.processed_instructions.1.load(Ordering::Relaxed),
progress.downloaded_bytes.0.load(Ordering::Relaxed),
progress.downloaded_bytes.1.load(Ordering::Relaxed),
progress.downloaded_files.0.load(Ordering::Relaxed),
progress.downloaded_files.1.load(Ordering::Relaxed),
progress.patched_files.0.load(Ordering::Relaxed),
progress.patched_files.1.load(Ordering::Relaxed),
"0 Mb/s"
progress.patched_files.2.load(Ordering::Relaxed),
format!("{}/s", crate::progress::convert((current_bytes - old_bytes)/4_f64))
);
old_bytes = current_bytes;
let me : Value = json.parse().or_else(|e| Err(Error::None(format!("Failed to parse Json, error \"{}\": {}", e, json))))?;
crate::spawn_wrapper::spawn(move || -> Result<(), Error> {progress_callback.call(None, &make_args!(me), None)?; Ok(()) });
Ok(())
Expand Down Expand Up @@ -573,6 +579,8 @@ impl Handler {
}
headers.insert("User-Agent".parse::<download_async::http::header::HeaderName>().unwrap(), format!("RenX-Launcher ({})", VERSION).parse::<download_async::http::header::HeaderValue>().unwrap());
let uri = url.as_string().ok_or_else(|| Error::None(format!("Couldn't parse url as string.")))?.parse::<download_async::http::Uri>().unwrap();

info!("Downloading uri: {}", &uri);
downloader.use_uri(uri);
downloader.allow_http();

Expand Down
19 changes: 19 additions & 0 deletions backend/src/progress.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use async_trait::async_trait;
use sciter::Value;
use crate::error::Error;
use log::error;

pub struct ValueProgress {
progress: std::sync::Arc<std::sync::Mutex<Value>>,
Expand Down Expand Up @@ -53,4 +54,22 @@ impl download_async::Progress for ValueProgress {
Ok(())
});
}
}

/// Convert a raw bytesize into a network speed
pub fn convert(num: f64) -> String {
let negative = if num.is_sign_positive() { "" } else { "-" };
let num = num.abs();
let units = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
if num < 1_f64 {
return format!("{}{} {}", negative, num, "B");
}
let delimiter = 1000_f64;
let exponent = std::cmp::min((num.ln() / delimiter.ln()).floor() as i32, (units.len() - 1) as i32);
let pretty_bytes = format!("{:.2}", num / delimiter.powi(exponent)).parse::<f64>().unwrap_or_else(|error| {
error!("{}:{}:{} has encountered an parsing issue: {}", module_path!(),file!(),line!(), error);
panic!("{}:{}:{} has encountered an parsing issue: {}", module_path!(),file!(),line!(), error)
}) * 1_f64;
let unit = units[exponent as usize];
format!("{}{} {}", negative, pretty_bytes, unit)
}
22 changes: 13 additions & 9 deletions dom/modals/progress-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export class ProgressModal extends Element
hash_progress = "0";
hash_progress_done = "0";
hash_progress_total = "0";
download_files_done = "0";
download_files_total = "0";
download_progress = "0";
download_speed = "0";
patch_progress = "0";
Expand All @@ -23,7 +25,7 @@ export class ProgressModal extends Element
return <div>
<p>Validating files: <span class="green hexpand">{this.hash_progress}%</span>{this.hash_progress_done}/{this.hash_progress_total} files</p>
<div class="downloadBar"><progressbar class="indicator" style={this.get_progressbar_style(this.hash_progress)}></progressbar></div>
<p>Dowloading: <span class="green hexpand">{this.download_progress}%</span>{this.download_speed}</p>
<p>Dowloading: <span class="green hexpand">{this.download_progress}%</span>{this.download_files_done}/{this.download_files_total} files, {this.download_speed}</p>
<div class="downloadBar"><progressbar class="indicator" style={this.get_progressbar_style(this.download_progress)}></progressbar></div>
<p>Applying: <span class="green hexpand">{this.patch_progress}%</span>{this.patch_progress_done}/{this.patch_progress_total} files</p>
<div class="downloadBar"><progressbar class="indicator" style={this.get_progressbar_style(this.patch_progress)}></progressbar></div>
Expand Down Expand Up @@ -59,24 +61,26 @@ export class ProgressModal extends Element
callback(progress_service) {
var progress = progress_service.data;

var download_progress = (progress["download"][1] != 0) ? progress["download"][0] * 100 / progress["download"][1] : 0.0;
var download_progress = (progress.download.bytes.maximum != 0) ? progress.download.bytes.value * 100 / progress.download.bytes.maximum : 0.0;

if (progress["download"][1] != 0 && progress["hash"][1] == 0) {
if (progress.download.bytes.maximum != 0 && progress.hash.maximum == 0) {
var processed_instructions = 100;
} else {
var processed_instructions = (progress["hash"][1] != 0) ? progress["hash"][0] * 100 / progress["hash"][1] : 0;
var processed_instructions = (progress.hash.maximum != 0) ? progress.hash.value * 100 / progress.hash.maximum : 0;
}

this.componentUpdate({
current_state: progress["action"],
hash_progress: processed_instructions,
hash_progress_done: progress["hash"][0],
hash_progress_total: progress["hash"][1],
hash_progress_done: progress.hash.value,
hash_progress_total: progress.hash.maximum,
download_files_done: progress.download.files.value,
download_files_total: progress.download.files.maximum,
download_progress: printf("%.1f", download_progress),
download_speed: progress["download_speed"],
patch_progress: (progress["patch"][1] != 0) ? progress["patch"][0] * 100 / progress["patch"][1] : 0,
patch_progress_done: progress["patch"][0],
patch_progress_total: progress["patch"][1]
patch_progress: (progress.patch.maximum != 0) ? progress.patch.value * 100 / progress.patch.maximum : 0,
patch_progress_done: progress.patch.value,
patch_progress_total: progress.patch.maximum
});
}

Expand Down
8 changes: 4 additions & 4 deletions dom/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export class Progress extends Object {

process_progress(progress) {
if(Object.keys(progress).length == 5) {
var download_progress = (progress["download"][1] != 0) ? progress["download"][0] * 100 / progress["download"][1] : 0.0;
var download_progress = (progress.download.bytes.maximum != 0) ? progress.download.bytes.value * 100 / progress.download.bytes.maximum : 0.0;

if (progress["download"][1] != 0 && progress["hash"][1] == 0) {
if (progress.download.bytes.maximum != 0 && progress.hash.maximum == 0) {
var processed_instructions = 100;
} else {
var processed_instructions = (progress["hash"][1] != 0) ? progress["hash"][0] * 100 / progress["hash"][1] : 0;
var processed_instructions = (progress.hash.maximum != 0) ? progress.hash.value * 100 / progress.hash.maximum : 0;
}
var patch_progress = (progress["patch"][1] != 0) ? progress["patch"][0] * 100 / progress["patch"][1] : 0;
var patch_progress = (progress.patch.maximum != 0) ? progress.patch.value * 100 / progress.patch.maximum : 0;

this.is_in_progress = true;
this.current_action = progress["action"];
Expand Down
2 changes: 1 addition & 1 deletion package-windows.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
cd backend && \
cross update && \
cross build --target=i686-pc-windows-gnu --release && \
cross +nightly build --target=i686-pc-windows-gnu --release && \
cd ..
cp ./backend/target/i686-pc-windows-gnu/release/renegade-x-launcher.exe "./Renegade X Launcher.exe" && \
(rm RenX-Launcher.zip || true) && \
Expand Down

0 comments on commit 1a9f7d9

Please sign in to comment.