Skip to content

Commit

Permalink
Cleanup SV installer models
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed Nov 21, 2024
1 parent 7ebe3f7 commit c54c2b5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private Set<Library> scheduleDownloads(
Files.delete(depFile);
}

final URL requestUrl = URI.create(String.format(LibraryManager.SPONGE_NEXUS_DOWNLOAD_URL,
final URL requestUrl = new URI(String.format(LibraryManager.SPONGE_NEXUS_DOWNLOAD_URL,
dep.sha512(), dep.group(), dep.module(), dep.version())).toURL();
final SonatypeResponse response = this.getResponseFor(this.gson, requestUrl);
if (response.items().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,18 @@ private static void bootstrap(final Path[] bootLibs, final Path spongeBoot, fina
}
}

private Version downloadMinecraftManifest() throws IOException {
private Version downloadMinecraftManifest() throws Exception {
Logger.info("Downloading the Minecraft versions manifest...");

VersionManifest.Version foundVersionManifest = null;

final Gson gson = new Gson();
final URLConnection conn = new URL(Constants.Libraries.MINECRAFT_MANIFEST_URL).openConnection();
final URLConnection conn = new URI(Constants.Libraries.MINECRAFT_MANIFEST_URL).toURL().openConnection();
conn.setConnectTimeout(5 /* seconds */ * 1000);
try (final JsonReader reader = new JsonReader(new InputStreamReader(conn.getInputStream()))) {
final VersionManifest manifest = gson.fromJson(reader, VersionManifest.class);
for (final VersionManifest.Version version : manifest.versions) {
if (Constants.Libraries.MINECRAFT_VERSION_TARGET.equals(version.id)) {
for (final VersionManifest.Version version : manifest.versions()) {
if (Constants.Libraries.MINECRAFT_VERSION_TARGET.equals(version.id())) {
foundVersionManifest = version;
break;
}
Expand All @@ -268,14 +268,12 @@ private Version downloadMinecraftManifest() throws IOException {
}

final Version version;

try (final JsonReader reader = new JsonReader(new InputStreamReader(foundVersionManifest.url.openStream()))) {
try (final JsonReader reader = new JsonReader(new InputStreamReader(foundVersionManifest.url().openStream()))) {
version = gson.fromJson(reader, Version.class);
}

if (version == null) {
throw new IOException(String.format("Failed to download version information for '%s'!",
Constants.Libraries.MINECRAFT_VERSION_TARGET));
throw new IOException(String.format("Failed to download version information for '%s'!", Constants.Libraries.MINECRAFT_VERSION_TARGET));
}

return version;
Expand All @@ -297,24 +295,25 @@ private Path expectedBundleLocation(final Path originalLocation) {

private CompletableFuture<Path> downloadMinecraft(final Version version, final Path librariesDirectory) {
return LibraryUtils.asyncFailableFuture(() -> {
final Path downloadTarget = this.expectedBundleLocation(this.expectedMinecraftLocation(librariesDirectory, version.id));
final Path downloadTarget = this.expectedBundleLocation(this.expectedMinecraftLocation(librariesDirectory, version.id()));
final Version.Downloads.Download server = version.downloads().server();

if (Files.notExists(downloadTarget)) {
if (!this.installer.getLauncherConfig().autoDownloadLibraries) {
throw new IOException(String.format("The Minecraft jar is not located at '%s' and downloading it has been turned off.", downloadTarget));
}
LibraryUtils.downloadAndVerifyDigest(TinyLogger.INSTANCE, version.downloads.server.url, downloadTarget, "SHA-1", version.downloads.server.sha1);
LibraryUtils.downloadAndVerifyDigest(TinyLogger.INSTANCE, server.url(), downloadTarget, "SHA-1", server.sha1());
} else {
if (this.installer.getLauncherConfig().checkLibraryHashes) {
Logger.info("Detected existing Minecraft Server jar, verifying hashes...");

// Pipe the download stream into the file and compute the SHA-1
if (LibraryUtils.validateDigest("SHA-1", version.downloads.server.sha1, downloadTarget)) {
if (LibraryUtils.validateDigest("SHA-1", server.sha1(), downloadTarget)) {
Logger.info("Minecraft Server jar verified!");
} else {
Logger.error("Checksum verification failed: Expected {}. Deleting cached Minecraft Server jar...", version.downloads.server.sha1);
Logger.error("Checksum verification failed: Expected {}. Deleting cached Minecraft Server jar...", server.sha1());
Files.delete(downloadTarget);
LibraryUtils.downloadAndVerifyDigest(TinyLogger.INSTANCE, version.downloads.server.url, downloadTarget, "SHA-1", version.downloads.server.sha1);
LibraryUtils.downloadAndVerifyDigest(TinyLogger.INSTANCE, server.url(), downloadTarget, "SHA-1", server.sha1());
}
} else {
Logger.info("Detected existing Minecraft jar. Skipping hash check as that is turned off...");
Expand Down Expand Up @@ -388,7 +387,7 @@ private CompletableFuture<Path> downloadMappings(final Version version, final Pa
.resolve(Constants.Libraries.MINECRAFT_VERSION_TARGET)
.resolve(Constants.Libraries.MINECRAFT_MAPPINGS_NAME);

final Version.Downloads.Download mappings = version.downloads.server_mappings;
final Version.Downloads.Download mappings = version.downloads().server_mappings();
if (mappings == null) {
throw new IOException(String.format("Mappings were not included in version manifest for %s", Constants.Libraries.MINECRAFT_VERSION_TARGET));
}
Expand All @@ -397,12 +396,11 @@ private CompletableFuture<Path> downloadMappings(final Version version, final Pa
if (Files.exists(downloadTarget)) {
if (checkHashes) {
Logger.info("Detected existing mappings, verifying hashes...");
if (LibraryUtils.validateDigest("SHA-1", mappings.sha1, downloadTarget)) {
if (LibraryUtils.validateDigest("SHA-1", mappings.sha1(), downloadTarget)) {
Logger.info("Mappings verified!");
return downloadTarget;
} else {
Logger.error("Checksum verification failed: Expected {}. Deleting cached server mappings file...",
version.downloads.server.sha1);
Logger.error("Checksum verification failed: Expected {}. Deleting cached server mappings file...", mappings.sha1());
Files.delete(downloadTarget);
}
} else {
Expand All @@ -412,9 +410,9 @@ private CompletableFuture<Path> downloadMappings(final Version version, final Pa

if (this.installer.getLauncherConfig().autoDownloadLibraries) {
if (checkHashes) {
LibraryUtils.downloadAndVerifyDigest(TinyLogger.INSTANCE, mappings.url, downloadTarget, "SHA-1", mappings.sha1);
LibraryUtils.downloadAndVerifyDigest(TinyLogger.INSTANCE, mappings.url(), downloadTarget, "SHA-1", mappings.sha1());
} else {
LibraryUtils.download(TinyLogger.INSTANCE, mappings.url, downloadTarget, false);
LibraryUtils.download(TinyLogger.INSTANCE, mappings.url(), downloadTarget, false);
}
} else {
throw new IOException(String.format("Mappings were not located at '%s' and downloading them has been turned off.", downloadTarget));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,64 +25,22 @@
package org.spongepowered.vanilla.installer.model.mojang;

import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;

public final class Version {
public record Version(AssetIndex assetIndex, String assets, Downloads downloads, String id,
List<Library> libraries, String mainClass, VersionType type, String releaseTime, String time) {

public AssetIndex assetIndex;
public String assets;
public Downloads downloads;
public String id;
public List<Library> libraries;
public String mainClass;
@SuppressWarnings("UseOfObsoleteDateTimeApi")
public Date releaseTime;
@SuppressWarnings("UseOfObsoleteDateTimeApi")
public Date time;
public VersionManifest.Version.Type type;
public record AssetIndex(String id, String sha1, int size, int totalSize, URL url) {}

public static class AssetIndex {

public String id;
public String sha1;
public int size;
public int totalSize;
public URL url;
}

public static class Downloads {

public Download client;
public Download client_mappings;
public Download server;
public Download server_mappings;

public static class Download {

public String sha1;
public int size;
public URL url;
public record Downloads(Download client, Download client_mappings, Download server, Download server_mappings) {
public record Download(String sha1, int size, URL url) {
}
}

public static class Library {

public org.spongepowered.vanilla.installer.model.mojang.Version.Library.Downloads downloads;
public String name;

public static class Downloads {

public Artifact artifact;
public Map<String, Artifact> classifiers;

public static class Artifact {

public String path;
public String sha1;
public int size;
public URL url;
public record Library(Library.Downloads downloads, String name) {
public record Downloads(Artifact artifact, Map<String, Artifact> classifiers) {
public record Artifact(String path, String sha1, int size, URL url) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,65 +24,10 @@
*/
package org.spongepowered.vanilla.installer.model.mojang;

import com.google.gson.annotations.SerializedName;

import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public final class VersionManifest {

public Latest latest;
public List<Version> versions;

public static class Latest {

public String snapshot;
public String release;
}

public static class Version {

public String id;
public Type type;
public URL url;
@SuppressWarnings("UseOfObsoleteDateTimeApi")
public Date time;
@SuppressWarnings("UseOfObsoleteDateTimeApi")
public Date releaseTime;

@Override
public boolean equals(final Object other) {
if (this == other) {
return true;
}
if (other == null || this.getClass() != other.getClass()) {
return false;
}
final Version that = (Version) other;
return Objects.equals(this.id, that.id) && Objects.equals(this.type, that.type);
}

@Override
public int hashCode() {
return Objects.hash(this.id, this.type);
}

@Override
public String toString() {
return this.type + ": " + this.id;
}

public enum Type {
@SerializedName("old_alpha")
OLD_ALPHA,
@SerializedName("old_beta")
OLD_BETA,
@SerializedName("release")
RELEASE,
@SerializedName("snapshot")
SNAPSHOT
}
}
public record VersionManifest(Latest latest, List<Version> versions) {
public record Latest(String snapshot, String release) {}
public record Version(String id, VersionType type, URL url, String time, String releaseTime) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.vanilla.installer.model.mojang;

import com.google.gson.annotations.SerializedName;

public enum VersionType {
@SerializedName("old_alpha")
OLD_ALPHA,
@SerializedName("old_beta")
OLD_BETA,
@SerializedName("release")
RELEASE,
@SerializedName("snapshot")
SNAPSHOT
}

0 comments on commit c54c2b5

Please sign in to comment.