forked from nscript-site/NScript.AndroidBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotClient.cs
More file actions
308 lines (275 loc) · 11.2 KB
/
BotClient.cs
File metadata and controls
308 lines (275 loc) · 11.2 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace NScript.AndroidBot
{
using Geb.Image;
using System.Xml;
using System.Xml.XPath;
using System.Drawing;
public class BotClient
{
#region Config FFmpeg
private static bool IsFFmpegConfigged;
private static void ConfigFFmpeg()
{
if (IsFFmpegConfigged == true) return;
String[] searchPaths = { "./lib", "./ffmpeg", "./lib/ffmpeg", "c://lib/ffmpeg", "d://lib/ffmpeg", "e://lib//ffmpeg" };
foreach (var path in searchPaths)
{
if (ConfigFFmpeg(path) != null) break;
}
IsFFmpegConfigged = true;
}
private static String ConfigFFmpeg(String dirPath)
{
DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
if (dirInfo.Exists == false) return null;
FileInfo[] files = dirInfo.GetFiles("avcodec*.dll");
if (files == null || files.Length == 0) return null;
FFmpeg.AutoGen.ffmpeg.RootPath = dirInfo.FullName;
return dirInfo.FullName;
}
#endregion
private Server Server { get; set; }
private MediaStream Stream { get; set; }
private Decoder Decoder { get; set; }
private FrameSink FrameSink { get; set; }
private Controller Controller { get; set; }
/// <summary>
/// 获得 xml 描述的当前界面内容。
/// </summary>
/// <param name="ignoreTextlessControls">是否忽略没有文本的控件</param>
/// <returns></returns>
public PageLayout GetScreenXml(bool ignoreTextlessControls = true)
{
String content = Server.DumpUI();
if (ignoreTextlessControls == true) content = LayoutUtils.ClearXmlContent(content);
return new PageLayout(content);
}
static char[] splitters = new char[] { '[', ']', ',' };
/// <summary>
/// 获得指定节点的 Bounds
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public Nullable<Rectangle> GetNodeBounds(XPathNavigator node)
{
// <node text="" resource-id="" content-desc="4G 手机信号满格。" bounds="[190,15][251,57]"></node>
if (node == null) return null;
String attr = node.GetAttribute("bounds", "");
if (attr == null) return null;
float scale = (float)Server.GetScale();
String[] terms = attr.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
if (terms.Length != 4) return null;
float x0, y0 = 0;
float x1, y1 = 0;
float.TryParse(terms[0], out x0);
float.TryParse(terms[1], out y0);
float.TryParse(terms[2], out x1);
float.TryParse(terms[3], out y1);
return new Rectangle((int)Math.Round(x0 * scale), (int)Math.Round(y0 * scale), (int)Math.Round((x1 - x0)*scale), (int)Math.Round((y1 - y0)*scale));
}
/// <summary>
/// Bot 选项
/// </summary>
public BotOptions Options { get; set; } = new BotOptions();
/// <summary>
/// 显示 Bot 相关信息
/// </summary>
public Action<String> OnMsg { get; set; }
/// <summary>
/// 媒体切片器
/// </summary>
public MediaSlicer Slicer { get; private set; } = new MediaSlicer();
/// <summary>
/// 显示画面的回调函数
/// </summary>
public Action<ImageBgr24> OnRender { get; set; }
/// <summary>
/// 音频数据的回调函数
/// </summary>
public Action<Byte[]> OnAudioDataReceive { get; set; }
/// <summary>
/// 获取当前画面截图
/// </summary>
/// <returns></returns>
public ImageBgr24 GetFameImage() { return FrameSink?.GetFrameImage(); }
public System.Drawing.Size FrameSize { get { return Server == null ? default(System.Drawing.Size) : Server.FrameSize; } }
/// <summary>
/// 向 Bot 发送消息
/// </summary>
/// <param name="msg"></param>
public BotClient Push(ControlMsg msg)
{
if (msg == null) return this;
Controller?.Push(msg);
return this;
}
/// <summary>
/// 发送鼠标事件
/// </summary>
/// <param name="type"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public BotClient Send(MouseEventType type, int x, int y)
{
return this.Push(new InjectTouchEventMsg(type, new Position(x, y, this.FrameSize)));
}
/// <summary>
/// 在指定的 node 上发送鼠标事件
/// </summary>
/// <param name="type"></param>
/// <param name="node"></param>
/// <returns></returns>
public BotClient Send(MouseEventType type, XPathNavigator node)
{
var bounds = GetNodeBounds(node);
int x = bounds.Value.X + bounds.Value.Width / 2;
int y = bounds.Value.Y + bounds.Value.Height / 2;
return this.Push(new InjectTouchEventMsg(type, new Position(x, y, this.FrameSize)));
}
/// <summary>
/// 发送鼠标事件
/// </summary>
/// <param name="type"></param>
/// <param name="point"></param>
/// <returns></returns>
public BotClient Send(MouseEventType type, System.Drawing.Point point)
{
return this.Push(new InjectTouchEventMsg(type, new Position(point.X, point.Y, this.FrameSize)));
}
public BotClient SendTouchMove(System.Drawing.Point pointFrom, System.Drawing.Point pointTo, double steps = 20)
{
this.Push(new InjectTouchEventMsg(MouseEventType.Down, new Position(pointFrom, this.FrameSize)));
double deltaX = (pointTo.X - pointFrom.X)/steps;
double deltaY = (pointTo.Y - pointFrom.Y)/steps;
this.SendWait(10);
for(int i = 0; i <= steps; i++)
{
int x = (int)Math.Round(pointFrom.X + deltaX * i);
int y = (int)Math.Round(pointFrom.Y + deltaY * i);
this.Push(new InjectTouchEventMsg(MouseEventType.Move, new Position(new System.Drawing.Point(x,y), this.FrameSize)));
this.SendWait(10);
}
this.Push(new InjectTouchEventMsg(MouseEventType.Up, new Position(pointFrom, this.FrameSize)));
return this;
}
/// <summary>
/// 发送点击事件
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public BotClient SendClick(int x, int y)
{
this.Push(new InjectTouchEventMsg(MouseEventType.Down, new Position(x, y, this.FrameSize)));
this.Push(new InjectTouchEventMsg(MouseEventType.Up, new Position(x, y, this.FrameSize)));
return this;
}
/// <summary>
/// 在指定的 node 上发送点击事件
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public BotClient SentClick(XPathNavigator node)
{
var bounds = GetNodeBounds(node);
int x = bounds.Value.X + bounds.Value.Width / 2;
int y = bounds.Value.Y + bounds.Value.Height / 2;
return SendClick(x, y);
}
public BotClient SendText(String content)
{
if (content == null) throw new ArgumentNullException("content");
return this.Push(new InjectTextMsg(content));
}
/// <summary>
/// 发送点击事件
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public BotClient SendClick(System.Drawing.Point point)
{
this.Push(new InjectTouchEventMsg(MouseEventType.Down, new Position(point.X, point.Y, this.FrameSize)));
this.Push(new InjectTouchEventMsg(MouseEventType.Up, new Position(point.X, point.Y, this.FrameSize)));
return this;
}
/// <summary>
/// 发送剪贴板消息
/// </summary>
/// <param name="content"></param>
/// <param name="post">设为true,则直接将剪贴板消息显示在当前框中</param>
/// <returns></returns>
public BotClient SendClipboardMsg(String content, bool post)
{
if (content == null) throw new ArgumentNullException("content");
SetClipboardMsg msg = new SetClipboardMsg(content, post);
return this.Push(msg);
}
/// <summary>
/// 后退
/// </summary>
/// <returns></returns>
public BotClient SendBack()
{
this.Push(new BackOrScreenOnMsg() { Action = AndroidKeyeventAction.AKEY_EVENT_ACTION_DOWN });
this.Push(new BackOrScreenOnMsg() { Action = AndroidKeyeventAction.AKEY_EVENT_ACTION_UP });
return this;
}
/// <summary>
/// 等待
/// </summary>
/// <param name="miniSeconds"></param>
/// <returns></returns>
public BotClient SendWait(int miniSeconds)
{
return this.Push(new WaitMsg(miniSeconds));
}
/// <summary>
/// 启动 Bot 运行
/// </summary>
public void Run()
{
ConfigFFmpeg();
BotOptions options = Options;
if (options == null) options = new BotOptions();
ServerParams serverParams = options.ToServerParams();
this.Server = new Server();
this.Server.OnMsg = this.OnMsg;
this.Server.Start(serverParams);
this.Slicer.FrameRate = serverParams.max_fps;
this.Decoder = new Decoder();
if(options.Display == true)
{
this.FrameSink = new FrameSink();
this.FrameSink.Width = this.Server.FrameSize.Width;
this.FrameSink.Height = this.Server.FrameSize.Height;
this.FrameSink.OnRender = (t) => { this.Slicer.Receive(t); this.OnRender?.Invoke(t); } ;
this.Decoder.AddSink(this.FrameSink);
}
this.Stream = new MediaStream();
this.Stream.OnMsg = this.OnMsg;
this.Stream.AddSink(this.Decoder);
this.Stream.ReceiveVideoSocket(this.Server.VideoSocket);
this.Stream.ReceiveAudioSocket(this.Server.AudioSocket, Options.MaxFps);
this.Stream.OnAudioDataReceive = (data) => { this.Slicer.Receive(data); };
this.Server.OnVideoSocketConnected = () => {
this.Stream.ReceiveVideoSocket(this.Server.VideoSocket);
};
this.Server.OnAudioSocketConnected = () =>
{
this.Stream.ReceiveAudioSocket(this.Server.AudioSocket, Options.MaxFps);
};
this.Controller = new Controller();
this.Controller.Bind(this.Server.ControlSocket);
this.Server.OnControlSocketConnected = () => {
this.Controller.Bind(this.Server.ControlSocket);
};
}
}
}