mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-13 16:22:43 +10:00
@ -7,11 +7,11 @@ use crate::{AppState, 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(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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,42 +36,39 @@ 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(¤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");
|
info!("Spawned download");
|
||||||
return Ok(())
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
@ -92,5 +104,5 @@ pub async fn stop_specific_game_download(state: tauri::State<'_, Mutex<AppState>
|
|||||||
info!("Stopping callback");
|
info!("Stopping callback");
|
||||||
callback.store(true, Ordering::Release);
|
callback.store(true, Ordering::Release);
|
||||||
|
|
||||||
return Ok(())
|
return Ok(());
|
||||||
}
|
}
|
||||||
@ -6,20 +6,29 @@ use gxhash::{gxhash128, GxHasher};
|
|||||||
use log::info;
|
use log::info;
|
||||||
use md5::{Context, Digest};
|
use md5::{Context, Digest};
|
||||||
use reqwest::blocking::Response;
|
use reqwest::blocking::Response;
|
||||||
use std::{fs::{File, OpenOptions}, hash::Hasher, io::{self, BufReader, BufWriter, Error, ErrorKind, Read, Seek, SeekFrom, Write}, path::PathBuf, sync::{atomic::{AtomicBool, Ordering}, Arc}};
|
use std::{
|
||||||
|
fs::{File, OpenOptions},
|
||||||
|
hash::Hasher,
|
||||||
|
io::{self, BufReader, BufWriter, Error, ErrorKind, Read, 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> {
|
||||||
@ -31,7 +40,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");
|
||||||
@ -79,8 +91,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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,13 +102,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();
|
||||||
@ -109,14 +125,15 @@ pub fn copy_to_drop_file_writer(response: &mut Response, writer: &mut DropFileWr
|
|||||||
let mut buf = [0u8; 1024];
|
let mut buf = [0u8; 1024];
|
||||||
response.read(&mut buf).unwrap();
|
response.read(&mut buf).unwrap();
|
||||||
match writer.write_all(&buf) {
|
match writer.write_all(&buf) {
|
||||||
Ok(_) => {},
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => match e.kind() {
|
||||||
match e.kind() {
|
ErrorKind::Interrupted => {
|
||||||
ErrorKind::Interrupted => {
|
info!("Interrupted");
|
||||||
info!("Interrupted");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
_ => {
|
||||||
_ => { println!("{}", e); return;}
|
println!("{}", e);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,5 +21,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,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
|
||||||
@ -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)
|
||||||
|
|||||||
@ -1,28 +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::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::sync::Arc;
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
sync::{LazyLock, Mutex},
|
sync::{LazyLock, Mutex},
|
||||||
};
|
};
|
||||||
use std::sync::Arc;
|
|
||||||
use tauri_plugin_deep_link::DeepLinkExt;
|
use tauri_plugin_deep_link::DeepLinkExt;
|
||||||
use crate::downloads::download_agent::{GameDownloadAgent};
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Serialize)]
|
#[derive(Clone, Copy, Serialize)]
|
||||||
pub enum AppStatus {
|
pub enum AppStatus {
|
||||||
@ -51,7 +53,7 @@ pub struct AppState {
|
|||||||
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]
|
||||||
|
|||||||
@ -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() {
|
||||||
|
|||||||
Reference in New Issue
Block a user