-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkonstant.py
278 lines (236 loc) · 11.6 KB
/
konstant.py
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
import sys
import os
import requests
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import webbrowser
#whatman was here
class LineNumberArea(QWidget):
def __init__(self, editor):
super().__init__(editor)
self.code_editor = editor
def paintEvent(self, event):
painter = QPainter(self)
painter.fillRect(event.rect(), QColor(250, 261, 250))
block = self.code_editor.firstVisibleBlock()
block_number = block.blockNumber()
top = int(self.code_editor.blockBoundingGeometry(block).translated(self.code_editor.contentOffset()).top())
bottom = top + int(self.code_editor.blockBoundingRect(block).height())
while block.isValid() and top <= event.rect().bottom():
if block.isVisible() and bottom >= event.rect().top():
number = str(block_number + 1)
painter.setPen(Qt.black)
painter.drawText(0, top, self.width(), self.code_editor.fontMetrics().height(), Qt.AlignRight, number)
block = block.next()
top = bottom
bottom = top + int(self.code_editor.blockBoundingRect(block).height())
block_number += 1
def update_width(self):
return self.fontMetrics().width(str(max(1, self.code_editor.blockCount()))) + 10
#im still here btw whatman
class CodeEditor(QPlainTextEdit):
def __init__(self, *args):
super().__init__(*args)
self.line_number_area = LineNumberArea(self)
self.blockCountChanged.connect(self.update_line_number_area_width)
self.updateRequest.connect(self.update_line_number_area)
self.cursorPositionChanged.connect(self.highlight_current_line)
self.update_line_number_area_width(0)
self.highlight_current_line()
def line_number_area_width(self):
return self.line_number_area.update_width()
def update_line_number_area_width(self, _):
self.setViewportMargins(self.line_number_area_width(), 0, 0, 0)
def update_line_number_area(self, rect, dy):
if dy:
self.line_number_area.scroll(0, dy)
else:
self.line_number_area.update(0, rect.y(), self.line_number_area.width(), rect.height())
if rect.contains(self.viewport().rect()):
self.update_line_number_area_width(0)
def resizeEvent(self, event):
super().resizeEvent(event)
rect = self.contentsRect()
self.line_number_area.setGeometry(QRect(rect.left(), rect.top(), self.line_number_area_width(), rect.height()))
def highlight_current_line(self):
extra_selections = []
if not self.isReadOnly():
selection = QTextEdit.ExtraSelection()
self.setExtraSelections(extra_selections)
#konstant only fr
class LuauSyntaxHighlighter(QSyntaxHighlighter):
def __init__(self, parent=None):
super(LuauSyntaxHighlighter, self).__init__(parent)
self.keyword_format = QTextCharFormat()
self.keyword_format.setForeground(QColor("#00FF00"))
self.function_format = QTextCharFormat()
self.function_format.setForeground(QColor("#FFD700"))
self.variable_format = QTextCharFormat()
self.variable_format.setForeground(QColor("#FF4500"))
self.comment_format = QTextCharFormat()
self.comment_format.setForeground(QColor("#A9A9A9"))
self.string_format = QTextCharFormat()
self.string_format.setForeground(QColor("#00BFFF"))
self.number_format = QTextCharFormat()
self.number_format.setForeground(QColor("#FF1493"))
self.boolean_format = QTextCharFormat()
self.boolean_format.setForeground(QColor("#FFA500"))
self.operator_format = QTextCharFormat()
self.operator_format.setForeground(QColor("#FF69B4"))
self.keywords = ["local", "function", "end", "return", "if", "then", "else", "elseif", "for", "in", "do", "while", "repeat", "until", "true", "false", "nil", "and", "or", "not", "break", "continue", "goto"]
self.functions = ["print", "pairs", "ipairs", "next", "type", "assert", "error", "require", "tonumber", "tostring", "math", "table", "string"]
self.variables = ["self", "_G", "_ENV"]
self.booleans = ["true", "false"]
self.operators = ["+", "-", "*", "/", "=", "==", "<", ">", "<=", ">=", "~=", ".."]
def highlightBlock(self, text):
for keyword in self.keywords:
index = text.find(keyword)
while index >= 0:
length = len(keyword)
self.setFormat(index, length, self.keyword_format)
index = text.find(keyword, index + length)
for function in self.functions:
index = text.find(function)
while index >= 0:
length = len(function)
self.setFormat(index, length, self.function_format)
index = text.find(function, index + length)
for variable in self.variables:
index = text.find(variable)
while index >= 0:
length = len(variable)
self.setFormat(index, length, self.variable_format)
index = text.find(variable, index + length)
for boolean in self.booleans:
index = text.find(boolean)
while index >= 0:
length = len(boolean)
self.setFormat(index, length, self.boolean_format)
index = text.find(boolean, index + length)
for operator in self.operators:
index = text.find(operator)
while index >= 0:
length = len(operator)
self.setFormat(index, length, self.operator_format)
index = text.find(operator, index + length)
if "--" in text:
comment_index = text.find("--")
self.setFormat(comment_index, len(text) - comment_index, self.comment_format)
in_string = False
for i, char in enumerate(text):
if char == '"' or char == "'":
if not in_string:
start = i
in_string = True
else:
self.setFormat(start, i - start + 1, self.string_format)
in_string = False
for word in text.split():
if word.isdigit():
index = text.find(word)
self.setFormat(index, len(word), self.number_format)
class KonstantApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Konstant")
self.setGeometry(100, 100, 800, 600)
icon_path = os.path.join(os.path.dirname(__file__), 'icons', 'konstant.png')
self.setWindowIcon(QIcon(icon_path))
version = self.fetch_version()
self.setWindowTitle(f"Konstant {version}")
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
layout = QVBoxLayout()
central_widget.setLayout(layout)
self.text_edit_input = CodeEditor(self)
self.text_edit_input.setFont(QFont("Courier", 10))
self.highlighter = LuauSyntaxHighlighter(self.text_edit_input.document())
layout.addWidget(QLabel("Input:"))
layout.addWidget(self.text_edit_input)
buttons_layout = QHBoxLayout()
self.open_button = QPushButton("Open", self)
self.open_button.clicked.connect(self.open_file)
buttons_layout.addWidget(self.open_button)
self.clear_button = QPushButton("Clear", self)
self.clear_button.clicked.connect(self.clear_editor)
buttons_layout.addWidget(self.clear_button)
self.save_input_button = QPushButton("Save Input", self)
self.save_input_button.clicked.connect(self.save_input)
buttons_layout.addWidget(self.save_input_button)
self.discord_button = QPushButton("Discord", self)
self.discord_button.clicked.connect(self.open_discord)
buttons_layout.addWidget(self.discord_button)
self.decompile_button = QPushButton("Decompile", self)
self.decompile_button.clicked.connect(self.decompile_script)
buttons_layout.addWidget(self.decompile_button)
layout.addLayout(buttons_layout)
self.text_edit_output = CodeEditor(self)
self.text_edit_output.setReadOnly(True)
self.text_edit_output.setFont(QFont("Courier", 10))
self.output_highlighter = LuauSyntaxHighlighter(self.text_edit_output.document())
layout.addWidget(QLabel("Output:"))
layout.addWidget(self.text_edit_output)
self.save_output_button = QPushButton("Save Output", self)
self.save_output_button.clicked.connect(self.save_output)
layout.addWidget(self.save_output_button)
def update_line_numbers(self):
self.line_number_area.update_line_numbers()
def clear_editor(self):
self.text_edit_input.clear()
def open_file(self):
options = QFileDialog.Options()
file_name, _ = QFileDialog.getOpenFileName(self, "Select Your file.", "", "Text Files (*.txt);;Lua Files (*.lua);;Luau Files (*.luau);;All Files (*)", options=options)
if file_name:
with open(file_name, 'r') as file:
file_content = file.read()
self.text_edit_input.setPlainText(file_content)
def save_input(self):
options = QFileDialog.Options()
file_name, _ = QFileDialog.getSaveFileName(self, "Save Input", "", "Lua Files (*.lua);;All Files (*)", options=options)
if file_name:
with open(file_name, 'w') as file:
file.write(self.text_edit_input.toPlainText())
def save_output(self):
options = QFileDialog.Options()
file_name, _ = QFileDialog.getSaveFileName(self, "Save Output", "", "Text Files (*.txt);;All Files (*)", options=options)
if file_name:
with open(file_name, 'w') as file:
file.write(self.text_edit_output.toPlainText())
def open_discord(self):
discord_url = "https://discord.gg/79aMKGcvH5"
webbrowser.open(discord_url)
def decompile_script(self):
input_script = self.text_edit_input.toPlainText()
if not input_script:
self.text_edit_output.setPlainText("Error: Input cannot be empty!")
return
try:
decompiled_code = self.decompile_api_call(input_script)
self.text_edit_output.setPlainText(decompiled_code)
except Exception as e:
self.text_edit_output.setPlainText(f"Error during decompilation: {e}")
def decompile_api_call(self, script_code):
API = "http://api.plusgiant5.com/konstant/decompile"
response = requests.post(API, data=script_code, headers={"Content-Type": "text/plain"})
if response.status_code == 200:
return response.text
else:
return f"API Error: {response.status_code}\n{response.text}"
def fetch_version(self):
version_url = "https://raw.githubusercontent.com/Whatmanhere/KonstantApp/refs/heads/main/version.txt"
try:
response = requests.get(version_url)
if response.status_code == 200:
return response.text.strip()
else:
return "Unknown"
except Exception:
return "Unknown"
def main():
app = QApplication(sys.argv)
window = KonstantApp()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()