-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
236 lines (207 loc) · 8.81 KB
/
Copy pathindex.js
File metadata and controls
236 lines (207 loc) · 8.81 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
require("dotenv").config()
const fs = require("fs")
const path = require("path")
const https = require("https")
const rl = require("node:readline/promises").createInterface({
input: process.stdin,
output: process.stdout
})
async function pipeToString(stream) {
let data = ""
stream.on("data", chunk => {
data += chunk
})
await new Promise(r => stream.on("end", r))
return data
}
function apiGet(url, options) {
const request = https.get(url, options)
return new Promise(async (resolve, reject) => {
const response = await new Promise(r => request.on("response", r))
if (response.statusCode != 200) return reject()
response.setTimeout(5000, reject)
const data = await pipeToString(response)
return resolve(JSON.parse(data))
})
}
async function curseforgeGet(path, options = {}) {
if (!options.headers) options.headers = {}
options.headers["x-api-key"] = process.env.CURSEFORGE_API_KEY
return apiGet("https://api.curseforge.com" + path, options)
}
async function download(source, destination) {
const request = https.get(source)
request.setTimeout(5000, () => {
request.destroy()
download(source, destination)
})
const response = await new Promise(r => request.on("response", r))
if (response.statusCode == 302 || response.statusCode == 301) {
return download(response.headers.location, destination)
}
response.pipe(fs.createWriteStream(destination))
return new Promise(r => request.on("close", r))
}
(async ()=>{
const datasource = process.env.JSON_SOURCE
let data
if (datasource == "file") {
if (!process.env.JSON_PATH) {
throw new Error("Mod json file path not present")
}
if (fs.existsSync(process.env.JSON_PATH)) {
const text = fs.readFileSync(process.env.JSON_PATH)
data = JSON.parse(text)
} else {
throw new Error(`Mod json file at path "${process.env.JSON_PATH}" is not present`)
}
} else if (datasource == "http") {
if (!process.env.JSON_PATH) {
throw new Error("Mod json file path not present")
}
const request = https.get(process.env.JSON_PATH)
const response = await new Promise(r => request.on("response", r))
if (response.statusCode != 200) {
throw new Error(`HTTP ${response.statusCode} recieved from "${process.env.JSON_PATH}"`)
}
const text = await pipeToString(response)
if (!text || text == "") {
throw new Error(`No data recieved from "${process.env.JSON_PATH}"`)
}
data = JSON.parse(text)
if (!data) {
throw new Error(`Invalid data recieved from "${process.env.JSON_PATH}"`)
}
} else {
throw new Error(`Invalid mod data source "${datasource}"`)
}
const folderpath = path.resolve(process.env.INSTANCE_FOLDER_PATH)
const modfolderpath = path.join(folderpath, "mods")
const configfolderpath = path.join(folderpath, "config")
{
const files = fs.readdirSync(modfolderpath)
if (files.length > 0) {
const answer = (await rl.question("There are already mods installed in the selected instance. Delete? (y/n) ")).toLowerCase()
if (answer == "y" || answer == "yes") {
await Promise.all(await files.map(filename => new Promise(r => fs.rm(path.join(modfolderpath, filename), r))))
} else {
process.exit()
}
}
}
if (fs.readdirSync(modfolderpath).length > 0) {
throw new Error("Could not delete files")
}
const supportedversions = data.supportedGameVersions
const supportedmodloaders = data.supportedModLoaders
if (supportedversions == null || typeof supportedversions != "object" || supportedversions.length <= 0) {
throw new Error(`Invalid supported versions array "${supportedversions}"`)
}
if (supportedmodloaders == null || typeof supportedmodloaders != "object" || supportedmodloaders.length <= 0) {
throw new Error(`Invalid supported modloaders array "${supportedmodloaders}"`)
}
let modloader
let mcversion
{
const filepath = path.join(folderpath, "profile.json")
if (fs.existsSync(filepath)) {
const data = JSON.parse(fs.readFileSync(filepath))
modloader = data.metadata.loader
console.log(`Modloader "${modloader}" detected`)
if (modloader == "vanilla") {
console.warn("Vanilla Modrinth instance detected")
process.exit()
}
mcversion = data.metadata.game_version
console.log(`Version "${mcversion}" detected`)
}
}
{
const filepath = path.join(folderpath, "minecraftinstance.json")
if (fs.existsSync(filepath)) {
const data = JSON.parse(fs.readFileSync(filepath))
const basemodloader = data.baseModLoader
if (basemodloader == null) {
console.warn("Vanilla Curseforge instance detected")
process.exit()
}
modloader = [null, "forge", "cauldron", "liteloader", "fabric", "quilt", "neoforge"][basemodloader.type]
console.log(`Modloader "${modloader}" detected`)
mcversion = basemodloader.minecraftVersion
console.log(`Version "${mcversion}" detected`)
}
}
if (modloader == null || !supportedmodloaders.includes(modloader)) {
while (true) {
const answer = await rl.question("Modloader? ")
if (supportedmodloaders.includes(answer)) {
modloader = answer
break
} else {
console.log("Modloader invalid or not supported")
}
}
}
if (mcversion == null || !supportedversions.includes(mcversion)) {
while (true) {
const answer = await rl.question("Minecraft version? ")
if (supportedversions.includes(answer)) {
mcversion = answer
break
} else {
console.log("Version invalid or not supported")
}
}
}
const curseforgemodloaderid = [null, "forge", "cauldron", "liteloader", "fabric", "quilt", "neoforge"].findIndex(v => v == modloader)
let categories = ["basic", "library", "optimization", "visual", "qol", "utility", "other"]
{
const answer = (await rl.question("Include only optimization mods? (y/n) ")).toLowerCase()
if (answer == "y" || answer == "yes") categories = ["basic", "library", "optimization"]
}
const installedmods = []
const downloads = []
for (const mod of data.mods) {
if (!categories.includes(mod.type)) {
continue
}
if (mod.incompatible && mod.incompatible.some(v => installedmods.includes(v))) {
continue
}
if (mod.dependencies && !mod.dependencies.some(v => installedmods.includes(v))) {
continue
}
if (mod.ask == true) {
const answer = await rl.question(`Include "${mod.name}? (y/n) `)
if (answer == "n") continue
}
if (mod.platform == "modrinth") {
const versions = await apiGet(`https://api.modrinth.com/v2/project/${mod.id}/version?loaders=${encodeURIComponent(JSON.stringify([modloader]))}&game_versions=${encodeURIComponent(JSON.stringify([mcversion]))}`).catch(console.error())
if (versions.length > 0) {
console.log(`Downloading "${mod.name}"`)
const url = versions[0].files[0].url
downloads.push(download(url, path.join(modfolderpath, path.basename(decodeURIComponent(url)))))
installedmods.push(mod.id)
}
} else if (mod.platform == "curseforge") {
if (process.env.CURSEFORGE_API_KEY == null) {
console.log(`Failed to download "${mod.name}" because of missing Curseforge API key`)
} else {
const mdata = await curseforgeGet(`/v1/mods/${mod.id}`)
const files = mdata.data.latestFilesIndexes
const matchingfiles = files.filter(file => file.gameVersion == mcversion && file.modLoader == curseforgemodloaderid)
if (matchingfiles.length > 0) {
console.log(`Downloading "${mod.name}"`)
const file = matchingfiles[0]
const url = (await curseforgeGet(`/v1/mods/${mod.id}/files/${file.fileId}/download-url`)).data
downloads.push(download(url, path.join(modfolderpath, path.basename(url))))
installedmods.push(mod.id)
}
}
}
}
await Promise.all(downloads)
console.log(`Downloaded ${installedmods.length} mods`)
console.log("Finished")
process.exit()
})()