Inceptum/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/install/steps/DownloadLibrariesStep.java

68 lines
3.2 KiB
Java
Raw Normal View History

2022-09-04 21:21:24 +02:00
package io.gitlab.jfronny.inceptum.launcher.system.install.steps;
2021-10-27 22:00:08 +02:00
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.commons.io.JFiles;
import io.gitlab.jfronny.inceptum.common.*;
import io.gitlab.jfronny.inceptum.common.api.GitlabApi;
import io.gitlab.jfronny.inceptum.common.api.MavenApi;
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.ArtifactInfo;
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.InstanceMeta;
import io.gitlab.jfronny.inceptum.launcher.model.mojang.VersionInfo;
import io.gitlab.jfronny.inceptum.launcher.system.install.Step;
import io.gitlab.jfronny.inceptum.launcher.util.ProcessState;
import io.gitlab.jfronny.inceptum.launcher.util.VersionInfoLibraryResolver;
import io.gitlab.jfronny.inceptum.launcher.system.install.SetupStepInfo;
2021-10-27 22:00:08 +02:00
import java.io.IOException;
import java.net.URISyntaxException;
2022-09-04 21:21:24 +02:00
import java.nio.file.*;
import java.util.concurrent.atomic.AtomicBoolean;
2021-10-27 22:00:08 +02:00
public class DownloadLibrariesStep implements Step {
@Override
public void execute(SetupStepInfo info, AtomicBoolean stopThread) throws IOException {
2021-10-30 19:26:59 +02:00
execute(info.version(), stopThread, info.currentState());
}
public static void execute(VersionInfo version, AtomicBoolean stopThread, ProcessState currentState) throws IOException {
2021-10-30 19:26:59 +02:00
for (ArtifactInfo artifact : VersionInfoLibraryResolver.getRelevant(version)) {
if (stopThread.get()) return;
Path path = MetaHolder.LIBRARIES_DIR.resolve(artifact.path);
2021-10-30 16:12:39 +02:00
if (!Files.exists(path)) {
currentState.updateStep("Downloading library: " + artifact.path);
2021-10-30 16:12:39 +02:00
if (!Files.exists(path.getParent())) Files.createDirectories(path.getParent());
if (!artifact.url.endsWith(".jar")) {
Utils.LOGGER.info("Not a valid URL for a jar: " + artifact.url);
2021-10-30 16:12:39 +02:00
continue;
}
try {
2022-09-04 21:21:24 +02:00
Net.downloadFile(artifact.url, artifact.sha1, path);
} catch (URISyntaxException e) {
throw new IOException("Could not download library", e);
}
2021-10-30 16:12:39 +02:00
}
if (artifact.isNative) {
currentState.updateStep("Extracting natives");
2021-11-02 16:44:26 +01:00
try (FileSystem libFs = Utils.openZipFile(path, false)) {
2022-09-18 15:15:30 +02:00
JFiles.copyRecursive(libFs.getPath("."), MetaHolder.NATIVES_DIR.resolve(InstanceMeta.getMinecraftVersion(version.id)));
2022-09-04 21:21:24 +02:00
} catch (Throwable t) {
2021-10-30 16:12:39 +02:00
Files.delete(path);
throw new IOException("Could not extract native", t);
}
}
2021-10-27 22:00:08 +02:00
}
String artifact = getLaunchWrapperArtifact();
if (!Files.exists(MetaHolder.LIBRARIES_DIR.resolve(MavenApi.mavenNotationToJarPath(artifact)))) {
try {
MavenApi.downloadLibrary(GitlabApi.PROJECT_MAVEN, artifact);
} catch (URISyntaxException e) {
throw new IOException("Could not download launchwrapper", e);
}
}
}
public static String getLaunchWrapperArtifact() {
return "io.gitlab.jfronny.inceptum:launchwrapper:" + (BuildMetadata.IS_PUBLIC ? BuildMetadata.VERSION : Updater.getUpdate().version);
2021-10-27 22:00:08 +02:00
}
}