This commit is contained in:
ThatGamerBlue
2021-02-04 07:16:17 +00:00
parent 58e1150d51
commit 4023066b5d
29 changed files with 329 additions and 555 deletions

View File

@@ -0,0 +1,135 @@
/*
* Copyright (c) 2021, ThatGamerBlue <thatgamerblue@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import net.runelite.client.util.CallableExceptionLogger;
import static net.runelite.client.util.RunnableExceptionLogger.wrap;
import org.jetbrains.annotations.NotNull;
// Awkward name because plugins already referenced the ExecutorServiceExceptionLogger
// (which only handles ScheduledExecutorServices) before this class was introduced
public class NonScheduledExecutorServiceExceptionLogger implements ExecutorService
{
private final ExecutorService service;
public NonScheduledExecutorServiceExceptionLogger(ExecutorService service)
{
this.service = service;
}
@Override
public void shutdown()
{
service.shutdown();
}
@NotNull
@Override
public List<Runnable> shutdownNow()
{
return service.shutdownNow();
}
@Override
public boolean isShutdown()
{
return service.isShutdown();
}
@Override
public boolean isTerminated()
{
return service.isTerminated();
}
@Override
public boolean awaitTermination(long timeout, @NotNull TimeUnit unit) throws InterruptedException
{
return service.awaitTermination(timeout, unit);
}
@Override
public void execute(@NotNull Runnable command)
{
service.execute(wrap(command));
}
@NotNull
@Override
public <T> Future<T> submit(@NotNull Callable<T> task)
{
return service.submit(CallableExceptionLogger.wrap(task));
}
@NotNull
@Override
public <T> Future<T> submit(@NotNull Runnable task, T result)
{
return service.submit(wrap(task), result);
}
@NotNull
@Override
public Future<?> submit(@NotNull Runnable task)
{
return service.submit(wrap(task));
}
@NotNull
@Override
public <T> List<Future<T>> invokeAll(@NotNull Collection<? extends Callable<T>> tasks) throws InterruptedException
{
return service.invokeAll(tasks);
}
@NotNull
@Override
public <T> List<Future<T>> invokeAll(@NotNull Collection<? extends Callable<T>> tasks, long timeout, @NotNull TimeUnit unit) throws InterruptedException
{
return service.invokeAll(tasks, timeout, unit);
}
@NotNull
@Override
public <T> T invokeAny(@NotNull Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
{
return service.invokeAny(tasks);
}
@Override
public <T> T invokeAny(@NotNull Collection<? extends Callable<T>> tasks, long timeout, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
{
return service.invokeAny(tasks, timeout, unit);
}
}

View File

@@ -30,7 +30,6 @@ import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.name.Names;
import com.openosrs.client.config.OpenOSRSConfig;
import com.openosrs.client.util.NonScheduledExecutorServiceExceptionLogger;
import java.applet.Applet;
import java.io.File;
import java.util.Properties;

View File

@@ -40,11 +40,11 @@ import com.openosrs.client.events.ExternalPluginChanged;
import com.openosrs.client.events.ExternalRepositoryChanged;
import com.openosrs.client.ui.OpenOSRSSplashScreen;
import com.openosrs.client.util.Groups;
import com.openosrs.client.util.MiscUtils;
import com.openosrs.client.util.SwingUtil;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
@@ -76,6 +76,7 @@ import net.runelite.client.config.RuneLiteConfig;
import net.runelite.client.eventbus.EventBus;
import net.runelite.client.events.ConfigChanged;
import net.runelite.client.ui.ClientUI;
import net.runelite.client.util.SwingUtil;
import org.jgroups.Message;
import org.pf4j.DefaultPluginManager;
import org.pf4j.DependencyResolver;
@@ -343,7 +344,7 @@ public class OPRSExternalPluginManager
{
config.append(repository.getId());
config.append("|");
config.append(MiscUtils.urlToStringEncoded(repository.getUrl()));
config.append(urlToStringEncoded(repository.getUrl()));
config.append(";");
}
config.deleteCharAt(config.lastIndexOf(";"));
@@ -1066,4 +1067,27 @@ public class OPRSExternalPluginManager
}
}
/**
* Mostly stolen from {@link java.net.URLStreamHandler#toExternalForm(URL)}
*
* @param url URL to encode
* @return URL, with path, query and ref encoded
*/
private static String urlToStringEncoded(URL url)
{
String s;
String path = url.getPath() != null ? Stream.of(url.getPath().split("/"))
.map(s2 -> URLEncoder.encode(s2, StandardCharsets.UTF_8)).collect(Collectors.joining("/")) : "";
return url.getProtocol()
+ ':'
+ (((s = url.getAuthority()) != null && s.length() > 0) ? "//" + s : "")
+ (path)
+ (((s = url.getQuery()) != null) ? '?' + urlEncode(s) : "")
+ (((s = url.getRef()) != null) ? '#' + urlEncode(s) : "");
}
private static String urlEncode(String s)
{
return URLEncoder.encode(s, StandardCharsets.UTF_8);
}
}

View File

@@ -87,7 +87,6 @@ public class PluginManager
* Base package where the core plugins are
*/
private static final String PLUGIN_PACKAGE = "net.runelite.client.plugins";
private static final String OPENOSRS_PACKAGE = "com.openosrs.client.plugins";
private final boolean developerMode;
private final boolean safeMode;
@@ -291,10 +290,6 @@ public class PluginManager
.map(ClassInfo::load)
.collect(Collectors.toList());
plugins.addAll(classPath.getTopLevelClassesRecursive(OPENOSRS_PACKAGE).stream()
.map(ClassInfo::load)
.collect(Collectors.toList()));
loadPlugins(plugins, (loaded, total) ->
SplashScreen.stage(.60, .70, null, "Loading Plugins", loaded, total, false));
}

View File

@@ -0,0 +1,131 @@
/*
*
* Copyright (c) 2019, Zeruth <TheRealNull@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.runelite.client.plugins.openosrs;
import ch.qos.logback.classic.Logger;
import com.openosrs.client.config.OpenOSRSConfig;
import net.runelite.client.plugins.openosrs.externals.ExternalPluginManagerPanel;
import java.awt.image.BufferedImage;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.client.config.Keybind;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ConfigChanged;
import net.runelite.client.input.KeyManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.ui.ClientToolbar;
import net.runelite.client.ui.NavigationButton;
import net.runelite.client.util.HotkeyListener;
import net.runelite.client.util.ImageUtil;
import org.slf4j.LoggerFactory;
@PluginDescriptor(
loadWhenOutdated = true, // prevent users from disabling
hidden = true, // prevent users from disabling
name = "OpenOSRS"
)
@Singleton
@Slf4j
public class OpenOSRSPlugin extends Plugin
{
@Inject
private OpenOSRSConfig config;
@Inject
private KeyManager keyManager;
@Inject
private Client client;
@Inject
private ClientToolbar clientToolbar;
private NavigationButton navButton;
private final HotkeyListener hotkeyListener = new HotkeyListener(() -> this.keybind)
{
@Override
public void hotkeyPressed()
{
if (client == null)
{
return;
}
detach = !detach;
client.setOculusOrbState(detach ? 1 : 0);
client.setOculusOrbNormalSpeed(detach ? 36 : 12);
}
};
private boolean detach;
private Keybind keybind;
@Override
protected void startUp()
{
ExternalPluginManagerPanel panel = injector.getInstance(ExternalPluginManagerPanel.class);
final BufferedImage icon = ImageUtil.getResourceStreamFromClass(getClass(), "externalmanager_icon.png");
navButton = NavigationButton.builder()
.tooltip("External Plugin Manager")
.icon(icon)
.priority(1)
.panel(panel)
.build();
clientToolbar.addNavigation(navButton);
this.keybind = config.detachHotkey();
keyManager.registerKeyListener(hotkeyListener);
}
@Override
protected void shutDown()
{
clientToolbar.removeNavigation(navButton);
}
@Subscribe
private void onConfigChanged(ConfigChanged event)
{
if (!event.getGroup().equals("openosrs"))
{
return;
}
this.keybind = config.detachHotkey();
if (event.getKey().equals("shareLogs") && !config.shareLogs())
{
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
logger.detachAppender("Sentry");
}
}
}

View File

@@ -0,0 +1,97 @@
package net.runelite.client.plugins.openosrs.externals;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultCaret;
import net.runelite.client.ui.ColorScheme;
import net.runelite.client.ui.FontManager;
import org.pf4j.update.PluginInfo;
public class ExternalBox extends JPanel
{
private static final Font normalFont = FontManager.getRunescapeFont();
private static final Font smallFont = FontManager.getRunescapeSmallFont();
PluginInfo pluginInfo;
JLabel install = new JLabel();
JMultilineLabel description = new JMultilineLabel();
ExternalBox(String name, URL url)
{
this(name, url.toString().replace("https://raw.githubusercontent.com/", "").replace("/master/", ""));
}
ExternalBox(PluginInfo pluginInfo)
{
this(pluginInfo.name, pluginInfo.description);
}
ExternalBox(String name, String desc)
{
setLayout(new BorderLayout());
setBackground(ColorScheme.DARKER_GRAY_COLOR);
JPanel titleWrapper = new JPanel(new BorderLayout());
titleWrapper.setBackground(ColorScheme.DARKER_GRAY_COLOR);
titleWrapper.setBorder(new CompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 0, ColorScheme.DARK_GRAY_COLOR),
BorderFactory.createLineBorder(ColorScheme.DARKER_GRAY_COLOR)
));
JLabel title = new JLabel();
title.setText(name);
title.setFont(normalFont);
title.setBorder(null);
title.setBackground(ColorScheme.DARKER_GRAY_COLOR);
title.setPreferredSize(new Dimension(0, 24));
title.setForeground(Color.WHITE);
title.setBorder(new EmptyBorder(0, 8, 0, 0));
JPanel titleActions = new JPanel(new BorderLayout(3, 0));
titleActions.setBorder(new EmptyBorder(0, 0, 0, 8));
titleActions.setBackground(ColorScheme.DARKER_GRAY_COLOR);
titleActions.add(install, BorderLayout.EAST);
titleWrapper.add(title, BorderLayout.CENTER);
titleWrapper.add(titleActions, BorderLayout.EAST);
description.setText(desc);
description.setFont(smallFont);
description.setDisabledTextColor(Color.WHITE);
description.setBackground(ColorScheme.DARKER_GRAY_COLOR);
add(titleWrapper, BorderLayout.NORTH);
add(description, BorderLayout.CENTER);
}
public static class JMultilineLabel extends JTextArea
{
private static final long serialVersionUID = 1L;
public JMultilineLabel()
{
super();
setEditable(false);
setCursor(null);
setOpaque(false);
setFocusable(false);
setWrapStyleWord(true);
setLineWrap(true);
setBorder(new EmptyBorder(0, 8, 0, 8));
setAlignmentY(JLabel.CENTER_ALIGNMENT);
DefaultCaret caret = (DefaultCaret) getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
}
}
}

View File

@@ -0,0 +1,294 @@
package net.runelite.client.plugins.openosrs.externals;
import net.runelite.client.plugins.OPRSExternalPluginManager;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ScheduledExecutorService;
import javax.inject.Inject;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import net.runelite.client.eventbus.EventBus;
import net.runelite.client.ui.ClientUI;
import net.runelite.client.ui.ColorScheme;
import net.runelite.client.ui.PluginPanel;
import net.runelite.client.util.ImageUtil;
public class ExternalPluginManagerPanel extends PluginPanel
{
private static final ImageIcon ADD_ICON_RAW;
private static final ImageIcon ADD_HOVER_ICON_RAW;
private static final ImageIcon ADD_ICON_GH;
private static final ImageIcon ADD_HOVER_ICON_GH;
static
{
final BufferedImage addIconRaw =
ImageUtil.getResourceStreamFromClass(ExternalPluginManagerPanel.class, "add_raw_icon.png");
final BufferedImage addIconGh = ImageUtil
.resizeImage(ImageUtil.getResourceStreamFromClass(ExternalPluginManagerPanel.class, "gh_icon.png"), 14, 14);
ADD_ICON_RAW = new ImageIcon(addIconRaw);
ADD_HOVER_ICON_RAW = new ImageIcon(ImageUtil.alphaOffset(addIconRaw, 0.53f));
ADD_ICON_GH = new ImageIcon(addIconGh);
ADD_HOVER_ICON_GH = new ImageIcon(ImageUtil.alphaOffset(addIconGh, 0.53f));
}
private final OPRSExternalPluginManager externalPluginManager;
private final ScheduledExecutorService executor;
private final EventBus eventBus;
@Inject
private ExternalPluginManagerPanel(OPRSExternalPluginManager externalPluginManager, ScheduledExecutorService executor, EventBus eventBus)
{
super(false);
this.externalPluginManager = externalPluginManager;
this.executor = executor;
this.eventBus = eventBus;
buildPanel();
}
private void buildPanel()
{
removeAll();
setLayout(new BorderLayout(0, 10));
setBackground(ColorScheme.DARK_GRAY_COLOR);
add(titleBar(), BorderLayout.NORTH);
add(tabbedPane(), BorderLayout.CENTER);
revalidate();
repaint();
}
private JPanel titleBar()
{
JPanel titlePanel = new JPanel(new BorderLayout());
titlePanel.setBorder(new EmptyBorder(10, 10, 10, 10));
JLabel title = new JLabel();
JLabel addGHRepo = new JLabel(ADD_ICON_GH);
JLabel addRawRepo = new JLabel(ADD_ICON_RAW);
JPanel buttonHolder = new JPanel(new BorderLayout());
buttonHolder.setBorder(new EmptyBorder(0, 0, 0, 0));
title.setText("External Plugin Manager");
title.setForeground(Color.WHITE);
addGHRepo.setToolTipText("Add new GitHub repository");
addGHRepo.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent mouseEvent)
{
if (externalPluginManager.getWarning())
{
JCheckBox checkbox = new JCheckBox("Don't show again.");
int answer = showWarningDialog(checkbox);
if (answer == 1)
{
return;
}
if (checkbox.isSelected())
{
externalPluginManager.setWarning(false);
}
}
JTextField owner = new JTextField();
JTextField name = new JTextField();
Object[] message = {
"Github Repository owner:", owner,
"Github Repository name:", name
};
int option =
JOptionPane.showConfirmDialog(ClientUI.getFrame(), message, "Add repository", JOptionPane.OK_CANCEL_OPTION);
if (option != JOptionPane.OK_OPTION || owner.getText().equals("") || name.getText().equals(""))
{
return;
}
if (externalPluginManager.doesGhRepoExist(owner.getText(), name.getText()))
{
JOptionPane.showMessageDialog(ClientUI.getFrame(), "This repository already exists.", "Error!",
JOptionPane.ERROR_MESSAGE);
return;
}
if (OPRSExternalPluginManager.testGHRepository(owner.getText(), name.getText()))
{
JOptionPane.showMessageDialog(ClientUI.getFrame(), "This doesn't appear to be a valid repository.", "Error!",
JOptionPane.ERROR_MESSAGE);
return;
}
externalPluginManager.addGHRepository(owner.getText(), name.getText());
}
@Override
public void mouseEntered(MouseEvent mouseEvent)
{
addGHRepo.setIcon(ADD_HOVER_ICON_GH);
}
@Override
public void mouseExited(MouseEvent mouseEvent)
{
addGHRepo.setIcon(ADD_ICON_GH);
}
});
addGHRepo.setBorder(new EmptyBorder(0, 3, 0, 0));
addRawRepo.setToolTipText("Add new raw repository");
addRawRepo.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent mouseEvent)
{
if (externalPluginManager.getWarning())
{
JCheckBox checkbox = new JCheckBox("Don't show again.");
int answer = showWarningDialog(checkbox);
if (answer == 1)
{
return;
}
if (checkbox.isSelected())
{
externalPluginManager.setWarning(false);
}
}
JTextField id = new JTextField();
JTextField url = new JTextField();
Object[] message = {
"Repository ID:", id,
"Repository URL:", url
};
int option =
JOptionPane.showConfirmDialog(ClientUI.getFrame(), message, "Add repository", JOptionPane.OK_CANCEL_OPTION);
if (option != JOptionPane.OK_OPTION || id.getText().equals("") || url.getText().equals(""))
{
return;
}
if (id.getText().startsWith("gh:") || id.getText().contains("|"))
{
JOptionPane.showMessageDialog(ClientUI.getFrame(),
"Repository id cannot begin with \"gh:\"\nor contain the pipe character '|'.", "Error!",
JOptionPane.ERROR_MESSAGE);
return;
}
if (externalPluginManager.doesRepoExist(id.getText()))
{
JOptionPane.showMessageDialog(ClientUI.getFrame(),
String.format("The repository with id %s already exists.", id.getText()), "Error!",
JOptionPane.ERROR_MESSAGE);
return;
}
URL urlActual;
try
{
urlActual = new URL(url.getText());
}
catch (MalformedURLException e)
{
JOptionPane.showMessageDialog(ClientUI.getFrame(), "This doesn't appear to be a valid repository.", "Error!",
JOptionPane.ERROR_MESSAGE);
return;
}
if (OPRSExternalPluginManager.testRepository(urlActual))
{
JOptionPane.showMessageDialog(ClientUI.getFrame(), "This doesn't appear to be a valid repository.", "Error!",
JOptionPane.ERROR_MESSAGE);
return;
}
externalPluginManager.addRepository(id.getText(), urlActual);
}
@Override
public void mouseEntered(MouseEvent mouseEvent)
{
addRawRepo.setIcon(ADD_HOVER_ICON_RAW);
}
@Override
public void mouseExited(MouseEvent mouseEvent)
{
addRawRepo.setIcon(ADD_ICON_RAW);
}
});
addRawRepo.setBorder(new EmptyBorder(0, 0, 0, 3));
titlePanel.add(title, BorderLayout.WEST);
buttonHolder.add(addRawRepo, BorderLayout.WEST);
buttonHolder.add(addGHRepo, BorderLayout.EAST);
titlePanel.add(buttonHolder, BorderLayout.EAST);
return titlePanel;
}
private JTabbedPane tabbedPane()
{
JTabbedPane mainTabPane = new JTabbedPane();
PluginsPanel pluginPanel = new PluginsPanel(this.externalPluginManager, this.executor, this.eventBus);
JScrollPane repositoryPanel = wrapContainer(new RepositoryPanel(this.externalPluginManager, this.eventBus));
mainTabPane.add("Plugins", pluginPanel);
mainTabPane.add("Repositories", repositoryPanel);
return mainTabPane;
}
private int showWarningDialog(JCheckBox checkbox)
{
Object[] options = {"Okay, I accept the risk", "Never mind, turn back", checkbox};
return JOptionPane.showOptionDialog(new JFrame(),
"Adding plugins from unverified sources may put your account, or personal information at risk! \n",
"Account security warning",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
options,
options[0]);
}
static JScrollPane wrapContainer(final JPanel container)
{
final JPanel wrapped = new JPanel(new BorderLayout());
wrapped.add(container, BorderLayout.NORTH);
final JScrollPane scroller = new JScrollPane(wrapped);
scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroller.getVerticalScrollBar().setPreferredSize(new Dimension(8, 0));
return scroller;
}
}

View File

@@ -0,0 +1,591 @@
package net.runelite.client.plugins.openosrs.externals;
import net.runelite.client.plugins.OPRSExternalPluginManager;
import com.google.gson.JsonSyntaxException;
import com.openosrs.client.events.ExternalPluginChanged;
import com.openosrs.client.events.ExternalRepositoryChanged;
import net.runelite.client.util.DeferredDocumentChangedListener;
import com.openosrs.client.util.SwingUtil;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;
import lombok.extern.slf4j.Slf4j;
import static net.runelite.api.util.Text.DISTANCE;
import net.runelite.client.eventbus.EventBus;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.ui.ClientUI;
import net.runelite.client.ui.ColorScheme;
import net.runelite.client.ui.FontManager;
import net.runelite.client.ui.PluginPanel;
import net.runelite.client.ui.components.IconTextField;
import net.runelite.client.ui.components.shadowlabel.JShadowedLabel;
import net.runelite.client.util.ImageUtil;
import org.pf4j.update.PluginInfo;
import org.pf4j.update.UpdateManager;
import org.pf4j.update.UpdateRepository;
import org.pf4j.update.VerifyException;
@Slf4j
public class PluginsPanel extends JPanel
{
private static final ImageIcon ADD_ICON;
private static final ImageIcon ADD_HOVER_ICON;
private static final ImageIcon DELETE_ICON;
private static final ImageIcon DELETE_HOVER_ICON;
private static final ImageIcon DELETE_ICON_GRAY;
private static final ImageIcon DELETE_HOVER_ICON_GRAY;
static
{
final BufferedImage addIcon =
ImageUtil.recolorImage(
ImageUtil.getResourceStreamFromClass(PluginsPanel.class, "add_icon.png"), ColorScheme.BRAND_BLUE
);
ADD_ICON = new ImageIcon(addIcon);
ADD_HOVER_ICON = new ImageIcon(ImageUtil.alphaOffset(addIcon, 0.53f));
final BufferedImage deleteImg =
ImageUtil.recolorImage(
ImageUtil.resizeCanvas(
ImageUtil.getResourceStreamFromClass(PluginsPanel.class, "delete_icon.png"), 14, 14
), ColorScheme.BRAND_BLUE
);
DELETE_ICON = new ImageIcon(deleteImg);
DELETE_HOVER_ICON = new ImageIcon(ImageUtil.alphaOffset(deleteImg, 0.53f));
DELETE_ICON_GRAY = new ImageIcon(ImageUtil.grayscaleImage(deleteImg));
DELETE_HOVER_ICON_GRAY = new ImageIcon(ImageUtil.alphaOffset(ImageUtil.grayscaleImage(deleteImg), 0.53f));
}
private final OPRSExternalPluginManager externalPluginManager;
private final UpdateManager updateManager;
private final ScheduledExecutorService executor;
private final EventBus eventBus;
private final IconTextField searchBar = new IconTextField();
private final JPanel filterwrapper = new JPanel(new BorderLayout(0, 10));
private final List<PluginInfo> installedPluginsList = new ArrayList<>();
private final List<PluginInfo> availablePluginsList = new ArrayList<>();
private final JPanel installedPluginsPanel = new JPanel(new GridBagLayout());
private final JPanel availablePluginsPanel = new JPanel(new GridBagLayout());
private JComboBox<String> filterComboBox;
private Set<String> deps;
PluginsPanel(OPRSExternalPluginManager externalPluginManager, ScheduledExecutorService executor, EventBus eventBus)
{
this.externalPluginManager = externalPluginManager;
this.updateManager = externalPluginManager.getUpdateManager();
this.executor = executor;
this.eventBus = eventBus;
setLayout(new BorderLayout(0, 10));
setBackground(ColorScheme.DARK_GRAY_COLOR);
buildFilter();
JTabbedPane mainTabPane = new JTabbedPane();
mainTabPane.add("Installed", ExternalPluginManagerPanel.wrapContainer(installedPluginsPanel()));
mainTabPane.add("Available", ExternalPluginManagerPanel.wrapContainer(availablePluginsPanel()));
add(filterwrapper, BorderLayout.NORTH);
add(mainTabPane, BorderLayout.CENTER);
eventBus.register(this);
reloadPlugins();
}
@Subscribe
public void onExternalRepositoryChanged(ExternalRepositoryChanged event)
{
buildFilter();
reloadPlugins();
repaint();
}
private void buildFilter()
{
filterwrapper.removeAll();
DeferredDocumentChangedListener listener = new DeferredDocumentChangedListener();
listener.addChangeListener(e ->
{
installedPlugins();
availablePlugins();
});
filterwrapper.setBorder(new EmptyBorder(10, 10, 0, 10));
List<String> repositories = getRepositories();
filterComboBox = new JComboBox<>(repositories.toArray(new String[0]));
filterComboBox.setPreferredSize(new Dimension(PluginPanel.PANEL_WIDTH - 20, 30));
filterComboBox.addActionListener(e -> {
installedPlugins();
availablePlugins();
});
if (repositories.size() > 2)
{
filterwrapper.add(filterComboBox, BorderLayout.NORTH);
}
searchBar.setIcon(IconTextField.Icon.SEARCH);
searchBar.setPreferredSize(new Dimension(PluginPanel.PANEL_WIDTH - 20, 30));
searchBar.setBackground(ColorScheme.DARKER_GRAY_COLOR);
searchBar.setHoverBackgroundColor(ColorScheme.DARK_GRAY_HOVER_COLOR);
searchBar.getDocument().addDocumentListener(listener);
filterwrapper.add(searchBar, BorderLayout.CENTER);
}
private List<String> getRepositories()
{
List<String> repositories = new ArrayList<>();
repositories.add("All");
for (UpdateRepository updateRepository : this.updateManager.getRepositories())
{
repositories.add(updateRepository.getUrl().toString().replace("https://raw.githubusercontent.com/", "").replace("/master/", ""));
}
return repositories;
}
private JLabel titleLabel(String text)
{
JLabel title = new JShadowedLabel();
title.setFont(FontManager.getRunescapeSmallFont());
title.setForeground(Color.WHITE);
title.setHorizontalAlignment(SwingConstants.CENTER);
title.setText("<html><body style = 'text-align:center'>" + text + "</body></html>");
return title;
}
private JPanel installedPluginsPanel()
{
JPanel installedPluginsContainer = new JPanel();
installedPluginsContainer.setLayout(new BorderLayout(0, 5));
installedPluginsContainer.setBorder(new EmptyBorder(0, 10, 10, 10));
installedPluginsContainer.add(installedPluginsPanel, BorderLayout.CENTER);
return installedPluginsContainer;
}
private JPanel availablePluginsPanel()
{
JPanel availablePluginsContainer = new JPanel();
availablePluginsContainer.setLayout(new BorderLayout(0, 5));
availablePluginsContainer.setBorder(new EmptyBorder(0, 10, 10, 10));
availablePluginsContainer.add(availablePluginsPanel, BorderLayout.CENTER);
return availablePluginsContainer;
}
static boolean mismatchesSearchTerms(String search, PluginInfo pluginInfo)
{
final String[] searchTerms = search.toLowerCase().split(" ");
final String[] pluginTerms = (pluginInfo.name + " " + pluginInfo.description).toLowerCase().split("[/\\s]");
for (String term : searchTerms)
{
if (Arrays.stream(pluginTerms).noneMatch((t) -> t.contains(term) ||
DISTANCE.apply(t, term) > 0.9))
{
return true;
}
}
return false;
}
private void reloadPlugins()
{
fetchPlugins();
try
{
SwingUtil.syncExec(() -> {
this.installedPlugins();
this.availablePlugins();
});
}
catch (InvocationTargetException | InterruptedException e)
{
e.printStackTrace();
}
}
private void fetchPlugins()
{
List<PluginInfo> availablePlugins = null;
List<PluginInfo> plugins = null;
List<String> disabledPlugins = externalPluginManager.getDisabledPlugins();
try
{
availablePlugins = updateManager.getAvailablePlugins();
plugins = updateManager.getPlugins();
}
catch (JsonSyntaxException ex)
{
log.error(String.valueOf(ex));
}
if (availablePlugins == null || plugins == null)
{
JOptionPane.showMessageDialog(ClientUI.getFrame(), "The external plugin list could not be loaded.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
availablePluginsList.clear();
installedPluginsList.clear();
deps = externalPluginManager.getDependencies();
for (PluginInfo pluginInfo : plugins)
{
if (availablePlugins.contains(pluginInfo) || disabledPlugins.contains(pluginInfo.id))
{
availablePluginsList.add(pluginInfo);
}
else
{
installedPluginsList.add(pluginInfo);
}
}
}
@Subscribe
private void onExternalPluginChanged(ExternalPluginChanged externalPluginChanged)
{
String pluginId = externalPluginChanged.getPluginId();
Optional<Component> externalBox;
if (externalPluginChanged.isAdded())
{
externalBox = Arrays.stream(
availablePluginsPanel.getComponents()
).filter(extBox ->
extBox instanceof ExternalBox && ((ExternalBox) extBox).pluginInfo.id.equals(pluginId)
).findFirst();
}
else
{
externalBox = Arrays.stream(
installedPluginsPanel.getComponents()
).filter(extBox ->
extBox instanceof ExternalBox && ((ExternalBox) extBox).pluginInfo.id.equals(pluginId)
).findFirst();
}
if (externalBox.isEmpty())
{
log.info("EXTERNALBOX IS EMPTY: {}", pluginId);
return;
}
ExternalBox extBox = (ExternalBox) externalBox.get();
deps = externalPluginManager.getDependencies();
try
{
SwingUtil.syncExec(() ->
{
if (externalPluginChanged.isAdded())
{
availablePluginsPanel.remove(externalBox.get());
availablePluginsList.remove(extBox.pluginInfo);
installedPluginsList.add(extBox.pluginInfo);
installedPluginsList.sort(Comparator.naturalOrder());
installedPlugins();
pluginInstallButton(extBox.install, extBox.pluginInfo, true, deps.contains(extBox.pluginInfo.id));
}
else
{
installedPluginsPanel.remove(externalBox.get());
installedPluginsList.remove(extBox.pluginInfo);
availablePluginsList.add(extBox.pluginInfo);
availablePluginsList.sort(Comparator.naturalOrder());
availablePlugins();
pluginInstallButton(extBox.install, extBox.pluginInfo, false, false);
}
});
}
catch (InvocationTargetException | InterruptedException e)
{
e.printStackTrace();
}
}
private void installedPlugins()
{
GridBagConstraints c = new GridBagConstraints();
installedPluginsPanel.removeAll();
String search = searchBar.getText();
for (PluginInfo pluginInfo : installedPluginsList)
{
if (!search.equals("") && mismatchesSearchTerms(search, pluginInfo))
{
continue;
}
if (filterComboBox.getSelectedIndex() != 0)
{
boolean filtered = true;
String filter = String.valueOf(filterComboBox.getSelectedItem());
for (UpdateRepository updateRepository : updateManager.getRepositories())
{
if (filter.equals(updateRepository.getUrl().toString().replace("https://raw.githubusercontent.com/", "").replace("/master/", "")) &&
pluginInfo.getRepositoryId().equals(updateRepository.getId()))
{
filtered = false;
}
}
if (filtered)
{
continue;
}
}
ExternalBox pluginBox = new ExternalBox(pluginInfo);
pluginBox.pluginInfo = pluginInfo;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.gridy += 1;
c.insets = new Insets(5, 0, 0, 0);
pluginInstallButton(pluginBox.install, pluginInfo, true, deps.contains(pluginInfo.id));
installedPluginsPanel.add(pluginBox, c);
}
if (installedPluginsPanel.getComponents().length < 1)
{
installedPluginsPanel.add(titleLabel("No plugins found"));
}
}
private void availablePlugins()
{
GridBagConstraints c = new GridBagConstraints();
availablePluginsPanel.removeAll();
String search = searchBar.getText();
for (PluginInfo pluginInfo : availablePluginsList)
{
if (!search.equals("") && mismatchesSearchTerms(search, pluginInfo))
{
continue;
}
if (filterComboBox.getSelectedIndex() != 0)
{
boolean filtered = true;
String filter = String.valueOf(filterComboBox.getSelectedItem());
for (UpdateRepository updateRepository : updateManager.getRepositories())
{
if (filter.equals(updateRepository.getUrl().toString().replace("https://raw.githubusercontent.com/", "").replace("/master/", "")) &&
pluginInfo.getRepositoryId().equals(updateRepository.getId()))
{
filtered = false;
}
}
if (filtered)
{
continue;
}
}
ExternalBox pluginBox = new ExternalBox(pluginInfo);
pluginBox.pluginInfo = pluginInfo;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.gridy += 1;
c.insets = new Insets(5, 0, 0, 0);
pluginInstallButton(pluginBox.install, pluginInfo, false, false);
availablePluginsPanel.add(pluginBox, c);
}
if (availablePluginsPanel.getComponents().length < 1)
{
availablePluginsPanel.add(titleLabel("No plugins found"));
}
}
private void pluginInstallButton(JLabel install, PluginInfo pluginInfo, boolean installed, boolean hideAction)
{
install.setIcon(installed ? hideAction ? DELETE_ICON_GRAY : DELETE_ICON : ADD_ICON);
install.setText("");
if (!hideAction)
{
install.setToolTipText(installed ? "Uninstall" : "Install");
}
install.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
if (installed)
{
if (hideAction)
{
JOptionPane.showMessageDialog(ClientUI.getFrame(), "This plugin can't be uninstalled because one or more other plugins have a dependency on it.", "Error!", JOptionPane.ERROR_MESSAGE);
}
else
{
install.setIcon(null);
install.setText("Uninstalling");
SwingWorker<Boolean, Void> worker = new SwingWorker<>()
{
@Override
protected Boolean doInBackground()
{
return externalPluginManager.uninstall(pluginInfo.id);
}
@Override
protected void done()
{
boolean status = false;
try
{
status = get();
}
catch (InterruptedException | ExecutionException e)
{
}
if (!status)
{
pluginInstallButton(install, pluginInfo, installed, hideAction);
}
}
};
worker.execute();
}
}
else
{
install.setIcon(null);
install.setText("Installing");
SwingWorker<Boolean, Void> worker = new SwingWorker<>()
{
@Override
protected Boolean doInBackground()
{
return installPlugin(pluginInfo);
}
@Override
protected void done()
{
boolean status = false;
try
{
status = get();
}
catch (InterruptedException | ExecutionException e)
{
}
if (!status)
{
pluginInstallButton(install, pluginInfo, installed, hideAction);
}
}
};
worker.execute();
}
}
@Override
public void mouseEntered(MouseEvent e)
{
if (install.getText().toLowerCase().contains("installing"))
{
return;
}
install.setIcon(installed ? hideAction ? DELETE_HOVER_ICON_GRAY : DELETE_HOVER_ICON : ADD_HOVER_ICON);
}
@Override
public void mouseExited(MouseEvent e)
{
if (install.getText().toLowerCase().contains("installing"))
{
return;
}
install.setIcon(installed ? hideAction ? DELETE_ICON_GRAY : DELETE_ICON : ADD_ICON);
}
});
}
private boolean installPlugin(PluginInfo pluginInfo)
{
try
{
return externalPluginManager.install(pluginInfo.id);
}
catch (VerifyException ex)
{
try
{
SwingUtil.syncExec(() ->
JOptionPane.showMessageDialog(ClientUI.getFrame(), pluginInfo.name + " could not be installed, the hash could not be verified.", "Error!", JOptionPane.ERROR_MESSAGE));
}
catch (InvocationTargetException | InterruptedException ignored)
{
}
}
return false;
}
}

View File

@@ -0,0 +1,179 @@
package net.runelite.client.plugins.openosrs.externals;
import net.runelite.client.plugins.OPRSExternalPluginManager;
import com.openosrs.client.ui.JMultilineLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Optional;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import net.runelite.client.ui.ColorScheme;
import net.runelite.client.ui.FontManager;
import net.runelite.client.util.ImageUtil;
import net.runelite.client.util.LinkBrowser;
import org.pf4j.update.PluginInfo;
import org.pf4j.update.UpdateRepository;
public class RepositoryBox extends JPanel
{
private static final Font normalFont = FontManager.getRunescapeFont();
private static final Font smallFont = FontManager.getRunescapeSmallFont();
private static final ImageIcon DELETE_ICON;
private static final ImageIcon DELETE_HOVER_ICON;
private static final ImageIcon DISCORD_ICON;
private static final ImageIcon DISCORD_HOVER_ICON;
static
{
final BufferedImage deleteImg =
ImageUtil.recolorImage(
ImageUtil.resizeCanvas(
ImageUtil.getResourceStreamFromClass(ExternalPluginManagerPanel.class, "delete_icon.png"), 14, 14
), ColorScheme.BRAND_BLUE
);
DELETE_ICON = new ImageIcon(deleteImg);
DELETE_HOVER_ICON = new ImageIcon(ImageUtil.alphaOffset(deleteImg, 0.53f));
final BufferedImage discordImg =
ImageUtil.recolorImage(
ImageUtil.resizeCanvas(
ImageUtil.getResourceStreamFromClass(ExternalPluginManagerPanel.class, "discord_icon.png"), 14, 14
), Color.WHITE
);
DISCORD_ICON = new ImageIcon(discordImg);
DISCORD_HOVER_ICON = new ImageIcon(ImageUtil.alphaOffset(discordImg, 0.53f));
}
RepositoryBox(OPRSExternalPluginManager externalPluginManager, UpdateRepository updateRepository)
{
setLayout(new BorderLayout());
setBackground(ColorScheme.DARKER_GRAY_COLOR);
String name = updateRepository.getId();
String urlString = updateRepository.getUrl().toString();
if (urlString.startsWith("/"))
{
urlString = urlString.substring(1);
}
JPanel titleWrapper = new JPanel(new BorderLayout());
titleWrapper.setBackground(ColorScheme.DARKER_GRAY_COLOR);
titleWrapper.setBorder(new CompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 0, ColorScheme.DARK_GRAY_COLOR),
BorderFactory.createLineBorder(ColorScheme.DARKER_GRAY_COLOR)
));
JLabel title = new JLabel();
title.setText(name);
title.setFont(normalFont);
title.setBorder(null);
title.setBackground(ColorScheme.DARKER_GRAY_COLOR);
title.setPreferredSize(new Dimension(0, 24));
title.setForeground(Color.WHITE);
title.setBorder(new EmptyBorder(0, 8, 0, 0));
JPanel titleActions = new JPanel(new BorderLayout(3, 0));
titleActions.setBorder(new EmptyBorder(0, 0, 0, 8));
titleActions.setBackground(ColorScheme.DARKER_GRAY_COLOR);
Optional<PluginInfo> firstPlugin = updateRepository.getPlugins().values().stream().findFirst();
if (firstPlugin.isPresent() && !firstPlugin.get().projectUrl.equals(""))
{
JLabel support = new JLabel();
support.setIcon(DISCORD_ICON);
support.setToolTipText("Support");
support.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
LinkBrowser.browse(firstPlugin.get().projectUrl);
}
@Override
public void mouseEntered(MouseEvent e)
{
support.setIcon(DISCORD_HOVER_ICON);
}
@Override
public void mouseExited(MouseEvent e)
{
support.setIcon(DISCORD_ICON);
}
});
titleActions.add(support, BorderLayout.WEST);
}
if (!name.equals("OpenOSRS") && !name.equals("Plugin-Hub"))
{
JLabel install = new JLabel();
install.setIcon(DELETE_ICON);
install.setToolTipText("Remove");
install.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
externalPluginManager.removeRepository(updateRepository.getId());
}
@Override
public void mouseEntered(MouseEvent e)
{
install.setIcon(DELETE_HOVER_ICON);
}
@Override
public void mouseExited(MouseEvent e)
{
install.setIcon(DELETE_ICON);
}
});
titleActions.add(install, BorderLayout.EAST);
}
titleWrapper.add(title, BorderLayout.CENTER);
titleWrapper.add(titleActions, BorderLayout.EAST);
JMultilineLabel repository = new JMultilineLabel();
repository.setText(formatURL(urlString));
repository.setFont(smallFont);
repository.setDisabledTextColor(Color.WHITE);
String finalUrlString = urlString;
repository.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
LinkBrowser.browse(formatURL(finalUrlString));
}
});
add(titleWrapper, BorderLayout.NORTH);
add(repository, BorderLayout.CENTER);
}
private String formatURL(String url)
{
if (url.contains("githubusercontent"))
{
url = url.replace("raw.githubusercontent", "github").replace("/master/", "");
}
return url;
}
}

View File

@@ -0,0 +1,55 @@
package net.runelite.client.plugins.openosrs.externals;
import net.runelite.client.plugins.OPRSExternalPluginManager;
import com.openosrs.client.events.ExternalRepositoryChanged;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.inject.Inject;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.runelite.client.eventbus.EventBus;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.ui.ColorScheme;
import org.pf4j.update.UpdateRepository;
public class RepositoryPanel extends JPanel
{
@Inject
public EventBus eventBus;
private final OPRSExternalPluginManager externalPluginManager;
private final GridBagConstraints c = new GridBagConstraints();
RepositoryPanel(OPRSExternalPluginManager externalPluginManager, EventBus eventBus)
{
this.externalPluginManager = externalPluginManager;
setLayout(new GridBagLayout());
setBackground(ColorScheme.DARK_GRAY_COLOR);
setBorder(new EmptyBorder(0, 10, 0, 10));
onExternalRepositoryChanged(null);
eventBus.register(this);
}
@Subscribe
private void onExternalRepositoryChanged(ExternalRepositoryChanged event)
{
removeAll();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridy = 0;
c.insets = new Insets(5, 0, 0, 0);
for (UpdateRepository repository : externalPluginManager.getRepositories())
{
final RepositoryBox p = new RepositoryBox(externalPluginManager, repository);
add(p, c);
c.gridy++;
}
}
}

View File

@@ -577,4 +577,134 @@ public class ImageUtil
}
return image;
}
/**
* Draw fg centered on top of bg
*/
public static SpritePixels mergeSprites(final Client client, final SpritePixels bg, final SpritePixels fg)
{
assert fg.getHeight() <= bg.getHeight() && fg.getWidth() <= bg.getWidth() : "Background has to be larger than foreground";
final int[] canvas = Arrays.copyOf(bg.getPixels(), bg.getWidth() * bg.getHeight());
final SpritePixels result = client.createSpritePixels(canvas, bg.getWidth(), bg.getHeight());
final int bgWid = bg.getWidth();
final int fgHgt = fg.getHeight();
final int fgWid = fg.getWidth();
final int xOffset = (bgWid - fgWid) / 2;
final int yOffset = (bg.getHeight() - fgHgt) / 2;
final int[] fgPixels = fg.getPixels();
for (int y1 = yOffset, y2 = 0; y2 < fgHgt; y1++, y2++)
{
int i1 = y1 * bgWid + xOffset;
int i2 = y2 * fgWid;
for (int x = 0; x < fgWid; x++, i1++, i2++)
{
if (fgPixels[i2] > 0)
{
canvas[i1] = fgPixels[i2];
}
}
}
return result;
}
/**
* Resize Sprite sprite to given width (newW) and height (newH)
*/
public static SpritePixels resizeSprite(final Client client, final SpritePixels sprite, int newW, int newH)
{
assert newW > 0 && newH > 0;
final int oldW = sprite.getWidth();
final int oldH = sprite.getHeight();
if (oldW == newW && oldH == newH)
{
return sprite;
}
final int[] canvas = new int[newW * newH];
final int[] pixels = sprite.getPixels();
final SpritePixels result = client.createSpritePixels(canvas, newW, newH);
int pixelX = 0;
int pixelY = 0;
final int oldMaxW = sprite.getMaxWidth();
final int oldMaxH = sprite.getMaxHeight();
final int pixelW = (oldMaxW << 16) / newW;
final int pixelH = (oldMaxH << 16) / newH;
int xOffset = 0;
int yOffset = 0;
int canvasIdx;
if (sprite.getOffsetX() > 0)
{
canvasIdx = (pixelW + (sprite.getOffsetX() << 16) - 1) / pixelW;
xOffset += canvasIdx;
pixelX += canvasIdx * pixelW - (sprite.getOffsetX() << 16);
}
if (sprite.getOffsetY() > 0)
{
canvasIdx = (pixelH + (sprite.getOffsetY() << 16) - 1) / pixelH;
yOffset += canvasIdx;
pixelY += canvasIdx * pixelH - (sprite.getOffsetY() << 16);
}
if (oldW < oldMaxW)
{
newW = (pixelW + ((oldW << 16) - pixelX) - 1) / pixelW;
}
if (oldH < oldMaxH)
{
newH = (pixelH + ((oldH << 16) - pixelY) - 1) / pixelH;
}
canvasIdx = xOffset + yOffset * newW;
int canvasOffset = 0;
if (yOffset + newH > newH)
{
newH -= yOffset + newH - newH;
}
int tmp;
if (yOffset < 0)
{
tmp = -yOffset;
newH -= tmp;
canvasIdx += tmp * newW;
pixelY += pixelH * tmp;
}
if (newW + xOffset > newW)
{
tmp = newW + xOffset - newW;
newW -= tmp;
canvasOffset += tmp;
}
if (xOffset < 0)
{
tmp = -xOffset;
newW -= tmp;
canvasIdx += tmp;
pixelX += pixelW * tmp;
canvasOffset += tmp;
}
client.scaleSprite(canvas, pixels, 0, pixelX, pixelY, canvasIdx, canvasOffset, newW, newH, pixelW, pixelH, oldW);
return result;
}
}

View File

@@ -207,4 +207,54 @@ public class LinkBrowser
}
});
}
/**
* Tries to open the specified {@code File} with the systems default text editor. If operation fails
* an error message is displayed with the option to copy the absolute file path to clipboard.
*
* @param file the File instance of the log file
* @return did the file open successfully?
*/
public static boolean openLocalFile(final File file)
{
if (file == null || !file.exists())
{
return false;
}
if (attemptOpenLocalFile(file))
{
log.debug("Opened log file through Desktop#open to {}", file);
return true;
}
showMessageBox("Unable to open file. Press 'OK' and the file path will be copied to your clipboard", file.getAbsolutePath());
return false;
}
private static boolean attemptOpenLocalFile(final File file)
{
if (!Desktop.isDesktopSupported())
{
return false;
}
final Desktop desktop = Desktop.getDesktop();
if (!desktop.isSupported(Desktop.Action.OPEN))
{
return false;
}
try
{
desktop.open(file);
return true;
}
catch (IOException ex)
{
log.warn("Failed to open Desktop#open {}", file, ex);
return false;
}
}
}

View File

@@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, ThatGamerBlue <thatgamerblue@gmail.com>
* Copyright (c) 2019, PKLite
* Copyright (c) 2020, ThatGamerBlue <thatgamerblue@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without

View File

@@ -40,6 +40,7 @@ import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.function.BiConsumer;
import javax.annotation.Nonnull;
@@ -302,4 +303,19 @@ public class SwingUtil
l.enter();
}
}
/**
* Executes a runnable on the EDT, blocking until it finishes.
*/
public static void syncExec(final Runnable r) throws InvocationTargetException, InterruptedException
{
if (EventQueue.isDispatchThread())
{
r.run();
}
else
{
EventQueue.invokeAndWait(r);
}
}
}