This commit is contained in:
2022-04-10 22:54:11 -04:00
parent c4350abee8
commit 98f849ea12
20 changed files with 1043 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
package tech.nevets.jamlupdater;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
static JProgressBar progressBar = new JProgressBar();
public static final int TASKS = 5;
public static int task = 0;
public static final int WIDTH = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
public static final int HEIGHT = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
public Gui() {
JPanel contentPane = new JPanel();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WIDTH / 8, HEIGHT / 8);
setContentPane(contentPane);
contentPane.setLayout(null);
int pbWidth = (int) (getWidth() / 1.3);
int pbHeight = getHeight() / 8;
int pbX = (int) ((getWidth() - pbWidth) / 2.3);
int pbY = getHeight() - (pbHeight * 4);
progressBar.setBounds(pbX, pbY, pbWidth, pbHeight);
contentPane.add(progressBar);
JLabel label = new JLabel("Updating JAML ...");
label.setBounds(40, 30, WIDTH / 12, HEIGHT / 64);
contentPane.add(label);
}
public static void smoothIncrease() {
int i = progressBar.getValue();
while (i <= (100 / TASKS) * task) {
progressBar.setValue(i);
i += 1;
}
}
}

View File

@@ -0,0 +1,98 @@
package tech.nevets.jamlupdater;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import org.apache.commons.io.FileUtils;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static File exePath;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
Gui.smoothIncrease();
}
Gui gui = new Gui();
gui.setTitle("JAML Autoupdater");
gui.setIconImage(new ImageIcon(Main.class.getClassLoader().getResource("icon.png")).getImage());
gui.setLocationRelativeTo(null);
gui.setVisible(true);
Gui.task++;
Gui.smoothIncrease();
if (System.getenv("JAML_HOME") == null) {
Gui.task++;
exePath = new File(System.getenv("APPDATA") + "\\.jaml\\JAML.exe");
Gui.smoothIncrease();
} else {
Gui.task++;
exePath = new File(System.getenv("JAML_HOME") + "\\JAML.exe");
Gui.smoothIncrease();
}
try {
Gui.task++;
String response = getRequest(new URI("https://api.nevets.tech/jaml/version.json"), new HashMap<>());
Gui.smoothIncrease();
Gui.task++;
JsonElement versionJson = new Gson().fromJson(response, JsonElement.class);
Gui.smoothIncrease();
Gui.task++;
String url = versionJson.getAsJsonObject().get("url").toString().replace("\"", "");
Gui.smoothIncrease();
Gui.task++;
FileUtils.copyURLToFile(new URL(url), exePath);
Gui.smoothIncrease();
Runtime.getRuntime().exec(exePath.toString());
System.exit(0);
} catch (URISyntaxException | IOException e) {
Gui.task++;
Gui.task++;
Gui.task++;
Gui.task++;
Gui.task++;
Gui.task++;
Gui.task++;
Gui.smoothIncrease();
e.printStackTrace();
}
}
public static String getRequest(URI uri, Map<String, String> headers) {
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.GET()
.uri(uri);
for (Map.Entry<String, String> pair : headers.entrySet()) {
requestBuilder.setHeader(pair.getKey(), pair.getValue());
}
HttpRequest request = requestBuilder.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return "";
}
}
}

BIN
src/main/resources/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 706 KiB