Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #5 and add local mode for testing #21

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
12 changes: 9 additions & 3 deletions src/main/java/org/quiltmc/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public final class Constants {
static final String USER_AGENT = "update-quilt-meta/" + TOOL_VERSION;

static final String GROUP = "org.quiltmc";
static final boolean TESTING = true;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably wanted to set this to false, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes!


// Maven
static final String BASE_MAVEN_URL = "https://maven.quiltmc.org/repository/release/";
Expand All @@ -21,12 +22,12 @@ public final class Constants {

// Backblaze
static final String B2_BUCKET = "meta-quiltmc-org";
static final String B2_APP_KEY_ID = System.getenv("B2_APP_KEY_ID");
static final String B2_APP_KEY = System.getenv("B2_APP_KEY");
static final String B2_APP_KEY_ID = getEnv("B2_APP_KEY_ID");
static final String B2_APP_KEY = getEnv("B2_APP_KEY");

// Cloudflare
static final String CF_ZONE_ID = "73c99d057aa12563eb4cad4ef14f0796";
static final String CF_KEY = System.getenv("CF_KEY");
static final String CF_KEY = getEnv("CF_KEY");
static final URL CF_PURGE_FILES_ENDPOINT;

static {
Expand All @@ -43,5 +44,10 @@ public final class Constants {
// Internal
static final String MANIFEST_FILE = "_manifest_01.gz";

private static String getEnv(String key) {
var env = System.getenv(key);
return env == null ? "" : env;
}

private Constants() {}
}
118 changes: 118 additions & 0 deletions src/main/java/org/quiltmc/LoaderJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.quiltmc;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

@SuppressWarnings("unused")
public class LoaderJson {
public int version;
@SerializedName("min_java_version")
public int minJavaVersion;
public Libraries libraries;
public MainClass mainClass;

public static void addSizeAndSignatures(JsonObject json, Gson gson, String mavenId, String url, String expectedExtension, String version) {
Library library = createFullyPopulatedLibrary(gson, mavenId, url, expectedExtension, version);

json.addProperty("md5", library.md5);
json.addProperty("sha1", library.sha1);
json.addProperty("sha256", library.sha256);
json.addProperty("sha512", library.sha512);
json.addProperty("size", library.size);
}

public static Library createFullyPopulatedLibrary(Gson gson, String mavenId, String url, String expectedExtension, String version) {
LoaderJson.Library asLibrary = new LoaderJson.Library(mavenId, url);
LoaderJson.addSizeAndSignatures(gson, asLibrary, expectedExtension, version);

return asLibrary;
}

public static void addSizeAndSignatures(Gson gson, Library library, String expectedExtension, String loaderVersion) {
AtomicBoolean fetchedModuleData = new AtomicBoolean(false);
String[] mavenLocation = library.name.split(":");
String mavenFolderUrl = library.url + mavenLocation[0].replace('.', '/') + '/' + mavenLocation[1] + '/' + mavenLocation[2] + '/';

try {
// fetch from gradle .module file on maven so we have to do less work
URL moduleUrl = new URL(mavenFolderUrl + mavenLocation[1] + '-' + mavenLocation[2] + ".module");

ModuleData moduleData = ModuleData.readModuleData(gson, moduleUrl);
ModuleData.findRuntimeFile(moduleData).ifPresent((file) -> {
library.md5 = file.md5;
library.sha1 = file.sha1;
library.sha256 = file.sha256;
library.sha512 = file.sha512;
library.size = file.size;

fetchedModuleData.set(true);
});
} catch (FileNotFoundException ignored) {
} catch (IOException e) {
throw new RuntimeException(e);
}

if (!fetchedModuleData.get()) {
String jarFileUrl = mavenFolderUrl + mavenLocation[1] + '-' + mavenLocation[2] + "." + expectedExtension;

try {
BufferedReader md5Reader = new BufferedReader(new InputStreamReader(new URL(jarFileUrl + ".md5").openStream()));
BufferedReader sha1 = new BufferedReader(new InputStreamReader(new URL(jarFileUrl + ".sha1").openStream()));
BufferedReader sha256 = new BufferedReader(new InputStreamReader(new URL(jarFileUrl + ".sha256").openStream()));
BufferedReader sha512 = new BufferedReader(new InputStreamReader(new URL(jarFileUrl + ".sha512").openStream()));

library.md5 = md5Reader.readLine();
library.sha1 = sha1.readLine();
library.sha256 = sha256.readLine();
library.sha512 = sha512.readLine();

URL url = new URL(jarFileUrl);
URLConnection conn = url.openConnection();
conn.connect();
library.size = conn.getContentLength();
} catch (IOException e) {
// this cannot be a hard crash because of a funny quirk: the json on quilt loader 0.17.5-beta.4 is completely broken and has ${version} instead of the real versions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be better to just go and finally drop the thing entirely - at least from meta if not from the Maven.
But doing that this way (by catching the error afterwards) also makes the generated files somewhat nondeterministic if intermittent download failures appear, which aren't too uncommon. For the same reason, you probably want to retry downloads once or twice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i believe pyro said they'd go in and drop it after i asked -- i haven't checked if it's actually been removed
[20 seconds later rai talking now say hi] it hasn't been removed yet. i'll make sure it happens so this code can be removed

System.out.println("[WARN] Could not append additional data for loader version: " + loaderVersion + " (artifact: " + mavenLocation[1] + ")");
e.printStackTrace();
}
}
}

public static class Libraries {
public List<Library> client;
public List<Library> server;
public List<Library> common;
public List<Library> development;
}

public static class Library {
public String name;
public String url;
public String md5;
public String sha1;
public String sha256;
public String sha512;
public int size;

public Library(String name, String url) {
this.name = name;
this.url = url;
}
}

public static class MainClass {
public String client;
public String server;
public String serverLauncher;
}
}
Loading