This repository was archived by the owner on May 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathinstaller.ts
More file actions
186 lines (172 loc) · 5.35 KB
/
Copy pathinstaller.ts
File metadata and controls
186 lines (172 loc) · 5.35 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
import {
getResolvedVersionHeader,
MinecraftFolder,
ResolvedLibrary,
ResolvedVersion,
} from '@xmcl/core'
import { readFile } from 'fs/promises'
import { join } from 'path'
import { AssetsOptions, AssetsTrackerEvents, installAssets, installResolvedAssets } from './assets'
import { InstallError, InstallIssue, isInstallError, mergeInstallIssue } from './error'
import { installForge } from './forge'
import {
installLibraries,
installResolvedLibraries,
LibrariesTrackerEvents,
LibraryOptions,
} from './libraries'
import { installMinecraftJar, JarOption, MinecraftTrackerEvents } from './minecraft'
import {
diagnoseProfile,
installByProfile,
InstallProfile,
InstallProfileOption,
ProfileTrackerEvents,
} from './profile'
import { Tracker } from './tracker'
export interface CompleteTrackerEvents
extends
MinecraftTrackerEvents,
LibrariesTrackerEvents,
AssetsTrackerEvents,
ProfileTrackerEvents {}
export interface CompleteOptions
extends
Omit<JarOption, 'tracker'>,
Omit<LibraryOptions, 'tracker'>,
Omit<AssetsOptions, 'tracker'>,
Omit<InstallProfileOption, 'tracker'> {
/**
* The tracker to track the complete installation process
*/
tracker?: Tracker<CompleteTrackerEvents>
}
async function readProfile(versionDir: string) {
const installProfilePath = join(versionDir, 'install_profile.json')
try {
const installProfile: InstallProfile = JSON.parse(await readFile(installProfilePath, 'utf8'))
return installProfile
} catch {
return undefined
}
}
/**
* Complete the installation of a resolved version, including minecraft jar, libraries, assets and profile.
*
* This can continue to install an aborted or failed installation, and it can diagnose the installation if `options.diagnose` is set to `true`.
*
* @param version The resolved version to install
* @param options Installation options
* @throws InstallError when diagnose is true and there are issues found during installation
*/
export async function completeInstallation(
version: ResolvedVersion,
options: CompleteOptions = {},
): Promise<void> {
let issue: InstallIssue = {}
await installMinecraftJar(version, { ...options, tracker: options.tracker }).catch((e) => {
if (options.diagnose && isInstallError(e)) {
mergeInstallIssue(issue, e.issue)
return
}
throw e
})
const folder = MinecraftFolder.from(version.minecraftDirectory)
const versionDir = folder.getVersionRoot(version.id)
const profile = await readProfile(versionDir)
if (profile) {
const issue = await diagnoseProfile(profile, folder, options.side)
if (issue) {
if (options.diagnose) {
throw new InstallError({
profile,
})
}
await installByProfile(profile, folder, { ...options, tracker: options.tracker })
}
}
await installLibraries(version, { ...options, tracker: options.tracker }).catch((e) => {
if (options.diagnose && isInstallError(e)) {
mergeInstallIssue(issue, e.issue)
return
}
throw e
})
await installAssets(version, { ...options, tracker: options.tracker }).catch((e) => {
if (options.diagnose && isInstallError(e)) {
mergeInstallIssue(issue, e.issue)
return
}
throw e
})
if (options.diagnose && Object.keys(issue).length > 0) {
if (issue.libraries && issue.libraries.length > 0) {
const optifines = [] as ResolvedLibrary[]
const forges = [] as ResolvedLibrary[]
const others = [] as ResolvedLibrary[]
for (const l of issue.libraries) {
if (l.groupId === 'optifine') {
optifines.push(l)
} else if (
l.groupId === 'net.minecraftforge' &&
l.artifactId === 'forge' &&
(l.classifier === 'client' || !l.classifier)
) {
forges.push(l)
} else {
others.push(l)
}
}
if (others.length > 0) {
issue.libraries = others
}
if (optifines.length > 0) {
issue.optifine = optifines[0].version
}
if (forges.length > 0) {
const header = getResolvedVersionHeader(version)
if (header.forge && header.minecraft) {
issue.forge = {
minecraft: header.minecraft,
version: header.forge,
}
}
}
}
throw new InstallError(issue)
}
}
export async function completeInstallationByError(
version: ResolvedVersion,
error: InstallError,
options: CompleteOptions = {},
): Promise<void> {
const issue = error.issue
const folder = MinecraftFolder.from(version.minecraftDirectory)
if (issue.jar) {
await installMinecraftJar(version, { ...options, tracker: options.tracker })
}
if (issue.forge) {
await installForge(
{
mcversion: issue.forge.minecraft,
version: issue.forge.version,
},
folder,
{ ...options, tracker: options.tracker },
)
} else if (issue.profile) {
await installByProfile(issue.profile, folder, { ...options, tracker: options.tracker })
}
if (issue.libraries && issue.libraries.length > 0) {
await installResolvedLibraries(issue.libraries, folder, {
...options,
tracker: options.tracker,
})
}
if (issue.assetsIndex) {
await installAssets(version, { ...options, tracker: options.tracker })
} else if (issue.assets && issue.assets.length > 0) {
await installResolvedAssets(issue.assets, folder, version.id, options)
}
}