This repository was archived by the owner on Feb 20, 2026. It is now read-only.
forked from Luuxis/minecraft-java-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatcher.ts
More file actions
179 lines (150 loc) · 5.22 KB
/
patcher.ts
File metadata and controls
179 lines (150 loc) · 5.22 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
/**
* This code is distributed under the CC-BY-NC 4.0 license:
* https://creativecommons.org/licenses/by-nc/4.0/
*
* Original author: Luuxis
* Fork author: Benjas333
*/
import { spawn } from 'child_process';
import fs from 'fs';
import path from 'path';
import { EventEmitter } from 'events';
import { getPathLibraries, getFileFromArchive } from '../utils/Index.js';
interface ForgePatcherOptions {
path: string;
loader: {
type: string;
};
}
interface Config {
java: string;
minecraft: string;
minecraftJson: string;
}
interface ProfileData {
client: string;
[key: string]: any;
}
interface Processor {
jar: string;
args: string[];
classpath: string[];
sides?: string[];
}
export interface Profile {
data: Record<string, ProfileData>;
processors?: any[];
libraries?: Array<{ name?: string }>; // The universal jar/libraries reference
path?: string;
}
export default class ForgePatcher extends EventEmitter {
private readonly options: ForgePatcherOptions;
constructor(options: ForgePatcherOptions) {
super();
this.options = options;
}
public async patcher(profile: Profile, config: Config, neoForgeOld: boolean = true): Promise<void> {
const { processors } = profile;
for (const [_, processor] of Object.entries(processors)) {
if (processor.sides && !processor.sides.includes('client')) continue;
const jarInfo = getPathLibraries(processor.jar);
const jarPath = path.resolve(this.options.path, 'libraries', jarInfo.path, jarInfo.name);
const args = processor.args
.map(arg => this.setArgument(arg, profile, config, neoForgeOld))
.map(arg => this.computePath(arg));
const classPaths = processor.classpath.map(cp => {
const cpInfo = getPathLibraries(cp);
return `"${path.join(this.options.path, 'libraries', cpInfo.path, cpInfo.name)}"`;
});
let mainClass: string | null = null;
try {
mainClass = await this.readJarManifest(jarPath);
} catch (error) {
this.emit('error', `Failed to read the JAR manifest: ${error.message || error}`);
continue;
}
if (!mainClass) {
this.emit('error', `Impossible to determine the main class in the JAR: ${jarPath}`);
continue;
}
await new Promise<void>((resolve) => {
const spawned = spawn(
`"${path.resolve(config.java)}"`,
[
'-classpath',
[`"${jarPath}"`, ...classPaths].join(path.delimiter),
mainClass,
...args
],
{ shell: true }
);
spawned.stdout.on('data', data => {
this.emit('patch', data.toString('utf-8'));
});
spawned.stderr.on('data', data => {
this.emit('patch', data.toString('utf-8'));
});
spawned.on('close', code => {
if (code !== 0) this.emit('error', `Forge patcher exited with code: ${code}`);
resolve();
});
});
}
}
public check(profile: Profile): boolean {
const { processors } = profile;
let files: string[] = [];
for (const processor of Object.values(processors)) {
if (processor.sides && !processor.sides.includes('client')) continue;
processor.args.forEach(arg => {
const finalArg = arg.replace('{', '').replace('}', '');
if (profile.data[finalArg]) {
if (finalArg === 'BINPATCH') return;
files.push(profile.data[finalArg].client);
}
});
}
files = Array.from(new Set(files));
for (const file of files) {
const lib = getPathLibraries(file.replace('[', '').replace(']', ''));
const filePath = path.resolve(this.options.path, 'libraries', lib.path, lib.name);
if (!fs.existsSync(filePath)) return false;
}
return true;
}
private setArgument(arg: string, profile: Profile, config: Config, neoForgeOld: boolean): string {
const finalArg = arg.replace('{', '').replace('}', '');
const universalLib = profile.libraries.find(lib => {
if (this.options.loader.type === 'forge') return lib.name.startsWith('net.minecraftforge:forge');
else return lib.name.startsWith(neoForgeOld ? 'net.neoforged:forge' : 'net.neoforged:neoforge');
});
if (profile.data[finalArg]) {
if (finalArg === 'BINPATCH') {
const jarInfo = getPathLibraries(profile.path || (universalLib?.name ?? ''));
return `"${path.join(this.options.path, 'libraries', jarInfo.path, jarInfo.name).replace('.jar', '-clientdata.lzma')}"`;
}
return profile.data[finalArg].client;
}
return arg
.replace('{SIDE}', 'client')
.replace('{ROOT}', `"${path.dirname(path.resolve(this.options.path, 'forge'))}"`)
.replace('{MINECRAFT_JAR}', `"${config.minecraft}"`)
.replace('{MINECRAFT_VERSION}', `"${config.minecraftJson}"`)
.replace('{INSTALLER}', `"${path.join(this.options.path, 'libraries')}"`)
.replace('{LIBRARY_DIR}', `"${path.join(this.options.path, 'libraries')}"`);
}
private computePath(arg: string): string {
if (arg.startsWith('[')) {
const libInfo = getPathLibraries(arg.replace('[', '').replace(']', ''));
return `"${path.join(this.options.path, 'libraries', libInfo.path, libInfo.name)}"`;
}
return arg;
}
private async readJarManifest(jarPath: string): Promise<string | null> {
const manifestContent: any = await getFileFromArchive(jarPath, 'META-INF/MANIFEST.MF');
if (!manifestContent) return null;
const mainClassLine = manifestContent.toString().split('Main-Class: ')[1];
if (!mainClassLine) return null;
return mainClassLine.split('\r\n')[0];
}
}