Skip to content

Commit e77ad37

Browse files
committed
..
1 parent c48a3b9 commit e77ad37

File tree

1 file changed

+134
-119
lines changed

1 file changed

+134
-119
lines changed

src/main.py

Lines changed: 134 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -52,92 +52,106 @@ def on_ready_read(socket: QLocalSocket):
5252

5353

5454
def cli_check_file(file_path: str) -> int:
55+
output_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "out.json")
5556
result = {
5657
"error": "",
5758
"data": []
5859
}
5960

60-
if not os.path.exists(file_path):
61-
result["error"] = "file not found"
62-
else:
63-
# 搜集目录或文件下的所有evf和evfs文件
64-
evf_evfs_files = []
65-
if os.path.isfile(file_path) and (
66-
file_path.endswith(".evf") or file_path.endswith(".evfs")
67-
):
68-
evf_evfs_files = [os.path.abspath(file_path)]
69-
elif os.path.isdir(file_path):
70-
evf_evfs_files = [
71-
os.path.abspath(os.path.join(root, file))
72-
for root, dirs, files in os.walk(file_path)
73-
for file in files
74-
if file.endswith(".evf") or file.endswith(".evfs")
75-
]
76-
77-
if not evf_evfs_files:
78-
result["error"] = "must be evf or evfs files or directory"
61+
try:
62+
if not os.path.exists(file_path):
63+
result["error"] = "file not found"
7964
else:
80-
# 实例化一个MineSweeperGUI出来
81-
app = QtWidgets.QApplication(sys.argv)
82-
mainWindow = mainWindowGUI.MainWindow()
83-
ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv)
65+
# 搜集目录或文件下的所有evf和evfs文件
66+
evf_evfs_files = []
67+
if os.path.isfile(file_path) and (
68+
file_path.endswith(".evf") or file_path.endswith(".evfs")
69+
):
70+
evf_evfs_files = [os.path.abspath(file_path)]
71+
elif os.path.isdir(file_path):
72+
evf_evfs_files = [
73+
os.path.abspath(os.path.join(root, file))
74+
for root, dirs, files in os.walk(file_path)
75+
for file in files
76+
if file.endswith(".evf") or file.endswith(".evfs")
77+
]
8478

85-
for ide, e in enumerate(evf_evfs_files):
86-
if not ui.checksum_module_ok():
87-
result["error"] = "checksum module error"
88-
break
89-
if e.endswith(".evf"):
90-
# 检验evf文件是否合法
91-
video = ms.EvfVideo(e)
92-
try:
93-
video.parse()
94-
except:
95-
evf_evfs_files[ide] = (e, 2)
96-
else:
97-
checksum = ui.checksum_guard.get_checksum(
98-
video.raw_data[: -(len(video.checksum) + 2)]
99-
)
100-
if video.checksum == checksum:
101-
evf_evfs_files[ide] = (e, 0)
102-
else:
103-
evf_evfs_files[ide] = (e, 1)
104-
elif e.endswith(".evfs"):
105-
# 检验evfs文件是否合法
106-
videos = ms.Evfs(e)
107-
try:
108-
videos.parse()
109-
except:
110-
evf_evfs_files[ide] = (e, 2)
111-
else:
112-
if videos.len() <= 0:
113-
evf_evfs_files[ide] = (e, 2)
114-
checksum = ui.checksum_guard.get_checksum(
115-
videos[0].evf_video.raw_data)
116-
if video.checksum != checksum:
117-
evf_evfs_files[ide] = (e, 1)
118-
continue
119-
for idcell, cell in enumerate(videos[1:]):
120-
checksum = ui.checksum_guard.get_checksum(
121-
cell.evf_video.raw_data + videos[idcell - 1].checksum
122-
)
123-
if cell.evf_video.checksum != checksum:
124-
evf_evfs_files[ide] = (e, 1)
125-
continue
126-
evf_evfs_files[ide] = (e, 0)
127-
128-
# 将结果转换为列表格式
129-
if not result["error"]: # 只有在没有错误的情况下才添加数据
130-
data_list = []
131-
for item in evf_evfs_files:
132-
if isinstance(item, tuple) and len(item) == 2:
133-
file_path_item, status = item
134-
data_list.append({"file": file_path_item, "status": status})
135-
result["data"] = data_list
136-
137-
# 写入out.json
138-
output_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "out.json")
139-
with open(output_file, "w", encoding="utf-8") as f:
140-
json.dump(result, f, ensure_ascii=False, indent=2)
79+
if not evf_evfs_files:
80+
result["error"] = "must be evf or evfs files or directory"
81+
else:
82+
try:
83+
# 设置环境变量以禁用GUI窗口(headless模式)
84+
os.environ["QT_QPA_PLATFORM"] = "offscreen"
85+
86+
# 实例化一个MineSweeperGUI出来
87+
app = QtWidgets.QApplication(sys.argv)
88+
mainWindow = mainWindowGUI.MainWindow()
89+
ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv)
90+
91+
for ide, e in enumerate(evf_evfs_files):
92+
if not ui.checksum_module_ok():
93+
result["error"] = "checksum module error"
94+
break
95+
if e.endswith(".evf"):
96+
# 检验evf文件是否合法
97+
video = ms.EvfVideo(e)
98+
try:
99+
video.parse()
100+
except:
101+
evf_evfs_files[ide] = (e, 2)
102+
else:
103+
checksum = ui.checksum_guard.get_checksum(
104+
video.raw_data[: -(len(video.checksum) + 2)]
105+
)
106+
if video.checksum == checksum:
107+
evf_evfs_files[ide] = (e, 0)
108+
else:
109+
evf_evfs_files[ide] = (e, 1)
110+
elif e.endswith(".evfs"):
111+
# 检验evfs文件是否合法
112+
videos = ms.Evfs(e)
113+
try:
114+
videos.parse()
115+
except:
116+
evf_evfs_files[ide] = (e, 2)
117+
else:
118+
if videos.len() <= 0:
119+
evf_evfs_files[ide] = (e, 2)
120+
checksum = ui.checksum_guard.get_checksum(
121+
videos[0].evf_video.raw_data)
122+
if video.checksum != checksum:
123+
evf_evfs_files[ide] = (e, 1)
124+
continue
125+
for idcell, cell in enumerate(videos[1:]):
126+
checksum = ui.checksum_guard.get_checksum(
127+
cell.evf_video.raw_data + videos[idcell - 1].checksum
128+
)
129+
if cell.evf_video.checksum != checksum:
130+
evf_evfs_files[ide] = (e, 1)
131+
continue
132+
evf_evfs_files[ide] = (e, 0)
133+
134+
# 将结果转换为列表格式
135+
if not result["error"]: # 只有在没有错误的情况下才添加数据
136+
data_list = []
137+
for item in evf_evfs_files:
138+
if isinstance(item, tuple) and len(item) == 2:
139+
file_path_item, status = item
140+
data_list.append({"file": file_path_item, "status": status})
141+
result["data"] = data_list
142+
except Exception as e:
143+
result["error"] = f"check error: {str(e)}"
144+
result["data"] = []
145+
except Exception as e:
146+
result["error"] = f"exception: {str(e)}"
147+
result["data"] = []
148+
finally:
149+
# 无论如何都要写入json文件
150+
try:
151+
with open(output_file, "w", encoding="utf-8") as f:
152+
json.dump(result, f, ensure_ascii=False, indent=2)
153+
except:
154+
pass
141155

142156
return 0
143157

@@ -154,50 +168,51 @@ def cli_check_file(file_path: str) -> int:
154168
exit_code = cli_check_file(args.check)
155169
sys.exit(exit_code)
156170

157-
app = QtWidgets.QApplication(sys.argv)
158-
serverName = "MineSweeperServer"
159-
socket = QLocalSocket()
160-
socket.connectToServer(serverName)
161-
if socket.waitForConnected(500):
162-
if len(sys.argv) == 2:
163-
filePath = sys.argv[1]
164-
socket.write(filePath.encode())
165-
socket.flush()
166-
time.sleep(0.5)
167-
app.quit()
168171
else:
169-
localServer = QLocalServer()
170-
localServer.listen(serverName)
171-
localServer.newConnection.connect(
172-
lambda: on_new_connection(localServer=localServer)
173-
)
174-
env = patch_env()
175-
context = AppContext(name="Metasweeper", version="1.0.0", display_name="元扫雷",
176-
plugin_dir=(Path(get_paths()) /
177-
"plugins").as_posix(),
178-
app_dir=get_paths()
179-
)
180-
PluginManager.instance().context = context
172+
app = QtWidgets.QApplication(sys.argv)
173+
serverName = "MineSweeperServer"
174+
socket = QLocalSocket()
175+
socket.connectToServer(serverName)
176+
if socket.waitForConnected(500):
177+
if len(sys.argv) == 2:
178+
filePath = sys.argv[1]
179+
socket.write(filePath.encode())
180+
socket.flush()
181+
time.sleep(0.5)
182+
app.quit()
183+
else:
184+
localServer = QLocalServer()
185+
localServer.listen(serverName)
186+
localServer.newConnection.connect(
187+
lambda: on_new_connection(localServer=localServer)
188+
)
189+
env = patch_env()
190+
context = AppContext(name="Metasweeper", version="1.0.0", display_name="元扫雷",
191+
plugin_dir=(Path(get_paths()) /
192+
"plugins").as_posix(),
193+
app_dir=get_paths()
194+
)
195+
PluginManager.instance().context = context
181196

182-
PluginManager.instance().start(Path(get_paths()) / "plugins", env)
183-
mainWindow = mainWindowGUI.MainWindow()
184-
ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv)
185-
ui.mainWindow.show()
186-
# ui.mainWindow.game_setting = ui.game_setting
197+
PluginManager.instance().start(Path(get_paths()) / "plugins", env)
198+
mainWindow = mainWindowGUI.MainWindow()
199+
ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv)
200+
ui.mainWindow.show()
201+
# ui.mainWindow.game_setting = ui.game_setting
187202

188-
# _translate = QtCore.QCoreApplication.translate
189-
hwnd = int(ui.mainWindow.winId())
203+
# _translate = QtCore.QCoreApplication.translate
204+
hwnd = int(ui.mainWindow.winId())
190205

191-
SetWindowDisplayAffinity = ctypes.windll.user32.SetWindowDisplayAffinity
192-
ui.disable_screenshot = lambda: ... if SetWindowDisplayAffinity(
193-
hwnd, 0x00000011) else 1/0
194-
ui.enable_screenshot = lambda: ... if SetWindowDisplayAffinity(
195-
hwnd, 0x00000000) else 1/0
196-
app.aboutToQuit.connect(PluginManager.instance().stop)
197-
sys.exit(app.exec_())
198-
...
199-
# except:
200-
# pass
206+
SetWindowDisplayAffinity = ctypes.windll.user32.SetWindowDisplayAffinity
207+
ui.disable_screenshot = lambda: ... if SetWindowDisplayAffinity(
208+
hwnd, 0x00000011) else 1/0
209+
ui.enable_screenshot = lambda: ... if SetWindowDisplayAffinity(
210+
hwnd, 0x00000000) else 1/0
211+
app.aboutToQuit.connect(PluginManager.instance().stop)
212+
sys.exit(app.exec_())
213+
...
214+
# except:
215+
# pass
201216

202217
# 最高优先级
203218
# 计时器快捷键切换

0 commit comments

Comments
 (0)