kotonebot 0.4.0__py3-none-any.whl → 0.6.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/backend/context/context.py +1002 -1002
- kotonebot/backend/core.py +6 -49
- kotonebot/backend/image.py +36 -5
- kotonebot/backend/loop.py +222 -208
- kotonebot/backend/ocr.py +7 -1
- kotonebot/client/device.py +108 -243
- kotonebot/client/host/__init__.py +34 -3
- kotonebot/client/host/adb_common.py +7 -9
- kotonebot/client/host/custom.py +6 -2
- kotonebot/client/host/leidian_host.py +2 -7
- kotonebot/client/host/mumu12_host.py +2 -7
- kotonebot/client/host/protocol.py +4 -3
- kotonebot/client/implements/__init__.py +62 -11
- kotonebot/client/implements/adb.py +5 -1
- kotonebot/client/implements/nemu_ipc/__init__.py +4 -0
- kotonebot/client/implements/uiautomator2.py +6 -2
- kotonebot/client/implements/windows.py +7 -3
- kotonebot/client/registration.py +1 -1
- kotonebot/client/scaler.py +467 -0
- kotonebot/config/base_config.py +1 -1
- kotonebot/config/config.py +61 -0
- kotonebot/core/__init__.py +13 -0
- kotonebot/core/entities/base.py +182 -0
- kotonebot/core/entities/compound.py +75 -0
- kotonebot/core/entities/ocr.py +117 -0
- kotonebot/core/entities/template_match.py +198 -0
- kotonebot/devtools/__init__.py +42 -0
- kotonebot/devtools/cli/__init__.py +6 -0
- kotonebot/devtools/cli/main.py +53 -0
- kotonebot/devtools/project/project.py +41 -0
- kotonebot/devtools/project/scanner.py +202 -0
- kotonebot/devtools/project/schema.py +99 -0
- kotonebot/devtools/resgen/__init__.py +42 -0
- kotonebot/devtools/resgen/codegen.py +331 -0
- kotonebot/devtools/resgen/core.py +94 -0
- kotonebot/devtools/resgen/parsers.py +360 -0
- kotonebot/devtools/resgen/utils.py +158 -0
- kotonebot/devtools/resgen/validation.py +115 -0
- kotonebot/devtools/web/dist/assets/bootstrap-icons-BOrJxbIo.woff +0 -0
- kotonebot/devtools/web/dist/assets/bootstrap-icons-BtvjY1KL.woff2 +0 -0
- kotonebot/devtools/web/dist/assets/ext-language_tools-CD021WJ2.js +2577 -0
- kotonebot/devtools/web/dist/assets/index-B_m5f2LF.js +2836 -0
- kotonebot/devtools/web/dist/assets/index-BlEDyGGa.css +9 -0
- kotonebot/devtools/web/dist/assets/language-client-C9muzqaq.js +128 -0
- kotonebot/devtools/web/dist/assets/mode-python-CtHp76XS.js +476 -0
- kotonebot/devtools/web/dist/icons/symbol-class.svg +3 -0
- kotonebot/devtools/web/dist/icons/symbol-file.svg +3 -0
- kotonebot/devtools/web/dist/icons/symbol-method.svg +3 -0
- kotonebot/devtools/web/dist/index.html +25 -0
- kotonebot/devtools/web/server/__init__.py +0 -0
- kotonebot/devtools/web/server/rest_api.py +217 -0
- kotonebot/devtools/web/server/server.py +85 -0
- kotonebot/errors.py +7 -2
- kotonebot/interop/win/__init__.py +10 -1
- kotonebot/interop/win/_mouse.py +311 -0
- kotonebot/interop/win/shake_mouse.py +224 -0
- kotonebot/primitives/__init__.py +3 -1
- kotonebot/primitives/geometry.py +817 -40
- kotonebot/primitives/visual.py +81 -1
- kotonebot/ui/pushkit/image_host.py +2 -1
- kotonebot/ui/pushkit/wxpusher.py +2 -1
- {kotonebot-0.4.0.dist-info → kotonebot-0.6.0.dist-info}/METADATA +4 -1
- kotonebot-0.6.0.dist-info/RECORD +105 -0
- kotonebot-0.6.0.dist-info/entry_points.txt +2 -0
- kotonebot/client/implements/adb_raw.py +0 -159
- kotonebot-0.4.0.dist-info/RECORD +0 -70
- /kotonebot/{tools → devtools}/mirror.py +0 -0
- /kotonebot/{tools → devtools/project}/__init__.py +0 -0
- {kotonebot-0.4.0.dist-info → kotonebot-0.6.0.dist-info}/WHEEL +0 -0
- {kotonebot-0.4.0.dist-info → kotonebot-0.6.0.dist-info}/licenses/LICENSE +0 -0
- {kotonebot-0.4.0.dist-info → kotonebot-0.6.0.dist-info}/top_level.txt +0 -0
kotonebot/backend/core.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import logging
|
|
2
|
-
from
|
|
3
|
-
from typing import Callable
|
|
2
|
+
from typing import TYPE_CHECKING, Callable
|
|
4
3
|
from typing_extensions import deprecated
|
|
5
4
|
|
|
6
5
|
import cv2
|
|
@@ -8,7 +7,11 @@ from cv2.typing import MatLike
|
|
|
8
7
|
|
|
9
8
|
from kotonebot.util import cv2_imread
|
|
10
9
|
from kotonebot.primitives import RectTuple, Rect, Point
|
|
11
|
-
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from kotonebot.primitives.visual import Image
|
|
12
|
+
else:
|
|
13
|
+
from kotonebot.primitives.visual import Image as _PrimitivesImage
|
|
14
|
+
Image = deprecated('Use kotonebot.primitives.Image instead.')(_PrimitivesImage)
|
|
12
15
|
|
|
13
16
|
@deprecated('unused')
|
|
14
17
|
class Ocr:
|
|
@@ -21,52 +24,6 @@ class Ocr:
|
|
|
21
24
|
self.text = text
|
|
22
25
|
self.language = language
|
|
23
26
|
|
|
24
|
-
# TODO: 这个类和 kotonebot.primitives.Image 重复了
|
|
25
|
-
@deprecated('Use kotonebot.primitives.Image instead.')
|
|
26
|
-
class Image:
|
|
27
|
-
def __init__(
|
|
28
|
-
self,
|
|
29
|
-
*,
|
|
30
|
-
path: str | None = None,
|
|
31
|
-
name: str | None = 'untitled',
|
|
32
|
-
data: MatLike | None = None,
|
|
33
|
-
):
|
|
34
|
-
self.path = path
|
|
35
|
-
self.name = name
|
|
36
|
-
self.__data: MatLike | None = data
|
|
37
|
-
self.__data_with_alpha: MatLike | None = None
|
|
38
|
-
|
|
39
|
-
@cache
|
|
40
|
-
def binary(self) -> 'Image':
|
|
41
|
-
return Image(data=cv2.cvtColor(self.data, cv2.COLOR_BGR2GRAY))
|
|
42
|
-
|
|
43
|
-
@property
|
|
44
|
-
def data(self) -> MatLike:
|
|
45
|
-
if self.__data is None:
|
|
46
|
-
if self.path is None:
|
|
47
|
-
raise ValueError('Either path or data must be provided.')
|
|
48
|
-
self.__data = cv2_imread(self.path)
|
|
49
|
-
if self.__data is None:
|
|
50
|
-
raise ResourceFileMissingError(self.path, 'sprite')
|
|
51
|
-
logger.debug(f'Read image "{self.name}" from {self.path}')
|
|
52
|
-
return self.__data
|
|
53
|
-
|
|
54
|
-
@property
|
|
55
|
-
def data_with_alpha(self) -> MatLike:
|
|
56
|
-
if self.__data_with_alpha is None:
|
|
57
|
-
if self.path is None:
|
|
58
|
-
raise ValueError('Either path or data must be provided.')
|
|
59
|
-
self.__data_with_alpha = cv2_imread(self.path, cv2.IMREAD_UNCHANGED)
|
|
60
|
-
if self.__data_with_alpha is None:
|
|
61
|
-
raise ResourceFileMissingError(self.path, 'sprite with alpha')
|
|
62
|
-
logger.debug(f'Read image "{self.name}" from {self.path}')
|
|
63
|
-
return self.__data_with_alpha
|
|
64
|
-
|
|
65
|
-
def __repr__(self) -> str:
|
|
66
|
-
if self.path is None:
|
|
67
|
-
return f'<Image: memory>'
|
|
68
|
-
else:
|
|
69
|
-
return f'<Image: "{self.name}" at {self.path}>'
|
|
70
27
|
|
|
71
28
|
# TODO: 这里的其他类应该移动到 primitives 模块下面
|
|
72
29
|
class HintBox(Rect):
|
kotonebot/backend/image.py
CHANGED
|
@@ -161,11 +161,19 @@ def template_match(
|
|
|
161
161
|
# 统一参数
|
|
162
162
|
template = unify_image(template, transparent)
|
|
163
163
|
image = unify_image(image)
|
|
164
|
+
th, tw = template.shape[:2]
|
|
165
|
+
ih, iw = image.shape[:2]
|
|
166
|
+
if th > ih or tw > iw:
|
|
167
|
+
raise ValueError(f"Template size ({tw}x{th}) is larger than image size ({iw}x{ih}).")
|
|
164
168
|
|
|
165
169
|
# 处理矩形区域
|
|
166
170
|
original_image = image
|
|
167
171
|
if rect is not None:
|
|
168
172
|
x, y, w, h = rect.xywh
|
|
173
|
+
if h < th or w < tw:
|
|
174
|
+
raise ValueError(
|
|
175
|
+
f"rect size ({w}x{h}) is smaller than template size ({tw}x{th})."
|
|
176
|
+
)
|
|
169
177
|
image = image[y:y+h, x:x+w]
|
|
170
178
|
|
|
171
179
|
if transparent is True and mask is not None:
|
|
@@ -186,12 +194,35 @@ def template_match(
|
|
|
186
194
|
if mask is not None:
|
|
187
195
|
mask = preprocessor.process(mask)
|
|
188
196
|
# 匹配模板
|
|
189
|
-
if
|
|
190
|
-
#
|
|
191
|
-
#
|
|
192
|
-
|
|
197
|
+
if not colored:
|
|
198
|
+
# 当 colored=False 时,使用灰度匹配以忽略颜色差异并提高速度
|
|
199
|
+
# 准备灰度图用于匹配
|
|
200
|
+
if image.ndim == 3:
|
|
201
|
+
img_for_match = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
202
|
+
else:
|
|
203
|
+
img_for_match = image
|
|
204
|
+
|
|
205
|
+
if template.ndim == 3:
|
|
206
|
+
tpl_for_match = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
|
|
207
|
+
else:
|
|
208
|
+
tpl_for_match = template
|
|
209
|
+
|
|
210
|
+
# 如果有 mask,确保为单通道二值掩码
|
|
211
|
+
if mask is not None:
|
|
212
|
+
if mask.ndim == 3:
|
|
213
|
+
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
|
|
214
|
+
mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
|
|
215
|
+
# 使用带 mask 的归一化相关性方法
|
|
216
|
+
result = cv2.matchTemplate(img_for_match, tpl_for_match, cv2.TM_CCORR_NORMED, mask=mask)
|
|
217
|
+
else:
|
|
218
|
+
result = cv2.matchTemplate(img_for_match, tpl_for_match, cv2.TM_CCOEFF_NORMED)
|
|
193
219
|
else:
|
|
194
|
-
|
|
220
|
+
if mask is not None:
|
|
221
|
+
# https://stackoverflow.com/questions/35642497/python-opencv-cv2-matchtemplate-with-transparency
|
|
222
|
+
# 使用 Mask 时,必须使用 TM_CCORR_NORMED 方法
|
|
223
|
+
result = cv2.matchTemplate(image, template, cv2.TM_CCORR_NORMED, mask=mask)
|
|
224
|
+
else:
|
|
225
|
+
result = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
|
|
195
226
|
|
|
196
227
|
# ========== 整理结果 ==========
|
|
197
228
|
# 去重、排序、转换为 TemplateMatchResult
|
kotonebot/backend/loop.py
CHANGED
|
@@ -1,208 +1,222 @@
|
|
|
1
|
-
import time
|
|
2
|
-
from functools import lru_cache, partial
|
|
3
|
-
from typing import Callable, Any, overload, Literal, Generic, TypeVar, cast, get_args, get_origin
|
|
4
|
-
from typing_extensions import deprecated
|
|
5
|
-
|
|
6
|
-
from cv2.typing import MatLike
|
|
7
|
-
|
|
8
|
-
from kotonebot.
|
|
9
|
-
from kotonebot import
|
|
10
|
-
from kotonebot
|
|
11
|
-
from kotonebot.backend.
|
|
12
|
-
from kotonebot.
|
|
13
|
-
from .
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
self.
|
|
20
|
-
self.
|
|
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
|
-
self.
|
|
81
|
-
self.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
self.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
self.
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
self.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
self.
|
|
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
|
-
self.
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
def
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
1
|
+
import time
|
|
2
|
+
from functools import lru_cache, partial
|
|
3
|
+
from typing import Callable, Any, overload, Literal, Generic, TypeVar, cast, get_args, get_origin
|
|
4
|
+
from typing_extensions import deprecated
|
|
5
|
+
|
|
6
|
+
from cv2.typing import MatLike
|
|
7
|
+
|
|
8
|
+
from kotonebot.config.config import conf
|
|
9
|
+
from kotonebot.util import Interval
|
|
10
|
+
from kotonebot import device, image, ocr
|
|
11
|
+
from kotonebot.backend.core import Image
|
|
12
|
+
from kotonebot.backend.ocr import TextComparator
|
|
13
|
+
from kotonebot.client.protocol import ClickableObjectProtocol
|
|
14
|
+
from .context import vars
|
|
15
|
+
|
|
16
|
+
@deprecated('No longer used.')
|
|
17
|
+
class LoopAction:
|
|
18
|
+
def __init__(self, loop: 'Loop', func: Callable[[], ClickableObjectProtocol | None]):
|
|
19
|
+
self.loop = loop
|
|
20
|
+
self.func = func
|
|
21
|
+
self.result: ClickableObjectProtocol | None = None
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def found(self):
|
|
25
|
+
"""
|
|
26
|
+
是否找到结果。若父 Loop 未在运行中,则返回 False。
|
|
27
|
+
"""
|
|
28
|
+
if not self.loop.running:
|
|
29
|
+
return False
|
|
30
|
+
return bool(self.result)
|
|
31
|
+
|
|
32
|
+
def __bool__(self):
|
|
33
|
+
return self.found
|
|
34
|
+
|
|
35
|
+
def reset(self):
|
|
36
|
+
"""
|
|
37
|
+
重置 LoopAction,以复用此对象。
|
|
38
|
+
"""
|
|
39
|
+
self.result = None
|
|
40
|
+
|
|
41
|
+
def do(self):
|
|
42
|
+
"""
|
|
43
|
+
执行 LoopAction。
|
|
44
|
+
:return: 执行结果。
|
|
45
|
+
"""
|
|
46
|
+
if not self.loop.running:
|
|
47
|
+
return
|
|
48
|
+
if self.loop.found_anything:
|
|
49
|
+
# 本轮循环已执行任意操作,因此不需要再继续检测
|
|
50
|
+
return
|
|
51
|
+
self.result = self.func()
|
|
52
|
+
if self.result:
|
|
53
|
+
self.loop.found_anything = True
|
|
54
|
+
|
|
55
|
+
def click(self, *, at: tuple[int, int] | None = None):
|
|
56
|
+
"""
|
|
57
|
+
点击寻找结果。若结果为空,会跳过执行。
|
|
58
|
+
|
|
59
|
+
:return:
|
|
60
|
+
"""
|
|
61
|
+
if self.result:
|
|
62
|
+
if at is not None:
|
|
63
|
+
device.click(*at)
|
|
64
|
+
else:
|
|
65
|
+
device.click(self.result)
|
|
66
|
+
|
|
67
|
+
def call(self, func: Callable[[ClickableObjectProtocol], Any]):
|
|
68
|
+
pass
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class Loop:
|
|
72
|
+
def __init__(
|
|
73
|
+
self,
|
|
74
|
+
*,
|
|
75
|
+
timeout: float = 300,
|
|
76
|
+
interval: float = 0.3,
|
|
77
|
+
auto_screenshot: bool = True,
|
|
78
|
+
skip_first_wait: bool = True
|
|
79
|
+
):
|
|
80
|
+
self.running = True
|
|
81
|
+
self.found_anything = False
|
|
82
|
+
self.auto_screenshot = auto_screenshot
|
|
83
|
+
"""
|
|
84
|
+
是否在每次循环开始时(Loop.tick() 被调用时)截图。
|
|
85
|
+
"""
|
|
86
|
+
self.__last_loop: float = -1
|
|
87
|
+
self.interval = interval
|
|
88
|
+
"""每次循环后等待的时间。"""
|
|
89
|
+
self.screenshot: MatLike | None = None
|
|
90
|
+
"""上次截图时的图像数据。"""
|
|
91
|
+
self.__skip_first_wait = skip_first_wait
|
|
92
|
+
self.__is_first_tick = True
|
|
93
|
+
|
|
94
|
+
def __iter__(self):
|
|
95
|
+
self.__is_first_tick = True
|
|
96
|
+
vars.flow.check()
|
|
97
|
+
return self
|
|
98
|
+
|
|
99
|
+
def __next__(self):
|
|
100
|
+
if not self.running:
|
|
101
|
+
raise StopIteration
|
|
102
|
+
self.found_anything = False
|
|
103
|
+
self.__last_loop = time.time()
|
|
104
|
+
return self.tick()
|
|
105
|
+
|
|
106
|
+
def tick(self):
|
|
107
|
+
if not (self.__is_first_tick and self.__skip_first_wait):
|
|
108
|
+
time.sleep(self.interval)
|
|
109
|
+
self.__is_first_tick = False
|
|
110
|
+
|
|
111
|
+
if self.auto_screenshot:
|
|
112
|
+
self.screenshot = device.screenshot()
|
|
113
|
+
self.__last_loop = time.time()
|
|
114
|
+
self.found_anything = False
|
|
115
|
+
# 执行全局回调
|
|
116
|
+
callbacks = conf().loop.loop_callbacks
|
|
117
|
+
while True:
|
|
118
|
+
did = False
|
|
119
|
+
for cb in callbacks:
|
|
120
|
+
did = cb(self)
|
|
121
|
+
if did:
|
|
122
|
+
time.sleep(self.interval)
|
|
123
|
+
self.screenshot = device.screenshot()
|
|
124
|
+
break
|
|
125
|
+
if not did:
|
|
126
|
+
break
|
|
127
|
+
|
|
128
|
+
return self
|
|
129
|
+
|
|
130
|
+
def exit(self):
|
|
131
|
+
"""
|
|
132
|
+
结束循环。
|
|
133
|
+
"""
|
|
134
|
+
self.running = False
|
|
135
|
+
|
|
136
|
+
@overload
|
|
137
|
+
@deprecated('Use plain if statement instead.')
|
|
138
|
+
def when(self, condition: Image) -> LoopAction:
|
|
139
|
+
...
|
|
140
|
+
|
|
141
|
+
@overload
|
|
142
|
+
@deprecated('Use plain if statement instead.')
|
|
143
|
+
def when(self, condition: TextComparator) -> LoopAction:
|
|
144
|
+
...
|
|
145
|
+
|
|
146
|
+
@deprecated('Use plain if statement instead.')
|
|
147
|
+
def when(self, condition: Any):
|
|
148
|
+
"""
|
|
149
|
+
判断某个条件是否成立。
|
|
150
|
+
|
|
151
|
+
:param condition:
|
|
152
|
+
:return:
|
|
153
|
+
"""
|
|
154
|
+
if isinstance(condition, Image):
|
|
155
|
+
func = partial(image.find, condition)
|
|
156
|
+
elif isinstance(condition, TextComparator):
|
|
157
|
+
func = partial(ocr.find, condition)
|
|
158
|
+
else:
|
|
159
|
+
raise ValueError('Invalid condition type.')
|
|
160
|
+
la = LoopAction(self, func)
|
|
161
|
+
la.reset()
|
|
162
|
+
la.do()
|
|
163
|
+
return la
|
|
164
|
+
|
|
165
|
+
@deprecated('Use plain if statement instead.')
|
|
166
|
+
def until(self, condition: Any):
|
|
167
|
+
"""
|
|
168
|
+
当满足指定条件时,结束循环。
|
|
169
|
+
|
|
170
|
+
等价于 ``loop.when(...).call(lambda _: loop.exit())``
|
|
171
|
+
"""
|
|
172
|
+
return self.when(condition).call(lambda _: self.exit())
|
|
173
|
+
|
|
174
|
+
@deprecated('Use image.find() and device.click() instead.')
|
|
175
|
+
def click_if(self, condition: Any, *, at: tuple[int, int] | None = None):
|
|
176
|
+
"""
|
|
177
|
+
检测指定对象是否出现,若出现,点击该对象或指定位置。
|
|
178
|
+
|
|
179
|
+
``click_if()`` 等价于 ``loop.when(...).click(...)``。
|
|
180
|
+
|
|
181
|
+
:param condition: 检测目标。
|
|
182
|
+
:param at: 点击位置。若为 None,表示点击找到的目标。
|
|
183
|
+
"""
|
|
184
|
+
return self.when(condition).click(at=at)
|
|
185
|
+
|
|
186
|
+
StateType = TypeVar('StateType')
|
|
187
|
+
class StatedLoop(Loop, Generic[StateType]):
|
|
188
|
+
def __init__(
|
|
189
|
+
self,
|
|
190
|
+
states: list[Any] | None = None,
|
|
191
|
+
initial_state: StateType | None = None,
|
|
192
|
+
*,
|
|
193
|
+
timeout: float = 300,
|
|
194
|
+
interval: float = 0.3,
|
|
195
|
+
auto_screenshot: bool = True
|
|
196
|
+
):
|
|
197
|
+
self.__tmp_states = states
|
|
198
|
+
self.__tmp_initial_state = initial_state
|
|
199
|
+
self.state: StateType
|
|
200
|
+
super().__init__(timeout=timeout, interval=interval, auto_screenshot=auto_screenshot)
|
|
201
|
+
|
|
202
|
+
def __iter__(self):
|
|
203
|
+
# __retrive_state_values() 只能在非 __init__ 中调用
|
|
204
|
+
self.__retrive_state_values()
|
|
205
|
+
return super().__iter__()
|
|
206
|
+
|
|
207
|
+
def __retrive_state_values(self):
|
|
208
|
+
# HACK: __orig_class__ 是 undocumented 属性
|
|
209
|
+
if not hasattr(self, '__orig_class__'):
|
|
210
|
+
# 如果 Foo 不是以参数化泛型的方式实例化的,可能没有 __orig_class__
|
|
211
|
+
if self.state is None:
|
|
212
|
+
raise ValueError('Either specify `states` or use StatedLoop[Literal[...]] syntax.')
|
|
213
|
+
else:
|
|
214
|
+
generic_type_args = get_args(self.__orig_class__) # type: ignore
|
|
215
|
+
if len(generic_type_args) != 1:
|
|
216
|
+
raise ValueError('StatedLoop must have exactly one generic type argument.')
|
|
217
|
+
state_values = get_args(generic_type_args[0])
|
|
218
|
+
if not state_values:
|
|
219
|
+
raise ValueError('StatedLoop must have at least one state value.')
|
|
220
|
+
self.states = cast(tuple[StateType, ...], state_values)
|
|
221
|
+
self.state = self.__tmp_initial_state or self.states[0]
|
|
222
|
+
return state_values
|
kotonebot/backend/ocr.py
CHANGED
|
@@ -36,9 +36,15 @@ def sanitize_text(text: str) -> str:
|
|
|
36
36
|
"""
|
|
37
37
|
对识别结果进行清理。此函数将被所有 OCR 引擎调用。
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
默认行为为先将文本 `Unicode 规范化`_,然后使用 `global_character_mapping` 中的映射数据进行清理。
|
|
40
40
|
可以重写此函数以实现自定义的清理逻辑。
|
|
41
|
+
|
|
42
|
+
.. note::
|
|
43
|
+
Unicode 规范化最常见的一个行为是将全角字符转换为半角字符。
|
|
44
|
+
|
|
45
|
+
.. _Unicode 规范化: https://docs.python.org/zh-cn/3.14/library/unicodedata.html#unicodedata.normalize
|
|
41
46
|
"""
|
|
47
|
+
text = unicodedata.normalize('NFKC', text)
|
|
42
48
|
for k, v in global_character_mapping.items():
|
|
43
49
|
text = text.replace(k, v)
|
|
44
50
|
return text
|