Removed gxhash and ran cargo fmt

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2024-11-01 07:51:56 +11:00
parent 86363327a3
commit bd39f1fd72
15 changed files with 129 additions and 104 deletions

10
src-tauri/Cargo.lock generated
View File

@ -1021,7 +1021,6 @@ dependencies = [
"ciborium", "ciborium",
"directories", "directories",
"env_logger", "env_logger",
"gxhash",
"hex", "hex",
"http", "http",
"log", "log",
@ -1698,15 +1697,6 @@ dependencies = [
"syn 2.0.79", "syn 2.0.79",
] ]
[[package]]
name = "gxhash"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09f0c897148ec6ff3ca864b7c886df75e6ba09972d206bd9a89af0c18c992253"
dependencies = [
"rand 0.8.5",
]
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.4.6" version = "0.4.6"

View File

@ -45,7 +45,6 @@ tokio = { version = "1.40.0", features = ["rt", "tokio-macros"] }
versions = { version = "6.3.2", features = ["serde"] } versions = { version = "6.3.2", features = ["serde"] }
urlencoding = "2.1.3" urlencoding = "2.1.3"
rustix = "0.38.37" rustix = "0.38.37"
gxhash = "2.3.0"
md5 = "0.7.0" md5 = "0.7.0"
[dependencies.uuid] [dependencies.uuid]

View File

@ -1,37 +1,36 @@
use std::{ use std::{
env, env,
sync::Mutex, time::{SystemTime, UNIX_EPOCH}, sync::Mutex,
time::{SystemTime, UNIX_EPOCH},
}; };
use log::{info, warn}; use log::{info, warn};
use openssl::{ use openssl::{ec::EcKey, hash::MessageDigest, pkey::PKey, sign::Signer};
ec::EcKey,
hash::MessageDigest,
pkey::PKey,
sign::{Signer},
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tauri::{AppHandle, Emitter, Manager}; use tauri::{AppHandle, Emitter, Manager};
use url::Url; use url::Url;
use crate::{db::{DatabaseAuth, DatabaseImpls}, AppState, AppStatus, User, DB}; use crate::{
db::{DatabaseAuth, DatabaseImpls},
AppState, AppStatus, User, DB,
};
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all="camelCase")] #[serde(rename_all = "camelCase")]
struct InitiateRequestBody { struct InitiateRequestBody {
name: String, name: String,
platform: String, platform: String,
} }
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all="camelCase")] #[serde(rename_all = "camelCase")]
struct HandshakeRequestBody { struct HandshakeRequestBody {
client_id: String, client_id: String,
token: String, token: String,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all="camelCase")] #[serde(rename_all = "camelCase")]
struct HandshakeResponse { struct HandshakeResponse {
private: String, private: String,
certificate: String, certificate: String,

View File

@ -7,11 +7,11 @@ use crate::DB;
use log::info; use log::info;
use rustix::fs::{fallocate, FallocateFlags}; use rustix::fs::{fallocate, FallocateFlags};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use urlencoding::encode;
use std::fs::{create_dir_all, File}; use std::fs::{create_dir_all, File};
use std::path::Path; use std::path::Path;
use std::sync::atomic::{AtomicBool, AtomicUsize}; use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use urlencoding::encode;
pub struct GameDownloadAgent { pub struct GameDownloadAgent {
pub id: String, pub id: String,
@ -20,7 +20,7 @@ pub struct GameDownloadAgent {
contexts: Mutex<Vec<DropDownloadContext>>, contexts: Mutex<Vec<DropDownloadContext>>,
progress: ProgressChecker<DropDownloadContext>, progress: ProgressChecker<DropDownloadContext>,
pub manifest: Mutex<Option<DropManifest>>, pub manifest: Mutex<Option<DropManifest>>,
pub callback: Arc<AtomicBool> pub callback: Arc<AtomicBool>,
} }
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)] #[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
pub enum GameDownloadState { pub enum GameDownloadState {
@ -58,7 +58,7 @@ impl GameDownloadAgent {
progress: ProgressChecker::new( progress: ProgressChecker::new(
Box::new(download_logic::download_game_chunk), Box::new(download_logic::download_game_chunk),
Arc::new(AtomicUsize::new(0)), Arc::new(AtomicUsize::new(0)),
callback callback,
), ),
contexts: Mutex::new(Vec::new()), contexts: Mutex::new(Vec::new()),
} }
@ -77,8 +77,7 @@ impl GameDownloadAgent {
// It's not necessary, I just can't figure out to make the borrow checker happy // It's not necessary, I just can't figure out to make the borrow checker happy
{ {
let lock = self.contexts.lock().unwrap().to_vec(); let lock = self.contexts.lock().unwrap().to_vec();
self.progress self.progress.run_context_parallel(lock, max_threads);
.run_context_parallel(lock, max_threads);
} }
Ok(()) Ok(())
} }
@ -97,7 +96,8 @@ impl GameDownloadAgent {
.join( .join(
format!( format!(
"/api/v1/client/metadata/manifest?id={}&version={}", "/api/v1/client/metadata/manifest?id={}&version={}",
self.id, encode(&self.version) self.id,
encode(&self.version)
) )
.as_str(), .as_str(),
) )
@ -164,7 +164,7 @@ impl GameDownloadAgent {
index: i, index: i,
game_id: game_id.to_string(), game_id: game_id.to_string(),
path: path.clone(), path: path.clone(),
checksum: chunk.checksums[i].clone() checksum: chunk.checksums[i].clone(),
}); });
running_offset += *length as u64; running_offset += *length as u64;
} }
@ -183,4 +183,3 @@ impl GameDownloadAgent {
Ok(()) Ok(())
} }
} }

View File

@ -1,4 +1,7 @@
use std::{sync::{atomic::Ordering, Arc, Mutex}, thread}; use std::{
sync::{atomic::Ordering, Arc, Mutex},
thread,
};
use log::info; use log::info;
@ -13,7 +16,10 @@ pub async fn queue_game_download(
state: tauri::State<'_, Mutex<AppState>>, state: tauri::State<'_, Mutex<AppState>>,
) -> Result<(), GameDownloadError> { ) -> Result<(), GameDownloadError> {
info!("Queuing Game Download"); info!("Queuing Game Download");
let download_agent = Arc::new(GameDownloadAgent::new(game_id.clone(), game_version.clone())); let download_agent = Arc::new(GameDownloadAgent::new(
game_id.clone(),
game_version.clone(),
));
download_agent.queue().await?; download_agent.queue().await?;
let mut queue = state.lock().unwrap(); let mut queue = state.lock().unwrap();
@ -30,29 +36,27 @@ pub async fn start_game_downloads(
let lock = state.lock().unwrap(); let lock = state.lock().unwrap();
let mut game_downloads = lock.game_downloads.clone(); let mut game_downloads = lock.game_downloads.clone();
drop(lock); drop(lock);
thread::spawn(move || { thread::spawn(move || loop {
loop { let mut current_id = String::new();
let mut current_id = String::new(); let mut download_agent = None;
let mut download_agent = None; {
{ for (id, agent) in &game_downloads {
for (id, agent) in &game_downloads { if agent.get_state() == GameDownloadState::Queued {
if agent.get_state() == GameDownloadState::Queued { download_agent = Some(agent.clone());
download_agent = Some(agent.clone()); current_id = id.clone();
current_id = id.clone(); info!("Got queued game to download");
info!("Got queued game to download"); break;
break;
}
} }
if download_agent.is_none() {
info!("No more games left to download");
return;
}
};
info!("Downloading game");
{
start_game_download(max_threads, download_agent.unwrap()).unwrap();
game_downloads.remove_entry(&current_id);
} }
if download_agent.is_none() {
info!("No more games left to download");
return;
}
};
info!("Downloading game");
{
start_game_download(max_threads, download_agent.unwrap()).unwrap();
game_downloads.remove_entry(&current_id);
} }
}); });
info!("Spawned download"); info!("Spawned download");
@ -61,11 +65,10 @@ pub async fn start_game_downloads(
pub fn start_game_download( pub fn start_game_download(
max_threads: usize, max_threads: usize,
download_agent: Arc<GameDownloadAgent> download_agent: Arc<GameDownloadAgent>,
) -> Result<(), GameDownloadError> { ) -> Result<(), GameDownloadError> {
info!("Triggered Game Download"); info!("Triggered Game Download");
download_agent.ensure_manifest_exists()?; download_agent.ensure_manifest_exists()?;
let local_manifest = { let local_manifest = {
@ -73,7 +76,13 @@ pub fn start_game_download(
(*manifest).clone().unwrap() (*manifest).clone().unwrap()
}; };
download_agent.generate_job_contexts(&local_manifest, download_agent.version.clone(), download_agent.id.clone()).unwrap(); download_agent
.generate_job_contexts(
&local_manifest,
download_agent.version.clone(),
download_agent.id.clone(),
)
.unwrap();
download_agent.begin_download(max_threads).unwrap(); download_agent.begin_download(max_threads).unwrap();
@ -81,7 +90,10 @@ pub fn start_game_download(
} }
#[tauri::command] #[tauri::command]
pub async fn stop_specific_game_download(state: tauri::State<'_, Mutex<AppState>>, game_id: String) -> Result<(), String> { pub async fn stop_specific_game_download(
state: tauri::State<'_, Mutex<AppState>>,
game_id: String,
) -> Result<(), String> {
info!("called stop_specific_game_download"); info!("called stop_specific_game_download");
let lock = state.lock().unwrap(); let lock = state.lock().unwrap();
let download_agent = lock.game_downloads.get(&game_id).unwrap(); let download_agent = lock.game_downloads.get(&game_id).unwrap();

View File

@ -4,21 +4,28 @@ use crate::downloads::manifest::DropDownloadContext;
use crate::DB; use crate::DB;
use log::info; use log::info;
use md5::{Context, Digest}; use md5::{Context, Digest};
use reqwest::blocking::Response; use std::{
use std::{fs::{File, OpenOptions}, io::{self, Error, ErrorKind, Read, Seek, SeekFrom, Write}, path::PathBuf, sync::{atomic::{AtomicBool, Ordering}, Arc}}; fs::{File, OpenOptions},
io::{self, Error, ErrorKind, Seek, SeekFrom, Write},
path::PathBuf,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
use urlencoding::encode; use urlencoding::encode;
pub struct DropFileWriter { pub struct DropFileWriter {
file: File, file: File,
hasher: Context, hasher: Context,
callback: Arc<AtomicBool> callback: Arc<AtomicBool>,
} }
impl DropFileWriter { impl DropFileWriter {
fn new(path: PathBuf, callback: Arc<AtomicBool>) -> Self { fn new(path: PathBuf, callback: Arc<AtomicBool>) -> Self {
Self { Self {
file: OpenOptions::new().write(true).open(path).unwrap(), file: OpenOptions::new().write(true).open(path).unwrap(),
hasher: Context::new(), hasher: Context::new(),
callback callback,
} }
} }
fn finish(mut self) -> io::Result<Digest> { fn finish(mut self) -> io::Result<Digest> {
@ -30,7 +37,10 @@ impl DropFileWriter {
impl Write for DropFileWriter { impl Write for DropFileWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if self.callback.load(Ordering::Acquire) { if self.callback.load(Ordering::Acquire) {
return Err(Error::new(ErrorKind::ConnectionAborted, "Interrupt command recieved")); return Err(Error::new(
ErrorKind::ConnectionAborted,
"Interrupt command recieved",
));
} }
//info!("Writing data to writer"); //info!("Writing data to writer");
@ -78,8 +88,7 @@ pub fn download_game_chunk(ctx: DropDownloadContext, callback: Arc<AtomicBool>)
let mut file: DropFileWriter = DropFileWriter::new(ctx.path, callback); let mut file: DropFileWriter = DropFileWriter::new(ctx.path, callback);
if ctx.offset != 0 { if ctx.offset != 0 {
file file.seek(SeekFrom::Start(ctx.offset))
.seek(SeekFrom::Start(ctx.offset))
.expect("Failed to seek to file offset"); .expect("Failed to seek to file offset");
} }
@ -90,13 +99,18 @@ pub fn download_game_chunk(ctx: DropDownloadContext, callback: Arc<AtomicBool>)
//copy_to_drop_file_writer(&mut response, &mut file); //copy_to_drop_file_writer(&mut response, &mut file);
match io::copy(&mut response, &mut file) { match io::copy(&mut response, &mut file) {
Ok(_) => {}, Ok(_) => {}
Err(e) => { info!("Copy errored with error {}", e)}, Err(e) => {
info!("Copy errored with error {}", e)
}
} }
let res = hex::encode(file.finish().unwrap().0); let res = hex::encode(file.finish().unwrap().0);
if res != ctx.checksum { if res != ctx.checksum {
info!("Checksum failed. Original: {}, Calculated: {} for {}", ctx.checksum, res, ctx.file_name); info!(
"Checksum failed. Original: {}, Calculated: {} for {}",
ctx.checksum, res, ctx.file_name
);
} }
// stream.flush().unwrap(); // stream.flush().unwrap();

View File

@ -19,5 +19,5 @@ pub struct DropDownloadContext {
pub offset: u64, pub offset: u64,
pub game_id: String, pub game_id: String,
pub path: PathBuf, pub path: PathBuf,
pub checksum: String pub checksum: String,
} }

View File

@ -1,5 +1,5 @@
pub mod download_agent;
pub mod download_commands;
mod download_logic;
mod manifest; mod manifest;
pub mod progress; pub mod progress;
pub mod download_agent;
mod download_logic;
pub mod download_commands;

View File

@ -9,7 +9,7 @@ where
{ {
counter: Arc<AtomicUsize>, counter: Arc<AtomicUsize>,
f: Arc<Box<dyn Fn(T, Arc<AtomicBool>) + Send + Sync + 'static>>, f: Arc<Box<dyn Fn(T, Arc<AtomicBool>) + Send + Sync + 'static>>,
callback: Arc<AtomicBool> callback: Arc<AtomicBool>,
} }
impl<T> ProgressChecker<T> impl<T> ProgressChecker<T>
@ -19,12 +19,12 @@ where
pub fn new( pub fn new(
f: Box<dyn Fn(T, Arc<AtomicBool>) + Send + Sync + 'static>, f: Box<dyn Fn(T, Arc<AtomicBool>) + Send + Sync + 'static>,
counter_reference: Arc<AtomicUsize>, counter_reference: Arc<AtomicUsize>,
callback: Arc<AtomicBool> callback: Arc<AtomicBool>,
) -> Self { ) -> Self {
Self { Self {
f: f.into(), f: f.into(),
counter: counter_reference, counter: counter_reference,
callback callback,
} }
} }
pub fn run_contexts_sequentially(&self, contexts: Vec<T>) { pub fn run_contexts_sequentially(&self, contexts: Vec<T>) {
@ -57,11 +57,13 @@ where
for context in contexts { for context in contexts {
let callback = self.callback.clone(); let callback = self.callback.clone();
let f = self.f.clone(); let f = self.f.clone();
s.spawn(move |_| {info!("Running thread"); f(context, callback)}); s.spawn(move |_| {
info!("Running thread");
f(context, callback)
});
} }
}); });
info!("Concluded scope"); info!("Concluded scope");
} }
pub fn get_progress(&self) -> usize { pub fn get_progress(&self) -> usize {
self.counter.load(Ordering::Relaxed) self.counter.load(Ordering::Relaxed)

View File

@ -1,27 +1,30 @@
mod auth; mod auth;
mod db; mod db;
mod downloads;
mod library; mod library;
mod remote; mod remote;
mod downloads;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use crate::db::DatabaseImpls;
use crate::downloads::download_agent::GameDownloadAgent;
use auth::{auth_initiate, generate_authorization_header, recieve_handshake}; use auth::{auth_initiate, generate_authorization_header, recieve_handshake};
use db::{DatabaseInterface, DATA_ROOT_DIR}; use db::{DatabaseInterface, DATA_ROOT_DIR};
use downloads::download_commands::{queue_game_download, start_game_downloads, stop_specific_game_download}; use downloads::download_commands::{
queue_game_download, start_game_downloads, stop_specific_game_download,
};
use env_logger::Env; use env_logger::Env;
use http::{header::*, response::Builder as ResponseBuilder}; use http::{header::*, response::Builder as ResponseBuilder};
use library::{fetch_game, fetch_library, Game}; use library::{fetch_game, fetch_library, Game};
use log::info; use log::info;
use remote::{gen_drop_url, use_remote}; use remote::{gen_drop_url, use_remote};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{
collections::HashMap, sync::{LazyLock, Mutex}
};
use std::sync::Arc; use std::sync::Arc;
use std::{
collections::HashMap,
sync::{LazyLock, Mutex},
};
use tauri_plugin_deep_link::DeepLinkExt; use tauri_plugin_deep_link::DeepLinkExt;
use crate::db::DatabaseImpls;
use crate::downloads::download_agent::{GameDownloadAgent};
#[derive(Clone, Copy, Serialize)] #[derive(Clone, Copy, Serialize)]
pub enum AppStatus { pub enum AppStatus {
@ -31,7 +34,7 @@ pub enum AppStatus {
SignedInNeedsReauth, SignedInNeedsReauth,
} }
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all="camelCase")] #[serde(rename_all = "camelCase")]
pub struct User { pub struct User {
id: String, id: String,
username: String, username: String,
@ -41,14 +44,14 @@ pub struct User {
} }
#[derive(Clone, Serialize)] #[derive(Clone, Serialize)]
#[serde(rename_all="camelCase")] #[serde(rename_all = "camelCase")]
pub struct AppState { pub struct AppState {
status: AppStatus, status: AppStatus,
user: Option<User>, user: Option<User>,
games: HashMap<String, Game>, games: HashMap<String, Game>,
#[serde(skip_serializing)] #[serde(skip_serializing)]
game_downloads: HashMap<String, Arc<GameDownloadAgent>> game_downloads: HashMap<String, Arc<GameDownloadAgent>>,
} }
#[tauri::command] #[tauri::command]
@ -144,7 +147,9 @@ pub fn run() {
info!("handling drop:// url"); info!("handling drop:// url");
let binding = event.urls(); let binding = event.urls();
let url = binding.first().unwrap(); let url = binding.first().unwrap();
if url.host_str().unwrap() == "handshake" { recieve_handshake(handle.clone(), url.path().to_string()) } if url.host_str().unwrap() == "handshake" {
recieve_handshake(handle.clone(), url.path().to_string())
}
}); });
Ok(()) Ok(())

View File

@ -4,9 +4,9 @@ use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
use tauri::{AppHandle, Manager}; use tauri::{AppHandle, Manager};
use crate::{auth::generate_authorization_header, AppState, DB};
use crate::db::DatabaseImpls;
use crate::db::DatabaseGameStatus; use crate::db::DatabaseGameStatus;
use crate::db::DatabaseImpls;
use crate::{auth::generate_authorization_header, AppState, DB};
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
struct FetchGameStruct { struct FetchGameStruct {
@ -83,7 +83,12 @@ pub fn fetch_game(id: String, app: tauri::AppHandle) -> Result<String, String> {
let data = FetchGameStruct { let data = FetchGameStruct {
game: game.clone(), game: game.clone(),
status: db_handle.games.games_statuses.get(&game.id).unwrap().clone(), status: db_handle
.games
.games_statuses
.get(&game.id)
.unwrap()
.clone(),
}; };
return Ok(json!(data).to_string()); return Ok(json!(data).to_string());

View File

@ -21,7 +21,7 @@ macro_rules! unwrap_or_return {
} }
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all="camelCase")] #[serde(rename_all = "camelCase")]
struct DropHealthcheck { struct DropHealthcheck {
app_name: String, app_name: String,
} }

View File

@ -1,6 +1,6 @@
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize};
use crate::downloads::progress::ProgressChecker; use crate::downloads::progress::ProgressChecker;
use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::sync::Arc;
#[test] #[test]
fn test_progress_sequentially() { fn test_progress_sequentially() {