Skip to content

Commit

Permalink
Make local maven url configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
sfPlayer1 committed Dec 15, 2023
1 parent af76fe7 commit 4d34129
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 210 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java'
id 'maven-publish'
id 'org.cadixdev.licenser' version '0.6.1'
id 'eclipse'
}

group 'net.fabricmc'
Expand Down
46 changes: 42 additions & 4 deletions src/main/java/net/fabricmc/meta/FabricMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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();

Expand All @@ -49,4 +82,9 @@ private static void update(){
}
}

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 @@ -16,28 +16,33 @@

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;
import java.util.Comparator;
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<String> incorrectVersions = new ArrayList<>();
private static final Logger LOGGER = LoggerFactory.getLogger(VersionDatabase.class);
Expand Down Expand Up @@ -107,7 +112,7 @@ private void loadMcData() throws IOException {
public List<MavenBuildVersion> getLoader() {
return loader.stream().filter(VersionDatabase::isPublicLoaderVersion).collect(Collectors.toList());
}

private static boolean isPublicLoaderVersion(BaseVersion version) {
return true;
}
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/net/fabricmc/meta/utils/LoaderMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
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 @@ -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<Version> versions;
Expand All @@ -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);
}
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 @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 4d34129

Please sign in to comment.