Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use json on maven #113

Merged
merged 2 commits into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 40 additions & 32 deletions src/main/java/net/minecraftforge/gradle/common/BaseExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import net.minecraftforge.gradle.GradleConfigurationException;
import org.gradle.api.Project;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Map;
Expand All @@ -19,6 +16,7 @@ public class BaseExtension {
protected String runDir = "run";
private LinkedList<String> srgExtra = new LinkedList<String>();

protected Map<String, Map<String, int[]>> mcpJson;
protected boolean mappingsSet = false;
protected String mappingsChannel = null;
protected int mappingsVersion = -1;
Expand Down Expand Up @@ -144,39 +142,49 @@ protected void checkMappings() {
// mappings or mc version are null
if (!mappingsSet || "null".equals(version) || Strings.isNullOrEmpty(version) || customVersion != null)
return;
// mcp version mapping is not available
if (mcpJson == null) {
project.getLogger().warn("we couldn't check mcp version json.");
return;
}

// check if it exists
Map<String, int[]> versionMap = mcpJson.get(version);
if (versionMap == null)
throw new GradleConfigurationException("There are no mappings for MC " + version);

String channel = getMappingsChannelNoSubtype();
if (!checkMappingsVersion(channel, version, mappingsVersion, "HEAD")
&& !checkMappingsVersion(channel, version, mappingsVersion, "GET")) {
throw new GradleConfigurationException(
"There is no such MCP version " + mappingsVersion + " in channel " + channel + " for " + version + "."
);
}
}
int[] channelList = versionMap.get(channel);
if (channelList == null)
throw new GradleConfigurationException("There is no such MCP mapping channel named " + channel);

private boolean checkMappingsVersion(String channel, String version, int mappingsVersion, String method) {
HttpURLConnection con = null;
try {
URL url = new URL(
"https://maven.minecraftforge.net/de/oceanlabs/mcp" +
"/mcp_" + channel +
"/" + mappingsVersion + "-" + version +
"/mcp_" + channel + "-" + mappingsVersion + "-" + version + ".zip"
);

con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(method);
con.setInstanceFollowRedirects(true);
con.setRequestProperty("User-Agent", Constants.USER_AGENT);

con.connect();

return con.getResponseCode() == 200;
} catch (IOException e) {
return false;
} finally {
if (con != null) con.disconnect();
// all is well with the world
if (searchArray(channelList, mappingsVersion))
return;

// if it gets here.. it wasnt found. Now we try to actually find it..
for (Entry<String, Map<String, int[]>> mcEntry : mcpJson.entrySet()) {
for (Entry<String, int[]> channelEntry : mcEntry.getValue().entrySet()) {
// found it!
if (searchArray(channelEntry.getValue(), mappingsVersion)) {
boolean rightMc = mcEntry.getKey().equals(version);
boolean rightChannel = channelEntry.getKey().equals(channel);

// right channel, but wrong mc
if (rightChannel && !rightMc) {
throw new GradleConfigurationException("This mapping '" + getMappings() + "' exists only for MC " + mcEntry.getKey() + "!");
}

// right MC , but wrong channel
else if (rightMc && !rightChannel) {
throw new GradleConfigurationException("This mapping '" + getMappings() + "' doesnt exist! perhaps you meant '" + channelEntry.getKey() + "_" + mappingsVersion + "'");
}
}
}
}

// wasnt found
throw new GradleConfigurationException("The specified mapping '" + getMappings() + "' does not exist!");
}

private static boolean searchArray(int[] array, int key) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/minecraftforge/gradle/common/BasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void execute(Project proj) {
});

// do Mcp Snapshots Stuff
setVersionInfoJson();
project.getConfigurations().create(Constants.CONFIG_MCP_DATA);

// Separated module
Expand Down Expand Up @@ -168,6 +169,15 @@ public void execute(Project project) {

private static boolean displayBanner = true;

private void setVersionInfoJson() {
File jsonCache = Constants.cacheFile(project, "caches", "minecraft", "McpMappings.json");
File etagFile = new File(jsonCache.getAbsolutePath() + ".etag");

getExtension().mcpJson = JsonFactory.GSON.fromJson(
getWithEtag(Constants.MCP_JSON_URL, jsonCache, etagFile),
new TypeToken<Map<String, Map<String, int[]>>>() {}.getType());
}

public void afterEvaluate() {
if (getExtension().mappingsSet()) {
project.getDependencies().add(Constants.CONFIG_MCP_DATA, ImmutableMap.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public Boolean call(Object o) {

// MCP things
public static final String CONFIG_MCP_DATA = "mcpSnapshotDataConfig";
@SuppressWarnings("HttpUrlsUsage")
public static final String MCP_JSON_URL = "http://export.mcpbot.bspk.rs/versions.json";
public static final String MCP_JSON_URL = FORGE_MAVEN + "/de/oceanlabs/mcp/versions.json";

// things in the cache dir.
public static final String NATIVES_DIR = "{CACHE_DIR}/minecraft/net/minecraft/minecraft_natives/{MC_VERSION}";
Expand Down