-
Notifications
You must be signed in to change notification settings - Fork 29
/
ModLoader.java
388 lines (348 loc) · 18.3 KB
/
ModLoader.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
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.neoforged.fml;
import static net.neoforged.fml.Logging.CORE;
import static net.neoforged.fml.Logging.LOADING;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.EventPriority;
import net.neoforged.fml.event.IModBusEvent;
import net.neoforged.fml.event.lifecycle.FMLConstructModEvent;
import net.neoforged.fml.event.lifecycle.ParallelDispatchEvent;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.fml.loading.ImmediateWindowHandler;
import net.neoforged.fml.loading.LoadingModList;
import net.neoforged.fml.loading.moddiscovery.ModFileInfo;
import net.neoforged.fml.loading.moddiscovery.ModInfo;
import net.neoforged.fml.loading.progress.StartupNotificationManager;
import net.neoforged.neoforgespi.language.IModInfo;
import net.neoforged.neoforgespi.language.IModLanguageLoader;
import net.neoforged.neoforgespi.language.ModFileScanData;
import net.neoforged.neoforgespi.locating.ForgeFeature;
import net.neoforged.neoforgespi.locating.IModFile;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.ApiStatus;
/**
* Contains the logic to load mods, i.e. turn the {@link LoadingModList} into the {@link ModList},
* as well as initialization tasks for mods and methods to dispatch mod bus events.
*
* <p>For the mod initialization flow, see {@code CommonModLoader} in NeoForge.
*
* <p>For mod bus event dispatch, see {@link #postEvent(Event)} and related methods.
*/
public final class ModLoader {
private ModLoader() {}
private static final Logger LOGGER = LogManager.getLogger();
private static final List<ModLoadingIssue> loadingIssues = new ArrayList<>();
private static ModList modList;
static {
CrashReportCallables.registerCrashCallable("ModLauncher", FMLLoader::getLauncherInfo);
CrashReportCallables.registerCrashCallable("ModLauncher launch target", FMLLoader::launcherHandlerName);
CrashReportCallables.registerCrashCallable("ModLauncher services", ModLoader::computeModLauncherServiceList);
CrashReportCallables.registerCrashCallable("FML Language Providers", ModLoader::computeLanguageList);
}
private static String computeLanguageList() {
return "\n" + FMLLoader.getLanguageLoadingProvider().applyForEach(lp -> lp.name() + "@" + lp.getClass().getPackage().getImplementationVersion()).collect(Collectors.joining("\n\t\t", "\t\t", ""));
}
private static String computeModLauncherServiceList() {
final List<Map<String, String>> mods = FMLLoader.modLauncherModList();
return "\n" + mods.stream().map(mod -> mod.getOrDefault("file", "nofile") +
" " + mod.getOrDefault("name", "missing") +
" " + mod.getOrDefault("type", "NOTYPE") +
" " + mod.getOrDefault("description", "")).collect(Collectors.joining("\n\t\t", "\t\t", ""));
}
/**
* Run on the primary starting thread by ClientModLoader and ServerModLoader
*
* @param syncExecutor An executor to run tasks on the main thread
* @param parallelExecutor An executor to run tasks on a parallel loading thread pool
* @param periodicTask Optional periodic task to perform on the main thread while other activities run
*/
public static void gatherAndInitializeMods(final Executor syncExecutor, final Executor parallelExecutor, final Runnable periodicTask) {
var loadingModList = FMLLoader.getLoadingModList();
loadingIssues.clear();
loadingIssues.addAll(loadingModList.getModLoadingIssues());
ForgeFeature.registerFeature("javaVersion", ForgeFeature.VersionFeatureTest.forVersionString(IModInfo.DependencySide.BOTH, System.getProperty("java.version")));
ForgeFeature.registerFeature("openGLVersion", ForgeFeature.VersionFeatureTest.forVersionString(IModInfo.DependencySide.CLIENT, ImmediateWindowHandler.getGLVersion()));
FMLLoader.backgroundScanHandler.waitForScanToComplete(periodicTask);
final ModList modList = ModList.of(loadingModList.getModFiles().stream().map(ModFileInfo::getFile).toList(),
loadingModList.getMods());
if (hasErrors()) {
var loadingErrors = getLoadingErrors();
for (var loadingError : loadingErrors) {
LOGGER.fatal(CORE, "Error during pre-loading phase: {}", loadingError, loadingError.cause());
}
cancelLoading(modList);
throw new ModLoadingException(loadingIssues);
}
List<? extends ForgeFeature.Bound> failedBounds = loadingModList.getMods().stream()
.map(ModInfo::getForgeFeatures)
.flatMap(Collection::stream)
.filter(bound -> !ForgeFeature.testFeature(FMLEnvironment.dist, bound))
.toList();
if (!failedBounds.isEmpty()) {
LOGGER.fatal(CORE, "Failed to validate feature bounds for mods: {}", failedBounds);
for (var fb : failedBounds) {
loadingIssues.add(ModLoadingIssue.error("fml.modloading.feature.missing", null, fb, ForgeFeature.featureValue(fb)).withAffectedMod(fb.modInfo()));
}
cancelLoading(modList);
throw new ModLoadingException(loadingIssues);
}
var modContainers = loadingModList.getModFiles().stream()
.map(ModFileInfo::getFile)
.map(ModLoader::buildMods)
.<ModContainer>mapMulti(Iterable::forEach)
.toList();
if (hasErrors()) {
for (var loadingError : getLoadingErrors()) {
LOGGER.fatal(CORE, "Failed to initialize mod containers: {}", loadingError, loadingError.cause());
}
cancelLoading(modList);
throw new ModLoadingException(loadingIssues);
}
modList.setLoadedMods(modContainers);
ModLoader.modList = modList;
constructMods(syncExecutor, parallelExecutor, periodicTask);
}
private static void cancelLoading(ModList modList) {
StartupNotificationManager.modLoaderMessage("ERROR DURING MOD LOADING");
modList.setLoadedMods(Collections.emptyList());
}
@Deprecated(forRemoval = true, since = "20.5")
public static boolean isLoadingStateValid() {
return !hasErrors();
}
private static void constructMods(Executor syncExecutor, Executor parallelExecutor, Runnable periodicTask) {
var workQueue = new DeferredWorkQueue("Mod Construction");
dispatchParallelTask("Mod Construction", parallelExecutor, periodicTask, modContainer -> {
modContainer.constructMod();
modContainer.acceptEvent(new FMLConstructModEvent(modContainer, workQueue));
});
waitForTask("Mod Construction: Deferred Queue", periodicTask, CompletableFuture.runAsync(workQueue::runTasks, syncExecutor));
}
/**
* Runs a single task on the {@code syncExecutor}, while ticking the loading screen.
*/
public static void runInitTask(String name, Executor syncExecutor, Runnable periodicTask, Runnable initTask) {
waitForTask(name, periodicTask, CompletableFuture.runAsync(initTask, syncExecutor));
}
/**
* Dispatches a parallel event across all mod containers, with progress displayed on the loading screen.
*/
public static void dispatchParallelEvent(String name, Executor syncExecutor, Executor parallelExecutor, Runnable periodicTask, BiFunction<ModContainer, DeferredWorkQueue, ParallelDispatchEvent> eventConstructor) {
var workQueue = new DeferredWorkQueue(name);
dispatchParallelTask(name, parallelExecutor, periodicTask, modContainer -> {
modContainer.acceptEvent(eventConstructor.apply(modContainer, workQueue));
});
runInitTask(name + ": Deferred Queue", syncExecutor, periodicTask, workQueue::runTasks);
}
/**
* Waits for a task to complete, displaying the name of the task on the loading screen.
*/
public static void waitForTask(String name, Runnable periodicTask, CompletableFuture<?> future) {
var progress = StartupNotificationManager.addProgressBar(name, 0);
try {
waitForFuture(name, periodicTask, future);
} finally {
progress.complete();
}
}
/**
* Exception that is fired when a mod loading future cannot be executed because a dependent future failed.
* It is only used for control flow and easy filtering out, but never logged or propagated further.
*/
private static class DependentFutureFailedException extends RuntimeException {}
/**
* Dispatches a task across all mod containers in parallel, with progress displayed on the loading screen.
*/
public static void dispatchParallelTask(String name, Executor parallelExecutor, Runnable periodicTask, Consumer<ModContainer> task) {
var progress = StartupNotificationManager.addProgressBar(name, modList.size());
try {
periodicTask.run();
Map<IModInfo, CompletableFuture<Void>> modFutures = new IdentityHashMap<>(modList.size());
var futureList = modList.getSortedMods().stream()
.map(modContainer -> {
// Collect futures for all dependencies first
var depFutures = LoadingModList.get().getDependencies(modContainer.getModInfo()).stream()
.map(modInfo -> {
var future = modFutures.get(modInfo);
if (future == null) {
throw new IllegalStateException("Dependency future for mod %s which is a dependency of %s not found!".formatted(
modInfo.getModId(), modContainer.getModId()));
}
return future;
})
.toArray(CompletableFuture[]::new);
// Build the future for this container
var future = CompletableFuture.allOf(depFutures)
.<Void>handleAsync((void_, exception) -> {
if (exception != null) {
// If there was any exception, short circuit.
// The exception will already be handled by `waitForFuture` since it comes from another mod.
LOGGER.debug("Skipping {} task for mod {} because a dependency threw an exception.", name, modContainer.getModId());
progress.increment();
// Throw a marker exception to make sure that dependencies of *this* task don't get executed.
throw new DependentFutureFailedException();
}
try {
ModLoadingContext.get().setActiveContainer(modContainer);
task.accept(modContainer);
} finally {
progress.increment();
ModLoadingContext.get().setActiveContainer(null);
}
return null;
}, parallelExecutor);
modFutures.put(modContainer.getModInfo(), future);
return future;
})
.toList();
var singleFuture = ModList.gather(futureList)
.thenCompose(ModList::completableFutureFromExceptionList);
waitForFuture(name, periodicTask, singleFuture);
} finally {
progress.complete();
}
}
private static void waitForFuture(String name, Runnable periodicTask, CompletableFuture<?> future) {
while (true) {
periodicTask.run();
try {
future.get(50, TimeUnit.MILLISECONDS);
return;
} catch (ExecutionException e) {
// Merge all potential modloading issues
var errorCount = 0;
for (var error : e.getCause().getSuppressed()) {
if (error instanceof DependentFutureFailedException) {
continue;
} else if (error instanceof ModLoadingException modLoadingException) {
loadingIssues.addAll(modLoadingException.getIssues());
} else {
loadingIssues.add(ModLoadingIssue.error("fml.modloading.uncaughterror", name).withCause(e));
}
errorCount++;
}
LOGGER.fatal(LOADING, "Failed to wait for future {}, {} errors found", name, errorCount);
cancelLoading(modList);
throw new ModLoadingException(loadingIssues);
} catch (Exception ignored) {}
}
}
private static List<ModContainer> buildMods(final IModFile modFile) {
final Map<IModLanguageLoader, Set<ModContainer>> byLoader = new IdentityHashMap<>();
var containers = modFile.getModFileInfo()
.getMods()
.stream()
.map(info -> {
var container = buildModContainerFromTOML(info, modFile.getScanResult());
var cont = byLoader.computeIfAbsent(info.getLoader(), k -> new HashSet<>());
if (container != null) cont.add(container);
return container;
})
.filter(Objects::nonNull)
.toList();
byLoader.forEach((loader, loaded) -> loader.validate(modFile, loaded, ModLoader::addLoadingIssue));
return containers;
}
private static ModContainer buildModContainerFromTOML(final IModInfo modInfo, final ModFileScanData scanData) {
try {
return modInfo.getLoader().loadMod(modInfo, scanData, FMLLoader.getGameLayer());
} catch (ModLoadingException mle) {
// exceptions are caught and added to the error list for later handling
loadingIssues.addAll(mle.getIssues());
// return a null container here because we tried and failed building a container.
return null;
}
}
public static <T extends Event & IModBusEvent> void runEventGenerator(Function<ModContainer, T> generator) {
if (hasErrors()) {
LOGGER.error("Cowardly refusing to send event generator to a broken mod state");
return;
}
// Construct events
List<ModContainer> modContainers = ModList.get().getSortedMods();
List<T> events = new ArrayList<>(modContainers.size());
ModList.get().forEachModInOrder(mc -> events.add(generator.apply(mc)));
// Post them
for (EventPriority phase : EventPriority.values()) {
for (int i = 0; i < modContainers.size(); i++) {
modContainers.get(i).acceptEvent(phase, events.get(i));
}
}
}
public static <T extends Event & IModBusEvent> void postEvent(T e) {
if (hasErrors()) {
LOGGER.error("Cowardly refusing to send event {} to a broken mod state", e.getClass().getName());
return;
}
for (EventPriority phase : EventPriority.values()) {
ModList.get().forEachModInOrder(mc -> mc.acceptEvent(phase, e));
}
}
public static <T extends Event & IModBusEvent> T postEventWithReturn(T e) {
postEvent(e);
return e;
}
public static <T extends Event & IModBusEvent> void postEventWrapContainerInModOrder(T event) {
postEventWithWrapInModOrder(event, (mc, e) -> ModLoadingContext.get().setActiveContainer(mc), (mc, e) -> ModLoadingContext.get().setActiveContainer(null));
}
public static <T extends Event & IModBusEvent> void postEventWithWrapInModOrder(T e, BiConsumer<ModContainer, T> pre, BiConsumer<ModContainer, T> post) {
if (hasErrors()) {
LOGGER.error("Cowardly refusing to send event {} to a broken mod state", e.getClass().getName());
return;
}
for (EventPriority phase : EventPriority.values()) {
ModList.get().forEachModInOrder(mc -> {
pre.accept(mc, e);
mc.acceptEvent(phase, e);
post.accept(mc, e);
});
}
}
/**
* @return If the errors occurred during mod loading. Use if you interact with vanilla systems directly during loading
* and don't want to cause extraneous crashes due to trying to do things that aren't possible.
* If you are running in a Mixin before mod loading has actually started, check {@link LoadingModList#hasErrors()} instead.
*/
public static boolean hasErrors() {
return loadingIssues.stream().anyMatch(issue -> issue.severity() == ModLoadingIssue.Severity.ERROR);
}
@ApiStatus.Internal
public static List<ModLoadingIssue> getLoadingErrors() {
return loadingIssues.stream().filter(issue -> issue.severity() == ModLoadingIssue.Severity.ERROR).toList();
}
@ApiStatus.Internal
public static List<ModLoadingIssue> getLoadingWarnings() {
return loadingIssues.stream().filter(issue -> issue.severity() == ModLoadingIssue.Severity.WARNING).toList();
}
@ApiStatus.Internal
public static List<ModLoadingIssue> getLoadingIssues() {
return List.copyOf(loadingIssues);
}
@ApiStatus.Internal
public static void addLoadingIssue(ModLoadingIssue issue) {
loadingIssues.add(issue);
}
}