OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
PluginManager.cs
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) Contributors, http://opensimulator.org/
4  * See CONTRIBUTORS.TXT for a full list of copyright holders.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the OpenSimulator Project nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 using System;
31 using System.Text;
32 using System.Linq;
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Collections.ObjectModel;
36 using Mono.Addins;
37 using Mono.Addins.Setup;
38 using Mono.Addins.Description;
39 using OpenSim.Framework;
40 
41 
42 namespace OpenSim.Framework
43 {
47  public class PluginManager : SetupService
48  {
49  public AddinRegistry PluginRegistry;
50 
51  public PluginManager(AddinRegistry registry): base (registry)
52  {
53  PluginRegistry = registry;
54 
55  }
56 
66  public bool InstallPlugin(int ndx, out Dictionary<string, object> result)
67  {
68  Dictionary<string, object> res = new Dictionary<string, object>();
69 
70  PackageCollection pack = new PackageCollection();
71  PackageCollection toUninstall;
72  DependencyCollection unresolved;
73 
74  IProgressStatus ps = new ConsoleProgressStatus(false);
75 
76  AddinRepositoryEntry[] available = GetSortedAvailbleAddins();
77 
78  if (ndx > (available.Length - 1))
79  {
80  MainConsole.Instance.Output("Selection out of range");
81  result = res;
82  return false;
83  }
84 
85  AddinRepositoryEntry aentry = available[ndx];
86 
87  Package p = Package.FromRepository(aentry);
88  pack.Add(p);
89 
90  ResolveDependencies(ps, pack, out toUninstall, out unresolved);
91 
92  // Attempt to install the plugin disabled
93  if (Install(ps, pack) == true)
94  {
95  MainConsole.Instance.Output("Ignore the following error...");
96  PluginRegistry.Update(ps);
97  Addin addin = PluginRegistry.GetAddin(aentry.Addin.Id);
98  PluginRegistry.DisableAddin(addin.Id);
99  addin.Enabled = false;
100 
101  MainConsole.Instance.Output("Installation Success");
102  ListInstalledAddins(out res);
103  result = res;
104  return true;
105  }
106  else
107  {
108  MainConsole.Instance.Output("Installation Failed");
109  result = res;
110  return false;
111  }
112  }
113 
114  // Remove plugin
121  public void UnInstall(int ndx)
122  {
123  Addin[] addins = GetSortedAddinList("RobustPlugin");
124 
125  if (ndx > (addins.Length -1))
126  {
127  MainConsole.Instance.Output("Selection out of range");
128  return;
129  }
130 
131  Addin addin = addins[ndx];
132  MainConsole.Instance.OutputFormat("Uninstalling plugin {0}", addin.Id);
133  AddinManager.Registry.DisableAddin(addin.Id);
134  addin.Enabled = false;
135  IProgressStatus ps = new ConsoleProgressStatus(false);
136  Uninstall(ps, addin.Id);
137  MainConsole.Instance.Output("Uninstall Success - restart to complete operation");
138  return;
139  }
140 
147  public string CheckInstalled()
148  {
149  return "CheckInstall";
150  }
151 
158  public void ListInstalledAddins(out Dictionary<string, object> result)
159  {
160  Dictionary<string, object> res = new Dictionary<string, object>();
161 
162  Addin[] addins = GetSortedAddinList("RobustPlugin");
163  if(addins.Count() < 1)
164  {
165  MainConsole.Instance.Output("Error!");
166  }
167  int count = 0;
168  foreach (Addin addin in addins)
169  {
170  Dictionary<string, object> r = new Dictionary<string, object>();
171  r["enabled"] = addin.Enabled == true ? true : false;
172  r["name"] = addin.LocalId;
173  r["version"] = addin.Version;
174 
175  res.Add(count.ToString(), r);
176 
177  count++;
178  }
179  result = res;
180  return;
181  }
182 
183  // List compatible plugins in registered repositories
190  public void ListAvailable(out Dictionary<string, object> result)
191  {
192  Dictionary<string, object> res = new Dictionary<string, object>();
193 
194  AddinRepositoryEntry[] addins = GetSortedAvailbleAddins();
195 
196  int count = 0;
197  foreach (AddinRepositoryEntry addin in addins)
198  {
199  Dictionary<string, object> r = new Dictionary<string, object>();
200  r["name"] = addin.Addin.Name;
201  r["version"] = addin.Addin.Version;
202  r["repository"] = addin.RepositoryName;
203 
204  res.Add(count.ToString(), r);
205  count++;
206  }
207  result = res;
208  return;
209  }
210 
211  // List available updates ** 1
215  public void ListUpdates()
216  {
217  IProgressStatus ps = new ConsoleProgressStatus(true);
218  Console.WriteLine ("Looking for updates...");
219  Repositories.UpdateAllRepositories (ps);
220  Console.WriteLine ("Available add-in updates:");
221 
222  AddinRepositoryEntry[] entries = Repositories.GetAvailableUpdates();
223 
224  foreach (AddinRepositoryEntry entry in entries)
225  {
226  Console.WriteLine(String.Format("{0}",entry.Addin.Id));
227  }
228  }
229 
230  // Sync to repositories
234  public string Update()
235  {
236  IProgressStatus ps = new ConsoleProgressStatus(true);
237  Repositories.UpdateAllRepositories(ps);
238  return "Update";
239  }
240 
241  // Register a repository
251  public bool AddRepository(string repo)
252  {
253  Repositories.RegisterRepository(null, repo, true);
254  PluginRegistry.Rebuild(null);
255 
256  return true;
257  }
258 
262  public void GetRepository()
263  {
264  Repositories.UpdateAllRepositories(new ConsoleProgressStatus(false));
265  }
266 
267  // Remove a repository from the list
274  public void RemoveRepository(string[] args)
275  {
276  AddinRepository[] reps = Repositories.GetRepositories();
277  Array.Sort(reps, (r1,r2) => r1.Title.CompareTo(r2.Title));
278  if (reps.Length == 0)
279  {
280  MainConsole.Instance.Output("No repositories have been registered.");
281  return;
282  }
283 
284  int n = Convert.ToInt16(args[2]);
285  if (n > (reps.Length -1))
286  {
287  MainConsole.Instance.Output("Selection out of range");
288  return;
289  }
290 
291  AddinRepository rep = reps[n];
292  Repositories.RemoveRepository(rep.Url);
293  return;
294  }
295 
296  // Enable repository
303  public void EnableRepository(string[] args)
304  {
305  AddinRepository[] reps = Repositories.GetRepositories();
306  Array.Sort(reps, (r1,r2) => r1.Title.CompareTo(r2.Title));
307  if (reps.Length == 0)
308  {
309  MainConsole.Instance.Output("No repositories have been registered.");
310  return;
311  }
312 
313  int n = Convert.ToInt16(args[2]);
314  if (n > (reps.Length -1))
315  {
316  MainConsole.Instance.Output("Selection out of range");
317  return;
318  }
319 
320  AddinRepository rep = reps[n];
321  Repositories.SetRepositoryEnabled(rep.Url, true);
322  return;
323  }
324 
325  // Disable a repository
332  public void DisableRepository(string[] args)
333  {
334  AddinRepository[] reps = Repositories.GetRepositories();
335  Array.Sort(reps, (r1,r2) => r1.Title.CompareTo(r2.Title));
336  if (reps.Length == 0)
337  {
338  MainConsole.Instance.Output("No repositories have been registered.");
339  return;
340  }
341 
342  int n = Convert.ToInt16(args[2]);
343  if (n > (reps.Length -1))
344  {
345  MainConsole.Instance.Output("Selection out of range");
346  return;
347  }
348 
349  AddinRepository rep = reps[n];
350  Repositories.SetRepositoryEnabled(rep.Url, false);
351  return;
352  }
353 
354  // List registered repositories
361  public void ListRepositories(out Dictionary<string, object> result)
362  {
363  Dictionary<string, object> res = new Dictionary<string, object>();
364  result = res;
365 
366  AddinRepository[] reps = GetSortedAddinRepo();
367  if (reps.Length == 0)
368  {
369  MainConsole.Instance.Output("No repositories have been registered.");
370  return;
371  }
372 
373  int count = 0;
374  foreach (AddinRepository rep in reps)
375  {
376  Dictionary<string, object> r = new Dictionary<string, object>();
377  r["enabled"] = rep.Enabled == true ? true : false;
378  r["name"] = rep.Name;
379  r["url"] = rep.Url;
380 
381  res.Add(count.ToString(), r);
382  count++;
383  }
384  return;
385  }
386 
390  public void UpdateRegistry()
391  {
392  PluginRegistry.Update();
393  }
394 
395  // Show plugin info
405  public bool AddinInfo(int ndx, out Dictionary<string, object> result)
406  {
407  Dictionary<string, object> res = new Dictionary<string, object>();
408  result = res;
409 
410  Addin[] addins = GetSortedAddinList("RobustPlugin");
411 
412  if (ndx > (addins.Length - 1))
413  {
414  MainConsole.Instance.Output("Selection out of range");
415  return false;
416  }
417  // author category description
418  Addin addin = addins[ndx];
419 
420  res["author"] = addin.Description.Author;
421  res["category"] = addin.Description.Category;
422  res["description"] = addin.Description.Description;
423  res["name"] = addin.Name;
424  res["url"] = addin.Description.Url;
425  res["file_name"] = addin.Description.FileName;
426 
427  result = res;
428  return true;
429  }
430 
431  // Disable a plugin
438  public void DisablePlugin(string[] args)
439  {
440  Addin[] addins = GetSortedAddinList("RobustPlugin");
441 
442  int n = Convert.ToInt16(args[2]);
443  if (n > (addins.Length -1))
444  {
445  MainConsole.Instance.Output("Selection out of range");
446  return;
447  }
448 
449  Addin addin = addins[n];
450  AddinManager.Registry.DisableAddin(addin.Id);
451  addin.Enabled = false;
452  return;
453  }
454 
455  // Enable plugin
462  public void EnablePlugin(string[] args)
463  {
464  Addin[] addins = GetSortedAddinList("RobustPlugin");
465 
466  int n = Convert.ToInt16(args[2]);
467  if (n > (addins.Length -1))
468  {
469  MainConsole.Instance.Output("Selection out of range");
470  return;
471  }
472 
473  Addin addin = addins[n];
474 
475  addin.Enabled = true;
476  AddinManager.Registry.EnableAddin(addin.Id);
477  // AddinManager.Registry.Update();
478  if(PluginRegistry.IsAddinEnabled(addin.Id))
479  {
480  ConsoleProgressStatus ps = new ConsoleProgressStatus(false);
481  if (!AddinManager.AddinEngine.IsAddinLoaded(addin.Id))
482  {
483  MainConsole.Instance.Output("Ignore the following error...");
484  AddinManager.Registry.Rebuild(ps);
485  AddinManager.AddinEngine.LoadAddin(ps, addin.Id);
486  }
487  }
488  else
489  {
490  MainConsole.Instance.OutputFormat("Not Enabled in this domain {0}", addin.Name);
491  }
492  return;
493  }
494 
495 
496 
497  #region Util
498  private void Testing()
499  {
500  Addin[] list = Registry.GetAddins();
501 
502  var addins = list.Where( a => a.Description.Category == "RobustPlugin");
503 
504  foreach (Addin addin in addins)
505  {
506  MainConsole.Instance.OutputFormat("Addin {0}", addin.Name);
507  }
508  }
509 
510  // These will let us deal with numbered lists instead
511  // of needing to type in the full ids
512  private AddinRepositoryEntry[] GetSortedAvailbleAddins()
513  {
514  ArrayList list = new ArrayList();
515  list.AddRange(Repositories.GetAvailableAddins());
516 
517  AddinRepositoryEntry[] addins = list.ToArray(typeof(AddinRepositoryEntry)) as AddinRepositoryEntry[];
518 
519  Array.Sort(addins,(r1,r2) => r1.Addin.Id.CompareTo(r2.Addin.Id));
520 
521  return addins;
522  }
523 
524  private AddinRepository[] GetSortedAddinRepo()
525  {
526  ArrayList list = new ArrayList();
527  list.AddRange(Repositories.GetRepositories());
528 
529  AddinRepository[] repos = list.ToArray(typeof(AddinRepository)) as AddinRepository[];
530  Array.Sort (repos,(r1,r2) => r1.Name.CompareTo(r2.Name));
531 
532  return repos;
533  }
534 
535  private Addin[] GetSortedAddinList(string category)
536  {
537 
538  ArrayList xlist = new ArrayList();
539  ArrayList list = new ArrayList();
540  try
541  {
542  list.AddRange(PluginRegistry.GetAddins());
543  }
544  catch (Exception)
545  {
546  Addin[] x = xlist.ToArray(typeof(Addin)) as Addin[];
547  return x;
548  }
549 
550  foreach (Addin addin in list)
551  {
552  if (addin.Description.Category == category)
553  xlist.Add(addin);
554  }
555 
556  Addin[] addins = xlist.ToArray(typeof(Addin)) as Addin[];
557  Array.Sort(addins,(r1,r2) => r1.Id.CompareTo(r2.Id));
558 
559  return addins;
560  }
561  #endregion Util
562  }
563 }
bool AddRepository(string repo)
Register a repository with our server.
void ListRepositories(out Dictionary< string, object > result)
Lists the repositories.
string Update()
Update this instance.
void ListUpdates()
Lists the updates.
void EnablePlugin(string[] args)
Enables the plugin.
void DisableRepository(string[] args)
Disables the repository.
void RemoveRepository(string[] args)
Removes the repository.
PluginManager(AddinRegistry registry)
string CheckInstalled()
Checks the installed.
bool AddinInfo(int ndx, out Dictionary< string, object > result)
Addins the info.
void GetRepository()
Gets the repository.
void EnableRepository(string[] args)
Enables the repository.
void DisablePlugin(string[] args)
Disables the plugin.
void ListAvailable(out Dictionary< string, object > result)
Lists the available.
void UnInstall(int ndx)
Uns the install.
bool InstallPlugin(int ndx, out Dictionary< string, object > result)
Installs the plugin.
void ListInstalledAddins(out Dictionary< string, object > result)
Lists the installed addins.
void UpdateRegistry()
Updates the registry.
Manager for registries and plugins