-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.py
77 lines (61 loc) · 1.98 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
# main.py
import multiprocessing as mp
import time
import sys
import signal
import cv2
from context import Context
from utils import change_window
import window
import grabscreen
from log import log
from process_handler import process
# from utils.change_window import *
# Event to control running state
running_event = mp.Event()
def signal_handler(sig, frame):
log.debug("Gracefully exiting...")
running_event.clear()
sys.exit(0)
def wait_for_game_window(running_event):
while running_event.is_set():
frame = grabscreen.grab_screen()
if frame is not None and window.set_windows_offset(frame):
log.debug("Game window detected and offsets set!")
return True
time.sleep(1)
return False
def main():
signal.signal(signal.SIGINT, signal_handler)
# Initialize camera
grabscreen.init_camera(target_fps=30)
change_window.correction_window()
if change_window.check_window_resolution_same(window.game_width, window.game_height) == False:
raise ValueError(
f"游戏分辨率和配置game_width({window.game_width}), game_height({window.game_height})不一致,请到window.py中修改"
)
running_event.set()
# Wait for game window
if not wait_for_game_window(running_event):
log.debug("Failed to detect game window.")
return
# Create and initialize Context
context = Context()
# Start child process
p_brain = mp.Process(target=process, args=(context, running_event))
p_brain.start()
try:
while running_event.is_set():
context.update_status()
except KeyboardInterrupt:
log.debug("Main process: Terminating child process...")
running_event.clear()
p_brain.terminate()
p_brain.join()
log.debug("Main process: Exiting.")
except Exception as e:
log.debug(f"An error occurred in main: {e}")
finally:
cv2.destroyAllWindows()
if __name__ == '__main__':
main()