-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
214 lines (180 loc) · 8.71 KB
/
Copy pathmain.py
File metadata and controls
214 lines (180 loc) · 8.71 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
import os
import sys
import traceback
from concurrent.futures import ProcessPoolExecutor, as_completed
from datetime import datetime
from multiprocessing import Manager
from traceback import format_exception
from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication, QFileDialog, QMainWindow, QTableWidgetItem
from extract_psx import extract_textures
from main_window_ui import Ui_main_window
PRINT_OUTPUT = True
PRINT_TRACEBACK = True
class NumericTableWidgetItem(QTableWidgetItem):
def __lt__(self, other):
self_value = int(self.text()) if self.text() else 0
other_value = int(other.text()) if other.text() else 0
return self_value < other_value
# Define main window class, inherits QMainWindow and Ui_main_window
class Window(QMainWindow, Ui_main_window):
current_dir = ""
output_dir = ""
current_files = []
files_processed = 0
start_time = 0
create_sub_dirs = False
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
# Open a directory picker and set the input directory path
def input_browse_clicked(self):
options = QFileDialog.Options()
dir_name = QFileDialog.getExistingDirectory(self, "Choose Directory", "", options=options)
if dir_name:
self.current_dir = dir_name
self.current_files = []
self.file_table.setRowCount(0)
self.get_psx_files(dir_name)
# Filter files with .psx or .PSX extensions
def filter_psx_files(self, file_list):
return [f for f in file_list if f.lower().endswith((".psx", ".PSX"))]
# Get .psx files from the chosen directory and update the GUI
def get_psx_files(self, dir_name):
self.input_path.setText(dir_name)
dir_files = [f for f in os.listdir(dir_name) if os.path.isfile(os.path.join(dir_name, f))]
psx_files = list(self.filter_psx_files(dir_files))
if len(psx_files) > 0:
self.file_table.setRowCount(len(psx_files))
for row, file in enumerate(psx_files):
self.current_files.append(file)
self.file_table.setItem(row, 0, QTableWidgetItem(file))
if self.output_dir:
self.extract_button.setEnabled(True)
else:
self.extract_button.setEnabled(False)
# Open a directory picker and set the output directory path
def output_browse_clicked(self):
options = QFileDialog.Options()
dir_name = QFileDialog.getExistingDirectory(self, "Choose Directory", "", options=options)
if dir_name:
self.output_dir = dir_name
self.output_path.setText(dir_name)
if len(self.current_files) > 0:
self.extract_button.setEnabled(True)
else:
self.extract_button.setEnabled(False)
# Clear the Textures Extracted and Status columns
def clear_columns(self):
for row in range(self.file_table.rowCount()):
self.file_table.setItem(row, 2, NumericTableWidgetItem(""))
self.file_table.setItem(row, 3, NumericTableWidgetItem(""))
# Start the extraction process when the extract button is clicked
def extract_clicked(self):
# Cleanup previous state
self.clear_columns()
self.progress_bar.setValue(0)
self.extract_button.setEnabled(False)
self.file_table.setSortingEnabled(False)
self.files_processed = 0
# Start the extraction process
self.start_time = datetime.now()
self.worker = Worker(self.current_files, self.current_dir, self.output_dir, self.file_table, self.create_sub_dirs)
self.worker.update_progress_bar_signal.connect(self.update_progress_bar)
self.worker.extraction_complete_signal.connect(self.extraction_complete)
self.worker.update_file_table_signal.connect(self.update_file_table)
self.worker.start()
# Update the progress bar based on the number of files processed
@pyqtSlot()
def update_progress_bar(self):
self.files_processed += 1
progress = round(self.files_processed / len(self.current_files) * 100)
self.progress_bar.setValue(progress)
# Update the file table in the GUI
@pyqtSlot(int, int, str)
def update_file_table(self, row, col, text):
self.file_table.setItem(row, col, NumericTableWidgetItem(text)) if col in [1, 2] else self.file_table.setItem(row, col, QTableWidgetItem(text))
# Update the UI and display the time elapsed when the extraction is complete
@pyqtSlot()
def extraction_complete(self):
self.progress_bar.setValue(100)
self.extract_button.setEnabled(True)
self.file_table.setSortingEnabled(True)
self.status_bar.showMessage(f"Time elapsed: {(datetime.now() - self.start_time).total_seconds()}")
# Toggle the create_sub_dirs boolean when the Create Subdirectories checkbox is clicked
def create_sub_dirs_clicked(self):
self.create_sub_dirs = not self.create_sub_dirs
# Function to process a single file
def process_file(queue, filename, input_dir, output_dir, file_index, create_sub_dirs):
output_strings = []
separator = "\n"
def update_file_table(row, cols):
for col, text in cols.items():
queue.put(("update_file_table_signal", row, col, text))
try:
extract_textures(filename, input_dir, output_dir, file_index, create_sub_dirs, output_strings, update_file_table)
if PRINT_OUTPUT:
output_strings.append(f"Finished extracting textures from {filename}\n")
except Exception as error:
if PRINT_OUTPUT:
output_strings.append(f"An error occurred while trying to extract from {filename}. The error was: {error}\n")
if PRINT_TRACEBACK:
traceback.print_exc()
finally:
queue.put(("update_progress_bar_signal",))
if PRINT_OUTPUT and len(output_strings) > 0:
print(separator.join(output_strings))
# Define the worker thread class, inherits QThread
class Worker(QThread):
# Define custom PyQt signals for progress, completion, and updating the file table
update_progress_bar_signal = pyqtSignal()
update_file_table_signal = pyqtSignal(int, int, str)
extraction_complete_signal = pyqtSignal()
def __init__(self, files, input_dir, output_dir, file_table, create_sub_dirs):
super().__init__()
# Initialize instance variables
self.files = files
self.input_dir = input_dir
self.output_dir = output_dir
self.file_table = file_table
self.create_sub_dirs = create_sub_dirs
# Run the worker thread
def run(self):
# Get the number of available CPU cores
max_workers = os.cpu_count()
# Use a Manager for inter-process communication
with Manager() as manager:
queue = manager.Queue() # Create a queue for sharing data between processes
# Use a ProcessPoolExecutor for parallel processing
with ProcessPoolExecutor(max_workers=max_workers) as executor:
# Submit each file for processing and store the resulting Future objects
futures = [
executor.submit(process_file, queue, filename, self.input_dir, self.output_dir, self.files.index(filename), self.create_sub_dirs) for filename in self.files
]
# Continuously check if all futures are done
while True:
if all(f.done() for f in futures):
break
# Process items in the queue until it is empty
while not queue.empty():
signal_type, *args = queue.get()
if signal_type == "update_file_table_signal":
self.update_file_table_signal.emit(*args)
elif signal_type == "update_progress_bar_signal":
self.update_progress_bar_signal.emit()
# Iterate through the completed futures and handle any exceptions
for future in as_completed(futures):
try:
future.result()
except Exception as error:
if PRINT_TRACEBACK:
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback_msg = "".join(format_exception(exc_type, exc_value, exc_traceback))
print(f"An error occurred in the process: {error}\nTraceback: {traceback_msg}")
# Emit the extraction complete signal to inform the GUI that the process is done
self.extraction_complete_signal.emit()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())