Skip to content

Commit 7ca53a2

Browse files
committed
Second attempt...
1 parent 65248f4 commit 7ca53a2

File tree

6 files changed

+184
-52
lines changed

6 files changed

+184
-52
lines changed

wrapper/src/app.rs

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
* other in this repository, all of which is reserved by Essential.
1414
*/
1515

16-
use crate::download::download_java;
17-
use crate::file::extract_java_task;
18-
use crate::installer::run_installer;
16+
use crate::download::{download_java_and_run_task};
1917
use crate::java::get_java_executable_in_downloaded_jre;
2018
use crate::logging::get_logging_file_from_temp_directory;
2119
use crate::{fonts, WrapperInfo, BRAND};
@@ -43,29 +41,30 @@ pub fn start_app(app: WrapperApp, decorations: bool) {
4341
WrapperApp::update,
4442
WrapperApp::view,
4543
)
46-
.window(window::Settings {
47-
size: Size {
48-
width: 688.,
49-
height: 344.,
50-
},
51-
position: window::Position::Centered,
52-
resizable: false,
53-
decorations,
54-
icon: from_file_data(include_bytes!("../resources/icon.png"), None)
55-
.inspect_err(|e| warn!("Error when loading icon: {}", e))
56-
.ok(),
57-
..Default::default()
58-
})
59-
.subscription(WrapperApp::subscription)
60-
.theme(move |_| AppTheme::default())
61-
.run_with(|| WrapperApp::new(app))
62-
.unwrap();
44+
.window(window::Settings {
45+
size: Size {
46+
width: 688.,
47+
height: 344.,
48+
},
49+
position: window::Position::Centered,
50+
resizable: false,
51+
decorations,
52+
icon: from_file_data(include_bytes!("../resources/icon.png"), None)
53+
.inspect_err(|e| warn!("Error when loading icon: {}", e))
54+
.ok(),
55+
..Default::default()
56+
})
57+
.subscription(WrapperApp::subscription)
58+
.theme(move |_| AppTheme::default())
59+
.run_with(|| WrapperApp::new(app))
60+
.unwrap();
6361
}
6462

6563
#[derive(Debug, Default)]
6664
pub struct WrapperApp {
6765
pub app_state: AppState,
6866
pub wrapper_info: WrapperInfo,
67+
pub url: String,
6968
}
7069

7170
#[derive(Debug, Clone, Default)]
@@ -141,26 +140,26 @@ impl WrapperApp {
141140
} else {
142141
self.app_state = new_state
143142
}
144-
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-
);
154-
}
155-
if let AppState::ExtractFinished = &mut self.app_state {
156-
self.app_state = if let Some(exec) =
157-
get_java_executable_in_downloaded_jre(&self.wrapper_info.cache_dir)
158-
{
159-
AppState::InstallerRunning(exec)
160-
} else {
161-
AppState::Errored("Couldn't find java after extracting".into())
162-
}
163-
}
143+
/* if let AppState::DownloadFinished(download_path) = &mut self.app_state {
144+
let task = extract_java_task(
145+
self.wrapper_info.cache_dir.clone(),
146+
download_path.clone(),
147+
);
148+
// self.app_state = AppState::ExtractingJava(download_path.clone());
149+
return Task::perform(
150+
async { AppState::ExtractFinished },
151+
AppMessage::UpdateState,
152+
);
153+
}
154+
if let AppState::ExtractFinished = &mut self.app_state {
155+
self.app_state = if let Some(exec) =
156+
get_java_executable_in_downloaded_jre(&self.wrapper_info.cache_dir)
157+
{
158+
AppState::InstallerRunning(exec)
159+
} else {
160+
AppState::Errored("Couldn't find java after extracting".into())
161+
}
162+
}*/
164163
debug!("Resulting state: {}", self.app_state);
165164
if let AppState::Finished = &mut self.app_state {
166165
return iced::exit();
@@ -283,7 +282,7 @@ impl WrapperApp {
283282
.into()
284283
}
285284

286-
fn subscription(&self) -> Subscription<AppMessage> {
285+
/*fn subscription(&self) -> Subscription<AppMessage> {
287286
debug!("Getting subscription {}", self.app_state);
288287
match &self.app_state {
289288
AppState::Downloading(url, _) => {
@@ -297,6 +296,24 @@ impl WrapperApp {
297296
}
298297
_ => Subscription::none(),
299298
}
299+
}*/
300+
301+
fn subscription(&self) -> Subscription<AppMessage> {
302+
debug!("Getting subscription {}", self.app_state);
303+
match &self.app_state {
304+
AppState::Errored(e) => Subscription::none(),
305+
_ => Subscription::run_with_id(
306+
"download-and-run",
307+
download_java_and_run_task(self.url.clone(), self.wrapper_info.clone()),
308+
).map(|t| {
309+
if let Err(e) = t {
310+
error!("Error when downloading java: {}", e);
311+
AppMessage::UpdateState(AppState::Errored(format!("Error downloading java: {}", e)))
312+
} else {
313+
AppMessage::UpdateState(t.unwrap())
314+
}
315+
}),
316+
}
300317
}
301318
}
302319

wrapper/src/download.rs

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
use crate::app::{AppMessage, AppState};
17-
use crate::java::get_java_zip_path_from_cache_dir;
17+
use crate::java::{get_java_executable_in_downloaded_jre, get_java_extract_folder_from_cache_dir, get_java_zip_path_from_cache_dir};
1818
use crate::util::TaskError;
1919
use iced::futures::{SinkExt, Stream, StreamExt};
2020
use iced::stream::try_channel;
@@ -24,8 +24,11 @@ use std::path::PathBuf;
2424
use std::sync::Arc;
2525
use std::time::Duration;
2626
use std::{fs, fs::File, io::Write};
27+
use crate::file::extract_zip;
28+
use crate::installer::{try_run_installer, InstallerExitCode, InstallerRunError};
29+
use crate::WrapperInfo;
2730

28-
pub fn download_java<T: ToString>(url: T, cache_dir: PathBuf) -> Subscription<AppMessage> {
31+
/*pub fn download_java<T: ToString>(url: T, cache_dir: PathBuf) -> Subscription<AppMessage> {
2932
Subscription::run_with_id(
3033
"download-java",
3134
download_java_task(url.to_string(), cache_dir),
@@ -38,9 +41,9 @@ pub fn download_java<T: ToString>(url: T, cache_dir: PathBuf) -> Subscription<Ap
3841
AppMessage::UpdateState(t.unwrap())
3942
}
4043
})
41-
}
44+
}*/
4245

43-
pub fn download_java_task(
46+
/*pub fn download_java_task(
4447
url: String,
4548
cache_dir: PathBuf,
4649
) -> impl Stream<Item = Result<AppState, TaskError>> {
@@ -105,4 +108,114 @@ pub fn download_java_task(
105108
info!("Successfully downloaded java!");
106109
Ok(())
107110
})
111+
}*/
112+
113+
pub fn download_java_and_run_task(
114+
url: String,
115+
wrapper_info: WrapperInfo,
116+
) -> impl Stream<Item = Result<AppState, TaskError>> {
117+
try_channel(100000, move |mut output| async move {
118+
info!("Starting download from {}", url);
119+
let download_path = get_java_zip_path_from_cache_dir(&wrapper_info.cache_dir);
120+
info!("Temporary jre zip file: {}", download_path.display());
121+
122+
info!("Check if the old file exists");
123+
if download_path.try_exists().unwrap_or(false) {
124+
info!("Deleting the old file");
125+
fs::remove_file(download_path.clone()).map_err(|e| {
126+
TaskError::IOError(
127+
"Error when deleting old JRE zip file".to_string(),
128+
Arc::new(e),
129+
)
130+
})?;
131+
}
132+
133+
let mut file = File::create(download_path.clone()).map_err(|e| {
134+
TaskError::IOError("Error creating the download file!".to_string(), Arc::new(e))
135+
})?;
136+
137+
info!("File created!");
138+
139+
let client = reqwest::Client::builder()
140+
.connect_timeout(Duration::from_secs(10))
141+
.timeout(Duration::from_secs(120)) // 3 mbit download for 45MB file
142+
.build()?;
143+
144+
let response = client.get(url.clone()).send().await?;
145+
146+
if !response.status().is_success() {
147+
warn!("Non-OK status received as response when downloading!");
148+
warn!(
149+
"Response text: \n{}",
150+
response.text().await.unwrap_or_else(|e| { e.to_string() })
151+
);
152+
return Err(TaskError::UnsuccessfulResponse());
153+
}
154+
155+
let total = response
156+
.content_length()
157+
.ok_or(TaskError::NoContentLength())?;
158+
159+
output.send(AppState::Downloading(url.clone(), 0.0)).await?;
160+
161+
let mut byte_stream = response.bytes_stream();
162+
let mut downloaded = 0;
163+
164+
while let Some(next_bytes) = byte_stream.next().await {
165+
let bytes = next_bytes?;
166+
downloaded += bytes.len();
167+
file.write_all(&bytes)?;
168+
let progress = 100.0 * downloaded as f32 / total as f32;
169+
if let Err(e) = output.try_send(AppState::Downloading(url.clone(), progress)) {
170+
debug!("Channel full!")
171+
}
172+
}
173+
174+
output.send(AppState::DownloadFinished(download_path.clone())).await?;
175+
info!("Successfully downloaded java! Now extracting...");
176+
output.send(AppState::ExtractingJava(download_path.clone())).await?;
177+
178+
let extract_path = get_java_extract_folder_from_cache_dir(&wrapper_info.cache_dir);
179+
info!("Temporary jre folder: {}", extract_path.display());
180+
181+
extract_zip(download_path, extract_path)?;
182+
output.send(AppState::ExtractFinished).await?;
183+
184+
info!("Extracted! Ruunning installer...");
185+
186+
let java_executable = get_java_executable_in_downloaded_jre(&wrapper_info.cache_dir)
187+
.ok_or(TaskError::UnsuccessfulResponse())?; // TODO error
188+
189+
output.send(AppState::InstallerRunning(java_executable.clone())).await?;
190+
let state = match try_run_installer(&java_executable, &wrapper_info) {
191+
Ok(ex) => match ex {
192+
InstallerExitCode::Success => AppState::Finished,
193+
InstallerExitCode::UnknownError => {
194+
error!("Unknown error when running installer!");
195+
AppState::Errored("Unknown error!".to_string())
196+
}
197+
InstallerExitCode::NoOpenGl => {
198+
error!("No OpenGL found when running installer!");
199+
AppState::Errored("No OpenGL found!".to_string())
200+
}
201+
InstallerExitCode::UnsupportedPath => {
202+
error!("Launcher found an unsupported path!");
203+
AppState::Errored("Launcher found an unsupported path!".to_string())
204+
}
205+
},
206+
Err(e) => {
207+
let error = match e {
208+
InstallerRunError::JavaFailed => "Java test failed!".to_string(),
209+
InstallerRunError::UnknownExitCode(c) => {
210+
format!("Unknown installer exit code: {}", c)
211+
}
212+
InstallerRunError::Other => "Unknown error!".to_string(),
213+
};
214+
AppState::Errored(error)
215+
}
216+
};
217+
218+
output.send(state).await?;
219+
Ok(())
220+
})
108221
}

wrapper/src/file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub fn extract_java_task(
128128
})
129129
}*/
130130

131-
pub fn extract_java_task(cache_dir: PathBuf, download_path: PathBuf) -> Task<AppMessage> {
131+
/*pub fn extract_java_task(cache_dir: PathBuf, download_path: PathBuf) -> Task<AppMessage> {
132132
Task::perform(
133133
async move {
134134
let (tx, rx) = oneshot::channel();
@@ -149,4 +149,4 @@ pub fn extract_java_task(cache_dir: PathBuf, download_path: PathBuf) -> Task<App
149149
},
150150
AppMessage::UpdateState,
151151
)
152-
}
152+
}*/

wrapper/src/installer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,19 @@ pub fn get_installer_path(_wrapper_info: &WrapperInfo) -> Option<String> {
153153
path.into_os_string().into_string().ok()
154154
}
155155

156-
pub fn run_installer(
156+
/*pub fn run_installer(
157157
java_executable: String,
158158
wrapper_info: WrapperInfo,
159159
) -> Subscription<AppMessage> {
160160
Subscription::run_with_id(
161161
"run-installer",
162162
run_installer_task(java_executable, wrapper_info).map(AppMessage::UpdateState),
163163
)
164-
}
164+
}*/
165165

166166
// This is 100% not how I should use a stream, but it works for now.
167167
// If you see this it means I forgot to come back and look at this again
168-
pub fn run_installer_task(
168+
/*pub fn run_installer_task(
169169
java_executable: String,
170170
wrapper_info: WrapperInfo,
171171
) -> impl Stream<Item = AppState> {
@@ -199,4 +199,4 @@ pub fn run_installer_task(
199199
};
200200
let _ = output.send(state).await;
201201
})
202-
}
202+
}*/

wrapper/src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ macro_rules! show_error {
2727
$crate::app::WrapperApp {
2828
app_state: $crate::app::AppState::Errored($error.to_string()),
2929
wrapper_info: $wrapper_info,
30+
url: String::new(),
3031
},
3132
true,
3233
);

wrapper/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ pub fn main() {
9696

9797
start_app(
9898
WrapperApp {
99-
app_state: AppState::Downloading(url, 0.),
99+
app_state: AppState::Downloading(url.clone(), 0.),
100100
wrapper_info: wrapper_info.clone(),
101+
url: url.to_string(),
101102
},
102103
false,
103104
);

0 commit comments

Comments
 (0)