diff --git a/MiniCraftLauncher/bin/.gitignore b/MiniCraftLauncher/bin/.gitignore new file mode 100644 index 0000000..c2d9872 --- /dev/null +++ b/MiniCraftLauncher/bin/.gitignore @@ -0,0 +1 @@ +/com/ diff --git a/MiniCraftLauncher/bin/docs/contents.html b/MiniCraftLauncher/bin/docs/contents.html deleted file mode 100644 index 6ebe72e..0000000 --- a/MiniCraftLauncher/bin/docs/contents.html +++ /dev/null @@ -1,45 +0,0 @@ -Table of Contents: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Startindex.html
Keybindskeybinds.html
\ No newline at end of file diff --git a/MiniCraftLauncher/bin/docs/index.html b/MiniCraftLauncher/bin/docs/index.html deleted file mode 100644 index bf5ed9f..0000000 --- a/MiniCraftLauncher/bin/docs/index.html +++ /dev/null @@ -1,11 +0,0 @@ - - -Welcome to the Reference! -

This is a small collection of html documents that are up to date with whatever features are in the verision of the launcher you are currently using.

-

Here there are textual guides on how to use this program's various features.

-

The best part: It works offline! All of the HTML is stored within the .jar file of this app.

-
-How to use the guide: -

On the left is the Table of Contents. Any link you click within will load a page on the right side, where this text is.

- - \ No newline at end of file diff --git a/MiniCraftLauncher/bin/docs/keybinds.html b/MiniCraftLauncher/bin/docs/keybinds.html deleted file mode 100644 index 537eee1..0000000 --- a/MiniCraftLauncher/bin/docs/keybinds.html +++ /dev/null @@ -1,45 +0,0 @@ -Keybinds - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Delete versionDELETE (del)
Edit versionENTER (Enter)
Launch versionDouble-click left mouse button
Version context menuClick right mouse button (right-click)
\ No newline at end of file diff --git a/MiniCraftLauncher/res/docs/contents.html b/MiniCraftLauncher/res/docs/contents.html deleted file mode 100644 index 6ebe72e..0000000 --- a/MiniCraftLauncher/res/docs/contents.html +++ /dev/null @@ -1,45 +0,0 @@ -Table of Contents: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Startindex.html
Keybindskeybinds.html
\ No newline at end of file diff --git a/MiniCraftLauncher/res/docs/index.html b/MiniCraftLauncher/res/docs/index.html deleted file mode 100644 index bf5ed9f..0000000 --- a/MiniCraftLauncher/res/docs/index.html +++ /dev/null @@ -1,11 +0,0 @@ - - -Welcome to the Reference! -

This is a small collection of html documents that are up to date with whatever features are in the verision of the launcher you are currently using.

-

Here there are textual guides on how to use this program's various features.

-

The best part: It works offline! All of the HTML is stored within the .jar file of this app.

-
-How to use the guide: -

On the left is the Table of Contents. Any link you click within will load a page on the right side, where this text is.

- - \ No newline at end of file diff --git a/MiniCraftLauncher/res/docs/keybinds.html b/MiniCraftLauncher/res/docs/keybinds.html deleted file mode 100644 index 537eee1..0000000 --- a/MiniCraftLauncher/res/docs/keybinds.html +++ /dev/null @@ -1,45 +0,0 @@ -Keybinds - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Delete versionDELETE (del)
Edit versionENTER (Enter)
Launch versionDouble-click left mouse button
Version context menuClick right mouse button (right-click)
\ No newline at end of file diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/ChannelObject.java b/MiniCraftLauncher/src/com/mt/minilauncher/ChannelObject.java new file mode 100644 index 0000000..c9b0ff4 --- /dev/null +++ b/MiniCraftLauncher/src/com/mt/minilauncher/ChannelObject.java @@ -0,0 +1,22 @@ +package com.mt.minilauncher; + +import java.io.Serializable; + +public class ChannelObject implements Serializable{ + /** + * + */ + private static final long serialVersionUID = 1L; + String channelFile; + + public ChannelObject(String channelFile) { + this.channelFile = channelFile; + } + + @Override + public String toString() { + return channelFile; + } + + +} diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/Initializer.java b/MiniCraftLauncher/src/com/mt/minilauncher/Initializer.java index aa22f2a..379ddd2 100644 --- a/MiniCraftLauncher/src/com/mt/minilauncher/Initializer.java +++ b/MiniCraftLauncher/src/com/mt/minilauncher/Initializer.java @@ -88,7 +88,7 @@ public static void cleanFolders() { if(x == Debug.OK) { Util.purgeDirectory(launcherPath.toFile()); touchFoldersAndFiles(); - LauncherWindow.instance.forceReDownload(); + LauncherWindow.instance.updateUI(); } else { return; } diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/LauncherWindow.java b/MiniCraftLauncher/src/com/mt/minilauncher/LauncherWindow.java index 93e08a4..0074f3d 100644 --- a/MiniCraftLauncher/src/com/mt/minilauncher/LauncherWindow.java +++ b/MiniCraftLauncher/src/com/mt/minilauncher/LauncherWindow.java @@ -1,50 +1,59 @@ package com.mt.minilauncher; import java.awt.EventQueue; + import javax.swing.JFrame; +import javax.swing.JMenuBar; +import javax.swing.JTextArea; +import java.awt.BorderLayout; +import javax.swing.JScrollPane; +import javax.swing.JTree; +import javax.swing.SwingUtilities; import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeModel; +import javax.xml.parsers.ParserConfigurationException; + +import org.xml.sax.SAXException; import com.mt.minilauncher.downloader.Downloader; +import com.mt.minilauncher.util.Callback; import com.mt.minilauncher.util.EditUtil; +import com.mt.minilauncher.util.OrderedProperties; import com.mt.minilauncher.util.Util; +import com.mt.minilauncher.util.XMLConverter; import com.mt.minilauncher.windows.AboutPanel; -import com.mt.minilauncher.windows.ReferenceApp; +import com.mt.minilauncher.windows.ChannelSelector; import com.mt.minilauncher.windows.SystemInfo; -import javax.swing.JMenuBar; +import javax.swing.JMenu; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; import javax.swing.JPopupMenu; -import javax.swing.JList; -import java.awt.BorderLayout; -import javax.swing.DefaultListModel; import javax.swing.ImageIcon; -import javax.swing.ListSelectionModel; -import javax.swing.SwingUtilities; -import javax.swing.JScrollPane; -import javax.swing.JMenu; +import javax.swing.JCheckBoxMenuItem; +import java.awt.Color; +import java.awt.Desktop; +import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Path; import java.nio.file.Paths; -import javax.swing.JMenuItem; -import java.awt.event.ActionListener; import java.awt.event.ActionEvent; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import javax.swing.JTextArea; -import java.awt.Color; -import javax.swing.JPanel; -import javax.swing.border.BevelBorder; -import javax.swing.JCheckBoxMenuItem; public class LauncherWindow { + public static LauncherWindow instance; private JFrame frmLauncher; - private JList list; -public static LauncherWindow instance; -private JTextArea textArea; -private JCheckBoxMenuItem enableHideMenuItem; - + private JCheckBoxMenuItem hideLauncherDuringPlayCheckBox; + private JTree tree; + private JTextArea console; /** * Launch the application. @@ -56,7 +65,6 @@ public void run() { LauncherWindow window = new LauncherWindow(); window.frmLauncher.setVisible(true); } catch (Exception e) { - Debug.callCrashDialog("ERROR","The app failed to launch for some reason.\nCheck the console output.",Debug.ERR); e.printStackTrace(); } } @@ -67,12 +75,13 @@ public void run() { * Create the application. */ public LauncherWindow() { - try { // enable native UI - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - } catch(Exception e){ - Debug.callCrashDialog("ERROR", "Couldn't access native UI class for some reason.\nCheck the console output.", Debug.ERR); - e.printStackTrace(); - } + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException + | UnsupportedLookAndFeelException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } initialize(); } @@ -82,7 +91,6 @@ public LauncherWindow() { private void initialize() { instance = this; Initializer.touchFoldersAndFiles(); - frmLauncher = new JFrame(); frmLauncher.setTitle("Launcher"); frmLauncher.setBounds(100, 100, 800, 600); @@ -90,17 +98,10 @@ private void initialize() { frmLauncher.setIconImage(new ImageIcon(LauncherWindow.class.getResource("/logo.png")).getImage()); JMenuBar menuBar = new JMenuBar(); frmLauncher.setJMenuBar(menuBar); - + JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); - - JMenuItem exitMenuItem = new JMenuItem("Exit"); - exitMenuItem.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - System.exit(0); - } - }); - + JMenuItem launcherFolderMenuItem = new JMenuItem("Open Launcher Folder"); launcherFolderMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -113,120 +114,90 @@ public void actionPerformed(ActionEvent e) { } }); fileMenu.add(launcherFolderMenuItem); - fileMenu.add(exitMenuItem); - - JMenu editMenu = new JMenu("Edit"); - menuBar.add(editMenu); - - JMenu channelMenu = new JMenu("Select Channel"); - channelMenu.setName("editMenu.channelMenu"); - editMenu.add(channelMenu); - - JMenu miniplusmenu = new JMenu("Minicraft+"); - channelMenu.add(miniplusmenu); - - JMenuItem releasesMenuItem = new JMenuItem("Release"); - releasesMenuItem.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - getList().setModel(Util.buildIndex(true, false)); - getList().updateUI(); - updateUI(); - } - }); - miniplusmenu.add(releasesMenuItem); - - JMenuItem devMenuItem = new JMenuItem("Pre-Release/dev"); - devMenuItem.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - getList().setModel(Util.buildIndex(false, false)); - getList().updateUI(); - updateUI(); - } - }); - miniplusmenu.add(devMenuItem); - - JMenuItem modsMenuItem = new JMenuItem("Mods"); - modsMenuItem.addActionListener(new ActionListener() { + + JMenuItem exitMenuItem = new JMenuItem("Exit"); + exitMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - Debug.callCrashDialog("Warning!", "The mods in this section are not fully supported.\nThey may contain hardcoded save/config paths or may not work at all.\nThey are here for funsies.", Debug.WARN); - getList().setModel(Util.buildIndex(false, true)); - getList().updateUI(); - updateUI(); + System.exit(0); } }); - channelMenu.add(modsMenuItem); - - - - JMenuItem insertVersionMenuItem = new JMenuItem("Insert Version Profile"); - insertVersionMenuItem.addActionListener(new ActionListener() { + fileMenu.add(exitMenuItem); + + JMenu editMenu = new JMenu("Edit"); + menuBar.add(editMenu); + + JMenuItem channelMenuItem = new JMenuItem("Select Channel"); + channelMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - int baseSize = getList().getModel().getSize(); - getList().setModel(Util.addToJList(getList().getModel(), new VersionObject(), baseSize)); - getList().updateUI(); - getList().setSelectedIndex(baseSize); - getList().updateUI(); - EditUtil.editInfo(getList().getSelectedValue()); - getList().updateUI(); + ChannelSelector cs = new ChannelSelector(); + cs.setVisible(true); + cs.getOkButton().addActionListener(l -> { + try { + Path filePath = Paths.get(Initializer.indexPath.toString(), + String.format("index%d.xml", cs.getList().getSelectedIndex())); + Util.downloadUsingNIO(cs.getList().getSelectedValue().channelFile, filePath.toString()); + DefaultTreeModel dtm = new DefaultTreeModel(XMLConverter.fromXML(filePath.toString())); + tree.setModel(dtm); + tree.updateUI(); + } catch (IOException | ParserConfigurationException | SAXException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + cs.dispose(); + }); } }); - - JMenuItem cleanIndexButton = new JMenuItem("Clean Index"); - cleanIndexButton.addActionListener(new ActionListener() { + editMenu.add(channelMenuItem); + + JMenuItem cleanIndexMenuItem = new JMenuItem("Clean Index"); + cleanIndexMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Util.purgeDirectory(Initializer.indexPath.toFile()); - list.setModel(new DefaultListModel()); - updateUI(); - } - }); - editMenu.add(cleanIndexButton); - editMenu.add(insertVersionMenuItem); - - JMenuItem refreshUIMenuItem = new JMenuItem("Refresh UI"); - refreshUIMenuItem.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - updateUI(); } }); - editMenu.add(refreshUIMenuItem); - - JMenuItem cleanFolderMenuItem = new JMenuItem("Clean Folders"); - cleanFolderMenuItem.addActionListener(new ActionListener() { + editMenu.add(cleanIndexMenuItem); + + JMenuItem cleanFoldersMenuItem = new JMenuItem("Clean Folders"); + cleanFoldersMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - Initializer.cleanFolders(); - getList().setModel(new DefaultListModel()); - getList().updateUI(); + Util.purgeDirectoryButKeepSubDirectories(Initializer.launcherPath.toFile()); } }); - editMenu.add(cleanFolderMenuItem); - + editMenu.add(cleanFoldersMenuItem); + JMenu optionsMenu = new JMenu("Options"); menuBar.add(optionsMenu); - - enableHideMenuItem = new JCheckBoxMenuItem("Hide Launcher During Play"); - enableHideMenuItem.setSelected(Util.parseBoolean(Initializer.options.get("window.hideDuringPlay").toString())); - optionsMenu.add(enableHideMenuItem); - + + hideLauncherDuringPlayCheckBox = new JCheckBoxMenuItem("Hide Launcher During Play"); + hideLauncherDuringPlayCheckBox.setSelected(true); + optionsMenu.add(hideLauncherDuringPlayCheckBox); + JMenu helpMenu = new JMenu("Help"); menuBar.add(helpMenu); - + JMenuItem aboutMenuItem = new JMenuItem("About"); aboutMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - Debug.callCrashDialog("About", new AboutPanel(), Debug.INF); + Debug.callCrashDialog("About", new AboutPanel(), Debug.TST); } }); helpMenu.add(aboutMenuItem); - + JMenuItem referenceMenuItem = new JMenuItem("Reference"); referenceMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - ReferenceApp ra = new ReferenceApp(); - ra.setVisible(true); + if(Desktop.isDesktopSupported()) { + try { + Desktop.getDesktop().browse(new URL("https://github.com/MajickTek/MiniCraftLauncher/wiki").toURI()); + } catch (IOException | URISyntaxException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } } }); helpMenu.add(referenceMenuItem); - + JMenuItem systemInfoMenuItem = new JMenuItem("System Info"); systemInfoMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -235,67 +206,78 @@ public void actionPerformed(ActionEvent e) { } }); helpMenu.add(systemInfoMenuItem); - + + console = new JTextArea(); + console.setEditable(false); + console.setBackground(Color.BLACK); + console.setForeground(Color.GREEN); + frmLauncher.getContentPane().add(console, BorderLayout.NORTH); + JScrollPane scrollPane = new JScrollPane(); frmLauncher.getContentPane().add(scrollPane, BorderLayout.CENTER); - - list = new JList<>(); - list.addKeyListener(new KeyAdapter() { - @Override - public void keyPressed(KeyEvent e) { - if(e.getKeyCode() == KeyEvent.VK_DELETE) { - clean(); - } else if(e.getKeyCode() == KeyEvent.VK_ENTER) { - edit(); - } - } - }); - list.addMouseListener(new MouseAdapter() { + + tree = new JTree(new DefaultMutableTreeNode("empty")); + + tree.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { - if(e.getClickCount() == 2) { - VersionObject selectedVersion = getList().getSelectedValue(); - if(selectedVersion != null) { - if(selectedVersion.isDownloaded) { - String jarPath = Paths.get(Initializer.jarPath.toString(), selectedVersion.version + ".jar").toString(); + if (e.getClickCount() == 2) { + DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); + if (node == null) + return; + if (node.isLeaf() && !node.toString().equals("empty")) { + VersionObject vo = (VersionObject) node.getUserObject(); + if (vo.isDownloaded) { + String jarPath = Paths.get(Initializer.jarPath.toString(), vo.version + ".jar").toString(); try { - Util.launchJar(jarPath, selectedVersion.version, instance, enableHideMenuItem.isSelected()); + Util.launchJar(jarPath, vo.version, instance.frmLauncher, true); } catch (IOException e1) { - Debug.callCrashDialog("ERROR", "Something went wrong launching the jar.\nPlease check the console output.", Debug.ERR); + // TODO Auto-generated catch block e1.printStackTrace(); } } else { - Downloader downloader = new Downloader(selectedVersion.getURL(), - Paths.get(Initializer.jarPath.toString(), - selectedVersion.version + ".jar").toString(), - textArea, - () -> {//callback function which runs when download is finished (at 100% and hasn't failed) - selectedVersion.isDownloaded = true; - list.updateUI(); + Downloader downloader = new Downloader(vo.getURL(), + Paths.get(Initializer.jarPath.toString(), vo.version + ".jar").toString(), console, + () -> {// callback function which runs when download is finished (at 100% and hasn't + // failed) + vo.isDownloaded = true; + tree.updateUI(); }); downloader.download(); } } } - } - - @Override - public void mousePressed(MouseEvent e) { - if(SwingUtilities.isRightMouseButton(e)) { - list.setSelectedIndex(list.locationToIndex(e.getPoint())); - + + // right click + if (SwingUtilities.isRightMouseButton(e)) { + int row = tree.getClosestRowForLocation(e.getX(), e.getY()); + tree.setSelectionRow(row); + tree.updateUI(); + DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); + if (node == null) + return; + + if (node.isLeaf() && !node.toString().equals("empty")) { + VersionObject vo = (VersionObject) node.getUserObject(); + JPopupMenu menu = new JPopupMenu(); JMenuItem editMenu = new JMenuItem("Edit"); JMenuItem cleanMenu = new JMenuItem("Clean"); JMenuItem folderMenu = new JMenuItem("Open Save Folder"); - - editMenu.addActionListener(a -> edit()); - - cleanMenu.addActionListener(a -> clean()); - - File jarPath = Paths.get(Initializer.savesDir.toString(), list.getSelectedValue().version).toFile(); + + editMenu.addActionListener(a -> { + node.setUserObject(EditUtil.editInfo(vo)); + updateUI(); + }); + + cleanMenu.addActionListener(a -> { + Initializer.cleanVersion(vo.version); + updateUI(); + }); + + File jarPath = Paths.get(Initializer.savesDir.toString(), vo.version).toFile(); folderMenu.addActionListener(a -> { - if(jarPath.exists()) { + if (jarPath.exists()) { try { Util.openNative(jarPath); } catch (IOException e1) { @@ -304,7 +286,7 @@ public void mousePressed(MouseEvent e) { } } else { try { - Util.openNative(Initializer.savesDir.toFile());//This should always be there + Util.openNative(Initializer.savesDir.toFile());// This should always be there } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); @@ -314,88 +296,54 @@ public void mousePressed(MouseEvent e) { menu.add(editMenu); menu.add(cleanMenu); menu.add(folderMenu); - menu.show(list, e.getPoint().x, e.getPoint().y); + menu.show(tree, e.getPoint().x, e.getPoint().y); + } + } } + }); - list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - list.setSelectedIndex(0); - scrollPane.setViewportView(list); - - list.setModel(Util.buildIndex(true, false)); - - JPanel panel = new JPanel(); - panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); - scrollPane.setColumnHeaderView(panel); - panel.setLayout(new BorderLayout(0, 0)); - - textArea = new JTextArea(); - textArea.setForeground(Color.GREEN); - textArea.setBackground(Color.BLACK); - textArea.setEditable(false); - panel.add(textArea); - list.updateUI(); + scrollPane.setViewportView(tree); updateUI(); - } - public void clean() { - Initializer.cleanVersion(list.getSelectedValue().version); - forceReDownload(list.getSelectedIndex()); - list.updateUI(); - updateUI(); + public JCheckBoxMenuItem getHideLauncherDuringPlayCheckBox() { + return hideLauncherDuringPlayCheckBox; } - public void edit() { - list.setModel(Util.addToJList(list.getModel(), EditUtil.editInfo(list.getSelectedValue()), list.getSelectedIndex())); - list.updateUI(); + public JTree getTree() { + return tree; } - public JList getList() { - return list; + public JTextArea getConsole() { + return console; } - + public void updateUI() { - DefaultListModel dlm = (DefaultListModel) getList().getModel(); + DefaultTreeModel dtm = (DefaultTreeModel) tree.getModel(); + walk(dtm, dtm.getRoot()); + tree.setModel(dtm); + tree.updateUI(); + } + + private void walk(DefaultTreeModel dtm, Object root) { + int cc; + cc = dtm.getChildCount(root); String basePath = Initializer.jarPath.toString(); - - for(int i = 0; i < dlm.getSize(); i++) { - String base = Paths.get(basePath, dlm.getElementAt(i).version + ".jar").toString(); - File file = new File(base); - if(file.exists()) { - dlm.get(i).isDownloaded = true; + for (int i = 0; i < cc; i++) { + Object child = dtm.getChild(root, i); + if (dtm.isLeaf(child)) { + System.out.println(child.toString() + " is a leaf"); + VersionObject vo = (VersionObject) child; + String base = Paths.get(basePath, vo.version + ".jar").toString(); + File file = new File(base); + if (file.exists()) { + vo.isDownloaded = true; + } + } else { + walk(dtm, child); } } - getList().setModel(dlm); - getList().updateUI(); - } - - public void forceReDownload() { - DefaultListModel dlm = (DefaultListModel) getList().getModel(); - - for(int i = 0; i < dlm.getSize(); i++) { - dlm.get(i).isDownloaded = false; - } - getList().setModel(dlm); - getList().updateUI(); - } - - public void forceReDownload(int index) { - DefaultListModel dlm = (DefaultListModel) getList().getModel(); - - dlm.get(index).isDownloaded = false; - getList().setModel(dlm); - getList().updateUI(); - } - - - public JFrame getFrmLauncher() { - return frmLauncher; - } - public JTextArea getTextArea() { - return textArea; - } - public JCheckBoxMenuItem getEnableHideMenuItem() { - return enableHideMenuItem; } + } diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/downloader/Downloader.java b/MiniCraftLauncher/src/com/mt/minilauncher/downloader/Downloader.java index 358b0b4..07cf181 100644 --- a/MiniCraftLauncher/src/com/mt/minilauncher/downloader/Downloader.java +++ b/MiniCraftLauncher/src/com/mt/minilauncher/downloader/Downloader.java @@ -34,6 +34,13 @@ public Downloader(String url, String localLocation, JTextArea jta, Callback onFi callback = onFinish; } + public Downloader(String url, String localLocation, Callback onFinish) { + this.url = url; + this.localLocation = localLocation; + this.jta = null; + callback = onFinish; + } + public void download() { FileDownloader fileDownloader = new FileDownloader(this); fileDownloader.setUrl(url); diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/util/EditUtil.java b/MiniCraftLauncher/src/com/mt/minilauncher/util/EditUtil.java index 48065f3..651cc18 100644 --- a/MiniCraftLauncher/src/com/mt/minilauncher/util/EditUtil.java +++ b/MiniCraftLauncher/src/com/mt/minilauncher/util/EditUtil.java @@ -49,6 +49,8 @@ public static VersionObject editInfo(VersionObject vo) { } else if(vo == null) { return new VersionObject(); + } else { + return vo; } return null; } diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/util/OrderedProperties.java b/MiniCraftLauncher/src/com/mt/minilauncher/util/OrderedProperties.java index cfd6cde..d84f73a 100644 --- a/MiniCraftLauncher/src/com/mt/minilauncher/util/OrderedProperties.java +++ b/MiniCraftLauncher/src/com/mt/minilauncher/util/OrderedProperties.java @@ -21,6 +21,10 @@ public synchronized Object put(Object key, Object value) { return linkMap.put(key, value); } + @Override + public Object get(Object key) { + return linkMap.get(key); + } @Override public boolean contains(Object value) { diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/util/Util.java b/MiniCraftLauncher/src/com/mt/minilauncher/util/Util.java index 9087c6c..9d171d5 100644 --- a/MiniCraftLauncher/src/com/mt/minilauncher/util/Util.java +++ b/MiniCraftLauncher/src/com/mt/minilauncher/util/Util.java @@ -19,6 +19,7 @@ import java.util.Properties; import javax.swing.DefaultListModel; +import javax.swing.JFrame; import javax.swing.ListModel; import javax.xml.parsers.ParserConfigurationException; @@ -32,54 +33,6 @@ public class Util { - - - public static DefaultListModel buildIndex(boolean isRelease, boolean modsFlag) { - String baseURL = "https://raw.githubusercontent.com/MajickTek/MiniCraftLauncherIndex/main/"; - - String indexFileName = isRelease ? "release.xml" : "dev.xml"; - if(!isRelease && modsFlag) { - indexFileName = "mods.xml"; - } - String indexURL = baseURL + indexFileName; - - try { - downloadUsingNIO(indexURL, Paths.get(Initializer.indexPath.toString(), indexFileName).toString()); - } catch (IOException e1) { - Debug.callCrashDialog("ERROR", "Failed to download index file. Check console for details.", Debug.ERR); - e1.printStackTrace(); - } - DefaultListModel model = new DefaultListModel<>(); - - VersionObject[] vos; - try { - vos = XMLConverter.fromXML(Paths.get(Initializer.indexPath.toString(), indexFileName).toString()); - for(int i = 0; i < vos.length; i++ ) { - model.add(i, vos[i]); - } - } catch (ParserConfigurationException | SAXException | IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return model; - } - - - - public static DefaultListModel addToJList(ListModel base, VersionObject vo, int index) { - DefaultListModel dlm = (DefaultListModel) base; - dlm.add(index, vo); - - return dlm; - } - - public static DefaultListModel removeFromJList(ListModel base, int index) { - DefaultListModel dlm = (DefaultListModel) base; - dlm.remove(index); - - return dlm; - } - public static void downloadUsingNIO(String urlStr, String file) throws IOException { URL url = new URL(urlStr); ReadableByteChannel rbc = Channels.newChannel(url.openStream()); @@ -89,18 +42,18 @@ public static void downloadUsingNIO(String urlStr, String file) throws IOExcepti rbc.close(); } - public static void launchJar(String path, String version, LauncherWindow lw, boolean hideLauncher) throws IOException { + public static void launchJar(String path, String version, JFrame frame, boolean hideLauncher) throws IOException { String vPath = Paths.get(Initializer.savesDir.toString(), version).toString(); Process ps = Runtime.getRuntime().exec(new String[] {"java", "-jar", path, "--savedir", vPath}); if(hideLauncher) { - lw.getFrmLauncher().setVisible(false); + frame.setVisible(false); try { ps.waitFor(); } catch (InterruptedException e) { Debug.callCrashDialog("ERROR", "Something failed while waiting for the game to terminate.\nCheck the console output.", Debug.ERR); e.printStackTrace(); } - lw.getFrmLauncher().setVisible(true); + frame.setVisible(true); } } diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/util/XMLConverter.java b/MiniCraftLauncher/src/com/mt/minilauncher/util/XMLConverter.java index 8a7b773..8605a02 100644 --- a/MiniCraftLauncher/src/com/mt/minilauncher/util/XMLConverter.java +++ b/MiniCraftLauncher/src/com/mt/minilauncher/util/XMLConverter.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.util.ArrayList; +import javax.swing.tree.DefaultMutableTreeNode; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -15,11 +16,13 @@ import org.w3c.dom.NodeList; import org.xml.sax.SAXException; +import com.mt.minilauncher.ChannelObject; import com.mt.minilauncher.VersionObject; public class XMLConverter { - public static VersionObject[] fromXML(String filePath) throws ParserConfigurationException, SAXException, IOException { - ArrayList tempList = new ArrayList<>(); + + public static DefaultMutableTreeNode fromXML(String filePath) throws ParserConfigurationException, SAXException, IOException { + DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode("Games"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); @@ -36,7 +39,8 @@ public static VersionObject[] fromXML(String filePath) throws ParserConfiguratio if(gameNode.getNodeType() == Node.ELEMENT_NODE) { Element gameElementNode = (Element) gameNode; String gameName = gameElementNode.getAttribute("name"); - + DefaultMutableTreeNode gameTreeNode = new DefaultMutableTreeNode(gameName); + rootTreeNode.add(gameTreeNode); NodeList versionList = gameElementNode.getElementsByTagName("version"); for (int j = 0; j < versionList.getLength(); j++) { Node versionNode = versionList.item(j); @@ -44,11 +48,13 @@ public static VersionObject[] fromXML(String filePath) throws ParserConfiguratio Element versionElementNode = (Element) versionNode; String versionNumber = versionElementNode.getAttribute("number"); String versionDownloadURL = (versionElementNode.getTextContent().startsWith("http://") || versionElementNode.getTextContent().startsWith("https://")) ? versionElementNode.getTextContent() : ""; - tempList.add(new VersionObject(versionDownloadURL, gameName + " " + versionNumber)); + DefaultMutableTreeNode versionTreeNode = new DefaultMutableTreeNode(new VersionObject(versionDownloadURL, versionNumber)); + gameTreeNode.add(versionTreeNode); } } } } - return tempList.toArray(new VersionObject[tempList.size()]); + + return rootTreeNode; } } diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/windows/ChannelSelector.java b/MiniCraftLauncher/src/com/mt/minilauncher/windows/ChannelSelector.java new file mode 100644 index 0000000..8797432 --- /dev/null +++ b/MiniCraftLauncher/src/com/mt/minilauncher/windows/ChannelSelector.java @@ -0,0 +1,150 @@ +package com.mt.minilauncher.windows; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.stream.Stream; + +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import javax.xml.parsers.ParserConfigurationException; + +import org.xml.sax.SAXException; + +import com.mt.minilauncher.ChannelObject; +import com.mt.minilauncher.Initializer; +import com.mt.minilauncher.VersionObject; +import com.mt.minilauncher.downloader.Downloader; +import com.mt.minilauncher.util.OrderedProperties; +import com.mt.minilauncher.util.Util; +import com.mt.minilauncher.util.XMLConverter; + +import javax.swing.JScrollPane; +import javax.swing.JList; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; + +public class ChannelSelector extends JDialog { + + /** + * + */ + private static final long serialVersionUID = 1L; + private final JPanel contentPanel = new JPanel(); + private JList list; + private JButton okButton; + private JButton cancelButton; + private JButton refreshListBtn; + + /** + * Launch the application. + */ + public static void main(String[] args) { + try { + ChannelSelector dialog = new ChannelSelector(); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Create the dialog. + */ + public ChannelSelector() { + setTitle("Channel Selector"); + setBounds(100, 100, 450, 300); + getContentPane().setLayout(new BorderLayout()); + contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + getContentPane().add(contentPanel, BorderLayout.CENTER); + contentPanel.setLayout(new BorderLayout(0, 0)); + { + JScrollPane scrollPane = new JScrollPane(); + contentPanel.add(scrollPane, BorderLayout.CENTER); + { + list = new JList(); + scrollPane.setViewportView(list); + } + } + { + JPanel buttonPane = new JPanel(); + buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); + getContentPane().add(buttonPane, BorderLayout.SOUTH); + { + refreshListBtn = new JButton("Refresh List"); + refreshListBtn.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + list.updateUI(); + } + }); + buttonPane.add(refreshListBtn); + } + { + okButton = new JButton("OK"); + okButton.setActionCommand("OK"); + buttonPane.add(okButton); + getRootPane().setDefaultButton(okButton); + } + { + cancelButton = new JButton("Cancel"); + cancelButton.setActionCommand("Cancel"); + buttonPane.add(cancelButton); + } + } + init(); + } + + private void init() { + try { + Util.downloadUsingNIO("https://github.com/MajickTek/MiniCraftLauncherIndex/raw/main/index.txt", Paths.get(Initializer.indexPath.toString(), "index.txt").toString()); + buildList(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + } + + private void buildList() { + Path indexPath = Paths.get(Initializer.indexPath.toString(), "index.txt"); + DefaultListModel model = new DefaultListModel<>(); + + OrderedProperties op = new OrderedProperties(); + try { + op.load(new FileInputStream(indexPath.toString())); + op.entrySet().forEach(l -> { + model.add(Integer.parseInt(l.getKey().toString()), new ChannelObject(l.getValue().toString())); + }); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + list.setModel(model); + list.updateUI(); + } + + public JList getList() { + return list; + } + public JButton getOkButton() { + return okButton; + } + public JButton getCancelButton() { + return cancelButton; + } +} diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/windows/ReferenceApp.java b/MiniCraftLauncher/src/com/mt/minilauncher/windows/ReferenceApp.java deleted file mode 100644 index f146c9e..0000000 --- a/MiniCraftLauncher/src/com/mt/minilauncher/windows/ReferenceApp.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.mt.minilauncher.windows; - -import java.awt.BorderLayout; -import java.awt.EventQueue; -import java.io.IOException; -import java.net.URISyntaxException; - -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.border.EmptyBorder; -import javax.swing.event.HyperlinkEvent; -import javax.swing.JEditorPane; -import javax.swing.JScrollPane; -import javax.swing.JSplitPane; - -public class ReferenceApp extends JFrame { - - private JPanel contentPane; - - /** - * Launch the application. - */ - public static void main(String[] args) { - EventQueue.invokeLater(new Runnable() { - public void run() { - try { - ReferenceApp frame = new ReferenceApp(); - frame.setVisible(true); - } catch (Exception e) { - e.printStackTrace(); - } - } - }); - } - - /** - * Create the frame. - */ - public ReferenceApp() { - setTitle("Reference"); - setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - setBounds(100, 100, 800, 600); - contentPane = new JPanel(); - contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); - contentPane.setLayout(new BorderLayout(0, 0)); - setContentPane(contentPane); - try { - - JSplitPane splitPane = new JSplitPane(); - splitPane.setResizeWeight(0.5); - contentPane.add(splitPane, BorderLayout.CENTER); - - JScrollPane scrollPane = new JScrollPane(); - splitPane.setRightComponent(scrollPane); - - JEditorPane htmlPane = new JEditorPane(); - scrollPane.setViewportView(htmlPane); - htmlPane.setEditable(false); - htmlPane.setContentType("text/html"); - htmlPane.setPage(ReferenceApp.class.getResource("/docs/index.html").toURI().toURL()); - - JScrollPane scrollPane_1 = new JScrollPane(); - splitPane.setLeftComponent(scrollPane_1); - - JEditorPane tableOfContentsPane = new JEditorPane(); - tableOfContentsPane.setEditable(false); - tableOfContentsPane.setContentType("text/html"); - scrollPane_1.setViewportView(tableOfContentsPane); - htmlPane.addHyperlinkListener(l -> { - if(l.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { - try { - htmlPane.setPage(l.getURL()); - System.out.println("Loading page: " + l.getURL()); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - }); - tableOfContentsPane.setPage(ReferenceApp.class.getResource("/docs/contents.html").toURI().toURL()); - tableOfContentsPane.addHyperlinkListener(l -> { - if(l.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { - try { - htmlPane.setPage(l.getURL());//the table of contents should open a link in the main view - System.out.println("Loading page: " + l.getURL()); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - }); - } catch (IOException | URISyntaxException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - -}