Advertisement
Guest User

Work with plugins

a guest
Feb 1st, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.51 KB | None | 0 0
  1.         List<IEffectPlugin> plugs;
  2.  
  3.         private void LoadPlugins(string from)
  4.         {
  5.             if (!Directory.Exists(from))
  6.             {
  7.                 AddEmptyItem(effectToolStripMenuItem);
  8.                 return;
  9.             }
  10.             var dlls = Directory.GetFiles(from, "*.dll").ToList().Where(s => Path.GetFileNameWithoutExtension(s) != "PlugAPI");
  11.             plugs = new List<IEffectPlugin>();
  12.             foreach (var s in dlls) LoadDll(s);
  13.             if (effectToolStripMenuItem.DropDownItems.Count == 0) AddEmptyItem(effectToolStripMenuItem);
  14.         }
  15.  
  16.         private void LoadDll(string dll)
  17.         {
  18.             Assembly a = Assembly.LoadFrom(dll);
  19.             foreach (var t in a.GetTypes())
  20.                 if (t.IsClass && !t.IsAbstract && t.GetConstructors().ToList().Exists(c => c.GetParameters().Length == 0))
  21.                 {
  22.                     try
  23.                     {
  24.                         object obj = Activator.CreateInstance(t);
  25.                         if (obj is IEffectPlugin)
  26.                         {
  27.                             IEffectPlugin iplug = (IEffectPlugin)obj;
  28.                             plugs.Add(iplug);
  29.                             ToolStripMenuItem strip = new ToolStripMenuItem();
  30.                             strip.Name = "effect_" + iplug.Name.Replace(" ", "");
  31.                             strip.Text = iplug.Name;
  32.                             strip.Click += effectClicked;
  33.  
  34.                             strip.DropDownItems.Add(new ToolStripMenuItem
  35.                             {
  36.                                 Text = "Author: " + iplug.Author +
  37.                                        "\nName: " + iplug.Name +
  38.                                        "\nDescription: " + iplug.Description +
  39.                                        "\nVersion: " + iplug.Version.ToString(),
  40.                                 Enabled = false
  41.                             });
  42.  
  43.                             bool finded = false;
  44.                             foreach (ToolStripItem ts in effectToolStripMenuItem.DropDownItems)
  45.                                 if (ts.Text.Equals(iplug.SubMenuName))
  46.                                 {
  47.                                     (ts as ToolStripMenuItem).DropDownItems.Add(strip);
  48.                                     finded = true;
  49.                                     break;
  50.                                 }
  51.                             if (!finded)
  52.                             {
  53.                                 var ts = (ToolStripMenuItem)effectToolStripMenuItem.DropDownItems.Add(iplug.SubMenuName);
  54.                                 ts.DropDownItems.Add(strip);
  55.                             }
  56.                         }
  57.                     }
  58.                     catch { continue; }
  59.                 }
  60.         }
  61.  
  62.         private void AddEmptyItem(ToolStripMenuItem toolit)
  63.         {
  64.             ToolStripMenuItem strip = new ToolStripMenuItem();
  65.             strip.Name = "effect_empty";
  66.             strip.Text = "Empty";
  67.             strip.Enabled = false;
  68.             toolit.DropDownItems.Add(strip);
  69.         }
  70.  
  71.         private void effectClicked(object sender, EventArgs e)
  72.         {
  73.             string effName = (sender as ToolStripMenuItem).Text;
  74.             var pl = plugs.Find(ip => ip.Name.Equals(effName));
  75.             if (pl != null && pictureBox1.Image != null)
  76.             {
  77.                 Image img = (Image)pictureBox1.Image.Clone(), img0 = null;
  78.                 this.Enabled = false;
  79.                 var setsForm = AutoGui.Gui.Create(pl.GetType());
  80.                 if (setsForm.ShowDialog() == DialogResult.OK)
  81.                 {
  82.                     this.Text += " [Working...]";
  83.                     img0 = (setsForm.Result as IEffectPlugin).ApplyEffect(img);
  84.                     this.Text = this.Text.Replace(" [Working...]", "");
  85.  
  86.                     pictureBox1.Image = (Image)img0.Clone();
  87.                     (listBox1.SelectedItem as FSFNameIC).Image = img0;
  88.                 }
  89.                 this.Enabled = true;
  90.                 /*using (var progressForm = new ShowProgress())
  91.                 {
  92.                     th.Start();
  93.                     progressForm.Left = this.Left + (this.Width - progressForm.Width) / 2;
  94.                     progressForm.Top = this.Top + (this.Height - progressForm.Height) / 2;
  95.                     progressForm.Show();
  96.                     while (!pl.Completed) progressForm.ProgressValue = pl.ProgressValue;
  97.                     progressForm.Hide();
  98.                 }*/
  99.             }
  100.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement