-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathcommands.rs
More file actions
88 lines (79 loc) · 2.51 KB
/
Copy pathcommands.rs
File metadata and controls
88 lines (79 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::pin::Pin;
use std::time::Duration;
use std::{ffi::OsStr, fs};
use crate::{
error::SJMCLResult,
multiplayer::helpers::terracotta::{build_download_param, decompress},
multiplayer::models::MultiplayerError,
resource::models::ResourceError,
tasks::commands::schedule_progressive_task_group,
tasks::monitor::TaskMonitor,
};
use serde_json::Value;
use tauri::{path::BaseDirectory, AppHandle, Manager};
use tokio::process::Command;
#[tauri::command]
pub async fn check_terracotta(app: AppHandle) -> SJMCLResult<bool> {
let dir = &app.path().resolve("terracotta", BaseDirectory::AppData)?;
Ok(dir.exists() && fs::read_dir(dir)?.next().is_some())
}
#[tauri::command]
pub async fn launch_terracotta(app: AppHandle) -> SJMCLResult<()> {
let dir = &app.path().resolve("terracotta", BaseDirectory::AppData)?;
for entry in fs::read_dir(&dir)? {
let entry = entry?;
let path = entry.path();
if path.is_file()
&& path.extension()
== if cfg!(target_os = "windows") {
Some(OsStr::new("exe"))
} else {
None
}
{
Command::new(path)
.arg("--hmcl")
.arg(
&app
.path()
.resolve("sjmcl-terracotta", BaseDirectory::Temp)?,
)
.spawn()?;
return Ok(());
}
}
Err(MultiplayerError::ExecutableNotFound.into())
}
#[tauri::command]
pub async fn download_terracotta(app: AppHandle) -> SJMCLResult<()> {
let download_param = build_download_param(&app).await?;
if download_param.is_empty() {
return Err(ResourceError::NoDownloadApi.into());
}
let task_group =
schedule_progressive_task_group(app.clone(), "terracotta".to_string(), download_param, false)
.await?;
let monitor = app.state::<Pin<Box<TaskMonitor>>>();
monitor.wait_for_task_group(&task_group.task_group).await?;
log::info!("Terracotta downloaded, starting decompression...");
decompress(&app).await?;
log::info!("Terracotta decompressed successfully.");
Ok(())
}
#[tauri::command]
pub async fn fetch_port(app: AppHandle) -> SJMCLResult<u16> {
let path = &app
.path()
.resolve("sjmcl-terracotta", BaseDirectory::Temp)?;
for _ in 0..6 {
if path.exists() {
let content = tokio::fs::read_to_string(path).await?;
let json: Value = serde_json::from_str(&content)?;
if let Some(port) = json.get("port").and_then(|v| v.as_u64()) {
return Ok(port as u16);
}
tokio::time::sleep(Duration::from_millis(500)).await;
}
}
Err(MultiplayerError::PortFileNotFound.into())
}