mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-12 07:42:44 +10:00
Removed gxhash and ran cargo fmt
Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
10
src-tauri/Cargo.lock
generated
10
src-tauri/Cargo.lock
generated
@ -1021,7 +1021,6 @@ dependencies = [
|
||||
"ciborium",
|
||||
"directories",
|
||||
"env_logger",
|
||||
"gxhash",
|
||||
"hex",
|
||||
"http",
|
||||
"log",
|
||||
@ -1698,15 +1697,6 @@ dependencies = [
|
||||
"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]]
|
||||
name = "h2"
|
||||
version = "0.4.6"
|
||||
|
||||
@ -45,7 +45,6 @@ tokio = { version = "1.40.0", features = ["rt", "tokio-macros"] }
|
||||
versions = { version = "6.3.2", features = ["serde"] }
|
||||
urlencoding = "2.1.3"
|
||||
rustix = "0.38.37"
|
||||
gxhash = "2.3.0"
|
||||
md5 = "0.7.0"
|
||||
|
||||
[dependencies.uuid]
|
||||
|
||||
@ -1,37 +1,36 @@
|
||||
use std::{
|
||||
env,
|
||||
sync::Mutex, time::{SystemTime, UNIX_EPOCH},
|
||||
sync::Mutex,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
use log::{info, warn};
|
||||
use openssl::{
|
||||
ec::EcKey,
|
||||
hash::MessageDigest,
|
||||
pkey::PKey,
|
||||
sign::{Signer},
|
||||
};
|
||||
use openssl::{ec::EcKey, hash::MessageDigest, pkey::PKey, sign::Signer};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::{AppHandle, Emitter, Manager};
|
||||
use url::Url;
|
||||
|
||||
use crate::{db::{DatabaseAuth, DatabaseImpls}, AppState, AppStatus, User, DB};
|
||||
use crate::{
|
||||
db::{DatabaseAuth, DatabaseImpls},
|
||||
AppState, AppStatus, User, DB,
|
||||
};
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct InitiateRequestBody {
|
||||
name: String,
|
||||
platform: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct HandshakeRequestBody {
|
||||
client_id: String,
|
||||
token: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct HandshakeResponse {
|
||||
private: String,
|
||||
certificate: String,
|
||||
|
||||
@ -86,4 +86,4 @@ impl DatabaseImpls for DatabaseInterface {
|
||||
let handle = self.borrow_data().unwrap();
|
||||
Url::parse(&handle.base_url).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,11 +7,11 @@ use crate::DB;
|
||||
use log::info;
|
||||
use rustix::fs::{fallocate, FallocateFlags};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use urlencoding::encode;
|
||||
use std::fs::{create_dir_all, File};
|
||||
use std::path::Path;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use urlencoding::encode;
|
||||
|
||||
pub struct GameDownloadAgent {
|
||||
pub id: String,
|
||||
@ -20,7 +20,7 @@ pub struct GameDownloadAgent {
|
||||
contexts: Mutex<Vec<DropDownloadContext>>,
|
||||
progress: ProgressChecker<DropDownloadContext>,
|
||||
pub manifest: Mutex<Option<DropManifest>>,
|
||||
pub callback: Arc<AtomicBool>
|
||||
pub callback: Arc<AtomicBool>,
|
||||
}
|
||||
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
|
||||
pub enum GameDownloadState {
|
||||
@ -58,7 +58,7 @@ impl GameDownloadAgent {
|
||||
progress: ProgressChecker::new(
|
||||
Box::new(download_logic::download_game_chunk),
|
||||
Arc::new(AtomicUsize::new(0)),
|
||||
callback
|
||||
callback,
|
||||
),
|
||||
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
|
||||
{
|
||||
let lock = self.contexts.lock().unwrap().to_vec();
|
||||
self.progress
|
||||
.run_context_parallel(lock, max_threads);
|
||||
self.progress.run_context_parallel(lock, max_threads);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -97,7 +96,8 @@ impl GameDownloadAgent {
|
||||
.join(
|
||||
format!(
|
||||
"/api/v1/client/metadata/manifest?id={}&version={}",
|
||||
self.id, encode(&self.version)
|
||||
self.id,
|
||||
encode(&self.version)
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
@ -164,7 +164,7 @@ impl GameDownloadAgent {
|
||||
index: i,
|
||||
game_id: game_id.to_string(),
|
||||
path: path.clone(),
|
||||
checksum: chunk.checksums[i].clone()
|
||||
checksum: chunk.checksums[i].clone(),
|
||||
});
|
||||
running_offset += *length as u64;
|
||||
}
|
||||
@ -183,4 +183,3 @@ impl GameDownloadAgent {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
use std::{sync::{atomic::Ordering, Arc, Mutex}, thread};
|
||||
use std::{
|
||||
sync::{atomic::Ordering, Arc, Mutex},
|
||||
thread,
|
||||
};
|
||||
|
||||
use log::info;
|
||||
|
||||
@ -13,7 +16,10 @@ pub async fn queue_game_download(
|
||||
state: tauri::State<'_, Mutex<AppState>>,
|
||||
) -> Result<(), GameDownloadError> {
|
||||
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?;
|
||||
|
||||
let mut queue = state.lock().unwrap();
|
||||
@ -30,30 +36,28 @@ pub async fn start_game_downloads(
|
||||
let lock = state.lock().unwrap();
|
||||
let mut game_downloads = lock.game_downloads.clone();
|
||||
drop(lock);
|
||||
thread::spawn(move || {
|
||||
loop {
|
||||
let mut current_id = String::new();
|
||||
let mut download_agent = None;
|
||||
{
|
||||
for (id, agent) in &game_downloads {
|
||||
if agent.get_state() == GameDownloadState::Queued {
|
||||
download_agent = Some(agent.clone());
|
||||
current_id = id.clone();
|
||||
info!("Got queued game to download");
|
||||
break;
|
||||
}
|
||||
thread::spawn(move || loop {
|
||||
let mut current_id = String::new();
|
||||
let mut download_agent = None;
|
||||
{
|
||||
for (id, agent) in &game_downloads {
|
||||
if agent.get_state() == GameDownloadState::Queued {
|
||||
download_agent = Some(agent.clone());
|
||||
current_id = id.clone();
|
||||
info!("Got queued game to download");
|
||||
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(¤t_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(¤t_id);
|
||||
}
|
||||
});
|
||||
info!("Spawned download");
|
||||
Ok(())
|
||||
@ -61,11 +65,10 @@ pub async fn start_game_downloads(
|
||||
|
||||
pub fn start_game_download(
|
||||
max_threads: usize,
|
||||
download_agent: Arc<GameDownloadAgent>
|
||||
download_agent: Arc<GameDownloadAgent>,
|
||||
) -> Result<(), GameDownloadError> {
|
||||
info!("Triggered Game Download");
|
||||
|
||||
|
||||
download_agent.ensure_manifest_exists()?;
|
||||
|
||||
let local_manifest = {
|
||||
@ -73,7 +76,13 @@ pub fn start_game_download(
|
||||
(*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();
|
||||
|
||||
@ -81,7 +90,10 @@ pub fn start_game_download(
|
||||
}
|
||||
|
||||
#[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");
|
||||
let lock = state.lock().unwrap();
|
||||
let download_agent = lock.game_downloads.get(&game_id).unwrap();
|
||||
@ -93,4 +105,4 @@ pub async fn stop_specific_game_download(state: tauri::State<'_, Mutex<AppState>
|
||||
callback.store(true, Ordering::Release);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,21 +4,28 @@ use crate::downloads::manifest::DropDownloadContext;
|
||||
use crate::DB;
|
||||
use log::info;
|
||||
use md5::{Context, Digest};
|
||||
use reqwest::blocking::Response;
|
||||
use std::{fs::{File, OpenOptions}, io::{self, Error, ErrorKind, Read, Seek, SeekFrom, Write}, path::PathBuf, sync::{atomic::{AtomicBool, Ordering}, Arc}};
|
||||
use std::{
|
||||
fs::{File, OpenOptions},
|
||||
io::{self, Error, ErrorKind, Seek, SeekFrom, Write},
|
||||
path::PathBuf,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
};
|
||||
use urlencoding::encode;
|
||||
|
||||
pub struct DropFileWriter {
|
||||
file: File,
|
||||
hasher: Context,
|
||||
callback: Arc<AtomicBool>
|
||||
callback: Arc<AtomicBool>,
|
||||
}
|
||||
impl DropFileWriter {
|
||||
fn new(path: PathBuf, callback: Arc<AtomicBool>) -> Self {
|
||||
Self {
|
||||
file: OpenOptions::new().write(true).open(path).unwrap(),
|
||||
hasher: Context::new(),
|
||||
callback
|
||||
callback,
|
||||
}
|
||||
}
|
||||
fn finish(mut self) -> io::Result<Digest> {
|
||||
@ -30,9 +37,12 @@ impl DropFileWriter {
|
||||
impl Write for DropFileWriter {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
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");
|
||||
self.hasher.write_all(buf).unwrap();
|
||||
self.file.write(buf)
|
||||
@ -78,26 +88,30 @@ pub fn download_game_chunk(ctx: DropDownloadContext, callback: Arc<AtomicBool>)
|
||||
let mut file: DropFileWriter = DropFileWriter::new(ctx.path, callback);
|
||||
|
||||
if ctx.offset != 0 {
|
||||
file
|
||||
.seek(SeekFrom::Start(ctx.offset))
|
||||
file.seek(SeekFrom::Start(ctx.offset))
|
||||
.expect("Failed to seek to file offset");
|
||||
}
|
||||
|
||||
// Writing everything to disk directly is probably slightly faster because it balances out the writes,
|
||||
// Writing everything to disk directly is probably slightly faster because it balances out the writes,
|
||||
// but this is better than the performance loss from constantly reading the callbacks
|
||||
|
||||
//let mut writer = BufWriter::with_capacity(1024 * 1024, file);
|
||||
|
||||
//copy_to_drop_file_writer(&mut response, &mut file);
|
||||
match io::copy(&mut response, &mut file) {
|
||||
Ok(_) => {},
|
||||
Err(e) => { info!("Copy errored with error {}", e)},
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
info!("Copy errored with error {}", e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let res = hex::encode(file.finish().unwrap().0);
|
||||
if res != ctx.checksum {
|
||||
info!("Checksum failed. Original: {}, Calculated: {} for {}", ctx.checksum, res, ctx.file_name);
|
||||
if res != ctx.checksum {
|
||||
info!(
|
||||
"Checksum failed. Original: {}, Calculated: {} for {}",
|
||||
ctx.checksum, res, ctx.file_name
|
||||
);
|
||||
}
|
||||
|
||||
// stream.flush().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,5 +19,5 @@ pub struct DropDownloadContext {
|
||||
pub offset: u64,
|
||||
pub game_id: String,
|
||||
pub path: PathBuf,
|
||||
pub checksum: String
|
||||
pub checksum: String,
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
pub mod download_agent;
|
||||
pub mod download_commands;
|
||||
mod download_logic;
|
||||
mod manifest;
|
||||
pub mod progress;
|
||||
pub mod download_agent;
|
||||
mod download_logic;
|
||||
pub mod download_commands;
|
||||
@ -9,7 +9,7 @@ where
|
||||
{
|
||||
counter: Arc<AtomicUsize>,
|
||||
f: Arc<Box<dyn Fn(T, Arc<AtomicBool>) + Send + Sync + 'static>>,
|
||||
callback: Arc<AtomicBool>
|
||||
callback: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl<T> ProgressChecker<T>
|
||||
@ -19,12 +19,12 @@ where
|
||||
pub fn new(
|
||||
f: Box<dyn Fn(T, Arc<AtomicBool>) + Send + Sync + 'static>,
|
||||
counter_reference: Arc<AtomicUsize>,
|
||||
callback: Arc<AtomicBool>
|
||||
callback: Arc<AtomicBool>,
|
||||
) -> Self {
|
||||
Self {
|
||||
f: f.into(),
|
||||
counter: counter_reference,
|
||||
callback
|
||||
callback,
|
||||
}
|
||||
}
|
||||
pub fn run_contexts_sequentially(&self, contexts: Vec<T>) {
|
||||
@ -57,11 +57,13 @@ where
|
||||
for context in contexts {
|
||||
let callback = self.callback.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");
|
||||
|
||||
}
|
||||
pub fn get_progress(&self) -> usize {
|
||||
self.counter.load(Ordering::Relaxed)
|
||||
|
||||
@ -1,27 +1,30 @@
|
||||
mod auth;
|
||||
mod db;
|
||||
mod downloads;
|
||||
mod library;
|
||||
mod remote;
|
||||
mod downloads;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use crate::db::DatabaseImpls;
|
||||
use crate::downloads::download_agent::GameDownloadAgent;
|
||||
use auth::{auth_initiate, generate_authorization_header, recieve_handshake};
|
||||
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 http::{header::*, response::Builder as ResponseBuilder};
|
||||
use library::{fetch_game, fetch_library, Game};
|
||||
use log::info;
|
||||
use remote::{gen_drop_url, use_remote};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
collections::HashMap, sync::{LazyLock, Mutex}
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{LazyLock, Mutex},
|
||||
};
|
||||
use tauri_plugin_deep_link::DeepLinkExt;
|
||||
use crate::db::DatabaseImpls;
|
||||
use crate::downloads::download_agent::{GameDownloadAgent};
|
||||
|
||||
#[derive(Clone, Copy, Serialize)]
|
||||
pub enum AppStatus {
|
||||
@ -31,7 +34,7 @@ pub enum AppStatus {
|
||||
SignedInNeedsReauth,
|
||||
}
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct User {
|
||||
id: String,
|
||||
username: String,
|
||||
@ -41,14 +44,14 @@ pub struct User {
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AppState {
|
||||
status: AppStatus,
|
||||
user: Option<User>,
|
||||
games: HashMap<String, Game>,
|
||||
|
||||
|
||||
#[serde(skip_serializing)]
|
||||
game_downloads: HashMap<String, Arc<GameDownloadAgent>>
|
||||
game_downloads: HashMap<String, Arc<GameDownloadAgent>>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@ -144,7 +147,9 @@ pub fn run() {
|
||||
info!("handling drop:// url");
|
||||
let binding = event.urls();
|
||||
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(())
|
||||
|
||||
@ -4,9 +4,9 @@ use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
use crate::{auth::generate_authorization_header, AppState, DB};
|
||||
use crate::db::DatabaseImpls;
|
||||
use crate::db::DatabaseGameStatus;
|
||||
use crate::db::DatabaseImpls;
|
||||
use crate::{auth::generate_authorization_header, AppState, DB};
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct FetchGameStruct {
|
||||
@ -83,7 +83,12 @@ pub fn fetch_game(id: String, app: tauri::AppHandle) -> Result<String, String> {
|
||||
|
||||
let data = FetchGameStruct {
|
||||
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());
|
||||
|
||||
@ -21,7 +21,7 @@ macro_rules! unwrap_or_return {
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct DropHealthcheck {
|
||||
app_name: String,
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
mod progress_tests;
|
||||
mod progress_tests;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize};
|
||||
use crate::downloads::progress::ProgressChecker;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[test]
|
||||
fn test_progress_sequentially() {
|
||||
@ -20,4 +20,4 @@ fn test_progress_parallel() {
|
||||
|
||||
fn test_fn(int: usize, callback: Arc<AtomicBool>) {
|
||||
println!("{}", int);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user