-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathModDiscoverer.java
561 lines (455 loc) · 17.8 KB
/
ModDiscoverer.java
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
* Copyright 2016 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.loader.impl.discovery;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.fabricmc.loader.impl.FabricLoaderImpl;
import net.fabricmc.loader.impl.FormattedException;
import net.fabricmc.loader.impl.discovery.ModCandidateFinder.ModCandidateConsumer;
import net.fabricmc.loader.impl.game.GameProvider.BuiltinMod;
import net.fabricmc.loader.impl.metadata.BuiltinModMetadata;
import net.fabricmc.loader.impl.metadata.DependencyOverrides;
import net.fabricmc.loader.impl.metadata.LoaderModMetadata;
import net.fabricmc.loader.impl.metadata.MetadataVerifier;
import net.fabricmc.loader.impl.metadata.ModMetadataParser;
import net.fabricmc.loader.impl.metadata.NestedJarEntry;
import net.fabricmc.loader.impl.metadata.ParseMetadataException;
import net.fabricmc.loader.impl.metadata.VersionOverrides;
import net.fabricmc.loader.impl.util.ExceptionUtil;
import net.fabricmc.loader.impl.util.LoaderUtil;
import net.fabricmc.loader.impl.util.SystemProperties;
import net.fabricmc.loader.impl.util.log.Log;
import net.fabricmc.loader.impl.util.log.LogCategory;
public final class ModDiscoverer {
private final VersionOverrides versionOverrides;
private final DependencyOverrides depOverrides;
private final List<ModCandidateFinder> candidateFinders = new ArrayList<>();
private final EnvType envType = FabricLoaderImpl.INSTANCE.getEnvironmentType();
private final Map<Long, ModScanTask> jijDedupMap = new ConcurrentHashMap<>(); // avoids reading the same jar twice
private final List<NestedModInitData> nestedModInitDatas = Collections.synchronizedList(new ArrayList<>()); // breaks potential cycles from deduplication
public ModDiscoverer(VersionOverrides versionOverrides, DependencyOverrides depOverrides) {
this.versionOverrides = versionOverrides;
this.depOverrides = depOverrides;
}
public void addCandidateFinder(ModCandidateFinder f) {
candidateFinders.add(f);
}
public List<ModCandidate> discoverMods(FabricLoaderImpl loader, Map<String, Set<ModCandidate>> envDisabledModsOut) throws ModResolutionException {
long startTime = System.nanoTime();
ForkJoinPool pool = new ForkJoinPool();
Set<Path> processedPaths = new HashSet<>(); // suppresses duplicate paths
List<Future<ModCandidate>> futures = new ArrayList<>();
ModCandidateConsumer taskSubmitter = (paths, requiresRemap) -> {
if (paths.size() == 1) {
Path path = LoaderUtil.normalizeExistingPath(paths.get(0));
if (processedPaths.add(path)) {
futures.add(pool.submit(new ModScanTask(Collections.singletonList(path), requiresRemap)));
}
} else {
List<Path> normalizedPaths = new ArrayList<>(paths.size());
for (Path path : paths) {
normalizedPaths.add(LoaderUtil.normalizeExistingPath(path));
}
if (!processedPaths.containsAll(normalizedPaths)) {
processedPaths.addAll(normalizedPaths);
futures.add(pool.submit(new ModScanTask(normalizedPaths, requiresRemap)));
}
}
};
for (ModCandidateFinder finder : candidateFinders) {
finder.findCandidates(taskSubmitter);
}
List<ModCandidate> candidates = new ArrayList<>();
// add builtin mods
for (BuiltinMod mod : loader.getGameProvider().getBuiltinMods()) {
ModCandidate candidate = ModCandidate.createBuiltin(mod, versionOverrides, depOverrides);
candidates.add(MetadataVerifier.verifyIndev(candidate, loader.isDevelopmentEnvironment()));
}
// Add the current Java version
candidates.add(MetadataVerifier.verifyIndev(createJavaMod(), loader.isDevelopmentEnvironment()));
ModResolutionException exception = null;
int timeout = Integer.getInteger(SystemProperties.DEBUG_DISCOVERY_TIMEOUT, 60);
if (timeout <= 0) timeout = Integer.MAX_VALUE;
try {
pool.shutdown();
pool.awaitTermination(timeout, TimeUnit.SECONDS);
for (Future<ModCandidate> future : futures) {
if (!future.isDone()) {
throw new TimeoutException();
}
try {
ModCandidate candidate = future.get();
if (candidate != null) candidates.add(candidate);
} catch (ExecutionException e) {
exception = ExceptionUtil.gatherExceptions(e, exception, exc -> new ModResolutionException("Mod discovery failed!", exc));
}
}
for (NestedModInitData data : nestedModInitDatas) {
for (Future<ModCandidate> future : data.futures) {
if (!future.isDone()) {
throw new TimeoutException();
}
try {
ModCandidate candidate = future.get();
if (candidate != null) data.target.add(candidate);
} catch (ExecutionException e) {
exception = ExceptionUtil.gatherExceptions(e, exception, exc -> new ModResolutionException("Mod discovery failed!", exc));
}
}
}
} catch (TimeoutException e) {
throw new FormattedException("Mod discovery took too long!",
"Analyzing the mod folder contents took longer than %d seconds. This may be caused by unusually slow hardware, pathological antivirus interference or other issues. The timeout can be changed with the system property %s (-D%<s=<desired timeout in seconds>).",
timeout, SystemProperties.DEBUG_DISCOVERY_TIMEOUT);
} catch (InterruptedException e) {
throw new FormattedException("Mod discovery interrupted!", e);
}
if (exception != null) {
throw exception;
}
// gather gather all mods (root+nested), initialize parent data
Set<ModCandidate> ret = Collections.newSetFromMap(new IdentityHashMap<>(candidates.size() * 2));
Queue<ModCandidate> queue = new ArrayDeque<>(candidates);
ModCandidate mod;
while ((mod = queue.poll()) != null) {
if (mod.getMetadata().loadsInEnvironment(envType)) {
if (!ret.add(mod)) continue;
for (ModCandidate child : mod.getNestedMods()) {
if (child.addParent(mod)) {
queue.add(child);
}
}
} else {
envDisabledModsOut.computeIfAbsent(mod.getId(), ignore -> Collections.newSetFromMap(new IdentityHashMap<>())).add(mod);
}
}
long endTime = System.nanoTime();
Log.debug(LogCategory.DISCOVERY, "Mod discovery time: %.1f ms", (endTime - startTime) * 1e-6);
return new ArrayList<>(ret);
}
private ModCandidate createJavaMod() {
ModMetadata metadata = new BuiltinModMetadata.Builder("java", System.getProperty("java.specification.version").replaceFirst("^1\\.", ""))
.setName(System.getProperty("java.vm.name"))
.build();
BuiltinMod builtinMod = new BuiltinMod(Collections.singletonList(Paths.get(System.getProperty("java.home"))), metadata);
return ModCandidate.createBuiltin(builtinMod, versionOverrides, depOverrides);
}
@SuppressWarnings("serial")
final class ModScanTask extends RecursiveTask<ModCandidate> {
private final List<Path> paths;
private final String localPath;
private final RewindableInputStream is;
private final long hash;
private final boolean requiresRemap;
private final List<String> parentPaths;
ModScanTask(List<Path> paths, boolean requiresRemap) {
this(paths, null, null, -1, requiresRemap, Collections.emptyList());
}
private ModScanTask(List<Path> paths, String localPath, RewindableInputStream is, long hash,
boolean requiresRemap, List<String> parentPaths) {
this.paths = paths;
this.localPath = localPath != null ? localPath : paths.get(0).toString();
this.is = is;
this.hash = hash;
this.requiresRemap = requiresRemap;
this.parentPaths = parentPaths;
}
@Override
protected ModCandidate compute() {
if (is != null) { // nested jar
try {
return computeJarStream();
} catch (ParseMetadataException e) { // already contains all context
throw ExceptionUtil.wrap(e);
} catch (Throwable t) {
throw new RuntimeException(String.format("Error analyzing nested jar %s from %s: %s", localPath, parentPaths, t), t);
}
} else { // regular classes-dir or jar
try {
for (Path path : paths) {
final ModCandidate candidate;
if (Files.isDirectory(path)) {
candidate = computeDir(path);
} else {
candidate = computeJarFile(path);
}
if (candidate != null) {
return candidate;
}
}
} catch (ParseMetadataException e) { // already contains all context
throw ExceptionUtil.wrap(e);
} catch (Throwable t) {
throw new RuntimeException(String.format("Error analyzing %s: %s", paths, t), t);
}
return null;
}
}
private ModCandidate computeDir(Path path) throws IOException, ParseMetadataException {
Path modJson = path.resolve("fabric.mod.json");
if (!Files.exists(modJson)) return null;
LoaderModMetadata metadata;
try (InputStream is = Files.newInputStream(modJson)) {
metadata = parseMetadata(is, path.toString());
}
return ModCandidate.createPlain(paths, metadata, requiresRemap, Collections.emptyList());
}
private ModCandidate computeJarFile(Path path) throws IOException, ParseMetadataException {
try (ZipFile zf = new ZipFile(path.toFile())) {
ZipEntry entry = zf.getEntry("fabric.mod.json");
if (entry == null) return null;
LoaderModMetadata metadata;
try (InputStream is = zf.getInputStream(entry)) {
metadata = parseMetadata(is, localPath);
}
if (!metadata.loadsInEnvironment(envType)) {
return ModCandidate.createPlain(paths, metadata, requiresRemap, Collections.emptyList());
}
List<ModScanTask> nestedModTasks;
if (metadata.getJars().isEmpty()) {
nestedModTasks = Collections.emptyList();
} else {
Set<NestedJarEntry> nestedJarPaths = new HashSet<>(metadata.getJars());
nestedModTasks = computeNestedMods(new ZipEntrySource() {
@Override
public ZipEntry getNextEntry() throws IOException {
while (jarIt.hasNext()) {
NestedJarEntry jar = jarIt.next();
ZipEntry ret = zf.getEntry(jar.getFile());
if (isValidNestedJarEntry(ret)) {
currentEntry = ret;
jarIt.remove();
return ret;
}
}
currentEntry = null;
return null;
}
@Override
public RewindableInputStream getInputStream() throws IOException {
try (InputStream is = zf.getInputStream(currentEntry)) {
return new RewindableInputStream(is);
}
}
private final Iterator<NestedJarEntry> jarIt = nestedJarPaths.iterator();
private ZipEntry currentEntry;
});
if (!nestedJarPaths.isEmpty() && FabricLoaderImpl.INSTANCE.isDevelopmentEnvironment()) {
Log.warn(LogCategory.METADATA, "Mod %s %s references missing nested jars: %s", metadata.getId(), metadata.getVersion(), nestedJarPaths);
}
}
List<ModCandidate> nestedMods;
if (nestedModTasks.isEmpty()) {
nestedMods = Collections.emptyList();
} else {
nestedMods = new ArrayList<>();
nestedModInitDatas.add(new NestedModInitData(nestedModTasks, nestedMods));
}
return ModCandidate.createPlain(paths, metadata, requiresRemap, nestedMods);
}
}
private ModCandidate computeJarStream() throws IOException, ParseMetadataException {
LoaderModMetadata metadata = null;
ZipEntry entry;
try (ZipInputStream zis = new ZipInputStream(is)) {
while ((entry = zis.getNextEntry()) != null) {
if (entry.getName().equals("fabric.mod.json")) {
metadata = parseMetadata(zis, localPath);
break;
}
}
}
if (metadata == null) return null;
if (!metadata.loadsInEnvironment(envType)) {
return ModCandidate.createNested(localPath, hash, metadata, requiresRemap, Collections.emptyList());
}
Collection<NestedJarEntry> nestedJars = metadata.getJars();
List<ModScanTask> nestedModTasks;
if (nestedJars.isEmpty()) {
nestedModTasks = Collections.emptyList();
} else {
Set<String> nestedJarPaths = new HashSet<>(nestedJars.size());
for (NestedJarEntry nestedJar : nestedJars) {
nestedJarPaths.add(nestedJar.getFile());
}
is.rewind();
try (ZipInputStream zis = new ZipInputStream(is)) {
nestedModTasks = computeNestedMods(new ZipEntrySource() {
@Override
public ZipEntry getNextEntry() throws IOException {
if (nestedJarPaths.isEmpty()) return null;
ZipEntry ret;
while ((ret = zis.getNextEntry()) != null) {
if (isValidNestedJarEntry(ret) && nestedJarPaths.remove(ret.getName())) {
is = new RewindableInputStream(zis); // reads the entry, which completes the ZipEntry with any trailing header data
return ret;
}
}
return null;
}
@Override
public RewindableInputStream getInputStream() throws IOException {
return is;
}
private RewindableInputStream is;
});
}
if (!nestedJarPaths.isEmpty() && FabricLoaderImpl.INSTANCE.isDevelopmentEnvironment()) {
Log.warn(LogCategory.METADATA, "Mod %s %s references missing nested jars: %s", metadata.getId(), metadata.getVersion(), nestedJarPaths);
}
}
List<ModCandidate> nestedMods;
if (nestedModTasks.isEmpty()) {
nestedMods = Collections.emptyList();
} else {
nestedMods = new ArrayList<>();
nestedModInitDatas.add(new NestedModInitData(nestedModTasks, nestedMods));
}
ModCandidate ret = ModCandidate.createNested(localPath, hash, metadata, requiresRemap, nestedMods);
ret.setData(is.getBuffer());
return ret;
}
private List<ModScanTask> computeNestedMods(ZipEntrySource entrySource) throws IOException {
List<String> parentPaths = new ArrayList<>(this.parentPaths.size() + 1);
parentPaths.addAll(this.parentPaths);
parentPaths.add(localPath);
List<ModScanTask> tasks = new ArrayList<>(5);
ModScanTask localTask = null;
ZipEntry entry;
while ((entry = entrySource.getNextEntry()) != null) {
long hash = ModCandidate.hash(entry);
ModScanTask task = jijDedupMap.get(hash);
if (task == null) {
task = new ModScanTask(null, entry.getName(), entrySource.getInputStream(), hash, requiresRemap, parentPaths);
ModScanTask prev = jijDedupMap.putIfAbsent(hash, task);
if (prev != null) {
task = prev;
} else if (localTask == null) { // don't fork first task, leave it for this thread
localTask = task;
} else {
task.fork();
}
}
tasks.add(task);
}
if (tasks.isEmpty()) return Collections.emptyList();
if (localTask != null) localTask.invoke();
return tasks;
}
private LoaderModMetadata parseMetadata(InputStream is, String localPath) throws ParseMetadataException {
return ModMetadataParser.parseMetadata(is, localPath, parentPaths, versionOverrides, depOverrides, FabricLoaderImpl.INSTANCE.isDevelopmentEnvironment());
}
}
private static boolean isValidNestedJarEntry(ZipEntry entry) {
return entry != null && !entry.isDirectory() && entry.getName().endsWith(".jar");
}
private interface ZipEntrySource {
ZipEntry getNextEntry() throws IOException;
RewindableInputStream getInputStream() throws IOException;
}
private static final class RewindableInputStream extends InputStream {
private final ByteBuffer buffer;
private int pos;
RewindableInputStream(InputStream parent) throws IOException { // no parent.close()
buffer = readMod(parent);
assert buffer.hasArray() && buffer.arrayOffset() == 0 && buffer.position() == 0;
}
public ByteBuffer getBuffer() {
return buffer;
}
public void rewind() {
pos = 0;
}
@Override
public int read() throws IOException {
if (pos >= buffer.limit()) {
return -1;
} else {
return buffer.get(pos++) & 0xff;
}
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int rem = buffer.limit() - pos;
if (rem <= 0) {
return -1;
} else {
len = Math.min(len, rem);
System.arraycopy(buffer.array(), pos, b, off, len);
pos += len;
return len;
}
}
}
static ByteBuffer readMod(InputStream is) throws IOException {
int available = is.available();
boolean availableGood = available > 1;
byte[] buffer = new byte[availableGood ? available : 30_000];
int offset = 0;
int len;
while ((len = is.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += len;
if (offset == buffer.length) {
if (availableGood) {
int val = is.read();
if (val < 0) break;
availableGood = false;
buffer = Arrays.copyOf(buffer, Math.max(buffer.length * 2, 30_000));
buffer[offset++] = (byte) val;
} else {
buffer = Arrays.copyOf(buffer, buffer.length * 2);
}
}
}
return ByteBuffer.wrap(buffer, 0, offset);
}
private static class NestedModInitData {
final List<? extends Future<ModCandidate>> futures;
final List<ModCandidate> target;
NestedModInitData(List<? extends Future<ModCandidate>> futures, List<ModCandidate> target) {
this.futures = futures;
this.target = target;
}
}
}