From 8a4f8e9b65c29acc89fee80fea176293958bbb1a Mon Sep 17 00:00:00 2001 From: Player Date: Thu, 16 Nov 2023 08:43:09 -0500 Subject: [PATCH] Make local maven url configurable --- build.gradle | 1 + .../java/net/fabricmc/meta/FabricMeta.java | 46 ++- .../fabricmc/meta/data/VersionDatabase.java | 31 +- .../net/fabricmc/meta/utils/LoaderMeta.java | 15 +- .../meta/utils/MinecraftLauncherMeta.java | 16 +- .../net/fabricmc/meta/utils/Reference.java | 35 ++ .../net/fabricmc/meta/web/ProfileHandler.java | 8 +- .../fabricmc/meta/web/ServerBootstrap.java | 326 +++++++++--------- .../meta/web/models/MavenUrlVersion.java | 27 +- 9 files changed, 295 insertions(+), 210 deletions(-) create mode 100644 src/main/java/net/fabricmc/meta/utils/Reference.java diff --git a/build.gradle b/build.gradle index ae569a2..75f09ec 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,7 @@ plugins { id 'java' id 'maven-publish' id 'org.cadixdev.licenser' version '0.6.1' + id 'eclipse' } group 'net.fabricmc' diff --git a/src/main/java/net/fabricmc/meta/FabricMeta.java b/src/main/java/net/fabricmc/meta/FabricMeta.java index b0a4d72..4718366 100644 --- a/src/main/java/net/fabricmc/meta/FabricMeta.java +++ b/src/main/java/net/fabricmc/meta/FabricMeta.java @@ -16,18 +16,51 @@ package net.fabricmc.meta; -import net.fabricmc.meta.data.VersionDatabase; -import net.fabricmc.meta.web.WebServer; - +import java.io.IOException; +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 config = new HashMap<>(); + private static boolean configInitialized; + 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(); + } 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(); @@ -49,4 +82,9 @@ private static void update(){ } } + public static Map getConfig() { + if (!configInitialized) throw new IllegalStateException("accessing config before initialization"); // to catch any accidental early access through etc + + return config; + } } diff --git a/src/main/java/net/fabricmc/meta/data/VersionDatabase.java b/src/main/java/net/fabricmc/meta/data/VersionDatabase.java index 201b566..264a77f 100644 --- a/src/main/java/net/fabricmc/meta/data/VersionDatabase.java +++ b/src/main/java/net/fabricmc/meta/data/VersionDatabase.java @@ -16,13 +16,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 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; @@ -30,14 +25,24 @@ import java.util.List; import java.util.stream.Collectors; -public class VersionDatabase { +import javax.xml.stream.XMLStreamException; - public static final String MAVEN_URL = "https://maven.fabricmc.net/"; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; - 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 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 incorrectVersions = new ArrayList<>(); private static final Logger LOGGER = LoggerFactory.getLogger(VersionDatabase.class); @@ -107,7 +112,7 @@ private void loadMcData() throws IOException { public List getLoader() { return loader.stream().filter(VersionDatabase::isPublicLoaderVersion).collect(Collectors.toList()); } - + private static boolean isPublicLoaderVersion(BaseVersion version) { return true; } diff --git a/src/main/java/net/fabricmc/meta/utils/LoaderMeta.java b/src/main/java/net/fabricmc/meta/utils/LoaderMeta.java index dcb97fa..b1f28fd 100644 --- a/src/main/java/net/fabricmc/meta/utils/LoaderMeta.java +++ b/src/main/java/net/fabricmc/meta/utils/LoaderMeta.java @@ -16,21 +16,20 @@ package net.fabricmc.meta.utils; -import com.google.gson.JsonObject; -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(); @@ -41,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) { diff --git a/src/main/java/net/fabricmc/meta/utils/MinecraftLauncherMeta.java b/src/main/java/net/fabricmc/meta/utils/MinecraftLauncherMeta.java index 7d482b9..92c07a7 100644 --- a/src/main/java/net/fabricmc/meta/utils/MinecraftLauncherMeta.java +++ b/src/main/java/net/fabricmc/meta/utils/MinecraftLauncherMeta.java @@ -16,17 +16,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 versions; @@ -45,7 +47,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); } diff --git a/src/main/java/net/fabricmc/meta/utils/Reference.java b/src/main/java/net/fabricmc/meta/utils/Reference.java new file mode 100644 index 0000000..a3d145d --- /dev/null +++ b/src/main/java/net/fabricmc/meta/utils/Reference.java @@ -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. + * + *

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. + * + *

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); +} diff --git a/src/main/java/net/fabricmc/meta/web/ProfileHandler.java b/src/main/java/net/fabricmc/meta/web/ProfileHandler.java index b8d3286..6489fb6 100644 --- a/src/main/java/net/fabricmc/meta/web/ProfileHandler.java +++ b/src/main/java/net/fabricmc/meta/web/ProfileHandler.java @@ -35,7 +35,7 @@ import com.google.gson.JsonObject; 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 { @@ -113,8 +113,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(), LoaderMeta.MAVEN_URL)); - libraries.add(getLibrary(info.getLoader().getMaven(), LoaderMeta.MAVEN_URL)); + libraries.add(formatLibrary(info.getIntermediary().getMaven(), Reference.FABRIC_MAVEN_URL)); + libraries.add(formatLibrary(info.getLoader().getMaven(), Reference.FABRIC_MAVEN_URL)); if (librariesObject.has(side)) { libraries.addAll(librariesObject.get(side).getAsJsonArray()); @@ -159,7 +159,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); diff --git a/src/main/java/net/fabricmc/meta/web/ServerBootstrap.java b/src/main/java/net/fabricmc/meta/web/ServerBootstrap.java index 4520f3d..ae22c78 100644 --- a/src/main/java/net/fabricmc/meta/web/ServerBootstrap.java +++ b/src/main/java/net/fabricmc/meta/web/ServerBootstrap.java @@ -16,21 +16,16 @@ package net.fabricmc.meta.web; -import io.javalin.core.util.Header; -import io.javalin.http.BadRequestResponse; -import io.javalin.http.Context; -import io.javalin.http.Handler; -import io.javalin.http.InternalServerErrorResponse; -import net.fabricmc.meta.FabricMeta; -import net.fabricmc.meta.web.models.BaseVersion; -import org.apache.commons.io.FileUtils; - import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URL; import java.nio.charset.StandardCharsets; -import java.nio.file.*; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -38,155 +33,166 @@ import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import io.javalin.core.util.Header; +import io.javalin.http.BadRequestResponse; +import io.javalin.http.Context; +import io.javalin.http.Handler; +import io.javalin.http.InternalServerErrorResponse; +import org.apache.commons.io.FileUtils; + +import net.fabricmc.meta.FabricMeta; +import net.fabricmc.meta.utils.Reference; +import net.fabricmc.meta.web.models.BaseVersion; + public class ServerBootstrap { - private static final Path CACHE_DIR = Paths.get("metadata", "installer"); - private static final Executor WORKER_EXECUTOR = Executors.newSingleThreadExecutor(); - - public static void setup() { - // http://localhost:5555/v2/versions/loader/1.17.1/0.12.0/0.8.0/server/jar - WebServer.javalin.get("/v2/versions/loader/:game_version/:loader_version/:installer_version/server/jar", boostrapHandler()); - } - - private static Handler boostrapHandler() { - return ctx -> { - if (!ctx.queryParamMap().isEmpty()) { - // Cannot really afford people to cache bust this. - throw new BadRequestResponse("Query params not allowed on this endpoint."); - } - - final String installerVersion = getAndValidateVersion(ctx, FabricMeta.database.installer, "installer_version"); - final String gameVersion = getAndValidateVersion(ctx, FabricMeta.database.game, "game_version"); - final String loaderVersion = getAndValidateVersion(ctx, FabricMeta.database.getAllLoader(), "loader_version"); - - validateLoaderVersion(loaderVersion); - validateInstallerVersion(installerVersion); - - // Set the filename and cache headers - final String filename = String.format("fabric-server-mc.%s-loader.%s-launcher.%s.jar", gameVersion, loaderVersion, installerVersion); - ctx.header(Header.CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", filename)); - final String cacheControl = String.format("public, max-age=%d", getCacheDuration(ctx)); - ctx.header(Header.CACHE_CONTROL, cacheControl); - ctx.contentType("application/java-archive"); - - ctx.result(getResultStream(installerVersion, gameVersion, loaderVersion)); - }; - } - - private static String getAndValidateVersion(Context ctx, List versions, String name) { - String version = ctx.pathParam(name); - - for (V v : versions) { - if (version.equals("stable") && v.isStable()) { - return v.getVersion(); - } - - if (v.getVersion().equals(version)) { - return version; - } - } - - throw new BadRequestResponse("Unable to find valid version for " + name); - } - - private static void validateLoaderVersion(String loaderVersion) { - String[] versionSplit = loaderVersion.split("\\."); - - // future 1.x versions - if (Integer.parseInt(versionSplit[0]) > 0) { - return; - } - - // 0.12.x or newer - if (Integer.parseInt(versionSplit[1]) >= 12) { - return; - } - - throw new BadRequestResponse("Fabric loader 0.12 or higher is required for unattended server installs. Please use a newer fabric loader version, or the full installer."); - } - - private static void validateInstallerVersion(String installerVersion) { - String[] versionSplit = installerVersion.split("\\."); - - // future 1.x versions - if (Integer.parseInt(versionSplit[0]) > 0) { - return; - } - - // 0.8.x or newer - if (Integer.parseInt(versionSplit[1]) >= 8) { - return; - } - - throw new BadRequestResponse("Fabric Installer 0.8 or higher is required for unattended server installs."); - } - - private static CompletableFuture getResultStream(String installerVersion, String gameVersion, String loaderVersion) { - Path bundledJar = CACHE_DIR.resolve(String.format("fabric-server-mc.%s-loader.%s-launcher.%s.jar", gameVersion, loaderVersion, installerVersion)); - - if (!Files.exists(bundledJar)) { - return CompletableFuture.supplyAsync(() -> { - try { - if (!Files.exists(bundledJar)) { - Path installerJar = getInstallerJar(installerVersion); - writePropertiesToJar(installerJar, bundledJar, loaderVersion, gameVersion); - } - - return Files.newInputStream(bundledJar); - } catch (IOException e) { - e.printStackTrace(); - throw new InternalServerErrorResponse("Failed to generate bundled jar"); - } - }, WORKER_EXECUTOR); - } - - try { - return CompletableFuture.completedFuture(Files.newInputStream(bundledJar)); - } catch (IOException e) { - e.printStackTrace(); - throw new InternalServerErrorResponse("Failed to serve bundled jar"); - } - } - - private static Path getInstallerJar(String installerVersion) throws IOException { - Path installerJar = CACHE_DIR.resolve(String.format("fabric-installer-%s.jar", installerVersion)); - - if (Files.exists(installerJar)) { - return installerJar; - } - - return downloadInstallerJar(installerJar, installerVersion); - } - - private static Path downloadInstallerJar(Path jar, String installerVersion) throws IOException { - final String url = String.format("https://maven.fabricmc.net/net/fabricmc/fabric-installer/%1$s/fabric-installer-%1$s-server.jar", installerVersion); - - System.out.println("Downloading: " + url); - FileUtils.copyURLToFile(new URL(url), jar.toFile()); - return jar; - } - - private static void writePropertiesToJar(Path inputJar, Path outputJar, String loaderVersion, String gameVersion) throws IOException { - String data = String.format("fabric-loader-version=%s\ngame-version=%s", loaderVersion, gameVersion); - - Path workingFile = Paths.get(outputJar.toAbsolutePath() + ".tmp"); - Files.copy(inputJar, workingFile); - - Map env = new HashMap<>(); - env.put("create", "true"); - URI uri = URI.create("jar:" + workingFile.toUri()); - - try (FileSystem zipFs = FileSystems.newFileSystem(uri, env)) { - Files.write(zipFs.getPath("install.properties"), data.getBytes(StandardCharsets.UTF_8)); - } - - Files.copy(workingFile, outputJar); - Files.delete(workingFile); - } - - private static int getCacheDuration(Context ctx) { - if (ctx.pathParamMap().containsValue("stable")) { - return 120; - } - return 86400; - } + private static final Path CACHE_DIR = Paths.get("metadata", "installer"); + private static final Executor WORKER_EXECUTOR = Executors.newSingleThreadExecutor(); + + public static void setup() { + // http://localhost:5555/v2/versions/loader/1.17.1/0.12.0/0.8.0/server/jar + WebServer.javalin.get("/v2/versions/loader/:game_version/:loader_version/:installer_version/server/jar", boostrapHandler()); + } + + private static Handler boostrapHandler() { + return ctx -> { + if (!ctx.queryParamMap().isEmpty()) { + // Cannot really afford people to cache bust this. + throw new BadRequestResponse("Query params not allowed on this endpoint."); + } + + final String installerVersion = getAndValidateVersion(ctx, FabricMeta.database.installer, "installer_version"); + final String gameVersion = getAndValidateVersion(ctx, FabricMeta.database.game, "game_version"); + final String loaderVersion = getAndValidateVersion(ctx, FabricMeta.database.getAllLoader(), "loader_version"); + + validateLoaderVersion(loaderVersion); + validateInstallerVersion(installerVersion); + + // Set the filename and cache headers + final String filename = String.format("fabric-server-mc.%s-loader.%s-launcher.%s.jar", gameVersion, loaderVersion, installerVersion); + ctx.header(Header.CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", filename)); + final String cacheControl = String.format("public, max-age=%d", getCacheDuration(ctx)); + ctx.header(Header.CACHE_CONTROL, cacheControl); + ctx.contentType("application/java-archive"); + + ctx.result(getResultStream(installerVersion, gameVersion, loaderVersion)); + }; + } + + private static String getAndValidateVersion(Context ctx, List versions, String name) { + String version = ctx.pathParam(name); + + for (V v : versions) { + if (version.equals("stable") && v.isStable()) { + return v.getVersion(); + } + + if (v.getVersion().equals(version)) { + return version; + } + } + + throw new BadRequestResponse("Unable to find valid version for " + name); + } + + private static void validateLoaderVersion(String loaderVersion) { + String[] versionSplit = loaderVersion.split("\\."); + + // future 1.x versions + if (Integer.parseInt(versionSplit[0]) > 0) { + return; + } + + // 0.12.x or newer + if (Integer.parseInt(versionSplit[1]) >= 12) { + return; + } + + throw new BadRequestResponse("Fabric loader 0.12 or higher is required for unattended server installs. Please use a newer fabric loader version, or the full installer."); + } + + private static void validateInstallerVersion(String installerVersion) { + String[] versionSplit = installerVersion.split("\\."); + + // future 1.x versions + if (Integer.parseInt(versionSplit[0]) > 0) { + return; + } + + // 0.8.x or newer + if (Integer.parseInt(versionSplit[1]) >= 8) { + return; + } + + throw new BadRequestResponse("Fabric Installer 0.8 or higher is required for unattended server installs."); + } + + private static CompletableFuture getResultStream(String installerVersion, String gameVersion, String loaderVersion) { + Path bundledJar = CACHE_DIR.resolve(String.format("fabric-server-mc.%s-loader.%s-launcher.%s.jar", gameVersion, loaderVersion, installerVersion)); + + if (!Files.exists(bundledJar)) { + return CompletableFuture.supplyAsync(() -> { + try { + if (!Files.exists(bundledJar)) { + Path installerJar = getInstallerJar(installerVersion); + writePropertiesToJar(installerJar, bundledJar, loaderVersion, gameVersion); + } + + return Files.newInputStream(bundledJar); + } catch (IOException e) { + e.printStackTrace(); + throw new InternalServerErrorResponse("Failed to generate bundled jar"); + } + }, WORKER_EXECUTOR); + } + + try { + return CompletableFuture.completedFuture(Files.newInputStream(bundledJar)); + } catch (IOException e) { + e.printStackTrace(); + throw new InternalServerErrorResponse("Failed to serve bundled jar"); + } + } + + private static Path getInstallerJar(String installerVersion) throws IOException { + Path installerJar = CACHE_DIR.resolve(String.format("fabric-installer-%s.jar", installerVersion)); + + if (Files.exists(installerJar)) { + return installerJar; + } + + return downloadInstallerJar(installerJar, installerVersion); + } + + private static Path downloadInstallerJar(Path jar, String installerVersion) throws IOException { + final String url = String.format(Reference.LOCAL_FABRIC_MAVEN_URL+"net/fabricmc/fabric-installer/%1$s/fabric-installer-%1$s-server.jar", installerVersion); + + System.out.println("Downloading: " + url); + FileUtils.copyURLToFile(new URL(url), jar.toFile()); + return jar; + } + + private static void writePropertiesToJar(Path inputJar, Path outputJar, String loaderVersion, String gameVersion) throws IOException { + String data = String.format("fabric-loader-version=%s\ngame-version=%s", loaderVersion, gameVersion); + + Path workingFile = Paths.get(outputJar.toAbsolutePath() + ".tmp"); + Files.copy(inputJar, workingFile); + + Map env = new HashMap<>(); + env.put("create", "true"); + URI uri = URI.create("jar:" + workingFile.toUri()); + + try (FileSystem zipFs = FileSystems.newFileSystem(uri, env)) { + Files.write(zipFs.getPath("install.properties"), data.getBytes(StandardCharsets.UTF_8)); + } + + Files.copy(workingFile, outputJar); + Files.delete(workingFile); + } + + private static int getCacheDuration(Context ctx) { + if (ctx.pathParamMap().containsValue("stable")) { + return 120; + } + return 86400; + } } diff --git a/src/main/java/net/fabricmc/meta/web/models/MavenUrlVersion.java b/src/main/java/net/fabricmc/meta/web/models/MavenUrlVersion.java index 969eae5..010f322 100644 --- a/src/main/java/net/fabricmc/meta/web/models/MavenUrlVersion.java +++ b/src/main/java/net/fabricmc/meta/web/models/MavenUrlVersion.java @@ -16,21 +16,20 @@ package net.fabricmc.meta.web.models; -import net.fabricmc.meta.data.VersionDatabase; +import net.fabricmc.meta.utils.Reference; public class MavenUrlVersion extends MavenVersion { + public final String url; - public final String url; - - public MavenUrlVersion(String maven) { - super(maven); - String[] split = maven.split(":"); - this.url = String.format("%s%s/%s/%s/%s-%s.jar", VersionDatabase.MAVEN_URL, - split[0].replaceAll("\\.", "/"), - split[1], - split[2], - split[1], - split[2] - ); - } + public MavenUrlVersion(String maven) { + super(maven); + String[] split = maven.split(":"); + this.url = String.format("%s%s/%s/%s/%s-%s.jar", Reference.FABRIC_MAVEN_URL, + split[0].replace('.', '/'), + split[1], + split[2], + split[1], + split[2] + ); + } }