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. bool found = false;
  192. AddinRepositoryEntry[] entries = Repositories.GetAvailableUpdates();
  193. foreach (AddinRepositoryEntry entry in entries)
  194. {
  195. Console.WriteLine(String.Format("{0}",entry.Addin.Id));
  196. }
  197. }
  198. // Sync to repositories
  199. /// <summary>
  200. /// Update this instance.
  201. /// </summary>
  202. public string Update()
  203. {
  204. IProgressStatus ps = new ConsoleProgressStatus(true);
  205. Repositories.UpdateAllRepositories(ps);
  206. return "Update";
  207. }
  208. // Register a repository
  209. /// <summary>
  210. /// Register a repository with our server.
  211. /// </summary>
  212. /// <returns>
  213. /// result of the action
  214. /// </returns>
  215. /// <param name='repo'>
  216. /// The URL of the repository we want to add
  217. /// </param>
  218. public bool AddRepository(string repo)
  219. {
  220. Repositories.RegisterRepository(null, repo, true);
  221. PluginRegistry.Rebuild(null);
  222. return true;
  223. }
  224. /// <summary>
  225. /// Gets the repository.
  226. /// </summary>
  227. public void GetRepository()
  228. {
  229. Repositories.UpdateAllRepositories(new ConsoleProgressStatus(false));
  230. }
  231. // Remove a repository from the list
  232. /// <summary>
  233. /// Removes the repository.
  234. /// </summary>
  235. /// <param name='args'>
  236. /// Arguments.
  237. /// </param>
  238. public void RemoveRepository(string[] args)
  239. {
  240. AddinRepository[] reps = Repositories.GetRepositories();
  241. Array.Sort(reps, (r1,r2) => r1.Title.CompareTo(r2.Title));
  242. if (reps.Length == 0)
  243. {
  244. MainConsole.Instance.Output("No repositories have been registered.");
  245. return;
  246. }
  247. int n = Convert.ToInt16(args[2]);
  248. if (n > (reps.Length -1))
  249. {
  250. MainConsole.Instance.Output("Selection out of range");
  251. return;
  252. }
  253. AddinRepository rep = reps[n];
  254. Repositories.RemoveRepository(rep.Url);
  255. return;
  256. }
  257. // Enable repository
  258. /// <summary>
  259. /// Enables the repository.
  260. /// </summary>
  261. /// <param name='args'>
  262. /// Arguments.
  263. /// </param>
  264. public void EnableRepository(string[] args)
  265. {
  266. AddinRepository[] reps = Repositories.GetRepositories();
  267. Array.Sort(reps, (r1,r2) => r1.Title.CompareTo(r2.Title));
  268. if (reps.Length == 0)
  269. {
  270. MainConsole.Instance.Output("No repositories have been registered.");
  271. return;
  272. }
  273. int n = Convert.ToInt16(args[2]);
  274. if (n > (reps.Length -1))
  275. {
  276. MainConsole.Instance.Output("Selection out of range");
  277. return;
  278. }
  279. AddinRepository rep = reps[n];
  280. Repositories.SetRepositoryEnabled(rep.Url, true);
  281. return;
  282. }
  283. // Disable a repository
  284. /// <summary>
  285. /// Disables the repository.
  286. /// </summary>
  287. /// <param name='args'>
  288. /// Arguments.
  289. /// </param>
  290. public void DisableRepository(string[] args)
  291. {
  292. AddinRepository[] reps = Repositories.GetRepositories();
  293. Array.Sort(reps, (r1,r2) => r1.Title.CompareTo(r2.Title));
  294. if (reps.Length == 0)
  295. {
  296. MainConsole.Instance.Output("No repositories have been registered.");
  297. return;
  298. }
  299. int n = Convert.ToInt16(args[2]);
  300. if (n > (reps.Length -1))
  301. {
  302. MainConsole.Instance.Output("Selection out of range");
  303. return;
  304. }
  305. AddinRepository rep = reps[n];
  306. Repositories.SetRepositoryEnabled(rep.Url, false);
  307. return;
  308. }
  309. // List registered repositories
  310. /// <summary>
  311. /// Lists the repositories.
  312. /// </summary>
  313. /// <param name='result'>
  314. /// Result.
  315. /// </param>
  316. public void ListRepositories(out Dictionary<string, object> result)
  317. {
  318. Dictionary<string, object> res = new Dictionary<string, object>();
  319. result = res;
  320. AddinRepository[] reps = GetSortedAddinRepo();
  321. if (reps.Length == 0)
  322. {
  323. MainConsole.Instance.Output("No repositories have been registered.");
  324. return;
  325. }
  326. int count = 0;
  327. foreach (AddinRepository rep in reps)
  328. {
  329. Dictionary<string, object> r = new Dictionary<string, object>();
  330. r["enabled"] = rep.Enabled == true ? true : false;
  331. r["name"] = rep.Name;
  332. r["url"] = rep.Url;
  333. res.Add(count.ToString(), r);
  334. count++;
  335. }
  336. return;
  337. }
  338. /// <summary>
  339. /// Updates the registry.
  340. /// </summary>
  341. public void UpdateRegistry()
  342. {
  343. PluginRegistry.Update();
  344. }
  345. // Show plugin info
  346. /// <summary>
  347. /// Addins the info.
  348. /// </summary>
  349. /// <returns>
  350. /// The info.
  351. /// </returns>
  352. /// <param name='args'>
  353. /// Arguments.
  354. /// </param>
  355. public bool AddinInfo(int ndx, out Dictionary<string, object> result)
  356. {
  357. Dictionary<string, object> res = new Dictionary<string, object>();
  358. result = res;
  359. Addin[] addins = GetSortedAddinList("RobustPlugin");
  360. if (ndx > (addins.Length - 1))
  361. {
  362. MainConsole.Instance.Output("Selection out of range");
  363. return false;
  364. }
  365. // author category description
  366. Addin addin = addins[ndx];
  367. res["author"] = addin.Description.Author;
  368. res["category"] = addin.Description.Category;
  369. res["description"] = addin.Description.Description;
  370. res["name"] = addin.Name;
  371. res["url"] = addin.Description.Url;
  372. res["file_name"] = addin.Description.FileName;
  373. result = res;
  374. return true;
  375. }
  376. // Disable a plugin
  377. /// <summary>
  378. /// Disables the plugin.
  379. /// </summary>
  380. /// <param name='args'>
  381. /// Arguments.
  382. /// </param>
  383. public void DisablePlugin(string[] args)
  384. {
  385. Addin[] addins = GetSortedAddinList("RobustPlugin");
  386. int n = Convert.ToInt16(args[2]);
  387. if (n > (addins.Length -1))
  388. {
  389. MainConsole.Instance.Output("Selection out of range");
  390. return;
  391. }
  392. Addin addin = addins[n];
  393. AddinManager.Registry.DisableAddin(addin.Id);
  394. addin.Enabled = false;
  395. return;
  396. }
  397. // Enable plugin
  398. /// <summary>
  399. /// Enables the plugin.
  400. /// </summary>
  401. /// <param name='args'>
  402. /// Arguments.
  403. /// </param>
  404. public void EnablePlugin(string[] args)
  405. {
  406. Addin[] addins = GetSortedAddinList("RobustPlugin");
  407. int n = Convert.ToInt16(args[2]);
  408. if (n > (addins.Length -1))
  409. {
  410. MainConsole.Instance.Output("Selection out of range");
  411. return;
  412. }
  413. Addin addin = addins[n];
  414. addin.Enabled = true;
  415. AddinManager.Registry.EnableAddin(addin.Id);
  416. // AddinManager.Registry.Update();
  417. if(PluginRegistry.IsAddinEnabled(addin.Id))
  418. {
  419. ConsoleProgressStatus ps = new ConsoleProgressStatus(false);
  420. if (!AddinManager.AddinEngine.IsAddinLoaded(addin.Id))
  421. {
  422. MainConsole.Instance.Output("Ignore the following error...");
  423. AddinManager.Registry.Rebuild(ps);
  424. AddinManager.AddinEngine.LoadAddin(ps, addin.Id);
  425. }
  426. }
  427. else
  428. {
  429. MainConsole.Instance.OutputFormat("Not Enabled in this domain {0}", addin.Name);
  430. }
  431. return;
  432. }
  433. #region Util
  434. private void Testing()
  435. {
  436. Addin[] list = Registry.GetAddins();
  437. var addins = list.Where( a => a.Description.Category == "RobustPlugin");
  438. foreach (Addin addin in addins)
  439. {
  440. MainConsole.Instance.OutputFormat("Addin {0}", addin.Name);
  441. }
  442. }
  443. // These will let us deal with numbered lists instead
  444. // of needing to type in the full ids
  445. private AddinRepositoryEntry[] GetSortedAvailbleAddins()
  446. {
  447. ArrayList list = new ArrayList();
  448. list.AddRange(Repositories.GetAvailableAddins());
  449. AddinRepositoryEntry[] addins = list.ToArray(typeof(AddinRepositoryEntry)) as AddinRepositoryEntry[];
  450. Array.Sort(addins,(r1,r2) => r1.Addin.Id.CompareTo(r2.Addin.Id));
  451. return addins;
  452. }
  453. private AddinRepository[] GetSortedAddinRepo()
  454. {
  455. ArrayList list = new ArrayList();
  456. list.AddRange(Repositories.GetRepositories());
  457. AddinRepository[] repos = list.ToArray(typeof(AddinRepository)) as AddinRepository[];
  458. Array.Sort (repos,(r1,r2) => r1.Name.CompareTo(r2.Name));
  459. return repos;
  460. }
  461. private Addin[] GetSortedAddinList(string category)
  462. {
  463. ArrayList xlist = new ArrayList();
  464. ArrayList list = new ArrayList();
  465. try
  466. {
  467. list.AddRange(PluginRegistry.GetAddins());
  468. }
  469. catch(Exception e)
  470. {
  471. Addin[] x = xlist.ToArray(typeof(Addin)) as Addin[];
  472. return x;
  473. }
  474. foreach (Addin addin in list)
  475. {
  476. if (addin.Description.Category == category)
  477. xlist.Add(addin);
  478. }
  479. Addin[] addins = xlist.ToArray(typeof(Addin)) as Addin[];
  480. Array.Sort(addins,(r1,r2) => r1.Id.CompareTo(r2.Id));
  481. return addins;
  482. }
  483. #endregion Util
  484. }
  485. }