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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
mod constants;
mod version;
mod profile;
mod strsub;
mod download;
use std::borrow::Cow;
use std::collections::HashMap;
use std::env::consts::{ARCH, OS};
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use const_format::formatcp;
use futures::TryFutureExt;
use log::{debug, warn};
use serde::{Deserialize, Serialize};
use sha1_smol::Sha1;
use sysinfo::System;
use tokio::fs::File;
use tokio::{fs, io};
use tokio::io::AsyncReadExt;
use version::VersionList;
use profile::{Instance, Profile};
use crate::launcher::version::{VersionResolveError, VersionResult};
use crate::version::{Library, OSRestriction, OperatingSystem};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Settings {
profiles: HashMap<String, Profile>,
instances: HashMap<String, Instance>
}
#[derive(Debug)]
enum SettingsLoadError {
IO(io::Error),
Format(serde_json::Error)
}
impl Display for SettingsLoadError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
SettingsLoadError::IO(err) => write!(f, "I/O error loading settings: {}", err),
SettingsLoadError::Format(err) => write!(f, "settings format error: {}", err),
}
}
}
impl Error for SettingsLoadError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
SettingsLoadError::IO(err) => Some(err),
SettingsLoadError::Format(err) => Some(err),
}
}
}
impl Settings {
async fn load(path: impl AsRef<Path>) -> Result<Settings, SettingsLoadError> {
let data = match fs::read_to_string(&path).await {
Ok(data) => data,
Err(e) => return match e.kind() {
ErrorKind::NotFound => Ok(Settings::default()),
_ => Err(SettingsLoadError::IO(e))
}
};
serde_json::from_str(data.as_str()).map_err(SettingsLoadError::Format)
}
}
struct SystemInfo {
os: OperatingSystem,
os_version: String,
arch: String,
arch_bits: usize
}
struct LibraryRepository {
home: PathBuf
}
pub struct Launcher {
online: bool,
home: PathBuf,
versions: VersionList,
settings_path: PathBuf, // maybe redundant but idc >:3
settings: Settings,
system_info: SystemInfo,
libraries: LibraryRepository
}
#[derive(Debug)]
pub enum LaunchError {
UnknownVersion(String),
LoadVersion(Box<dyn Error>),
ResolveVersion(VersionResolveError)
}
impl Display for LaunchError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self {
LaunchError::UnknownVersion(id) => write!(f, "unknown version id: {id}"),
LaunchError::LoadVersion(e) => write!(f, "error loading remote version: {e}"),
LaunchError::ResolveVersion(e) => write!(f, "error resolving remote version: {e}")
}
}
}
impl Error for LaunchError {
fn cause(&self) -> Option<&dyn Error> {
match &self {
LaunchError::LoadVersion(e) => Some(e.as_ref()),
LaunchError::ResolveVersion(e) => Some(e),
_ => None
}
}
}
impl Launcher {
// FIXME: more descriptive error type por favor
pub async fn new(home: &Path, online: bool) -> Result<Launcher, Box<dyn Error>> {
let home = home.to_owned();
let versions_home = home.join("versions");
let versions;
match tokio::fs::create_dir_all(&home).await {
Ok(_) => (),
Err(e) => match e.kind() {
ErrorKind::AlreadyExists => (),
_ => {
warn!("Failed to create launcher home directory: {}", e);
return Err(e.into());
}
}
}
debug!("Version list online?: {online}");
if online {
versions = VersionList::online(versions_home.as_ref()).await?;
} else {
versions = VersionList::offline(versions_home.as_ref()).await?;
}
let settings_path = home.join("ozone.json");
let settings = Settings::load(&settings_path).await?;
Ok(Launcher {
online,
home: home.to_owned(),
versions,
settings_path,
settings,
system_info: SystemInfo::new(),
libraries: LibraryRepository { home: home.join("libraries") }
})
}
pub async fn prepare_launch(&self, profile: &Profile) -> Result<(), LaunchError> {
/* tasks 2 l;aunch the gayme!!!! :3
* - java runtime
* - normal process (good research, past figboot :3)
* - libraries
* - check which libraries we actually need (some have classifiers that don't apply to us)
* - of the libraries we need, check which have correct size and sha1
* - redownload necessary libraries
* - (if offline mode and there are libraries to download, then explode violently)
* - extract natives
* - logging
* - download the config if present and necessary
* - (explode if offline mode and we need to download stuff)
* - assets
* - get asset index (check if our local copy is good and redownload if not)
* - check what ones are good and what needs to be downloaded
* - download them
* - (if offline mode, explode)
* - if virtual or resource-mapped, copy (or perhaps hardlink? that would be cool)
* - the actual client jar
* - check integriddy and download if needed
* - (explode if offline mode)
* - launch the game
* - build argument list and whatnot also
*/
let ver_res = self.versions.get_version_lazy(&profile.version_id);
let ver = match ver_res {
VersionResult::Remote(mv) => Cow::Owned(self.versions.load_remote_version(mv).await.map_err(|e| LaunchError::LoadVersion(e))?),
VersionResult::Complete(cv) => Cow::Borrowed(cv),
VersionResult::None => {
return Err(LaunchError::UnknownVersion(profile.version_id.clone()).into())
}
};
let ver = self.versions.resolve_version(ver.as_ref()).await.map_err(|e| LaunchError::ResolveVersion(e))?;
todo!()
}
}
#[derive(Debug, Clone)]
enum LibraryError {
InvalidName(String),
IOError(ErrorKind)
}
impl Display for LibraryError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
LibraryError::InvalidName(name) => write!(f, "invalid name: {name}"),
LibraryError::IOError(e) => write!(f, "io error reading library: {e}"),
}
}
}
impl Error for LibraryError {}
const ARCH_BITS: &'static str = formatcp!("{}", usize::BITS);
impl LibraryRepository {
fn lib_replace(key: &str) -> Option<Cow<'static, str>> {
match key {
"arch" => Some(Cow::Borrowed(ARCH_BITS)),
_ => None
}
}
fn get_artifact_base_dir(name: &str) -> Option<PathBuf> {
let end_of_gid = name.find(':')?;
Some(name[..end_of_gid].split('.').chain(name.split(':').skip(1).take(2)).collect())
}
fn get_artifact_filename(name: &str, classifier: Option<&str>) -> Option<PathBuf> {
let n: Vec<&str> = name.splitn(4, ':').skip(1).collect();
if let Some(classifier) = classifier {
match n.len() {
3 => Some(PathBuf::from(strsub::replace_thru(format!("{}-{}-{}.jar", n[1], n[2], classifier), Self::lib_replace))),
4 => Some(PathBuf::from(strsub::replace_thru(format!("{}-{}-{}-{}.jar", n[1], n[2], classifier, n[3]), Self::lib_replace))),
_ => None
}
} else {
match n.len() {
3 => Some(PathBuf::from(strsub::replace_thru(format!("{}-{}.jar", n[1], n[2]), Self::lib_replace))),
4 => Some(PathBuf::from(strsub::replace_thru(format!("{}-{}-{}.jar", n[1], n[2], n[3]), Self::lib_replace))),
_ => None
}
}
}
fn get_artifact_path(name: &str, classifier: Option<&str>) -> Option<PathBuf> {
let Some(mut p) = Self::get_artifact_base_dir(name) else {
return None;
};
p.push(Self::get_artifact_filename(name, classifier)?);
Some(p)
}
}
impl SystemInfo {
fn new() -> SystemInfo {
let os = match OS {
"windows" => OperatingSystem::Windows,
"macos" => OperatingSystem::MacOS,
"linux" => OperatingSystem::Linux,
_ => OperatingSystem::Unknown // could probably consider "hurd" and "*bsd" to be linux...
};
let mut os_version = System::os_version().unwrap_or_default();
if os == OperatingSystem::Windows && (os_version.starts_with("10") || os_version.starts_with("11")) {
os_version.replace_range(..2, "10.0"); // minecraft expects this funny business...
}
let mut arch = ARCH.to_owned();
if arch == "x86_64" {
// this nomenclature is preferred, since some versions expect the arch containing "x86" to mean 32-bit.
arch.replace_range(.., "amd64");
}
SystemInfo {
os,
os_version,
arch,
arch_bits: size_of::<*const i32>() * 8
}
}
fn is_our_os(&self, os: OperatingSystem) -> bool {
if self.os == OperatingSystem::Unknown {
return false;
}
self.os == os
}
fn applies(&self, restriction: &OSRestriction) -> bool {
restriction.os.is_none_or(|os| self.is_our_os(os))
&& restriction.version.as_deref().is_none_or(|pat| pat.is_match(&self.os_version))
&& restriction.arch.as_deref().is_none_or(|pat| pat.is_match(&self.arch))
}
}
|