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