-
Notifications
You must be signed in to change notification settings - Fork 269
/
FabricLoaderImpl.java
636 lines (511 loc) · 20 KB
/
FabricLoaderImpl.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
* 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;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.jetbrains.annotations.VisibleForTesting;
import org.objectweb.asm.Opcodes;
import net.fabricmc.accesswidener.AccessWidener;
import net.fabricmc.accesswidener.AccessWidenerReader;
import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.LanguageAdapter;
import net.fabricmc.loader.api.MappingResolver;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.ObjectShare;
import net.fabricmc.loader.api.entrypoint.EntrypointContainer;
import net.fabricmc.loader.impl.discovery.ArgumentModCandidateFinder;
import net.fabricmc.loader.impl.discovery.ClasspathModCandidateFinder;
import net.fabricmc.loader.impl.discovery.DirectoryModCandidateFinder;
import net.fabricmc.loader.impl.discovery.ModCandidateImpl;
import net.fabricmc.loader.impl.discovery.ModDiscoverer;
import net.fabricmc.loader.impl.discovery.ModResolutionException;
import net.fabricmc.loader.impl.discovery.ModResolver;
import net.fabricmc.loader.impl.discovery.RuntimeModRemapper;
import net.fabricmc.loader.impl.entrypoint.EntrypointStorage;
import net.fabricmc.loader.impl.game.GameProvider;
import net.fabricmc.loader.impl.launch.FabricLauncherBase;
import net.fabricmc.loader.impl.launch.knot.Knot;
import net.fabricmc.loader.impl.metadata.DependencyOverrides;
import net.fabricmc.loader.impl.metadata.EntrypointMetadata;
import net.fabricmc.loader.impl.metadata.LoaderModMetadata;
import net.fabricmc.loader.impl.metadata.VersionOverrides;
import net.fabricmc.loader.impl.util.DefaultLanguageAdapter;
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;
@SuppressWarnings("deprecation")
public final class FabricLoaderImpl extends net.fabricmc.loader.FabricLoader {
public static final FabricLoaderImpl INSTANCE = InitHelper.get();
public static final int ASM_VERSION = Opcodes.ASM9;
public static final String VERSION = "0.16.2";
public static final String MOD_ID = "fabricloader";
public static final String CACHE_DIR_NAME = ".fabric"; // relative to game dir
private static final String PROCESSED_MODS_DIR_NAME = "processedMods"; // relative to cache dir
public static final String REMAPPED_JARS_DIR_NAME = "remappedJars"; // relative to cache dir
private static final String TMP_DIR_NAME = "tmp"; // relative to cache dir
protected final Map<String, ModContainerImpl> modMap = new HashMap<>();
private List<ModCandidateImpl> modCandidates;
protected List<ModContainerImpl> mods = new ArrayList<>();
private final Map<String, LanguageAdapter> adapterMap = new HashMap<>();
private final EntrypointStorage entrypointStorage = new EntrypointStorage();
private final AccessWidener accessWidener = new AccessWidener();
private final ObjectShare objectShare = new ObjectShareImpl();
private boolean frozen = false;
private Object gameInstance;
private MappingResolver mappingResolver;
private GameProvider provider;
private Path gameDir;
private Path configDir;
private FabricLoaderImpl() { }
/**
* Freeze the FabricLoader, preventing additional mods from being loaded.
*/
public void freeze() {
if (frozen) {
throw new IllegalStateException("Already frozen!");
}
frozen = true;
finishModLoading();
}
public GameProvider getGameProvider() {
if (provider == null) throw new IllegalStateException("game provider not set (yet)");
return provider;
}
public GameProvider tryGetGameProvider() {
return provider;
}
public void setGameProvider(GameProvider provider) {
this.provider = provider;
setGameDir(provider.getLaunchDirectory());
}
private void setGameDir(Path gameDir) {
this.gameDir = gameDir;
this.configDir = gameDir.resolve("config");
}
@Override
public Object getGameInstance() {
return gameInstance;
}
@Override
public EnvType getEnvironmentType() {
return FabricLauncherBase.getLauncher().getEnvironmentType();
}
/**
* @return The game instance's root directory.
*/
@Override
public Path getGameDir() {
if (gameDir == null) throw new IllegalStateException("invoked too early?");
return gameDir;
}
@Override
@Deprecated
public File getGameDirectory() {
return getGameDir().toFile();
}
/**
* @return The game instance's configuration directory.
*/
@Override
public Path getConfigDir() {
if (!Files.exists(configDir)) {
try {
Files.createDirectories(configDir);
} catch (IOException e) {
throw new RuntimeException("Creating config directory", e);
}
}
return configDir;
}
@Override
@Deprecated
public File getConfigDirectory() {
return getConfigDir().toFile();
}
public void load() {
if (provider == null) throw new IllegalStateException("game provider not set");
if (frozen) throw new IllegalStateException("Frozen - cannot load additional mods!");
try {
setup();
} catch (ModResolutionException exception) {
if (exception.getCause() == null) {
throw FormattedException.ofLocalized("exception.incompatible", exception.getMessage());
} else {
throw FormattedException.ofLocalized("exception.incompatible", exception);
}
}
}
private void setup() throws ModResolutionException {
boolean remapRegularMods = isDevelopmentEnvironment();
VersionOverrides versionOverrides = new VersionOverrides();
DependencyOverrides depOverrides = new DependencyOverrides(configDir);
// discover mods
ModDiscoverer discoverer = new ModDiscoverer(versionOverrides, depOverrides);
discoverer.addCandidateFinder(new ClasspathModCandidateFinder());
discoverer.addCandidateFinder(new DirectoryModCandidateFinder(getModsDirectory0(), remapRegularMods));
discoverer.addCandidateFinder(new ArgumentModCandidateFinder(remapRegularMods));
Map<String, Set<ModCandidateImpl>> envDisabledMods = new HashMap<>();
modCandidates = discoverer.discoverMods(this, envDisabledMods);
// dump version and dependency overrides info
if (!versionOverrides.getAffectedModIds().isEmpty()) {
Log.info(LogCategory.GENERAL, "Versions overridden for %s", String.join(", ", versionOverrides.getAffectedModIds()));
}
if (!depOverrides.getAffectedModIds().isEmpty()) {
Log.info(LogCategory.GENERAL, "Dependencies overridden for %s", String.join(", ", depOverrides.getAffectedModIds()));
}
// resolve mods
modCandidates = ModResolver.resolve(modCandidates, getEnvironmentType(), envDisabledMods);
dumpModList(modCandidates);
dumpNonFabricMods(discoverer.getNonFabricMods());
Path cacheDir = gameDir.resolve(CACHE_DIR_NAME);
Path outputdir = cacheDir.resolve(PROCESSED_MODS_DIR_NAME);
// runtime mod remapping
if (remapRegularMods) {
if (System.getProperty(SystemProperties.REMAP_CLASSPATH_FILE) == null) {
Log.warn(LogCategory.MOD_REMAP, "Runtime mod remapping disabled due to no fabric.remapClasspathFile being specified. You may need to update loom.");
} else {
RuntimeModRemapper.remap(modCandidates, cacheDir.resolve(TMP_DIR_NAME), outputdir);
}
}
// shuffle mods in-dev to reduce the risk of false order reliance, apply late load requests
if (isDevelopmentEnvironment() && System.getProperty(SystemProperties.DEBUG_DISABLE_MOD_SHUFFLE) == null) {
Collections.shuffle(modCandidates);
}
String modsToLoadLate = System.getProperty(SystemProperties.DEBUG_LOAD_LATE);
if (modsToLoadLate != null) {
for (String modId : modsToLoadLate.split(",")) {
for (Iterator<ModCandidateImpl> it = modCandidates.iterator(); it.hasNext(); ) {
ModCandidateImpl mod = it.next();
if (mod.getId().equals(modId)) {
it.remove();
modCandidates.add(mod);
break;
}
}
}
}
// add mods
for (ModCandidateImpl mod : modCandidates) {
if (!mod.hasPath() && !mod.isBuiltin()) {
try {
mod.setPaths(Collections.singletonList(mod.copyToDir(outputdir, false)));
} catch (IOException e) {
throw new RuntimeException("Error extracting mod "+mod, e);
}
}
addMod(mod);
}
modCandidates = null;
}
@VisibleForTesting
public void dumpNonFabricMods(List<Path> nonFabricMods) {
if (nonFabricMods.isEmpty()) return;
StringBuilder outputText = new StringBuilder();
for (Path nonFabricMod : nonFabricMods) {
outputText.append("\n\t- ").append(nonFabricMod.getFileName());
}
int modsCount = nonFabricMods.size();
Log.warn(LogCategory.GENERAL, "Found %d non-fabric mod%s:%s", modsCount, modsCount != 1 ? "s" : "", outputText);
}
private void dumpModList(List<ModCandidateImpl> mods) {
StringBuilder modListText = new StringBuilder();
boolean[] lastItemOfNestLevel = new boolean[mods.size()];
List<ModCandidateImpl> topLevelMods = mods.stream()
.filter(mod -> mod.getParentMods().isEmpty())
.collect(Collectors.toList());
int topLevelModsCount = topLevelMods.size();
for (int i = 0; i < topLevelModsCount; i++) {
boolean lastItem = i == topLevelModsCount - 1;
if (lastItem) lastItemOfNestLevel[0] = true;
dumpModList0(topLevelMods.get(i), modListText, 0, lastItemOfNestLevel);
}
int modsCount = mods.size();
Log.info(LogCategory.GENERAL, "Loading %d mod%s:%n%s", modsCount, modsCount != 1 ? "s" : "", modListText);
}
private void dumpModList0(ModCandidateImpl mod, StringBuilder log, int nestLevel, boolean[] lastItemOfNestLevel) {
if (log.length() > 0) log.append('\n');
for (int depth = 0; depth < nestLevel; depth++) {
log.append(depth == 0 ? "\t" : lastItemOfNestLevel[depth] ? " " : " | ");
}
log.append(nestLevel == 0 ? "\t" : " ");
log.append(nestLevel == 0 ? "-" : lastItemOfNestLevel[nestLevel] ? " \\--" : " |--");
log.append(' ');
log.append(mod.getId());
log.append(' ');
log.append(mod.getVersion().getFriendlyString());
List<ModCandidateImpl> nestedMods = new ArrayList<>(mod.getNestedMods());
nestedMods.sort(Comparator.comparing(nestedMod -> nestedMod.getMetadata().getId()));
if (!nestedMods.isEmpty()) {
Iterator<ModCandidateImpl> iterator = nestedMods.iterator();
ModCandidateImpl nestedMod;
boolean lastItem;
while (iterator.hasNext()) {
nestedMod = iterator.next();
lastItem = !iterator.hasNext();
if (lastItem) lastItemOfNestLevel[nestLevel+1] = true;
dumpModList0(nestedMod, log, nestLevel + 1, lastItemOfNestLevel);
if (lastItem) lastItemOfNestLevel[nestLevel+1] = false;
}
}
}
private void finishModLoading() {
// add mods to classpath
// TODO: This can probably be made safer, but that's a long-term goal
for (ModContainerImpl mod : mods) {
if (!mod.getMetadata().getId().equals(MOD_ID) && !mod.getMetadata().getType().equals("builtin")) {
for (Path path : mod.getCodeSourcePaths()) {
FabricLauncherBase.getLauncher().addToClassPath(path);
}
}
}
setupLanguageAdapters();
setupMods();
}
public boolean hasEntrypoints(String key) {
return entrypointStorage.hasEntrypoints(key);
}
@Override
public <T> List<T> getEntrypoints(String key, Class<T> type) {
return entrypointStorage.getEntrypoints(key, type);
}
@Override
public <T> List<EntrypointContainer<T>> getEntrypointContainers(String key, Class<T> type) {
return entrypointStorage.getEntrypointContainers(key, type);
}
@Override
public <T> void invokeEntrypoints(String key, Class<T> type, Consumer<? super T> invoker) {
if (!hasEntrypoints(key)) {
Log.debug(LogCategory.ENTRYPOINT, "No subscribers for entrypoint '%s'", key);
return;
}
RuntimeException exception = null;
Collection<EntrypointContainer<T>> entrypoints = FabricLoaderImpl.INSTANCE.getEntrypointContainers(key, type);
Log.debug(LogCategory.ENTRYPOINT, "Iterating over entrypoint '%s'", key);
for (EntrypointContainer<T> container : entrypoints) {
try {
invoker.accept(container.getEntrypoint());
} catch (Throwable t) {
exception = ExceptionUtil.gatherExceptions(t,
exception,
exc -> new RuntimeException(String.format("Could not execute entrypoint stage '%s' due to errors, provided by '%s' at '%s'!",
key, container.getProvider().getMetadata().getId(), container.getDefinition()),
exc));
}
}
if (exception != null) {
throw exception;
}
}
@Override
public MappingResolver getMappingResolver() {
if (mappingResolver == null) {
final String targetNamespace = FabricLauncherBase.getLauncher().getTargetNamespace();
mappingResolver = new LazyMappingResolver(() -> new MappingResolverImpl(
FabricLauncherBase.getLauncher().getMappingConfiguration().getMappings(),
targetNamespace
), targetNamespace);
}
return mappingResolver;
}
@Override
public ObjectShare getObjectShare() {
return objectShare;
}
public ModCandidateImpl getModCandidate(String id) {
if (modCandidates == null) return null;
for (ModCandidateImpl mod : modCandidates) {
if (mod.getId().equals(id)) return mod;
}
return null;
}
@Override
public Optional<net.fabricmc.loader.api.ModContainer> getModContainer(String id) {
return Optional.ofNullable(modMap.get(id));
}
@Override
public Collection<ModContainer> getAllMods() {
return Collections.unmodifiableList(mods);
}
public List<ModContainerImpl> getModsInternal() {
return mods;
}
@Override
public boolean isModLoaded(String id) {
return modMap.containsKey(id);
}
@Override
public boolean isDevelopmentEnvironment() {
return FabricLauncherBase.getLauncher().isDevelopment();
}
private void addMod(ModCandidateImpl candidate) throws ModResolutionException {
ModContainerImpl container = new ModContainerImpl(candidate);
mods.add(container);
modMap.put(candidate.getId(), container);
for (String provides : candidate.getProvides()) {
modMap.put(provides, container);
}
}
private void setupLanguageAdapters() {
adapterMap.put("default", DefaultLanguageAdapter.INSTANCE);
for (ModContainerImpl mod : mods) {
// add language adapters
for (Map.Entry<String, String> laEntry : mod.getInfo().getLanguageAdapterDefinitions().entrySet()) {
if (adapterMap.containsKey(laEntry.getKey())) {
throw new RuntimeException("Duplicate language adapter key: " + laEntry.getKey() + "! (" + laEntry.getValue() + ", " + adapterMap.get(laEntry.getKey()).getClass().getName() + ")");
}
try {
adapterMap.put(laEntry.getKey(), (LanguageAdapter) Class.forName(laEntry.getValue(), true, FabricLauncherBase.getLauncher().getTargetClassLoader()).getDeclaredConstructor().newInstance());
} catch (Exception e) {
throw new RuntimeException("Failed to instantiate language adapter: " + laEntry.getKey(), e);
}
}
}
}
private void setupMods() {
for (ModContainerImpl mod : mods) {
try {
for (String in : mod.getInfo().getOldInitializers()) {
String adapter = mod.getInfo().getOldStyleLanguageAdapter();
entrypointStorage.addDeprecated(mod, adapter, in);
}
for (String key : mod.getInfo().getEntrypointKeys()) {
for (EntrypointMetadata in : mod.getInfo().getEntrypoints(key)) {
entrypointStorage.add(mod, key, in, adapterMap);
}
}
} catch (Exception e) {
throw new RuntimeException(String.format("Failed to setup mod %s (%s)", mod.getInfo().getName(), mod.getOrigin()), e);
}
}
}
public void loadAccessWideners() {
AccessWidenerReader accessWidenerReader = new AccessWidenerReader(accessWidener);
for (net.fabricmc.loader.api.ModContainer modContainer : getAllMods()) {
LoaderModMetadata modMetadata = (LoaderModMetadata) modContainer.getMetadata();
String accessWidener = modMetadata.getAccessWidener();
if (accessWidener == null) continue;
Path path = modContainer.findPath(accessWidener).orElse(null);
if (path == null) throw new RuntimeException(String.format("Missing accessWidener file %s from mod %s", accessWidener, modContainer.getMetadata().getId()));
try (BufferedReader reader = Files.newBufferedReader(path)) {
accessWidenerReader.read(reader, FabricLauncherBase.getLauncher().getTargetNamespace());
} catch (Exception e) {
throw new RuntimeException("Failed to read accessWidener file from mod " + modMetadata.getId(), e);
}
}
}
public void prepareModInit(Path newRunDir, Object gameInstance) {
if (!frozen) {
throw new RuntimeException("Cannot instantiate mods when not frozen!");
}
if (gameInstance != null && FabricLauncherBase.getLauncher() instanceof Knot) {
ClassLoader gameClassLoader = gameInstance.getClass().getClassLoader();
ClassLoader targetClassLoader = FabricLauncherBase.getLauncher().getTargetClassLoader();
boolean matchesKnot = (gameClassLoader == targetClassLoader);
boolean containsKnot = false;
if (matchesKnot) {
containsKnot = true;
} else {
gameClassLoader = gameClassLoader.getParent();
while (gameClassLoader != null && gameClassLoader.getParent() != gameClassLoader) {
if (gameClassLoader == targetClassLoader) {
containsKnot = true;
}
gameClassLoader = gameClassLoader.getParent();
}
}
if (!matchesKnot) {
if (containsKnot) {
Log.info(LogCategory.KNOT, "Environment: Target class loader is parent of game class loader.");
} else {
Log.warn(LogCategory.KNOT, "\n\n* CLASS LOADER MISMATCH! THIS IS VERY BAD AND WILL PROBABLY CAUSE WEIRD ISSUES! *\n"
+ " - Expected game class loader: %s\n"
+ " - Actual game class loader: %s\n"
+ "Could not find the expected class loader in game class loader parents!\n",
FabricLauncherBase.getLauncher().getTargetClassLoader(), gameClassLoader);
}
}
}
this.gameInstance = gameInstance;
if (gameDir != null) {
try {
if (!gameDir.toRealPath().equals(newRunDir.toRealPath())) {
Log.warn(LogCategory.GENERAL, "Inconsistent game execution directories: engine says %s, while initializer says %s...",
newRunDir.toRealPath(), gameDir.toRealPath());
setGameDir(newRunDir);
}
} catch (IOException e) {
Log.warn(LogCategory.GENERAL, "Exception while checking game execution directory consistency!", e);
}
} else {
setGameDir(newRunDir);
}
}
public AccessWidener getAccessWidener() {
return accessWidener;
}
/**
* Sets the game instance. This is only used in 20w22a+ by the dedicated server and should not be called by anything else.
*/
public void setGameInstance(Object gameInstance) {
if (getEnvironmentType() != EnvType.SERVER) {
throw new UnsupportedOperationException("Cannot set game instance on a client!");
}
if (this.gameInstance != null) {
throw new UnsupportedOperationException("Cannot overwrite current game instance!");
}
this.gameInstance = gameInstance;
}
@Override
public String[] getLaunchArguments(boolean sanitize) {
return getGameProvider().getLaunchArguments(sanitize);
}
@Override
protected Path getModsDirectory0() {
String directory = System.getProperty(SystemProperties.MODS_FOLDER);
return directory != null ? Paths.get(directory) : gameDir.resolve("mods");
}
/**
* Provides singleton for static init assignment regardless of load order.
*/
public static class InitHelper {
private static FabricLoaderImpl instance;
public static FabricLoaderImpl get() {
if (instance == null) instance = new FabricLoaderImpl();
return instance;
}
}
static {
LoaderUtil.verifyNotInTargetCl(FabricLoaderImpl.class);
}
}