Skip to content

Commit

Permalink
New backend but things also work now yippee
Browse files Browse the repository at this point in the history
  • Loading branch information
Deftu committed Nov 3, 2024
1 parent f65a7e3 commit be663e2
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 58 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "org.polyfrost.oneconfig"
version = "1.1.0-alpha.32"
version = "1.1.0-alpha.33"

allprojects {
apply(plugin = "maven-publish")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,6 @@ private IOUtils() {
}
}

@SneakyThrows
public static int getJarVersion(URL jarFile) {
try (JarInputStream inputStream = new JarInputStream(jarFile.openStream(), false)) {
Manifest manifest = inputStream.getManifest();
if (manifest == null) {
return -1;
}

String version = manifest.getMainAttributes().getValue("Implementation-Version");
if (version == null) {
return -1;
}

return parseVersion(version);
}
}

public static byte[] readFully(InputStream inputStream) throws IOException {
byte[] buffer = new byte[8192];
int bytesRead;
Expand All @@ -97,16 +80,4 @@ public static byte[] readFully(InputStream inputStream) throws IOException {

return output.toByteArray();
}

private static int parseVersion(String version) {
Matcher matcher = VERSION_REGEX.matcher(version);
if (!matcher.matches()) {
return -1;
}

int major = Integer.parseInt(matcher.group("major"));
int minor = Integer.parseInt(matcher.group("minor"));
int patch = matcher.group("patch") == null ? 0 : Integer.parseInt(matcher.group("patch"));
return major * 10000 + minor * 100 + patch;
}
}
13 changes: 10 additions & 3 deletions stage0/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ sourceSets {

dependencies {
include(projects.common)

include("com.github.zafarkhaja:java-semver:0.10.2")
platforms.forEach { plat ->
plat.dependencies.forEach { "${plat.name}CompileOnly"(it) }
plat.j9Platform?.let { j9 ->
Expand Down Expand Up @@ -112,7 +112,7 @@ tasks {
manifest.attributes["Multi-Release"] = true
}
manifest.attributes += platform.extraAttributes
from(sourceSets.main.get().output)
from(jar)

manifest.inheritFrom(jar.get().manifest)
configurations = listOf(include)
Expand All @@ -121,7 +121,14 @@ tasks {

named<ShadowJar>("shadowJar") {
enabled = true
}
from(jar)
}

jar {
from(project(":stage1").tasks.named<Jar>("shadowJar").map { it.outputs.files }) {
rename { "oneconfig-loader/stage1.jar" }
}
}
}

configure<PublishingExtension> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class LaunchWrapperRuntimeAccess implements Capabilities.RuntimeAccess {

@Override
@SneakyThrows
public void appendToClassPath(boolean mod, @NotNull URL @NotNull ... urls) {
public void appendToClassPath(String id, boolean mod, @NotNull URL @NotNull ... urls) {
for (@NotNull URL url : urls) {
Launch.classLoader.addURL(url);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.regex.Matcher;

import com.github.zafarkhaja.semver.Version;
import lombok.SneakyThrows;

import org.polyfrost.oneconfig.loader.base.Capabilities;
Expand Down Expand Up @@ -53,6 +57,7 @@ public void load() {
// Lookup stage1
logger.info("Getting stage1 from cache");
Path stage1Jar = lookupStage1();
logger.info("Found stage1 at {}", stage1Jar);

// Load in classloader as a library
runtimeAccess.appendToClassPath("stage1", false, stage1Jar.toUri().toURL());
Expand Down Expand Up @@ -103,10 +108,10 @@ private Path lookupStage1() throws IOException {
Files.createDirectories(dataDir);

URL latestUrl = null;
int latestVersion = -1;
Version latestVersion = null;

if (Files.exists(stage1File)) {
latestVersion = IOUtils.getJarVersion(stage1File.toUri().toURL());
latestVersion = getJarVersion(stage1File.toUri().toURL());
}

Enumeration<URL> resources = Stage0Loader.class.getClassLoader().getResources(STAGE1_RESOURCE_PATH);
Expand All @@ -116,8 +121,14 @@ private Path lookupStage1() throws IOException {

while (resources.hasMoreElements()) {
URL url = resources.nextElement();
int version = IOUtils.getJarVersion(url);
if (version > latestVersion) {
Version version = getJarVersion(url);
logger.info("Found stage1 at {} with version {}", url, version);

if (version == null) {
continue;
}

if (latestVersion == null || version.isHigherThan(latestVersion)) {
latestUrl = url;
latestVersion = version;
}
Expand All @@ -133,4 +144,21 @@ private Path lookupStage1() throws IOException {
return stage1File;
}

@SneakyThrows
public static Version getJarVersion(URL jarFile) {
try (JarInputStream inputStream = new JarInputStream(jarFile.openStream(), false)) {
Manifest manifest = inputStream.getManifest();
if (manifest == null) {
return null;
}

String version = manifest.getMainAttributes().getValue("Implementation-Version");
if (version == null) {
return null;
}

return Version.parse(version);
}
}

}

This file was deleted.

8 changes: 8 additions & 0 deletions stage1/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ dependencies {

"legacyCompileOnly"("net.minecraft:launchwrapper:1.12")
}

tasks.jar {
manifest {
attributes(
"Implementation-Version" to version,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class LegacyCapabilities implements Capabilities {
public RuntimeAccess fetchRuntimeAccess() {
return new RuntimeAccess() {
@Override
public void appendToClassPath(boolean mod, @NotNull URL @NotNull ... urls) {
public void appendToClassPath(String id, boolean mod, @NotNull URL @NotNull ... urls) {
for (@NotNull URL url : urls) {
Launch.classLoader.addURL(url);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.polyfrost.oneconfig.loader.stage1;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -9,9 +10,14 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;

Expand Down Expand Up @@ -65,6 +71,9 @@ public void load() {
maybeDownloadRelaunch(loaderFrame);
downloadOneConfigArtifacts(loaderFrame);

// Close our updater window, we're done
loaderFrame.destroy();

String targetSpecifier = gameMetadata.getTargetSpecifier();
log.info("Target specifier: {}", targetSpecifier);

Expand Down Expand Up @@ -151,7 +160,7 @@ private void maybeDownloadRelaunch(LoaderFrame loaderFrame) {
.resolve("data");
Path relaunchFile = dataDir.resolve("relaunch.jar");

if (!relaunchArtifact.checksum.isMatching(relaunchFile)) {
if (!Files.exists(relaunchFile) || !relaunchArtifact.checksum.isMatching(relaunchFile)) {
loaderFrame.updateMessage("Downloading OneConfig Loader Relaunch...");
relaunchArtifact.downloadTo(getRequestHelper(), relaunchFile, loaderFrame::updateProgress);
}
Expand All @@ -176,6 +185,13 @@ private void downloadOneConfigArtifacts(LoaderFrame loaderFrame) {

boolean usingSnapshots = "true".equals(System.getProperty(ARTIFACT_SNAPSHOTS)) || "true".equals(System.getProperty(ONECONFIG_ARTIFACT_SNAPSHOTS));
List<BackendArtifact> artifacts = readArtifactsAt("https://api.polyfrost.org/v1/artifacts/oneconfig?version=" + gameVersion + "&loader=" + loaderName + "&snapshots=" + usingSnapshots);

String dummyArtifactsPath = System.getProperty("oneconfig.loader.stage1.dummyArtifacts");
if (dummyArtifactsPath != null) {
artifacts = readArtifactsFrom(Files.newInputStream(Paths.get(dummyArtifactsPath)));
System.out.println("Using dummy artifacts: " + dummyArtifactsPath);
}

if (artifacts == null) {
// Retry with the opposite snapshot setting
artifacts = readArtifactsAt("https://api.polyfrost.org/v1/artifacts/oneconfig?version=" + gameVersion + "&loader=" + loaderName + "&snapshots=" + !usingSnapshots);
Expand All @@ -187,19 +203,56 @@ private void downloadOneConfigArtifacts(LoaderFrame loaderFrame) {
}
}

System.out.println("Artifacts: " + artifacts);
if (artifacts.isEmpty()) {
throw new RuntimeException("No artifacts found for OneConfig");
}

// Compare all the hashes of any known artifacts and download any that are missing or have changed
for (BackendArtifact artifact : artifacts) {
Path artifactFile = dataDir.resolve(artifact.name);
if (!artifact.checksum.isMatching(artifactFile)) {
Path artifactFile = dataDir.resolve(artifact.name + ".jar");
if (!Files.exists(artifactFile) || !artifact.checksum.isMatching(artifactFile)) {
loaderFrame.updateMessage("Downloading OneConfig artifact: " + artifact.name);
artifact.downloadTo(getRequestHelper(), artifactFile, loaderFrame::updateProgress);
}

runtimeAccess.appendToClassPath(artifact.group + ":" + artifact.name, false, artifactFile.toUri().toURL());
if (artifact.jij) {
Path jijDir = dataDir
.resolve("jij")
.resolve(artifact.name);

// We need to extract all the JARs inside here and add them individually
try (JarInputStream jarInputStream = new JarInputStream(Files.newInputStream(artifactFile))) {
// All of our JARs are inside META-INF/jars
ZipEntry entry;
while ((entry = jarInputStream.getNextJarEntry()) != null) {
String name = entry.getName();
if (name.startsWith("META-INF/jars/") && name.endsWith(".jar")) {
// Now, we need to extract this JAR into it's own directory
Path jarFile = jijDir.resolve(name.substring("META-INF/jars/".length()));
if (!Files.exists(jarFile.getParent())) {
Files.createDirectories(jarFile.getParent());
}

try (FileOutputStream outputStream = new FileOutputStream(jarFile.toFile())) {
byte[] buffer = new byte[4096];
int read;
while ((read = jarInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
}

// Once the JAR is extracted, add it to the classpath
String jarName = jarFile.getFileName().toString();
runtimeAccess.appendToClassPath(jarName, artifact.name.contains("dependencies"), jarFile.toUri().toURL());
}

jarInputStream.closeEntry();
}
}
} else {
runtimeAccess.appendToClassPath(artifact.group + ":" + artifact.name, artifact.name.contains("dependencies"), artifactFile.toUri().toURL());
}
}

// Write the new artifact cache
Expand All @@ -224,13 +277,14 @@ private BackendArtifact readArtifactAt(String url) {

try (InputStream inputStream = connection.getInputStream()) {
return new Gson().fromJson(new String(IOUtils.readFully(inputStream), StandardCharsets.UTF_8), BackendArtifact.class);
} catch (Exception e) {
return null;
}
}

@SneakyThrows
@SuppressWarnings("unchecked")
private List<BackendArtifact> readArtifactsFrom(InputStream inputStream) {
return new Gson().fromJson(new String(IOUtils.readFully(inputStream), StandardCharsets.UTF_8), List.class);
return new Gson().fromJson(new String(IOUtils.readFully(inputStream), StandardCharsets.UTF_8), new TypeToken<List<BackendArtifact>>(){}.getType());
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ public class BackendArtifact {

public final String group, name, url;
public final BackendChecksum checksum;
public final boolean jij;

public BackendArtifact(String group, String name, String url, BackendChecksum checksum) {
public BackendArtifact(String group, String name, String url, BackendChecksum checksum, boolean jij) {
this.group = group;
this.name = name;
this.url = url;
this.checksum = checksum;
this.jij = jij;
}

public BackendArtifact(String group, String name, String url, BackendChecksum checksum) {
this(group, name, url, checksum, false);
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@
public class BackendChecksum {

public final String type, hash;
private transient final MessageDigest digest;
private transient MessageDigest digest;

public BackendChecksum(String type, String hash) {
this.type = type;
this.hash = hash;
ensureDigest();
}

@SneakyThrows
public boolean isMatching(Path path) {
ensureDigest();

return hash.equals(PolyHashing.hash(path, digest));
}

private void ensureDigest() {
MessageDigest digest;

try {
Expand All @@ -34,9 +44,4 @@ public BackendChecksum(String type, String hash) {
this.digest = digest;
}

@SneakyThrows
public boolean isMatching(Path path) {
return hash.equals(PolyHashing.hash(path, digest));
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.polyfrost.oneconfig.loader.stage1.ui;

import org.polyfrost.oneconfig.loader.ui.Resources;

import javax.swing.*;

import java.awt.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.polyfrost.oneconfig.loader.stage1.ui;

import org.polyfrost.oneconfig.loader.ui.Palette;
import org.polyfrost.oneconfig.loader.ui.Resources;

import javax.swing.*;

Expand Down

0 comments on commit be663e2

Please sign in to comment.