Skip to content
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
4 changes: 4 additions & 0 deletions src/main/java/net/ornithemc/meta/data/VersionDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import net.ornithemc.meta.utils.PomDependencyParser;
import net.ornithemc.meta.utils.PomParser;
import net.ornithemc.meta.utils.VersionManifest;
import net.ornithemc.meta.web.LibraryUpgradesV3;
import net.ornithemc.meta.web.LibraryUpgradesV3.LibraryUpgrade;
import net.ornithemc.meta.web.models.*;

import javax.xml.stream.XMLStreamException;
Expand Down Expand Up @@ -73,6 +75,7 @@ public class VersionDatabase {
public List<MavenBuildGameVersion> nests;
public List<MavenUrlVersion> installer;
public List<MavenVersion> osl;
public List<LibraryUpgrade> libraryUpgrades;
private VersionDatabase() {
this.game = new Int2ObjectOpenHashMap<>();
this.intermediary = new Int2ObjectOpenHashMap<>();
Expand Down Expand Up @@ -176,6 +179,7 @@ public static VersionDatabase generate() throws IOException, XMLStreamException

database.oslModules.put(module, parser.getMeta(MavenVersion::new, "net.ornithemc.osl:" + module + ":"));
}
database.libraryUpgrades = LibraryUpgradesV3.get();
database.loadMcData();
OrnitheMeta.LOGGER.info("DB update took {}ms", System.currentTimeMillis() - start);
return database;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/ornithemc/meta/utils/VersionManifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

public class VersionManifest {

Expand All @@ -39,15 +38,15 @@ public VersionManifest() {
this.versions = new HashMap<>();
}

public Semver get(String id) {
public Semver getVersion(String id) {
return versions.computeIfAbsent(id, key -> {
try (InputStreamReader input = new InputStreamReader(new URL(String.format(DETAILS_URL, id)).openStream())) {
JsonNode details = OrnitheMeta.MAPPER.readTree(input);
String normalized = details.get("normalizedVersion").asText();

return new Semver(normalized);
} catch (IOException e) {
throw new NoSuchElementException("no version with id " + id + " exists!");
return null;
}
});
}
Expand Down
38 changes: 32 additions & 6 deletions src/main/java/net/ornithemc/meta/web/EndpointsV3.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import io.javalin.http.Handler;
import net.ornithemc.meta.OrnitheMeta;
import net.ornithemc.meta.data.VersionDatabase;
import net.ornithemc.meta.web.LibraryUpgradesV3.LibraryUpgrade;
import net.ornithemc.meta.web.models.BaseVersion;
import net.ornithemc.meta.web.models.Library;
import net.ornithemc.meta.web.models.LoaderInfoV3;
import net.ornithemc.meta.web.models.LoaderType;
import net.ornithemc.meta.web.models.MavenBuildGameVersion;
Expand Down Expand Up @@ -71,6 +73,8 @@ public static void setup() {
jsonGet("/nests", context -> withLimitSkip(context, OrnitheMeta.database.nests));
jsonGet("/nests/:game_version", context -> withLimitSkip(context, filter(context, OrnitheMeta.database.nests)));

jsonGetF("/libraries/:game_version", generation -> context -> withLimitSkip(context, getLibraries(context, generation)));

jsonGet("/fabric-loader", context -> withLimitSkip(context, OrnitheMeta.database.getLoader(LoaderType.FABRIC)));
jsonGetF("/fabric-loader/:game_version", generation -> context -> withLimitSkip(context, getLoaderInfoAll(context, generation, LoaderType.FABRIC)));
jsonGetF("/fabric-loader/:game_version/:loader_version", generation -> context -> getLoaderInfo(context, generation, LoaderType.FABRIC));
Expand Down Expand Up @@ -149,7 +153,24 @@ private static <T extends Predicate<String>> List<T> filter(Context context, Lis
return Collections.emptyList();
}
return versionList.stream().filter(t -> t.test(context.pathParam("game_version"))).collect(Collectors.toList());
}

private static List<Library> getLibraries(Context context, int generation) {
if (!context.pathParamMap().containsKey("game_version")) {
return null;
}

String gameVersion = context.pathParam("game_version");
Semver version = OrnitheMeta.database.manifest.getVersion(gameVersion);

if (version == null) {
return null;
}

return OrnitheMeta.database.libraryUpgrades.stream()
.filter(l -> l.test(generation, gameVersion))
.map(LibraryUpgrade::asLibrary)
.collect(Collectors.toList());
}

private static Object getLoaderInfo(Context context, int generation, LoaderType type) {
Expand Down Expand Up @@ -238,6 +259,12 @@ private static List<?> getOslModuleInfo(Context context) {

String module = context.pathParam("module");
String gameVersion = context.pathParam("game_version");
Semver version = OrnitheMeta.database.manifest.getVersion(gameVersion);

if (version == null) {
return null;
}

List<MavenVersion> versions = OrnitheMeta.database.getOslModule(module);

if (context.pathParamMap().containsKey("base_version")) {
Expand All @@ -249,11 +276,11 @@ private static List<?> getOslModuleInfo(Context context) {
}

versions = versions.stream()
.filter(version -> {
.filter(v -> {
String minGameVersion = null;
String maxGameVersion = null;

String buildVersion = version.getVersion();
String buildVersion = v.getVersion();
String[] parts = buildVersion.split("mc");

if (parts.length == 2) { // old format: <base version>+mc<min mc version>#<max mc version>
Expand All @@ -271,11 +298,10 @@ private static List<?> getOslModuleInfo(Context context) {
}

try {
Semver v = OrnitheMeta.database.manifest.get(gameVersion);
Semver vmin = OrnitheMeta.database.manifest.get(minGameVersion);
Semver vmax = OrnitheMeta.database.manifest.get(maxGameVersion);
Semver vmin = OrnitheMeta.database.manifest.getVersion(minGameVersion);
Semver vmax = OrnitheMeta.database.manifest.getVersion(maxGameVersion);

return v.compareTo(vmin) >= 0 && v.compareTo(vmax) <= 0;
return version.compareTo(vmin) >= 0 && version.compareTo(vmax) <= 0;
} catch (NoSuchElementException e) {
return false;
}
Expand Down
141 changes: 141 additions & 0 deletions src/main/java/net/ornithemc/meta/web/LibraryUpgradesV3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (c) 2019 FabricMC
*
* Modifications copyright (c) 2022 OrnitheMC
*
* 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.ornithemc.meta.web;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vdurmont.semver4j.Semver;

import net.ornithemc.meta.OrnitheMeta;
import net.ornithemc.meta.data.VersionDatabase;
import net.ornithemc.meta.web.models.Library;

public class LibraryUpgradesV3 {

private static final Path FILE_PATH = Paths.get("library-upgrades-v3.json");

private static List<LibraryUpgrade> cache;

public static List<LibraryUpgrade> get() {
reload();
validate();

return cache;
}

private static void reload() {
if (Files.exists(FILE_PATH)) {
try (InputStream is = Files.newInputStream(FILE_PATH);) {
cache = OrnitheMeta.MAPPER.readValue(is, new TypeReference<List<LibraryUpgrade>>() { });
} catch (IOException e) {
OrnitheMeta.LOGGER.warn("unable to load library upgrades from file", e);
}
}
}

private static void validate() {
if (cache == null) {
throw new RuntimeException("library upgrades v3 could not be read from file: file does not exist or is badly formatted");
}

for (LibraryUpgrade lib : cache) {
String name = lib.name;
String[] parts = name.split("[:]");

if (parts.length < 3 || parts.length > 4) {
throw new RuntimeException("invalid maven notation for library upgrade: " + name);
}

Integer minGeneration = lib.minIntermediaryGeneration;
Integer maxGeneration = lib.maxIntermediaryGeneration;

if (minGeneration != null && maxGeneration != null && minGeneration > maxGeneration) {
throw new RuntimeException("invalid intermediary generation bounds for library upgrade: " + name + " (" + minGeneration + " > " + maxGeneration + ")");
}

String minGameVersion = lib.minGameVersion;
String maxGameVersion = lib.maxGameVersion;

if (minGameVersion != null && maxGameVersion != null) {
Semver min = OrnitheMeta.database.manifest.getVersion(minGameVersion);
Semver max = OrnitheMeta.database.manifest.getVersion(maxGameVersion);

if (min == null) {
throw new RuntimeException("unknown minimum game version for library upgrade: " + name + " (" + minGameVersion + ")");
}
if (max == null) {
throw new RuntimeException("unknown maximum game version for library upgrade: " + name + " (" + maxGameVersion + ")");
}

if (min.compareTo(max) > 0) {
throw new RuntimeException("invalid game version bounds for library upgrade: " + name + " (" + minGameVersion + " > " + maxGameVersion + ")");
}
}
}
}

public static class LibraryUpgrade {

public String name;
public String url = VersionDatabase.MINECRAFT_LIBRARIES_URL;

public Integer minIntermediaryGeneration;
public Integer maxIntermediaryGeneration;
public String minGameVersion;
public String maxGameVersion;

public boolean test(int generation, String gameVersion) {
if (this.minIntermediaryGeneration != null && generation < this.minIntermediaryGeneration) {
return false;
}
if (this.maxIntermediaryGeneration != null && generation > this.maxIntermediaryGeneration) {
return false;
}

Semver v = OrnitheMeta.database.manifest.getVersion(gameVersion);

if (this.minGameVersion != null) {
Semver vmin = OrnitheMeta.database.manifest.getVersion(this.minGameVersion);

if (v.compareTo(vmin) < 0) {
return false;
}
}
if (this.maxGameVersion != null) {
Semver vmax = OrnitheMeta.database.manifest.getVersion(this.maxGameVersion);

if (v.compareTo(vmax) > 0) {
return false;
}
}

return true;
}

public Library asLibrary() {
return new Library(this.name, this.url);
}
}
}
18 changes: 17 additions & 1 deletion src/main/java/net/ornithemc/meta/web/ProfileHandlerV3.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import net.ornithemc.meta.OrnitheMeta;
import net.ornithemc.meta.data.VersionDatabase;
import net.ornithemc.meta.web.models.LoaderInfoV3;
import net.ornithemc.meta.web.models.LoaderType;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -130,7 +131,15 @@ private static JsonNode buildProfileJson(int generation, LoaderInfoV3 info, Stri
info.getGame(side),
generation);

ArrayNode libraries = ProfileLibraryManager.getLibraries(info, side);
JsonNode librariesNode = launcherMeta.get("libraries");
// Build the libraries array with the existing libs + loader and intermediary
ArrayNode libraries = (ArrayNode) librariesNode.get("common");
libraries.add(getLibrary(info.getIntermediary().getMaven(), VersionDatabase.ORNITHE_MAVEN_URL));
libraries.add(getLibrary(info.getLoader().getMaven(), info.getLoaderType().getMavenUrl()));

if (librariesNode.has(side)) {
libraries.addAll((ArrayNode) librariesNode.get(side));
}

String currentTime = ISO_8601.format(new Date());

Expand Down Expand Up @@ -168,4 +177,11 @@ private static JsonNode buildProfileJson(int generation, LoaderInfoV3 info, Stri

return profile;
}

private static JsonNode getLibrary(String mavenPath, String url) {
ObjectNode objectNode = OrnitheMeta.MAPPER.createObjectNode();
objectNode.put("name", mavenPath);
objectNode.put("url", url);
return objectNode;
}
}
Loading