runelite-client: add logout feature to account plugin

This commit is contained in:
Adam
2017-05-16 20:30:08 -04:00
parent 7e0cc60097
commit 79a10cc2d2
7 changed files with 165 additions and 25 deletions

View File

@@ -38,12 +38,12 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import joptsimple.OptionParser; import joptsimple.OptionParser;
import joptsimple.OptionSet; import joptsimple.OptionSet;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.client.account.AccountSession; import net.runelite.client.account.AccountSession;
import net.runelite.client.events.SessionClose;
import net.runelite.client.events.SessionOpen; import net.runelite.client.events.SessionOpen;
import net.runelite.client.menus.MenuManager; import net.runelite.client.menus.MenuManager;
import net.runelite.client.plugins.PluginManager; import net.runelite.client.plugins.PluginManager;
@@ -68,9 +68,9 @@ public class RuneLite
private ClientUI gui; private ClientUI gui;
private PluginManager pluginManager; private PluginManager pluginManager;
private MenuManager menuManager = new MenuManager(this); private final MenuManager menuManager = new MenuManager(this);
private OverlayRenderer renderer; private OverlayRenderer renderer;
private EventBus eventBus = new EventBus(this::eventExceptionHandler); private final EventBus eventBus = new EventBus(this::eventExceptionHandler);
private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(4); private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
private WSClient wsclient; private WSClient wsclient;
@@ -171,8 +171,6 @@ public class RuneLite
return; return;
} }
SESSION_FILE.getParentFile().mkdirs();
try (FileWriter fw = new FileWriter(SESSION_FILE)) try (FileWriter fw = new FileWriter(SESSION_FILE))
{ {
new Gson().toJson(accountSession, fw); new Gson().toJson(accountSession, fw);
@@ -185,30 +183,29 @@ public class RuneLite
} }
} }
public void deleteSession()
{
SESSION_FILE.delete();
}
/** /**
* Set the given session as the active session and open a socket to the * Set the given session as the active session and open a socket to the
* server with the given session * server with the given session
*
* @param session * @param session
*/ */
public void openSession(AccountSession session) public void openSession(AccountSession session)
{ {
boolean needExecutor = false; // If the ws session already exists, don't need to do anything
if (wsclient == null || !wsclient.getSession().equals(session))
if (wsclient != null)
{ {
wsclient.close(); if (wsclient != null)
} {
else wsclient.close();
{ }
needExecutor = true;
}
wsclient = new WSClient(session); wsclient = new WSClient(session);
wsclient.connect(); wsclient.connect();
if (needExecutor)
{
executor.scheduleWithFixedDelay(wsclient::ping, WSClient.PING_TIME.getSeconds(), WSClient.PING_TIME.getSeconds(), TimeUnit.SECONDS);
} }
accountSession = session; accountSession = session;
@@ -216,6 +213,26 @@ public class RuneLite
eventBus.post(new SessionOpen()); eventBus.post(new SessionOpen());
} }
public void closeSession()
{
if (wsclient != null)
{
wsclient.close();
wsclient = null;
}
if (accountSession == null)
{
return;
}
logger.debug("Logging out of account {}", accountSession.getUsername());
accountSession = null; // No more account
eventBus.post(new SessionClose());
}
private void eventExceptionHandler(Throwable exception, SubscriberExceptionContext context) private void eventExceptionHandler(Throwable exception, SubscriberExceptionContext context)
{ {
logger.warn("uncaught exception in event subscriber", exception); logger.warn("uncaught exception in event subscriber", exception);

View File

@@ -28,6 +28,9 @@ import com.google.common.eventbus.EventBus;
import com.google.gson.Gson; import com.google.gson.Gson;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import net.runelite.client.account.AccountSession; import net.runelite.client.account.AccountSession;
import net.runelite.http.api.RuneliteAPI; import net.runelite.http.api.RuneliteAPI;
import net.runelite.http.api.ws.messages.Handshake; import net.runelite.http.api.ws.messages.Handshake;
@@ -46,19 +49,27 @@ public class WSClient extends WebSocketListener implements AutoCloseable
{ {
private static final Logger logger = LoggerFactory.getLogger(WSClient.class); private static final Logger logger = LoggerFactory.getLogger(WSClient.class);
public static final Duration PING_TIME = Duration.ofSeconds(30); private static final Duration PING_TIME = Duration.ofSeconds(30);
private static final Gson gson = WebsocketGsonFactory.build(); private static final Gson gson = WebsocketGsonFactory.build();
private static final EventBus eventBus = RuneLite.getRunelite().getEventBus(); private static final EventBus eventBus = RuneLite.getRunelite().getEventBus();
private static final ScheduledExecutorService executor = RuneLite.getRunelite().getExecutor();
private final OkHttpClient client = new OkHttpClient(); private final OkHttpClient client = new OkHttpClient();
private final AccountSession session; private final AccountSession session;
private WebSocket webSocket; private WebSocket webSocket;
private final ScheduledFuture pingFuture;
public WSClient(AccountSession session) public WSClient(AccountSession session)
{ {
this.session = session; this.session = session;
this.pingFuture = executor.scheduleWithFixedDelay(this::ping, PING_TIME.getSeconds(), PING_TIME.getSeconds(), TimeUnit.SECONDS);
}
public AccountSession getSession()
{
return session;
} }
public void connect() public void connect()
@@ -99,6 +110,11 @@ public class WSClient extends WebSocketListener implements AutoCloseable
@Override @Override
public void close() public void close()
{ {
if (pingFuture != null)
{
pingFuture.cancel(true);
}
if (webSocket != null) if (webSocket != null)
{ {
webSocket.close(1000, null); webSocket.close(1000, null);

View File

@@ -25,6 +25,7 @@
package net.runelite.client.account; package net.runelite.client.account;
import java.time.Instant; import java.time.Instant;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
public class AccountSession public class AccountSession
@@ -33,6 +34,37 @@ public class AccountSession
private String username; private String username;
private Instant created; private Instant created;
@Override
public int hashCode()
{
int hash = 3;
hash = 29 * hash + Objects.hashCode(this.uuid);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final AccountSession other = (AccountSession) obj;
if (!Objects.equals(this.uuid, other.uuid))
{
return false;
}
return true;
}
public UUID getUuid() public UUID getUuid()
{ {
return uuid; return uuid;

View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) 2017, Adam <Adam@sigterm.info>
* 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.events;
public class SessionClose
{
}

View File

@@ -36,10 +36,12 @@ import javax.imageio.ImageIO;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import net.runelite.client.RuneLite; import net.runelite.client.RuneLite;
import net.runelite.client.account.AccountSession; import net.runelite.client.account.AccountSession;
import net.runelite.client.events.SessionClose;
import net.runelite.client.events.SessionOpen; import net.runelite.client.events.SessionOpen;
import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.Plugin;
import net.runelite.client.ui.ClientUI; import net.runelite.client.ui.ClientUI;
import net.runelite.client.ui.NavigationButton; import net.runelite.client.ui.NavigationButton;
import net.runelite.client.ui.NavigationPanel;
import net.runelite.client.util.RunnableExceptionLogger; import net.runelite.client.util.RunnableExceptionLogger;
import net.runelite.http.api.account.LoginClient; import net.runelite.http.api.account.LoginClient;
import net.runelite.http.api.account.OAuthResponse; import net.runelite.http.api.account.OAuthResponse;
@@ -54,17 +56,22 @@ public class AccountPlugin extends Plugin
private final RuneLite runelite = RuneLite.getRunelite(); private final RuneLite runelite = RuneLite.getRunelite();
private final ClientUI ui = runelite.getGui(); private final ClientUI ui = runelite.getGui();
private final NavigationButton loginButton = new NavigationButton("Login"); private final NavigationButton loginButton = new NavigationButton("Login");
private final NavigationButton logoutButton = new NavigationButton("Logout");
private final LoginClient loginClient = new LoginClient(); private final LoginClient loginClient = new LoginClient();
@Override @Override
protected void startUp() throws Exception protected void startUp() throws Exception
{ {
loginButton.getButton().addActionListener(this::loginClick);
ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("login_icon.png"))); ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("login_icon.png")));
loginButton.getButton().setIcon(icon); loginButton.getButton().setIcon(icon);
icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("logout_icon.png")));
logoutButton.getButton().setIcon(icon);
loginButton.getButton().addActionListener(this::loginClick);
logoutButton.getButton().addActionListener(this::logoutClick);
ui.getNavigationPanel().addNavigation(loginButton); ui.getNavigationPanel().addNavigation(loginButton);
} }
@@ -79,6 +86,17 @@ public class AccountPlugin extends Plugin
executor.execute(RunnableExceptionLogger.wrap(this::openLoginPage)); executor.execute(RunnableExceptionLogger.wrap(this::openLoginPage));
} }
private void logoutClick(ActionEvent ae)
{
runelite.closeSession();
runelite.deleteSession();
// Replace logout nav button with login
NavigationPanel navigationPanel = ui.getNavigationPanel();
navigationPanel.removeNavigation(logoutButton);
navigationPanel.addNavigation(loginButton);
}
private void openLoginPage() private void openLoginPage()
{ {
OAuthResponse login; OAuthResponse login;
@@ -130,11 +148,15 @@ public class AccountPlugin extends Plugin
{ {
logger.debug("Now logged in as {}", loginResponse.getUsername()); logger.debug("Now logged in as {}", loginResponse.getUsername());
runelite.getGui().setTitle("RuneLite (" + loginResponse.getUsername() + ")"); //runelite.getGui().setTitle("RuneLite (" + loginResponse.getUsername() + ")");
AccountSession session = runelite.getAccountSession(); AccountSession session = runelite.getAccountSession();
session.setUsername(loginResponse.getUsername()); session.setUsername(loginResponse.getUsername());
// Open session, again, now that we have a username
// This triggers onSessionOpen
runelite.openSession(session);
// Save session to disk
runelite.saveSession(); runelite.saveSession();
} }
@@ -151,6 +173,22 @@ public class AccountPlugin extends Plugin
logger.debug("Session opened as {}", session.getUsername()); logger.debug("Session opened as {}", session.getUsername());
runelite.getGui().setTitle("RuneLite (" + session.getUsername() + ")"); runelite.getGui().setTitle("RuneLite (" + session.getUsername() + ")");
replaceLoginWithLogout();
}
private void replaceLoginWithLogout()
{
// Replace login nav button with logout
NavigationPanel navigationPanel = ui.getNavigationPanel();
navigationPanel.removeNavigation(loginButton);
navigationPanel.addNavigation(logoutButton);
}
@Subscribe
public void onSessionClose(SessionClose sessionClose)
{
runelite.getGui().setTitle("RuneLite");
} }
} }

View File

@@ -52,4 +52,11 @@ public class NavigationPanel extends JPanel
add(button.getButton()); add(button.getButton());
revalidate(); revalidate();
} }
public void removeNavigation(NavigationButton button)
{
buttons.remove(button);
remove(button.getButton());
revalidate();
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 371 B