Skip to content

Commit

Permalink
Start working on IntermediaryProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Jan 26, 2024
1 parent 29f1d25 commit 3f46955
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.legacyfabric.multifilament.provider;

public class FabricProvider implements IntermediaryProvider {
final FabricLikeMetadata metadata = new FabricLikeMetadata("https://meta.fabricmc.net/v2/versions/game");

@Override
public String getIntermediaryURL(String mcVersion) {
return "https://github.com/FabricMC/intermediary/raw/master/mappings/" + mcVersion + ".tiny";
}

@Override
public boolean supportVersion(String mcVersion) {
return metadata.contains(mcVersion);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.legacyfabric.multifilament.provider;

import org.gradle.internal.impldep.com.google.gson.Gson;
import org.gradle.internal.impldep.com.google.gson.JsonArray;
import org.gradle.internal.impldep.com.google.gson.JsonElement;
import org.gradle.internal.impldep.com.google.gson.JsonObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public interface IntermediaryProvider {
String getIntermediaryURL(String mcVersion);
boolean supportVersion(String mcVersion);

class FabricLikeMetadata {
private final String url;
private final List<String> versions = new ArrayList<>();
private static final Gson gson = new Gson();

public FabricLikeMetadata(String url) {
this.url = url;
}

private void computeData() {
try {
URL url1 = new URL(url);
try (InputStream stream = url1.openStream()) {
try (Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
JsonArray array = gson.fromJson(reader, JsonArray.class);

for (JsonElement element : array) {
if (element instanceof JsonObject object) {
if (object.has("version")) {
String version = object.get("version").getAsString();

if (!versions.contains(version)) versions.add(version);
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

public boolean contains(String version) {
if (versions.isEmpty()) this.computeData();

return versions.contains(version);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.legacyfabric.multifilament.provider;

public class LegacyFabricProvider implements IntermediaryProvider {
private final int revision;
final FabricLikeMetadata metadata = new FabricLikeMetadata("https://meta.legacyfabric.net/v2/versions/game");

public LegacyFabricProvider(int revision) {
this.revision = revision;
}

@Override
public String getIntermediaryURL(String mcVersion) {
return "https://github.com/Legacy-Fabric/Legacy-Intermediaries/raw/v2" + getIntermediaryFolder() + "/mappings/" + mcVersion + ".tiny";
}

private String getIntermediaryFolder() {
switch (revision) {
case 1:
return "legacy/";
case 0:
return "legacy_old/";
default:
return "";
}
}

@Override
public boolean supportVersion(String mcVersion) {
return metadata.contains(mcVersion);
}
}

0 comments on commit 3f46955

Please sign in to comment.