-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathwine.rs
More file actions
515 lines (432 loc) · 14.5 KB
/
Copy pathwine.rs
File metadata and controls
515 lines (432 loc) · 14.5 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
use std::{
collections::HashMap,
env,
ffi::OsStr,
fs::{create_dir_all, remove_dir_all, remove_file, File},
io::Read,
path::PathBuf,
process::{ExitStatus, Stdio},
};
use flate2::read::GzDecoder;
use lazy_static::lazy_static;
use log::{info, warn};
use regex::Regex;
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use tar::Archive;
use tokio::{
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
process::Command,
sync::Mutex,
};
use xz2::read::XzDecoder;
use crate::util::{
github::{fetch_github_release, fetch_github_releases, github_download_asset, GithubRelease},
native::{maxima_dir, maxima_cache_dir, DownloadError, NativeError, SafeParent, SafeStr, WineError},
registry::RegistryError,
};
lazy_static! {
static ref PROTON_PATTERN: Regex = Regex::new(r"GE-Proton\d+-\d+\.tar\.gz").unwrap();
}
// A Proton verb to use
pub enum CommandType {
// Set the prefix up and runs the command
Run,
// Waits for any hanging wineserver instances and runs the command
WaitForExitAndRun,
// Directly calls the command, doesn't setup the prefix (use with caution)
RunInPrefix,
}
impl std::fmt::Display for CommandType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = match self {
Self::RunInPrefix => "runinprefix",
Self::Run => "run",
Self::WaitForExitAndRun => "waitforexitandrun",
};
f.write_str(display)
}
}
const VERSION_FILE: &str = "dependency-versions.toml";
#[derive(Deserialize, Default)]
pub(crate) struct LutrisRuntime {
name: String,
created_at: String,
url: String,
}
#[derive(Serialize, Deserialize, Default)]
#[serde(default)]
struct Versions {
proton: String,
eac_runtime: String,
umu: String,
}
/// Returns internal prtoton pfx path
pub fn wine_prefix_dir() -> Result<PathBuf, NativeError> {
Ok(maxima_dir()?.join("wine/prefix"))
}
pub fn proton_dir() -> Result<PathBuf, NativeError> {
Ok(maxima_dir()?.join("wine/proton"))
}
pub fn wine_dir() -> Result<PathBuf, NativeError> {
Ok(maxima_dir()?.join("wine"))
}
pub fn eac_dir() -> Result<PathBuf, NativeError> {
Ok(maxima_dir()?.join("wine/eac_runtime"))
}
pub fn umu_bin() -> Result<PathBuf, NativeError> {
Ok(maxima_dir()?.join("wine/umu/umu-run"))
}
fn versions() -> Result<Versions, NativeError> {
let file = maxima_dir()?.join(VERSION_FILE);
if !file.exists() {
return Ok(Versions::default());
}
let data = std::fs::read_to_string(file)?;
Ok(toml::from_str(&data).unwrap_or_default())
}
fn set_versions(versions: Versions) -> Result<(), NativeError> {
let file = maxima_dir()?.join(VERSION_FILE);
std::fs::write(file, toml::to_string(&versions)?)?;
Ok(())
}
pub(crate) async fn check_wine_validity() -> Result<bool, NativeError> {
if !proton_dir()?.exists() {
return Ok(false);
}
let version = versions()?.proton;
let release = get_wine_release();
if let Err(err) = release {
if !version.is_empty() {
warn!("Failed to check wine release, rate limited?");
return Ok(true);
}
return Err(NativeError::Wine(err));
}
Ok(version == release?.tag_name)
}
pub(crate) async fn get_lutris_runtimes() -> Result<Vec<LutrisRuntime>, WineError> {
let client = reqwest::Client::builder()
.user_agent("ArmchairDevelopers/Maxima")
.build()?;
let res = client.get("https://lutris.net/api/runtimes").send().await?;
let res = res.error_for_status()?;
let data = res.json().await?;
Ok(data)
}
pub(crate) async fn check_runtime_validity(
key: &str,
runtimes: &[LutrisRuntime],
) -> Result<bool, NativeError> {
let versions = versions()?;
let version = match key {
"umu" => &versions.umu,
"eac_runtime" => &versions.eac_runtime,
_ => {
return Err(NativeError::Wine(WineError::UnimplementedRuntime(
key.to_string(),
)))
}
};
let path = wine_dir()?.join(key);
if !path.exists() {
return Ok(false);
}
let runtime_version = runtimes.iter().find(|r| r.name == key);
Ok(runtime_version.is_some_and(|r| &r.created_at == version))
}
pub(crate) async fn install_runtime(
key: &str,
runtimes: &[LutrisRuntime],
) -> Result<(), NativeError> {
info!("Downloading {key}");
let runtime = runtimes
.iter()
.find(|r| r.name == key)
.ok_or(NativeError::Wine(WineError::UnimplementedRuntime(
key.to_string(),
)))?;
let mut versions = versions()?;
let path = wine_dir()?.join(key);
let runtime_ver = match key {
"umu" => &mut versions.umu,
"eac_runtime" => &mut versions.eac_runtime,
_ => {
return Err(NativeError::Wine(WineError::UnimplementedRuntime(
key.to_string(),
)))
}
};
let res = match ureq::get(&runtime.url)
.set("User-Agent", "ArmchairDevelopers/Maxima")
.call()
{
Err(err) => return Err(NativeError::Download(DownloadError::Request1(err))),
Ok(res) => res,
};
if res.status() != StatusCode::OK {
return Err(NativeError::Download(DownloadError::Http(key.to_string())));
}
let mut body: Vec<u8> = vec![];
res.into_reader().read_to_end(&mut body)?;
if path.exists() {
remove_dir_all(&path)?;
}
create_dir_all(&path)?;
let data: Box<dyn std::io::Read> = if runtime.url.ends_with(".xz") {
Box::new(XzDecoder::new(&body[..]))
} else {
Box::new(&body[..])
};
let archive = Archive::new(data);
extract_archive(path, archive)?;
let created_at = runtime.created_at.clone();
*runtime_ver = created_at;
set_versions(versions)
}
fn get_wine_release() -> Result<GithubRelease, WineError> {
let releases = fetch_github_releases("GloriousEggroll", "proton-ge-custom")?;
let mut release = None;
for r in releases {
if r.tag_name.ends_with("LoL") {
continue;
}
release = Some(r);
break;
}
release.ok_or(WineError::Fetch)
}
pub async fn run_wine_command<I: IntoIterator<Item = T>, T: AsRef<OsStr>>(
arg: T,
args: Option<I>,
cwd: Option<PathBuf>,
want_output: bool,
command_type: CommandType,
) -> Result<String, NativeError> {
let proton_path = proton_dir()?;
let proton_prefix_path = wine_prefix_dir()?;
let eac_path = eac_dir()?;
let umu_bin = umu_bin()?;
let wine_path =
env::var("MAXIMA_WINE_COMMAND").unwrap_or_else(|_| umu_bin.to_string_lossy().to_string());
// Create command with all necessary wine env variables
let mut binding = Command::new(wine_path.clone());
let mut child = binding
.env("WINEPREFIX", proton_prefix_path)
.env("GAMEID", "umu-0")
.env("PROTON_VERB", &command_type.to_string())
.env("PROTONPATH", proton_path)
.env("STORE", "ea")
.env("PROTON_EAC_RUNTIME", eac_path)
.env("UMU_ZENITY", "1")
.env("WINEDEBUG", "fixme-all")
.env("LD_PRELOAD", "") // Fixes some log errors for some games
.arg(arg);
if !wine_path.ends_with("umu-run") {
// wsock32 is used as a proxy for Northstar (Titanfall 2). TODO: provide user-facing option for this!
child = child.env(
"WINEDLLOVERRIDES",
"CryptBase,wsock32,bcrypt,dxgi,d3d11,d3d12,d3d12core=n,b;winemenubuilder.exe=d",
);
}
if let Some(arguments) = args {
child = child.args(arguments);
}
if let Some(cwd) = cwd {
child.current_dir(cwd);
}
let status: ExitStatus;
let mut output_str = String::new();
if want_output {
let output = child
.stdout(Stdio::piped())
.spawn()?
.wait_with_output()
.await?;
output_str = String::from_utf8_lossy(&output.stdout).to_string();
status = output.status;
} else {
status = child.spawn()?.wait().await?;
};
if !status.success() {
return Err(NativeError::Wine(WineError::Command {
output: output_str,
exit: status,
}));
}
Ok(output_str.to_string())
}
pub(crate) async fn install_wine() -> Result<(), NativeError> {
let release = get_wine_release()?;
let asset = match release
.assets
.iter()
.find(|x| PROTON_PATTERN.captures(&x.name).is_some())
{
Some(asset) => asset,
None => return Err(NativeError::Wine(WineError::Fetch)),
};
let dir = maxima_cache_dir()?.join("downloads");
create_dir_all(&dir)?;
let archive_path = dir.join(&asset.name);
github_download_asset(asset, &archive_path)?;
extract_wine(&archive_path)?;
let mut versions = versions()?;
versions.proton = release.tag_name;
set_versions(versions)?;
if let Err(err) = remove_file(&archive_path) {
warn!("Failed to delete {:?} - {:?}", archive_path, err);
}
let _ = run_wine_command("", None::<[&str; 0]>, None, false, CommandType::Run).await;
Ok(())
}
fn extract_wine(archive_path: &PathBuf) -> Result<(), NativeError> {
info!("Extracting proton...");
let dir = proton_dir()?;
if dir.exists() {
remove_dir_all(&dir)?;
}
create_dir_all(&dir)?;
let archive_file = File::open(archive_path)?;
let archive_decoder = GzDecoder::new(archive_file);
let archive = Archive::new(archive_decoder);
extract_archive(dir, archive)
}
fn extract_archive<R: Read + Sized>(
dir: PathBuf,
mut archive: Archive<R>,
) -> Result<(), NativeError> {
for entry in archive.entries()? {
let mut entry = entry?;
let entry_path = entry.path()?;
let next = match entry_path.components().next() {
Some(next) => next,
None => {
return Err(NativeError::PathComponentNext(entry_path.clone().into()));
}
};
let destination_path = dir.join(entry_path.strip_prefix(next)?);
if let Some(parent_dir) = destination_path.parent() {
create_dir_all(parent_dir)?;
}
entry.unpack(destination_path)?;
}
Ok(())
}
pub async fn setup_wine_registry() -> Result<(), NativeError> {
let mut reg_content = "Windows Registry Editor Version 5.00\n\n".to_string();
// This supports text values only at the moment
// if you need a dword - implement it
let entries: &[(&str, &[(&str, &str)])] = &[
(
"HKEY_LOCAL_MACHINE\\Software\\Electronic Arts\\EA Desktop",
&[("InstallSuccessful", "true")],
),
(
"HKEY_LOCAL_MACHINE\\Software\\Electronic Arts\\Origin",
&[
("InstallSuccessful", "true"),
("ClientPath", "C:/Windows/System32/conhost.exe"),
],
),
(
"HKEY_LOCAL_MACHINE\\Software\\Wow6432Node\\Electronic Arts\\EA Desktop",
&[("InstallSuccessful", "true")],
),
(
"HKEY_LOCAL_MACHINE\\Software\\Wow6432Node\\Electronic Arts\\Origin",
&[
("InstallSuccessful", "true"),
("ClientPath", "C:/Windows/System32/conhost.exe"),
],
),
];
for (key, values) in entries.into_iter() {
reg_content.push_str(&format!("[{}]\n", key));
for (name, value) in values.into_iter() {
let value = value.replace("\\", "\\\\");
reg_content.push_str(&format!("\"{}\"=\"{}\"\n\n", name, value));
}
}
let path = maxima_cache_dir()?.join("wine.reg");
tokio::fs::create_dir_all(path.safe_parent()?).await?;
{
let mut reg_file = tokio::fs::File::create(&path).await?;
reg_file.write_all(reg_content.as_bytes()).await?;
}
run_wine_command(
"regedit",
Some(vec![path.safe_str()?]),
None,
false,
CommandType::Run,
)
.await?;
tokio::fs::remove_file(path).await?;
Ok(())
}
pub type WineRegistry = HashMap<String, String>;
lazy_static! {
static ref MX_WINE_REGISTRY: Mutex<WineRegistry> = Mutex::new(WineRegistry::new());
}
async fn parse_wine_registry(file_path: &str) -> WineRegistry {
let mut registry_map = MX_WINE_REGISTRY.lock().await;
if !registry_map.is_empty() {
return registry_map.clone();
}
let file = tokio::fs::File::open(file_path)
.await
.expect("Could not open file");
let reader = BufReader::new(file);
let mut current_section = String::new();
let mut lines = reader.lines();
while let Some(line) = lines.next_line().await.expect("Failed to read file") {
let trimmed_line = line.trim();
if trimmed_line.starts_with('[') && trimmed_line.contains(']') {
if let Some(end) = trimmed_line.find(']') {
current_section = trimmed_line[1..end].to_string();
}
} else if trimmed_line.contains('=') && trimmed_line.starts_with('"') {
let parts: Vec<&str> = trimmed_line.splitn(2, '=').collect();
if parts.len() == 2 {
let key = parts[0].trim_matches('"').to_string();
let value = parts[1].trim_matches('"').to_string();
let full_key = format!("{}\\{}", current_section, key).replace("\\\\", "\\");
registry_map.insert(full_key.to_lowercase(), value);
}
}
}
registry_map.clone()
}
pub async fn parse_mx_wine_registry() -> Result<WineRegistry, NativeError> {
let path = wine_prefix_dir()?.join("system.reg");
if !path.exists() {
return Ok(HashMap::new());
}
Ok(parse_wine_registry(path.safe_str()?).await)
}
pub async fn invalidate_mx_wine_registry() {
MX_WINE_REGISTRY.lock().await.clear();
}
fn normalize_key(key: &str) -> String {
let lower_key = key.to_lowercase();
if lower_key.starts_with("hkey_local_machine\\") {
lower_key
.trim_start_matches("hkey_local_machine\\")
.to_string()
} else {
lower_key
}
}
pub async fn get_mx_wine_registry_value(query_key: &str) -> Result<Option<String>, RegistryError> {
let registry_map = parse_mx_wine_registry().await?;
let normalized_query_key = normalize_key(query_key);
let value = if let Some(value) = registry_map.get(&normalized_query_key) {
Some(value.clone())
} else {
let wow6432_query_key =
normalized_query_key.replace("software\\", "software\\wow6432node\\");
registry_map.get(&wow6432_query_key).cloned()
};
Ok(value.map(|x| x.replace("Z:", "").replace("\\", "/")))
}