Skip to content

Commit 72cecb4

Browse files
committed
Works when no progress bar...
1 parent 65248f4 commit 72cecb4

File tree

4 files changed

+78
-110
lines changed

4 files changed

+78
-110
lines changed

wrapper/src/app.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use crate::download::download_java;
1717
use crate::file::extract_java_task;
18-
use crate::installer::run_installer;
18+
use crate::installer::{run_installer_task};
1919
use crate::java::get_java_executable_in_downloaded_jre;
2020
use crate::logging::get_logging_file_from_temp_directory;
2121
use crate::{fonts, WrapperInfo, BRAND};
@@ -142,15 +142,7 @@ impl WrapperApp {
142142
self.app_state = new_state
143143
}
144144
if let AppState::DownloadFinished(download_path) = &mut self.app_state {
145-
let task = extract_java_task(
146-
self.wrapper_info.cache_dir.clone(),
147-
download_path.clone(),
148-
);
149-
// self.app_state = AppState::ExtractingJava(download_path.clone());
150-
return Task::perform(
151-
async { AppState::ExtractFinished },
152-
AppMessage::UpdateState,
153-
);
145+
self.app_state = AppState::ExtractingJava(download_path.clone());
154146
}
155147
if let AppState::ExtractFinished = &mut self.app_state {
156148
self.app_state = if let Some(exec) =
@@ -166,9 +158,20 @@ impl WrapperApp {
166158
return iced::exit();
167159
}
168160
// Compiler complained?
161+
162+
let info = self.wrapper_info.clone();
163+
let cache_dir = info.cache_dir.clone();
169164
let state = self.app_state.clone();
170-
window::get_oldest().and_then(move |id| match state {
165+
window::get_oldest().and_then(move |id| match &state {
171166
AppState::Downloading(_, _) => window::change_mode(id, Mode::Windowed),
167+
AppState::ExtractingJava(download_path) => Task::batch(vec![
168+
window::change_mode(id, Mode::Windowed),
169+
extract_java_task(cache_dir.clone(), download_path.clone()),
170+
]),
171+
AppState::InstallerRunning(exec) => Task::batch(vec![
172+
window::change_mode(id, Mode::Hidden),
173+
run_installer_task(exec.clone(), info.clone()),
174+
]),
172175
AppState::Errored(_) => Task::batch(vec![
173176
window::change_mode(id, Mode::Windowed),
174177
window::toggle_decorations(id),
@@ -289,12 +292,6 @@ impl WrapperApp {
289292
AppState::Downloading(url, _) => {
290293
download_java(url.clone(), self.wrapper_info.cache_dir.clone())
291294
}
292-
/*AppState::ExtractingJava(download_path) => {
293-
extract_java(self.wrapper_info.cache_dir.clone(), download_path.clone())
294-
}*/
295-
AppState::InstallerRunning(java_executable) => {
296-
run_installer(java_executable.clone(), self.wrapper_info.clone())
297-
}
298295
_ => Subscription::none(),
299296
}
300297
}

wrapper/src/download.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ pub fn download_java_task(
6060
})?;
6161
}
6262

63-
let mut file = File::create(download_path.clone()).map_err(|e| {
64-
TaskError::IOError("Error creating the download file!".to_string(), Arc::new(e))
65-
})?;
66-
67-
info!("File created!");
68-
6963
let client = reqwest::Client::builder()
7064
.connect_timeout(Duration::from_secs(10))
7165
.timeout(Duration::from_secs(120)) // 3 mbit download for 45MB file
@@ -88,21 +82,39 @@ pub fn download_java_task(
8882

8983
output.send(AppState::Downloading(url.clone(), 0.0)).await?;
9084

85+
let mut buffer = vec![0u8; total as usize];
9186
let mut byte_stream = response.bytes_stream();
9287
let mut downloaded = 0;
9388

9489
while let Some(next_bytes) = byte_stream.next().await {
9590
let bytes = next_bytes?;
91+
let previously_downloaded = downloaded;
9692
downloaded += bytes.len();
97-
file.write_all(&bytes)?;
93+
buffer[previously_downloaded..downloaded].copy_from_slice(&bytes);
9894
let progress = 100.0 * downloaded as f32 / total as f32;
9995
if let Err(e) = output.try_send(AppState::Downloading(url.clone(), progress)) {
10096
debug!("Channel full!")
10197
}
10298
}
10399

104-
output.send(AppState::DownloadFinished(download_path)).await?;
105-
info!("Successfully downloaded java!");
100+
if let Err(e) = output.try_send(AppState::Downloading(url.clone(), 100f32)) {
101+
debug!("Channel full! {}", e)
102+
}
103+
104+
info!("Finished downloading!");
105+
106+
let mut file = File::create(download_path.clone()).map_err(|e| {
107+
TaskError::IOError("Error creating the download file!".to_string(), Arc::new(e))
108+
})?;
109+
110+
info!("File created!");
111+
112+
file.write_all(&buffer[..downloaded])?;
113+
114+
info!("Written to file!");
115+
if let Err(e) = output.try_send(AppState::DownloadFinished(download_path.clone())) {
116+
debug!("Channel full! {}", e)
117+
}
106118
Ok(())
107119
})
108120
}

wrapper/src/file.rs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -95,39 +95,6 @@ pub fn extract_zip(zip_path: PathBuf, target_path: PathBuf) -> ZipResult<()> {
9595
ZipArchive::new(file)?.extract_unwrapped_root_dir(target_path, root_dir_common_filter)
9696
}
9797

98-
/*pub fn extract_java(cache_dir: PathBuf, download_path: PathBuf) -> Subscription<AppMessage> {
99-
Subscription::run_with_id("extract-java", extract_java_task(cache_dir, download_path))
100-
.map(|t| {
101-
if let Err(e) = t {
102-
error!("Error when extracting java: {}", e);
103-
AppMessage::UpdateState(AppState::Errored(format!("Error extracting java: {}", e)))
104-
} else {
105-
AppMessage::UpdateState(t.unwrap())
106-
}
107-
})
108-
}
109-
110-
pub fn extract_java_task(
111-
cache_dir: PathBuf,
112-
download_path: PathBuf,
113-
) -> impl Stream<Item = Result<AppState, TaskError>> {
114-
try_channel(10, move |mut output| async move {
115-
let extract_path = get_java_extract_folder_from_cache_dir(&cache_dir);
116-
info!("Temporary jre folder: {}", extract_path.display());
117-
let state = extract_zip(download_path.clone(), extract_path.clone())
118-
.map(|_| {
119-
info!("Successfully extracted java!");
120-
AppState::ExtractFinished
121-
})
122-
.unwrap_or_else(|e| {
123-
error!("Error when extracting java: {}", e);
124-
AppState::Errored(format!("Error extracting java: {}", e))
125-
});
126-
output.try_send(state).map_err(|_| TaskError::ChannelFailure())?;
127-
Ok(())
128-
})
129-
}*/
130-
13198
pub fn extract_java_task(cache_dir: PathBuf, download_path: PathBuf) -> Task<AppMessage> {
13299
Task::perform(
133100
async move {
@@ -138,14 +105,9 @@ pub fn extract_java_task(cache_dir: PathBuf, download_path: PathBuf) -> Task<App
138105
let res = extract_zip(download_path, extract_path)
139106
.map(|_| AppState::ExtractFinished)
140107
.unwrap_or_else(|e| AppState::Errored(format!("Error extracting java: {e}")));
141-
info!("Result: {}", res);
142108
let _ = tx.send(res);
143109
});
144-
let res = rx
145-
.await
146-
.unwrap_or_else(|_| AppState::Errored("Extraction thread dropped".into()));
147-
info!("Result 2: {}", res);
148-
res
110+
rx.await.unwrap_or_else(|_| AppState::Errored("Extraction thread dropped".into()))
149111
},
150112
AppMessage::UpdateState,
151113
)

wrapper/src/installer.rs

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ use crate::java::test_java;
1717
use crate::process::run_process;
1818
use crate::{app, show_error, WrapperInfo, VERSION};
1919
use app::{AppMessage, AppState};
20-
use iced::futures::Stream;
21-
use iced::futures::{SinkExt, StreamExt};
22-
use iced::stream::channel;
23-
use iced::Subscription;
20+
use iced::{Task};
2421
use log::{error, info, warn};
2522
use std::process::exit;
23+
use iced::futures::channel::oneshot;
2624

2725
pub enum InstallerExitCode {
2826
Success,
@@ -153,50 +151,49 @@ pub fn get_installer_path(_wrapper_info: &WrapperInfo) -> Option<String> {
153151
path.into_os_string().into_string().ok()
154152
}
155153

156-
pub fn run_installer(
157-
java_executable: String,
158-
wrapper_info: WrapperInfo,
159-
) -> Subscription<AppMessage> {
160-
Subscription::run_with_id(
161-
"run-installer",
162-
run_installer_task(java_executable, wrapper_info).map(AppMessage::UpdateState),
163-
)
164-
}
165-
166-
// This is 100% not how I should use a stream, but it works for now.
167-
// If you see this it means I forgot to come back and look at this again
168154
pub fn run_installer_task(
169155
java_executable: String,
170156
wrapper_info: WrapperInfo,
171-
) -> impl Stream<Item = AppState> {
172-
channel(1, move |mut output| async move {
173-
let state = match try_run_installer(&java_executable, &wrapper_info) {
174-
Ok(ex) => match ex {
175-
InstallerExitCode::Success => AppState::Finished,
176-
InstallerExitCode::UnknownError => {
177-
error!("Unknown error when running installer!");
178-
AppState::Errored("Unknown error!".to_string())
179-
}
180-
InstallerExitCode::NoOpenGl => {
181-
error!("No OpenGL found when running installer!");
182-
AppState::Errored("No OpenGL found!".to_string())
183-
}
184-
InstallerExitCode::UnsupportedPath => {
185-
error!("Launcher found an unsupported path!");
186-
AppState::Errored("Launcher found an unsupported path!".to_string())
187-
}
188-
},
189-
Err(e) => {
190-
let error = match e {
191-
InstallerRunError::JavaFailed => "Java test failed!".to_string(),
192-
InstallerRunError::UnknownExitCode(c) => {
193-
format!("Unknown installer exit code: {}", c)
157+
) -> Task<AppMessage> {
158+
Task::perform(
159+
async move {
160+
let (tx, rx) = oneshot::channel();
161+
std::thread::spawn(move || {
162+
let state = match try_run_installer(&java_executable, &wrapper_info) {
163+
Ok(ex) => match ex {
164+
InstallerExitCode::Success => AppState::Finished,
165+
InstallerExitCode::UnknownError => {
166+
error!("Unknown error when running installer!");
167+
AppState::Errored("Unknown error!".to_string())
168+
}
169+
InstallerExitCode::NoOpenGl => {
170+
error!("No OpenGL found when running installer!");
171+
AppState::Errored("No OpenGL found!".to_string())
172+
}
173+
InstallerExitCode::UnsupportedPath => {
174+
error!("Launcher found an unsupported path!");
175+
AppState::Errored("Launcher found an unsupported path!".to_string())
176+
}
177+
},
178+
Err(e) => {
179+
let error = match e {
180+
InstallerRunError::JavaFailed => "Java test failed!".to_string(),
181+
InstallerRunError::UnknownExitCode(c) => {
182+
format!("Unknown installer exit code: {}", c)
183+
}
184+
InstallerRunError::Other => "Unknown error!".to_string(),
185+
};
186+
AppState::Errored(error)
194187
}
195-
InstallerRunError::Other => "Unknown error!".to_string(),
196188
};
197-
AppState::Errored(error)
198-
}
199-
};
200-
let _ = output.send(state).await;
201-
})
189+
let _ = tx.send(state);
190+
});
191+
let res = rx
192+
.await
193+
.unwrap_or_else(|_| AppState::Errored("Extraction thread dropped".into()));
194+
info!("Result 2: {}", res);
195+
res
196+
},
197+
AppMessage::UpdateState,
198+
)
202199
}

0 commit comments

Comments
 (0)