Skip to content

Commit 15db21b

Browse files
committed
feat: support moonlight mod loading
1 parent b3abe33 commit 15db21b

6 files changed

Lines changed: 138 additions & 23 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
edition = "2021"
33
name = "modhook"
4-
version = "1.0.1"
4+
version = "1.0.2"
55

66
[lib]
77
crate-type = ["cdylib"]

README.md

Lines changed: 103 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
An in-memory mod loader for Discord.
44

5-
# Note about 64-bit vs 32-bit Discord.
5+
# Note about 64-bit & 32-bit Discord.
66

7-
Discord recently released x64 builds of all their clients.
8-
For this program to work, it must be compiled for the same architecture that your Discord runs on.
7+
This modloader currently only supports 64-bit Discord. If you aren't currently using the 64-bit version of Discord, make sure you download the 64-bit version from the Discord website.
98

10-
If you go into account settings and scroll to the bottom of the sidebar, you should see `Host x.x.xxx x64` or `Host x.x.xxx x86`.
11-
12-
Make sure to get the right build for this architecture.
9+
You can check your architecture in Discord settings. Scroll to the bottom of the sidebar and look at "Host".
10+
If it's 64-bit, it'll say x64.
1311

1412
# Usage
1513

@@ -19,28 +17,114 @@ Make sure this directory doesn't change, as any shortcuts you make need to point
1917
If you right-click and drag `modhook.exe`, when you release the mouse button there will be an option to create a shortcut.
2018
Alternatively, you can always make a shortcut manually.
2119

22-
## Commandline Arguments
20+
If you are using a shortcut, the "Start In" directory must be the folder the ModHook executable is in.
21+
22+
## Example Usage: Vencord
2323

24-
As an example of what I use...
24+
Change the `-d` flag if you want to use a different Discord branch.
2525

2626
```
27-
"C:\Users\megu\Workspace\Discord\ModHook\ModHook-RS\target\release\modhook.exe" -d "C:\Users\megu\AppData\Local\Discord" -m "c:\users\megu\workspace\discord\vencord\dist\patcher.js"
27+
"C:\path\to\modhook.exe"
28+
-d "C:\Users\megu\AppData\Local\Discord"
29+
-m "c:\path\to\vencord\dist\patcher.js"
2830
```
2931

30-
And here's the documentation of all the flags...
32+
## Example Usage: Moonlight
3133

3234
```
33-
C:\path\to\modhook.exe
34-
-d "c:\users\megu\appdata\local\discord" # This is the path to the folder Discord is installed to.
35-
-m "c:\users\megu\downloads\vencord\dist\patcher.js" # This is the path to your mod's entrypoint file.
36-
-f "_app.asar" # This isn't needed if you're using Vencord, Replugged, or any mod that uses "_app.asar" or "app.orig.asar"
37-
# Below are optional, and probably not needed.
38-
-t "c:\users\megu\downloads\vencord\dist\patcher.js" # This is the query used to see if the mod is loaded. This defaults to the value of `-m`
39-
-c "MyAlternativeProfile" # A unique name that lets you have multiple instances of discord with unique profiles
40-
-a "c:\users\megu\downloads\ModHook\app.asar" # The path to the custom app.asar loader.
41-
-h # This just shows a help command in case you get lost.
35+
"C:\path\to\modhook.exe"
36+
-d "C:\Users\megu\AppData\Local\Discord"
37+
-m "c:\path\to\moonlight\dist\injector.js"
4238
```
4339

40+
## All configuration flags
41+
42+
Names prefixed with a \* are required.
43+
44+
| Flag | Name | Purpose | Example |
45+
| ----------- | ---------------- | -------------------------------------------------------- | ---------------------------------------- |
46+
| -d | \*Directory | The path to the Discord AppData folder | -d "c:\Users\megu\AppData\Local\Discord" |
47+
| -m | \*Mod Entrypoint | The path to the Mod Entrypoint | -m "c:\path\to\vencord\dist\patcher.js" |
48+
| --moonlight | Moonlight | Support for the Moonlight Mod | --moonlight |
49+
| -f | ASAR Filename | The name of the _modded_ ASAR | -f "\_app.asar" |
50+
| -t | Toggle Query | Toggle asar redirection when this query is hit | -t "\injector.js" |
51+
| -a | Custom ASAR | Use a custom ASAR instead of the modhook-provided one | -a c:\path\to\my_app.asar |
52+
| -c | Custom UserDir | Use a custom user directory. Helpful for multi-instances | -c "my-custom-userdir" |
53+
| -h | Help | Shows the Help command | -h |
54+
55+
### -d (\*Directory)
56+
57+
This is the path to your Discord AppData folder.
58+
59+
Usage: `-d <path>`
60+
61+
Common values are:
62+
63+
- `C:\Users\<username>\AppData\Local\Discord`
64+
- `C:\Users\<username>\AppData\Local\DiscordPTB`
65+
- `C:\Users\<username>\AppData\Local\DiscordCanary`
66+
- `C:\Users\<username>\AppData\Local\DiscordDevelopment`
67+
68+
### -m (\*Mod Entrypoint)
69+
70+
This is the path to the entrypoint of the Discord mod.
71+
72+
Usage: `-m <path>`
73+
74+
Common values are:
75+
76+
- `C:\path\to\vencord\dist\patcher.js`
77+
- `C:\path\to\moonlight\dist\injector.js`
78+
79+
### --moonlight (Moonlight)
80+
81+
Use this flag if you're loading Moonlight.
82+
83+
Moonlight's entrypoint works slightly differently to most, so this is needed to load it.
84+
85+
Usage: `--moonlight`
86+
87+
### -f (ASAR Filename)
88+
89+
The name of the modded ASAR.
90+
91+
Many mods rename the original ASAR to `_app.asar` or `app.orig.asar` or similar.
92+
If your mod does not use one of these, you need to provide it with this flag.
93+
94+
Usage: `-f <asar name>`
95+
96+
### -t (Toggle Query)
97+
98+
The query to use to toggle ASAR redirection.
99+
100+
ModHook redirects all calls to `app.asar/index.js` to a custom asar that load the mod.
101+
102+
Once the mod is loaded, we want to disable this redirection so that Discord can load.
103+
104+
This is only needed if for some reason, the query should be something different to the mod entrypoint (-m).
105+
106+
Usage: `-t c:\my\custom\query`
107+
108+
### -a (Custom ASAR)
109+
110+
Custom ASAR to laod, instead of the default ModHook one.
111+
112+
If you want to use your own ASAR instead of the ModHook one, use this flag.
113+
114+
Usage: `-a c:\path\to\custom.asar`
115+
116+
### -c (Custom User Directory)
117+
118+
Allows you to have a custom User Directory for your instance.
119+
120+
This means that instead of being in `%AppData%\Discord`, it'll be in `%AppData%\DiscordModHook\AppData\<custom user dir>`.
121+
122+
This allows two things:
123+
124+
- Each custom directory will have it's own profile, so you can have multiple accounts on the same branch.
125+
- This means that you will have to log in for each custom User Directory you use.
126+
- Each directory stores all the electron cache - this makes it safe to run multiple instances of the same branch.
127+
44128
# Setup
45129

46130
You'll need rust...

asar/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/// BEGIN MODHOOK INFO ///
22
const CUSTOM_DATA_DIR = process.env.MODHOOK_CUSTOM_DATA_DIR;
33
const MOD_ENTRYPOINT = process.env.MODHOOK_MOD_ENTRYPOINT;
4+
const IS_MOONLIGHT = process.env.MODHOOK_IS_MOONLIGHT;
45
/// END MODHOOK INFO ///
56

67
if (CUSTOM_DATA_DIR) {
78
const { app } = require("electron");
8-
const customAppDir = app.getPath("appData") + "\\DiscordModHook\\AppData\\" + CUSTOM_DATA_DIR;
9+
const customAppDir =
10+
app.getPath("appData") + "\\DiscordModHook\\AppData\\" + CUSTOM_DATA_DIR;
911
const _setPath = app.setPath;
1012

1113
app.setPath = function (name, path) {
@@ -19,4 +21,10 @@ if (CUSTOM_DATA_DIR) {
1921
app.setPath("userData", customAppDir);
2022
}
2123

22-
require(MOD_ENTRYPOINT);
24+
if (IS_MOONLIGHT) {
25+
require(MOD_ENTRYPOINT).inject(
26+
require("path").resolve(__dirname, "..\\_app.asar")
27+
);
28+
} else {
29+
require(MOD_ENTRYPOINT);
30+
}

src/env.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ pub struct Environment {
3131
///
3232
/// e.g. "_app.asar"
3333
pub modded_asar_filename: Option<String>,
34+
35+
/// Whether or not the mod is the moonlight mod.
36+
///
37+
/// Moonlight uses `require(entrypoint).inject(asarPath);`
38+
/// instead of the usual `require(entrypoint);`
39+
pub is_moonlight: bool,
3440
}
3541

3642
#[allow(dead_code)]
@@ -43,6 +49,7 @@ impl Environment {
4349
toggle_query: None,
4450
custom_data_dir: None,
4551
modded_asar_filename: None,
52+
is_moonlight: false,
4653
};
4754

4855
if let Ok(path) = std::env::var("MODHOOK_ASAR_PATH") {
@@ -68,6 +75,10 @@ impl Environment {
6875
env.modded_asar_filename = Some("_app.asar".to_string());
6976
}
7077

78+
if let Ok(is_moonlight) = std::env::var("MODHOOK_IS_MOONLIGHT") {
79+
env.is_moonlight = is_moonlight == "true";
80+
}
81+
7182
env
7283
}
7384

@@ -99,5 +110,9 @@ impl Environment {
99110
// Currently supported mods:
100111
// - Vencord
101112
std::env::set_var("DISABLE_UPDATER_AUTO_PATCHING", "true");
113+
114+
if self.is_moonlight {
115+
std::env::set_var("MODHOOK_IS_MOONLIGHT", "true");
116+
}
102117
}
103118
}

src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ pub struct Args {
5454
/// Example: --asar-path "c:\users\megu\vencord\app.asar"
5555
#[arg(short, long, verbatim_doc_comment)]
5656
pub asar_path: Option<String>,
57+
58+
/// Whether or not the mod is the moonlight mod.
59+
/// Example: --moonlight
60+
/// Moonlight uses `require(entrypoint).inject(asarPath);`
61+
/// instead of the usual `require(entrypoint);`
62+
#[arg(value_enum, long = "moonlight", verbatim_doc_comment)]
63+
pub is_moonlight: bool,
5764
}
5865

5966
fn main() {
@@ -77,6 +84,7 @@ fn main() {
7784
toggle_query: args.toggle_query,
7885
custom_data_dir: args.custom_data_dir,
7986
modded_asar_filename: args.modded_asar_filename,
87+
is_moonlight: args.is_moonlight,
8088
};
8189

8290
unsafe {

0 commit comments

Comments
 (0)