electron_hook/
lib.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
#![warn(missing_docs)]
//! A library for modding Electron apps in-memory, without modifying any program files.
//!
//! This library was made for improving the modding experience for Discord, but it can be used for any Electron app.
//!
//! # Features
//!
//! - `asar`: Enables the ASAR archive builder. (enabled by default)
//! - `uuid`: Enables the use of random UUIDs for ASAR archive names. (enabled by default)
//!
//! # Examples
//!
//! electron-hook maps the original `app.asar` to `_app.asar`,
//! so keep this in mind if you need to call the original file anywhere,
//! as shown in this example.
//!
//! ```rust,ignore
//! use electron_hook::asar::Asar;
//!
//! let mod_dir = mod_artifact_dir("moonlight");
//!
//! let _download_url = "https://github.com/moonlight-mod/moonlight/releases/latest/download/dist.tar.gz";
//! // extract and save `_download_url` into `mod_dir`
//!
//! let mod_entrypoint = mod_dir.join("injector.js");
//!
//! let template = r#"
//!     console.log("Mod injected!!!");
//!     let asar = require("path").resolve(__dirname, "../_app.asar");
//!     require("$ENTRYPOINT").inject(asar);
//! "#;
//!
//! // Create the asar file
//! let asar = Asar::new()
//!     .with_id("moonlight")
//!     .with_template(template)
//!     .with_mod_entrypoint(mod_dir)
//!     .create()
//!     .unwrap();
//!
//! electron_hook::launch(
//!     "/path/to/executable/Discord",
//!     asar.path().to_str().unwrap(),
//!     vec!["--pass-arguments-here"],
//!     None, // Optional profile directory
//!     true, // Detach the process
//! );
//! ```

#[cfg(any(doc, feature = "asar"))]
pub mod asar;
pub mod paths;

// For Linux
#[cfg(target_os = "linux")]
mod linux;

// For Windows
// TODO: Re-implement Windows support.
#[cfg(target_os = "windows")]
mod windows;

// TODO: For MacOS

/// Launches an Electron executable with the provided information.
///
/// On Windows, the `executable_or_directory` parameter is the path to the directory containing `Update.exe`.
///
/// On Linux, the `executable_or_directory` parameter is the path to the executable.
///
/// It is recommended to set `detach` to true to prevent the process from dying when the parent process is closed.
#[allow(unused_variables)]
pub fn launch(
    executable: &str,
    library_path: &str,
    asar_path: &str,
    args: Vec<String>,
    detach: bool,
) -> Result<Option<u32>, String> {
    #[cfg(target_os = "linux")]
    {
        linux::launch(executable, library_path, asar_path, args, detach)
    }

    #[cfg(target_os = "windows")]
    {
        // No need for detach on Windows, as the process already detaches itself.
        windows::launch(executable, library_path, asar_path, args)
    }
}