Skip to content

Commit

Permalink
Installer work
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Dec 11, 2018
1 parent e2de979 commit 97892de
Show file tree
Hide file tree
Showing 19 changed files with 368 additions and 1,121 deletions.
15 changes: 15 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
node {
stage 'Checkout'

checkout scm

stage 'Build'

sh "rm -rf build/libs/"
sh "chmod +x gradlew"
sh "./gradlew build uploadArchives --refresh-dependencies --stacktrace"

stage "Archive artifacts"

archive 'build/libs/*'
}
21 changes: 4 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ buildscript {
}

sourceCompatibility = 1.8
version = '0.0.2'
version = '0.1.0'
archivesBaseName = "fabric-installer"

// Fetch build number from Jenkins
def ENV = System.getenv()
Expand All @@ -27,29 +28,14 @@ repositories {
mavenCentral()
maven {
name = 'fabric'
url = 'http://maven.fabricmc.net/'
}
maven {
name = 'cuchaz'
url = 'http://maven.cuchazinteractive.com/'
}
maven {
name "RX14 Repository"
url 'http://mvn.rx14.co.uk/local/'
url = 'https://maven.fabricmc.net/'
}
}

dependencies {
compile 'com.google.code.gson:gson:2.8.5'
compile 'com.google.guava:guava:27.0-jre'
compile 'commons-io:commons-io:2.6'
compile 'org.apache.maven:maven-artifact:3.5.4'
compile 'org.apache.maven:maven-repository-metadata:3.5.4'
compile 'org.zeroturnaround:zt-zip:1.13'
// compile 'org.slf4j:slf4j-api:1.7.25'
// compile 'org.slf4j:slf4j-simple:1.7.25'
compile 'com.intellij:forms_rt:7.0.3'
// compile 'org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-depchain:3.1.3'
}

task copyAndReplace(type: Copy) {
Expand All @@ -76,3 +62,4 @@ jar {
}

apply from: 'https://github.com/FabricMC/fabric-docs/raw/master/gradle/license.gradle'
apply from: 'https://github.com/FabricMC/fabric-docs/raw/master/gradle/maven.gradle'
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
group = net.fabricmc
72 changes: 72 additions & 0 deletions src/main/java/net/fabricmc/installer/ClientInstaller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2016, 2017, 2018 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.installer;

import com.google.gson.JsonObject;
import net.fabricmc.installer.util.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class ClientInstaller {

public static void install(File mcDir, String mappingsVersion, String loaderVersion, IInstallerProgress progress) throws IOException {
System.out.println("Installing " + mappingsVersion + " with fabric " + loaderVersion);
String[] split = mappingsVersion.split("\\.");

String profileName = String.format("%s-%s-%s", Reference.LOADER_NAME, loaderVersion, mappingsVersion);

String url = String.format("%s/%s/%s/%s/%3$s-%4$s.json", Reference.MAVEN_SERVER_URL, Reference.PACKAGE, Reference.LOADER_NAME, loaderVersion);
String fabricInstallMeta = IOUtils.toString(new URL(url), "UTF-8");
JsonObject installMeta = Utils.GSON.fromJson(fabricInstallMeta, JsonObject.class);

MinecraftLaunchJson launchJson = new MinecraftLaunchJson(installMeta);
launchJson.id = profileName;
launchJson.inheritsFrom = split[0];

//Adds loader and the mappings
launchJson.libraries.add(new MinecraftLaunchJson.Library(Reference.PACKAGE.replaceAll("/", ".") + ":" + Reference.MAPPINGS_NAME + ":" + mappingsVersion, Reference.MAVEN_SERVER_URL));
launchJson.libraries.add(new MinecraftLaunchJson.Library(Reference.PACKAGE.replaceAll("/", ".") + ":" + Reference.LOADER_NAME + ":" + loaderVersion, Reference.MAVEN_SERVER_URL));

File versionsDir = new File(mcDir, "versions");
File profileDir = new File(versionsDir, profileName);
File profileJson = new File(profileDir, profileName + ".json");

if (!profileDir.exists()) {
profileDir.mkdirs();
}

/*
This is a fun meme
The vanilla launcher assumes the profile name is the same name as a maven artifact, how ever our profile name is a combination of 2
(mappings and loader). The launcher will also accept any jar with the same name as the profile, it doesnt care if its empty
*/
File dummyJar = new File(profileDir, profileName + ".jar");
FileUtils.touch(dummyJar);

FileUtils.writeStringToFile(profileJson, Utils.GSON.toJson(launchJson), StandardCharsets.UTF_8);

progress.updateProgress(Translator.getString("install.success"), 100);
}
}
166 changes: 166 additions & 0 deletions src/main/java/net/fabricmc/installer/InstallerGui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright (c) 2016, 2017, 2018 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.installer;

import net.fabricmc.installer.util.*;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.function.Consumer;

public class InstallerGui extends JFrame implements IInstallerProgress {
private JPanel contentPane;
private JButton buttonInstall;
private JComboBox<String> mappingVersionComboBox;
private JComboBox<String> loaderVersionComboBox;
private JTextField installLocation;
private JButton selectFolderButton;
private JProgressBar progressBar;
private JLabel statusLabel;

public InstallerGui() throws XmlPullParserException, IOException {
initComponents();
setContentPane(contentPane);
setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
setIconImage(Toolkit.getDefaultToolkit().getImage(classLoader.getResource("icon.png")));

MavenHandler loaderMaven = new MavenHandler();
loaderMaven.load(Reference.MAVEN_SERVER_URL, Reference.PACKAGE, Reference.LOADER_NAME);
for (String str : loaderMaven.versions) {
loaderVersionComboBox.addItem(str);
}
loaderVersionComboBox.setSelectedIndex(0);

MavenHandler mappingMaven = new MavenHandler();
mappingMaven.load(Reference.MAVEN_SERVER_URL, Reference.PACKAGE, Reference.MAPPINGS_NAME);
for (String str : mappingMaven.versions) {
mappingVersionComboBox.addItem(str);
}
mappingVersionComboBox.setSelectedIndex(0);
}

public void install() {
String mappingsVersion = (String) mappingVersionComboBox.getSelectedItem();
String loaderVersion = (String) loaderVersionComboBox.getSelectedItem();
System.out.println(Translator.getString("gui.installing"));
new Thread(() -> {
try {
updateProgress(Translator.getString("gui.installing") + ": " + loaderVersion, 0);
File mcPath = new File(installLocation.getText());
if (!mcPath.exists()) {
throw new RuntimeException(Translator.getString("install.client.error.noLauncher"));
}
ClientInstaller.install(mcPath, mappingsVersion, loaderVersion, this);
} catch (Exception e) {
e.printStackTrace();
error(e.getLocalizedMessage());
}
}).start();
}

public void selectInstallLocation() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(installLocation.getText()));
chooser.setDialogTitle(Translator.getString("gui.selectInstallLocation"));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);

if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
installLocation.setText(chooser.getSelectedFile().getAbsolutePath());
}
}

@Override
public void updateProgress(String text, int percentage) {
statusLabel.setText(text);
statusLabel.setForeground(Color.BLACK);
progressBar.setValue(percentage);
}

@Override
public void error(String error) {
statusLabel.setText(error);
statusLabel.setForeground(Color.RED);
}

public static void start()
throws XmlPullParserException, IOException, ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException,
IllegalAccessException {
//This will make people happy
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
InstallerGui dialog = new InstallerGui();
dialog.pack();
dialog.setTitle(Translator.getString("fabric.installer.name"));
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}

private void initComponents() {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 10, 5, 10));

addRow(jPanel -> {
jPanel.add(new JLabel(Translator.getString("gui.version.mappings")));
jPanel.add(mappingVersionComboBox = new JComboBox<>());
});

addRow(jPanel -> {
jPanel.add(new JLabel("gui.version.loader"));
jPanel.add(loaderVersionComboBox = new JComboBox<>());
});

addRow(jPanel -> {
jPanel.add(new JLabel(Translator.getString("gui.selectInstallLocation")));
jPanel.add(installLocation = new JTextField());
jPanel.add(selectFolderButton = new JButton());

selectFolderButton.setText("...");
selectFolderButton.addActionListener(e -> selectInstallLocation());
installLocation.setText(Utils.findDefaultInstallDir().getAbsolutePath());
});

addRow(jPanel -> {
jPanel.add(statusLabel = new JLabel());
jPanel.add(progressBar = new JProgressBar());
progressBar.setMaximum(100);

//Forces the progress bar to expand to fit the width
jPanel.setLayout(new GridLayout(0, 1));
});

addRow(jPanel -> {
jPanel.add(buttonInstall = new JButton(Translator.getString("gui.install")));
buttonInstall.addActionListener(e -> install());
getRootPane().setDefaultButton(buttonInstall);
});

}

private void addRow(Consumer<JPanel> consumer) {
JPanel panel = new JPanel(new FlowLayout());
consumer.accept(panel);
contentPane.add(panel, BorderLayout.CENTER);
}

}
Loading

0 comments on commit 97892de

Please sign in to comment.