-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
340 lines (296 loc) · 12.3 KB
/
main.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
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
import os
import re
import logging
import subprocess
from logging import handlers
from threading import Thread
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt, QStringListModel
from PyQt5.QtWidgets import *
from form import Ui_Form
class Logger(object):
level_relations = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
} # 日志级别关系映射
def __init__(self, filename, level='info', when='D', backCount=3,
fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'):
self.logger = logging.getLogger(filename)
format_str = logging.Formatter(fmt) # 设置日志格式
self.logger.setLevel(self.level_relations.get(level)) # 设置日志级别
sh = logging.StreamHandler() # 往屏幕上输出
sh.setFormatter(format_str) # 设置屏幕上显示的格式
th = handlers.TimedRotatingFileHandler(filename=filename, when=when, backupCount=backCount, encoding='utf-8')
# 往文件里写入#指定间隔时间自动生成文件的处理器
th.setFormatter(format_str) # 设置文件里写入的格式
self.logger.addHandler(sh) # 把对象加到logger里
self.logger.addHandler(th)
class mainForm(QWidget):
def __init__(self):
# 调用父类构造函数,初始化空窗口
super().__init__()
# 使用ui文件导入定义界面类
self.ui = Ui_Form()
# 初始化界面
self.ui.setupUi(self)
# 设置类成员
self.qList = []
self.subMSD = None
# 日志对象
self.log = Logger('debug.log', level='debug')
# 设置信号
self.ui.toolButton_gamePos.clicked.connect(self.open_file_game)
self.ui.toolButton_savePos.clicked.connect(self.open_file_txtsave)
self.ui.pushButton_scan.clicked.connect(self.scanThreadFunc)
self.ui.pushButton_opentxt.clicked.connect(self.open_txtlist)
self.ui.pushButton_readtxt.clicked.connect(self.read_txtlist)
self.ui.pushButton_index.clicked.connect(self.index_search)
self.ui.pushButton_down.clicked.connect(self.download_mes)
self.ui.lineEdit_audioPath.textChanged.connect(self.on_audiopath_changed)
self.ui.comboBox_legend.currentIndexChanged.connect(self.legend_chosen)
self.ui.pushButton_diag.clicked.connect(self.search_diag)
self.ui.pushButton_effect.clicked.connect(self.search_effect)
self.ui.commandLinkButton.clicked.connect(self.about)
# 获得音源列表txt文件所在地址
def get_txtPos(self):
gamePos = self.ui.lineEdit_gamePath.text()
txtPos = self.ui.lineEdit_savePath.text()
if txtPos == "":
txtPos = gamePos + "/miles_audio/audio_list.txt"
else:
if txtPos[-1] == '/':
txtPos = txtPos + "audio_list.txt"
else:
txtPos = txtPos + "/audio_list.txt"
return txtPos
def scanThreadFunc(self):
thread = Thread(target=self.on_scan)
thread.start()
self.ui.pushButton_scan.setEnabled(False)
# 点击扫描按钮,调用MSD获得音频列表,写入txt文件
def on_scan(self):
try:
self.ui.listWidget.clear()
gamePos = self.ui.lineEdit_gamePath.text()
print(gamePos)
headPos = gamePos[0:2]
txtPos = self.get_txtPos()
print(headPos, txtPos)
audioPos = self.ui.lineEdit_audioPath.text()
if not os.path.isfile(gamePos + "\\MSD.exe"):
QMessageBox.critical(self, "错误", "请检查MSD.exe文件是否在相应目录下")
return
cmdMSD = ""
if audioPos != "":
audioPos = '.' + audioPos
cmdMSD = ''' %s & cd "%s" & .\\msd --folder=%s 0 1 & .\\msd --folder=%s -l > "%s" ''' \
% (headPos, gamePos, audioPos, audioPos, txtPos)
else:
cmdMSD = ''' %s & cd "%s" & .\\msd 0 1 & .\\msd -l > "%s" ''' % (headPos, gamePos, txtPos)
print(cmdMSD)
self.log.logger.info(cmdMSD)
q = os.popen(cmdMSD)
q.close()
cnt = 0
with open(txtPos, 'r') as flist:
for cnt, _ in enumerate(flist):
pass
cnt += 1
self.ui.label_nAudio.setText(str(cnt))
self.ui.spinBox.setMaximum(cnt - 1)
self.ui.pushButton_opentxt.setEnabled(True)
self.ui.pushButton_readtxt.setEnabled(True)
except Exception as e:
print(e)
self.log.logger.error(e)
QMessageBox.critical(self, "错误", "请确认路径参数正确后重试!", QMessageBox.Close)
# 从txt文件中读取音频名称,展示到列表中,并初始化搜索框
def read_txtlist(self):
txtPos = self.get_txtPos()
self.qList.clear()
# 获得所有音频名称的列表
with open(txtPos, 'r') as flist:
for line in flist:
self.qList.append(line)
self.ui.listWidget.addItems(self.qList)
# 连接信号:双击调用MSD播放音频
self.ui.listWidget.itemDoubleClicked.connect(self.play_one_aduio)
# 启用查找按钮
self.ui.pushButton_index.setEnabled(True)
self.ui.pushButton_diag.setEnabled(True)
self.ui.pushButton_effect.setEnabled(True)
self.ui.pushButton_down.setEnabled(True)
self.init_diag_combo()
# 防止重复读取,读取后禁用按钮
self.ui.pushButton_readtxt.setEnabled(False)
# 按照序号搜索
def index_search(self):
# 获得数字框的数字
id = int(self.ui.spinBox.text())
# 清除列表中的所有项目,再加入新项目
self.ui.listWidget.clear()
self.ui.listWidget.addItem(self.qList[id])
# 允许重新读取整个列表
self.ui.pushButton_readtxt.setEnabled(True)
# 打开选择文件夹窗口
def open_file_game(self):
filepath = QtWidgets.QFileDialog.getExistingDirectory(self, "选取文件夹", ".")
print(filepath)
self.ui.lineEdit_gamePath.setText(filepath)
def open_file_txtsave(self):
filepath = QtWidgets.QFileDialog.getExistingDirectory(self, "选取文件夹", ".")
print(filepath)
self.ui.lineEdit_savePath.setText(filepath)
# 用默认程序打开txt文件
def open_txtlist(self):
gamePos = self.ui.lineEdit_gamePath.text()
txt = self.ui.lineEdit_savePath.text()
if txt == "":
txt = gamePos + "\\miles_audio"
head = txt[0:2]
command = '''%s & \
cd %s & \
start .\\audio_list.txt
''' % (head, txt)
try:
p_res = os.popen(command)
print(p_res.read())
except Exception as e:
print(e)
# 播放一次音频(会将wav文件存储到默认音频目录)
def play_one_aduio(self):
thread = Thread(target=self.playThreadFunc)
thread.start()
QMessageBox.information(self, "请稍等", "正在播放抓取的音频")
def playThreadFunc(self):
try:
gamePos = self.ui.lineEdit_gamePath.text()
headPos = gamePos[0:2]
item = self.ui.listWidget.selectedItems()[0]
id = item.text().split(',')[0]
print(id)
audioPos = self.ui.lineEdit_audioPath.text()
cmdMSD = ""
if audioPos != "":
audioPos = '.' + audioPos
cmdMSD = '''%s & \
cd %s & \
.\\msd --folder=%s %s
''' % (headPos, gamePos, audioPos, id)
else:
cmdMSD = '''%s & \
cd %s & \
.\\msd %s
''' % (headPos, gamePos, id)
p_res = os.popen(cmdMSD)
print(p_res.read())
except Exception as e:
print(e)
self.log.logger.error(e)
def download_mes(self):
thread = Thread(target=self.DownThreadFunc)
thread.start()
# 另存音频到指定地址
def DownThreadFunc(self):
filepath = QtWidgets.QFileDialog.getExistingDirectory(self, "选择保存文件路径", ".")
print(filepath)
try:
gamePos = self.ui.lineEdit_gamePath.text()
headPos = gamePos[0:2]
item = self.ui.listWidget.selectedItems()[0]
id = item.text().split(',')[0]
print(id)
audioPos = self.ui.lineEdit_audioPath.text()
cmdMSD = ""
if audioPos != "":
audioPos = '.' + audioPos
cmdMSD = '''%s & \
cd %s & \
.\\msd -m --folder=%s --out=%s %s
''' % (headPos, gamePos, audioPos, filepath, id)
else:
cmdMSD = '''%s & \
cd %s & \
.\\msd -m --out=%s %s
''' % (headPos, gamePos, filepath, id)
p_res = os.popen(cmdMSD)
if p_res.read() != "":
os.popen("start %s" % filepath)
except Exception as e:
print(e)
self.log.logger.error(e)
def on_audiopath_changed(self):
self.ui.pushButton_scan.setEnabled(True)
self.ui.pushButton_readtxt.setEnabled(False)
self.ui.pushButton_index.setEnabled(False)
self.ui.pushButton_diag.setEnabled(False)
self.ui.pushButton_effect.setEnabled(False)
self.ui.pushButton_down.setEnabled(False)
def init_diag_combo(self):
legendList = []
for each in self.qList:
# print(each)
# 匹配传奇名最小字符长度为3,别问我之前为啥是4,问就是出了ash这个怪物:(
result = re.search(r"[0-9]+,diag_(ap|mp)_([a-zA-Z]{3,})_", each)
if result is not None:
l = result.group(2)
# print(each, l)
if l not in legendList:
legendList.append(l)
self.ui.comboBox_legend.addItems(legendList)
def legend_chosen(self):
actionList = []
self.ui.comboBox_action.clear()
legend = self.ui.comboBox_legend.currentText()
for each in self.qList:
# print(each)
result = re.search(r"[0-9]+,diag_(ap|mp)_%s_([a-zA-Z]+)_" % legend, each)
if result is not None:
l = result.group(2)
# print(each, l)
if l not in actionList:
actionList.append(l)
actionList.append("[None]")
self.ui.comboBox_action.addItems(actionList)
def search_diag(self):
legend = self.ui.comboBox_legend.currentText()
action = self.ui.comboBox_action.currentText()
key = self.ui.lineEdit_diag.text()
items = []
result = ""
for each in self.qList:
if action == "[None]":
result = re.search(r"([0-9]+),diag_(ap|mp)_%s_.*%s.*" % (legend, key), each, flags=re.IGNORECASE)
else:
result = re.search(r"([0-9]+),diag_(ap|mp)_%s_%s_.*%s.*" % (legend, action, key), each,
flags=re.IGNORECASE)
if result is not None:
id = int(result.group(1))
if id not in items:
items.append(self.qList[id])
self.ui.listWidget.clear()
self.ui.listWidget.addItems(items)
# 允许重新读取整个列表
self.ui.pushButton_readtxt.setEnabled(True)
def search_effect(self):
key = self.ui.lineEdit_effect.text()
items = []
for each in self.qList:
result = re.search(r"([0-9]+),((?!diag).)*%s((?!diag).)*" % key, each, flags=re.IGNORECASE)
if result is not None:
id = int(result.group(1))
if id not in items:
items.append(self.qList[id])
self.ui.listWidget.clear()
self.ui.listWidget.addItems(items)
# 允许重新读取整个列表
self.ui.pushButton_readtxt.setEnabled(True)
def about(self):
QMessageBox.about(self, "关于", "Apex 音频提取器v0.2 \n Github:Nick-bit233")
if __name__ == '__main__':
app = QApplication([])
wid = mainForm()
wid.show()
app.exec_()