Skip to content

Commit

Permalink
Fix bugs with newsfeed & core files & instance files & add progress b…
Browse files Browse the repository at this point in the history
…ar for firmware downloads (#255)

* Fix bugs with newsfeed & core files & instance files & add progress bar for firmware downloads

* Fix the btoa issue I saw & bump the version

* Add error message when firmware download fails

* node version bump in workflow

* Regen lock file to fix SHA issue

* Updates things, pins to a particular threejs version that seems to work with postprocessing
  • Loading branch information
neil-morrison44 authored Mar 4, 2024
1 parent 1a62f9e commit 61f45c6
Show file tree
Hide file tree
Showing 20 changed files with 2,379 additions and 8,055 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test_rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: setup node
uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
- name: install Rust stable
uses: actions-rs/toolchain@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_tauri.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: setup node
uses: actions/setup-node@v4
with:
node-version: 16
node-version: 20
- name: install Rust stable
uses: actions-rs/toolchain@v1
with:
Expand Down
10,076 changes: 2,202 additions & 7,874 deletions package-lock.json

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pocket-sync",
"private": true,
"version": "4.6.0",
"version": "4.6.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand All @@ -14,48 +14,48 @@
"dependencies": {
"@neil-morrison44/gameboy-emulator": "^1.3.0",
"@prantlf/jsonlint": "11.7.0",
"@react-three/drei": "^9.90.0",
"@react-three/fiber": "^8.15.12",
"@react-three/postprocessing": "^2.15.11",
"@tauri-apps/api": "^1.5.1",
"@types/three": "^0.159.0",
"@zip.js/zip.js": "^2.7.32",
"date-fns": "^2.30.0",
"@react-three/drei": "^9.99.7",
"@react-three/fiber": "^8.15.19",
"@react-three/postprocessing": "^2.16.0",
"@tauri-apps/api": "^1.5.3",
"@types/three": "0.160.0",
"@zip.js/zip.js": "^2.7.37",
"date-fns": "^3.3.1",
"fast-fuzzy": "^1.12.0",
"html-react-parser": "^5.0.11",
"i18next": "^23.7.8",
"html-react-parser": "^5.1.8",
"i18next": "^23.10.0",
"i18next-icu": "^2.3.0",
"postprocessing": "^6.33.4",
"postprocessing": "^6.35.0",
"r3f-perf": "^7.1.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^13.5.0",
"react-i18next": "^14.0.5",
"react-markdown": "^9.0.1",
"recoil": "^0.7.7",
"remark-gfm": "^4.0.0",
"tauri-plugin-log-api": "https://github.com/tauri-apps/tauri-plugin-log/archive/refs/heads/v1.tar.gz",
"three": "^0.159.0"
"three": "0.160.0"
},
"devDependencies": {
"@tauri-apps/cli": "^1.5.7",
"@tauri-apps/cli": "^1.5.10",
"@types/jsonlint": "^1.6.3",
"@types/node": "^20.10.3",
"@types/react": "^18.2.42",
"@types/react-dom": "^18.2.17",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"@types/node": "^20.11.24",
"@types/react": "^18.2.62",
"@types/react-dom": "^18.2.19",
"@typescript-eslint/eslint-plugin": "^7.1.1",
"@typescript-eslint/parser": "^7.1.1",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.55.0",
"eslint": "^8.57.0",
"eslint-plugin-i18n-json": "^4.0.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react": "^7.34.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-unused-imports": "^3.0.0",
"postcss-nesting": "^12.0.1",
"stylelint": "^15.11.0",
"stylelint-config-standard": "^34.0.0",
"eslint-plugin-unused-imports": "^3.1.0",
"postcss-nesting": "^12.0.4",
"stylelint": "^16.2.1",
"stylelint-config-standard": "^36.0.0",
"typescript": "^5.3.3",
"vite": "^5.0.6",
"vite-plugin-i18next-loader": "^2.0.4"
"vite": "^5.1.5",
"vite-plugin-i18next-loader": "^2.0.12"
},
"prettier": {
"trailingComma": "es5",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

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

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pocket-sync"
version = "4.6.0"
version = "4.6.1"
description = "A GUI tool for doing stuff with the Analogue Pocket"
authors = ["neil-morrison44"]
license = "AGPL-3.0"
Expand Down
103 changes: 26 additions & 77 deletions src-tauri/src/checks.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
use log::info;
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use notify_debouncer_full::{new_debouncer, DebouncedEvent};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, time::Duration};
use tauri::Window;
use tokio::sync::mpsc;

#[derive(Serialize, Deserialize, Clone)]
struct FSEventPayload {
events: Vec<notify::Event>,
pocket_path: String,
}

enum DebouncedOrRoot {
Root(Result<Event, notify::Error>),
Debounced(Result<Vec<DebouncedEvent>, Vec<notify::Error>>),
PolledDisconnect,
}

pub fn check_if_folder_looks_like_pocket(path: &PathBuf) -> bool {
let json_path = path.join("Analogue_Pocket.json");

Expand All @@ -37,91 +29,48 @@ pub fn check_if_folder_looks_like_pocket(path: &PathBuf) -> bool {
}

pub async fn connection_task(window: Window, pocket_path: PathBuf) -> () {
let (tx, mut rx) = mpsc::channel(10);
info!(
"Watching files and folders at {}....",
&pocket_path.display()
);

let root_tx = tx.clone();
let mut debouncer = new_debouncer(Duration::from_millis(100), None, move |res| {
root_tx.try_send(DebouncedOrRoot::Debounced(res)).unwrap();
})
.unwrap();

debouncer
.watcher()
.watch(&pocket_path, RecursiveMode::Recursive)
.unwrap();

// This causes a deadlock on Windows for some reason
// debouncer
// .cache()
// .add_root(&pocket_path, RecursiveMode::Recursive);

let files_tx = tx.clone();
let main_window = window.clone();
let pocket_path_string = String::from(pocket_path.to_string_lossy());
let mut root_watcher = RecommendedWatcher::new(
move |res| {
files_tx.try_send(DebouncedOrRoot::Root(res)).unwrap();
if let Ok(event) = res {
main_window
.emit(
"pocket-fs-event",
FSEventPayload {
events: vec![event],
pocket_path: pocket_path_string.clone(),
},
)
.unwrap();
}
},
Config::default(),
)
.unwrap();

root_watcher
.watch(&pocket_path, RecursiveMode::NonRecursive)
.watch(&pocket_path, RecursiveMode::Recursive)
.unwrap();

let poll_tx = tx.clone();
let polling_path = pocket_path.clone();
tauri::async_runtime::spawn(async move {
loop {
tokio::time::sleep(Duration::from_secs(1)).await;
if !polling_path.exists() {
poll_tx
.send(DebouncedOrRoot::PolledDisconnect)
.await
.unwrap();
break;
}
}
});

let main_window = window.clone();
while let Some(res) = rx.recv().await {
match res {
DebouncedOrRoot::Debounced(Ok(events)) => {
// println!("changed: {:?}", &events);
main_window
.emit(
"pocket-fs-event",
FSEventPayload {
events: events.into_iter().map(|e| e.event).collect::<Vec<_>>(),
pocket_path: String::from(pocket_path.to_str().unwrap()),
},
)
.unwrap();
}
DebouncedOrRoot::Debounced(Err(e)) => println!("watch error: {:?}", e),
DebouncedOrRoot::Root(Ok(r)) if r.kind.is_remove() => {
main_window
.emit(
"pocket-connection",
ConnectionEventPayload { connected: false },
)
.unwrap();
break;
}
DebouncedOrRoot::Root(Ok(_)) | DebouncedOrRoot::Root(Err(_)) => {}
DebouncedOrRoot::PolledDisconnect => {
main_window
.emit(
"pocket-connection",
ConnectionEventPayload { connected: false },
)
.unwrap();
break;
}

loop {
tokio::time::sleep(Duration::from_secs(1)).await;
if !polling_path.exists() {
main_window
.emit(
"pocket-connection",
ConnectionEventPayload { connected: false },
)
.unwrap();
break;
}
}

Expand Down
18 changes: 0 additions & 18 deletions src-tauri/src/core_json_files/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,14 @@ use std::{fs, path::PathBuf};
pub struct CoreMetadata {
platform_ids: Vec<String>,
shortname: String,
description: String,
author: String,
url: String,
version: String,
date_release: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Framework {
target_product: String,
version_required: String,
sleep_supported: bool,
dock: Dock,
hardware: Hardware,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Dock {
supported: bool,
analog_output: bool,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Hardware {
link_port: bool,
cartridge_adapter: i32,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
31 changes: 27 additions & 4 deletions src-tauri/src/firmware.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::{path::PathBuf, time::Instant};
use tokio::io::AsyncWriteExt;

use crate::hashes::md5_for_file;
use crate::{hashes::md5_for_file, progress::ProgressEvent, util::progress_download};

#[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -44,13 +45,35 @@ pub async fn get_release_notes(version: &str) -> Result<FirmwareDetails, reqwest
pub async fn download_firmware_file(
url: &str,
path: &PathBuf,
) -> Result<(), Box<dyn std::error::Error>> {
window: &tauri::Window,
) -> Result<()> {
let response = reqwest::get(url).await?;
let bytes = response.bytes().await?;
let mut last_message = Instant::now();

let bytes = progress_download(response, |total_size, downloaded| {
let percentage = (downloaded as f32) / (total_size as f32);
let now = Instant::now();
if now.duration_since(last_message).as_millis() < 16 {
return;
}
last_message = now;
window
.emit(
"progress-event::firmware_download",
ProgressEvent {
finished: downloaded == total_size,
progress: percentage,
message: None,
},
)
.unwrap();
})
.await?;

let mut file = tokio::io::BufWriter::new(tokio::fs::File::create(path).await?);
file.write_all(&bytes).await?;
file.flush().await?;

Ok(())
}

Expand Down
37 changes: 11 additions & 26 deletions src-tauri/src/install_zip/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bytes::BytesMut;
use futures::StreamExt;
use log::info;
use reqwest::StatusCode;
Expand All @@ -21,6 +20,7 @@ use crate::{
core::CoreFile,
updaters::{CoreUpdateDetails, UpdatersFile},
},
util::progress_download,
PocketSyncState,
};

Expand Down Expand Up @@ -119,32 +119,17 @@ pub async fn start_zip_task(window: Window) -> () {
let response = reqwest::get(&install.zip_url).await.unwrap();
match response.status() {
StatusCode::OK => {
let zip_file = {
let total_size = response.content_length().unwrap();
let mut downloaded: u64 = 0;
let mut stream = response.bytes_stream();

let mut file = BytesMut::with_capacity(total_size.try_into().unwrap());
while let Some(item) = stream.next().await {
let chunk = item
.or(Err(format!("Error while downloading file")))
.unwrap();

file.extend(&chunk);
let new =
std::cmp::min(downloaded + (chunk.len() as u64), total_size);
downloaded = new;

FromRustPayload::DownloadProgress {
url: install.zip_url.clone(),
downloaded,
total_size,
}
.emit(&main_window)
.unwrap();
let zip_file = progress_download(response, |total_size, downloaded| {
FromRustPayload::DownloadProgress {
url: install.zip_url.clone(),
downloaded,
total_size,
}
file
};
.emit(&main_window)
.unwrap();
})
.await
.unwrap();

let cursor = Cursor::new(zip_file);
let archive = zip::ZipArchive::new(cursor).unwrap();
Expand Down
Loading

0 comments on commit 61f45c6

Please sign in to comment.