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
use std::collections::HashMap;
use std::ffi::OsString;
use std::os::unix::prelude::OsStringExt;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use serde::{Serialize, Deserialize};
pub mod ext;
mod shared_libraries;
pub use shared_libraries::{
Wine as WineSharedLibs,
Gstreamer as GstreamerSharedLibs
};
#[cfg(feature = "wine-bundles")]
pub mod bundle;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum WineArch {
/// 32 bit only wine.
Win32,
/// 64 bit only wine.
Win64,
/// 64 bit wine which can run 32 bit apps (since 10.0 release).
Wow64
}
impl WineArch {
#[allow(clippy::should_implement_trait)]
#[inline]
pub fn from_str(arch: impl AsRef<str>) -> Option<Self> {
match arch.as_ref() {
"win32" | "x32" | "32" => Some(Self::Win32),
"win64" | "x64" | "64" => Some(Self::Win64),
"wow64" => Some(Self::Wow64),
_ => None
}
}
#[inline]
pub const fn to_str(&self) -> &str {
match self {
Self::Win32 => "win32",
Self::Win64 => "win64",
Self::Wow64 => "wow64"
}
}
}
impl Default for WineArch {
#[inline]
fn default() -> Self {
Self::Win64
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WineBoot {
/// Path to `wineboot` execution script (packaged with some custom wine builds)
Unix(PathBuf),
/// Path to `wineboot.exe` executable (available in wine prefix / lib folder)
Windows(PathBuf)
}
#[allow(clippy::from_over_into)]
impl Into<PathBuf> for WineBoot {
#[inline]
fn into(self) -> PathBuf {
match self {
WineBoot::Unix(path) |
WineBoot::Windows(path) => path
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WineLoader {
/// Set `WINELOADER` variable as binary specified in `Wine` struct
Current,
/// Don't set `WINELOADER` variable, so wine will try to use system-wide binary
Default,
/// Set custom `WINELOADER` variable
Custom(PathBuf)
}
impl Default for WineLoader {
#[inline]
fn default() -> Self {
Self::Default
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Wine {
/// Path to the wine binary
pub binary: PathBuf,
/// Specifies `WINEPREFIX` variable
pub prefix: PathBuf,
/// Specifies `WINEARCH` variable
pub arch: WineArch,
/// Path to wineboot binary
pub wineboot: Option<WineBoot>,
/// Specifies `WINESERVER` variable
pub wineserver: Option<PathBuf>,
/// Specifies `WINELOADER` variable
pub wineloader: WineLoader,
/// Describes which `LD_LIBRARY_PATH` value should be used
pub wine_libs: WineSharedLibs,
/// Describes which `GST_PLUGIN_PATH` value should be used
///
/// https://gstreamer.freedesktop.org/documentation/gstreamer/gstregistry.html?gi-language=c
pub gstreamer_libs: GstreamerSharedLibs
}
impl Default for Wine {
#[inline]
fn default() -> Self {
Self::from_binary("wine")
}
}
impl AsRef<Wine> for Wine {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}
impl Wine {
#[inline]
pub fn from_binary(binary: impl Into<PathBuf>) -> Self {
Self {
binary: binary.into(),
prefix: PathBuf::from(std::env::var("HOME")
.unwrap_or_else(|_| format!("/home/{}", std::env::var("USER").unwrap())))
.join(".wine"),
arch: WineArch::Win64,
wineboot: None,
wineserver: None,
wineloader: WineLoader::default(),
wine_libs: WineSharedLibs::default(),
gstreamer_libs: GstreamerSharedLibs::default()
}
}
/// Try to get version of provided wine binary. Runs command: `wine --version`
///
/// ```
/// use wincompatlib::prelude::*;
///
/// match Wine::default().version() {
/// Ok(version) => println!("Wine version: {:?}", version),
/// Err(err) => eprintln!("Wine is not available: {}", err)
/// }
/// ```
pub fn version(&self) -> anyhow::Result<OsString> {
let output = Command::new(&self.binary)
.arg("--version")
.stdout(Stdio::piped())
.stderr(Stdio::null())
.output()?;
Ok(OsString::from_vec(output.stdout))
}
fn get_inner_binary(&self, binary: &str) -> Option<PathBuf> {
if let Some(parent) = self.binary.parent() {
// [wine folder]/bin/[binary]
let binary_path = parent.join(binary);
if binary_path.exists() {
return Some(binary_path);
}
if let Some(parent) = parent.parent() {
let windows = match self.arch {
WineArch::Win32 => parent.join("lib/wine/i386-windows"),
WineArch::Win64 | WineArch::Wow64 => parent.join("lib64/wine/x86_64-windows")
};
// [wine folder]/lib/wine/i386-windows/[binary]
// [wine folder]/lib64/wine/x86_64-windows/[binary]
let binary_path = windows.join(binary);
if binary_path.exists() {
return Some(binary_path);
}
// [wine folder]/lib/wine/i386-windows/[binary].exe
// [wine folder]/lib64/wine/x86_64-windows/[binary].exe
let binary_path = windows.join(format!("{}.exe", binary));
if binary_path.exists() {
return Some(binary_path);
}
}
}
None
}
/// Try to get path to wineboot binary
///
/// If wine binary is specified (so not system), then function will try to find wineboot binary inside of this wine's folder
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// use std::path::PathBuf;
///
/// // Build with wineboot script
/// assert_eq!(Wine::from_binary("wine_folder/bin/wine").wineboot(), Some(WineBoot::Unix(PathBuf::from("wine_folder/bin/wineboot"))));
///
/// // Build without wineboot script, but with wineboot.exe file
/// assert_eq!(Wine::from_binary("wine_folder/bin/wine").wineboot(), Some(WineBoot::Windows(PathBuf::from("wine_folder/lib64/wine/x86_64-windows/wineboot.exe"))));
///
/// // Build without wineboot script and wineboot.exe file, but this file exists in wine prefix
/// assert_eq!(Wine::from_binary("wine_folder/bin/wine").with_prefix("path/to/prefix").wineboot(), Some(WineBoot::Windows(PathBuf::from("path/to/prefix/drive_c/windows/system32/wineboot.exe"))));
///
/// // Builds without any wineboot version
/// assert_eq!(Wine::from_binary("wine_folder/bin/wine").wineboot(), None);
/// ```
pub fn wineboot(&self) -> Option<WineBoot> {
if let Some(wineboot) = &self.wineboot {
return Some(wineboot.to_owned());
}
if let Some(wineboot) = self.get_inner_binary("wineboot") {
if let Some(ext) = wineboot.extension() {
if ext == "exe" {
return Some(WineBoot::Windows(wineboot));
}
}
return Some(WineBoot::Unix(wineboot));
}
let wineboot = self.prefix.join("drive_c/windows/system32/wineboot.exe");
wineboot.exists().then_some(WineBoot::Windows(wineboot))
}
#[inline]
/// Get path to wineserver binary, or "wineserver" if not specified
///
/// If wine binary is specified (so not system), then function will try to find wineserver binary inside of this wine's folder
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// use std::path::PathBuf;
///
/// // Build with wineserver
/// assert_eq!(Wine::from_binary("wine_build_with_wineserver/wine").wineserver(), PathBuf::from("wine_build_with_wineserver/wineserver"));
///
/// // Build without wineserver
/// assert_eq!(Wine::from_binary("wine_build_without_wineserver/wine").wineserver(), PathBuf::from("wineserver"));
/// ```
pub fn wineserver(&self) -> PathBuf {
self.wineserver.clone()
.unwrap_or_else(|| self.get_inner_binary("wineserver")
.unwrap_or_else(|| PathBuf::from("wineserver")))
}
#[inline]
/// Get path to wine binary, or "wine" if not specified (`WineLoader::Default`)
pub fn wineloader(&self) -> &Path {
match &self.wineloader {
WineLoader::Default => Path::new("wine"),
WineLoader::Current => self.binary.as_path(),
WineLoader::Custom(path) => path
}
}
/// Get environment variables map from current struct's values
///
/// Can contain (if specified in current struct):
///
/// - `WINEPREFIX`
/// - `WINEARCH`
/// - `WINESERVER`
/// - `WINELOADER`
/// - `LD_LIBRARY_PATH`
/// - `GST_PLUGIN_PATH`
///
/// ```
/// use wincompatlib::prelude::*;
///
/// use std::process::Command;
///
/// let wine = Wine::default().with_arch(WineArch::Win64);
///
/// Command::new(&wine.binary)
/// .envs(wine.get_envs())
/// .spawn();
/// ```
pub fn get_envs(&self) -> HashMap<&str, OsString> {
let mut env = HashMap::new();
env.insert("WINEPREFIX", self.prefix.as_os_str().to_os_string());
env.insert("WINEARCH", self.arch.to_str().into());
if let Some(server) = &self.wineserver {
env.insert("WINESERVER", server.as_os_str().to_os_string());
}
match &self.wineloader {
WineLoader::Default => (),
WineLoader::Current => {
env.insert("WINELOADER", self.binary.as_os_str().to_os_string());
},
WineLoader::Custom(path) => {
env.insert("WINELOADER", path.as_os_str().to_os_string());
}
}
if let Some(path) = self.wine_libs.get_paths() {
env.insert("LD_LIBRARY_PATH", OsString::from(path));
}
if let Some(path) = self.gstreamer_libs.get_paths() {
env.insert("GST_PLUGIN_PATH", OsString::from(path));
}
env
}
#[cfg(feature = "dxvk")]
#[inline]
/// Run `Dxvk::install` with parameters from current Wine struct. Will try to use system-wide binaries if some not specified
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// Wine::from_binary("/path/to/wine")
/// .with_arch(WineArch::Win64)
/// .install_dxvk("/path/to/dxvk-2.1", InstallParams::default())
/// .expect("Failed to install DXVK 2.1");
/// ```
pub fn install_dxvk<T: Into<PathBuf>>(&self, dxvk_folder: T, params: super::dxvk::InstallParams) -> anyhow::Result<()> {
super::dxvk::Dxvk::install(self, dxvk_folder, params)
}
#[cfg(feature = "dxvk")]
#[inline]
/// Run `Dxvk::uninstall` with parameters from current Wine struct. Will try to use system-wide binaries if some not specified
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// Wine::from_binary("/path/to/wine")
/// .with_arch(WineArch::Win64)
/// .uninstall_dxvk(InstallParams::default())
/// .expect("Failed to uninstall DXVK");
/// ```
pub fn uninstall_dxvk(&self, params: super::dxvk::InstallParams) -> anyhow::Result<()> {
super::dxvk::Dxvk::uninstall(self, params)
}
}