Skip to content

Commit

Permalink
Release 1.2.0 with new git-based download system
Browse files Browse the repository at this point in the history
  • Loading branch information
Justsnoopy30 committed Oct 12, 2021
1 parent eab4fca commit d9a3463
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 81 deletions.
19 changes: 13 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ plugins {
id "de.undercouch.download" version "4.1.1"
}

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

archivesBaseName = project.archives_base_name
version = project.version
Expand All @@ -16,13 +16,23 @@ repositories {
url "https://maven.fabricmc.net"
}

flatDir {
dir "deps"
}

mavenCentral()
mavenLocal()
}

dependencies {
implementation "org.json:json:20210307"
implementation "com.formdev:flatlaf:1.6"
// Custom jgit with support for shallow cloning by me
implementation name: "org.eclipse.jgit-6.0.0-SNAPSHOT"
// Manual dependencies for custom built jgit
implementation 'org.slf4j:slf4j-api:1.7.32'
implementation 'org.slf4j:slf4j-log4j12:1.7.32'
implementation "com.jcraft:jsch:0.1.55"

implementation "net.fabricmc:fabric-installer:0.7.4"
}
Expand Down Expand Up @@ -62,17 +72,14 @@ task nativeExe(dependsOn: [downloadBootstrap], type: FileOutput) {
build.dependsOn nativeExe

jar {
// into 'mods', {
// from 'mods'
// }

manifest {
attributes("Main-Class": application.mainClass)
}

from "LICENSE"

from (configurations.compileClasspath.filter{ it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude 'META-INF/LICENSE'
exclude 'META-INF/*.SF'
exclude 'META-INF/*.DSA'
exclude 'META-INF/*.RSA'
Expand Down
Binary file added deps/org.eclipse.jgit-6.0.0-SNAPSHOT-sources.jar
Binary file not shown.
Binary file added deps/org.eclipse.jgit-6.0.0-SNAPSHOT.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
org.gradle.jvmargs=-Xmx1G

# Version and packaging info
version=1.1.1
version=1.2.0
maven_group=net.hypercubemc
archives_base_name=Universe-Installer

Expand Down
209 changes: 136 additions & 73 deletions src/main/java/net/hypercubemc/universe_installer/Installer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,35 @@
import net.fabricmc.installer.util.Reference;
import net.fabricmc.installer.util.Utils;
import net.hypercubemc.universe_installer.layouts.VerticalLayout;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.Depth;
import org.json.JSONException;

import javax.net.ssl.HttpsURLConnection;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Installer {
InstallerMeta INSTALLER_META;
List<InstallerMeta.Edition> EDITIONS;
List<String> GAME_VERSIONS;
String META_BASE_URL = "https://raw.githubusercontent.com/HyperCubeMC/Universe-Installer-Files/master/";
String ZIP_BASE_URL = "https://media.githubusercontent.com/media/HyperCubeMC/Universe-Installer-Files/master/";
String META_URL = "https://raw.githubusercontent.com/HyperCubeMC/Universe-Installer-Files/master/meta.json";
String REPO_URL = "https://github.com/HyperCubeMC/Universe-Installer-Files.git";

String selectedEditionName;
String selectedEditionDisplayName;
Expand Down Expand Up @@ -65,6 +73,31 @@ public void start() {
e.printStackTrace();
}

// JGit now depends on Java 11+
if (Float.parseFloat(System.getProperty("java.specification.version")) < 11) {
JLabel label = new JLabel();
Font font = label.getFont();

String style = "font-family:" + font.getFamily() + ";" + "font-weight:" + (font.isBold() ? "bold" : "normal") + ";" +
"font-size:" + font.getSize() + "pt;";

JEditorPane message = new JEditorPane("text/html", "<html><body style=\"" + style + "\">"
+ "This program requires Java 11 or above to run. Please install an updated Java release from <a href=\"https://adoptium.net/\">https://adoptium.net</a>"
+ "</body></html>");

message.addHyperlinkListener(e -> {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (IOException | URISyntaxException ignored) {}
}
});
message.setEditable(false);
message.setBackground(label.getBackground());
JOptionPane.showMessageDialog(null, message, "Outdated Java", JOptionPane.ERROR_MESSAGE);
return;
}

config.load();

Main.LOADER_META = new MetaHandler(Reference.getMetaServerEndpoint("v2/versions/loader"));
Expand All @@ -77,7 +110,7 @@ public void start() {
return;
}

INSTALLER_META = new InstallerMeta(META_BASE_URL + "meta.json");
INSTALLER_META = new InstallerMeta(META_URL);
try {
INSTALLER_META.load();
} catch (IOException e) {
Expand Down Expand Up @@ -239,13 +272,7 @@ public void start() {
progressBar.setValue(0);
setInteractionEnabled(false);

String zipName = selectedEditionName + ".zip";

String downloadURL = ZIP_BASE_URL + selectedVersion + "/" + zipName;

File saveLocation = getStorageDirectory().resolve(zipName).toFile();

final Downloader downloader = new Downloader(downloadURL, saveLocation);
Downloader downloader = new Downloader(REPO_URL, getStorageDirectory().resolve("repo"));
downloader.addPropertyChangeListener(event -> {
if ("progress".equals(event.getPropertyName())) {
progressBar.setValue((Integer) event.getNewValue());
Expand Down Expand Up @@ -288,7 +315,7 @@ public void start() {
if (useCustomLoader) deleteDirectory(modsFolder);
if (!modsFolder.exists() || !modsFolder.isDirectory()) modsFolder.mkdir();

boolean installSuccess = installFromZip(saveLocation);
boolean installSuccess = installFromPack(getStorageDirectory().resolve("repo").resolve(selectedVersion).resolve(selectedEditionName).toFile());
if (installSuccess) {
button.setText("Installation succeeded!");
finishedSuccessfulInstall = true;
Expand Down Expand Up @@ -320,83 +347,119 @@ public void start() {

// Works up to 2GB because of long limitation
class Downloader extends SwingWorker<Void, Void> {
private final String url;
private final File file;
private final String repo;
private final Path path;

public Downloader(String url, File file) {
this.url = url;
this.file = file;
public Downloader(String repo, Path path) {
this.repo = repo;
this.path = path;
}

@Override
protected Void doInBackground() throws Exception {
URL url = new URL(this.url);
HttpsURLConnection connection = (HttpsURLConnection) url
.openConnection();
long filesize = connection.getContentLengthLong();
if (filesize == -1) {
throw new Exception("Content length must not be -1 (unknown)!");
}
long totalDataRead = 0;
try (java.io.BufferedInputStream in = new java.io.BufferedInputStream(
connection.getInputStream())) {
java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
try (java.io.BufferedOutputStream bout = new BufferedOutputStream(
fos, 1024)) {
byte[] data = new byte[1024];
int i;
while ((i = in.read(data, 0, 1024)) >= 0) {
totalDataRead = totalDataRead + i;
bout.write(data, 0, i);
int percent = (int) ((totalDataRead * 100) / filesize);
setProgress(percent);
}
ProgressMonitor progressMonitor = new ProgressMonitor() {
int totalWork = 0;
int workDone = 0;

boolean receivingCompleted = false;

@Override
public void start(int totalTasks) {
System.out.println("Starting work on tasks");
}
}
return null;
}
}

public boolean installFromZip(File zip) {
try {
int BUFFER_SIZE = 2048; // Buffer Size
@Override
public void beginTask(String title, int totalWork) {
System.out.println("Start " + title + ": " + totalWork);
// This is the first task endTask is called for, and the only one needed for download progress
if (title.startsWith("Receiving")) this.totalWork = totalWork;
workDone = 0;
}

ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zip));
@Override
public void update(int completed) {
if (receivingCompleted) return;

ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
String entryName = entry.getName();
workDone += completed;
if (workDone < totalWork) setProgress((int) ((double) workDone * 100 / totalWork));
}

if (config.shouldUseCustomLoader() && entryName.startsWith("mods/")) {
entryName = entryName.replace("mods/", "universe-reserved/");
@Override
public void endTask() {
workDone = 0;
if (!receivingCompleted) receivingCompleted = true;
System.out.println("Done");
}

File filePath = getInstallDir().resolve(entryName).toFile();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
} else {
// if the entry is a directory, make the directory
filePath.mkdir();
@Override
public boolean isCancelled() {
return false;
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
};

if (repositoryExists(path.toFile())) {
Git.open(path.toFile()).pull()
.setProgressMonitor(progressMonitor)
.call();
} else {
if (path.toFile().exists()) deleteDirectory(path.toFile());

Git.cloneRepository()
.setURI(repo)
.setDirectory(path.toFile())
.setProgressMonitor(progressMonitor)
.setDepth(new Depth(1))
.call();
}
setProgress(100);

return null;
}
}

public boolean installFromPack(File pack) {
try {
File[] files = pack.listFiles();

if (files == null) {
return false;
}
zipIn.close();

installFiles(files);
return true;
} catch (IOException e) {
e.getCause().printStackTrace();
e.printStackTrace();
return false;
}
}

private void installFiles(File[] files) throws IOException {
for (File entry : files) {
String entryPath = getStorageDirectory().resolve("repo").resolve(selectedVersion).resolve(selectedEditionName).relativize(entry.toPath()).toString();

if (config.shouldUseCustomLoader() && entryPath.startsWith("mods" + File.separator)) {
entryPath = entryPath.replace("mods" + File.separator, "universe-reserved" + File.separator);
}

File filePath = getInstallDir().resolve(entryPath).toFile();
if (!entry.isDirectory()) {
Files.copy(entry.toPath(), filePath.toPath(), StandardCopyOption.REPLACE_EXISTING);
} else {
// if the entry is a directory, make the directory
filePath.mkdir();
File[] subFiles = entry.listFiles();
if (subFiles != null) installFiles(subFiles);
}
}
}

public boolean repositoryExists(File directory) {
FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
repositoryBuilder.findGitDir(directory);

return repositoryBuilder.getGitDir() != null;
}

public Path getStorageDirectory() {
return this.getAppDataDirectory().resolve(getStorageDirectoryName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class UniverseConfig {
public UniverseConfig() {
customInstallDir = null;
useCustomLoader = true;
propertiesPath = Installer.INSTANCE.getStorageDirectory().resolve("universe.properties");
propertiesPath = Installer.INSTANCE.getStorageDirectory().resolve("config").resolve("universe.properties");
properties = new Properties();
}

Expand All @@ -40,6 +40,9 @@ public void setCustomInstallDir(Path customInstallDir) {
}

public void save() {
File configDir = propertiesPath.getParent().toFile();
if (!configDir.exists() || !configDir.isDirectory()) configDir.mkdir();

if (customInstallDir != null) properties.setProperty("custom-install-dir", customInstallDir.toString());
else properties.remove("custom-install-dir");
if (!useCustomLoader) properties.setProperty("use-custom-loader", String.valueOf(false));
Expand Down

0 comments on commit d9a3463

Please sign in to comment.