new downloader gui WIP, auto-download depends on start

This commit is contained in:
Downforce Agent 2024-07-09 02:10:37 -05:00
parent d109285657
commit 0008301f83
3 changed files with 214 additions and 3 deletions

34
src/Fozzie.form Normal file
View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="Fozzie">
<grid id="27dc6" binding="frameContainer" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="4913b" class="javax.swing.JProgressBar" binding="progressBar">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="50" height="-1"/>
<maximum-size width="200" height="-1"/>
</grid>
</constraints>
<properties>
<indeterminate value="false"/>
<stringPainted value="true"/>
<value value="0"/>
</properties>
</component>
<component id="46ede" class="javax.swing.JLabel" binding="label">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Downloading file..."/>
</properties>
</component>
</children>
</grid>
</form>

149
src/Fozzie.java Normal file
View File

@ -0,0 +1,149 @@
/*
* Firestar Mod Manager
* Copyright (C) 2024 bonkmaykr
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class Fozzie {
private JFrame frame = new JFrame();
public JProgressBar progressBar;
private JPanel frameContainer;
private JLabel label;
private HttpURLConnection httpConn;
private int contentLength;
private InputStream inputStream;
//public File output;
public boolean backgroundDone = false;
boolean DownloadFile(String url, String odir, String oname) {
//output = o;
frame.add(frameContainer);
frame.setSize(300, 100);
frame.setTitle("Firestar Mod Manager");
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.setLayout(new GridLayout());
frame.setLocationRelativeTo(null);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
label.setText("Downloading \"" + oname + "\"");
try {
URL fileURL = new URL(url);
httpConn = (HttpURLConnection) fileURL.openConnection();
int response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
contentLength = httpConn.getContentLength();
inputStream = httpConn.getInputStream();
} else if (response == 404) {
throw new IOException(
"File missing from remote server.");
} else {
throw new IOException(
"Unexpected response; Server replied with " + response);
}
new FozzieDownloader(this, url, odir, oname, httpConn.getContentLength()).doInBackground();
while (!backgroundDone) {}
inputStream.close();
httpConn.disconnect();
frame.dispose();
return true;
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(frame, "Internal Error: URL given to Fozzie is not valid.\nGet a programmer!", "Fatal Error", JOptionPane.ERROR_MESSAGE);
frame.dispose();
return false;
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Error: " + e.getMessage(), "Fatal Error", JOptionPane.ERROR_MESSAGE);
frame.dispose();
return false;
}
}
public void disconnect() throws IOException {
inputStream.close();
httpConn.disconnect();
}
public int getContentLength() {
return this.contentLength;
}
public InputStream getInputStream() {
return this.inputStream;
}
}
class FozzieDownloader extends SwingWorker<Void, Void> {
private static final int BUFFER_SIZE = 4096;
private String downloadURL;
private String saveDirectory;
private String saveName;
private final Fozzie gui;
private final long completeSize;
public FozzieDownloader(Fozzie gui, String downloadURL, String saveDirectory, String saveName, long size) {
this.gui = gui;
this.downloadURL = downloadURL;
this.saveDirectory = saveDirectory;
this.saveName = saveName;
this.completeSize = size;
}
@Override
protected Void doInBackground() throws Exception {
long downloadedSize = 0;
File downloadLocationDir = new File(saveDirectory);
File downloadLocation = new File(saveDirectory + "/" + saveName);
downloadLocationDir.mkdirs();
if (!downloadLocation.isFile()) {
downloadLocation.createNewFile();
}
BufferedInputStream in = new BufferedInputStream(new URL(downloadURL).openStream());
FileOutputStream fos = new FileOutputStream(saveDirectory + "/" + saveName);
BufferedOutputStream out = new BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data,0,1024))>=0) {
downloadedSize += x;
int progress = (int) ((((double)downloadedSize) / ((double)completeSize)) * 100d);
gui.progressBar.setValue(progress);
out.write(data, 0, x);
}
out.close();
in.close();
gui.backgroundDone = true;
return null;
}
}

View File

@ -16,6 +16,8 @@
* along with this program. If not, see https://www.gnu.org/licenses/. * along with this program. If not, see https://www.gnu.org/licenses/.
*/ */
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.json.*; import org.json.*;
import java.awt.*; import java.awt.*;
@ -23,12 +25,12 @@ import java.io.*;
import java.nio.file.*; import java.nio.file.*;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
import javax.swing.JOptionPane; import javax.swing.*;
public class Main { public class Main {
// Build Information // Build Information
public static final String vstr = "Release 1.2"; public static final String vstr = "Release 1.3";
public static final String vcode = "Dekka"; public static final String vcode = "Tetsuo";
public static final int vint = 0; public static final int vint = 0;
// User Settings // User Settings
@ -86,6 +88,15 @@ public class Main {
fExo2 = new Font("Arial", Font.PLAIN, 12); fExo2 = new Font("Arial", Font.PLAIN, 12);
} }
// download dependencies if we know we haven't been here before
// will also need to call this if a needed file is missing before use
//
// mostly for testing. will move to onboarding screen later
if (!new File(System.getProperty("user.home") + "/.firestar/").isDirectory()) {
new File(System.getProperty("user.home") + "/.firestar/").mkdirs();
downloadDependencies();
}
// check and load configs // check and load configs
File fConf = new File(System.getProperty("user.home") + "/.firestar/firestar.conf"); File fConf = new File(System.getProperty("user.home") + "/.firestar/firestar.conf");
if (!fConf.isFile()) { if (!fConf.isFile()) {
@ -153,4 +164,21 @@ public class Main {
} }
file.delete(); file.delete();
} }
public static boolean downloadDependencies () {
boolean downloader = new Fozzie().DownloadFile("https://bonkmaykr.worlio.com/http/firestar/firesdk.zip", System.getProperty("user.home") + "/.firestar/", "firesdk.zip");
if (!downloader) {return false;}
ZipFile sdk = new ZipFile(System.getProperty("user.home") + "/.firestar/firesdk.zip");
try {
sdk.extractAll(System.getProperty("user.home") + "/.firestar/");
} catch (ZipException e) {
JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "Critical Error", JOptionPane.ERROR_MESSAGE);
System.out.println(e.getMessage());
return false;
}
sdk.getFile().delete(); // cleanup
return true;
}
} }