This repository has been archived by the owner on Nov 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
windows.py
274 lines (230 loc) · 7.3 KB
/
windows.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
import ctypes
import subprocess
import winreg
from time import sleep
import psutil
import pywintypes
import regex as re
import win32api
import win32con
import win32gui
import win32process
import winxpgui
from PIL import Image
from utils import (
Client,
ClientConfig,
MyFileHandler,
MyFormatter,
MyStreamHandler,
Path,
Settings,
Sg,
box,
ceil,
chunks,
compare,
copy,
datetime,
excepthook,
floor,
format_time,
get_font_name,
get_menu_pixels,
get_rotation,
get_text,
global_settings,
hdlr,
importlib,
json,
load_font,
log,
logger,
logging,
my_emit,
os,
requests,
rfh,
set_text,
sys,
timedelta,
)
BELOW_NORMAL_PRIORITY_CLASS = 0x4000 # Why is this not in win32con??? It has literally all other priority classes..
class NotRespondingError(Exception):
pass
class SteamExeNotFound(Exception):
pass
def get_window(title):
def window_enumeration_handler(hwnd, response):
if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
_, pid = win32process.GetWindowThreadProcessId(hwnd)
proc = psutil.Process(pid)
if proc.name() == title:
response.append((hwnd, proc))
res = []
win32gui.EnumWindows(window_enumeration_handler, res)
return res[0] if res else None
class SteamClient:
_WIN_REG_SHELL = (winreg.HKEY_CLASSES_ROOT, r"steam\Shell\Open\Command")
_PROCESS_NAME = "steam.exe"
def __init__(self):
self.path = self.find_exe()
self.cmd = f'"{self.path}" -applaunch 291550 -noeac'
# yoinked from battlenet extension for gog galaxy
@staticmethod
def __search_registry_for_run_cmd(*args):
try:
key = winreg.OpenKey(*args)
for i in range(1024):
try:
_, exe_cmd, _type = winreg.EnumValue(key, i)
if exe_cmd and _type == winreg.REG_SZ:
return exe_cmd
except OSError:
break
except FileNotFoundError:
return None
def _find_exe_running(self):
for proc in psutil.process_iter():
if proc.name() == self._PROCESS_NAME:
return proc.exe()
# yoinked from battlenet extension for gog galaxy
def _find_exe_registry(self):
shell_reg_value = self.__search_registry_for_run_cmd(*self._WIN_REG_SHELL)
if shell_reg_value is None:
return None
reg = re.compile('"(.*?)"')
return reg.search(shell_reg_value).groups()[0]
def find_exe(self):
res = self._find_exe_running() or self._find_exe_registry()
if not res:
raise SteamExeNotFound()
return res
def run_brawlhalla(self):
subprocess.Popen(self.cmd, creationflags=subprocess.DETACHED_PROCESS)
class BrawlhallaProcess:
def __init__(self, hwnd, proc):
self.window = hwnd
self.process = proc
@classmethod
def find(cls):
res = get_window("Brawlhalla.exe") or get_window(
"BrawlhallaGame.exe"
) # support for beta
if not res:
return None
return cls(*res)
@property
def responding(self):
cmd = f'tasklist /FI "PID eq {self.process.pid}" /FI "STATUS eq running"'
status = subprocess.check_output(cmd, creationflags=subprocess.DETACHED_PROCESS)
return str(self.process.pid) in str(status)
def kill(self):
while self.process.is_running():
self.process.kill()
sleep(0.5)
def get_window_rect(self):
return win32gui.GetWindowRect(self.window)
def get_window_size(self):
left, top, right, bot = self.get_window_rect()
return right - left, bot - top
def get_client_size(self):
left, top, right, bot = win32gui.GetClientRect(self.window)
return right - left, bot - top
@property
def fullscreen(self):
return self.get_client_size() == self.get_window_size()
def resize(self):
window_size = self.get_window_size()
client_size = self.get_client_size()
logger.debug("resize", *window_size, *client_size, *Sg.Window.get_screen_size())
w_border = window_size[0] - client_size[0]
h_border = window_size[1] - client_size[1]
while self.get_client_size() != (
1920,
1080,
): # getwindowsize or getclientsize or setwindowpos or something else is weird so it sometimes doesnt work first try
win32gui.SetWindowPos(
self.window,
0,
0,
0,
1920 + w_border,
1080 + h_border,
win32con.SWP_NOZORDER,
)
def move_off_screen(self):
logger.debug("move_offscreen")
w, h = Sg.Window.get_screen_size()
win32gui.SetWindowPos(
self.window,
0,
w * 4,
h * 4,
0,
0,
win32con.SWP_NOSIZE | win32con.SWP_NOZORDER,
)
def make_transparent(self):
style = win32gui.GetWindowLong(self.window, win32con.GWL_EXSTYLE)
win32gui.ShowWindow(self.window, win32con.SW_HIDE)
style |= (
win32con.WS_EX_COMPOSITED
| win32con.WS_EX_LAYERED
| win32con.WS_EX_TRANSPARENT
| win32con.WS_EX_TOOLWINDOW
| win32con.WS_EX_NOACTIVATE
)
style &= ~win32con.WS_EX_APPWINDOW
win32gui.SetWindowLong(self.window, win32con.GWL_EXSTYLE, style)
sleep(1)
win32gui.ShowWindow(self.window, win32con.SW_SHOW)
winxpgui.SetLayeredWindowAttributes(self.window, 0, 0, win32con.LWA_ALPHA)
def hide(self):
self.move_off_screen()
self.make_transparent()
def make_screenshot(self):
import win32ui
w, h = self.get_client_size()
window_dc = win32gui.GetWindowDC(self.window)
mfc_dc = win32ui.CreateDCFromHandle(window_dc)
save_dc = mfc_dc.CreateCompatibleDC()
save_bit_map = win32ui.CreateBitmap()
save_bit_map.CreateCompatibleBitmap(mfc_dc, w, h)
save_dc.SelectObject(save_bit_map)
ctypes.windll.user32.PrintWindow(self.window, save_dc.GetSafeHdc(), 1)
bmpinfo = save_bit_map.GetInfo()
bmpstr = save_bit_map.GetBitmapBits(True)
im = Image.frombuffer(
"RGB",
(bmpinfo["bmWidth"], bmpinfo["bmHeight"]),
bmpstr,
"raw",
"BGRX",
0,
1,
)
win32gui.DeleteObject(save_bit_map.GetHandle())
save_dc.DeleteDC()
mfc_dc.DeleteDC()
win32gui.ReleaseDC(self.window, window_dc)
return im
def set_low_priority(self):
handle = win32api.OpenProcess(
win32con.PROCESS_SET_INFORMATION, True, self.process.pid
)
win32process.SetPriorityClass(handle, BELOW_NORMAL_PRIORITY_CLASS)
win32api.CloseHandle(handle)
class Singleton:
def __init__(self):
res = get_window("BHBot.exe")
if res:
window, _ = res
self.set_focus(window)
sys.exit()
@staticmethod
def set_focus(window):
try:
win32gui.SetForegroundWindow(window)
except pywintypes.error:
pass