PluginManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Text;
  29. using System.Linq;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Collections.ObjectModel;
  33. using Mono.Addins;
  34. using Mono.Addins.Setup;
  35. using Mono.Addins.Description;
  36. using OpenSim.Framework;
  37. namespace OpenSim.Framework
  38. {
  39. /// <summary>
  40. /// Manager for registries and plugins
  41. /// </summary>
  42. public class PluginManager : SetupService
  43. {
  44. public AddinRegistry PluginRegistry;
  45. public PluginManager(AddinRegistry registry): base (registry)
  46. {
  47. PluginRegistry = registry;
  48. }
  49. /// <summary>
  50. /// Installs the plugin.
  51. /// </summary>
  52. /// <returns>
  53. /// The plugin.
  54. /// </returns>
  55. /// <param name='args'>
  56. /// Arguments.
  57. /// </param>
  58. public bool InstallPlugin(int ndx, out Dictionary<string, object> result)
  59. {
  60. Dictionary<string, object> res = new Dictionary<string, object>();
  61. PackageCollection pack = new PackageCollection();
  62. PackageCollection toUninstall;
  63. DependencyCollection unresolved;
  64. IProgressStatus ps = new ConsoleProgressStatus(false);
  65. AddinRepositoryEntry[] available = GetSortedAvailbleAddins();
  66. if (ndx > (available.Length - 1))
  67. {
  68. MainConsole.Instance.Output("Selection out of range");
  69. result = res;
  70. return false;
  71. }
  72. AddinRepositoryEntry aentry = available[ndx];
  73. Package p = Package.FromRepository(aentry);
  74. pack.Add(p);
  75. ResolveDependencies(ps, pack, out toUninstall, out unresolved);
  76. // Attempt to install the plugin disabled
  77. if (Install(ps, pack) == true)
  78. {
  79. MainConsole.Instance.Output("Ignore the following error...");
  80. PluginRegistry.Update(ps);
  81. Addin addin = PluginRegistry.GetAddin(aentry.Addin.Id);
  82. PluginRegistry.DisableAddin(addin.Id);
  83. addin.Enabled = false;
  84. MainConsole.Instance.Output("Installation Success");
  85. ListInstalledAddins(out res);
  86. result = res;
  87. return true;
  88. }
  89. else
  90. {
  91. MainConsole.Instance.Output("Installation Failed");
  92. result = res;
  93. return false;
  94. }
  95. }
  96. // Remove plugin
  97. /// <summary>
  98. /// Uns the install.
  99. /// </summary>
  100. /// <param name='args'>
  101. /// Arguments.
  102. /// </param>
  103. public void UnInstall(int ndx)
  104. {
  105. Addin[] addins = GetSortedAddinList("RobustPlugin");
  106. if (ndx > (addins.Length -1))
  107. {
  108. MainConsole.Instance.Output("Selection out of range");
  109. return;
  110. }
  111. Addin addin = addins[ndx];
  112. MainConsole.Instance.OutputFormat("Uninstalling plugin {0}", addin.Id);
  113. AddinManager.Registry.DisableAddin(addin.Id);
  114. addin.Enabled = false;
  115. IProgressStatus ps = new ConsoleProgressStatus(false);
  116. Uninstall(ps, addin.Id);
  117. MainConsole.Instance.Output("Uninstall Success - restart to complete operation");
  118. return;
  119. }
  120. /// <summary>
  121. /// Checks the installed.
  122. /// </summary>
  123. /// <returns>
  124. /// The installed.
  125. /// </returns>
  126. public string CheckInstalled()
  127. {
  128. return "CheckInstall";
  129. }
  130. /// <summary>
  131. /// Lists the installed addins.
  132. /// </summary>
  133. /// <param name='result'>
  134. /// Result.
  135. /// </param>
  136. public void ListInstalledAddins(out Dictionary<string, object> result)
  137. {
  138. Dictionary<string, object> res = new Dictionary<string, object>();
  139. Addin[] addins = GetSortedAddinList("RobustPlugin");
  140. if(addins.Count() < 1)
  141. {
  142. MainConsole.Instance.Output("Error!");
  143. }
  144. int count = 0;
  145. foreach (Addin addin in addins)
  146. {
  147. Dictionary<string, object> r = new Dictionary<string, object>();
  148. r["enabled"] = addin.Enabled == true ? true : false;
  149. r["name"] = addin.LocalId;
  150. r["version"] = addin.Version;
  151. res.Add(count.ToString(), r);
  152. count++;
  153. }
  154. result = res;
  155. return;
  156. }
  157. // List compatible plugins in registered repositories
  158. /// <summary>
  159. /// Lists the available.
  160. /// </summary>
  161. /// <param name='result'>
  162. /// Result.
  163. /// </param>
  164. public void ListAvailable(out Dictionary<string, object> result)
  165. {
  166. Dictionary<string, object> res = new Dictionary<string, object>();
  167. AddinRepositoryEntry[] addins = GetSortedAvailbleAddins();
  168. int count = 0;
  169. foreach (AddinRepositoryEntry addin in addins)
  170. {
  171. Dictionary<string, object> r = new Dictionary<string, object>();
  172. r["name"] = addin.Addin.Name;
  173. r["version"] = addin.Addin.Version;
  174. r["repository"] = addin.RepositoryName;
  175. res.Add(count.ToString(), r);
  176. count++;
  177. }
  178. result = res;
  179. return;
  180. }
  181. // List available updates ** 1
  182. /// <summary>
  183. /// Lists the updates.
  184. /// </summary>
  185. public void ListUpdates()
  186. {
  187. IProgressStatus ps = new ConsoleProgressStatus(true);
  188. Console.WriteLine ("Looking for updates...");
  189. Repositories.UpdateAllRepositories (ps);
  190. Console.WriteLine ("Available add-in updates:");
  191. AddinRepositoryEntry[] entries = Repositories.GetAvailableUpdates();
  192. foreach (AddinRepositoryEntry entry in entries)
  193. {
  194. Console.WriteLine(String.Format("{0}",entry.Addin.Id));
  195. }
  196. }
  197. // Sync to repositories
  198. /// <summary>
  199. /// Update this instance.
  200. /// </summary>
  201. public string Update()
  202. {
  203. IProgressStatus ps = new ConsoleProgressStatus(true);
  204. Repositories.UpdateAllRepositories(ps);
  205. return "Update";
  206. }
  207. // Register a repository
  208. /// <summary>
  209. /// Register a repository with our server.
  210. /// </summary>
  211. /// <returns>
  212. /// result of the action
  213. /// </returns>
  214. /// <param name='repo'>
  215. /// The URL of the repository we want to add
  216. /// </param>
  217. public bool AddRepository(string repo)
  218. {
  219. Repositories.RegisterRepository(null, repo, true);
  220. PluginRegistry.Rebuild(null);
  221. return true;
  222. }
  223. /// <summary>
  224. /// Gets the repository.
  225. /// </summary>
  226. public void GetRepository()
  227. {
  228. Repositories.UpdateAllRepositories(new ConsoleProgressStatus(false));
  229. }
  230. // Remove a repository from the list
  231. /// <summary>
  232. /// Removes the repository.
  233. /// </summary>
  234. /// <param name='args'>
  235. /// Arguments.
  236. /// </param>
  237. public void RemoveRepository(string[] args)
  238. {
  239. AddinRepository[] reps = Repositories.GetRepositories();
  240. Array.Sort(reps, (r1,r2) => r1.Title.CompareTo(r2.Title));
  241. if (reps.Length == 0)
  242. {
  243. MainConsole.Instance.Output("No repositories have been registered.");
  244. return;
  245. }
  246. int n = Convert.ToInt16(args[2]);
  247. if (n > (reps.Length -1))
  248. {
  249. MainConsole.Instance.Output("Selection out of range");
  250. return;
  251. }
  252. AddinRepository rep = reps[n];
  253. Repositories.RemoveRepository(rep.Url);
  254. return;
  255. }
  256. // Enable repository
  257. /// <summary>
  258. /// Enables the repository.
  259. /// </summary>
  260. /// <param name='args'>
  261. /// Arguments.
  262. /// </param>
  263. public void EnableRepository(string[] args)
  264. {
  265. AddinRepository[] reps = Repositories.GetRepositories();
  266. Array.Sort(reps, (r1,r2) => r1.Title.CompareTo(r2.Title));
  267. if (reps.Length == 0)
  268. {
  269. MainConsole.Instance.Output("No repositories have been registered.");
  270. return;
  271. }
  272. int n = Convert.ToInt16(args[2]);
  273. if (n > (reps.Length -1))
  274. {
  275. MainConsole.Instance.Output("Selection out of range");
  276. return;
  277. }
  278. AddinRepository rep = reps[n];
  279. Repositories.SetRepositoryEnabled(rep.Url, true);
  280. return;
  281. }
  282. // Disable a repository
  283. /// <summary>
  284. /// Disables the repository.
  285. /// </summary>
  286. /// <param name='args'>
  287. /// Arguments.
  288. /// </param>
  289. public void DisableRepository(string[] args)
  290. {
  291. AddinRepository[] reps = Repositories.GetRepositories();
  292. Array.Sort(reps, (r1,r2) => r1.Title.CompareTo(r2.Title));
  293. if (reps.Length == 0)
  294. {
  295. MainConsole.Instance.Output("No repositories have been registered.");
  296. return;
  297. }
  298. int n = Convert.ToInt16(args[2]);
  299. if (n > (reps.Length -1))
  300. {
  301. MainConsole.Instance.Output("Selection out of range");
  302. return;
  303. }
  304. AddinRepository rep = reps[n];
  305. Repositories.SetRepositoryEnabled(rep.Url, false);
  306. return;
  307. }
  308. // List registered repositories
  309. /// <summary>
  310. /// Lists the repositories.
  311. /// </summary>
  312. /// <param name='result'>
  313. /// Result.
  314. /// </param>
  315. public void ListRepositories(out Dictionary<string, object> result)
  316. {
  317. Dictionary<string, object> res = new Dictionary<string, object>();
  318. result = res;
  319. AddinRepository[] reps = GetSortedAddinRepo();
  320. if (reps.Length == 0)
  321. {
  322. MainConsole.Instance.Output("No repositories have been registered.");
  323. return;
  324. }
  325. int count = 0;
  326. foreach (AddinRepository rep in reps)
  327. {
  328. Dictionary<string, object> r = new Dictionary<string, object>();
  329. r["enabled"] = rep.Enabled == true ? true : false;
  330. r["name"] = rep.Name;
  331. r["url"] = rep.Url;
  332. res.Add(count.ToString(), r);
  333. count++;
  334. }
  335. return;
  336. }
  337. /// <summary>
  338. /// Updates the registry.
  339. /// </summary>
  340. public void UpdateRegistry()
  341. {
  342. PluginRegistry.Update();
  343. }
  344. // Show plugin info
  345. /// <summary>
  346. /// Addins the info.
  347. /// </summary>
  348. /// <returns>
  349. /// The info.
  350. /// </returns>
  351. /// <param name='args'>
  352. /// Arguments.
  353. /// </param>
  354. public bool AddinInfo(int ndx, out Dictionary<string, object> result)
  355. {
  356. Dictionary<string, object> res = new Dictionary<string, object>();
  357. result = res;
  358. Addin[] addins = GetSortedAddinList("RobustPlugin");
  359. if (ndx > (addins.Length - 1))
  360. {
  361. MainConsole.Instance.Output("Selection out of range");
  362. return false;
  363. }
  364. // author category description
  365. Addin addin = addins[ndx];
  366. res["author"] = addin.Description.Author;
  367. res["category"] = addin.Description.Category;
  368. res["description"] = addin.Description.Description;
  369. res["name"] = addin.Name;
  370. res["url"] = addin.Description.Url;
  371. res["file_name"] = addin.Description.FileName;
  372. result = res;
  373. return true;
  374. }
  375. // Disable a plugin
  376. /// <summary>
  377. /// Disables the plugin.
  378. /// </summary>
  379. /// <param name='args'>
  380. /// Arguments.
  381. /// </param>
  382. public void DisablePlugin(string[] args)
  383. {
  384. Addin[] addins = GetSortedAddinList("RobustPlugin");
  385. int n = Convert.ToInt16(args[2]);
  386. if (n > (addins.Length -1))
  387. {
  388. MainConsole.Instance.Output("Selection out of range");
  389. return;
  390. }
  391. Addin addin = addins[n];
  392. AddinManager.Registry.DisableAddin(addin.Id);
  393. addin.Enabled = false;
  394. return;
  395. }
  396. // Enable plugin
  397. /// <summary>
  398. /// Enables the plugin.
  399. /// </summary>
  400. /// <param name='args'>
  401. /// Arguments.
  402. /// </param>
  403. public void EnablePlugin(string[] args)
  404. {
  405. Addin[] addins = GetSortedAddinList("RobustPlugin");
  406. int n = Convert.ToInt16(args[2]);
  407. if (n > (addins.Length -1))
  408. {
  409. MainConsole.Instance.Output("Selection out of range");
  410. return;
  411. }
  412. Addin addin = addins[n];
  413. addin.Enabled = true;
  414. AddinManager.Registry.EnableAddin(addin.Id);
  415. // AddinManager.Registry.Update();
  416. if(PluginRegistry.IsAddinEnabled(addin.Id))
  417. {
  418. ConsoleProgressStatus ps = new ConsoleProgressStatus(false);
  419. if (!AddinManager.AddinEngine.IsAddinLoaded(addin.Id))
  420. {
  421. MainConsole.Instance.Output("Ignore the following error...");
  422. AddinManager.Registry.Rebuild(ps);
  423. AddinManager.AddinEngine.LoadAddin(ps, addin.Id);
  424. }
  425. }
  426. else
  427. {
  428. MainConsole.Instance.OutputFormat("Not Enabled in this domain {0}", addin.Name);
  429. }
  430. return;
  431. }
  432. #region Util
  433. private void Testing()
  434. {
  435. Addin[] list = Registry.GetAddins();
  436. var addins = list.Where( a => a.Description.Category == "RobustPlugin");
  437. foreach (Addin addin in addins)
  438. {
  439. MainConsole.Instance.OutputFormat("Addin {0}", addin.Name);
  440. }
  441. }
  442. // These will let us deal with numbered lists instead
  443. // of needing to type in the full ids
  444. private AddinRepositoryEntry[] GetSortedAvailbleAddins()
  445. {
  446. ArrayList list = new ArrayList();
  447. list.AddRange(Repositories.GetAvailableAddins());
  448. AddinRepositoryEntry[] addins = list.ToArray(typeof(AddinRepositoryEntry)) as AddinRepositoryEntry[];
  449. Array.Sort(addins,(r1,r2) => r1.Addin.Id.CompareTo(r2.Addin.Id));
  450. return addins;
  451. }
  452. private AddinRepository[] GetSortedAddinRepo()
  453. {
  454. ArrayList list = new ArrayList();
  455. list.AddRange(Repositories.GetRepositories());
  456. AddinRepository[] repos = list.ToArray(typeof(AddinRepository)) as AddinRepository[];
  457. Array.Sort (repos,(r1,r2) => r1.Name.CompareTo(r2.Name));
  458. return repos;
  459. }
  460. private Addin[] GetSortedAddinList(string category)
  461. {
  462. ArrayList xlist = new ArrayList();
  463. ArrayList list = new ArrayList();
  464. try
  465. {
  466. list.AddRange(PluginRegistry.GetAddins());
  467. }
  468. catch (Exception)
  469. {
  470. Addin[] x = xlist.ToArray(typeof(Addin)) as Addin[];
  471. return x;
  472. }
  473. foreach (Addin addin in list)
  474. {
  475. if (addin.Description.Category == category)
  476. xlist.Add(addin);
  477. }
  478. Addin[] addins = xlist.ToArray(typeof(Addin)) as Addin[];
  479. Array.Sort(addins,(r1,r2) => r1.Id.CompareTo(r2.Id));
  480. return addins;
  481. }
  482. #endregion Util
  483. }
  484. }