2020-10-31 03:37:37 +13:00
|
|
|
import os
|
2020-11-26 10:23:06 +13:00
|
|
|
from getpass import getuser
|
|
|
|
from json import loads
|
2020-11-06 08:52:06 +13:00
|
|
|
from logging import getLogger
|
2020-10-31 03:37:37 +13:00
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
from PyQt5.QtCore import QUrl, pyqtSignal
|
2020-11-28 23:44:26 +13:00
|
|
|
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage
|
2020-12-15 01:07:59 +13:00
|
|
|
from PyQt5.QtWidgets import QDialog, QWidget, QVBoxLayout, QLabel, QPushButton, QStackedLayout, QLineEdit, QButtonGroup, \
|
|
|
|
QRadioButton
|
2020-11-26 10:23:06 +13:00
|
|
|
from legendary.core import LegendaryCore
|
2020-10-31 03:37:37 +13:00
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
logger = getLogger("LoginWindow")
|
2020-10-31 03:37:37 +13:00
|
|
|
|
2020-11-06 08:52:06 +13:00
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
class LoginBrowser(QWebEngineView):
|
2020-10-31 03:37:37 +13:00
|
|
|
def __init__(self):
|
2020-11-26 10:23:06 +13:00
|
|
|
super(LoginBrowser, self).__init__()
|
2020-11-28 23:44:26 +13:00
|
|
|
self.browser_profile = QWebEngineProfile("storage", self)
|
|
|
|
self.webpage = QWebEnginePage(self.browser_profile, self)
|
|
|
|
self.setPage(self.webpage)
|
2020-10-31 03:37:37 +13:00
|
|
|
|
2020-11-28 05:58:16 +13:00
|
|
|
def createWindow(self, webengine_window_type):
|
2020-11-26 10:23:06 +13:00
|
|
|
return self
|
2020-10-31 03:37:37 +13:00
|
|
|
|
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
class ImportWidget(QWidget):
|
|
|
|
signal = pyqtSignal(bool)
|
2020-10-31 03:37:37 +13:00
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
def __init__(self, core: LegendaryCore):
|
2020-10-31 03:37:37 +13:00
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
super(ImportWidget, self).__init__()
|
|
|
|
self.wine_paths = []
|
|
|
|
self.layout = QVBoxLayout()
|
|
|
|
self.core = core
|
2020-10-31 03:37:37 +13:00
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
self.import_text = QLabel("<h2>Import from existing Epic Games Launcher installation</h2>\nYou will get "
|
|
|
|
"logged out there")
|
|
|
|
self.import_accept_button = QPushButton("Import")
|
|
|
|
self.import_accept_button.clicked.connect(self.auth)
|
2020-12-15 01:07:59 +13:00
|
|
|
appdata_paths = self.get_appdata_path()
|
2020-10-31 03:37:37 +13:00
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
self.layout.addWidget(self.import_text)
|
2020-12-15 01:07:59 +13:00
|
|
|
|
|
|
|
if len(appdata_paths) == 0:
|
|
|
|
self.path = QLineEdit()
|
|
|
|
self.path.setPlaceholderText("Path to Wineprefix (Not implemented)")
|
|
|
|
self.layout.addWidget(self.path)
|
2020-12-15 01:48:20 +13:00
|
|
|
else:
|
|
|
|
self.btn_group = QButtonGroup()
|
2020-12-15 01:07:59 +13:00
|
|
|
for i in appdata_paths:
|
|
|
|
radio_button = QRadioButton(i)
|
|
|
|
self.btn_group.addButton(radio_button)
|
|
|
|
self.layout.addWidget(radio_button)
|
|
|
|
|
|
|
|
# for i in appdata_paths:
|
|
|
|
|
|
|
|
self.appdata_path_text = QLabel(f"Appdata path: {self.core.egl.appdata_path}")
|
|
|
|
|
|
|
|
self.login_text = QLabel("")
|
|
|
|
# self.layout.addWidget(self.btn_group)
|
|
|
|
self.layout.addWidget(self.login_text)
|
2020-11-26 10:23:06 +13:00
|
|
|
self.layout.addStretch(1)
|
|
|
|
self.layout.addWidget(self.import_accept_button)
|
2020-10-31 03:37:37 +13:00
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
self.setLayout(self.layout)
|
2020-11-06 08:52:06 +13:00
|
|
|
|
2020-12-15 01:07:59 +13:00
|
|
|
def get_appdata_path(self) -> list:
|
2020-11-26 10:23:06 +13:00
|
|
|
if self.core.egl.appdata_path:
|
2020-12-15 01:07:59 +13:00
|
|
|
return [self.core.egl.appdata_path]
|
2020-11-26 10:23:06 +13:00
|
|
|
|
|
|
|
else: # Linux
|
|
|
|
wine_paths = []
|
2020-12-15 01:07:59 +13:00
|
|
|
possible_wine_paths = [os.path.expanduser('~/Games/epic-games-store/'),
|
|
|
|
os.path.expanduser('~/.wine/')]
|
|
|
|
|
|
|
|
for i in possible_wine_paths:
|
2020-11-26 10:23:06 +13:00
|
|
|
if os.path.exists(i):
|
2020-12-15 01:48:20 +13:00
|
|
|
if os.path.exists(os.path.join(i, "drive_c/users", getuser(),
|
|
|
|
'Local Settings/Application Data/EpicGamesLauncher',
|
|
|
|
'Saved/Config/Windows')):
|
|
|
|
wine_paths.append(i)
|
2020-12-15 01:07:59 +13:00
|
|
|
|
|
|
|
if len(wine_paths) > 0:
|
|
|
|
appdata_dirs = [
|
|
|
|
os.path.join(i, "drive_c/users", getuser(), 'Local Settings/Application Data/EpicGamesLauncher',
|
|
|
|
'Saved/Config/Windows') for i in wine_paths]
|
|
|
|
return appdata_dirs
|
|
|
|
return []
|
2020-11-26 10:23:06 +13:00
|
|
|
|
|
|
|
def auth(self):
|
|
|
|
self.import_accept_button.setDisabled(True)
|
2020-12-15 01:48:20 +13:00
|
|
|
if not self.btn_group:
|
|
|
|
self.core.egl.appdata_path = self.path.text()
|
2020-12-15 01:07:59 +13:00
|
|
|
|
|
|
|
for i in self.btn_group.buttons():
|
2020-12-15 01:48:20 +13:00
|
|
|
|
2020-12-15 01:07:59 +13:00
|
|
|
if i.isChecked():
|
2020-12-15 01:48:20 +13:00
|
|
|
self.core.egl.appdata_path = i.text()
|
|
|
|
try:
|
|
|
|
if self.core.auth_import():
|
|
|
|
logger.info(f"Logged in as {self.core.lgd.userdata['displayName']}")
|
|
|
|
self.signal.emit(True)
|
|
|
|
return
|
|
|
|
|
|
|
|
except:
|
|
|
|
pass
|
2020-11-26 10:23:06 +13:00
|
|
|
|
2020-12-15 01:07:59 +13:00
|
|
|
self.import_accept_button.setDisabled(False)
|
2020-12-15 01:48:20 +13:00
|
|
|
logger.warning("Error: No valid session found")
|
2020-12-15 01:07:59 +13:00
|
|
|
self.login_text.setText("Error: No valid session found")
|
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
|
|
|
|
class LoginWindow(QDialog):
|
|
|
|
def __init__(self, core: LegendaryCore):
|
|
|
|
super(LoginWindow, self).__init__()
|
|
|
|
self.core = core
|
|
|
|
self.success_code = False
|
|
|
|
self.widget = QWidget()
|
|
|
|
self.setGeometry(0, 0, 200, 300)
|
|
|
|
self.welcome_layout = QVBoxLayout()
|
|
|
|
self.title = QLabel(
|
|
|
|
"<h2>Welcome to Rare the graphical interface for Legendary, an open source Epic Games alternative.</h2>\n<h3>Select one Option to Login</h3>")
|
|
|
|
self.browser_btn = QPushButton("Use browser to login")
|
|
|
|
self.browser_btn.clicked.connect(self.browser_login)
|
|
|
|
self.import_btn = QPushButton("Import from existing Epic Games installation")
|
|
|
|
|
|
|
|
self.import_btn.clicked.connect(self.import_login)
|
|
|
|
self.text = QLabel("")
|
|
|
|
self.exit_btn = QPushButton("Exit App")
|
|
|
|
self.exit_btn.clicked.connect(self.exit_login)
|
|
|
|
|
|
|
|
self.welcome_layout.addWidget(self.title)
|
|
|
|
self.welcome_layout.addWidget(self.browser_btn)
|
|
|
|
self.welcome_layout.addWidget(self.import_btn)
|
|
|
|
self.welcome_layout.addWidget(self.text)
|
|
|
|
self.welcome_layout.addWidget(self.exit_btn)
|
|
|
|
self.widget.setLayout(self.welcome_layout)
|
|
|
|
|
|
|
|
self.browser = LoginBrowser()
|
|
|
|
self.browser.loadFinished.connect(self.check_for_sid_page)
|
|
|
|
|
|
|
|
self.import_widget = ImportWidget(self.core)
|
|
|
|
self.import_widget.signal.connect(self.import_resp)
|
|
|
|
self.layout = QStackedLayout()
|
|
|
|
self.layout.addWidget(self.widget)
|
|
|
|
self.layout.addWidget(self.browser)
|
|
|
|
self.layout.addWidget(self.import_widget)
|
2020-11-06 08:52:06 +13:00
|
|
|
self.setLayout(self.layout)
|
2020-11-26 10:23:06 +13:00
|
|
|
self.show()
|
2020-10-31 03:37:37 +13:00
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
def import_resp(self, b: bool):
|
|
|
|
if b:
|
|
|
|
self.success()
|
2020-11-06 08:52:06 +13:00
|
|
|
else:
|
2020-11-26 10:23:06 +13:00
|
|
|
self.layout.setCurrentIndex(0)
|
|
|
|
self.text.setText("<h4 style='color: red'>No valid session found</h4>")
|
2020-10-31 03:37:37 +13:00
|
|
|
|
2020-11-26 10:23:06 +13:00
|
|
|
def login(self):
|
|
|
|
self.exec_()
|
|
|
|
return self.success_code
|
|
|
|
|
|
|
|
def success(self):
|
|
|
|
self.success_code = True
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def retry(self):
|
|
|
|
self.__init__(self.core)
|
|
|
|
|
|
|
|
def exit_login(self):
|
|
|
|
self.code = 1
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def browser_login(self):
|
|
|
|
self.setGeometry(0, 0, 800, 600)
|
|
|
|
self.browser.load(QUrl(
|
|
|
|
'https://www.epicgames.com/id/login?redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Fid%2Fapi%2Fredirect'))
|
|
|
|
self.layout.setCurrentIndex(1)
|
|
|
|
|
|
|
|
def import_login(self):
|
|
|
|
self.layout.setCurrentIndex(2)
|
|
|
|
|
|
|
|
def check_for_sid_page(self):
|
|
|
|
if self.browser.url() == QUrl("https://www.epicgames.com/id/api/redirect"):
|
|
|
|
self.browser.page().toPlainText(self.browser_auth)
|
|
|
|
|
|
|
|
def browser_auth(self, json):
|
|
|
|
token = self.core.auth_sid(loads(json)["sid"])
|
|
|
|
if self.core.auth_code(token):
|
|
|
|
logger.info(f"Successfully logged in as {self.core.lgd.userdata['displayName']}")
|
|
|
|
self.success()
|
|
|
|
else:
|
|
|
|
self.layout.setCurrentIndex(0)
|
|
|
|
logger.warning("Login failed")
|
|
|
|
self.browser.close()
|
|
|
|
self.text.setText("Login Failed")
|