forked from xcf-t/minecraft-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.ts
283 lines (222 loc) · 8.97 KB
/
app.ts
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
import {ForgeVersion, MinecraftVersion} from "./utils/Versions";
import * as path from 'path';
import Downloader from "./utils/Downloader";
import {LibraryManager} from "./utils/Libraries";
import {AssetManager} from "./utils/Assets";
import {child_process, fs} from 'mz';
import * as mkdirp from 'mkdirp';
import * as nbt from 'nbt';
import {Authentication, AuthenticationResult} from "./utils/Authentication";
import {ForgeVersionDescription, ForgeVersionType} from "./utils/Manifests";
import {CustomForgeMod, CurseForgeMod, ForgeMod} from "./utils/Mods";
import {InstallationProgress} from "./utils/InstallationProgress";
export {Authentication, AuthenticationResult} from "./utils/Authentication";
export {ForgeVersion, MinecraftVersion} from "./utils/Versions";
export {InstallationProgress} from "./utils/InstallationProgress";
export {CurseForgeMod, CustomForgeMod, ForgeMod} from "./utils/Mods";
export {ForgeVersionDescription, ForgeVersionType} from "./utils/Manifests";
export class MinecraftClient {
version: MinecraftVersion;
options: ClientOptions;
forge: ForgeVersion;
nativeDir: string;
progress: InstallationProgress;
libraryManager: LibraryManager;
assetManager: AssetManager;
private constructor(version: MinecraftVersion, forge?: ForgeVersion, options: ClientOptions = MinecraftClient.defaultConfig, progress?: InstallationProgress) {
for(let i in MinecraftClient.defaultConfig)
if(!options[i])
options[i] = MinecraftClient.defaultConfig[i];
this.options = options;
this.version = version;
this.forge = forge;
this.libraryManager = new LibraryManager(options, version);
this.assetManager = new AssetManager(options, version);
this.progress = progress || InstallationProgress.callback();
}
private static readonly defaultConfig: ClientOptions = {
javaExecutable: 'java',
features: {}
};
public static getMinecraftClient(version: string | MinecraftVersion, options: ClientOptions, progress?: InstallationProgress): Promise<MinecraftClient | null> {
return this.getClient(version, null, options, progress);
}
public static getForgeClient(version: string | MinecraftVersion, forge: ForgeVersionType | ForgeVersionDescription, options: ClientOptions, progress?: InstallationProgress): Promise<MinecraftClient | null> {
return this.getClient(version, forge, options, progress);
}
public static async getClient(version: string | MinecraftVersion, forge: ForgeVersionType | ForgeVersionDescription, options: ClientOptions, progress?: InstallationProgress): Promise<MinecraftClient | null> {
let mcVersion: MinecraftVersion;
if(typeof version === 'string') {
mcVersion = <MinecraftVersion>await MinecraftVersion.getVersion(<string>version, options);
} else {
mcVersion = <MinecraftVersion>version;
}
let forgeVersion: ForgeVersion;
if(forge) {
if(forge === "recommended" || forge === "latest")
forgeVersion = <ForgeVersion>await ForgeVersion.getPromotedVersion(mcVersion, forge);
else {
let version: string = forge; //14.23.4.2709
let build: number; // [14, 23, 4, 2709].reverse() => [2709,4,23,14][0] => 2709
if(version.indexOf('.') === -1)
return null; // failsafe?
build = parseInt(version.split('\.').reverse()[0]);
forgeVersion = await ForgeVersion.getCustomVersion(build, version, mcVersion);
}
}
if(!mcVersion)
return null;
return new MinecraftClient(mcVersion, forgeVersion, options, progress);
}
public async checkInstallation(): Promise<void> {
this.progress.step("Installing Libraries");
await this.libraryManager.installMinecraftLibraries(this.progress);
if(this.forge) {
this.progress.step("Installing Forge Libraries");
await this.libraryManager.installForgeLibraries(this.forge, this.progress);
}
this.progress.step("Installing Assets");
await this.assetManager.install(this.progress);
}
private async loadServersDat(): Promise<ServersDatItem[]> {
const serversFilePath = path.join(this.options.gameDir, "servers.dat");
let serversDat = null;
try {
serversDat = fs.readFileSync(serversFilePath);
} catch (err) {
return [];
}
const data: ServersDatManifest = await (new Promise((res, rej) => {
nbt.parse(serversDat, (err, data) => err ? rej(err) : res(data))
}));
return data.value.servers.value.value;
}
private saveServersDat(serversData: ServersDatItem[]) {
const serversFilePath = path.join(this.options.gameDir, "servers.dat");
const translatedData = nbt.writeUncompressed({
name: "", value: {
servers: {
type: "list", value: {
type: "compound", value: serversData
}
}
}
});
const bufferedData = Buffer.from(translatedData);
fs.writeFileSync(serversFilePath, bufferedData);
}
public async ensureServersDat(server: ServerInfo) {
const servers: ServersDatItem[] = await this.loadServersDat();
const preparedServerHost: string = server.host + (server.port ? (':' + server.port) : '');
const preparedServer: ServersDatItem = {
ip: {
type: "string",
value: preparedServerHost
},
name: {
type: "string",
value: server.name || "Server"
}
};
if (!servers.length) {
return this.saveServersDat([preparedServer]);
}
if (!servers.find((item: ServersDatItem) => item.ip.value === preparedServerHost)) {
return this.saveServersDat([preparedServer, ...servers]);
}
}
public async checkMods(mods: ForgeMod[], exclusive: boolean): Promise<void> {
this.progress.step("Installing Mods");
mkdirp(path.join(this.options.gameDir, 'mods'));
let files: string[];
if(exclusive && await fs.exists(path.join(this.options.gameDir, 'mods')))
files = await fs.readdir(path.join(this.options.gameDir, 'mods'));
else
files = [];
files = files.filter(value => value.indexOf('.jar') !== -1);
for(let i = 0; i < mods.length; i++) {
let mod: ForgeMod = mods[i];
this.progress.call(i/mods.length);
let file: string = path.join(this.options.gameDir, 'mods', mod.file + '.jar');
if(exclusive) {
let i: number = files.indexOf(mod.file + '.jar');
if(i !== -1)
files.splice(i, 1);
}
if(mod.sha1)
await Downloader.checkOrDownload(mod.url, mod.sha1, file);
else
await Downloader.existsOrDownload(mod.url, file);
}
if(exclusive) {
let task: Promise<void>[] = [];
for(let i = 0; i < files.length; i++)
task.push(fs.unlink(path.join(this.options.gameDir, 'mods', files[i])));
await Promise.all(task);
}
this.progress.call(1);
}
public async launch(auth: AuthenticationResult, launchOptions: LaunchOptions = {}): Promise<child_process.ChildProcess> {
this.nativeDir = await this.libraryManager.unpackNatives(this.version);
const args: string[] = [
...this.libraryManager.getJavaArguments(this.nativeDir, launchOptions),
...this.libraryManager.getLaunchArguments(auth, launchOptions)
];
let cp: child_process.ChildProcess = child_process.spawn(this.options.javaExecutable, args, {
cwd: this.options.gameDir
});
if(launchOptions.redirectOutput) {
cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
}
return cp;
}
}
export declare type ClientOptions = {
gameDir?: string,
javaExecutable?: string,
features?: {
is_demo_user?: boolean
has_custom_resolution?: boolean,
has_custom_server?: boolean
}
}
export declare type LaunchOptions = {
redirectOutput?: boolean,
resolution?: {
width: number,
height: number
},
server?: {
host: string,
port: number
},
memory?: string
}
export declare type ServerInfo = {
host: string,
port?: number,
name?: string
}
export type ServersDatItem = {
ip: {
type: "string",
value: string
},
name: {
type: "string",
value: string
}
}
export type ServersDatManifest = {
name: "",
value: {
servers: {
type: "list",
value: {
type: "compound",
value: ServersDatItem[]
}
}
}
}