-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.rs
189 lines (171 loc) · 5.61 KB
/
install.rs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use json_comments::strip_comments;
use if_chain::if_chain;
use std::path::PathBuf;
use std::{
fs::{copy, create_dir_all, read_dir},
io,
io::Read
};
use tokio::fs::{remove_dir_all, rename};
use crate::archive_handler;
use crate::gui::mod_list::{ModEntry, ModVersionMeta};
#[derive(Debug, Clone)]
pub enum InstallError {
DirectoryExists(PathBuf, bool),
DeleteError(String),
NameError,
NoModError,
MoveError,
UnsupportedArchive,
ArchiveError,
CopyError,
IDExists(PathBuf, PathBuf, Option<PathBuf>, String),
NewModParseError
}
pub async fn handle_archive(
source_path: PathBuf,
root_dir: PathBuf,
is_folder: bool,
current_mods: Vec<String>
) -> Result<String, InstallError> {
if_chain! {
if let Some(path) = source_path.to_str();
if let Some(_full_name) = source_path.file_name();
if let Some(_file_name) = source_path.file_stem();
if let Some(file_name) = _file_name.to_str();
let mod_dir = root_dir.join("mods");
let raw_temp_dest = mod_dir.join(format!("temp_{}", file_name));
let raw_dest = mod_dir.join(_file_name);
if let Some(temp_dest) = raw_temp_dest.to_str();
then {
if raw_dest.exists() {
return Err(InstallError::DirectoryExists(source_path.clone(), is_folder))
}
if is_folder {
if let Ok(Some(mod_path)) = find_nested_mod(&source_path) {
if let Ok(new_mod_info) = ModEntry::from_file(mod_path.join("mod_info.json").clone()) {
if let Some(id) = current_mods.iter().find(|id| **id == new_mod_info.id) {
return Err(InstallError::IDExists(mod_path, raw_dest, None, id.clone()))
}
if let Err(_) = copy_dir_recursive(&raw_dest, &mod_path) {
return Err(InstallError::CopyError)
} else {
if remove_dir_all(mod_path).await.is_err() {
return Err(InstallError::DeleteError(format!("{:?}", _file_name)))
} else {
return Ok(format!("{:?}", _file_name))
}
}
} else {
return Err(InstallError::NoModError)
}
} else {
Err(InstallError::NewModParseError)
}
} else {
match archive_handler::handle_archive(&path.to_owned(), &temp_dest.to_owned(), &file_name.to_owned()) {
Ok(true) => {
match find_nested_mod(&raw_temp_dest) {
Ok(Some(mod_path)) => {
if let Ok(new_mod_info) = ModEntry::from_file(mod_path.join("mod_info.json").clone()) {
if let Some(id) = current_mods.iter().find(|id| **id == new_mod_info.id) {
return Err(InstallError::IDExists(mod_path, raw_dest, Some(raw_temp_dest), id.clone()))
}
if let Ok(_) = rename(mod_path, raw_dest).await {
if raw_temp_dest.exists() {
if remove_dir_all(&raw_temp_dest).await.is_err() {
return Err(InstallError::DeleteError(format!("{:?}", _file_name)))
}
}
return Ok(format!("{:?}", _file_name))
} else {
return Err(InstallError::MoveError)
}
} else {
Err(InstallError::NewModParseError)
}
},
_ => return Err(InstallError::NoModError)
}
},
Ok(false) => return Err(InstallError::UnsupportedArchive),
Err(_err) => return Err(InstallError::ArchiveError)
}
}
} else {
return Err(InstallError::NameError)
}
}
}
fn find_nested_mod(dest: &PathBuf) -> Result<Option<PathBuf>, io::Error> {
for entry in read_dir(dest)? {
let entry = entry?;
if entry.file_type()?.is_dir() {
let res = find_nested_mod(&entry.path())?;
if res.is_some() {
return Ok(res);
}
} else if entry.file_type()?.is_file() {
if entry.file_name() == "mod_info.json" {
return Ok(Some(dest.to_path_buf()));
}
}
}
Ok(None)
}
fn copy_dir_recursive(to: &PathBuf, from: &PathBuf) -> io::Result<()> {
if !to.exists() {
create_dir_all(to)?;
}
for entry in from.read_dir()? {
let entry = entry?;
if entry.file_type()?.is_dir() {
copy_dir_recursive(&to.to_path_buf().join(entry.file_name()), &entry.path())?;
} else if entry.file_type()?.is_file() {
copy(entry.path(), &to.to_path_buf().join(entry.file_name()))?;
}
}
Ok(())
}
pub async fn get_master_version(local: ModVersionMeta) -> (String, Result<Option<ModVersionMeta>, String>) {
let res = send_request(local.remote_url.clone()).await;
match res {
Err(err) => (local.id, Err(err)),
Ok(remote) => {
if_chain! {
let mut stripped = String::new();
if strip_comments(remote.as_bytes()).read_to_string(&mut stripped).is_ok();
if let Ok(normalized) = handwritten_json::normalize(&stripped);
if let Ok(remote) = json5::from_str::<ModVersionMeta>(&normalized);
then {
if remote.version > local.version {
(
local.id,
Ok(Some(remote))
)
} else {
(
local.id,
Ok(None)
)
}
} else {
(
local.id,
Err(format!("Parse error. Payload:\n{}", remote))
)
}
}
}
}
}
async fn send_request(url: String) -> Result<String, String>{
reqwest::get(url)
.await
.map_err(|e| format!("{:?}", e))?
.error_for_status()
.map_err(|e| format!("{:?}", e))?
.text()
.await
.map_err(|e| format!("{:?}", e))
}