-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluginMain.cs
635 lines (558 loc) · 17.3 KB
/
PluginMain.cs
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using WeifenLuo.WinFormsUI.Docking;
using LibraryDepot.Resources;
using PluginCore.Localization;
using PluginCore.Utilities;
using PluginCore.Managers;
using PluginCore.Helpers;
using PluginCore;
using ProjectManager.Projects;
using ProjectManager.Actions;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using ProjectManager.Controls.TreeView;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.Resources;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
//using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
//using System.Timers;
namespace LibraryDepot
{
public class PluginMain : IPlugin
{
private String pluginName = "LibraryDepot";
private String pluginGuid = "01B1E6B4-DA6E-4d3a-A1E7-AD65E4238C37";
private String pluginHelp = "www.flashdevelop.org/community/";
private String pluginDesc = "Library Depot utility plugin for FlashDevelop 3";
private String pluginAuth = "Jean-Louis PERSAT";
private String settingFilename;
private Settings settingObject;
private Image pluginImage;
private string __LibraryPath;
private ProjectTreeView __ptv;
private string[] __DisallowedDirectories = { ".svn", "_svn" };
private string[] __AllowedFiles = { ".as" };
//private System.Timers.Timer __timer;
private bool _stop;
private FileSystemWatcher watcher;
#region Required Properties
/// <summary>
/// Api level of the plugin
/// </summary>
public Int32 Api
{
get { return 1; }
}
/// <summary>
/// Name of the plugin
/// </summary>
public String Name
{
get { return this.pluginName; }
}
/// <summary>
/// GUID of the plugin
/// </summary>
public String Guid
{
get { return this.pluginGuid; }
}
/// <summary>
/// Author of the plugin
/// </summary>
public String Author
{
get { return this.pluginAuth; }
}
/// <summary>
/// Description of the plugin
/// </summary>
public String Description
{
get { return this.pluginDesc; }
}
/// <summary>
/// Web address for help
/// </summary>
public String Help
{
get { return this.pluginHelp; }
}
/// <summary>
/// Object that contains the settings
/// </summary>
[Browsable(false)]
public Object Settings
{
get { return settingObject; }
}
#endregion
#region Required Methods
/// <summary>
/// Initializes the plugin
/// </summary>
public void Initialize()
{
this.InitBasics();
this.LoadSettings();
}
/// <summary>
/// Disposes the plugin
/// </summary>
public void Dispose()
{
this.SaveSettings();
}
/// <summary>
/// Handles the incoming events
/// </summary>
public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority prority)
{
switch (e.Type)
{
case EventType.Command:
string cmd = (e as DataEvent).Action;
if (cmd == "ProjectManager.TreeSelectionChanged")
{
__ptv = (ProjectTreeView)sender;
BuildContextMenu(sender);
}
break;
}
}
/// <summary>
/// Nodes the selected.
/// </summary>
/// <param name="sender">The sender.</param>
private void BuildContextMenu(Object sender)
{
//MessageBox.Show("BuildContextMenu");
int __idx = 0;
int __savedpos = 0;
try
{
ToolStripItemCollection __tsi = __ptv.ContextMenuStrip.Items;
foreach (ToolStripItem __Item in __tsi)
{
if (__Item.Text == LocaleHelper.GetString("LibraryDepot.Menu.Title"))
{
// removing menu
__tsi.Remove(__Item);
// removing separator
__tsi.RemoveAt(__idx);
// saving position for recreating treemenu
__savedpos = __idx;
}
__idx++;
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
ToolStripMenuItem tsmi = new ToolStripMenuItem(LocaleHelper.GetString("LibraryDepot.Menu.Title"), PluginBase.MainForm.FindImage("205"));
BuildDirectoryMenu(tsmi, __LibraryPath);
BuildFileMenu(tsmi, __LibraryPath);
(sender as Control).ContextMenuStrip.Items.Insert(__savedpos, tsmi);
(sender as Control).ContextMenuStrip.Items.Insert(__savedpos + 1, new ToolStripSeparator());
/*if (settingObject.CheckTimer != TimerCheckInterval.Never)
{
__timer.Start();
}*/
}
#endregion
#region Custom Methods
/// <summary>
/// Initializes important variables
/// </summary>
public void InitBasics()
{
String dataPath = Path.Combine(PathHelper.DataDir, "LibraryDepot");
if (!Directory.Exists(dataPath)) Directory.CreateDirectory(dataPath);
this.settingFilename = Path.Combine(dataPath, "Settings.fdb");
this.pluginImage = PluginBase.MainForm.FindImage("106");
}
/// <summary>
/// Adds the required event handlers
/// </summary>
public void AddEventHandlers()
{
// Set events you want to listen (combine as flags)
EventManager.AddEventHandler(this, EventType.Command | EventType.FileSwitch | EventType.UIStarted);
}
/// <summary>
/// Initializes the localization of the plugin
/// </summary>
public void InitLocalization()
{
LocaleVersion locale = PluginBase.MainForm.Settings.LocaleVersion;
switch (locale)
{
/*
case LocaleVersion.fi_FI :
// We have Finnish available... or not. :)
LocaleHelper.Initialize(LocaleVersion.fi_FI);
break;
*/
default :
// Plugins should default to English...
LocaleHelper.Initialize(LocaleVersion.en_US);
break;
}
this.pluginDesc = LocaleHelper.GetString("Info.Description");
/*__timer = new System.Timers.Timer();
__timer.Elapsed += new ElapsedEventHandler(HandleTimerComplete);
if (settingObject.CheckTimer != TimerCheckInterval.Never)
{
__timer.Interval = (int)settingObject.CheckTimer;
}*/
}
/*private void HandleTimerComplete(object sender, ElapsedEventArgs e)
{
//MessageBox.Show("HandleTimerComplete:" + __ptv.ContextMenuStrip.Visible.ToString());
if (__ptv.ContextMenuStrip.Visible)
{
__timer.Stop();
__timer.Start();
}
else
{
__timer.Stop();
BuildContextMenu(__ptv);
}
}*/
/// <summary>
/// Loads the plugin settings
/// </summary>
public void LoadSettings()
{
settingObject = new Settings();
if (!File.Exists(this.settingFilename))
{
this.SaveSettings();
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.Description = "Select your library depot folder";
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
settingObject.LibraryPath = folderBrowserDialog.SelectedPath;
__LibraryPath = folderBrowserDialog.SelectedPath;
}
else
{
__LibraryPath = GetPath(PathHelper.AppDir) + "\\FlashDevelop\\Depot\\";
if (!Directory.Exists(__LibraryPath))
Directory.CreateDirectory(__LibraryPath);
settingObject.LibraryPath = __LibraryPath;
}
}
else
{
Object obj = ObjectSerializer.Deserialize(this.settingFilename, settingObject);
settingObject = (Settings)obj;
__LibraryPath = settingObject.LibraryPath;
}
//__LibraryPath = settingObject.LibraryPath;
//if (__LibraryPath == "")
//{
// __LibraryPath = GetPath(PathHelper.AppDir) + "\\FlashDevelop\\Depot\\";
// settingObject.LibraryPath = __LibraryPath;
//}
settingObject.Changed += SettingChanged;
watcher = new FileSystemWatcher();
watcher.Path = __LibraryPath;
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size;
watcher.Created += new FileSystemEventHandler(OnFileEvent);
watcher.Deleted += new FileSystemEventHandler(OnFileEvent);
watcher.Renamed += new RenamedEventHandler(OnRenameEvent);
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
this.AddEventHandlers();
this.InitLocalization();
}
public void OnFileEvent(object sender, FileSystemEventArgs fsea)
{
BuildContextMenu(__ptv);
}
public void OnRenameEvent(Object source, RenamedEventArgs rea)
{
BuildContextMenu(__ptv);
}
/// <summary>
/// Saves the plugin settings
/// </summary>
public void SaveSettings()
{
settingObject.Changed -= SettingChanged;
ObjectSerializer.Serialize(this.settingFilename, settingObject);
}
/// <summary>
/// Settings the changed.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="add">if set to <c>true</c> [add].</param>
private void SettingChanged(string type)
{
// recreate menu on LibraryPath change
switch(type)
{
case "LibraryPath":
__LibraryPath = settingObject.LibraryPath;
BuildContextMenu(__ptv);
break;
/*case "CheckTimer":
if (settingObject.CheckTimer != TimerCheckInterval.Never)
{
__timer.Interval = (int)settingObject.CheckTimer;
__timer.Start();
}
else
{
__timer.Stop();
}
break;*/
case "EnableWatcher":
watcher.EnableRaisingEvents = settingObject.EnableWatcher;
break;
}
}
private void BuildDirectoryMenu(ToolStripMenuItem __menu, string __path)
{
if (!Directory.Exists(__path)) return;
string[] __Directories = Directory.GetDirectories(__path);
ToolStripMenuItem tsmiDir = null;
foreach (string __DirectoryPath in __Directories)
{
DirectoryInfo __DirectoryInfo = new DirectoryInfo(__DirectoryPath);
tsmiDir = new ToolStripMenuItem(__DirectoryInfo.Name, PluginBase.MainForm.FindImage("203"));
BuildDirectoryMenu(tsmiDir, __DirectoryPath);
BuildFileMenu(tsmiDir, __DirectoryPath, true);
__menu.DropDownItems.Add(tsmiDir);
}
}
private void BuildFileMenu(ToolStripMenuItem __menu, string __path)
{
BuildFileMenu(__menu, __path, false);
}
private void BuildFileMenu(ToolStripMenuItem __menu, string __path, bool __checkexistingdir)
{
bool __hasItems = false;
string[] __Files = Directory.GetFiles(__path);
bool __isAllowed = false;
foreach (string __FilePath in __Files)
{
FileInfo __FileInfo = new FileInfo(__FilePath);
Image __picto = null;
switch (__FileInfo.Extension.ToLower())
{
case ".zip":
__isAllowed = true;
__hasItems = true;
//__picto = PluginBase.MainForm.FindImage("133");
__picto = PluginBase.MainForm.FindImage("274");
break;
case ".swc":
__isAllowed = true;
__hasItems = true;
//__picto = PluginBase.MainForm.FindImage("274");
Assembly assembly = Assembly.GetExecutingAssembly();
try
{
__picto = new Bitmap(assembly.GetManifestResourceStream("LibraryDepot.Resources.SwcFile.png"));
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
break;
default:
__isAllowed = false;
break;
}
if (__isAllowed)
__menu.DropDownItems.Add(new ToolStripMenuItem(__FileInfo.Name, __picto, new EventHandler(delegate
{
OutputLog(__FileInfo.Name);
ChooseCopyType(__FileInfo.FullName);
}
)));
}
if (__checkexistingdir)
if (!__hasItems)
__menu.Enabled = false;
}
private void ChooseCopyType(string __file)
{
FileInfo __FileInfo = new FileInfo(__file);
switch (__FileInfo.Extension.ToLower())
{
case ".zip":
ExtractLibrary(__file);
break;
case ".swc":
CopyLibrary(__file);
break;
}
}
private void ExtractLibrary(string __file)
{
bool __foundSRC = false;
using (ZipInputStream s = new ZipInputStream(File.OpenRead(__file)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
if (Path.GetDirectoryName(theEntry.Name).ToLower() == "src")
__foundSRC = true;
}
}
FastZipEvents events = new FastZipEvents();
events.ProcessFile = ProcessFileMethod;
FastZip __fastZip = new FastZip(events);
string __fileFilter = @"+\.as$";
string __directoryFilter;
if (__foundSRC)
{
__directoryFilter = @"src";
__fastZip.ExtractZip(__file, GetPath(), FastZip.Overwrite.Prompt, OverwritePrompt, __fileFilter, __directoryFilter, true);
//MessageBox.Show(GetPath() + "/classes/");
if (Directory.Exists(GetPath() + "/classes/"))
{
Directory.Move(GetPath() + "/src/", GetPath() + "/classes/");
}
else if (settingObject.SrcPath.Replace("/", "").Replace("\\", "").ToLower() != "src")
{
Directory.Move(GetPath() + "/src/", GetPath() + settingObject.SrcPath);
}
}
else
{
__directoryFilter = null;
//MessageBox.Show(Directory.Exists(GetPath() + "/classes/").ToString());
if (Directory.Exists(GetPath() + "/classes/"))
{
__fastZip.ExtractZip(__file, GetPath() + "/classes/", FastZip.Overwrite.Prompt, OverwritePrompt, __fileFilter, __directoryFilter, true);
}
else
{
__fastZip.ExtractZip(__file, GetPath() + settingObject.SrcPath, FastZip.Overwrite.Prompt, OverwritePrompt, __fileFilter, __directoryFilter, true);
}
}
//PluginBase.MainForm.EditorMenu.Hide();
/*using (ZipInputStream s = new ZipInputStream(File.OpenRead(__file)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
if (isDirectoryAllowed(directoryName))
{
string fileName = Path.GetFileName(theEntry.Name);
if (isFileAllowed(Path.GetExtension(theEntry.Name)))
{
// create directory
if (directoryName.Length > 0)
{
Directory.CreateDirectory(GetPath() + settingObject.SrcPath + directoryName);
}
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(GetPath() + settingObject.SrcPath + theEntry.Name))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
}
}
}*/
}
private bool OverwritePrompt(string fileName)
{
// In this method you can choose whether to overwrite a file.
DialogResult dr = MessageBox.Show("Overwrite " + fileName, "Overwrite?", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Cancel)
{
_stop = true;
// Must return true if we want to abort processing, so that the ProcessFileMethod will be called.
// When the ProcessFileMethod sets ContinueRunning false, processing will immediately stop.
return true;
}
return dr == DialogResult.Yes;
}
private void ProcessFileMethod(object sender, ScanEventArgs args)
{
string fileName = args.Name;
// To stop all further processing, set args.ContinueRunning = false
if (_stop)
{
args.ContinueRunning = false;
}
}
private void CopyLibrary(string __file)
{
FileInfo __FileInfo = new FileInfo(__file);
if (!Directory.Exists(GetPath() + settingObject.SwcPath))
Directory.CreateDirectory(GetPath() + settingObject.SwcPath);
File.Copy(__file, GetPath() + settingObject.SwcPath + __FileInfo.Name);
}
private void OutputLog(string __file)
{
TraceManager.Add(LocaleHelper.GetString("LibraryDepot.Log.Info") + __file);
}
private string GetPath()
{
return GetPath(PluginBase.CurrentProject.ProjectPath);
}
private string GetPath(string __path)
{
int __pos = __path.LastIndexOf("\\");
string __realpath = __path.Substring(0, __pos + 1);
return __realpath;
}
private bool isDirectoryAllowed(string __directory)
{
if (__directory.Length == 0)
return false;
foreach (string __item in __DisallowedDirectories)
{
if (__item.ToLower() == __directory.ToLower())
{
return false;
}
}
return true;
}
private bool isFileAllowed(string __fileext)
{
foreach (string __item in __AllowedFiles)
{
if (__item.ToLower() == __fileext.ToLower())
{
return true;
}
}
return false;
}
#endregion
}
}