-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdaptive.py
More file actions
271 lines (227 loc) · 10.7 KB
/
Copy pathAdaptive.py
File metadata and controls
271 lines (227 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import sys
from os import system
from utilities import GITHUBURL, fetchConfig, editConfig, getLeaguePath, checkLeaguePath, resourcePath, APPID, checkAutostart, toggleAutostart, VERSION, addCurrentProfile, profileList, removeProfile, renameProfile, editModeConfig, fetchModeConfig, replaceConfig, ISSUESURL, isOutdated, RELEASEURL
from lcu_driver import Connector
from ctypes import windll
from gui import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QDialog, QMessageBox, QInputDialog, QSystemTrayIcon, QMenu
from PyQt5.QtCore import QObject, QThread, pyqtSignal as Sig, pyqtSlot as Slot
from PyQt5.QtGui import QIcon, QPixmap
class Window(QMainWindow, Ui_MainWindow):
startLCUrequested = Sig()
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.iconPixmap = QPixmap(resourcePath("asset/icon.png"))
self.WIcon = QIcon(resourcePath("asset/icon.png"))
self.setWindowIcon(self.WIcon)
self.checkUpdatesStartup()
self.icon.setPixmap(self.iconPixmap)
self.statusBar().showMessage("Waiting for League ...")
self.configList.addItems(profileList())
self.creds.setText(f"<html><head/><body><p><span style=\" font-size:20pt; font-weight:600;\">Adaptive {VERSION}</span></p><p><span style=\" font-size:10pt;\">by Ria</span></p></body></html>")
self.windowOnStartup.setChecked(fetchConfig("showWindowOnStartup"))
self.startWithWin.setChecked(checkAutostart())
self.updateOnStartup.setChecked(fetchConfig("updateOnStartup"))
self.updateLeaguePath()
self.createSystemTray()
self.connectSignalSlots()
self.hide()
self.lcuWorker = LCUWorker()
self.lcuThread = QThread()
self.lcuWorker.modes.connect(self.fetchedModes)
self.startLCUrequested.connect(self.lcuWorker.startLCU)
self.lcuWorker.moveToThread(self.lcuThread)
self.lcuThread.start()
self.startLCUrequested.emit()
def changeEvent(self, event):
event.accept()
if event.type() == 105:
self.hide()
def createSystemTray(self):
self.sysTray = QSystemTrayIcon(self)
self.sysTray.setIcon(self.WIcon)
self.sysTray.setVisible(True)
self.sysTray.setToolTip("Adaptive")
self.sysTray.activated.connect(self.sysTrayActivated)
self.sysTrayMenu = QMenu(self)
creds = self.sysTrayMenu.addAction(f"Adaptive {VERSION} - by Ria")
creds.setDisabled(True)
creds.setIcon(self.WIcon)
self.sysTrayMenu.addSeparator()
settingsAct = self.sysTrayMenu.addAction("Settings")
settingsAct.triggered.connect(self.showNormal)
exitAct = self.sysTrayMenu.addAction("Exit")
exitAct.triggered.connect(self.close)
self.sysTray.setContextMenu(self.sysTrayMenu)
def sysTrayActivated(self, reason):
if reason == 3:
self.showNormal()
def fetchedModes(self, modes: dict):
self.modeNameId = modes
self.statusBar().clearMessage()
self.chooseMode.clear()
self.chooseMode.addItems(list(modes.keys()))
self.updateConfigList()
if fetchConfig("leaguePath") == "":
editConfig("leaguePath", getLeaguePath())
self.updateLeaguePath()
def connectSignalSlots(self):
self.sourceCode.clicked.connect(self.showSourceCode)
self.chooseMode.activated[str].connect(self.modeChanged)
self.chooseConfig.activated[str].connect(self.configChanged)
self.pathBrowse.clicked.connect(self.selectLeaguePath)
self.startWithWin.toggled.connect(toggleAutostart)
self.addCurrent.clicked.connect(self.addProfile)
self.removeConfig.clicked.connect(self.remProfile)
self.renameConfig.clicked.connect(self.renProfile)
self.windowOnStartup.toggled.connect(self.toggleWindowOnStartup)
self.reportBug.clicked.connect(self.showReportBug)
self.updateOnStartup.toggled.connect(self.toggleUpdateOnStartup)
self.checkUpdates.clicked.connect(self.checkForUpdates)
def checkUpdatesStartup(self):
if fetchConfig("updateOnStartup"):
outdated = isOutdated()
if outdated:
choice = QMessageBox.question(self, "Update available", f"A newer version of Adaptive detected ({outdated}). Do you want to visit the download site?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if choice == QMessageBox.Yes:
system(f"start \"\" {RELEASEURL}")
def checkForUpdates(self):
outdated = isOutdated()
if outdated:
choice = QMessageBox.question(self, "Update available", f"A newer version of Adaptive detected ({outdated}). Do you want to visit the download site?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if choice == QMessageBox.Yes:
system(f"start \"\" {RELEASEURL}")
else:
QMessageBox.information(self, "No updates available", "You are already on the latest version.", QMessageBox.Ok)
def toggleWindowOnStartup(self):
editConfig("showWindowOnStartup", self.windowOnStartup.isChecked())
def toggleUpdateOnStartup(self):
editConfig("updateOnStartup", self.updateOnStartup.isChecked())
def modeChanged(self, mode):
self.updateConfigList()
def configChanged(self, config):
if config == "No changes": config = -1
editModeConfig(self.modeNameId[self.chooseMode.currentText()], config)
def updateConfigList(self):
self.chooseConfig.clear()
self.chooseConfig.addItem("No changes")
self.chooseConfig.addItems(profileList())
try:
selected = fetchModeConfig(self.modeNameId[self.chooseMode.currentText()])
if selected != -1:
self.chooseConfig.setCurrentIndex(self.chooseConfig.findText(selected))
except AttributeError: pass
def updateLeaguePath(self):
self.pathText.setText(fetchConfig('leaguePath'))
def showSourceCode(self):
system(f"start \"\" {GITHUBURL}")
def showReportBug(self):
system(f"start \"\" {ISSUESURL}")
def selectLeaguePath(self):
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.DirectoryOnly)
errorDialog = QMessageBox()
errorDialog.setIcon(QMessageBox.Critical)
errorDialog.setWindowIcon(self.WIcon)
errorDialog.setText('Folder must contain the game League of Legends\nTypically "C:\\Riot Games\\League of Legends"')
errorDialog.setWindowTitle("Invalid path")
while True:
if dialog.exec() == QDialog.Accepted:
path = dialog.selectedFiles()[0].replace("/", "\\", -1)
if checkLeaguePath(path):
editConfig("leaguePath", path)
self.updateLeaguePath()
break
errorDialog.exec()
continue
break
def addProfile(self):
name, ok = QInputDialog.getText(self, "Enter a name for this Config", "Name:" + " " * 75)
if name and ok:
errorDialog = QMessageBox()
errorDialog.setIcon(QMessageBox.Critical)
errorDialog.setWindowTitle("An Error occured")
errorDialog.setWindowIcon(self.WIcon)
res = addCurrentProfile(name)
if res == -1:
errorDialog.setText('Name already existed.')
elif res == -2:
errorDialog.setText("Please set your League's path in the Settings tab.")
elif res == -4:
errorDialog.setText('Names cannot contain \\ / : * ? " < > |')
elif res in (-3, 0):
self.configList.addItem(name)
self.updateConfigList()
return
errorDialog.exec()
def remProfile(self):
current = self.configList.currentItem()
if current is None: return
removeProfile(current.text())
self.configList.takeItem(self.configList.currentRow())
self.updateConfigList()
def renProfile(self):
current = self.configList.currentItem()
if current is None: return
name, ok = QInputDialog.getText(self, "Enter another name for this Config", "Name:" + " " * 85)
if name and ok:
errorDialog = QMessageBox()
errorDialog.setIcon(QMessageBox.Critical)
errorDialog.setWindowTitle("An Error occured")
errorDialog.setWindowIcon(self.WIcon)
res = renameProfile(current.text(), name)
if res == -1:
errorDialog.setText('Name already existed.')
elif res == -4:
errorDialog.setText('Names cannot contain \\ / : * ? " < > |')
elif res == 0:
self.configList.item(self.configList.currentRow()).setText(name)
self.updateConfigList()
return
errorDialog.exec()
class LCUWorker(QObject):
modes = Sig(dict)
def __init__(self):
super().__init__()
self.connector = Connector()
@self.connector.ready
async def connect(connection):
while True:
modesm = await connection.request('get', '/lol-game-queues/v1/queues')
if not modesm.status == 500:
modesm = await modesm.json()
break
print("Logged in")
maps = (await (await connection.request('get', '/lol-maps/v2/maps')).json())
mapIdName = {}
for map in maps:
mapIdName[map["id"]] = map["name"]
modesl = {}
for mode in modesm:
if mode["queueAvailability"] == "Available":
modesl[f'{mapIdName[mode["mapId"]]} ({mode["description"]})'] = mode["id"]
modesl = {k: v for k, v in sorted(modesl.items())}
modesl["Custom"] = -1
self.modes.emit(modesl)
@self.connector.ws.register("/lol-gameflow/v1/session", event_types = ("CREATE", "UPDATE", "DELETE"))
async def gameFlow(connection, event):
data = event.data
phase = data['phase']
if phase == "ChampSelect":
queueId = data["gameData"]["queue"]["id"]
print(queueId)
config = fetchModeConfig(queueId)
if config != -1:
replaceConfig(config)
print("Applied ", config)
@Slot()
def startLCU(self):
self.connector.start()
if __name__ == "__main__":
windll.shell32.SetCurrentProcessExplicitAppUserModelID(APPID)
app = QApplication(sys.argv)
win = Window()
if fetchConfig("showWindowOnStartup"):
win.show()
sys.exit(app.exec())