-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCONClient.pyw
More file actions
358 lines (324 loc) · 17.9 KB
/
Copy pathRCONClient.pyw
File metadata and controls
358 lines (324 loc) · 17.9 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from datetime import datetime
from PyQt5 import QtCore, QtGui, QtWidgets
from helpers import *
from plugins import PLUGINS
from plugins.base import PluginBase
# noinspection PyUnresolvedReferences
class Ui_MainWindow(object):
# This function is auto-generated by pyuic, do not change as it may be overridden.
# noinspection PyAttributeOutsideInit
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(783, 683)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("./icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
self.centralwidget.setSizePolicy(sizePolicy)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setContentsMargins(6, 5, 6, 0)
self.gridLayout.setObjectName("gridLayout")
self.master_layout = QtWidgets.QGridLayout()
self.master_layout.setSizeConstraint(QtWidgets.QLayout.SetNoConstraint)
self.master_layout.setObjectName("master_layout")
self.config_dropdown = QtWidgets.QComboBox(self.centralwidget)
self.config_dropdown.setObjectName("config_dropdown")
self.master_layout.addWidget(self.config_dropdown, 0, 0, 1, 1)
self.scroll_area = QtWidgets.QScrollArea(self.centralwidget)
self.scroll_area.setFrameShape(QtWidgets.QFrame.NoFrame)
self.scroll_area.setFrameShadow(QtWidgets.QFrame.Raised)
self.scroll_area.setWidgetResizable(True)
self.scroll_area.setObjectName("scroll_area")
self.scroll_area_contents = QtWidgets.QWidget()
self.scroll_area_contents.setGeometry(QtCore.QRect(0, 0, 769, 578))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scroll_area_contents.sizePolicy().hasHeightForWidth())
self.scroll_area_contents.setSizePolicy(sizePolicy)
self.scroll_area_contents.setObjectName("scroll_area_contents")
self.gridLayout_2 = QtWidgets.QGridLayout(self.scroll_area_contents)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setSpacing(0)
self.gridLayout_2.setObjectName("gridLayout_2")
self.console = QtWidgets.QTextBrowser(self.scroll_area_contents)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.console.sizePolicy().hasHeightForWidth())
self.console.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Consolas")
font.setPointSize(10)
self.console.setFont(font)
self.console.setObjectName("console")
self.gridLayout_2.addWidget(self.console, 0, 0, 1, 1)
self.scroll_area.setWidget(self.scroll_area_contents)
self.master_layout.addWidget(self.scroll_area, 1, 0, 1, 1)
self.input_layout = QtWidgets.QHBoxLayout()
self.input_layout.setObjectName("input_layout")
self.command_input = QtWidgets.QLineEdit(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.command_input.sizePolicy().hasHeightForWidth())
self.command_input.setSizePolicy(sizePolicy)
self.command_input.setMinimumSize(QtCore.QSize(680, 0))
self.command_input.setInputMask("")
self.command_input.setObjectName("command_input")
self.input_layout.addWidget(self.command_input)
self.submit_button = QtWidgets.QPushButton(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.submit_button.sizePolicy().hasHeightForWidth())
self.submit_button.setSizePolicy(sizePolicy)
self.submit_button.setObjectName("submit_button")
self.input_layout.addWidget(self.submit_button)
self.master_layout.addLayout(self.input_layout, 2, 0, 1, 1)
self.gridLayout.addLayout(self.master_layout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 783, 21))
self.menubar.setObjectName("menubar")
self.menu_configs = QtWidgets.QMenu(self.menubar)
self.menu_configs.setObjectName("menu_configs")
self.menu_preferences = QtWidgets.QMenu(self.menubar)
self.menu_preferences.setObjectName("menu_preferences")
self.menu_instance = QtWidgets.QMenu(self.menubar)
self.menu_instance.setObjectName("menu_instance")
self.menu_plugins = QtWidgets.QMenu(self.menubar)
self.menu_plugins.setObjectName("menu_plugins")
MainWindow.setMenuBar(self.menubar)
self.action_config_new = QtWidgets.QAction(MainWindow)
self.action_config_new.setObjectName("action_config_new")
self.action_config_edit = QtWidgets.QAction(MainWindow)
self.action_config_edit.setObjectName("action_config_edit")
self.action_config_delete = QtWidgets.QAction(MainWindow)
self.action_config_delete.setObjectName("action_config_delete")
self.action_instance_clear = QtWidgets.QAction(MainWindow)
self.action_instance_clear.setObjectName("action_instance_clear")
self.action_config_reorder = QtWidgets.QAction(MainWindow)
self.action_config_reorder.setObjectName("action_config_reorder")
self.action_instance_save = QtWidgets.QAction(MainWindow)
self.action_instance_save.setObjectName("action_instance_save")
self.menu_configs.addAction(self.action_config_new)
self.menu_configs.addAction(self.action_config_edit)
self.menu_configs.addAction(self.action_config_delete)
self.menu_configs.addAction(self.action_config_reorder)
self.menu_instance.addAction(self.action_instance_clear)
self.menu_instance.addAction(self.action_instance_save)
self.menubar.addAction(self.menu_instance.menuAction())
self.menubar.addAction(self.menu_configs.menuAction())
self.menubar.addAction(self.menu_plugins.menuAction())
self.menubar.addAction(self.menu_preferences.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.config_dropdown, self.command_input)
MainWindow.setTabOrder(self.command_input, self.console)
MainWindow.setTabOrder(self.console, self.scroll_area)
MainWindow.setTabOrder(self.scroll_area, self.submit_button)
# Start custom code, do not override anything under this line.
# Custom variables
self.consoles = {}
self.command_history = []
self.command_history_index = 0
# Setup dropdown
self.set_dropdown()
# Bind callbacks
self.submit_button.clicked.connect(self.submit_command) # Bind submit button to submit_command function
self.action_config_new.triggered.connect(self.new_config) # Bind new config button to new_config function
self.action_config_edit.triggered.connect(self.edit_config) # Bind edit config button to edit_config function
self.action_config_delete.triggered.connect(
self.delete_config) # Bind delete config button to delete_config function
self.action_config_reorder.triggered.connect(
self.reorder_configs) # Bind reorder config button to reorder_configs function
self.action_instance_clear.triggered.connect(
self.clear_console) # Bind clear instance button to clear_console function
self.action_instance_save.triggered.connect(
self.save_console) # Bind save instance button to save_console function
self.config_dropdown.currentIndexChanged.connect(
self.change_console_config) # Bind dropdown to change_console_config function
# Custom event hooks
self.command_input.keyPressEvent = self.input_handle_keypress
# Set default focus to command input
self.command_input.setFocus()
# This function is auto-generated by pyuic, do not change as it may be overridden.
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "RCON Client v{}".format(VERSION)))
self.command_input.setPlaceholderText(_translate("MainWindow", "Enter your command here"))
self.submit_button.setText(_translate("MainWindow", "Submit"))
self.submit_button.setShortcut(_translate("MainWindow", "Return"))
self.menu_configs.setTitle(_translate("MainWindow", "Configurations"))
self.menu_preferences.setTitle(_translate("MainWindow", "Preferences"))
self.menu_instance.setTitle(_translate("MainWindow", "Instance"))
self.menu_plugins.setTitle(_translate("MainWindow", "Plugins"))
self.action_config_new.setText(_translate("MainWindow", "New"))
self.action_config_edit.setText(_translate("MainWindow", "Edit Current"))
self.action_config_delete.setText(_translate("MainWindow", "Delete Current"))
self.action_instance_clear.setText(_translate("MainWindow", "Clear Console"))
self.action_config_reorder.setText(_translate("MainWindow", "Reorder"))
self.action_instance_save.setText(_translate("MainWindow", "Save Console"))
self.action_instance_save.setShortcut(_translate("MainWindow", "Ctrl+S"))
# CUSTOM METHODS
def set_dropdown(self):
self.config_dropdown.clear()
self.config_dropdown.addItems(CONFIG.keys())
def get_active_config(self) -> str:
return self.config_dropdown.currentText()
def append_to_console(self, text: str):
self.console.append(text)
self.scroll_to_bottom()
# Update consoles output cache
self.consoles[self.get_active_config()] = self.console.toPlainText()
def scroll_to_bottom(self):
self.console.verticalScrollBar().setValue(self.console.verticalScrollBar().maximum())
def call_hooks(self, hook_name, *args, **kwargs):
for plugin in PLUGINS:
res = call_if_hooked(plugin, hook_name, *args, **kwargs)
if res is not None:
self.console.append("=== [/{0}] ===\n{1}\n=== [{0}/] ===\n".format(plugin.name, res))
# CALLBACKS
# noinspection PyAttributeOutsideInit
def submit_command(self):
"""This is a callback function that is called when the submit button is clicked."""
# Only submit command if command_input or the button has focus. This is to avoid accidental submits w/ keybinds.
if self.command_input.hasFocus() or self.submit_button.hasFocus():
# Do not allow the submission of an empty command
if self.command_input.text().strip() == "":
return
# Do not continue if there is no active config
if self.get_active_config() == "":
return QtWidgets.QMessageBox.critical(None, "Error", "No active configuration selected.")
command = self.command_input.text()
self.command_history_index = 0
self.command_history.append(command)
self.console.append("] {}".format(command))
self.command_input.clear()
self.call_hooks("on_command_send", command)
self.scroll_to_bottom()
try:
response = send_rcon_command(command, *CONFIG[self.get_active_config()].values())
except Exception as e:
response = "Error: {}".format(e)
self.console.append(response)
self.call_hooks("on_response_received", command, response)
self.scroll_to_bottom()
# Update consoles output cache
self.consoles[self.get_active_config()] = self.console.toPlainText()
def submit_command_plugin(self, command, show: bool = True) -> str:
"""Function that allows plugins to submit a command, optionally update the console,
return the response, and bypass any hooks, command history, and most checks."""
if self.get_active_config() == "":
QtWidgets.QMessageBox.critical(None, "Error", "No active configuration selected.")
return "No active configuration selected."
if show:
self.console.append("] {}".format(command))
try:
response = send_rcon_command(command, *CONFIG[self.get_active_config()].values())
except Exception as e:
response = "Error: {}".format(e)
if show:
self.console.append(response)
self.scroll_to_bottom()
# Update consoles output cache
self.consoles[self.get_active_config()] = self.console.toPlainText()
return response
def new_config(self):
dialog = ConfigEditor(self.new_config_dialog_callback)
dialog.exec()
def edit_config(self):
dialog = ConfigEditor(self.edit_config_dialog_callback, CONFIG[self.get_active_config()],
self.get_active_config())
dialog.exec()
def delete_config(self):
reply = QtWidgets.QMessageBox.question(None, "Confirm", "Are you sure you want to delete \"{}\"?".format(
self.get_active_config()), QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
del CONFIG[self.get_active_config()]
save_config(CONFIG)
self.set_dropdown()
def reorder_configs(self):
dialog = ConfigReorderDialog(self.reorder_configs_dialog_callback, list(CONFIG.keys()))
dialog.exec()
def new_config_dialog_callback(self, config_name, ip, port, password, _):
if config_name in CONFIG:
return QtWidgets.QMessageBox.critical(None, "Error", "A configuration with that name already exists.")
CONFIG[config_name] = {"ip": ip, "port": port, "password": password}
save_config(CONFIG)
self.set_dropdown()
def edit_config_dialog_callback(self, config_name, ip, port, password, old_config_name):
if old_config_name is not None and config_name in CONFIG:
return QtWidgets.QMessageBox.critical(None, "Error", "A configuration with that name already exists.")
CONFIG[config_name] = {"ip": ip, "port": port, "password": password}
if old_config_name is not None:
del CONFIG[old_config_name]
save_config(CONFIG)
self.set_dropdown()
def reorder_configs_dialog_callback(self, config_order):
# noinspection PyGlobalUndefined
global CONFIG
CONFIG = {i: CONFIG[i] for i in config_order}
save_config(CONFIG)
self.set_dropdown()
def clear_console(self):
self.console.clear()
def save_console(self):
filename = QtWidgets.QFileDialog.getSaveFileName(None, "Save Console", clean_filename(
"rcon_output_{}_{}".format(self.get_active_config().lower().strip(),
datetime.now().strftime("%Y-%H-%M-%S_%f"))), "Text Files (*.txt)")[0]
if filename == "":
return
with open(filename, "w", encoding="utf-8") as f:
try:
f.write(self.console.toPlainText())
except UnicodeEncodeError:
QtWidgets.QMessageBox.critical(None, "Error", "Could not save console. Invalid characters detected.")
def change_console_config(self):
self.clear_console()
if self.get_active_config() in self.consoles:
self.console.append(self.consoles[self.get_active_config()])
self.scroll_to_bottom()
# CUSTOM HOOKS
def input_handle_keypress(self, e):
if e.key() == QtCore.Qt.Key_Up and abs(self.command_history_index - 1) <= len(self.command_history):
self.command_history_index -= 1
elif e.key() == QtCore.Qt.Key_Down and self.command_history_index + 1 <= -1:
self.command_history_index += 1
else:
return QtWidgets.QLineEdit.keyPressEvent(self.command_input, e)
self.command_input.setText(self.command_history[self.command_history_index])
def call_if_hooked(child_obj, hook_name, *args, **kwargs):
parent_func = getattr(PluginBase, hook_name)
child_func = getattr(child_obj, hook_name)
if parent_func != child_func:
return child_func(*args, **kwargs)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
master_window = QtWidgets.QMainWindow()
main_window = Ui_MainWindow()
main_window.setupUi(master_window)
for plugin in PLUGINS:
menu_action = QtWidgets.QAction(master_window)
menu_action.setObjectName("action_plugin_{}".format(plugin.name))
menu_action.setText(plugin.name)
# noinspection PyUnresolvedReferences
menu_action.triggered.connect(lambda _, p=plugin: p.on_menu())
main_window.menu_plugins.addAction(menu_action)
if plugin.keybind is not None:
menu_action.setShortcut(plugin.keybind)
# noinspection PyProtectedMember
plugin._set_main_window(main_window)
call_if_hooked(plugin, "on_load")
master_window.show()
sys.exit(app.exec_())