From 1b99b2cea41834c47940802d6881c98ee25990cf Mon Sep 17 00:00:00 2001 From: Jonathing Date: Tue, 21 Oct 2025 20:51:24 -0400 Subject: [PATCH] Revert "Remove runtime mods.toml string substitution (#10698)" (#10701) This reverts commit fc80e3028943237e60024d513f274a30c8457e71, 17ce723edf3f8e53c206db0c0d8838f041db5a04, and 0e6341a23eb206aa946fde648b6429d09e1c8481. --- build_forge.gradle | 9 +--- .../fml/loading/StringSubstitutor.java | 44 +++++++++++++++++++ .../fml/loading/moddiscovery/ModFile.java | 9 ++-- .../fml/loading/moddiscovery/ModFileInfo.java | 1 + .../fml/loading/moddiscovery/ModInfo.java | 2 + src/main/resources/META-INF/mods.toml | 2 +- 6 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 fmlloader/src/main/java/net/minecraftforge/fml/loading/StringSubstitutor.java diff --git a/build_forge.gradle b/build_forge.gradle index 89561e4e67..4be10a27a6 100644 --- a/build_forge.gradle +++ b/build_forge.gradle @@ -839,19 +839,12 @@ tasks.register('filterJarNewSRG', FilterNewJar).configure { blacklist = filterJarNew.blacklist } -tasks.named('universalJar', Jar) { +tasks.named('universalJar').configure { dependsOn downloadCrowdin from zipTree(downloadCrowdin.dest).matching { include 'assets/forge/lang/*.json' } - final Map replaceProperties = Map.of('forge_version', FORGE_VERSION) - inputs.properties replaceProperties - - filesMatching(['META-INF/mods.toml']) { - expand replaceProperties + [project: project] - } - from(EXTRA_TXTS) duplicatesStrategy = DuplicatesStrategy.EXCLUDE exclude '.cache' diff --git a/fmlloader/src/main/java/net/minecraftforge/fml/loading/StringSubstitutor.java b/fmlloader/src/main/java/net/minecraftforge/fml/loading/StringSubstitutor.java new file mode 100644 index 0000000000..21c3e625f8 --- /dev/null +++ b/fmlloader/src/main/java/net/minecraftforge/fml/loading/StringSubstitutor.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.fml.loading; + +import net.minecraftforge.fml.loading.moddiscovery.ModFile; + +import java.util.Map; + +import org.apache.commons.lang3.text.StrLookup; +import org.apache.commons.lang3.text.StrSubstitutor; + +@SuppressWarnings("deprecation") +public class StringSubstitutor { + private static final Map GLOBALS = Map.of( + "mcVersion", FMLLoader.versionInfo().mcVersion(), + "forgeVersion", FMLLoader.versionInfo().forgeVersion() + ); + + public static String replace(final String in, final ModFile file) { + return new StrSubstitutor(getStringLookup(file)).replace(in); + } + + private static StrLookup getStringLookup(final ModFile file) { + return new StrLookup<>() { + @Override + public String lookup(String key) { + var parts = key.split("\\."); + if (parts.length == 1) + return key; + var pfx = parts[0]; + + if ("global".equals(pfx)) + return GLOBALS.get(parts[1]); + else if ("file".equals(pfx) && file != null) + return String.valueOf(file.getSubstitutionMap().get().get(parts[1])); + + return key; + } + }; + } +} diff --git a/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModFile.java b/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModFile.java index 05201fef95..6869e1d121 100644 --- a/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModFile.java +++ b/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModFile.java @@ -5,6 +5,7 @@ package net.minecraftforge.fml.loading.moddiscovery; +import com.google.common.collect.ImmutableMap; import com.mojang.logging.LogUtils; import cpw.mods.jarhandling.SecureJar; import net.minecraftforge.fml.loading.FMLLoader; @@ -38,6 +39,7 @@ import java.util.jar.Manifest; public class ModFile implements IModFile { private static final Logger LOGGER = LogUtils.getLogger(); private final String jarVersion; + private Map fileProperties; private List loaders; private Throwable scanError; private final SecureJar jar; @@ -67,10 +69,9 @@ public class ModFile implements IModFile { } @Override - public Supplier> getSubstitutionMap() { - return Map::of; + public Supplier> getSubstitutionMap() { + return () -> ImmutableMap.builder().put("jarVersion", jarVersion).putAll(fileProperties).build(); } - @Override public Type getType() { return modFileType; @@ -150,7 +151,7 @@ public class ModFile implements IModFile { } public void setFileProperties(Map fileProperties) { - LOGGER.warn(LogMarkers.LOADING, "Setting file properties for the purposes of runtime mods.toml string substitution is no longer supported."); + this.fileProperties = fileProperties; } @Override diff --git a/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModFileInfo.java b/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModFileInfo.java index 8d9488dfad..f6ea9a2331 100644 --- a/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModFileInfo.java +++ b/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModFileInfo.java @@ -101,6 +101,7 @@ public class ModFileInfo implements IModFileInfo, IConfigurable { this.properties = config.>getConfigElement("properties") .orElse(Collections.emptyMap()); } + this.modFile.setFileProperties(this.properties); final List modConfigs = config.getConfigList("mods"); if (modConfigs == null || modConfigs.isEmpty()) diff --git a/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModInfo.java b/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModInfo.java index 5490093e6e..f41741e423 100644 --- a/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModInfo.java +++ b/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModInfo.java @@ -6,6 +6,7 @@ package net.minecraftforge.fml.loading.moddiscovery; import com.mojang.logging.LogUtils; +import net.minecraftforge.fml.loading.StringSubstitutor; import net.minecraftforge.fml.loading.StringUtils; import net.minecraftforge.forgespi.language.IConfigurable; import net.minecraftforge.forgespi.language.IModInfo; @@ -73,6 +74,7 @@ public record ModInfo( } ArtifactVersion version = config.getConfigElement("version") + .map(s -> StringSubstitutor.replace(s, owningFile.getFile())) .map(DefaultArtifactVersion::new) .orElse(DEFAULT_VERSION); diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 1f6747ac20..d863d55ca1 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -6,7 +6,7 @@ license="LGPL v2.1" [[mods]] modId="forge" -version="${forge_version}" +version="${global.forgeVersion}" updateJSONURL="https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json" displayURL="https://minecraftforge.net/" displayName="Forge"