diff --git a/src/Fozzie.form b/src/Fozzie.form new file mode 100644 index 0000000..9b538b3 --- /dev/null +++ b/src/Fozzie.form @@ -0,0 +1,34 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/Fozzie.java b/src/Fozzie.java new file mode 100644 index 0000000..7d996c2 --- /dev/null +++ b/src/Fozzie.java @@ -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 { + 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; + } +} diff --git a/src/Main.java b/src/Main.java index cf55013..e3d3f6b 100644 --- a/src/Main.java +++ b/src/Main.java @@ -16,6 +16,8 @@ * 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 java.awt.*; @@ -23,12 +25,12 @@ import java.io.*; import java.nio.file.*; import java.util.*; import java.util.List; -import javax.swing.JOptionPane; +import javax.swing.*; public class Main { // Build Information - public static final String vstr = "Release 1.2"; - public static final String vcode = "Dekka"; + public static final String vstr = "Release 1.3"; + public static final String vcode = "Tetsuo"; public static final int vint = 0; // User Settings @@ -86,6 +88,15 @@ public class Main { 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 File fConf = new File(System.getProperty("user.home") + "/.firestar/firestar.conf"); if (!fConf.isFile()) { @@ -153,4 +164,21 @@ public class Main { } 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; + } } \ No newline at end of file