Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
Argent77 committed Sep 1, 2020
2 parents 72b7fdf + 08e2f79 commit 09a928c
Show file tree
Hide file tree
Showing 70 changed files with 2,699 additions and 343 deletions.
172 changes: 158 additions & 14 deletions src/org/infinity/NearInfinity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Near Infinity - An Infinity Engine Browser and Editor
// Copyright (C) 2001 - 2018 Jon Olav Hauglid
// Copyright (C) 2001 - 2020 Jon Olav Hauglid
// See LICENSE.txt for license information

package org.infinity;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Image;
Expand Down Expand Up @@ -41,11 +43,14 @@
import java.util.prefs.Preferences;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
Expand All @@ -62,6 +67,7 @@
import org.infinity.gui.BrowserMenuBar;
import org.infinity.gui.ButtonPopupWindow;
import org.infinity.gui.ChildFrame;
import org.infinity.gui.DataMenuItem;
import org.infinity.gui.InfinityTextArea;
import org.infinity.gui.OpenFileFrame;
import org.infinity.gui.PopupWindowEvent;
Expand Down Expand Up @@ -101,6 +107,7 @@
import org.infinity.util.StringTable;
import org.infinity.util.Table2daCache;
import org.infinity.util.io.DlcManager;
import org.infinity.util.io.FileEx;
import org.infinity.util.io.FileManager;

public final class NearInfinity extends JFrame implements ActionListener, ViewableContainer
Expand Down Expand Up @@ -136,6 +143,8 @@ public final class NearInfinity extends JFrame implements ActionListener, Viewab

private Viewable viewable;
private ButtonPopupWindow bpwQuickSearch;
private JButton btnLaunchGame;
private JPopupMenu launchMenu;
private int tablePanelHeight;
private ProgressMonitor pmProgress;
private int progressIndex, globalFontSize;
Expand Down Expand Up @@ -247,10 +256,10 @@ public static void main(String args[])
} else {
// Override game folder via application parameter
Path f = FileManager.resolve(args[idx]);
if (Files.isRegularFile(f)) {
if (FileEx.create(f).isFile()) {
f = f.getParent();
}
if (Files.isDirectory(f)) {
if (FileEx.create(f).isDirectory()) {
gameOverride = f;
break;
}
Expand Down Expand Up @@ -304,17 +313,17 @@ private NearInfinity(Path gameOverride, Profile.Game forcedGame)
setJMenuBar(menu);

final String lastDir;
if (gameOverride != null && Files.isDirectory(gameOverride)) {
if (gameOverride != null && FileEx.create(gameOverride).isDirectory()) {
lastDir = gameOverride.toString();
} else {
lastDir = prefs.get(LAST_GAMEDIR, null);
}

final Path keyFile;
Path path;
if (Files.isRegularFile(path = FileManager.resolve(KEYFILENAME))) {
if (FileEx.create(path = FileManager.resolve(KEYFILENAME)).isFile()) {
keyFile = path;
} else if (lastDir != null && Files.isRegularFile(path = FileManager.resolve(lastDir, KEYFILENAME))) {
} else if (lastDir != null && FileEx.create(path = FileManager.resolve(lastDir, KEYFILENAME)).isFile()) {
keyFile = path;
} else {
keyFile = findKeyfile();
Expand Down Expand Up @@ -355,11 +364,7 @@ protected Void doInBackground() throws Exception
@Override
public void windowClosing(WindowEvent event)
{
if (removeViewable()) {
storePreferences();
ChildFrame.closeWindows();
System.exit(0);
}
quit();
}
});
try {
Expand All @@ -381,6 +386,7 @@ public void windowClosing(WindowEvent event)
tree.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));

JToolBar toolBar = new JToolBar("Navigation", JToolBar.HORIZONTAL);
toolBar.setMargin(new Insets(4, 4, 4, 4));
JButton b;
toolBar.setRollover(true);
toolBar.setFloatable(false);
Expand Down Expand Up @@ -449,6 +455,16 @@ public void run()
}
});

toolBar.add(Box.createHorizontalGlue());
btnLaunchGame = new JButton(Icons.getIcon(Icons.ICON_LAUNCH_24));
btnLaunchGame.setFocusable(false);
btnLaunchGame.setEnabled(false);
btnLaunchGame.setMargin(new Insets(0, 0, 0, 0));
btnLaunchGame.setToolTipText("Launch game");
btnLaunchGame.addActionListener(this);
toolBar.add(btnLaunchGame);
launchMenu = new JPopupMenu();

JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.add(tree, BorderLayout.CENTER);
leftPanel.add(toolBar, BorderLayout.NORTH);
Expand All @@ -465,6 +481,7 @@ public void run()
hideProgress();
}

updateLauncher();
setSize(prefs.getInt(WINDOW_SIZEX, 930), prefs.getInt(WINDOW_SIZEY, 700));
int centerX = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() - getSize().width >> 1;
int centerY = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() - getSize().height >> 1;
Expand Down Expand Up @@ -613,6 +630,40 @@ public void actionPerformed(ActionEvent event)
} finally {
WindowBlocker.blockWindow(this, false);
}
} else if (event.getSource() == btnLaunchGame) {
//Path launchPath = null;
DataMenuItem dmi = null;
boolean ctrl = (event.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0;
if (ctrl || launchMenu.getComponentCount() == 1) {
// getting first available binary path
for (int i = 0, cnt = launchMenu.getComponentCount(); i < cnt; i++) {
if (launchMenu.getComponent(i) instanceof DataMenuItem) {
dmi = (DataMenuItem)launchMenu.getComponent(i);
break;
}
}
}

if ((launchMenu.getComponentCount() > 1 && ctrl) || launchMenu.getComponentCount() == 1) {
if (dmi != null) {
dmi.doClick();
} else {
JOptionPane.showMessageDialog(this, "Could not determine game executable.",
"Launch game", JOptionPane.ERROR_MESSAGE);
}
} else if (launchMenu.getComponentCount() > 1) {
launchMenu.show(btnLaunchGame, 0, btnLaunchGame.getHeight());
}
} else if (event.getSource() instanceof DataMenuItem &&
((DataMenuItem)event.getSource()).getParent() == launchMenu) {
DataMenuItem dmi = (DataMenuItem)event.getSource();
if (dmi.getData() instanceof Path) {
Path path = (Path)dmi.getData();
if (!launchGameBinary(path)) {
JOptionPane.showMessageDialog(this, "Game executable could not be launched.",
"Launch game", JOptionPane.ERROR_MESSAGE);
}
}
}
}

Expand Down Expand Up @@ -685,6 +736,7 @@ public void openGame(Path keyFile)
removeViewable();
ResourceTreeModel treemodel = ResourceFactory.getResourceTreeModel();
updateWindowTitle();
updateLauncher();
final String msg = String.format(STATUSBAR_TEXT_FMT,
Profile.getProperty(Profile.Key.GET_GAME_TITLE),
Profile.getGameRoot(), treemodel.size());
Expand Down Expand Up @@ -741,6 +793,7 @@ public void refreshGame()
if (removeViewable()) {
ChildFrame.closeWindows();
ResourceTreeModel treemodel = ResourceFactory.getResourceTreeModel();
updateLauncher();
final String msg = String.format(STATUSBAR_TEXT_FMT,
Profile.getProperty(Profile.Key.GET_GAME_TITLE),
Profile.getGameRoot(), treemodel.size());
Expand Down Expand Up @@ -774,7 +827,7 @@ public boolean editGameIni(Component parent)
boolean retVal = false;
Path iniFile = Profile.getProperty(Profile.Key.GET_GAME_INI_FILE);
try {
if (iniFile != null && Files.isRegularFile(iniFile)) {
if (iniFile != null && FileEx.create(iniFile).isFile()) {
new ViewFrame(parent, new PlainTextResource(new FileResourceEntry(iniFile)));
} else {
throw new Exception();
Expand Down Expand Up @@ -851,6 +904,74 @@ public int getGlobalFontSize()
return globalFontSize;
}

/** Updates the launcher button configuration. */
public void updateLauncher()
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
// cleaning up old configuration
for (int idx = launchMenu.getComponentCount() - 1; idx >= 0; idx--) {
Component c = launchMenu.getComponent(idx);
if (c instanceof JMenuItem) {
((JMenuItem)c).removeActionListener(NearInfinity.this);
}
}
launchMenu.removeAll();

// setting up new configuration
BrowserMenuBar.Bookmark bookmark = BrowserMenuBar.getInstance().getBookmarkOf(Profile.getChitinKey());
List<Path> binPaths = null;
if (bookmark != null) {
List<String> list = bookmark.getBinaryPaths(Platform.getPlatform());
if (list != null && !list.isEmpty()) {
binPaths = new ArrayList<>();
for (final String name : list) {
Path path = null;
if (name.startsWith("/")) {
path = FileManager.resolveExisting(name);
if (path == null)
path = FileManager.resolveExisting(Profile.getGameRoot().toString(), name);
} else {
path = FileManager.resolveExisting(Profile.getGameRoot().toString(), name);
}
if (path != null)
binPaths.add(path);
}
}
}
if (binPaths == null || binPaths.isEmpty())
binPaths = Profile.getGameBinaryPaths();
if (binPaths != null && binPaths.isEmpty())
binPaths = null;

// updating launch controls
if (binPaths != null) {
for (final Path path : binPaths) {
DataMenuItem dmi = new DataMenuItem(path.toString());
dmi.setData(path);
dmi.addActionListener(NearInfinity.this);
launchMenu.add(dmi);
}
}
boolean isEnabled = (binPaths != null) && BrowserMenuBar.getInstance().getLauncherEnabled();
btnLaunchGame.setEnabled(isEnabled);
if (binPaths == null ) {
btnLaunchGame.setIcon(Icons.getIcon(Icons.ICON_LAUNCH_24));
btnLaunchGame.setToolTipText("Launch game");
} else if (binPaths.size() == 1) {
btnLaunchGame.setIcon(Icons.getIcon(Icons.ICON_LAUNCH_24));
btnLaunchGame.setToolTipText("Launch " + binPaths.get(0).toString());
} else {
String ctrlName = (Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() == Event.CTRL_MASK) ? "Ctrl" : "Command";
btnLaunchGame.setIcon(Icons.getIcon(Icons.ICON_LAUNCH_PLUS_24));
btnLaunchGame.setToolTipText("Launch game (launch directly with " + ctrlName + "+Click)");
}
}
});
}

private static boolean reloadFactory(boolean refreshOnly)
{
boolean retVal = false;
Expand Down Expand Up @@ -1038,6 +1159,29 @@ private void enableOSXQuitStrategy()
}
}

// Executes the specified path
private boolean launchGameBinary(Path binPath)
{
boolean retVal = false;
if (binPath != null && Files.exists(binPath)) {
try {
if (Platform.IS_MACOS && binPath.toString().toLowerCase(Locale.ENGLISH).endsWith(".app")) {
// This method may be required for launching Mac App Bundles
Desktop.getDesktop().open(binPath.toFile());
} else {
ProcessBuilder pb = new ProcessBuilder(binPath.toString());
pb.directory(binPath.getParent().toFile());
pb.inheritIO();
pb.start();
}
retVal = true;
} catch (Exception e) {
e.printStackTrace();
}
}
return retVal;
}

// -------------------------- INNER CLASSES --------------------------

private static final class ConsoleStream extends PrintStream
Expand Down Expand Up @@ -1109,7 +1253,7 @@ public void drop(DropTargetDropEvent event)
event.dropComplete(true);
if (files != null && files.size() == 1) {
Path path = files.get(0).toPath();
if (path != null && Files.isRegularFile(path) &&
if (path != null && FileEx.create(path).isFile() &&
path.getFileName().toString().toUpperCase(Locale.ENGLISH).endsWith(".KEY")) {
Path curFile = Profile.getChitinKey();
if (!path.equals(curFile)) {
Expand All @@ -1133,7 +1277,7 @@ public void run()
if (files != null) {
files.forEach((file) -> {
Path path = file.toPath();
if (Files.isRegularFile(path)) {
if (FileEx.create(path).isFile()) {
OpenFileFrame.openExternalFile(NearInfinity.getInstance(), path);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/check/StringUseChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public Object getObjectAt(int columnIndex)
@Override
public String toString()
{
return "StringRef: " + strRef + " /* " + string.replaceAll("\r\n", Misc.LINE_SEPARATOR) + " */";
return "StringRef: " + strRef + " /* " + string.replace("\r\n", Misc.LINE_SEPARATOR) + " */";
}
}
}
4 changes: 2 additions & 2 deletions src/org/infinity/datatype/ResourceBitmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,13 @@ public String toString()
@Override
public boolean equals(Object o)
{
return desc.equalsIgnoreCase(o.toString());
return desc.equalsIgnoreCase(Misc.safeToString(o));
}

@Override
public int compareTo(RefEntry o)
{
return desc.compareToIgnoreCase(o.toString());
return desc.compareToIgnoreCase(Misc.safeToString(o));
}

public boolean isResource() { return (entry != null); }
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/datatype/Song2daBitmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private static List<RefEntry> createSongList_MUSIC(List<Path> searchDirs)
String name = e.getSymbol();
long key = e.getID();
name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
String ref = name.replaceAll("_", "").toUpperCase(Locale.ENGLISH) + ".MUS";
String ref = name.replace("_", "").toUpperCase(Locale.ENGLISH) + ".MUS";
if (key == 0L) {
ref = name;
}
Expand Down
6 changes: 3 additions & 3 deletions src/org/infinity/datatype/TextBitmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public TextBitmap(ByteBuffer buffer, int offset, int length, String name, Map<St
this.ids = new String[items.size()];
this.names = new String[this.ids.length];
int idx = 0;
for (final String key: items.keySet()) {
this.ids[idx] = key;
this.names[idx] = items.get(key);
for (final Map.Entry<String, String> entry : items.entrySet()) {
this.ids[idx] = entry.getKey();
this.names[idx] = entry.getValue();
idx++;
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/org/infinity/gui/BIFFEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.infinity.resource.key.BIFFWriter;
import org.infinity.resource.key.FileResourceEntry;
import org.infinity.resource.key.ResourceEntry;
import org.infinity.util.io.FileEx;
import org.infinity.util.io.FileManager;
import org.infinity.util.io.StreamUtils;

Expand Down Expand Up @@ -166,7 +167,7 @@ public void run()
for (final ResourceEntry entry : tobif) {
Path file = FileManager.query(Profile.getRootFolders(), Profile.getOverrideFolderName(),
entry.getResourceName());
if (file != null && Files.isRegularFile(file)) {
if (file != null && FileEx.create(file).isFile()) {
try {
Files.delete(file);
} catch (IOException e) {
Expand Down
Loading

0 comments on commit 09a928c

Please sign in to comment.