Skip to content

Commit

Permalink
Check expected Minecraft version before running MCPConfig (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel authored Dec 16, 2023
1 parent 45bb684 commit 1ea6b89
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,23 @@
/**
* Data extracted from the MCPConfig JSON file.
*
* @param version the Minecraft version - the value of the {@code version} property
* @param data the value of the {@code data} property
* @param mappingsPath the path to srg mappings inside the MCP zip
* @param official the value of the {@code official} property
* @param steps the MCP step definitions by environment type
* @param functions the MCP function definitions by name
*/
public record McpConfigData(
String version,
JsonObject data,
String mappingsPath,
boolean official,
Map<String, List<McpConfigStep>> steps,
Map<String, McpConfigFunction> functions
) {
public static McpConfigData fromJson(JsonObject json) {
String version = json.get("version").getAsString();
JsonObject data = json.getAsJsonObject("data");
String mappingsPath = data.get("mappings").getAsString();
boolean official = json.has("official") && json.getAsJsonPrimitive("official").getAsBoolean();
Expand All @@ -73,6 +76,6 @@ public static McpConfigData fromJson(JsonObject json) {
functionsBuilder.put(key, McpConfigFunction.fromJson(functionsJson.getAsJsonObject(key)));
}

return new McpConfigData(data, mappingsPath, official, stepsBuilder.build(), functionsBuilder.build());
return new McpConfigData(version, data, mappingsPath, official, stepsBuilder.build(), functionsBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.configuration.providers.forge.ConfigValue;
import net.fabricmc.loom.configuration.providers.forge.ForgeProvider;
import net.fabricmc.loom.configuration.providers.forge.mcpconfig.steplogic.ConstantLogic;
import net.fabricmc.loom.configuration.providers.forge.mcpconfig.steplogic.DownloadManifestFileLogic;
import net.fabricmc.loom.configuration.providers.forge.mcpconfig.steplogic.FunctionLogic;
Expand All @@ -69,6 +70,7 @@
import net.fabricmc.loom.util.ForgeToolExecutor;
import net.fabricmc.loom.util.download.DownloadBuilder;
import net.fabricmc.loom.util.function.CollectionUtil;
import net.fabricmc.loom.util.gradle.GradleUtils;

public final class McpExecutor {
private static final LogLevel STEP_LOG_LEVEL = LogLevel.LIFECYCLE;
Expand All @@ -92,9 +94,35 @@ public McpExecutor(Project project, MinecraftProvider minecraftProvider, Path ca
this.dependencySet.skip(step -> getStepLogic(step.name(), step.type()) instanceof NoOpLogic);
this.dependencySet.setIgnoreDependenciesFilter(step -> getStepLogic(step.name(), step.type()).hasNoContext());

checkMinecraftVersion(provider);
addDefaultFiles(provider, environment);
}

private void checkMinecraftVersion(McpConfigProvider provider) {
final String expected = provider.getData().version();
final String actual = minecraftProvider.minecraftVersion();

if (!expected.equals(actual)) {
final LoomGradleExtension extension = LoomGradleExtension.get(project);
final ForgeProvider forgeProvider = extension.getForgeProvider();
final String message = "%s %s is not for Minecraft %s (expected: %s)."
.formatted(
extension.getPlatform().get().displayName(),
forgeProvider.getVersion().getCombined(),
actual,
expected
);

if (GradleUtils.getBooleanProperty(project, Constants.Properties.ALLOW_MISMATCHED_PLATFORM_VERSION)) {
project.getLogger().warn(message);
} else {
final String fullMessage = "%s\nYou can suppress this error by adding '%s = true' to gradle.properties."
.formatted(message, Constants.Properties.ALLOW_MISMATCHED_PLATFORM_VERSION);
throw new UnsupportedOperationException(fullMessage);
}
}
}

private void addDefaultFiles(McpConfigProvider provider, String environment) {
for (Map.Entry<String, JsonElement> entry : provider.getData().data().entrySet()) {
if (entry.getValue().isJsonPrimitive()) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/fabricmc/loom/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public static final class Properties {
public static final String DISABLE_REMAPPED_VARIANTS = "fabric.loom.disableRemappedVariants";
public static final String DISABLE_PROJECT_DEPENDENT_MODS = "fabric.loom.disableProjectDependentMods";
public static final String LIBRARY_PROCESSORS = "fabric.loom.libraryProcessors";
public static final String ALLOW_MISMATCHED_PLATFORM_VERSION = "loom.allowMismatchedPlatformVersion";
}

public static final class Forge {
Expand Down

0 comments on commit 1ea6b89

Please sign in to comment.