-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptEngine.cs
More file actions
431 lines (373 loc) · 17 KB
/
Copy pathScriptEngine.cs
File metadata and controls
431 lines (373 loc) · 17 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.CSharp;
namespace ADBFileManager.Scripting
{
/// <summary>
/// Contract that all C# script addons must implement.
/// </summary>
public interface IScript
{
string Name { get; }
string Description { get; }
string Author { get; }
string Version { get; }
void Initialize(IScriptContext context);
}
/// <summary>
/// API interface exposed to scripts to interact with ADB, file system, and UI.
/// </summary>
public interface IScriptContext
{
// Active State
string CurrentDeviceSerial { get; }
string CurrentPath { get; }
// ADB Commands
ExecutionResult RunAdbCommand(string args);
ExecutionResult RunAdbShell(string shellCommand);
StorageInfo GetStorageInfo(string remotePath = null);
// Remote File Operations
List<AdbFileInfo> GetFileList(string remotePath = null);
ExecutionResult PushFile(string localPath, string remotePath);
ExecutionResult PullFile(string remotePath, string localPath);
ExecutionResult CreateDirectory(string remotePath);
ExecutionResult CreateFile(string remotePath, string initialContent = "");
ExecutionResult DeleteItem(string remotePath);
ExecutionResult RenameItem(string oldPath, string newPath);
ExecutionResult ScanMedia(string remotePath);
ExecutionResult scanMedia(string remotePath);
// UI & Selection Access
List<AdbFileInfo> GetSelectedFiles();
void RefreshFileList();
// UI Extensibility & Dialogs
void RegisterContextMenu(string title, Action<List<AdbFileInfo>> action, string fileExtensionFilter = null, bool foldersOnly = false);
void RegisterContextMenu(string title, Action<List<AdbFileInfo>> action, string fileExtensionFilter, string fileNameFilter, string pathFilter, bool foldersOnly = false);
void RegisterToolsMenuItem(string title, Action action);
void ShowMessage(string message, string caption = "Script Notice");
bool ShowConfirmation(string message, string caption = "Script Question");
string PromptInput(string prompt, string title = "Script Input", string defaultValue = "");
void ShowTextPreview(string title, string content);
}
public class RegisteredContextMenu
{
public string Title { get; set; }
public Action<List<AdbFileInfo>> Action { get; set; }
public string FileExtensionFilter { get; set; }
public string FileNameFilter { get; set; }
public string PathFilter { get; set; }
public bool FoldersOnly { get; set; }
public IScript SourceScript { get; set; }
}
public class RegisteredMenuItem
{
public string Title { get; set; }
public Action Action { get; set; }
public IScript SourceScript { get; set; }
}
public class LoadedScriptInfo
{
public IScript ScriptInstance { get; set; }
public string FilePath { get; set; }
public string ScriptName { get { return ScriptInstance != null ? ScriptInstance.Name : Path.GetFileName(FilePath); } }
public string Description { get { return ScriptInstance != null ? ScriptInstance.Description : ""; } }
public string Author { get { return ScriptInstance != null ? ScriptInstance.Author : ""; } }
public string Version { get { return ScriptInstance != null ? ScriptInstance.Version : ""; } }
public List<string> CompilationErrors { get; set; }
public bool HasErrors { get { return CompilationErrors != null && CompilationErrors.Count > 0; } }
public LoadedScriptInfo()
{
CompilationErrors = new List<string>();
}
}
public class ScriptManager : IScriptContext
{
private readonly AdbService adbService;
private readonly Func<string> getDeviceSerialFunc;
private readonly Func<string> getCurrentPathFunc;
private readonly Func<List<AdbFileInfo>> getSelectedFilesFunc;
private readonly Action refreshFileListAction;
public List<LoadedScriptInfo> LoadedScripts { get; private set; }
public List<RegisteredContextMenu> ContextMenuItems { get; private set; }
public List<RegisteredMenuItem> ToolsMenuItems { get; private set; }
public ScriptManager(
AdbService adbService,
Func<string> getDeviceSerialFunc,
Func<string> getCurrentPathFunc,
Func<List<AdbFileInfo>> getSelectedFilesFunc,
Action refreshFileListAction)
{
this.adbService = adbService;
this.getDeviceSerialFunc = getDeviceSerialFunc;
this.getCurrentPathFunc = getCurrentPathFunc;
this.getSelectedFilesFunc = getSelectedFilesFunc;
this.refreshFileListAction = refreshFileListAction;
LoadedScripts = new List<LoadedScriptInfo>();
ContextMenuItems = new List<RegisteredContextMenu>();
ToolsMenuItems = new List<RegisteredMenuItem>();
}
public string CurrentDeviceSerial { get { return getDeviceSerialFunc != null ? getDeviceSerialFunc() : null; } }
public string CurrentPath { get { return getCurrentPathFunc != null ? getCurrentPathFunc() : "/sdcard/"; } }
public void LoadScripts(string scriptsFolder)
{
LoadedScripts.Clear();
ContextMenuItems.Clear();
ToolsMenuItems.Clear();
if (!Directory.Exists(scriptsFolder))
{
try
{
Directory.CreateDirectory(scriptsFolder);
}
catch { }
return;
}
string[] files = Directory.GetFiles(scriptsFolder, "*.cs", SearchOption.TopDirectoryOnly);
foreach (string file in files)
{
var scriptInfo = CompileAndInstantiateScript(file);
LoadedScripts.Add(scriptInfo);
if (scriptInfo.ScriptInstance != null)
{
try
{
scriptInfo.ScriptInstance.Initialize(this);
}
catch (Exception ex)
{
scriptInfo.CompilationErrors.Add("Runtime initialization error: " + ex.Message);
}
}
}
}
private LoadedScriptInfo CompileAndInstantiateScript(string filePath)
{
var info = new LoadedScriptInfo { FilePath = filePath };
try
{
using (var provider = new CSharpCodeProvider())
{
var parameters = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false,
TreatWarningsAsErrors = false
};
// Add framework assembly references
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("System.Core.dll");
parameters.ReferencedAssemblies.Add("System.Drawing.dll");
parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
parameters.ReferencedAssemblies.Add("System.IO.Compression.dll");
parameters.ReferencedAssemblies.Add("System.IO.Compression.FileSystem.dll");
// Add current running assembly (ADBFileManager.exe)
string currentAssemblyLocation = Assembly.GetExecutingAssembly().Location;
if (!string.IsNullOrEmpty(currentAssemblyLocation) && File.Exists(currentAssemblyLocation))
{
parameters.ReferencedAssemblies.Add(currentAssemblyLocation);
}
CompilerResults results = provider.CompileAssemblyFromFile(parameters, filePath);
if (results.Errors.HasErrors)
{
foreach (CompilerError err in results.Errors)
{
info.CompilationErrors.Add(string.Format("Line {0}, Col {1}: {2}", err.Line, err.Column, err.ErrorText));
}
return info;
}
Assembly asm = results.CompiledAssembly;
Type scriptInterfaceType = typeof(IScript);
foreach (Type type in asm.GetTypes())
{
if (scriptInterfaceType.IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
{
try
{
var instance = (IScript)Activator.CreateInstance(type);
info.ScriptInstance = instance;
break;
}
catch (Exception ex)
{
info.CompilationErrors.Add("Failed to create script instance: " + ex.Message);
}
}
}
if (info.ScriptInstance == null && !info.HasErrors)
{
info.CompilationErrors.Add("No class implementing IScript was found in file.");
}
}
}
catch (Exception ex)
{
info.CompilationErrors.Add("Compilation exception: " + ex.Message);
}
return info;
}
#region IScriptContext Implementation
public ExecutionResult RunAdbCommand(string args)
{
string serial = CurrentDeviceSerial;
string formattedArgs = args;
// If a device is selected and command doesn't explicitly start with -s, prepend -s DEVICEID
if (!string.IsNullOrEmpty(serial) && !args.TrimStart().StartsWith("-s "))
{
formattedArgs = string.Format("-s \"{0}\" {1}", serial, args);
}
return adbService.RunAdbCommand(formattedArgs);
}
public ExecutionResult RunAdbShell(string shellCommand)
{
string serial = CurrentDeviceSerial;
if (string.IsNullOrEmpty(serial))
{
return new ExecutionResult { ExitCode = -1, Error = "No ADB device selected." };
}
string cleanCmd = shellCommand.Replace("'", "'\\''");
return adbService.RunAdbCommand(string.Format("-s \"{0}\" shell \"{1}\"", serial, cleanCmd));
}
public StorageInfo GetStorageInfo(string remotePath = null)
{
string serial = CurrentDeviceSerial;
if (string.IsNullOrEmpty(serial)) return null;
string targetPath = string.IsNullOrEmpty(remotePath) ? CurrentPath : remotePath;
return adbService.GetStorageInfo(serial, targetPath);
}
public List<AdbFileInfo> GetFileList(string remotePath = null)
{
string serial = CurrentDeviceSerial;
if (string.IsNullOrEmpty(serial)) return new List<AdbFileInfo>();
string targetPath = string.IsNullOrEmpty(remotePath) ? CurrentPath : remotePath;
return adbService.ListDirectory(serial, targetPath);
}
public ExecutionResult PushFile(string localPath, string remotePath)
{
string serial = CurrentDeviceSerial;
if (string.IsNullOrEmpty(serial)) return new ExecutionResult { ExitCode = -1, Error = "No ADB device selected." };
return adbService.Push(serial, localPath, remotePath);
}
public ExecutionResult PullFile(string remotePath, string localPath)
{
string serial = CurrentDeviceSerial;
if (string.IsNullOrEmpty(serial)) return new ExecutionResult { ExitCode = -1, Error = "No ADB device selected." };
return adbService.Pull(serial, remotePath, localPath);
}
public ExecutionResult CreateDirectory(string remotePath)
{
string serial = CurrentDeviceSerial;
if (string.IsNullOrEmpty(serial)) return new ExecutionResult { ExitCode = -1, Error = "No ADB device selected." };
return adbService.CreateFolder(serial, remotePath);
}
public ExecutionResult CreateFile(string remotePath, string initialContent = "")
{
string serial = CurrentDeviceSerial;
if (string.IsNullOrEmpty(serial)) return new ExecutionResult { ExitCode = -1, Error = "No ADB device selected." };
return adbService.CreateFile(serial, remotePath, initialContent);
}
public ExecutionResult DeleteItem(string remotePath)
{
string serial = CurrentDeviceSerial;
if (string.IsNullOrEmpty(serial)) return new ExecutionResult { ExitCode = -1, Error = "No ADB device selected." };
return adbService.DeleteItem(serial, remotePath);
}
public ExecutionResult RenameItem(string oldPath, string newPath)
{
string serial = CurrentDeviceSerial;
if (string.IsNullOrEmpty(serial)) return new ExecutionResult { ExitCode = -1, Error = "No ADB device selected." };
return adbService.RenameItem(serial, oldPath, newPath);
}
public ExecutionResult ScanMedia(string remotePath)
{
string serial = CurrentDeviceSerial;
if (string.IsNullOrEmpty(serial)) return new ExecutionResult { ExitCode = -1, Error = "No ADB device selected." };
return adbService.ScanMedia(serial, remotePath);
}
public ExecutionResult scanMedia(string remotePath)
{
return ScanMedia(remotePath);
}
public List<AdbFileInfo> GetSelectedFiles()
{
return getSelectedFilesFunc != null ? getSelectedFilesFunc() : new List<AdbFileInfo>();
}
public void RefreshFileList()
{
if (refreshFileListAction != null)
{
if (Form.ActiveForm != null && Form.ActiveForm.InvokeRequired)
{
Form.ActiveForm.BeginInvoke(refreshFileListAction);
}
else
{
refreshFileListAction();
}
}
}
public static bool MatchesWildcard(string text, string pattern)
{
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(pattern)) return false;
pattern = pattern.Trim();
if (pattern == "*") return true;
string regexPattern = "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$";
return Regex.IsMatch(text, regexPattern, RegexOptions.IgnoreCase);
}
public void RegisterContextMenu(string title, Action<List<AdbFileInfo>> action, string fileExtensionFilter = null, bool foldersOnly = false)
{
RegisterContextMenu(title, action, fileExtensionFilter, null, null, foldersOnly);
}
public void RegisterContextMenu(string title, Action<List<AdbFileInfo>> action, string fileExtensionFilter, string fileNameFilter, string pathFilter, bool foldersOnly = false)
{
ContextMenuItems.Add(new RegisteredContextMenu
{
Title = title,
Action = action,
FileExtensionFilter = fileExtensionFilter,
FileNameFilter = fileNameFilter,
PathFilter = pathFilter,
FoldersOnly = foldersOnly
});
}
public void RegisterToolsMenuItem(string title, Action action)
{
ToolsMenuItems.Add(new RegisteredMenuItem
{
Title = title,
Action = action
});
}
public void ShowMessage(string message, string caption = "Script Notice")
{
MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public bool ShowConfirmation(string message, string caption = "Script Question")
{
return MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
}
public string PromptInput(string prompt, string title = "Script Input", string defaultValue = "")
{
using (var dlg = new PromptDialog(prompt, title, defaultValue))
{
if (dlg.ShowDialog() == DialogResult.OK)
{
return dlg.InputText;
}
}
return null;
}
public void ShowTextPreview(string title, string content)
{
using (var viewer = new FileViewerForm(title, content))
{
viewer.ShowDialog();
}
}
#endregion
}
}