Extract session-related logic to SessionManager
- Extract runelite session related logic from RuneLite to SessionManager class and expose this as Guice service - Move WSClient to account package Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
@@ -26,7 +26,6 @@ package net.runelite.client;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.eventbus.EventBus;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
@@ -38,10 +37,7 @@ import java.awt.TrayIcon;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import javax.imageio.ImageIO;
|
||||
@@ -57,16 +53,13 @@ import joptsimple.OptionSet;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.runelite.api.Client;
|
||||
import net.runelite.api.Query;
|
||||
import net.runelite.client.account.AccountSession;
|
||||
import net.runelite.client.account.SessionManager;
|
||||
import net.runelite.client.chat.ChatMessageManager;
|
||||
import net.runelite.client.config.ConfigManager;
|
||||
import net.runelite.client.events.SessionClose;
|
||||
import net.runelite.client.events.SessionOpen;
|
||||
import net.runelite.client.menus.MenuManager;
|
||||
import net.runelite.client.plugins.PluginManager;
|
||||
import net.runelite.client.ui.ClientUI;
|
||||
import net.runelite.client.ui.overlay.OverlayRenderer;
|
||||
import net.runelite.http.api.account.AccountClient;
|
||||
import org.pushingpixels.substance.api.skin.SubstanceGraphiteLookAndFeel;
|
||||
|
||||
@Singleton
|
||||
@@ -75,7 +68,6 @@ public class RuneLite
|
||||
{
|
||||
public static final File RUNELITE_DIR = new File(System.getProperty("user.home"), ".runelite");
|
||||
public static final File PROFILES_DIR = new File(RUNELITE_DIR, "profiles");
|
||||
public static final File SESSION_FILE = new File(RUNELITE_DIR, "session");
|
||||
public static final File PLUGIN_DIR = new File(RUNELITE_DIR, "plugins");
|
||||
|
||||
public static Image ICON;
|
||||
@@ -113,9 +105,8 @@ public class RuneLite
|
||||
@Inject
|
||||
private OverlayRenderer overlayRenderer;
|
||||
|
||||
private WSClient wsclient;
|
||||
|
||||
private AccountSession accountSession;
|
||||
@Inject
|
||||
private SessionManager sessionManager;
|
||||
|
||||
Notifier notifier;
|
||||
|
||||
@@ -199,7 +190,7 @@ public class RuneLite
|
||||
pluginManager.startCorePlugins();
|
||||
|
||||
// Load the session, including saved configuration
|
||||
loadSession();
|
||||
sessionManager.loadSession();
|
||||
|
||||
// Begin watching for new plugins
|
||||
pluginManager.watch();
|
||||
@@ -251,118 +242,6 @@ public class RuneLite
|
||||
});
|
||||
}
|
||||
|
||||
private void loadSession()
|
||||
{
|
||||
if (!SESSION_FILE.exists())
|
||||
{
|
||||
log.info("No session file exists");
|
||||
return;
|
||||
}
|
||||
|
||||
AccountSession session;
|
||||
|
||||
try (FileInputStream in = new FileInputStream(SESSION_FILE))
|
||||
{
|
||||
session = new Gson().fromJson(new InputStreamReader(in), AccountSession.class);
|
||||
|
||||
log.debug("Loaded session for {}", session.getUsername());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.warn("Unable to load session file", ex);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if session is still valid
|
||||
AccountClient accountClient = new AccountClient(session.getUuid());
|
||||
if (!accountClient.sesssionCheck())
|
||||
{
|
||||
log.debug("Loaded session {} is invalid", session.getUuid());
|
||||
return;
|
||||
}
|
||||
|
||||
openSession(session);
|
||||
}
|
||||
|
||||
public void saveSession()
|
||||
{
|
||||
if (accountSession == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try (FileWriter fw = new FileWriter(SESSION_FILE))
|
||||
{
|
||||
new Gson().toJson(accountSession, fw);
|
||||
|
||||
log.debug("Saved session to {}", SESSION_FILE);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
log.warn("Unable to save session file", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteSession()
|
||||
{
|
||||
SESSION_FILE.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the given session as the active session and open a socket to the
|
||||
* server with the given session
|
||||
*
|
||||
* @param session
|
||||
*/
|
||||
public void openSession(AccountSession session)
|
||||
{
|
||||
// If the ws session already exists, don't need to do anything
|
||||
if (wsclient == null || !wsclient.getSession().equals(session))
|
||||
{
|
||||
if (wsclient != null)
|
||||
{
|
||||
wsclient.close();
|
||||
}
|
||||
|
||||
wsclient = new WSClient(eventBus, executor, session);
|
||||
wsclient.connect();
|
||||
}
|
||||
|
||||
accountSession = session;
|
||||
|
||||
if (session.getUsername() != null)
|
||||
{
|
||||
// Initialize config for new session
|
||||
// If the session isn't logged in yet, don't switch to the new config
|
||||
configManager.switchSession(session);
|
||||
}
|
||||
|
||||
eventBus.post(new SessionOpen());
|
||||
}
|
||||
|
||||
public void closeSession()
|
||||
{
|
||||
if (wsclient != null)
|
||||
{
|
||||
wsclient.close();
|
||||
wsclient = null;
|
||||
}
|
||||
|
||||
if (accountSession == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
log.debug("Logging out of account {}", accountSession.getUsername());
|
||||
|
||||
accountSession = null; // No more account
|
||||
|
||||
// Restore config
|
||||
configManager.switchSession(null);
|
||||
|
||||
eventBus.post(new SessionClose());
|
||||
}
|
||||
|
||||
public Client getClient()
|
||||
{
|
||||
return client;
|
||||
@@ -408,11 +287,6 @@ public class RuneLite
|
||||
return trayIcon;
|
||||
}
|
||||
|
||||
public AccountSession getAccountSession()
|
||||
{
|
||||
return accountSession;
|
||||
}
|
||||
|
||||
public <T> T[] runQuery(Query query)
|
||||
{
|
||||
return (T[]) query.result(client);
|
||||
|
||||
@@ -33,6 +33,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
import javax.inject.Singleton;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.runelite.api.Client;
|
||||
import net.runelite.client.account.SessionManager;
|
||||
import net.runelite.client.chat.ChatMessageManager;
|
||||
import net.runelite.client.config.ConfigManager;
|
||||
import net.runelite.client.config.RuneliteConfig;
|
||||
@@ -57,6 +58,7 @@ public class RuneliteModule extends AbstractModule
|
||||
bind(Scheduler.class);
|
||||
bind(PluginManager.class);
|
||||
bind(RuneliteProperties.class);
|
||||
bind(SessionManager.class);
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* 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.account;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
import com.google.gson.Gson;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.runelite.client.RuneLite;
|
||||
import net.runelite.client.config.ConfigManager;
|
||||
import net.runelite.client.events.SessionClose;
|
||||
import net.runelite.client.events.SessionOpen;
|
||||
import net.runelite.http.api.account.AccountClient;
|
||||
|
||||
@Singleton
|
||||
@Slf4j
|
||||
public class SessionManager
|
||||
{
|
||||
private static final File SESSION_FILE = new File(RuneLite.RUNELITE_DIR, "session");
|
||||
private WSClient wsclient;
|
||||
|
||||
@Getter
|
||||
private AccountSession accountSession;
|
||||
|
||||
@Inject
|
||||
private EventBus eventBus;
|
||||
|
||||
@Inject
|
||||
private ConfigManager configManager;
|
||||
|
||||
@Inject
|
||||
private ScheduledExecutorService executor;
|
||||
|
||||
public void loadSession()
|
||||
{
|
||||
if (!SESSION_FILE.exists())
|
||||
{
|
||||
log.info("No session file exists");
|
||||
return;
|
||||
}
|
||||
|
||||
AccountSession session;
|
||||
|
||||
try (FileInputStream in = new FileInputStream(SESSION_FILE))
|
||||
{
|
||||
session = new Gson().fromJson(new InputStreamReader(in), AccountSession.class);
|
||||
|
||||
log.debug("Loaded session for {}", session.getUsername());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.warn("Unable to load session file", ex);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if session is still valid
|
||||
AccountClient accountClient = new AccountClient(session.getUuid());
|
||||
if (!accountClient.sesssionCheck())
|
||||
{
|
||||
log.debug("Loaded session {} is invalid", session.getUuid());
|
||||
return;
|
||||
}
|
||||
|
||||
openSession(session);
|
||||
}
|
||||
|
||||
public void saveSession()
|
||||
{
|
||||
if (accountSession == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try (FileWriter fw = new FileWriter(SESSION_FILE))
|
||||
{
|
||||
new Gson().toJson(accountSession, fw);
|
||||
|
||||
log.debug("Saved session to {}", SESSION_FILE);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
log.warn("Unable to save session file", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteSession()
|
||||
{
|
||||
SESSION_FILE.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the given session as the active session and open a socket to the
|
||||
* server with the given session
|
||||
*
|
||||
* @param session
|
||||
*/
|
||||
public void openSession(AccountSession session)
|
||||
{
|
||||
// If the ws session already exists, don't need to do anything
|
||||
if (wsclient == null || !wsclient.getSession().equals(session))
|
||||
{
|
||||
if (wsclient != null)
|
||||
{
|
||||
wsclient.close();
|
||||
}
|
||||
|
||||
wsclient = new WSClient(eventBus, executor, session);
|
||||
wsclient.connect();
|
||||
}
|
||||
|
||||
accountSession = session;
|
||||
|
||||
if (session.getUsername() != null)
|
||||
{
|
||||
// Initialize config for new session
|
||||
// If the session isn't logged in yet, don't switch to the new config
|
||||
configManager.switchSession(session);
|
||||
}
|
||||
|
||||
eventBus.post(new SessionOpen());
|
||||
}
|
||||
|
||||
public void closeSession()
|
||||
{
|
||||
if (wsclient != null)
|
||||
{
|
||||
wsclient.close();
|
||||
wsclient = null;
|
||||
}
|
||||
|
||||
if (accountSession == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
log.debug("Logging out of account {}", accountSession.getUsername());
|
||||
|
||||
accountSession = null; // No more account
|
||||
|
||||
// Restore config
|
||||
configManager.switchSession(null);
|
||||
|
||||
eventBus.post(new SessionClose());
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@
|
||||
* (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;
|
||||
package net.runelite.client.account;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
import com.google.gson.Gson;
|
||||
@@ -32,12 +32,11 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.runelite.client.account.AccountSession;
|
||||
import net.runelite.http.api.RuneliteAPI;
|
||||
import net.runelite.http.api.ws.messages.Handshake;
|
||||
import net.runelite.http.api.ws.messages.Ping;
|
||||
import net.runelite.http.api.ws.WebsocketGsonFactory;
|
||||
import net.runelite.http.api.ws.WebsocketMessage;
|
||||
import net.runelite.http.api.ws.messages.Handshake;
|
||||
import net.runelite.http.api.ws.messages.Ping;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
@@ -37,6 +37,7 @@ import javax.inject.Inject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.runelite.client.RuneLite;
|
||||
import net.runelite.client.account.AccountSession;
|
||||
import net.runelite.client.account.SessionManager;
|
||||
import net.runelite.client.events.SessionClose;
|
||||
import net.runelite.client.events.SessionOpen;
|
||||
import net.runelite.client.plugins.Plugin;
|
||||
@@ -58,6 +59,9 @@ public class AccountPlugin extends Plugin
|
||||
@Inject
|
||||
RuneLite runelite;
|
||||
|
||||
@Inject
|
||||
SessionManager sessionManager;
|
||||
|
||||
@Inject
|
||||
ClientUI ui;
|
||||
|
||||
@@ -87,7 +91,7 @@ public class AccountPlugin extends Plugin
|
||||
private void logoutClick(ActionEvent ae)
|
||||
{
|
||||
// Destroy session
|
||||
AccountSession session = runelite.getAccountSession();
|
||||
AccountSession session = sessionManager.getAccountSession();
|
||||
if (session != null)
|
||||
{
|
||||
AccountClient client = new AccountClient(session.getUuid());
|
||||
@@ -101,8 +105,8 @@ public class AccountPlugin extends Plugin
|
||||
}
|
||||
}
|
||||
|
||||
runelite.closeSession(); // remove session from client
|
||||
runelite.deleteSession(); // delete saved session file
|
||||
sessionManager.closeSession(); // remove session from client
|
||||
sessionManager.deleteSession(); // delete saved session file
|
||||
|
||||
// Replace logout nav button with login
|
||||
PluginToolbar navigationPanel = ui.getPluginToolbar();
|
||||
@@ -129,7 +133,7 @@ public class AccountPlugin extends Plugin
|
||||
session.setUuid(login.getUid());
|
||||
session.setCreated(Instant.now());
|
||||
|
||||
runelite.openSession(session);
|
||||
sessionManager.openSession(session);
|
||||
|
||||
if (!Desktop.isDesktopSupported())
|
||||
{
|
||||
@@ -161,21 +165,21 @@ public class AccountPlugin extends Plugin
|
||||
{
|
||||
log.debug("Now logged in as {}", loginResponse.getUsername());
|
||||
|
||||
AccountSession session = runelite.getAccountSession();
|
||||
AccountSession session = sessionManager.getAccountSession();
|
||||
session.setUsername(loginResponse.getUsername());
|
||||
|
||||
// Open session, again, now that we have a username
|
||||
// This triggers onSessionOpen
|
||||
runelite.openSession(session);
|
||||
sessionManager.openSession(session);
|
||||
|
||||
// Save session to disk
|
||||
runelite.saveSession();
|
||||
sessionManager.saveSession();
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onSessionOpen(SessionOpen sessionOpen)
|
||||
{
|
||||
AccountSession session = runelite.getAccountSession();
|
||||
AccountSession session = sessionManager.getAccountSession();
|
||||
|
||||
if (session.getUsername() == null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user