kotonebot 0.3.1__py3-none-any.whl → 0.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kotonebot/__init__.py +39 -39
- kotonebot/backend/bot.py +312 -302
- kotonebot/backend/color.py +525 -525
- kotonebot/backend/context/__init__.py +3 -3
- kotonebot/backend/context/context.py +49 -56
- kotonebot/backend/context/task_action.py +183 -175
- kotonebot/backend/core.py +129 -126
- kotonebot/backend/debug/entry.py +89 -89
- kotonebot/backend/debug/mock.py +78 -78
- kotonebot/backend/debug/server.py +222 -222
- kotonebot/backend/debug/vars.py +351 -351
- kotonebot/backend/dispatch.py +227 -227
- kotonebot/backend/flow_controller.py +196 -196
- kotonebot/backend/loop.py +12 -88
- kotonebot/backend/ocr.py +535 -529
- kotonebot/backend/preprocessor.py +103 -103
- kotonebot/client/__init__.py +9 -9
- kotonebot/client/device.py +528 -502
- kotonebot/client/fast_screenshot.py +377 -377
- kotonebot/client/host/__init__.py +43 -12
- kotonebot/client/host/adb_common.py +107 -94
- kotonebot/client/host/custom.py +118 -114
- kotonebot/client/host/leidian_host.py +196 -201
- kotonebot/client/host/mumu12_host.py +353 -358
- kotonebot/client/host/protocol.py +214 -213
- kotonebot/client/host/windows_common.py +58 -55
- kotonebot/client/implements/__init__.py +71 -7
- kotonebot/client/implements/adb.py +89 -85
- kotonebot/client/implements/adb_raw.py +162 -158
- kotonebot/client/implements/nemu_ipc/__init__.py +11 -7
- kotonebot/client/implements/nemu_ipc/external_renderer_ipc.py +284 -284
- kotonebot/client/implements/nemu_ipc/nemu_ipc.py +327 -327
- kotonebot/client/implements/remote_windows.py +188 -192
- kotonebot/client/implements/uiautomator2.py +85 -81
- kotonebot/client/implements/windows.py +176 -168
- kotonebot/client/protocol.py +69 -69
- kotonebot/client/registration.py +24 -24
- kotonebot/config/base_config.py +96 -96
- kotonebot/config/manager.py +36 -36
- kotonebot/errors.py +76 -71
- kotonebot/interop/win/__init__.py +10 -0
- kotonebot/interop/win/_mouse.py +311 -0
- kotonebot/interop/win/message_box.py +313 -313
- kotonebot/interop/win/reg.py +37 -37
- kotonebot/interop/win/shortcut.py +43 -43
- kotonebot/interop/win/task_dialog.py +513 -469
- kotonebot/logging/__init__.py +2 -2
- kotonebot/logging/log.py +17 -17
- kotonebot/primitives/__init__.py +17 -17
- kotonebot/primitives/geometry.py +862 -290
- kotonebot/primitives/visual.py +63 -63
- kotonebot/tools/mirror.py +354 -354
- kotonebot/ui/file_host/sensio.py +36 -36
- kotonebot/ui/file_host/tmp_send.py +54 -54
- kotonebot/ui/pushkit/__init__.py +3 -3
- kotonebot/ui/pushkit/image_host.py +88 -87
- kotonebot/ui/pushkit/protocol.py +13 -13
- kotonebot/ui/pushkit/wxpusher.py +54 -53
- kotonebot/ui/user.py +148 -143
- kotonebot/util.py +436 -409
- {kotonebot-0.3.1.dist-info → kotonebot-0.5.0.dist-info}/METADATA +82 -76
- kotonebot-0.5.0.dist-info/RECORD +71 -0
- {kotonebot-0.3.1.dist-info → kotonebot-0.5.0.dist-info}/licenses/LICENSE +673 -673
- kotonebot-0.3.1.dist-info/RECORD +0 -70
- {kotonebot-0.3.1.dist-info → kotonebot-0.5.0.dist-info}/WHEEL +0 -0
- {kotonebot-0.3.1.dist-info → kotonebot-0.5.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import time
|
|
3
|
+
from typing import Callable, Literal, TypedDict, overload, Tuple
|
|
4
|
+
|
|
5
|
+
import mouse
|
|
6
|
+
from typing_extensions import Unpack
|
|
7
|
+
|
|
8
|
+
from kotonebot.primitives import Point
|
|
9
|
+
|
|
10
|
+
MouseButton = Literal['left', 'right', 'middle']
|
|
11
|
+
TweenFunc = Callable[[float], float]
|
|
12
|
+
Tween = Literal['linear', 'ease_in', 'ease_out', 'ease_in_out'] | TweenFunc
|
|
13
|
+
|
|
14
|
+
# https://stackoverflow.com/a/76554895
|
|
15
|
+
def high_precision_sleep(duration):
|
|
16
|
+
start_time = time.perf_counter()
|
|
17
|
+
while True:
|
|
18
|
+
elapsed_time = time.perf_counter() - start_time
|
|
19
|
+
remaining_time = duration - elapsed_time
|
|
20
|
+
if remaining_time <= 0:
|
|
21
|
+
break
|
|
22
|
+
if remaining_time > 0.02: # Sleep for 5ms if remaining time is greater
|
|
23
|
+
time.sleep(max(remaining_time/2, 0.0001)) # Sleep for the remaining time or minimum sleep interval
|
|
24
|
+
else:
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
class AnimationParams(TypedDict, total=False):
|
|
28
|
+
duration: float
|
|
29
|
+
"""动画持续时间,单位为秒。
|
|
30
|
+
|
|
31
|
+
动画实际持续时间可能会略大于此值,具体取决于系统以及 delay_func 的实现。
|
|
32
|
+
"""
|
|
33
|
+
speed: float
|
|
34
|
+
"""动画速度,单位为像素/秒。"""
|
|
35
|
+
steps: int
|
|
36
|
+
"""动画步数。"""
|
|
37
|
+
tween: Tween
|
|
38
|
+
"""插值函数。
|
|
39
|
+
|
|
40
|
+
可选 'linear', 'ease_in', 'ease_out', 'ease_in_out',默认为 'ease_in_out'。
|
|
41
|
+
也可以是一个函数,其输入为动画进度,输出为动画值。
|
|
42
|
+
"""
|
|
43
|
+
delay_func: Callable[[float], None] | None
|
|
44
|
+
"""延时函数。默认为 time.sleep。
|
|
45
|
+
|
|
46
|
+
可选,如果提供了此参数,则会在每个动画点之间调用此函数。
|
|
47
|
+
"""
|
|
48
|
+
user_interrupt: Callable[[], bool | None] | Literal[True] | None
|
|
49
|
+
"""可选,用户中断函数,默认为 None。
|
|
50
|
+
|
|
51
|
+
若提供此参数,那么在动画执行时会检测用户输入,如果用户尝试移动鼠标,
|
|
52
|
+
会自动终止动画(传入 True)或调用此函数(传入 Callable,根据返回值决定是否继续)。
|
|
53
|
+
"""
|
|
54
|
+
user_interrupt_threshold: float | None
|
|
55
|
+
"""可选,用户中断阈值,单位为像素,默认为 None,表示使用全局参数。
|
|
56
|
+
|
|
57
|
+
如果提供此参数,则在检测用户中断时,仅当移动鼠标的距离大于此值时才会触发终止。
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
default_speed = 3000
|
|
61
|
+
animation_args: AnimationParams = {
|
|
62
|
+
'steps': 100,
|
|
63
|
+
'tween': 'ease_in_out',
|
|
64
|
+
'delay_func': high_precision_sleep,
|
|
65
|
+
'user_interrupt': None,
|
|
66
|
+
'user_interrupt_threshold': 30,
|
|
67
|
+
}
|
|
68
|
+
"""全局动画参数。"""
|
|
69
|
+
|
|
70
|
+
# https://easings.net
|
|
71
|
+
def _tween_linear(t: float) -> float:
|
|
72
|
+
return t
|
|
73
|
+
|
|
74
|
+
def _tween_ease_in(t: float) -> float:
|
|
75
|
+
return t * t
|
|
76
|
+
|
|
77
|
+
def _tween_ease_out(t: float) -> float:
|
|
78
|
+
return t * (2 - t)
|
|
79
|
+
|
|
80
|
+
def _tween_ease_in_out(t: float) -> float:
|
|
81
|
+
return t * t * (3 - 2 * t)
|
|
82
|
+
|
|
83
|
+
_TWEEN_FUNCTIONS = {
|
|
84
|
+
'linear': _tween_linear,
|
|
85
|
+
'ease_in': _tween_ease_in,
|
|
86
|
+
'ease_out': _tween_ease_out,
|
|
87
|
+
'ease_in_out': _tween_ease_in_out,
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
def _get_animated_points(start_point: Point, end_point: Point, steps: int, tween: Tween):
|
|
91
|
+
if isinstance(tween, str):
|
|
92
|
+
tween_func = _TWEEN_FUNCTIONS.get(tween, _tween_linear)
|
|
93
|
+
else:
|
|
94
|
+
tween_func = tween
|
|
95
|
+
for i in range(steps + 1):
|
|
96
|
+
progress = i / steps
|
|
97
|
+
eased_progress = tween_func(progress)
|
|
98
|
+
x = start_point.x + (end_point.x - start_point.x) * eased_progress
|
|
99
|
+
y = start_point.y + (end_point.y - start_point.y) * eased_progress
|
|
100
|
+
yield Point(int(x), int(y))
|
|
101
|
+
|
|
102
|
+
def do_tween(start: Point, end: Point, args: AnimationParams, *, skip_first: bool = True):
|
|
103
|
+
"""从起点到终点根据输入的动画参数进行插值,并自动进行延时。
|
|
104
|
+
|
|
105
|
+
:param start: 开始位置。
|
|
106
|
+
:param end: 结束位置。
|
|
107
|
+
:param args: 动画参数,详见 :class:`.AnimationParams`。
|
|
108
|
+
:param skip_first: 是否跳过第一个点。默认为 True。
|
|
109
|
+
:raises ValueError: 输入的 speed 为非正数时抛出。
|
|
110
|
+
:raises ValueError: 同时提供 speed 与 duration 参数时抛出。
|
|
111
|
+
:return: 一个迭代器,包含所有插值的中间点。
|
|
112
|
+
"""
|
|
113
|
+
duration = args.get('duration')
|
|
114
|
+
speed = args.get('speed')
|
|
115
|
+
steps = args.get('steps', animation_args.get('steps'))
|
|
116
|
+
tween = args.get('tween', animation_args.get('tween'))
|
|
117
|
+
delay_func = args.get('delay_func', animation_args.get('delay_func'))
|
|
118
|
+
user_interrupt = args.get('user_interrupt', animation_args.get('user_interrupt'))
|
|
119
|
+
user_interrupt_threshold = args.get('user_interrupt_threshold', animation_args.get('user_interrupt_threshold'))
|
|
120
|
+
assert steps is not None and tween is not None and delay_func is not None
|
|
121
|
+
|
|
122
|
+
def _speed_to_duration(speed: float) -> float:
|
|
123
|
+
return math.sqrt((end.x - start.x)**2 + (end.y - start.y)**2) / speed
|
|
124
|
+
|
|
125
|
+
if duration is None and speed is not None:
|
|
126
|
+
# 设置了速度,但没有设置时长,则计算时长
|
|
127
|
+
if speed <= 0:
|
|
128
|
+
raise ValueError('speed must be positive')
|
|
129
|
+
duration = _speed_to_duration(speed)
|
|
130
|
+
elif duration is not None and speed is None:
|
|
131
|
+
# 设置了时长,但没有设置速度,则计算速度
|
|
132
|
+
pass
|
|
133
|
+
elif duration is not None and speed is not None:
|
|
134
|
+
# 两个都设置
|
|
135
|
+
raise ValueError('duration and speed cannot be set at the same time')
|
|
136
|
+
else:
|
|
137
|
+
# 两个都没有设置,使用默认速度
|
|
138
|
+
duration = _speed_to_duration(default_speed)
|
|
139
|
+
|
|
140
|
+
delay = duration / steps if steps > 0 else 0
|
|
141
|
+
print(duration, steps, delay)
|
|
142
|
+
|
|
143
|
+
point_iterator = _get_animated_points(start, end, steps, tween)
|
|
144
|
+
if skip_first:
|
|
145
|
+
next(point_iterator, None)
|
|
146
|
+
|
|
147
|
+
# 调用函数 (drag/move) 负责设置鼠标位置。
|
|
148
|
+
# 在每次迭代中,我们检查当前鼠标位置是否与我们在“上一次”迭代中生成的位置匹配。
|
|
149
|
+
# 我们无法在此处知道初始鼠标位置,因此我们从第二个点开始检查。
|
|
150
|
+
|
|
151
|
+
iterator = iter(point_iterator)
|
|
152
|
+
try:
|
|
153
|
+
prev_pos = next(iterator)
|
|
154
|
+
yield prev_pos
|
|
155
|
+
delay_func(delay)
|
|
156
|
+
except StopIteration:
|
|
157
|
+
return
|
|
158
|
+
|
|
159
|
+
for pt in iterator:
|
|
160
|
+
# 检测用户中断
|
|
161
|
+
if user_interrupt:
|
|
162
|
+
pos = get_pos()
|
|
163
|
+
should_interrupt = False
|
|
164
|
+
if user_interrupt_threshold is not None:
|
|
165
|
+
if pos.distance_to(prev_pos) > user_interrupt_threshold:
|
|
166
|
+
should_interrupt = True
|
|
167
|
+
else:
|
|
168
|
+
if pos != prev_pos:
|
|
169
|
+
should_interrupt = True
|
|
170
|
+
|
|
171
|
+
if should_interrupt:
|
|
172
|
+
# 鼠标被用户移动,中断动画。
|
|
173
|
+
if user_interrupt is True:
|
|
174
|
+
return
|
|
175
|
+
if callable(user_interrupt):
|
|
176
|
+
if not user_interrupt():
|
|
177
|
+
return
|
|
178
|
+
|
|
179
|
+
yield pt
|
|
180
|
+
prev_pos = pt
|
|
181
|
+
delay_func(delay)
|
|
182
|
+
|
|
183
|
+
@overload
|
|
184
|
+
def set_pos(p: Point) -> None:
|
|
185
|
+
"""移动光标到指定位置。
|
|
186
|
+
|
|
187
|
+
:param p: 坐标,Point 实例。
|
|
188
|
+
"""
|
|
189
|
+
|
|
190
|
+
@overload
|
|
191
|
+
def set_pos(p: Tuple[int, int]) -> None:
|
|
192
|
+
"""移动光标到指定位置。
|
|
193
|
+
|
|
194
|
+
:param p: 坐标,二元 tuple[int, int]。
|
|
195
|
+
"""
|
|
196
|
+
|
|
197
|
+
@overload
|
|
198
|
+
def set_pos(p: int, y: int) -> None:
|
|
199
|
+
"""移动光标到指定位置。
|
|
200
|
+
|
|
201
|
+
:param p: 坐标,x 坐标。
|
|
202
|
+
:param y: 坐标,y 坐标。
|
|
203
|
+
"""
|
|
204
|
+
|
|
205
|
+
def set_pos(p, y: int | None = None):
|
|
206
|
+
if y is None:
|
|
207
|
+
# 可能是 Point 或二元 tuple
|
|
208
|
+
if isinstance(p, Point):
|
|
209
|
+
x = int(p.x)
|
|
210
|
+
y = int(p.y)
|
|
211
|
+
else:
|
|
212
|
+
# 假定为 (x, y)
|
|
213
|
+
_x, _y = p
|
|
214
|
+
x = int(_x)
|
|
215
|
+
y = int(_y)
|
|
216
|
+
else:
|
|
217
|
+
x = int(p)
|
|
218
|
+
y = int(y)
|
|
219
|
+
|
|
220
|
+
mouse.move(x, y)
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def get_pos() -> Point:
|
|
224
|
+
x, y = mouse.get_position()
|
|
225
|
+
return Point(x, y)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def down(button: MouseButton):
|
|
229
|
+
mouse.press(button)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def up(button: MouseButton):
|
|
233
|
+
mouse.release(button)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def click(button: MouseButton, *, duration: float = 0.1):
|
|
237
|
+
"""模拟鼠标点击。
|
|
238
|
+
|
|
239
|
+
:param button: 必填,鼠标按钮。
|
|
240
|
+
:param duration: 可选,点击持续时间。默认为 0.1。
|
|
241
|
+
"""
|
|
242
|
+
down(button)
|
|
243
|
+
time.sleep(duration)
|
|
244
|
+
up(button)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def drag(
|
|
248
|
+
start: Point,
|
|
249
|
+
end: Point,
|
|
250
|
+
*,
|
|
251
|
+
button: MouseButton | None = 'left',
|
|
252
|
+
**kargs: Unpack[AnimationParams],
|
|
253
|
+
):
|
|
254
|
+
"""模拟鼠标拖拽。
|
|
255
|
+
参数中与动画相关的部分详见 :class:`.AnimationParams`。
|
|
256
|
+
|
|
257
|
+
:param start: 必填,起点。
|
|
258
|
+
:param end: 必填,终点。
|
|
259
|
+
:param button: 可选,拖拽使用的鼠标按钮。默认为 `left`。None 表示不按下任何鼠标按钮,相当于只移动光标。
|
|
260
|
+
:param duration: 可选,动画持续时间。
|
|
261
|
+
:param speed: 可选,动画速度。
|
|
262
|
+
:param steps: 可选,动画步数。
|
|
263
|
+
:param tween: 可选,动画曲线。
|
|
264
|
+
:param delay_func: 可选,延时函数。详见 :class:`.AnimationParams`。
|
|
265
|
+
:param user_interrupt: 可选,用户中断函数。详见 :class:`.AnimationParams`。
|
|
266
|
+
:param user_interrupt_threshold: 可选,用户中断阈值。详见 :class:`.AnimationParams`。
|
|
267
|
+
"""
|
|
268
|
+
if button:
|
|
269
|
+
set_pos(start)
|
|
270
|
+
time.sleep(0.02)
|
|
271
|
+
down(button)
|
|
272
|
+
time.sleep(0.02)
|
|
273
|
+
|
|
274
|
+
try:
|
|
275
|
+
for p in do_tween(start, end, kargs):
|
|
276
|
+
set_pos(p)
|
|
277
|
+
pass
|
|
278
|
+
finally:
|
|
279
|
+
if button:
|
|
280
|
+
up(button)
|
|
281
|
+
|
|
282
|
+
def move(
|
|
283
|
+
start: Point,
|
|
284
|
+
end: Point,
|
|
285
|
+
/,
|
|
286
|
+
**kargs: Unpack[AnimationParams],
|
|
287
|
+
):
|
|
288
|
+
"""模拟鼠标移动。
|
|
289
|
+
参数中与动画相关的部分详见 :class:`kotonebot.interop.win.AnimationParams`。
|
|
290
|
+
|
|
291
|
+
:param start: 必填,起点。
|
|
292
|
+
:param end: 必填,终点。
|
|
293
|
+
:param duration: 可选,动画持续时间。
|
|
294
|
+
:param speed: 可选,动画速度。
|
|
295
|
+
:param steps: 可选,动画步数。
|
|
296
|
+
:param tween: 可选,动画曲线。
|
|
297
|
+
:param delay_func: 可选,延时函数。详见 :class:`.AnimationParams`。
|
|
298
|
+
:param user_interrupt: 可选,用户中断函数。详见 :class:`.AnimationParams`。
|
|
299
|
+
:param user_interrupt_threshold: 可选,用户中断阈值。详见 :class:`.AnimationParams`。
|
|
300
|
+
"""
|
|
301
|
+
drag(start, end, button=None, **kargs)
|
|
302
|
+
|
|
303
|
+
__all__ = [
|
|
304
|
+
'set_pos',
|
|
305
|
+
'get_pos',
|
|
306
|
+
'down',
|
|
307
|
+
'up',
|
|
308
|
+
'click',
|
|
309
|
+
'drag',
|
|
310
|
+
'move',
|
|
311
|
+
]
|