forked from Voxelum/minecraft-launcher-core-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnose.ts
More file actions
257 lines (229 loc) · 8.76 KB
/
Copy pathdiagnose.ts
File metadata and controls
257 lines (229 loc) · 8.76 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
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
import { ResolvedLibrary, ResolvedVersion, Version } from "./version";
import { MinecraftFolder, MinecraftLocation } from "./folder";
import { checksum, exists, isNotNull, readFile } from "./utils";
/**
* Represent a issue for your diagnosed minecraft client.
*/
export interface Issue {
/**
* The type of the issue.
*/
type: "missing" | "corrupted";
/**
* The role of the file in Minecraft.
*/
role: string;
/**
* The path of the problematic file.
*/
file: string;
/**
* The useful hint to fix this issue. This should be a human readable string.
*/
hint: string;
/**
* The expected checksum of the file. Can be an empty string if this file is missing or not check checksum at all!
*/
expectedChecksum: string;
/**
* The actual checksum of the file. Can be an empty string if this file is missing or not check checksum at all!
*/
receivedChecksum: string;
}
export type MinecraftIssues = LibraryIssue | MinecraftJarIssue | VersionJsonIssue | AssetIssue | AssetIndexIssue;
/**
* The library issue represents a corrupted or missing lib.
* You can use `Installer.installResolvedLibraries` to fix this.
*/
export interface LibraryIssue extends Issue {
role: "library";
/**
* The problematic library
*/
library: ResolvedLibrary;
}
/**
* The minecraft jar issue represents a corrupted or missing minecraft jar.
* You can use `Installer.installVersion` to fix this.
*/
export interface MinecraftJarIssue extends Issue {
role: "minecraftJar";
/**
* The minecraft version for that jar
*/
version: string;
}
/**
* The minecraft jar issue represents a corrupted or missing version jar.
*
* This means your version is totally broken, and you should reinstall this version.
*
* - If this is just a Minecraft version, you will need to use `Installer.install` to re-install Minecraft.
* - If this is a Forge version, you will need to use `ForgeInstaller.install` to re-install.
* - Others are the same, just re-install
*/
export interface VersionJsonIssue extends Issue {
role: "versionJson";
/**
* The version of version json that has problem.
*/
version: string;
}
/**
* The asset issue represents a corrupted or missing minecraft asset file.
* You can use `Installer.installResolvedAssets` to fix this.
*/
export interface AssetIssue extends Issue {
role: "asset";
/**
* The problematic asset
*/
asset: { name: string; hash: string; size: number; };
}
/**
* The asset index issue represents a corrupted or missing minecraft asset index file.
* You can use `Installer.installAssets` to fix this.
*/
export interface AssetIndexIssue extends Issue {
role: "assetIndex";
/**
* The minecraft version of the asset index
*/
version: string;
}
export interface MinecraftIssueReport {
minecraftLocation: MinecraftFolder;
version: string;
issues: MinecraftIssues[];
}
/**
* Diagnose a single file by a certain checksum algorithm. By default, this use sha1
*/
export async function diagnoseFile<T extends string>({ file, expectedChecksum, role, hint, algorithm }: { file: string; expectedChecksum: string; role: T; hint: string; algorithm?: string }) {
let issue = false;
let receivedChecksum = "";
algorithm = algorithm ?? "sha1";
const fileExisted = await exists(file);
if (!fileExisted) {
issue = true;
} else if (expectedChecksum !== "") {
receivedChecksum = await checksum(file, algorithm);
issue = receivedChecksum !== expectedChecksum;
}
const type = fileExisted ? "corrupted" : "missing" as const
if (issue) {
return {
type,
role,
file,
expectedChecksum,
receivedChecksum,
hint,
} as const;
}
return undefined;
}
/**
* Diagnose the version. It will check the version json/jar, libraries and assets.
*
* @param version The version id string
* @param minecraft The minecraft location
* @beta
*/
export async function diagnose(version: string, minecraftLocation: MinecraftLocation): Promise<MinecraftIssueReport> {
const minecraft = MinecraftFolder.from(minecraftLocation);
let report: MinecraftIssueReport = {
minecraftLocation: minecraft,
version: version,
issues: [],
}
let issues: Issue[] = report.issues;
let resolvedVersion: ResolvedVersion;
try {
resolvedVersion = await Version.parse(minecraft, version);
} catch (err) {
const e: any = err;
if (e.error === "CorruptedVersionJson") {
issues.push({ type: "corrupted", role: "versionJson", file: minecraft.getVersionJson(e.version), expectedChecksum: "", receivedChecksum: "", hint: "Re-install the minecraft!" });
} else {
issues.push({ type: "missing", role: "versionJson", file: minecraft.getVersionJson(e.version), expectedChecksum: "", receivedChecksum: "", hint: "Re-install the minecraft!" });
}
return report;
}
const jarIssue = await diagnoseJar(resolvedVersion, minecraft);
if (jarIssue) {
report.issues.push(jarIssue);
}
const assetIndexIssue = await diagnoseAssetIndex(resolvedVersion, minecraft);
if (assetIndexIssue) {
report.issues.push(assetIndexIssue);
}
const librariesIssues = await diagnoseLibraries(resolvedVersion, minecraft);
if (librariesIssues.length > 0) {
report.issues.push(...librariesIssues);
}
if (!assetIndexIssue) {
const objects = (await readFile(minecraft.getAssetsIndex(resolvedVersion.assets), "utf-8").then((b) => JSON.parse(b.toString()))).objects;
const assetsIssues = await diagnoseAssets(objects, minecraft);
if (assetsIssues.length > 0) {
report.issues.push(...assetsIssues);
}
}
return report;
}
/**
* Diagnose assets currently installed.
* @param assetObjects The assets object metadata to check
* @param minecraft The minecraft location
* @returns The diagnose report
*/
export async function diagnoseAssets(assetObjects: Record<string, { hash: string; size: number }>, minecraft: MinecraftFolder): Promise<Array<AssetIssue>> {
const filenames = Object.keys(assetObjects);
const issues = await Promise.all(filenames.map(async (filename) => {
const { hash, size } = assetObjects[filename];
const assetPath = minecraft.getAsset(hash);
const issue = await diagnoseFile({ file: assetPath, expectedChecksum: hash, role: "asset", hint: "Problem on asset! Please consider to use Installer.installAssets to fix." });
if (issue) {
return Object.assign(issue, { asset: { name: filename, hash, size } });
}
return undefined;
}));
return issues.filter(isNotNull);
}
/**
* Diagnose all libraries presented in this resolved version.
*
* @param resolvedVersion The resolved version to check
* @param minecraft The minecraft location
* @returns List of libraries issue
* @see {@link ResolvedVersion}
*/
export async function diagnoseLibraries(resolvedVersion: ResolvedVersion, minecraft: MinecraftFolder): Promise<Array<LibraryIssue>> {
const issues = await Promise.all(resolvedVersion.libraries.map(async (lib) => {
const libPath = minecraft.getLibraryByPath(lib.download.path);
const issue = await diagnoseFile({ file: libPath, expectedChecksum: lib.download.sha1, role: "library", hint: "Problem on library! Please consider to use Installer.installLibraries to fix." });
if (issue) {
return Object.assign(issue, { library: lib });
}
return undefined;
}));
return issues.filter(isNotNull);
}
export async function diagnoseAssetIndex(resolvedVersion: ResolvedVersion, minecraft: MinecraftFolder): Promise<AssetIndexIssue | undefined> {
const assetsIndexPath = minecraft.getAssetsIndex(resolvedVersion.assets);
const issue = await diagnoseFile(
{ file: assetsIndexPath, expectedChecksum: resolvedVersion.assetIndex.sha1, role: "assetIndex", hint: "Problem on assets index file! Please consider to use Installer.installAssets to fix." });
if (issue) {
return Object.assign(issue, { version: resolvedVersion.minecraftVersion });
}
return undefined;
}
export async function diagnoseJar(resolvedVersion: ResolvedVersion, minecraft: MinecraftFolder): Promise<MinecraftJarIssue | undefined> {
const jarPath = minecraft.getVersionJar(resolvedVersion.minecraftVersion);
const issue = await diagnoseFile(
{ file: jarPath, expectedChecksum: resolvedVersion.downloads.client.sha1, role: "minecraftJar", hint: "Problem on Minecraft jar! Please consider to use Installer.instalVersion to fix." });
if (issue) {
return Object.assign(issue, { version: resolvedVersion.minecraftVersion });
}
return undefined;
}