From f84500016a43540472a1ac2c64074ecedb413dec Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Wed, 3 Apr 2024 10:50:16 +0200 Subject: [PATCH] Update to simpler mod loading from latest FML --- gradle.properties | 4 +- .../client/renderer/Sheets.java.patch | 2 +- .../neoforge/client/gui/ModListScreen.java | 3 +- .../client/loading/ClientModLoader.java | 17 ++-- .../neoforge/common/NeoForgeMod.java | 4 +- .../data/loading/DatagenModLoader.java | 6 +- .../neoforge/internal/CommonModLoader.java | 87 +++++++++++++++++++ .../internal/NeoForgeStatesProvider.java | 29 ------- .../neoforge/resource/ResourcePackLoader.java | 5 +- .../server/loading/ServerModLoader.java | 9 +- .../net.neoforged.fml.IModStateProvider | 1 - .../resources/assets/neoforge/lang/en_us.json | 2 +- 12 files changed, 112 insertions(+), 57 deletions(-) create mode 100644 src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java delete mode 100644 src/main/java/net/neoforged/neoforge/internal/NeoForgeStatesProvider.java delete mode 100644 src/main/resources/META-INF/services/net.neoforged.fml.IModStateProvider diff --git a/gradle.properties b/gradle.properties index 8fdd95951f..fc20ce68d7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,7 +19,7 @@ accesstransformers_version=10.0.1 coremods_version=6.0.4 eventbus_version=7.2.0 modlauncher_version=10.1.10 -securejarhandler_version=2.1.24 +securejarhandler_version=2.1.31 bootstraplauncher_version=1.1.2 asm_version=9.5 installer_version=2.1.+ @@ -31,7 +31,7 @@ jetbrains_annotations_version=24.0.1 slf4j_api_version=2.0.7 apache_maven_artifact_version=3.8.5 jarjar_version=0.4.0 -fancy_mod_loader_version=3.0.10 +fancy_mod_loader_version=3.0.12 mojang_logging_version=1.1.1 log4j_version=2.22.1 guava_version=31.1.2-jre diff --git a/patches/net/minecraft/client/renderer/Sheets.java.patch b/patches/net/minecraft/client/renderer/Sheets.java.patch index ca6cb16d0b..7674619fae 100644 --- a/patches/net/minecraft/client/renderer/Sheets.java.patch +++ b/patches/net/minecraft/client/renderer/Sheets.java.patch @@ -32,7 +32,7 @@ + } + + static { -+ if (net.neoforged.fml.ModLoader.isLoadingStateValid() && !net.neoforged.fml.ModLoader.get().hasCompletedState("LOAD_REGISTRIES")) { ++ if (net.neoforged.fml.ModLoader.isLoadingStateValid() && !net.neoforged.neoforge.internal.CommonModLoader.areRegistriesLoaded()) { + com.mojang.logging.LogUtils.getLogger().error( + "net.minecraft.client.renderer.Sheets loaded too early, modded registry-based materials may not work correctly", + new IllegalStateException("net.minecraft.client.renderer.Sheets loaded too early") diff --git a/src/main/java/net/neoforged/neoforge/client/gui/ModListScreen.java b/src/main/java/net/neoforged/neoforge/client/gui/ModListScreen.java index 43280faedc..47ff4ecf1a 100644 --- a/src/main/java/net/neoforged/neoforge/client/gui/ModListScreen.java +++ b/src/main/java/net/neoforged/neoforge/client/gui/ModListScreen.java @@ -40,7 +40,6 @@ import net.minecraft.server.packs.repository.PackSource; import net.minecraft.server.packs.resources.IoSupplier; import net.minecraft.util.FormattedCharSequence; -import net.neoforged.fml.ModContainer; import net.neoforged.fml.ModList; import net.neoforged.fml.VersionChecker; import net.neoforged.fml.loading.FMLPaths; @@ -402,7 +401,7 @@ public void upload() { lines.add(I18nExtension.getDisplayName(selectedMod)); lines.add(I18nExtension.parseMessage("fml.menu.mods.info.version", MavenVersionStringHelper.artifactVersionToString(selectedMod.getVersion()))); - lines.add(I18nExtension.parseMessage("fml.menu.mods.info.idstate", selectedMod.getModId(), ModList.get().getModContainerById(selectedMod.getModId()).map(ModContainer::getCurrentState).map(Object::toString).orElse("NONE"))); + lines.add(I18nExtension.parseMessage("fml.menu.mods.info.idstate", selectedMod.getModId(), "LOADED")); // TODO: remove mod loading stages from here too selectedMod.getConfig().getConfigElement("credits").ifPresent(credits -> lines.add(I18nExtension.parseMessage("fml.menu.mods.info.credits", credits))); selectedMod.getConfig().getConfigElement("authors").ifPresent(authors -> lines.add(I18nExtension.parseMessage("fml.menu.mods.info.authors", authors))); diff --git a/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java b/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java index 8bee7f4f37..d2fa6114af 100644 --- a/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java +++ b/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java @@ -31,6 +31,7 @@ import net.neoforged.neoforge.common.NeoForgeConfig; import net.neoforged.neoforge.common.util.LogicalSidedProvider; import net.neoforged.neoforge.internal.BrandingControl; +import net.neoforged.neoforge.internal.CommonModLoader; import net.neoforged.neoforge.logging.CrashReportExtender; import net.neoforged.neoforge.resource.ResourcePackLoader; import net.neoforged.neoforge.server.LanguageHook; @@ -38,7 +39,7 @@ import org.apache.logging.log4j.Logger; @OnlyIn(Dist.CLIENT) -public class ClientModLoader { +public class ClientModLoader extends CommonModLoader { private static final Logger LOGGER = LogManager.getLogger(); private static boolean loading; private static Minecraft mc; @@ -53,7 +54,7 @@ public static void begin(final Minecraft minecraft, final PackRepository default ClientModLoader.mc = minecraft; LogicalSidedProvider.setClient(() -> minecraft); LanguageHook.loadBuiltinLanguages(); - createRunnableWithCatch(() -> ModLoader.get().gatherAndInitializeMods(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor(), ImmediateWindowHandler::renderTick)).run(); + createRunnableWithCatch(() -> begin(ImmediateWindowHandler::renderTick)).run(); if (error == null) { ResourcePackLoader.populatePackRepository(defaultResourcePacks, PackType.CLIENT_RESOURCES); DataPackConfig.DEFAULT.addModPacks(ResourcePackLoader.getDataPackNames()); @@ -63,9 +64,9 @@ public static void begin(final Minecraft minecraft, final PackRepository default } private static CompletableFuture onResourceReload(final PreparableReloadListener.PreparationBarrier stage, final ResourceManager resourceManager, final ProfilerFiller prepareProfiler, final ProfilerFiller executeProfiler, final Executor asyncExecutor, final Executor syncExecutor) { - return CompletableFuture.runAsync(createRunnableWithCatch(() -> startModLoading(ModWorkManager.wrappedExecutor(syncExecutor), asyncExecutor)), ModWorkManager.parallelExecutor()) + return CompletableFuture.runAsync(createRunnableWithCatch(() -> startModLoading(syncExecutor, asyncExecutor)), ModWorkManager.parallelExecutor()) .thenCompose(stage::wait) - .thenRunAsync(() -> finishModLoading(ModWorkManager.wrappedExecutor(syncExecutor), asyncExecutor), ModWorkManager.parallelExecutor()); + .thenRunAsync(() -> finishModLoading(syncExecutor, asyncExecutor), ModWorkManager.parallelExecutor()); } private static Runnable createRunnableWithCatch(Runnable r) { @@ -79,12 +80,12 @@ private static Runnable createRunnableWithCatch(Runnable r) { }; } - private static void startModLoading(ModWorkManager.DrivenExecutor syncExecutor, Executor parallelExecutor) { - createRunnableWithCatch(() -> ModLoader.get().loadMods(syncExecutor, parallelExecutor, ImmediateWindowHandler::renderTick)).run(); + private static void startModLoading(Executor syncExecutor, Executor parallelExecutor) { + createRunnableWithCatch(() -> load(syncExecutor, parallelExecutor)).run(); } - private static void finishModLoading(ModWorkManager.DrivenExecutor syncExecutor, Executor parallelExecutor) { - createRunnableWithCatch(() -> ModLoader.get().finishMods(syncExecutor, parallelExecutor, ImmediateWindowHandler::renderTick)).run(); + private static void finishModLoading(Executor syncExecutor, Executor parallelExecutor) { + createRunnableWithCatch(() -> finish(syncExecutor, parallelExecutor)).run(); loading = false; loadingComplete = true; // reload game settings on main thread diff --git a/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java b/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java index 6495e3ad58..c74c079c8f 100644 --- a/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java +++ b/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java @@ -84,7 +84,6 @@ import net.neoforged.fml.CrashReportCallables; import net.neoforged.fml.ModContainer; import net.neoforged.fml.ModLoader; -import net.neoforged.fml.ModLoadingStage; import net.neoforged.fml.ModLoadingWarning; import net.neoforged.fml.StartupMessageManager; import net.neoforged.fml.VersionChecker; @@ -615,8 +614,7 @@ public NeoForgeMod(IEventBus modEventBus, Dist dist, ModContainer container) { if (isPRBuild(container.getModInfo().getVersion().toString())) { isPRBuild = true; ModLoader.get().addWarning(new ModLoadingWarning( - container.getModInfo(), ModLoadingStage.CONSTRUCT, - "loadwarning.neoforge.prbuild")); + container.getModInfo(), "loadwarning.neoforge.prbuild")); } } diff --git a/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java b/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java index f93630b021..e5fcbcd1c3 100644 --- a/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java +++ b/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java @@ -15,14 +15,14 @@ import net.minecraft.data.registries.VanillaRegistries; import net.minecraft.server.Bootstrap; import net.neoforged.fml.ModLoader; -import net.neoforged.fml.ModWorkManager; import net.neoforged.neoforge.client.ClientHooks; import net.neoforged.neoforge.common.data.ExistingFileHelper; import net.neoforged.neoforge.data.event.GatherDataEvent; +import net.neoforged.neoforge.internal.CommonModLoader; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -public class DatagenModLoader { +public class DatagenModLoader extends CommonModLoader { private static final Logger LOGGER = LogManager.getLogger(); private static GatherDataEvent.DataGeneratorConfig dataGeneratorConfig; private static ExistingFileHelper existingFileHelper; @@ -40,7 +40,7 @@ public static void begin(final Set mods, final Path path, final Collecti LOGGER.info("Initializing Data Gatherer for mods {}", mods); runningDataGen = true; Bootstrap.bootStrap(); - ModLoader.get().gatherAndInitializeMods(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor(), () -> {}); + begin(() -> {}); CompletableFuture lookupProvider = CompletableFuture.supplyAsync(VanillaRegistries::createLookup, Util.backgroundExecutor()); dataGeneratorConfig = new GatherDataEvent.DataGeneratorConfig(mods, path, inputs, lookupProvider, serverGenerators, clientGenerators, devToolGenerators, reportsGenerator, structureValidator, flat); diff --git a/src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java b/src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java new file mode 100644 index 0000000000..3f1f3d0813 --- /dev/null +++ b/src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java @@ -0,0 +1,87 @@ +/* + * Copyright (c) NeoForged and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.neoforged.neoforge.internal; + +import java.util.concurrent.Executor; +import net.neoforged.api.distmarker.Dist; +import net.neoforged.fml.ModList; +import net.neoforged.fml.ModLoader; +import net.neoforged.fml.ModWorkManager; +import net.neoforged.fml.config.ConfigTracker; +import net.neoforged.fml.config.ModConfig; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; +import net.neoforged.fml.event.lifecycle.FMLDedicatedServerSetupEvent; +import net.neoforged.fml.event.lifecycle.FMLLoadCompleteEvent; +import net.neoforged.fml.event.lifecycle.InterModEnqueueEvent; +import net.neoforged.fml.event.lifecycle.InterModProcessEvent; +import net.neoforged.fml.loading.FMLEnvironment; +import net.neoforged.fml.loading.FMLPaths; +import net.neoforged.neoforge.network.registration.NetworkRegistry; +import net.neoforged.neoforge.registries.GameData; +import net.neoforged.neoforge.registries.RegistryManager; +import org.jetbrains.annotations.ApiStatus; + +/** + * Internal class for handling the steps of mod loading that are common for client, data and server runs. + */ +@ApiStatus.Internal +public abstract class CommonModLoader { + private static boolean registriesLoaded = false; + + public static boolean areRegistriesLoaded() { + return registriesLoaded; + } + + protected static void begin(Runnable periodicTask) { + var syncExecutor = ModWorkManager.syncExecutor(); + + ModLoader.get().gatherAndInitializeMods(syncExecutor, ModWorkManager.parallelExecutor(), periodicTask); + + ModLoader.get().runInitTask("Registry initialization", syncExecutor, periodicTask, () -> { + RegistryManager.postNewRegistryEvent(); + GameData.unfreezeData(); + GameData.postRegisterEvents(); + GameData.freezeData(); + registriesLoaded = true; + }); + } + + protected static void load(Executor syncExecutor, Executor parallelExecutor) { + Runnable periodicTask = () -> {}; // no need to pass something else for now + + if (!ModLoader.isLoadingStateValid()) { + return; + } + + ModLoader.get().runInitTask("Config loading", syncExecutor, periodicTask, () -> { + if (FMLEnvironment.dist == Dist.CLIENT) { + ConfigTracker.INSTANCE.loadConfigs(ModConfig.Type.CLIENT, FMLPaths.CONFIGDIR.get()); + } + ConfigTracker.INSTANCE.loadConfigs(ModConfig.Type.COMMON, FMLPaths.CONFIGDIR.get()); + }); + + ModLoader.get().dispatchParallelEvent("Common setup", syncExecutor, parallelExecutor, periodicTask, FMLCommonSetupEvent::new); + ModLoader.get().dispatchParallelEvent("Sided setup", syncExecutor, parallelExecutor, periodicTask, + FMLEnvironment.dist.isClient() ? FMLClientSetupEvent::new : FMLDedicatedServerSetupEvent::new); + + ModLoader.get().runInitTask("Registration events", syncExecutor, periodicTask, RegistrationEvents::init); + } + + protected static void finish(Executor syncExecutor, Executor parallelExecutor) { + Runnable periodicTask = () -> {}; // no need to pass something else for now + + if (!ModLoader.isLoadingStateValid()) { + return; + } + + ModLoader.get().dispatchParallelEvent("Enqueue IMC", syncExecutor, parallelExecutor, periodicTask, InterModEnqueueEvent::new); + ModLoader.get().dispatchParallelEvent("Process IMC", syncExecutor, parallelExecutor, periodicTask, InterModProcessEvent::new); + ModLoader.get().dispatchParallelEvent("Complete loading of %d mods".formatted(ModList.get().size()), syncExecutor, parallelExecutor, periodicTask, FMLLoadCompleteEvent::new); + + ModLoader.get().runInitTask("Network registry lock", syncExecutor, periodicTask, NetworkRegistry.getInstance()::setup); + } +} diff --git a/src/main/java/net/neoforged/neoforge/internal/NeoForgeStatesProvider.java b/src/main/java/net/neoforged/neoforge/internal/NeoForgeStatesProvider.java deleted file mode 100644 index 50806aaca0..0000000000 --- a/src/main/java/net/neoforged/neoforge/internal/NeoForgeStatesProvider.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) Forge Development LLC and contributors - * SPDX-License-Identifier: LGPL-2.1-only - */ - -package net.neoforged.neoforge.internal; - -import java.util.List; -import net.neoforged.fml.IModLoadingState; -import net.neoforged.fml.IModStateProvider; -import net.neoforged.fml.ModLoadingPhase; -import net.neoforged.fml.ModLoadingState; -import net.neoforged.neoforge.network.registration.NetworkRegistry; -import net.neoforged.neoforge.registries.GameData; -import net.neoforged.neoforge.registries.RegistryManager; - -public class NeoForgeStatesProvider implements IModStateProvider { - final ModLoadingState CREATE_REGISTRIES = ModLoadingState.withInline("CREATE_REGISTRIES", "CONSTRUCT", ModLoadingPhase.GATHER, ml -> RegistryManager.postNewRegistryEvent()); - final ModLoadingState UNFREEZE = ModLoadingState.withInline("UNFREEZE_DATA", "CREATE_REGISTRIES", ModLoadingPhase.GATHER, ml -> GameData.unfreezeData()); - final ModLoadingState LOAD_REGISTRIES = ModLoadingState.withInline("LOAD_REGISTRIES", "UNFREEZE_DATA", ModLoadingPhase.GATHER, ml -> GameData.postRegisterEvents()); - final ModLoadingState FREEZE = ModLoadingState.withInline("FREEZE_DATA", "LOAD_REGISTRIES", ModLoadingPhase.GATHER, ml -> GameData.freezeData()); - final ModLoadingState REGISTRATION_EVENTS = ModLoadingState.withInline("REGISTRATION_EVENTS", "SIDED_SETUP", ModLoadingPhase.LOAD, ml -> RegistrationEvents.init()); - final ModLoadingState NETLOCK = ModLoadingState.withInline("NETWORK_LOCK", "COMPLETE", ModLoadingPhase.COMPLETE, ml -> NetworkRegistry.getInstance().setup()); - - @Override - public List getAllStates() { - return List.of(CREATE_REGISTRIES, UNFREEZE, LOAD_REGISTRIES, FREEZE, REGISTRATION_EVENTS, NETLOCK); - } -} diff --git a/src/main/java/net/neoforged/neoforge/resource/ResourcePackLoader.java b/src/main/java/net/neoforged/neoforge/resource/ResourcePackLoader.java index e4297e2730..45300f20f1 100644 --- a/src/main/java/net/neoforged/neoforge/resource/ResourcePackLoader.java +++ b/src/main/java/net/neoforged/neoforge/resource/ResourcePackLoader.java @@ -42,7 +42,6 @@ import net.minecraft.world.flag.FeatureFlagSet; import net.neoforged.fml.ModList; import net.neoforged.fml.ModLoader; -import net.neoforged.fml.ModLoadingStage; import net.neoforged.fml.ModLoadingWarning; import net.neoforged.neoforge.event.AddPackFindersEvent; import net.neoforged.neoforgespi.language.IModFileInfo; @@ -117,7 +116,7 @@ private static void packFinder(Map modResource MOD_PACK_SELECTION_CONFIG); if (modPack == null) { - ModLoader.get().addWarning(new ModLoadingWarning(mod, ModLoadingStage.ERROR, "fml.modloading.brokenresources", e.getKey())); + ModLoader.get().addWarning(new ModLoadingWarning(mod, "fml.modloading.brokenresources", e.getKey())); continue; } } else { @@ -135,7 +134,7 @@ private static void packFinder(Map modResource } } catch (IOException exception) { LOGGER.error("Failed to read pack.mcmeta file of mod {}", mod.getModId(), exception); - ModLoader.get().addWarning(new ModLoadingWarning(mod, ModLoadingStage.ERROR, "fml.modloading.brokenresources", e.getKey())); + ModLoader.get().addWarning(new ModLoadingWarning(mod, "fml.modloading.brokenresources", e.getKey())); } } diff --git a/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java b/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java index 64584ce067..b261b4a217 100644 --- a/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java +++ b/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java @@ -14,12 +14,13 @@ import net.neoforged.fml.ModWorkManager; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.common.util.LogicalSidedProvider; +import net.neoforged.neoforge.internal.CommonModLoader; import net.neoforged.neoforge.logging.CrashReportExtender; import net.neoforged.neoforge.server.LanguageHook; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -public class ServerModLoader { +public class ServerModLoader extends CommonModLoader { private static final Logger LOGGER = LogManager.getLogger(); private static boolean hasErrors = false; @@ -29,9 +30,9 @@ public static void load() { }); LanguageHook.loadBuiltinLanguages(); try { - ModLoader.get().gatherAndInitializeMods(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor(), () -> {}); - ModLoader.get().loadMods(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor(), () -> {}); - ModLoader.get().finishMods(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor(), () -> {}); + begin(() -> {}); + load(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor()); + finish(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor()); } catch (LoadingFailedException error) { ServerModLoader.hasErrors = true; // In case its not loaded properly diff --git a/src/main/resources/META-INF/services/net.neoforged.fml.IModStateProvider b/src/main/resources/META-INF/services/net.neoforged.fml.IModStateProvider deleted file mode 100644 index 5b6e0aae27..0000000000 --- a/src/main/resources/META-INF/services/net.neoforged.fml.IModStateProvider +++ /dev/null @@ -1 +0,0 @@ -net.neoforged.neoforge.internal.NeoForgeStatesProvider \ No newline at end of file diff --git a/src/main/resources/assets/neoforge/lang/en_us.json b/src/main/resources/assets/neoforge/lang/en_us.json index 42215556b7..3061fd53fc 100644 --- a/src/main/resources/assets/neoforge/lang/en_us.json +++ b/src/main/resources/assets/neoforge/lang/en_us.json @@ -65,7 +65,7 @@ "fml.modloading.failedtoloadmod":"{0,modinfo,name} ({0,modinfo,id}) has failed to load correctly\n\u00a77{2,exc,msg}", "fml.modloading.feature.missing": "{0,modinfo,name} ({0,modinfo,id}) is missing a feature it requires to run\n§7It requires {3,featurebound} but {4} is available", "fml.modloading.uncaughterror":"An uncaught parallel processing error has occurred.\n\u00a77{2,exc,msg}", - "fml.modloading.errorduringevent":"{0,modinfo,name} ({0,modinfo,id}) encountered an error during the {1,lower} event phase\n\u00a77{2,exc,msg}", + "fml.modloading.errorduringevent":"{0,modinfo,name} ({0,modinfo,id}) encountered an error while dispatching the {3} event\n\u00a77{2,exc,msg}", "fml.modloading.failedtoloadforge": "Failed to load NeoForge", "fml.modloading.missingdependency": "Mod \u00a7e{4}\u00a7r requires \u00a76{3}\u00a7r \u00a7o{5,vr}\u00a7r\n\u00a77Currently, \u00a76{3}\u00a7r\u00a77 is \u00a7o{6,i18n,fml.messages.artifactversion.ornotinstalled}§r\n{7,optional,§7Reason for the dependency: §r}",