Skip to content

Commit

Permalink
Merge branch 'upstream'
Browse files Browse the repository at this point in the history
# Conflicts:
#	build.gradle
#	src/main/java/net/fabricmc/meta/FabricMeta.java
#	src/main/java/net/fabricmc/meta/data/VersionDatabase.java
#	src/main/java/net/fabricmc/meta/utils/LoaderMeta.java
#	src/main/java/net/fabricmc/meta/web/ProfileHandler.java
#	src/main/java/net/fabricmc/meta/web/ServerBootstrap.java
#	src/main/java/net/fabricmc/meta/web/models/MavenUrlVersion.java
  • Loading branch information
thecatcore committed Feb 6, 2024
2 parents 8680a3d + 5366767 commit afee67e
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 234 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ plugins {
id 'application'
id 'maven-publish'
id 'org.cadixdev.licenser' version '0.6.1'
id "com.github.gmazzo.buildconfig" version "5.2.0"
id 'eclipse'
id "com.github.gmazzo.buildconfig" version "5.2.0"
}

def legacyVersion = "1.7.6"
group 'net.legacyfabric'
version '1.5.2'
version '1.5.3'

version = legacyVersion + "+fabricmc." + project.version

Expand Down
84 changes: 74 additions & 10 deletions src/main/java/net/fabricmc/meta/FabricMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,60 @@

package net.fabricmc.meta;

import net.fabricmc.meta.data.VersionDatabase;
import net.fabricmc.meta.web.WebServer;
import net.legacyfabric.meta.data.LegacyVersionDatabase;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class FabricMeta {
import com.google.gson.stream.JsonReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.fabricmc.meta.data.VersionDatabase;
import net.fabricmc.meta.utils.Reference;
import net.fabricmc.meta.web.WebServer;

public class FabricMeta {
public static volatile VersionDatabase database;

private static final Logger LOGGER = LoggerFactory.getLogger(VersionDatabase.class);
private static final Map<String, String> config = new HashMap<>();
private static boolean configInitialized;
private static URL heartbeatUrl; // URL pinged with every successful update()

public static void main(String[] args) {
Path configFile = Paths.get("config.json");

if (Files.exists(configFile)) {
try (JsonReader reader = new JsonReader(Files.newBufferedReader(configFile))) {
reader.beginObject();

while (reader.hasNext()) {
config.put(reader.nextName(), reader.nextString());
}

reader.endObject();

String heartbeatUrlString = config.get("heartbeatUrl");

if (heartbeatUrlString != null) {
heartbeatUrl = new URL(heartbeatUrlString);
}
} catch (IOException | IllegalStateException e) {
throw new RuntimeException("malformed config in "+configFile, e);
}
}

configInitialized = true;

LOGGER.info("Starting with local maven {}", Reference.LOCAL_FABRIC_MAVEN_URL);

update();

Expand All @@ -43,13 +82,38 @@ public static void main(String[] args) {
private static void update(){
try {
database = VersionDatabase.generate(new LegacyVersionDatabase());
} catch (IOException | XMLStreamException e) {
if(database == null){
throw new RuntimeException(e);
updateHeartbeat();
} catch (Throwable t) {
if (database == null){
throw new RuntimeException(t);
} else {
e.printStackTrace();
LOGGER.warn("update failed", t);
}
}
}

private static void updateHeartbeat() {
if (heartbeatUrl == null) return;

try {
HttpURLConnection conn = (HttpURLConnection) heartbeatUrl.openConnection();
conn.setRequestMethod("HEAD");
conn.setConnectTimeout(500);
conn.setReadTimeout(500);

int status = conn.getResponseCode();

if (status != HttpURLConnection.HTTP_OK) {
LOGGER.warn("heartbeat request failed with status {}", status);
}
} catch (IOException e) {
LOGGER.warn("heartbeat request failed: {}", e.toString());
}
}

public static Map<String, String> getConfig() {
if (!configInitialized) throw new IllegalStateException("accessing config before initialization"); // to catch any accidental early access through <clinit> etc

return config;
}
}
31 changes: 18 additions & 13 deletions src/main/java/net/fabricmc/meta/data/VersionDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@

package net.fabricmc.meta.data;

import net.fabricmc.meta.utils.MinecraftLauncherMeta;
import net.fabricmc.meta.utils.PomParser;
import net.fabricmc.meta.web.models.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.SimpleLoggerFactory;
import static net.fabricmc.meta.utils.Reference.LOCAL_FABRIC_MAVEN_URL;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -33,13 +27,24 @@
import java.util.function.Function;
import java.util.stream.Collectors;

public class VersionDatabase {
public static final String MAVEN_URL = "https://maven.fabricmc.net/";
import javax.xml.stream.XMLStreamException;

public static final PomParser MAPPINGS_PARSER = new PomParser(MAVEN_URL + "net/fabricmc/yarn/maven-metadata.xml");
public static final PomParser INTERMEDIARY_PARSER = new PomParser(MAVEN_URL + "net/fabricmc/intermediary/maven-metadata.xml");
public static final PomParser LOADER_PARSER = new PomParser(MAVEN_URL + "net/fabricmc/fabric-loader/maven-metadata.xml");
public static final PomParser INSTALLER_PARSER = new PomParser(MAVEN_URL + "net/fabricmc/fabric-installer/maven-metadata.xml");
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.fabricmc.meta.utils.MinecraftLauncherMeta;
import net.fabricmc.meta.utils.PomParser;
import net.fabricmc.meta.web.models.BaseVersion;
import net.fabricmc.meta.web.models.MavenBuildGameVersion;
import net.fabricmc.meta.web.models.MavenBuildVersion;
import net.fabricmc.meta.web.models.MavenUrlVersion;
import net.fabricmc.meta.web.models.MavenVersion;

public class VersionDatabase {
public static final PomParser MAPPINGS_PARSER = new PomParser(LOCAL_FABRIC_MAVEN_URL + "net/fabricmc/yarn/maven-metadata.xml");
public static final PomParser INTERMEDIARY_PARSER = new PomParser(LOCAL_FABRIC_MAVEN_URL + "net/fabricmc/intermediary/maven-metadata.xml");
public static final PomParser LOADER_PARSER = new PomParser(LOCAL_FABRIC_MAVEN_URL + "net/fabricmc/fabric-loader/maven-metadata.xml");
public static final PomParser INSTALLER_PARSER = new PomParser(LOCAL_FABRIC_MAVEN_URL + "net/fabricmc/fabric-installer/maven-metadata.xml");

private static final ArrayList<String> incorrectVersions = new ArrayList<>();
private static final Logger LOGGER = LoggerFactory.getLogger(VersionDatabase.class);
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/net/fabricmc/meta/utils/LoaderMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@

package net.fabricmc.meta.utils;

import com.google.gson.JsonObject;
import net.fabricmc.meta.data.VersionDatabase;
import net.fabricmc.meta.web.WebServer;
import net.fabricmc.meta.web.models.LoaderInfoBase;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;

public class LoaderMeta {
import com.google.gson.JsonObject;
import org.apache.commons.io.FileUtils;

import net.fabricmc.meta.web.WebServer;
import net.fabricmc.meta.web.models.LoaderInfoBase;

public class LoaderMeta {
public static final File BASE_DIR = new File("metadata");
public static final String MAVEN_URL = "https://maven.fabricmc.net/";

public static JsonObject getMeta(LoaderInfoBase loaderInfo){
String loaderMaven = loaderInfo.getLoader().getMaven();
Expand All @@ -42,7 +40,7 @@ public static JsonObject getMeta(LoaderInfoBase loaderInfo){
File launcherMetaFile = new File(BASE_DIR, path + "/" + filename);
if(!launcherMetaFile.exists()){
try {
String url = String.format("%s%s/%s", MAVEN_URL, path, filename);
String url = String.format("%s%s/%s", Reference.LOCAL_FABRIC_MAVEN_URL, path, filename);
System.out.println("Downloading " + url);
FileUtils.copyURLToFile(new URL(url), launcherMetaFile);
} catch (IOException e) {
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/net/fabricmc/meta/utils/MinecraftLauncherMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@

package net.fabricmc.meta.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MinecraftLauncherMeta {
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.io.IOUtils;

public class MinecraftLauncherMeta {
public static final Gson GSON = new GsonBuilder().create();

List<Version> versions;
Expand All @@ -46,7 +48,7 @@ public static MinecraftLauncherMeta getMeta() throws IOException {
}

public static MinecraftLauncherMeta getExperimentalMeta() throws IOException {
String url = "https://maven.fabricmc.net/net/minecraft/experimental_versions.json";
String url = Reference.LOCAL_FABRIC_MAVEN_URL+"net/minecraft/experimental_versions.json";
String json = IOUtils.toString(new URL(url), StandardCharsets.UTF_8);
return GSON.fromJson(json, MinecraftLauncherMeta.class);
}
Expand Down
42 changes: 27 additions & 15 deletions src/main/java/net/fabricmc/meta/utils/PomParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

package net.fabricmc.meta.utils;

import net.fabricmc.meta.web.models.BaseVersion;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -34,6 +29,13 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import net.fabricmc.meta.web.models.BaseVersion;

public class PomParser {

public String path;
Expand All @@ -49,14 +51,24 @@ private void load() throws IOException, XMLStreamException {
versions.clear();

URL url = new URL(path);
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(url.openStream());
while (reader.hasNext()) {
if (reader.next() == XMLStreamConstants.START_ELEMENT && reader.getLocalName().equals("version")) {
String text = reader.getElementText();
versions.add(text);
URLConnection conn = url.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
XMLStreamReader reader = null;

try {
reader = XMLInputFactory.newInstance().createXMLStreamReader(conn.getInputStream());

while (reader.hasNext()) {
if (reader.next() == XMLStreamConstants.START_ELEMENT && reader.getLocalName().equals("version")) {
String text = reader.getElementText();
versions.add(text);
}
}
} finally {
if (reader != null) reader.close();
}
reader.close();

Collections.reverse(versions);
latestVersion = versions.get(0);
}
Expand Down Expand Up @@ -89,9 +101,9 @@ public <T extends BaseVersion> List<T> getMeta(Function<String, T> function, Str
// Read a file containing a new line separated list of versions that should not be marked as stable.
List<String> unstableVersions = Files.readAllLines(unstableVersionsPath);
list.stream()
.filter(v -> !unstableVersions.contains(v.getVersion()))
.findFirst()
.ifPresent(v -> v.setStable(true));
.filter(v -> !unstableVersions.contains(v.getVersion()))
.findFirst()
.ifPresent(v -> v.setStable(true));
} else {
stableIdentifier.process(list);
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/net/fabricmc/meta/utils/Reference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.meta.utils;

import net.fabricmc.meta.FabricMeta;

public final class Reference {
/**
* Fabric maven url to expose to the user.
*
* <p>This shouldn't be directly accessed by this meta server instance!
*/
public static final String FABRIC_MAVEN_URL = "https://maven.fabricmc.net/";

/**
* Fabric maven url to access from this meta server instance.
*
* <p>This is not to be included in any output data!
*/
public static final String LOCAL_FABRIC_MAVEN_URL = FabricMeta.getConfig().getOrDefault("localFabricMavenUrl", FABRIC_MAVEN_URL);
}
8 changes: 4 additions & 4 deletions src/main/java/net/fabricmc/meta/web/ProfileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import net.legacyfabric.meta.data.LegacyVersionDatabase;
import org.apache.commons.io.IOUtils;

import net.fabricmc.meta.utils.LoaderMeta;
import net.fabricmc.meta.utils.Reference;
import net.fabricmc.meta.web.models.LoaderInfoV2;

public class ProfileHandler {
Expand Down Expand Up @@ -116,8 +116,8 @@ private static JsonObject buildProfileJson(LoaderInfoV2 info, String side) {
JsonObject librariesObject = launcherMeta.get("libraries").getAsJsonObject();
// Build the libraries array with the existing libs + loader and intermediary
JsonArray libraries = (JsonArray) librariesObject.get("common");
libraries.add(getLibrary(info.getIntermediary().getMaven(), LegacyVersionDatabase.MAVEN_URL));
libraries.add(getLibrary(info.getLoader().getMaven(), VersionDatabase.MAVEN_URL));
libraries.add(formatLibrary(info.getIntermediary().getMaven(), LegacyVersionDatabase.MAVEN_URL));
libraries.add(formatLibrary(info.getLoader().getMaven(), Reference.FABRIC_MAVEN_URL));

if (librariesObject.has(side)) {
libraries.addAll(librariesObject.get(side).getAsJsonArray());
Expand Down Expand Up @@ -163,7 +163,7 @@ private static JsonObject buildProfileJson(LoaderInfoV2 info, String side) {
return profile;
}

private static JsonObject getLibrary(String mavenPath, String url) {
private static JsonObject formatLibrary(String mavenPath, String url) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", mavenPath);
jsonObject.addProperty("url", url);
Expand Down
Loading

0 comments on commit afee67e

Please sign in to comment.